clang 20.0.0git
Context.cpp
Go to the documentation of this file.
1//===--- Context.cpp - Context 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 "Context.h"
10#include "ByteCodeEmitter.h"
11#include "Compiler.h"
12#include "EvalEmitter.h"
13#include "Interp.h"
14#include "InterpFrame.h"
15#include "InterpStack.h"
16#include "PrimType.h"
17#include "Program.h"
18#include "clang/AST/Expr.h"
20
21using namespace clang;
22using namespace clang::interp;
23
24Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
25
27
29 assert(Stk.empty());
30 Function *Func = P->getFunction(FD);
31 if (!Func || !Func->hasBody())
32 Func = Compiler<ByteCodeEmitter>(*this, *P).compileFunc(FD);
33
34 if (!Func)
35 return false;
36
37 APValue DummyResult;
38 if (!Run(Parent, Func, DummyResult))
39 return false;
40
41 return Func->isConstexpr();
42}
43
45 ++EvalID;
46 bool Recursing = !Stk.empty();
47 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
48
49 auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/E->isGLValue());
50
51 if (Res.isInvalid()) {
52 C.cleanup();
53 Stk.clear();
54 return false;
55 }
56
57 if (!Recursing) {
58 assert(Stk.empty());
59#ifndef NDEBUG
60 // Make sure we don't rely on some value being still alive in
61 // InterpStack memory.
62 Stk.clear();
63#endif
64 }
65
66 Result = Res.toAPValue();
67
68 return true;
69}
70
72 ++EvalID;
73 bool Recursing = !Stk.empty();
74 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
75
76 auto Res = C.interpretExpr(E);
77 if (Res.isInvalid()) {
78 C.cleanup();
79 Stk.clear();
80 return false;
81 }
82
83 if (!Recursing) {
84 assert(Stk.empty());
85#ifndef NDEBUG
86 // Make sure we don't rely on some value being still alive in
87 // InterpStack memory.
88 Stk.clear();
89#endif
90 }
91
92 Result = Res.toAPValue();
93 return true;
94}
95
97 APValue &Result) {
98 ++EvalID;
99 bool Recursing = !Stk.empty();
100 Compiler<EvalEmitter> C(*this, *P, Parent, Stk);
101
104 (VD->getType()->isRecordType() || VD->getType()->isArrayType());
105 auto Res = C.interpretDecl(VD, CheckGlobalInitialized);
106 if (Res.isInvalid()) {
107 C.cleanup();
108 Stk.clear();
109 return false;
110 }
111
112 if (!Recursing) {
113 assert(Stk.empty());
114#ifndef NDEBUG
115 // Make sure we don't rely on some value being still alive in
116 // InterpStack memory.
117 Stk.clear();
118#endif
119 }
120
121 Result = Res.toAPValue();
122 return true;
123}
124
125const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
126
127std::optional<PrimType> Context::classify(QualType T) const {
128 if (T->isBooleanType())
129 return PT_Bool;
130
131 // We map these to primitive arrays.
132 if (T->isAnyComplexType() || T->isVectorType())
133 return std::nullopt;
134
136 switch (Ctx.getIntWidth(T)) {
137 case 64:
138 return PT_Sint64;
139 case 32:
140 return PT_Sint32;
141 case 16:
142 return PT_Sint16;
143 case 8:
144 return PT_Sint8;
145 default:
146 return PT_IntAPS;
147 }
148 }
149
151 switch (Ctx.getIntWidth(T)) {
152 case 64:
153 return PT_Uint64;
154 case 32:
155 return PT_Uint32;
156 case 16:
157 return PT_Uint16;
158 case 8:
159 return PT_Uint8;
160 default:
161 return PT_IntAP;
162 }
163 }
164
165 if (T->isNullPtrType())
166 return PT_Ptr;
167
168 if (T->isFloatingType())
169 return PT_Float;
170
171 if (T->isSpecificBuiltinType(BuiltinType::BoundMember) ||
173 return PT_MemberPtr;
174
176 T->isFunctionType())
177 return PT_FnPtr;
178
179 if (T->isReferenceType() || T->isPointerType() ||
181 return PT_Ptr;
182
183 if (const auto *AT = T->getAs<AtomicType>())
184 return classify(AT->getValueType());
185
186 if (const auto *DT = dyn_cast<DecltypeType>(T))
187 return classify(DT->getUnderlyingType());
188
189 return std::nullopt;
190}
191
192unsigned Context::getCharBit() const {
193 return Ctx.getTargetInfo().getCharWidth();
194}
195
196/// Simple wrapper around getFloatTypeSemantics() to make code a
197/// little shorter.
198const llvm::fltSemantics &Context::getFloatSemantics(QualType T) const {
199 return Ctx.getFloatTypeSemantics(T);
200}
201
202bool Context::Run(State &Parent, const Function *Func, APValue &Result) {
203
204 {
205 InterpState State(Parent, *P, Stk, *this);
206 State.Current = new InterpFrame(State, Func, /*Caller=*/nullptr, CodePtr(),
207 Func->getArgSize());
208 if (Interpret(State, Result)) {
209 assert(Stk.empty());
210 return true;
211 }
212
213 // State gets destroyed here, so the Stk.clear() below doesn't accidentally
214 // remove values the State's destructor might access.
215 }
216
217 Stk.clear();
218 return false;
219}
220
221// TODO: Virtual bases?
222const CXXMethodDecl *
224 const CXXRecordDecl *StaticDecl,
225 const CXXMethodDecl *InitialFunction) const {
226 assert(DynamicDecl);
227 assert(StaticDecl);
228 assert(InitialFunction);
229
230 const CXXRecordDecl *CurRecord = DynamicDecl;
231 const CXXMethodDecl *FoundFunction = InitialFunction;
232 for (;;) {
233 const CXXMethodDecl *Overrider =
234 FoundFunction->getCorrespondingMethodDeclaredInClass(CurRecord, false);
235 if (Overrider)
236 return Overrider;
237
238 // Common case of only one base class.
239 if (CurRecord->getNumBases() == 1) {
240 CurRecord = CurRecord->bases_begin()->getType()->getAsCXXRecordDecl();
241 continue;
242 }
243
244 // Otherwise, go to the base class that will lead to the StaticDecl.
245 for (const CXXBaseSpecifier &Spec : CurRecord->bases()) {
246 const CXXRecordDecl *Base = Spec.getType()->getAsCXXRecordDecl();
247 if (Base == StaticDecl || Base->isDerivedFrom(StaticDecl)) {
248 CurRecord = Base;
249 break;
250 }
251 }
252 }
253
254 llvm_unreachable(
255 "Couldn't find an overriding function in the class hierarchy?");
256 return nullptr;
257}
258
260 assert(FD);
261 const Function *Func = P->getFunction(FD);
262 bool IsBeingCompiled = Func && Func->isDefined() && !Func->isFullyCompiled();
263 bool WasNotDefined = Func && !Func->isConstexpr() && !Func->isDefined();
264
265 if (IsBeingCompiled)
266 return Func;
267
268 if (!Func || WasNotDefined) {
269 if (auto F = Compiler<ByteCodeEmitter>(*this, *P).compileFunc(FD))
270 Func = F;
271 }
272
273 return Func;
274}
275
276unsigned Context::collectBaseOffset(const RecordDecl *BaseDecl,
277 const RecordDecl *DerivedDecl) const {
278 assert(BaseDecl);
279 assert(DerivedDecl);
280 const auto *FinalDecl = cast<CXXRecordDecl>(BaseDecl);
281 const RecordDecl *CurDecl = DerivedDecl;
282 const Record *CurRecord = P->getOrCreateRecord(CurDecl);
283 assert(CurDecl && FinalDecl);
284
285 unsigned OffsetSum = 0;
286 for (;;) {
287 assert(CurRecord->getNumBases() > 0);
288 // One level up
289 for (const Record::Base &B : CurRecord->bases()) {
290 const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
291
292 if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
293 OffsetSum += B.Offset;
294 CurRecord = B.R;
295 CurDecl = BaseDecl;
296 break;
297 }
298 }
299 if (CurDecl == FinalDecl)
300 break;
301 }
302
303 assert(OffsetSum > 0);
304 return OffsetSum;
305}
306
307const Record *Context::getRecord(const RecordDecl *D) const {
308 return P->getOrCreateRecord(D);
309}
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
Expr * E
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:186
unsigned getIntWidth(QualType T) const
const llvm::fltSemantics & getFloatTypeSemantics(QualType T) const
Return the APFloat 'semantics' for the specified scalar floating point type.
const LangOptions & getLangOpts() const
Definition: ASTContext.h:796
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:778
Represents a base class of a C++ class.
Definition: DeclCXX.h:146
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:249
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2060
CXXMethodDecl * getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD, bool MayBeBase=false)
Find if RD declares a function that overrides this function, and if so, return it.
Definition: DeclCXX.cpp:2210
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
base_class_range bases()
Definition: DeclCXX.h:619
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:613
base_class_iterator bases_begin()
Definition: DeclCXX.h:626
This represents one expression.
Definition: Expr.h:110
bool isGLValue() const
Definition: Expr.h:280
Represents a function declaration or definition.
Definition: Decl.h:1932
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:461
A (possibly-)qualified type.
Definition: Type.h:941
Represents a struct/union/class.
Definition: Decl.h:4141
unsigned getCharWidth() const
Definition: TargetInfo.h:496
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1882
bool isBooleanType() const
Definition: Type.h:8423
bool isFunctionReferenceType() const
Definition: Type.h:8039
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:2167
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:2217
bool isArrayType() const
Definition: Type.h:8064
bool isFunctionPointerType() const
Definition: Type.h:8032
bool isPointerType() const
Definition: Type.h:7996
bool isReferenceType() const
Definition: Type.h:8010
bool isSpecificBuiltinType(unsigned K) const
Test for a particular builtin type.
Definition: Type.h:8264
bool isAnyComplexType() const
Definition: Type.h:8100
bool isMemberPointerType() const
Definition: Type.h:8046
bool isFunctionType() const
Definition: Type.h:7992
bool isObjCObjectPointerType() const
Definition: Type.h:8134
bool isVectorType() const
Definition: Type.h:8104
bool isFloatingType() const
Definition: Type.cpp:2249
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8516
bool isNullPtrType() const
Definition: Type.h:8328
bool isRecordType() const
Definition: Type.h:8092
QualType getType() const
Definition: Decl.h:678
Represents a variable declaration or definition.
Definition: Decl.h:879
Pointer into the code segment.
Definition: Source.h:30
Compilation context for expressions.
Definition: Compiler.h:104
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:125
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:26
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition: Context.cpp:24
bool evaluate(State &Parent, const Expr *E, APValue &Result)
Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
Definition: Context.cpp:71
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:192
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:96
const llvm::fltSemantics & getFloatSemantics(QualType T) const
Return the floating-point semantics for T.
Definition: Context.cpp:198
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition: Context.h:97
unsigned collectBaseOffset(const RecordDecl *BaseDecl, const RecordDecl *DerivedDecl) const
Definition: Context.cpp:276
const Record * getRecord(const RecordDecl *D) const
Definition: Context.cpp:307
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl)
Checks if a function is a potential constant expression.
Definition: Context.cpp:28
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:44
const CXXMethodDecl * getOverridingFunction(const CXXRecordDecl *DynamicDecl, const CXXRecordDecl *StaticDecl, const CXXMethodDecl *InitialFunction) const
Definition: Context.cpp:223
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:127
const Function * getOrCreateFunction(const FunctionDecl *FD)
Definition: Context.cpp:259
Bytecode function.
Definition: Function.h:77
Frame storing local variables.
Definition: InterpFrame.h:26
void clear()
Clears the stack without calling any destructors.
Definition: InterpStack.cpp:25
bool empty() const
Returns whether the stack is empty.
Definition: InterpStack.h:91
Interpreter context.
Definition: InterpState.h:36
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Structure/Class descriptor.
Definition: Record.h:25
unsigned getNumBases() const
Definition: Record.h:89
llvm::iterator_range< const_base_iter > bases() const
Definition: Record.h:85
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
Defines the clang::TargetInfo interface.
bool Interpret(InterpState &S, APValue &Result)
Interpreter entry point.
Definition: Interp.cpp:929
bool CheckGlobalInitialized(InterpState &S, CodePtr OpPC, const Pointer &Ptr)
Check if a global variable is initialized.
Definition: Interp.cpp:497
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
const FunctionProtoType * T