15#include "llvm/ADT/ScopeExit.h"
22 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {}
25 for (
auto &
V : Locals) {
38 bool ConvertResultToRValue,
39 bool DestroyToplevelScope) {
43 EvalResult.setSource(E);
45 if (!this->
visitExpr(E, DestroyToplevelScope)) {
48 EvalResult.setInvalid();
51 return std::move(this->EvalResult);
55 bool CheckFullyInitialized) {
58 this->CheckFullyInitialized = CheckFullyInitialized;
59 S.EvaluatingDecl = VD;
61 EvalResult.setSource(VD);
64 this->ConvertResultToRValue = !
Init->isGLValue() && !T->isPointerType() &&
65 !T->isObjCObjectPointerType();
66 EvalResult.setSource(VD);
69 EvalResult.setInvalid();
71 S.EvaluatingDecl =
nullptr;
72 updateGlobalTemporaries();
73 return std::move(this->EvalResult);
80 EvalResult.setSource(VD);
83 EvalResult.setInvalid();
85 return std::move(this->EvalResult);
91 this->ConvertResultToRValue =
false;
92 this->CheckFullyInitialized =
false;
94 EvalResult.setSource(E);
99 EvalResult.setInvalid();
102 return std::move(this->EvalResult);
108 this->ConvertResultToRValue =
false;
109 this->CheckFullyInitialized =
false;
111 EvalResult.setSource(E);
114 EvalResult.setInvalid();
116 return std::move(this->EvalResult);
123 this->
Params.insert({PD, {0,
false}});
136 auto *B =
new (Memory.get())
Block(Ctx.getEvalID(), D,
false);
143 Desc.IsActive =
false;
145 Desc.IsFieldMutable =
false;
147 Desc.IsInitialized =
false;
150 unsigned Off = Locals.size();
151 Locals.push_back(std::move(Memory));
158 if (S.Stk.pop<
bool>())
167 if (!S.Stk.pop<
bool>())
176 CurrentLabel = ActiveLabel = Label;
184 CurrentLabel = Label;
195 size_t StackSizeBefore = S.Stk.size();
197 if (!this->
visit(Arg)) {
198 S.Stk.clearTo(StackSizeBefore);
200 if (S.inConstantContext() || Arg->
HasSideEffects(S.getASTContext()))
207 const auto &Ptr = S.Stk.pop<
Pointer>();
212 if (!this->emitPop(T, E))
217template <PrimType OpType>
bool EvalEmitter::emitRet(
SourceInfo Info) {
226template <>
bool EvalEmitter::emitRet<PT_Ptr>(
SourceInfo Info) {
233 return (*this->PtrCB)(Ptr);
247 if (ConvertResultToRValue) {
263 if (std::optional<APValue>
V =
265 EvalResult.takeValue(std::move(*
V));
274 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
281 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
287bool EvalEmitter::emitRetVoid(
SourceInfo Info) {
288 EvalResult.setValid();
292bool EvalEmitter::emitRetValue(
SourceInfo Info) {
293 const auto &Ptr = S.Stk.pop<
Pointer>();
295 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
297 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
300 if (std::optional<APValue> APV =
301 Ptr.
toRValue(S.getASTContext(), EvalResult.getSourceType())) {
302 EvalResult.takeValue(std::move(*APV));
306 EvalResult.setInvalid();
310bool EvalEmitter::emitGetPtrLocal(uint32_t I,
SourceInfo Info) {
314 Block *B = getLocal(I);
315 S.Stk.push<
Pointer>(B,
sizeof(InlineDescriptor));
319bool EvalEmitter::emitGetRefLocal(uint32_t I,
SourceInfo Info) {
323 Block *B = getLocal(I);
327template <PrimType OpType>
328bool EvalEmitter::emitGetLocal(uint32_t I,
SourceInfo Info) {
332 using T =
typename PrimConv<OpType>::T;
334 Block *B = getLocal(I);
339 S.Stk.push<T>(B->
deref<T>());
343template <PrimType OpType>
344bool EvalEmitter::emitSetLocal(uint32_t I,
SourceInfo Info) {
348 using T =
typename PrimConv<OpType>::T;
350 Block *B = getLocal(I);
351 B->
deref<T>() = S.Stk.pop<T>();
353 Desc.IsInitialized =
true;
359bool EvalEmitter::emitDestroy(uint32_t I,
SourceInfo Info) {
371bool EvalEmitter::emitGetLocalEnabled(uint32_t I,
SourceInfo Info) {
375 Block *B = getLocal(I);
378 S.Stk.push<
bool>(Desc.IsActive);
382bool EvalEmitter::emitEnableLocal(uint32_t I,
SourceInfo Info) {
391 Block *B = getLocal(I);
393 Desc.IsActive =
true;
402void EvalEmitter::updateGlobalTemporaries() {
403 for (
const auto &[E, Temp] : S.SeenGlobalTemporaries) {
406 const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
407 APValue *Cached = Temp->getOrCreateValue(
true);
409 QualType TempType = E->
getType();
410 if (
const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
411 TempType = MTE->getSubExpr()->skipRValueSubobjectAdjustments()->getType();
413 if (OptPrimType T = Ctx.classify(TempType)) {
415 { *Cached = Ptr.
deref<T>().toAPValue(Ctx.getASTContext()); });
417 if (std::optional<APValue> APV = Ptr.
toRValue(Ctx, TempType))
421 S.SeenGlobalTemporaries.clear();
429#include "Opcodes.inc"
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
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)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
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.
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
llvm::DenseMap< const ParmVarDecl *, FuncParam > Params
Parameter indices.
EvaluationResult interpretDecl(const VarDecl *VD, const Expr *Init, bool CheckFullyInitialized)
EvaluationResult interpretDestructor(const VarDecl *VD, const APValue &Value)
EvaluationResult interpretExpr(const Expr *E, bool ConvertResultToRValue=false, bool DestroyToplevelScope=false)
bool jump(const LabelTy &Label, SourceInfo SI)
virtual bool visit(const Expr *E)=0
bool speculate(const CallExpr *E, const LabelTy &EndLabel)
Speculative execution.
virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value)=0
bool jumpFalse(const LabelTy &Label, SourceInfo SI)
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...
EvaluationResult interpretAsLValuePointer(const Expr *E, PtrCallback PtrCB)
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)
virtual bool emitBool(bool V, const Expr *E)=0
bool jumpTrue(const LabelTy &Label, SourceInfo SI)
Emits jumps.
llvm::SmallVector< SmallVector< Local, 8 >, 2 > Descriptors
Local descriptors.
virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope)=0
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.
QualType getType() const
Returns the type of the innermost field.
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 isPastEnd() const
Checks if the pointer points past the end of the object.
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 handleReference(InterpState &S, CodePtr OpPC, Block *B)
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.
bool PushIgnoreDiags(InterpState &S, CodePtr OpPC)
bool PopIgnoreDiags(InterpState &S, CodePtr OpPC)
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)
OptionalUnsigned< unsigned > UnsignedOrNone
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.