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) {
54 this->CheckFullyInitialized = CheckFullyInitialized;
55 S.EvaluatingDecl = VD;
57 EvalResult.setSource(VD);
62 this->ConvertResultToRValue = !
Init->isGLValue() && !
T->isPointerType() &&
63 !
T->isObjCObjectPointerType();
65 this->ConvertResultToRValue =
false;
67 EvalResult.setSource(VD);
70 EvalResult.setInvalid();
72 S.EvaluatingDecl =
nullptr;
73 updateGlobalTemporaries();
74 return std::move(this->EvalResult);
81 this->ConvertResultToRValue =
false;
82 this->CheckFullyInitialized =
false;
84 EvalResult.setSource(E);
89 EvalResult.setInvalid();
92 return std::move(this->EvalResult);
99 this->
Params.insert({PD, {0,
false}});
112 auto *B =
new (Memory.get())
Block(Ctx.getEvalID(), D,
false);
126 unsigned Off = Locals.size();
127 Locals.push_back(std::move(Memory));
133 if (S.Stk.pop<
bool>())
141 if (!S.Stk.pop<
bool>())
149 CurrentLabel = ActiveLabel = Label;
156 CurrentLabel = Label;
161 size_t StackSizeBefore = S.Stk.size();
163 if (!this->
visit(Arg)) {
164 S.Stk.clearTo(StackSizeBefore);
166 if (S.inConstantContext() || Arg->
HasSideEffects(S.getASTContext()))
173 const auto &Ptr = S.Stk.pop<
Pointer>();
178 if (!this->emitPop(
T, E))
183template <PrimType OpType>
bool EvalEmitter::emitRet(
const SourceInfo &Info) {
192template <>
bool EvalEmitter::emitRet<PT_Ptr>(
const SourceInfo &Info) {
205 return (*this->PtrCB)(Ptr);
213 if (ConvertResultToRValue) {
229 if (std::optional<APValue>
V =
231 EvalResult.takeValue(std::move(*
V));
240 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
247 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
253bool EvalEmitter::emitRetVoid(
const SourceInfo &Info) {
254 EvalResult.setValid();
258bool EvalEmitter::emitRetValue(
const SourceInfo &Info) {
259 const auto &Ptr = S.Stk.pop<
Pointer>();
261 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
263 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
266 if (std::optional<APValue> APV =
267 Ptr.
toRValue(S.getASTContext(), EvalResult.getSourceType())) {
268 EvalResult.takeValue(std::move(*APV));
272 EvalResult.setInvalid();
276bool EvalEmitter::emitGetPtrLocal(uint32_t I,
const SourceInfo &Info) {
280 Block *B = getLocal(I);
281 S.Stk.push<
Pointer>(B,
sizeof(InlineDescriptor));
285template <PrimType OpType>
286bool EvalEmitter::emitGetLocal(uint32_t I,
const SourceInfo &Info) {
290 using T =
typename PrimConv<OpType>::T;
292 Block *B = getLocal(I);
297 S.Stk.push<
T>(*
reinterpret_cast<T *
>(B->
data()));
301template <PrimType OpType>
302bool EvalEmitter::emitSetLocal(uint32_t I,
const SourceInfo &Info) {
306 using T =
typename PrimConv<OpType>::T;
308 Block *B = getLocal(I);
309 *
reinterpret_cast<T *
>(B->
data()) = S.Stk.pop<
T>();
310 InlineDescriptor &Desc = *
reinterpret_cast<InlineDescriptor *
>(B->
rawData());
311 Desc.IsInitialized =
true;
316bool EvalEmitter::emitDestroy(uint32_t I,
const SourceInfo &Info) {
333void EvalEmitter::updateGlobalTemporaries() {
334 for (
const auto &[E, Temp] : S.SeenGlobalTemporaries) {
335 UnsignedOrNone GlobalIndex = P.getGlobal(E);
337 const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
338 APValue *Cached = Temp->getOrCreateValue(
true);
339 if (OptPrimType
T = Ctx.classify(E->getType())) {
341 { *Cached = Ptr.
deref<
T>().toAPValue(Ctx.getASTContext()); });
343 if (std::optional<APValue> APV =
344 Ptr.
toRValue(Ctx, Temp->getTemporaryExpr()->getType()))
348 S.SeenGlobalTemporaries.clear();
356#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.
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
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.
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Inline descriptor embedded in structures and arrays.
unsigned IsActive
Flag indicating if the field is the active member of a union.
unsigned IsBase
Flag indicating if the field is an embedded base class.
unsigned Offset
Offset inside the structure/array.
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
unsigned IsConst
Flag indicating if the storage is constant or not.
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Mapping from primitive types to their representation.
Information about a local's storage.