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/Decl.h"
18 #include "clang/AST/Stmt.h"
19 #include "llvm/Support/Endian.h"
20 
21 namespace clang {
22 namespace interp {
23 class Function;
24 
25 /// Pointer into the code segment.
26 class CodePtr final {
27 public:
28   CodePtr() : Ptr(nullptr) {}
29 
30   CodePtr &operator+=(int32_t Offset) {
31     Ptr += Offset;
32     return *this;
33   }
34 
35   int32_t operator-(const CodePtr &RHS) const {
36     assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
37     return Ptr - RHS.Ptr;
38   }
39 
40   CodePtr operator-(size_t RHS) const {
41     assert(Ptr != nullptr && "Invalid code pointer");
42     return CodePtr(Ptr - RHS);
43   }
44 
45   bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
46 
47   operator bool() const { return Ptr; }
48 
49   /// Reads data and advances the pointer.
50   template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
51     assert(aligned(Ptr));
52     using namespace llvm::support;
53     T Value = endian::read<T, endianness::native, 1>(Ptr);
54     Ptr += align(sizeof(T));
55     return Value;
56   }
57 
58 private:
59   friend class Function;
60   /// Constructor used by Function to generate pointers.
61   CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
62   /// Pointer into the code owned by a function.
63   const std::byte *Ptr;
64 };
65 
66 /// Describes the statement/declaration an opcode was generated from.
67 class SourceInfo final {
68 public:
69   SourceInfo() {}
70   SourceInfo(const Stmt *E) : Source(E) {}
71   SourceInfo(const Decl *D) : Source(D) {}
72 
73   SourceLocation getLoc() const;
74 
75   const Stmt *asStmt() const { return Source.dyn_cast<const Stmt *>(); }
76   const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
77   const Expr *asExpr() const;
78 
79   operator bool() const { return !Source.isNull(); }
80 
81 private:
82   llvm::PointerUnion<const Decl *, const Stmt *> Source;
83 };
84 
85 using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
86 
87 /// Interface for classes which map locations to sources.
88 class SourceMapper {
89 public:
90   virtual ~SourceMapper() {}
91 
92   /// Returns source information for a given PC in a function.
93   virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0;
94 
95   /// Returns the expression if an opcode belongs to one, null otherwise.
96   const Expr *getExpr(const Function *F, CodePtr PC) const;
97   /// Returns the location from which an opcode originates.
98   SourceLocation getLocation(const Function *F, CodePtr PC) const;
99 };
100 
101 } // namespace interp
102 } // namespace clang
103 
104 #endif
105