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 CodePtr() = default; 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 CodePtr operator+(ssize_t RHS) const { 49 assert(Ptr != nullptr && "Invalid code pointer"); 50 return CodePtr(Ptr + RHS); 51 } 52 53 bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; } 54 const std::byte *operator*() const { return Ptr; } 55 explicit operator bool() const { return Ptr; } 56 bool operator<=(const CodePtr &RHS) const { return Ptr <= RHS.Ptr; } 57 bool operator>=(const CodePtr &RHS) const { return Ptr >= RHS.Ptr; } 58 59 /// Reads data and advances the pointer. 60 template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() { 61 assert(aligned(Ptr)); 62 using namespace llvm::support; 63 T Value = endian::read<T, llvm::endianness::native>(Ptr); 64 Ptr += align(sizeof(T)); 65 return Value; 66 } 67 68 private: 69 friend class Function; 70 /// Constructor used by Function to generate pointers. 71 CodePtr(const std::byte *Ptr) : Ptr(Ptr) {} 72 /// Pointer into the code owned by a function. 73 const std::byte *Ptr = nullptr; 74 }; 75 76 /// Describes the statement/declaration an opcode was generated from. 77 class SourceInfo final { 78 public: 79 SourceInfo() {} 80 SourceInfo(const Stmt *E) : Source(E) {} 81 SourceInfo(const Decl *D) : Source(D) {} 82 83 SourceLocation getLoc() const; 84 SourceRange getRange() const; 85 86 const Stmt *asStmt() const { 87 return dyn_cast_if_present<const Stmt *>(Source); 88 } 89 const Decl *asDecl() const { 90 return dyn_cast_if_present<const Decl *>(Source); 91 } 92 const Expr *asExpr() const; 93 94 operator bool() const { return !Source.isNull(); } 95 96 private: 97 llvm::PointerUnion<const Decl *, const Stmt *> Source; 98 }; 99 100 using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>; 101 102 /// Interface for classes which map locations to sources. 103 class SourceMapper { 104 public: 105 virtual ~SourceMapper() {} 106 107 /// Returns source information for a given PC in a function. 108 virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0; 109 110 /// Returns the expression if an opcode belongs to one, null otherwise. 111 const Expr *getExpr(const Function *F, CodePtr PC) const; 112 /// Returns the location from which an opcode originates. 113 SourceLocation getLocation(const Function *F, CodePtr PC) const; 114 SourceRange getRange(const Function *F, CodePtr PC) const; 115 }; 116 117 } // namespace interp 118 } // namespace clang 119 120 #endif 121