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:
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 operator bool() const { return !Source.isNull(); }
95
96private:
97 llvm::PointerUnion<const Decl *, const Stmt *> Source;
98};
99static_assert(sizeof(SourceInfo) == sizeof(void *));
100
101// A map from byte code offset to source information.
102// This is used to get the location in the input source file for diagnostics.
103class SourceMap final {
104private:
107
108public:
109 SourceMap() = default;
110 void push(uint32_t Offset, SourceInfo Info) {
111 Offsets.push_back(Offset);
112 Infos.push_back(Info);
113 }
114
116 assert(!Offsets.empty());
117 assert(Offsets.size() == Infos.size());
118#ifndef NDEBUG
119 assert(llvm::is_sorted(Offsets));
120#endif
121 const auto *It = llvm::lower_bound(Offsets, Offset);
122 return Infos[It - Offsets.begin()];
123 }
124};
125
126/// Interface for classes which map locations to sources.
128public:
129 virtual ~SourceMapper() {}
130
131 /// Returns source information for a given PC in a function.
132 virtual SourceInfo getSource(CodePtr PC) const = 0;
133
134 /// Returns the expression if an opcode belongs to one, null otherwise.
135 const Expr *getExpr(CodePtr PC) const;
136 /// Returns the location from which an opcode originates.
138 SourceRange getRange(CodePtr PC) const;
139};
140
141} // namespace interp
142} // namespace clang
143
144#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: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:99
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
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
void push(uint32_t Offset, SourceInfo Info)
Definition Source.h:110
SourceInfo findSourceForOffset(uint32_t Offset) const
Definition Source.h:115
Interface for classes which map locations to sources.
Definition Source.h:127
SourceRange getRange(CodePtr PC) const
Definition Source.cpp:43
virtual SourceInfo getSource(CodePtr PC) const =0
Returns source information for a given PC in a function.
SourceLocation getLocation(CodePtr PC) const
Returns the location from which an opcode originates.
Definition Source.cpp:39
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.
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