clang 23.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 "InterpFrame.h"
11#include "InterpStack.h"
12#include "Program.h"
13#include "State.h"
14#include "clang/AST/DeclCXX.h"
16
17using namespace clang;
18using namespace clang::interp;
19
32
45
52
54 while (Current && !Current->isBottomFrame()) {
55 InterpFrame *Next = Current->Caller;
56 delete Current;
57 Current = Next;
58 }
59 BottomFrame.destroyScopes();
60
61 while (DeadBlocks) {
62 DeadBlock *Next = DeadBlocks->Next;
63
64 // There might be a pointer in a global structure pointing to the dead
65 // block.
66 for (Pointer *P = DeadBlocks->B.Pointers; P; P = P->asBlockPointer().Next)
67 DeadBlocks->B.removePointer(P);
68
69 std::free(DeadBlocks);
70 DeadBlocks = Next;
71 }
72}
73
75 // As a last resort, make sure all pointers still pointing to a dead block
76 // don't point to it anymore.
77 if (Alloc)
78 Alloc->cleanup();
79}
80
82
84 assert(B);
85 assert(!B->isDynamic());
86 assert(!B->isStatic());
87 assert(!B->isDead());
88
89 // The block might have a pointer saved in a field in its data
90 // that points to the block itself. We call the dtor first,
91 // which will destroy all the data but leave InlineDescriptors
92 // intact. If the block THEN still has pointers, we create a
93 // DeadBlock for it.
94 if (B->IsInitialized)
95 B->invokeDtor();
96
97 assert(!B->isInitialized());
98 if (B->hasPointers()) {
99 size_t Size = B->getSize();
100 // Allocate a new block, transferring over pointers.
101 char *Memory =
102 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock) + Size));
103 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
104 // Since the block doesn't hold any actual data anymore, we can just
105 // memcpy() everything over.
106 std::memcpy(D->rawData(), B->rawData(), Size);
107 D->B.IsInitialized = false;
108 }
109}
110
112 if (!Alloc)
113 return true;
114
115 bool NoAllocationsLeft = !Alloc->hasAllocations();
116
118 for (const auto &[Source, Site] : Alloc->allocation_sites()) {
119 assert(!Site.empty());
120
121 CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)
122 << (Site.size() - 1) << Source->getSourceRange();
123 }
124 }
125 // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there
126 // in the first place.
127 return NoAllocationsLeft || !getLangOpts().CPlusPlus20;
128}
129
131 for (const InterpFrame *F = Current; F; F = F->Caller) {
132 const Function *Func = F->getFunction();
133 if (!Func)
134 continue;
135 const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Func->getDecl());
136 if (!MD)
137 continue;
138 const IdentifierInfo *FnII = MD->getIdentifier();
139 if (!FnII || !FnII->isStr(Name))
140 continue;
141
142 const auto *CTSD =
143 dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent());
144 if (!CTSD)
145 continue;
146
147 const IdentifierInfo *ClassII = CTSD->getIdentifier();
148 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
149 if (CTSD->isInStdNamespace() && ClassII && ClassII->isStr("allocator") &&
150 TAL.size() >= 1 && TAL[0].getKind() == TemplateArgument::Type) {
151 QualType ElemType = TAL[0].getAsType();
152 const auto *NewCall = cast<CallExpr>(F->Caller->getExpr(F->getRetPC()));
153 return {NewCall, ElemType};
154 }
155 }
156
157 return {};
158}
159
161 if (InfiniteSteps)
162 return true;
163
164 --StepsLeft;
165 if (StepsLeft != 0)
166 return true;
167
168 FFDiag(Current->getSource(OpPC), diag::note_constexpr_step_limit_exceeded);
169 return false;
170}
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:937
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 InterpBlock.h:44
unsigned getSize() const
Returns the size of the block.
Definition InterpBlock.h:87
void invokeDtor()
Invokes the Destructor.
bool isDead() const
Definition InterpBlock.h:85
bool isStatic() const
Checks if the block has static storage duration.
Definition InterpBlock.h:79
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.
Definition InterpBlock.h:92
bool isDynamic() const
Definition InterpBlock.h:83
bool hasPointers() const
Checks if the block has any live pointers.
Definition InterpBlock.h:75
Pointer into the code segment.
Definition Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:47
Descriptor for a dead block.
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
Frame storing local variables.
Definition InterpFrame.h:27
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
InterpFrame BottomFrame
Bottom function frame.
Context & Ctx
Interpreter Context.
bool noteStep(CodePtr OpPC)
Note that a step has been executed.
const unsigned EvalID
ID identifying this evaluation.
InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
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.
void deallocate(Block *B)
Deallocates a pointer.
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:97
The program contains and links the bytecode for all functions.
Definition Program.h:36
Interface for classes which map locations to sources.
Definition Source.h:101
EvaluationMode EvalMode
Definition State.h:190
Expr::EvalStatus & getEvalStatus() const
Definition State.h:92
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:21
State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus)
Definition State.h:83
bool CheckingPotentialConstantExpression
Whether we're checking that an expression is a potential constant expression.
Definition State.h:180
bool CheckingForUndefinedBehavior
Whether we're checking for an expression that has undefined behavior.
Definition State.h:188
ASTContext & getASTContext() const
Definition State.h:93
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:42
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:120
bool InConstantContext
Whether or not we're in a context where the front end requires a constant value.
Definition State.h:175
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Definition Address.h:327