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