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
35 CodePtr &operator+=(int32_t Offset) {
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(Size: 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() {}
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 *>(Val: Source);
88 }
89 const Decl *asDecl() const {
90 return dyn_cast_if_present<const Decl *>(Val: Source);
91 }
92 const Expr *asExpr() const { return dyn_cast_if_present<Expr>(Val: 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:
105 llvm::SmallVector<uint32_t> Offsets;
106 llvm::SmallVector<SourceInfo> Infos;
107
108public:
109 SourceMap() = default;
110 void push(uint32_t Offset, SourceInfo Info) {
111 Offsets.push_back(Elt: Offset);
112 Infos.push_back(Elt: Info);
113 }
114
115 SourceInfo findSourceForOffset(uint32_t Offset) const {
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(Range: Offsets, Value&: Offset);
122 return Infos[It - Offsets.begin()];
123 }
124};
125
126/// Interface for classes which map locations to sources.
127class SourceMapper {
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
135} // namespace interp
136} // namespace clang
137
138#endif
139