clang 19.0.0git
Source.h
Go to the documentation of this file.
1//===--- Source.h - Source location provider for the VM --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Defines a program which organises and links multiple bytecode functions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_SOURCE_H
14#define LLVM_CLANG_AST_INTERP_SOURCE_H
15
16#include "PrimType.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/Stmt.h"
19#include "llvm/ADT/PointerUnion.h"
20#include "llvm/Support/Endian.h"
21
22namespace clang {
23class Expr;
24class SourceLocation;
25class SourceRange;
26namespace interp {
27class Function;
28
29/// Pointer into the code segment.
30class CodePtr final {
31public:
32 CodePtr() : Ptr(nullptr) {}
33
34 CodePtr &operator+=(int32_t Offset) {
35 Ptr += Offset;
36 return *this;
37 }
38
39 int32_t operator-(const CodePtr &RHS) const {
40 assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
41 return Ptr - RHS.Ptr;
42 }
43
44 CodePtr operator-(size_t RHS) const {
45 assert(Ptr != nullptr && "Invalid code pointer");
46 return CodePtr(Ptr - RHS);
47 }
48
49 bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
50 const std::byte *operator*() const { return Ptr; }
51
52 operator bool() const { return Ptr; }
53
54 /// Reads data and advances the pointer.
55 template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
56 assert(aligned(Ptr));
57 using namespace llvm::support;
58 T Value = endian::read<T, llvm::endianness::native>(Ptr);
59 Ptr += align(sizeof(T));
60 return Value;
61 }
62
63private:
64 friend class Function;
65 /// Constructor used by Function to generate pointers.
66 CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
67 /// Pointer into the code owned by a function.
68 const std::byte *Ptr;
69};
70
71/// Describes the statement/declaration an opcode was generated from.
72class SourceInfo final {
73public:
75 SourceInfo(const Stmt *E) : Source(E) {}
76 SourceInfo(const Decl *D) : Source(D) {}
77
78 SourceLocation getLoc() const;
79 SourceRange getRange() const;
80
81 const Stmt *asStmt() const { return Source.dyn_cast<const Stmt *>(); }
82 const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
83 const Expr *asExpr() const;
84
85 operator bool() const { return !Source.isNull(); }
86
87private:
88 llvm::PointerUnion<const Decl *, const Stmt *> Source;
89};
90
91using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
92
93/// Interface for classes which map locations to sources.
95public:
96 virtual ~SourceMapper() {}
97
98 /// Returns source information for a given PC in a function.
99 virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0;
100
101 /// Returns the expression if an opcode belongs to one, null otherwise.
102 const Expr *getExpr(const Function *F, CodePtr PC) const;
103 /// Returns the location from which an opcode originates.
104 SourceLocation getLocation(const Function *F, CodePtr PC) const;
105 SourceRange getRange(const Function *F, CodePtr PC) const;
106};
107
108} // namespace interp
109} // namespace clang
110
111#endif
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
This represents one expression.
Definition: Expr.h:110
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition: Stmt.h:84
Pointer into the code segment.
Definition: Source.h:30
const std::byte * operator*() const
Definition: Source.h:50
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition: Source.h:55
bool operator!=(const CodePtr &RHS) const
Definition: Source.h:49
CodePtr & operator+=(int32_t Offset)
Definition: Source.h:34
int32_t operator-(const CodePtr &RHS) const
Definition: Source.h:39
CodePtr operator-(size_t RHS) const
Definition: Source.h:44
Bytecode function.
Definition: Function.h:77
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
SourceInfo(const Decl *D)
Definition: Source.h:76
const Decl * asDecl() const
Definition: Source.h:82
const Expr * asExpr() const
Definition: Source.cpp:35
const Stmt * asStmt() const
Definition: Source.h:81
SourceLocation getLoc() const
Definition: Source.cpp:15
SourceRange getRange() const
Definition: Source.cpp:25
SourceInfo(const Stmt *E)
Definition: Source.h:75
Interface for classes which map locations to sources.
Definition: Source.h:94
virtual SourceInfo getSource(const Function *F, CodePtr PC) const =0
Returns source information for a given PC in a function.
SourceLocation getLocation(const Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition: Source.cpp:47
SourceRange getRange(const Function *F, CodePtr PC) const
Definition: Source.cpp:51
const Expr * getExpr(const Function *F, CodePtr PC) const
Returns the expression if an opcode belongs to one, null otherwise.
Definition: Source.cpp:41
constexpr bool aligned(uintptr_t Value)
Definition: PrimType.h:103
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:91
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition: PrimType.h:99
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
#define bool
Definition: stdbool.h:20