/src/llvm-project/clang/lib/AST/Interp/Source.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
22 | | namespace clang { |
23 | | class Expr; |
24 | | class SourceLocation; |
25 | | class SourceRange; |
26 | | namespace interp { |
27 | | class Function; |
28 | | |
29 | | /// Pointer into the code segment. |
30 | | class CodePtr final { |
31 | | public: |
32 | 0 | CodePtr() : Ptr(nullptr) {} |
33 | | |
34 | 0 | CodePtr &operator+=(int32_t Offset) { |
35 | 0 | Ptr += Offset; |
36 | 0 | return *this; |
37 | 0 | } |
38 | | |
39 | 0 | int32_t operator-(const CodePtr &RHS) const { |
40 | 0 | assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer"); |
41 | 0 | return Ptr - RHS.Ptr; |
42 | 0 | } |
43 | | |
44 | 0 | CodePtr operator-(size_t RHS) const { |
45 | 0 | assert(Ptr != nullptr && "Invalid code pointer"); |
46 | 0 | return CodePtr(Ptr - RHS); |
47 | 0 | } |
48 | | |
49 | 0 | bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; } |
50 | 0 | const std::byte *operator*() const { return Ptr; } |
51 | | |
52 | 0 | operator bool() const { return Ptr; } |
53 | | |
54 | | /// Reads data and advances the pointer. |
55 | 0 | template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() { |
56 | 0 | assert(aligned(Ptr)); |
57 | 0 | using namespace llvm::support; |
58 | 0 | T Value = endian::read<T, llvm::endianness::native>(Ptr); |
59 | 0 | Ptr += align(sizeof(T)); |
60 | 0 | return Value; |
61 | 0 | } Unexecuted instantiation: _ZN5clang6interp7CodePtr4readINS0_6OpcodeEEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES6_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIN4llvm12RoundingModeEEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES7_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIjEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIbEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIaEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIsEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIiEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIlEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readIhEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readItEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readImEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES5_E4typeEv Unexecuted instantiation: _ZN5clang6interp7CodePtr4readINS0_8CastKindEEENSt3__19enable_ifIXntsr3std10is_pointerIT_EE5valueES6_E4typeEv |
62 | | |
63 | | private: |
64 | | friend class Function; |
65 | | /// Constructor used by Function to generate pointers. |
66 | 0 | 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. |
72 | | class SourceInfo final { |
73 | | public: |
74 | 0 | SourceInfo() {} |
75 | 0 | SourceInfo(const Stmt *E) : Source(E) {} |
76 | 0 | SourceInfo(const Decl *D) : Source(D) {} |
77 | | |
78 | | SourceLocation getLoc() const; |
79 | | SourceRange getRange() const; |
80 | | |
81 | 0 | const Stmt *asStmt() const { return Source.dyn_cast<const Stmt *>(); } |
82 | 0 | const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); } |
83 | | const Expr *asExpr() const; |
84 | | |
85 | 0 | operator bool() const { return !Source.isNull(); } |
86 | | |
87 | | private: |
88 | | llvm::PointerUnion<const Decl *, const Stmt *> Source; |
89 | | }; |
90 | | |
91 | | using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>; |
92 | | |
93 | | /// Interface for classes which map locations to sources. |
94 | | class SourceMapper { |
95 | | public: |
96 | 0 | 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 |