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/ADT/STLExtras.h"
21#include "llvm/Support/Endian.h"
22
23namespace clang {
24class Expr;
25class SourceLocation;
26class SourceRange;
27namespace interp {
28class Function;
29
30/// Pointer into the code segment.
31class CodePtr final {
32public:
33 CodePtr() = default;
34
36 Ptr += Offset;
37 return *this;
38 }
39
40 CodePtr operator+(int32_t Offset) { return CodePtr(Ptr + Offset); }
41
42 int32_t operator-(const CodePtr &RHS) const {
43 assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
44 return Ptr - RHS.Ptr;
45 }
46
47 CodePtr operator-(size_t RHS) const {
48 assert(Ptr != nullptr && "Invalid code pointer");
49 return CodePtr(Ptr - RHS);
50 }
51
52 bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
53 const std::byte *operator*() const { return Ptr; }
54 explicit operator bool() const { return Ptr; }
55 bool operator<=(const CodePtr &RHS) const { return Ptr <= RHS.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
68private:
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.
77class SourceInfo final {
78public:
79 SourceInfo() : Source(nullptr) {}
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 { return dyn_cast_if_present<Expr>(asStmt()); }
93
94 explicit operator bool() const { return !Source.isNull(); }
95
96 bool operator!=(SourceInfo O) { return Source != O.Source; }
97
98private:
99 llvm::PointerUnion<const Decl *, const Stmt *> Source;
100};
101static_assert(sizeof(SourceInfo) == sizeof(void *));
102
103// A map from byte code offset to source information.
104// This is used to get the location in the input source file for diagnostics.
105class SourceMap final {
106private:
109
110public:
111 SourceMap() = default;
112 void push(uint32_t Offset, SourceInfo Info) {
113 Offsets.push_back(Offset);
114 Infos.push_back(Info);
115 }
116
117 bool empty() const { return Offsets.empty(); }
118 SourceInfo back() const { return Infos.back(); }
119
121 assert(!Offsets.empty());
122 assert(Offsets.size() == Infos.size());
123#ifndef NDEBUG
124 assert(llvm::is_sorted(Offsets));
125#endif
126
127 // Find the last offset <= Offset.
128 auto *It = llvm::upper_bound(Offsets, Offset);
129 if (It == Offsets.begin())
130 return Infos[0];
131
132 --It;
133 return Infos[It - Offsets.begin()];
134 }
135};
136
137/// Interface for classes which map locations to sources.
139public:
140 virtual ~SourceMapper() {}
141 /// Returns source information for a given PC in a function.
142 virtual SourceInfo getSource(CodePtr PC) const = 0;
143};
144
145} // namespace interp
146} // namespace clang
147
148#endif
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
This represents one expression.
Definition Expr.h:113
Encodes a location in the source.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
Definition Stmt.h:85
Pointer into the code segment.
Definition Source.h:31
bool operator<=(const CodePtr &RHS) const
Definition Source.h:55
bool operator==(const CodePtr RHS) const
Definition Source.h:57
const std::byte * operator*() const
Definition Source.h:53
std::enable_if_t<!std::is_pointer< T >::value, T > read()
Reads data and advances the pointer.
Definition Source.h:60
CodePtr operator+(int32_t Offset)
Definition Source.h:40
bool operator!=(const CodePtr &RHS) const
Definition Source.h:52
CodePtr & operator+=(int32_t Offset)
Definition Source.h:35
friend class Function
Definition Source.h:69
int32_t operator-(const CodePtr &RHS) const
Definition Source.h:42
bool operator>=(const CodePtr &RHS) const
Definition Source.h:56
CodePtr operator-(size_t RHS) const
Definition Source.h:47
Bytecode function.
Definition Function.h:98
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
const Expr * asExpr() const
Definition Source.h:92
SourceInfo(const Decl *D)
Definition Source.h:81
bool operator!=(SourceInfo O)
Definition Source.h:96
const Decl * asDecl() const
Definition Source.h:89
const Stmt * asStmt() const
Definition Source.h:86
SourceLocation getLoc() const
Definition Source.cpp:15
SourceRange getRange() const
Definition Source.cpp:25
SourceInfo(const Stmt *E)
Definition Source.h:80
SourceInfo back() const
Definition Source.h:118
void push(uint32_t Offset, SourceInfo Info)
Definition Source.h:112
SourceInfo findSourceForOffset(uint32_t Offset) const
Definition Source.h:120
bool empty() const
Definition Source.h:117
Interface for classes which map locations to sources.
Definition Source.h:138
virtual SourceInfo getSource(CodePtr PC) const =0
Returns source information for a given PC in a function.
constexpr bool aligned(uintptr_t Value)
Definition PrimType.h:217
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:213
Top level wrappers for InstallAPI frontend operations.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
const FunctionProtoType * T
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t