13#ifndef LLVM_CLANG_AST_INTERP_PROGRAM_H
14#define LLVM_CLANG_AST_INTERP_PROGRAM_H
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/PointerUnion.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/Allocator.h"
48 for (Global *G : Globals)
49 G->block()->invokeDtor();
54 for (
auto RecordPair : Records) {
55 if (
Record *R = RecordPair.second)
74 assert(Idx < Globals.size());
75 return Globals[Idx]->block();
83 const Expr *Init =
nullptr);
95 template <
typename... Ts>
98 auto *Func =
new Function(*
this, Def, std::forward<Ts>(Args)...);
99 Funcs.insert({Def, std::unique_ptr<Function>(Func)});
103 template <
typename... Ts>
105 auto *Func =
new Function(*
this, std::forward<Ts>(Args)...);
106 AnonFuncs.emplace_back(Func);
119 bool IsConst =
false,
bool IsTemporary =
false,
120 bool IsMutable =
false) {
121 return allocateDescriptor(D,
Type, MDSize, IsConst, IsTemporary, IsMutable);
127 bool IsConst =
false,
bool IsTemporary =
false,
128 bool IsMutable =
false,
129 const Expr *Init =
nullptr);
135 P.startDeclaration(VD);
145 if (CurrentDeclaration == NoDeclaration)
146 return std::optional<unsigned>{};
147 return LastDeclaration;
155 const Expr *Init =
nullptr);
160 llvm::DenseMap<const FunctionDecl *, std::unique_ptr<Function>> Funcs;
162 std::vector<std::unique_ptr<Function>> AnonFuncs;
165 llvm::DenseMap<const FunctionDecl *, std::vector<unsigned>> Relocs;
168 std::vector<const void *> NativePointers;
170 llvm::DenseMap<const void *, unsigned> NativePointerIndices;
173 using PoolAllocTy = llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator>;
181 template <
typename... Tys>
182 Global(Tys... Args) : B(
std::
forward<Tys>(Args)...) {}
185 void *
operator new(
size_t Meta, PoolAllocTy &Alloc,
size_t Data) {
186 return Alloc.Allocate(Meta +
Data,
alignof(
void *));
190 std::byte *data() {
return B.data(); }
192 Block *block() {
return &B; }
200 PoolAllocTy Allocator;
203 std::vector<Global *> Globals;
205 llvm::DenseMap<const void *, unsigned> GlobalIndices;
208 llvm::DenseMap<const RecordDecl *, Record *> Records;
211 llvm::DenseMap<const ParmVarDecl *, unsigned> DummyParams;
214 template <
typename... Ts>
215 Descriptor *allocateDescriptor(Ts &&... Args) {
216 return new (Allocator) Descriptor(std::forward<Ts>(Args)...);
220 static constexpr unsigned NoDeclaration = (
unsigned)-1;
222 unsigned LastDeclaration = 0;
224 unsigned CurrentDeclaration = NoDeclaration;
227 void startDeclaration(
const ValueDecl *Decl) {
228 LastDeclaration += 1;
229 CurrentDeclaration = LastDeclaration;
233 void endDeclaration() {
234 CurrentDeclaration = NoDeclaration;
240 void dump(llvm::raw_ostream &OS)
const;
This represents one expression.
Represents a function declaration or definition.
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Represents a parameter to a function.
A (possibly-)qualified type.
Represents a struct/union/class.
StringLiteral - This represents a string literal expression, e.g.
The base class of the type hierarchy.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
A memory block, either on the stack or in the heap.
Holds all information required to evaluate constexpr code in a module.
A pointer to a memory block, live or dead.
Context to manage declaration lifetimes.
DeclScope(Program &P, const ValueDecl *VD)
The program contains and links the bytecode for all functions.
std::optional< unsigned > getOrCreateDummy(const ParmVarDecl *PD)
Returns or creates a dummy value for parameters.
std::optional< unsigned > getOrCreateGlobal(const ValueDecl *VD, const Expr *Init=nullptr)
Returns or creates a global an creates an index to it.
Function * getFunction(const FunctionDecl *F)
Returns a function.
Block * getGlobal(unsigned Idx)
Returns the value of a global.
Function * createFunction(Ts &&... Args)
Creates an anonymous function.
const void * getNativePointer(unsigned Idx)
Returns the value of a marshalled native pointer.
Descriptor * createDescriptor(const DeclTy &D, PrimType Type, Descriptor::MetadataSize MDSize=std::nullopt, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false)
Creates a descriptor for a primitive type.
unsigned getOrCreateNativePointer(const void *Ptr)
Marshals a native pointer to an ID for embedding in bytecode.
unsigned createGlobalString(const StringLiteral *S)
Emits a string literal among global data.
Function * createFunction(const FunctionDecl *Def, Ts &&... Args)
Creates a new function from a code range.
void dump() const
Dumps the disassembled bytecode to llvm::errs().
std::optional< unsigned > getCurrentDecl() const
Returns the current declaration ID.
Pointer getPtrGlobal(unsigned Idx)
Returns a pointer to a global.
std::optional< unsigned > createGlobal(const ValueDecl *VD, const Expr *E)
Creates a global and returns its index.
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Structure/Class descriptor.
PrimType
Enumeration of the primitive types of the VM.
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Describes a memory block created by an allocation site.
std::optional< unsigned > MetadataSize