clang  14.0.0git
Source.h
Go to the documentation of this file.
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 
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:
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
clang::interp::CodePtr::CodePtr
CodePtr()
Definition: Source.h:27
clang::interp::CodePtr::operator-
CodePtr operator-(size_t RHS) const
Definition: Source.h:39
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::interp::CodePtr::operator-
int32_t operator-(const CodePtr &RHS) const
Definition: Source.h:34
Decl.h
Offset
unsigned Offset
Definition: Format.cpp:2335
clang::interp::SourceInfo::SourceInfo
SourceInfo(const Decl *D)
Definition: Source.h:70
bool
#define bool
Definition: stdbool.h:15
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:66
clang::interp::SourceInfo::asStmt
const Stmt * asStmt() const
Definition: Source.h:74
clang::interp::CodePtr::operator!=
bool operator!=(const CodePtr &RHS) const
Definition: Source.h:44
clang::interp::SourceInfo::asDecl
const Decl * asDecl() const
Definition: Source.h:75
clang::interp::CodePtr::read
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition: Source.h:47
Value
Value
Definition: UninitializedValues.cpp:102
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::interp::SourceMap
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition: Source.h:84
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang
Definition: CalledOnceCheck.h:17
clang::interp::Function
Bytecode function.
Definition: Function.h:59
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::interp::SourceInfo::SourceInfo
SourceInfo(const Stmt *E)
Definition: Source.h:69
clang::index::SymbolKind::Function
@ Function
Stmt.h
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::interp::CodePtr::operator+=
CodePtr & operator+=(int32_t Offset)
Definition: Source.h:29
clang::interp::SourceInfo::SourceInfo
SourceInfo()
Definition: Source.h:68
clang::interp::SourceMapper::~SourceMapper
virtual ~SourceMapper()
Definition: Source.h:89
clang::interp::SourceMapper
Interface for classes which map locations to sources.
Definition: Source.h:87