Source.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
  39. using namespace llvm::support;
  40. T Value = endian::read<T, endianness::native, 1>(Ptr);
  41. Ptr += sizeof(T);
  42. return Value;
  43. }
  44. private:
  45. /// Constructor used by Function to generate pointers.
  46. CodePtr(const char *Ptr) : Ptr(Ptr) {}
  47. private:
  48. friend class Function;
  49. /// Pointer into the code owned by a function.
  50. const char *Ptr;
  51. };
  52. /// Describes the statement/declaration an opcode was generated from.
  53. class SourceInfo {
  54. public:
  55. SourceInfo() {}
  56. SourceInfo(const Stmt *E) : Source(E) {}
  57. SourceInfo(const Decl *D) : Source(D) {}
  58. SourceLocation getLoc() const;
  59. const Stmt *asStmt() const { return Source.dyn_cast<const Stmt *>(); }
  60. const Decl *asDecl() const { return Source.dyn_cast<const Decl *>(); }
  61. const Expr *asExpr() const;
  62. operator bool() const { return !Source.isNull(); }
  63. private:
  64. llvm::PointerUnion<const Decl *, const Stmt *> Source;
  65. };
  66. using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
  67. /// Interface for classes which map locations to sources.
  68. class SourceMapper {
  69. public:
  70. virtual ~SourceMapper() {}
  71. /// Returns source information for a given PC in a function.
  72. virtual SourceInfo getSource(Function *F, CodePtr PC) const = 0;
  73. /// Returns the expression if an opcode belongs to one, null otherwise.
  74. const Expr *getExpr(Function *F, CodePtr PC) const;
  75. /// Returns the location from which an opcode originates.
  76. SourceLocation getLocation(Function *F, CodePtr PC) const;
  77. };
  78. } // namespace interp
  79. } // namespace clang
  80. #endif