Source.h 3.3 KB

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