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 this->ConvertResultToRValue =
false;
81 this->CheckFullyInitialized =
false;
83 EvalResult.setSource(E);
88 EvalResult.setInvalid();
91 return std::move(this->EvalResult);
98 this->
Params.insert({PD, {0,
false}});
111 auto *B =
new (Memory.get())
Block(Ctx.getEvalID(), D,
false);
118 Desc.IsActive =
false;
120 Desc.IsFieldMutable =
false;
122 Desc.IsInitialized =
false;
125 unsigned Off = Locals.size();
126 Locals.push_back(std::move(Memory));
133 if (S.Stk.pop<
bool>())
142 if (!S.Stk.pop<
bool>())
151 CurrentLabel = ActiveLabel = Label;
159 CurrentLabel = Label;
170 size_t StackSizeBefore = S.Stk.size();
172 if (!this->
visit(Arg)) {
173 S.Stk.clearTo(StackSizeBefore);
175 if (S.inConstantContext() || Arg->
HasSideEffects(S.getASTContext()))
182 const auto &Ptr = S.Stk.pop<
Pointer>();
187 if (!this->emitPop(T, E))
192template <PrimType OpType>
bool EvalEmitter::emitRet(
SourceInfo Info) {
201template <>
bool EvalEmitter::emitRet<PT_Ptr>(
SourceInfo Info) {
208 return (*this->PtrCB)(Ptr);
222 if (ConvertResultToRValue) {
238 if (std::optional<APValue>
V =
240 EvalResult.takeValue(std::move(*
V));
249 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
256 EvalResult.takeValue(Ptr.
toAPValue(Ctx.getASTContext()));
262bool EvalEmitter::emitRetVoid(
SourceInfo Info) {
263 EvalResult.setValid();
267bool EvalEmitter::emitRetValue(
SourceInfo Info) {
268 const auto &Ptr = S.Stk.pop<
Pointer>();
270 if (!EvalResult.checkReturnValue(S, Ctx, Ptr, Info))
272 if (CheckFullyInitialized && !EvalResult.checkFullyInitialized(S, Ptr))
275 if (std::optional<APValue> APV =
276 Ptr.
toRValue(S.getASTContext(), EvalResult.getSourceType())) {
277 EvalResult.takeValue(std::move(*APV));
281 EvalResult.setInvalid();
285bool EvalEmitter::emitGetPtrLocal(uint32_t I,
SourceInfo Info) {
289 Block *B = getLocal(I);
290 S.Stk.push<
Pointer>(B,
sizeof(InlineDescriptor));
294template <PrimType OpType>
295bool EvalEmitter::emitGetLocal(uint32_t I,
SourceInfo Info) {
299 using T =
typename PrimConv<OpType>::T;
301 Block *B = getLocal(I);
306 S.Stk.push<T>(B->
deref<T>());
310template <PrimType OpType>
311bool EvalEmitter::emitSetLocal(uint32_t I,
SourceInfo Info) {
315 using T =
typename PrimConv<OpType>::T;
317 Block *B = getLocal(I);
318 B->
deref<T>() = S.Stk.pop<T>();
320 Desc.IsInitialized =
true;
325bool EvalEmitter::emitDestroy(uint32_t I,
SourceInfo Info) {
337bool EvalEmitter::emitGetLocalEnabled(uint32_t I,
SourceInfo Info) {
341 Block *B = getLocal(I);
344 S.Stk.push<
bool>(Desc.IsActive);
348bool EvalEmitter::emitEnableLocal(uint32_t I,
SourceInfo Info) {
357 Block *B = getLocal(I);
359 Desc.IsActive =
true;
368void EvalEmitter::updateGlobalTemporaries() {
369 for (
const auto &[E, Temp] : S.SeenGlobalTemporaries) {
370 UnsignedOrNone GlobalIndex = P.getGlobal(E);
372 const Pointer &Ptr = P.getPtrGlobal(*GlobalIndex);
373 APValue *Cached = Temp->getOrCreateValue(
true);
375 QualType TempType = E->
getType();
376 if (
const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
377 TempType = MTE->getSubExpr()->skipRValueSubobjectAdjustments()->getType();
379 if (OptPrimType T = Ctx.classify(TempType)) {
381 { *Cached = Ptr.
deref<T>().toAPValue(Ctx.getASTContext()); });
383 if (std::optional<APValue> APV = Ptr.
toRValue(Ctx, TempType))
387 S.SeenGlobalTemporaries.clear();
395#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)
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 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.
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...
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.
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 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.
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)
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.