clang 24.0.0git
InterpState.cpp
Go to the documentation of this file.
1//===--- InterpState.cpp - Interpreter for the constexpr VM -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "InterpState.h"
10#include "EvalSettings.h"
11#include "InterpFrame.h"
12#include "InterpStack.h"
13#include "Program.h"
14#include "Reflect.h"
15#include "State.h"
16#include "clang/AST/DeclCXX.h"
18
19using namespace clang;
20using namespace clang::interp;
21
23 InterpStack &Stk, FrameAllocator &FrameAlloc,
25 : State(Ctx.getASTContext(), Settings.SProxy, Settings.EvalStatus), M(M),
26 FrameAlloc(FrameAlloc), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
27 Current(&BottomFrame), StepsLeft(Ctx.getLangOpts().ConstexprStepLimit),
28 InfiniteSteps(StepsLeft == 0), EvalID(Ctx.getEvalID()) {
33 EvalMode = Settings.EvalMode;
34}
35
37 InterpStack &Stk, FrameAllocator &FrameAlloc,
38 Context &Ctx, const Function *Func)
39 : State(Ctx.getASTContext(), Settings.SProxy, Settings.EvalStatus),
40 M(nullptr), FrameAlloc(FrameAlloc), P(P), Stk(Stk), Ctx(Ctx),
42 StepsLeft(Ctx.getLangOpts().ConstexprStepLimit),
43 InfiniteSteps(StepsLeft == 0), EvalID(Ctx.getEvalID()) {
48 EvalMode = Settings.EvalMode;
49}
50
52 FrameAllocator &FrameAlloc, Context &Ctx,
53 SourceMapper *M)
54 : State(Ctx.getASTContext(), /*SProxy=*/nullptr, Status), M(M),
55 FrameAlloc(FrameAlloc), P(P), Stk(Stk), Ctx(Ctx), BottomFrame(*this),
56 Current(&BottomFrame), StepsLeft(Ctx.getLangOpts().ConstexprStepLimit),
57 InfiniteSteps(StepsLeft == 0), EvalID(Ctx.getEvalID()) {
58 InConstantContext = true;
62}
63
70
72 assert(Current->isBottomFrame());
73
74 while (DeadBlocks) {
75 DeadBlock *Next = DeadBlocks->Next;
76
77 // There might be a pointer in a global structure pointing to the dead
78 // block.
79 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)
80 DeadBlocks->B.removePointer(P);
81
82 std::free(DeadBlocks);
83 DeadBlocks = Next;
84 }
85}
86
88 // As a last resort, make sure all pointers still pointing to a dead block
89 // don't point to it anymore.
90 if (Alloc)
91 Alloc->cleanup();
92}
93
95
96void InterpState::deallocateWithPointers(Block *B) {
97 assert(B);
98 assert(B->hasPointers());
99 assert(!B->isDynamic());
100 assert(!B->isGlobal());
101 assert(!B->isDead());
102 assert(!B->isInitialized());
103
104 size_t Size = B->getAllocSize();
105 // Allocate a new block, transferring over pointers.
106 char *Memory =
107 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
108 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
109 // Since the block doesn't hold any actual data anymore, we can just
110 // memcpy() everything over.
111 std::memcpy(D->rawData(), B->rawData(), Size);
112 D->B.IsInitialized = false;
113}
114
116 if (!Alloc)
117 return true;
118
119 bool NoAllocationsLeft = !Alloc->hasAllocations();
120
122 for (const auto &[Source, Site] : Alloc->allocation_sites()) {
123 assert(!Site.empty());
124
125 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
126 << (Site.size() - 1) << Source->getSourceRange();
127 }
128 }
129 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
130 // in the first place.
131 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;
132}
133
135 for (const InterpFrame *F = Current; F; F = F->Caller) {
136 const Function *Func = F->getFunction();
137 if (!Func)
138 continue;
139 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());
140 if (!MD)
141 continue;
142 const IdentifierInfo *FnII = MD->getIdentifier();
143 if (!FnII || !FnII->isStr(Name))
144 continue;
145
146 const auto *CTSD =
147 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());
148 if (!CTSD)
149 continue;
150
151 const IdentifierInfo *ClassII = CTSD->getIdentifier();
152 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
153 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&
154 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
155 QualType ElemType = TAL[0].getAsType();
156 const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetOpPC()));
157 return {NewCall, ElemType};
158 }
159 }
160
161 return {};
162}
163
164bool InterpState::diagnoseStepLimitExceeded(CodePtr OpPC) {
165 FFDiag(Current->getSource(OpPC), diag::note_constexpr_step_limit_exceeded, 1)
166 << getLangOpts().ConstexprStepLimit;
167 Note(Current->getSource(OpPC), diag::note_constexpr_steps);
168 return false;
169}
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
FormatToken * Next
The next token in the unwrapped line.
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
A (possibly-)qualified type.
Definition TypeBase.h:938
A template argument list.
unsigned size() const
Retrieve the number of template arguments in this template argument list.
@ Type
The template argument is a type.
A memory block, either on the stack or in the heap.
Definition Block.h:68
bool isGlobal() const
Definition Block.h:99
bool isDead() const
Definition Block.h:117
unsigned getAllocSize() const
Returns the size of the block, including metadata.
Definition Block.h:125
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition Block.h:130
bool isDynamic() const
Definition Block.h:111
bool hasPointers() const
Checks if the block has any live pointers.
Definition Block.h:105
Pointer into the code segment.
Definition Source.h:31
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:49
Descriptor for a dead block.
Definition Block.h:249
Allocator for function frames.
Base class for stack frames, shared between VM and walker.
Definition Frame.h:28
Bytecode function.
Definition Function.h:98
Frame storing local variables.
Definition InterpFrame.h:27
SourceInfo getSource(CodePtr PC) const
Map a location to a source.
Stack frame storing temporaries and parameters.
Definition InterpStack.h:26
InterpFrame BottomFrame
Bottom function frame.
Context & Ctx
Interpreter Context.
InterpState(const EvalSettings &Settings, Program &P, InterpStack &Stk, FrameAllocator &FrameAlloc, Context &Ctx, SourceMapper *M=nullptr)
const unsigned EvalID
ID identifying this evaluation.
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
unsigned StepsLeft
Steps left during evaluation.
InterpFrame * Current
The current frame.
const Frame * getCurrentFrame() override
std::optional< bool > ConstantContextOverride
const bool InfiniteSteps
Whether infinite evaluation steps have been requested.
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
Program & P
Reference to the module containing all bytecode.
A pointer to a memory block, live or dead.
Definition Pointer.h:547
The program contains and links the bytecode for all functions.
Definition Program.h:38
Interface for classes which map locations to sources.
Definition Source.h:138
EvaluationMode EvalMode
Definition State.h:193
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
Definition State.cpp:37
bool CheckingPotentialConstantExpression
Whether we're checking that an expression is a potential constant expression.
Definition State.h:183
bool CheckingForUndefinedBehavior
Whether we're checking for an expression that has undefined behavior.
Definition State.h:191
Expr::EvalStatus & EvalStatus
Definition State.h:196
ASTContext & getASTContext() const
Definition State.h:92
SemaProxy * SProxy
Definition State.h:195
State(ASTContext &ASTCtx, SemaProxy *SProxy, Expr::EvalStatus &EvalStatus)
Definition State.h:83
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition State.cpp:60
const LangOptions & getLangOpts() const
Definition State.h:94
bool checkingPotentialConstantExpression() const
Are we checking whether the expression is a potential constant expression?
Definition State.h:125
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition State.h:178
Top level wrappers for InstallAPI frontend operations.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ ConstantExpression
Evaluate as a constant expression.
Definition State.h:58
U cast(CodeGen::Address addr)
Definition Address.h:327
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition Expr.h:623
const EvaluationMode EvalMode