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
34 CodePtr &operator+=(int32_t Offset) {
35 Ptr += Offset;
36 return *this;
37 }
38
39 int32_t operator-(const CodePtr &RHS) const {
40 assert(Ptr != nullptr && RHS.Ptr != nullptr && "Invalid code pointer");
41 return Ptr - RHS.Ptr;
42 }
43
44 CodePtr operator-(size_t RHS) const {
45 assert(Ptr != nullptr && "Invalid code pointer");
46 return CodePtr(Ptr - RHS);
47 }
48
49 bool operator!=(const CodePtr &RHS) const { return Ptr != RHS.Ptr; }
50 const std::byte *operator*() const { return Ptr; }
51 explicit operator bool() const { return Ptr; }
52 bool operator<=(const CodePtr &RHS) const { return Ptr <= RHS.Ptr; }
53 bool operator>=(const CodePtr &RHS) const { return Ptr >= RHS.Ptr; }
54 bool operator==(const CodePtr RHS) const { return Ptr == RHS.Ptr; }
55
56 /// Reads data and advances the pointer.
57 template <typename T> std::enable_if_t<!std::is_pointer<T>::value, T> read() {
58 assert(aligned(Ptr));
59 using namespace llvm::support;
60 T Value = endian::read<T, llvm::endianness::native>(Ptr);
61 Ptr += align(Size: sizeof(T));
62 return Value;
63 }
64
65private:
66 friend class Function;
67 /// Constructor used by Function to generate pointers.
68 CodePtr(const std::byte *Ptr) : Ptr(Ptr) {}
69 /// Pointer into the code owned by a function.
70 const std::byte *Ptr = nullptr;
71};
72
73/// Describes the statement/declaration an opcode was generated from.
74class SourceInfo final {
75public:
76 SourceInfo() {}
77 SourceInfo(const Stmt *E) : Source(E) {}
78 SourceInfo(const Decl *D) : Source(D) {}
79
80 SourceLocation getLoc() const;
81 SourceRange getRange() const;
82
83 const Stmt *asStmt() const {
84 return dyn_cast_if_present<const Stmt *>(Val: Source);
85 }
86 const Decl *asDecl() const {
87 return dyn_cast_if_present<const Decl *>(Val: Source);
88 }
89 const Expr *asExpr() const;
90
91 operator bool() const { return !Source.isNull(); }
92
93private:
94 llvm::PointerUnion<const Decl *, const Stmt *> Source;
95};
96static_assert(sizeof(SourceInfo) == sizeof(void *));
97
98using SourceMap = std::vector<std::pair<unsigned, SourceInfo>>;
99
100/// Interface for classes which map locations to sources.
101class SourceMapper {
102public:
103 virtual ~SourceMapper() {}
104
105 /// Returns source information for a given PC in a function.
106 virtual SourceInfo getSource(const Function *F, CodePtr PC) const = 0;
107
108 /// Returns the expression if an opcode belongs to one, null otherwise.
109 const Expr *getExpr(const Function *F, CodePtr PC) const;
110 /// Returns the location from which an opcode originates.
111 SourceLocation getLocation(const Function *F, CodePtr PC) const;
112 SourceRange getRange(const Function *F, CodePtr PC) const;
113};
114
115} // namespace interp
116} // namespace clang
117
118#endif
119