clang 24.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 "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
22namespace clang {
23class Expr;
24class SourceLocation;
25class SourceRange;
26namespace interp {
27class Function;
28
29/// Pointer into the code segment.
30class CodePtr final {
31public:
32 CodePtr() = default;
33
35 Ptr += Offset;
36 return *this;
37 }
38
39 CodePtr operator+(int32_t Offset) { return CodePtr(Ptr + Offset); }
40
41 int32_t operator-(const CodePtr &RHS) const {
42 assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
43 return Ptr - RHS.Ptr;
44 }
45
46 CodePtr operator-(size_t RHS) const {
47 assert(Ptr != nullptr && "Invalid code pointer");
48 return CodePtr(Ptr - RHS);
49 }
50
51 bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
52 const std::byte *operator*() const { return Ptr; }
53 explicit operator bool() const { return Ptr; }
54 bool operator<=(const CodePtr &RHS) const { return Ptr <= RHS.Ptr; }
55 bool operator>=(const CodePtr &RHS) const { return Ptr >= RHS.Ptr; }
56 bool operator==(const CodePtr RHS) const { return Ptr == RHS.Ptr; }
57
58 /// Reads data and advances the pointer.
59 template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
60 assert(aligned(Ptr));
61 using namespace llvm::support;
62 T Value = endian::read<T, llvm::endianness::native>(Ptr);
63 Ptr += align(sizeof(T));
64 return Value;
65 }
66
67private:
68 friend class Function;
69 /// Constructor used by Function to generate pointers.
70 CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
71 /// Pointer into the code owned by a function.
72 const std::byte *Ptr = nullptr;
73};
74
75/// Describes the statement/declaration an opcode was generated from.
76class SourceInfo final {
77public:
79 SourceInfo(const Stmt *E) : Source(E) {}
80 SourceInfo(const Decl *D) : Source(D) {}
81
82 SourceLocation getLoc() const;
83 SourceRange getRange() const;
84
85 const Stmt *asStmt() const {
86 return dyn_cast_if_present<const Stmt *>(Source);
87 }
88 const Decl *asDecl() const {
89 return dyn_cast_if_present<const Decl *>(Source);
90 }
91 const Expr *asExpr() const;
92
93 operator bool() const { return !Source.isNull(); }
94
95private:
96 llvm::PointerUnion<const Decl *, const Stmt *> Source;
97};
98static_assert(sizeof(SourceInfo) == sizeof(void *));
99
100using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
101
102/// Interface for classes which map locations to sources.
104public:
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
Expr * getExpr()
Get 'expr' part of the associated expression/statement.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:112
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition Stmt.h:86
Pointer into the code segment.
Definition Source.h:30
bool operator<=(const CodePtr &RHS) const
Definition Source.h:54
bool operator==(const CodePtr RHS) const
Definition Source.h:56
const std::byte * operator*() const
Definition Source.h:52
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition Source.h:59
CodePtr operator+(int32_t Offset)
Definition Source.h:39
bool operator!=(const CodePtr &RHS) const
Definition Source.h:51
CodePtr & operator+=(int32_t Offset)
Definition Source.h:34
friend class Function
Definition Source.h:68
int32_t operator-(const CodePtr &RHS) const
Definition Source.h:41
bool operator>=(const CodePtr &RHS) const
Definition Source.h:55
CodePtr operator-(size_t RHS) const
Definition Source.h:46
Bytecode function.
Definition Function.h:99
Describes the statement/declaration an opcode was generated from.
Definition Source.h:76
SourceInfo(const Decl *D)
Definition Source.h:80
const Decl * asDecl() const
Definition Source.h:88
const Expr * asExpr() const
Definition Source.cpp:35
const Stmt * asStmt() const
Definition Source.h:85
SourceLocation getLoc() const
Definition Source.cpp:15
SourceRange getRange() const
Definition Source.cpp:25
SourceInfo(const Stmt *E)
Definition Source.h:79
Interface for classes which map locations to sources.
Definition Source.h:103
virtual SourceInfo getSource(const Function *F, CodePtr PC) const =0
Returns source information for a given PC in a function.
SourceLocation getLocation(const Function *F, CodePtr PC) const
Returns the location from which an opcode originates.
Definition Source.cpp:47
SourceRange getRange(const Function *F, CodePtr PC) const
Definition Source.cpp:51
constexpr bool aligned(uintptr_t Value)
Definition PrimType.h:205
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:201
std::vector< std::pair< unsigned, SourceInfo > > SourceMap
Definition Source.h:100
The JSON file list parser is used to communicate input to InstallAPI.
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t