13#ifndef LLVM_CLANG_AST_INTERP_PROGRAM_H
14#define LLVM_CLANG_AST_INTERP_PROGRAM_H
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/Support/Allocator.h"
44 for (Global *G : Globals)
51 for (
auto RecordPair : Records) {
52 if (
Record *R = RecordPair.second)
74 assert(Idx < Globals.size());
75 return Globals[Idx]->block();
100 template <
typename... Ts>
103 auto *
Func =
new Function(*
this, Def, std::forward<Ts>(Args)...);
104 Funcs.insert({Def, std::unique_ptr<Function>(
Func)});
109 auto *
Func =
new Function(*
this, std::forward<Ts>(Args)...);
110 AnonFuncs.emplace_back(
Func);
122 const Type *SourceTy =
nullptr,
124 bool IsConst =
false,
bool IsTemporary =
false,
125 bool IsMutable =
false,
126 bool IsVolatile =
false) {
127 return allocateDescriptor(D, SourceTy, T, MDSize, IsConst, IsTemporary,
128 IsMutable, IsVolatile);
134 bool IsConst =
false,
bool IsTemporary =
false,
135 bool IsMutable =
false,
bool IsVolatile =
false,
138 void *
Allocate(
size_t Size,
unsigned Align = 8)
const {
139 return Allocator.Allocate(Size, Align);
142 return static_cast<T *
>(
Allocate(
Num *
sizeof(T),
alignof(T)));
151 P.CurrentDeclaration = P.LastDeclaration;
162 if (CurrentDeclaration == NoDeclaration)
164 return CurrentDeclaration;
171 bool IsExtern,
bool IsWeak,
177 llvm::DenseMap<const FunctionDecl *, std::unique_ptr<Function>> Funcs;
179 std::vector<std::unique_ptr<Function>> AnonFuncs;
182 std::vector<const void *> NativePointers;
184 llvm::DenseMap<const void *, unsigned> NativePointerIndices;
187 using PoolAllocTy = llvm::BumpPtrAllocator;
195 template <
typename... Tys>
199 void *
operator new(
size_t Meta, PoolAllocTy &
Alloc,
size_t Data) {
200 return Alloc.Allocate(Meta +
Data,
alignof(
void *));
204 std::byte *data() {
return B.
data(); }
206 Block *block() {
return &B; }
207 const Block *block()
const {
return &B; }
214 mutable PoolAllocTy Allocator;
217 std::vector<Global *> Globals;
219 llvm::DenseMap<const void *, unsigned> GlobalIndices;
222 llvm::DenseMap<const RecordDecl *, Record *> Records;
225 llvm::DenseMap<const void *, unsigned> DummyVariables;
228 template <
typename... Ts> Descriptor *allocateDescriptor(Ts &&...Args) {
229 return new (Allocator) Descriptor(std::forward<Ts>(Args)...);
233 static constexpr unsigned NoDeclaration = ~0u;
235 unsigned LastDeclaration = 0;
237 unsigned CurrentDeclaration = NoDeclaration;
242 void dump(llvm::raw_ostream &OS)
const;
249 size_t Alignment = 8) {
250 return C.Allocate(Bytes, Alignment);
258 size_t Alignment = 8) {
259 return C.Allocate(Bytes, Alignment);
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
This represents one expression.
Represents a function declaration or definition.
FunctionDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
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 ...
Represents a variable declaration or definition.
A memory block, either on the stack or in the heap.
std::byte * data()
Returns a pointer to the stored data.
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Holds all information required to evaluate constexpr code in a module.
A pointer to a memory block, live or dead.
bool isInitialized() const
Checks if an object was initialized.
Context to manage declaration lifetimes.
The program contains and links the bytecode for all functions.
void * Allocate(size_t Size, unsigned Align=8) const
Function * getFunction(const FunctionDecl *F)
Returns a function.
Block * getGlobal(unsigned Idx)
Returns the value of a global.
const Context & getContext() const
UnsignedOrNone getOrCreateGlobal(const ValueDecl *VD, const Expr *Init=nullptr)
Returns or creates a global an creates an index to it.
unsigned getOrCreateNativePointer(const void *Ptr)
Marshals a native pointer to an ID for embedding in bytecode.
Function * createFunction(const FunctionDecl *Def, Ts &&...Args)
Creates a new function from a code range.
bool isGlobalInitialized(unsigned Index) const
Pointer getPtrGlobal(unsigned Idx) const
Returns a pointer to a global.
unsigned getOrCreateDummy(const DeclTy &D)
Returns or creates a dummy value for unknown declarations.
void dump() const
Dumps the disassembled bytecode to llvm::errs().
const void * getNativePointer(unsigned Idx) const
Returns the value of a marshalled native pointer.
T * Allocate(size_t Num=1) const
void Deallocate(void *Ptr) const
UnsignedOrNone createGlobal(const ValueDecl *VD, const Expr *Init)
Creates a global and returns its index.
Descriptor * createDescriptor(const DeclTy &D, PrimType T, const Type *SourceTy=nullptr, Descriptor::MetadataSize MDSize=std::nullopt, bool IsConst=false, bool IsTemporary=false, bool IsMutable=false, bool IsVolatile=false)
Creates a descriptor for a primitive type.
unsigned createGlobalString(const StringLiteral *S, const Expr *Base=nullptr)
Emits a string literal among global data.
Function * createFunction(Ts &&...Args)
Creates an anonymous function.
UnsignedOrNone getCurrentDecl() const
Returns the current declaration ID.
Record * getOrCreateRecord(const RecordDecl *RD)
Returns a record or creates one if it does not exist.
Structure/Class descriptor.
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
PrimType
Enumeration of the primitive types of the VM.
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
bool Init(InterpState &S, CodePtr OpPC)
The JSON file list parser is used to communicate input to InstallAPI.
OptionalUnsigned< unsigned > UnsignedOrNone
Describes a memory block created by an allocation site.
std::optional< unsigned > MetadataSize