1 //===--- Source.h - Source location provider for the VM --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines a program which organises and links multiple bytecode functions.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_INTERP_SOURCE_H
14 #define LLVM_CLANG_AST_INTERP_SOURCE_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"
29 /// Pointer into the code segment.
32 CodePtr() : Ptr(nullptr) {}
34 CodePtr
&operator+=(int32_t Offset
) {
39 int32_t operator-(const CodePtr
&RHS
) const {
40 assert(Ptr
!= nullptr && RHS
.Ptr
!= nullptr && "Invalid code pointer");
44 CodePtr
operator-(size_t RHS
) const {
45 assert(Ptr
!= nullptr && "Invalid code pointer");
46 return CodePtr(Ptr
- RHS
);
49 bool operator!=(const CodePtr
&RHS
) const { return Ptr
!= RHS
.Ptr
; }
50 const std::byte
*operator*() const { return Ptr
; }
52 operator bool() const { return Ptr
; }
54 /// Reads data and advances the pointer.
55 template <typename T
> std::enable_if_t
<!std::is_pointer
<T
>::value
, T
> read() {
57 using namespace llvm::support
;
58 T Value
= endian::read
<T
, llvm::endianness::native
>(Ptr
);
59 Ptr
+= align(sizeof(T
));
64 friend class Function
;
65 /// Constructor used by Function to generate pointers.
66 CodePtr(const std::byte
*Ptr
) : Ptr(Ptr
) {}
67 /// Pointer into the code owned by a function.
71 /// Describes the statement/declaration an opcode was generated from.
72 class SourceInfo final
{
75 SourceInfo(const Stmt
*E
) : Source(E
) {}
76 SourceInfo(const Decl
*D
) : Source(D
) {}
78 SourceLocation
getLoc() const;
79 SourceRange
getRange() const;
81 const Stmt
*asStmt() const { return Source
.dyn_cast
<const Stmt
*>(); }
82 const Decl
*asDecl() const { return Source
.dyn_cast
<const Decl
*>(); }
83 const Expr
*asExpr() const;
85 operator bool() const { return !Source
.isNull(); }
88 llvm::PointerUnion
<const Decl
*, const Stmt
*> Source
;
91 using SourceMap
= std::vector
<std::pair
<unsigned, SourceInfo
>>;
93 /// Interface for classes which map locations to sources.
96 virtual ~SourceMapper() {}
98 /// Returns source information for a given PC in a function.
99 virtual SourceInfo
getSource(const Function
*F
, CodePtr PC
) const = 0;
101 /// Returns the expression if an opcode belongs to one, null otherwise.
102 const Expr
*getExpr(const Function
*F
, CodePtr PC
) const;
103 /// Returns the location from which an opcode originates.
104 SourceLocation
getLocation(const Function
*F
, CodePtr PC
) const;
105 SourceRange
getRange(const Function
*F
, CodePtr PC
) const;
108 } // namespace interp