15#include "llvm/ADT/ScopeExit.h"
22 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), EvalResult(&Ctx) {}
30 bool ConvertResultToRValue,
31 bool DestroyToplevelScope) {
35 EvalResult.setSource(E);
37 if (!this->
visitExpr(E, DestroyToplevelScope)) {
40 EvalResult.setInvalid();
43 return std::move(this->EvalResult);
47 bool CheckFullyInitialized) {
50 this->CheckFullyInitialized = CheckFullyInitialized;
51 S.EvaluatingDecl = VD;
53 EvalResult.setSource(VD);
56 this->ConvertResultToRValue = !
Init->isGLValue() && !
T->isPointerType() &&
57 !
T->isObjCObjectPointerType();
60 EvalResult.setInvalid();
62 S.EvaluatingDecl =
nullptr;
63 updateGlobalTemporaries();
64 return std::move(this->EvalResult);
71 S.EvaluatingDecl = VD;
73 EvalResult.setSource(VD);
76 EvalResult.setInvalid();
78 S.EvaluatingDecl =
nullptr;
79 return std::move(this->EvalResult);
85 this->ConvertResultToRValue =
false;
86 this->CheckFullyInitialized =
false;
88 EvalResult.setSource(E);
93 EvalResult.setInvalid();
96 return std::move(this->EvalResult);
102 this->ConvertResultToRValue =
false;
103 this->CheckFullyInitialized =
false;
105 EvalResult.setSource(E);
108 EvalResult.setInvalid();
110 return std::move(this->EvalResult);
117 this->
Params.insert({PD, {0,
false}});
130 if (EvalResult.empty() || EvalResult.isInvalid())
133 assert(!EvalResult.empty());
137 return Result.getInt().getBoolValue();
146 char *Memory =
reinterpret_cast<char *
>(
156 Desc.IsActive =
false;
158 Desc.IsFieldMutable =
false;
160 Desc.IsInitialized =
false;
163 unsigned Off = Locals.size();
164 Locals.push_back(Memory);
171 if (S.Stk.pop<
bool>())
180 if (!S.Stk.pop<
bool>())
189 CurrentLabel = ActiveLabel = Label;
197 CurrentLabel = Label;
208 size_t StackSizeBefore = S.Stk.size();
210 if (!this->
visit(Arg)) {
211 S.Stk.clearTo(StackSizeBefore);
213 if (S.inConstantContext() || Arg->
HasSideEffects(S.getASTContext()))
220 const auto &Ptr = S.Stk.pop<
Pointer>();
225 if (!this->emitPop(
T, E))
230template <PrimType OpType>
bool EvalEmitter::emitRet(
SourceInfo Info) {
239template <>
bool EvalEmitter::emitRet<PT_Ptr>(
SourceInfo Info) {
246 return (*this->PtrCB)(S,
CodePtr(), Ptr);
254 if (Ptr.isFunctionPointer()) {
255 if (ConvertResultToRValue && Ptr.asFunctionPointer().Func->getDecl())
262 if (ConvertResultToRValue) {
271 if (!Ptr.isZero() && !Ptr.isConst() && Ptr.isBlockPointer() &&
272 Ptr.block()->getEvalID() != Ctx.
getEvalID())
275 if (std::optional<APValue>
V =
277 EvalResult.takeValue(std::move(*
V));
285 if (
Ptr.isBlockPointer() && !
Ptr.block()->isStatic()) {
286 EvalResult.takeValue(
Ptr.toAPValue(Ctx.getASTContext()));
290 if (!
Ptr.isLive() && !
Ptr.isTemporary())
296 if (
const Descriptor *DeclDesc =
Ptr.getDeclDesc();
297 DeclDesc && S.EvaluatingDecl &&
298 DeclDesc->asVarDecl() == S.EvaluatingDecl &&
299 S.getLangOpts().CPlusPlus23 &&
300 S.EvaluatingDecl->getType()->isReferenceType()) {
301 V.setConstexprUnknown(
true);
303 EvalResult.takeValue(std::move(
V));
309bool EvalEmitter::emitRetVoid(
SourceInfo Info) {
310 EvalResult.setValid();
314bool EvalEmitter::emitRetValue(
SourceInfo Info) {
317 if (!EvalResult.checkDynamicAllocations(S, Ptr, Info))
319 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
322 if (std::optional<APValue> APV =
323 Ptr.toRValue(Ctx, EvalResult.getSourceType())) {
324 EvalResult.takeValue(std::move(*APV));
328 EvalResult.setInvalid();
336 Block *B = getLocal(I);
337 S.Stk.push<
Pointer>(B,
sizeof(InlineDescriptor));
345 Block *B = getLocal(I);
349template <PrimType OpType>
354 using T =
typename PrimConv<OpType>::T;
356 Block *B = getLocal(I);
365template <PrimType OpType>
370 using T =
typename PrimConv<OpType>::T;
372 Block *B = getLocal(I);
373 B->
deref<
T>() = S.Stk.pop<
T>();
375 Desc.IsInitialized =
true;
397 Block *B = getLocal(I);
400 S.Stk.push<
bool>(Desc.IsActive);
413 Block *B = getLocal(I);
415 Desc.IsActive =
true;
424void EvalEmitter::updateGlobalTemporaries() {
425 for (
const auto &[E, Temp] : S.SeenGlobalTemporaries) {
428 const Pointer &
Ptr = P.getPtrGlobal(*GlobalIndex);
429 APValue *Cached = Temp->getOrCreateValue(
true);
431 QualType TempType = E->getType();
432 if (
const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
433 TempType = MTE->getSubExpr()->skipRValueSubobjectAdjustments()->getType();
435 if (OptPrimType
T = Ctx.classify(TempType)) {
437 { *Cached =
Ptr.deref<
T>().toAPValue(Ctx.getASTContext()); });
439 if (std::optional<APValue> APV =
Ptr.toRValue(Ctx, TempType))
443 S.SeenGlobalTemporaries.clear();
451#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.
static constexpr uint8_t InlineDescMD
Pointer into the code segment.
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.
Local createLocal(const Descriptor *D)
Callback for registering a local.
std::optional< bool > interpretWithSubstitutions(const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition)
virtual bool visitDtorCall(const VarDecl *VD, const APValue &Value)=0
bool jumpFalse(const LabelTy &Label, SourceInfo SI)
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.
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 visitWithSubstitutions(const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition)=0
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)
llvm::SmallVector< SmallVector< Local, 2 >, 1 > Descriptors
Local descriptors.
virtual bool emitBool(bool V, const Expr *E)=0
bool jumpTrue(const LabelTy &Label, SourceInfo SI)
Emits jumps.
virtual bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope)=0
llvm::function_ref< bool(InterpState &S, CodePtr OpPC, const Pointer &)> PtrCallback
Defines the result of an evaluation.
bool checkDynamicAllocations(InterpState &S, const Pointer &Ptr, SourceInfo Info)
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
QualType getSourceType() const
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.
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 PushIgnoreDiags(InterpState &S)
bool PopIgnoreDiags(InterpState &S)
bool handleReference(InterpState &S, CodePtr OpPC, Block *B)
bool CheckBCPResult(InterpState &S, const Pointer &Ptr)
bool This(InterpState &S, CodePtr OpPC)
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)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
@ Result
The result type of a method or function.
OptionalUnsigned< unsigned > UnsignedOrNone
const FunctionProtoType * T
@ Off
Never emit colors regardless of the output stream.
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_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.