20 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {}
23 for (
auto &
V : Locals) {
36 bool ConvertResultToRValue,
37 bool DestroyToplevelScope) {
41 EvalResult.setSource(E);
43 if (!this->
visitExpr(E, DestroyToplevelScope)) {
46 EvalResult.setInvalid();
49 return std::move(this->EvalResult);
53 bool CheckFullyInitialized) {
56 this->CheckFullyInitialized = CheckFullyInitialized;
57 S.EvaluatingDecl = VD;
59 EvalResult.setSource(VD);
62 this->ConvertResultToRValue = !
Init->isGLValue() && !
T->isPointerType() &&
63 !
T->isObjCObjectPointerType();
64 EvalResult.setSource(VD);
67 EvalResult.setInvalid();
69 S.EvaluatingDecl =
nullptr;
70 updateGlobalTemporaries();
71 return std::move(this->EvalResult);
78 this->ConvertResultToRValue =
false;
79 this->CheckFullyInitialized =
false;
81 EvalResult.setSource(E);
86 EvalResult.setInvalid();
89 return std::move(this->EvalResult);
96 this->
Params.insert({PD, {0,
false}});
109 auto *B =
new (Memory.get())
Block(Ctx.getEvalID(), D,
false);
116 Desc.IsActive =
false;
118 Desc.IsFieldMutable =
false;
120 Desc.IsInitialized =
false;
123 unsigned Off = Locals.size();
124 Locals.push_back(std::move(Memory));
130 if (S.Stk.pop<
bool>())
138 if (!S.Stk.pop<
bool>())
146 CurrentLabel = ActiveLabel = Label;
153 CurrentLabel = Label;
160 size_t StackSizeBefore = S.Stk.size();
162 if (!this->
visit(Arg)) {
163 S.Stk.clearTo(StackSizeBefore);
165 if (S.inConstantContext() || Arg->
HasSideEffects(S.getASTContext()))
172 const auto &Ptr = S.Stk.pop<
Pointer>();
177 if (!this->emitPop(
T, E))
182template <PrimType OpType>
bool EvalEmitter::emitRet(
SourceInfo Info) {
191template <>
bool EvalEmitter::emitRet<PT_Ptr>(
SourceInfo Info) {
204 return (*this->PtrCB)(Ptr);
212 if (ConvertResultToRValue) {
228 if (std::optional<APValue>
V =
230 EvalResult.takeValue(std::move(*
V));
239 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
246 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
252bool EvalEmitter::emitRetVoid(
SourceInfo Info) {
253 EvalResult.setValid();
257bool EvalEmitter::emitRetValue(
SourceInfo Info) {
258 const auto &Ptr = S.Stk.pop<
Pointer>();
260 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
262 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
265 if (std::optional<APValue> APV =
266 Ptr.
toRValue(S.getASTContext(), EvalResult.getSourceType())) {
267 EvalResult.takeValue(std::move(*APV));
271 EvalResult.setInvalid();
275bool EvalEmitter::emitGetPtrLocal(uint32_t I,
SourceInfo Info) {
279 Block *B = getLocal(I);
280 S.Stk.push<
Pointer>(B,
sizeof(InlineDescriptor));
284template <PrimType OpType>
285bool EvalEmitter::emitGetLocal(uint32_t I,
SourceInfo Info) {
289 using T =
typename PrimConv<OpType>::T;
291 Block *B = getLocal(I);
296 S.Stk.push<
T>(*
reinterpret_cast<T *
>(B->
data()));
300template <PrimType OpType>
301bool EvalEmitter::emitSetLocal(uint32_t I,
SourceInfo Info) {
305 using T =
typename PrimConv<OpType>::T;
307 Block *B = getLocal(I);
308 *
reinterpret_cast<T *
>(B->
data()) = S.Stk.pop<
T>();
310 Desc.IsInitialized =
true;
315bool EvalEmitter::emitDestroy(uint32_t I,
SourceInfo Info) {
327bool EvalEmitter::emitGetLocalEnabled(uint32_t I,
SourceInfo Info) {
331 Block *B = getLocal(I);
334 S.Stk.push<
bool>(Desc.IsActive);
338bool EvalEmitter::emitEnableLocal(uint32_t I,
SourceInfo Info) {
347 Block *B = getLocal(I);
349 Desc.IsActive =
true;
358void EvalEmitter::updateGlobalTemporaries() {
359 for (
const auto &[E, Temp] : S.SeenGlobalTemporaries) {
360 UnsignedOrNone GlobalIndex = P.getGlobal(E);
362 const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
363 APValue *Cached = Temp->getOrCreateValue(
true);
364 if (OptPrimType
T = Ctx.classify(E->getType())) {
366 { *Cached = Ptr.
deref<
T>().toAPValue(Ctx.getASTContext()); });
368 if (std::optional<APValue> APV =
369 Ptr.
toRValue(Ctx, Temp->getTemporaryExpr()->getType()))
373 S.SeenGlobalTemporaries.clear();
381#include "Opcodes.inc"
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, const APValue &Value)
Check that this evaluated value is fully-initialized and can be loaded by an lvalue-to-rvalue convers...
#define TYPE_SWITCH(Expr, B)
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
SourceLocation getLocation() const
This represents one expression.
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Represents a function declaration or definition.
ArrayRef< ParmVarDecl * > parameters() const
Represents a parameter to a function.
A (possibly-)qualified type.
Represents a variable declaration or definition.
A memory block, either on the stack or in the heap.
void invokeDtor()
Invokes the Destructor.
std::byte * data()
Returns a pointer to the stored data.
bool isStatic() const
Checks if the block has static storage duration.
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
unsigned getEvalID() const
The Evaluation ID this block was created in.
Holds all information required to evaluate constexpr code in a module.
ASTContext & getASTContext() const
Returns the AST context.
unsigned getEvalID() const
bool jump(const LabelTy &Label)
EvaluationResult interpretDecl(const VarDecl *VD, const Expr *Init, bool CheckFullyInitialized)
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false, bool DestroyToplevelScope=false)
bool jumpFalse(const LabelTy &Label)
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
Local createLocal(Descriptor *D)
Callback for registering a local.
bool interpretCall(const FunctionDecl *FD, const Expr *E)
Interpret the given expression as if it was in the body of the given function, i.e.
llvm::function_ref< bool(const Pointer &)> PtrCallback
void emitLabel(LabelTy Label)
Define a label.
bool isActive() const
Since expressions can only jump forward, predicated execution is used to deal with if-else statements...
virtual bool visitExpr(const Expr *E, bool DestroyToplevelScope)=0
Methods implemented by the compiler.
bool fallthrough(const LabelTy &Label)
virtual bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext)=0
void cleanup()
Clean up all resources.
LabelTy getLabel()
Create a label.
EvaluationResult interpretAsPointer(const Expr *E, PtrCallback PtrCB)
Interpret the given Expr to a Pointer.
EvalEmitter(Context &Ctx, Program &P, State &Parent, InterpStack &Stk)
llvm::DenseMap< const ParmVarDecl *, ParamOffset > Params
Parameter indices.
virtual bool emitBool(bool V, const Expr *E)=0
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
bool jumpTrue(const LabelTy &Label)
Emits jumps.
Defines the result of an evaluation.
QualType getSourceType() const
bool checkReturnValue(InterpState &S, const Context &Ctx, const Pointer &Ptr, const SourceInfo &Info)
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Check that all subobjects of the given pointer have been initialized.
Stack frame storing temporaries and parameters.
T pop()
Returns the value from the top of the stack and removes it.
InterpStack & Stk
Temporary stack.
A pointer to a memory block, live or dead.
bool isConst() const
Checks if an object or a subfield is mutable.
T & deref() const
Dereferences the pointer, if it's live.
bool pointsToStringLiteral() const
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
bool isLive() const
Checks if the pointer is live.
bool isZero() const
Checks if the pointer is null.
APValue toAPValue(const ASTContext &ASTCtx) const
Converts the pointer to an APValue.
bool isDereferencable() const
Whether this block can be read from at all.
bool isBlockPointer() const
std::optional< APValue > toRValue(const Context &Ctx, QualType ResultType) const
Converts the pointer to an APValue that is an rvalue.
bool isTemporary() const
Checks if the storage is temporary.
const Block * block() const
bool isFunctionPointer() const
The program contains and links the bytecode for all functions.
Describes the statement/declaration an opcode was generated from.
Interface for the VM to interact with the AST walker's context.
bool CheckBCPResult(InterpState &S, const Pointer &Ptr)
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
This is not used by any of the opcodes directly.
PrimType
Enumeration of the primitive types of the VM.
bool Init(InterpState &S, CodePtr OpPC)
bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
const FunctionProtoType * T
Describes a memory block created by an allocation site.
const bool IsConst
Flag indicating if the block is mutable.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Inline descriptor embedded in structures and arrays.
Mapping from primitive types to their representation.
Information about a local's storage.