/src/llvm-project/clang/lib/AST/Interp/Context.cpp
Line | Count | Source (jump to first uncovered line) |
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 "ByteCodeExprGen.h" |
12 | | #include "ByteCodeGenError.h" |
13 | | #include "ByteCodeStmtGen.h" |
14 | | #include "EvalEmitter.h" |
15 | | #include "Interp.h" |
16 | | #include "InterpFrame.h" |
17 | | #include "InterpStack.h" |
18 | | #include "PrimType.h" |
19 | | #include "Program.h" |
20 | | #include "clang/AST/Expr.h" |
21 | | #include "clang/Basic/TargetInfo.h" |
22 | | |
23 | | using namespace clang; |
24 | | using namespace clang::interp; |
25 | | |
26 | 0 | Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {} |
27 | | |
28 | 0 | Context::~Context() {} |
29 | | |
30 | 0 | bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) { |
31 | 0 | assert(Stk.empty()); |
32 | 0 | Function *Func = P->getFunction(FD); |
33 | 0 | if (!Func || !Func->hasBody()) { |
34 | 0 | if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) { |
35 | 0 | Func = *R; |
36 | 0 | } else { |
37 | 0 | handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) { |
38 | 0 | Parent.FFDiag(Err.getRange().getBegin(), |
39 | 0 | diag::err_experimental_clang_interp_failed) |
40 | 0 | << Err.getRange(); |
41 | 0 | }); |
42 | 0 | return false; |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | 0 | APValue DummyResult; |
47 | 0 | if (!Run(Parent, Func, DummyResult)) { |
48 | 0 | return false; |
49 | 0 | } |
50 | | |
51 | 0 | return Func->isConstexpr(); |
52 | 0 | } |
53 | | |
54 | 0 | bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) { |
55 | 0 | assert(Stk.empty()); |
56 | 0 | ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result); |
57 | 0 | if (Check(Parent, C.interpretExpr(E))) { |
58 | 0 | assert(Stk.empty()); |
59 | 0 | #ifndef NDEBUG |
60 | | // Make sure we don't rely on some value being still alive in |
61 | | // InterpStack memory. |
62 | 0 | Stk.clear(); |
63 | 0 | #endif |
64 | 0 | return true; |
65 | 0 | } |
66 | | |
67 | 0 | Stk.clear(); |
68 | 0 | return false; |
69 | 0 | } |
70 | | |
71 | | bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD, |
72 | 0 | APValue &Result) { |
73 | 0 | assert(Stk.empty()); |
74 | 0 | ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result); |
75 | 0 | if (Check(Parent, C.interpretDecl(VD))) { |
76 | 0 | assert(Stk.empty()); |
77 | 0 | #ifndef NDEBUG |
78 | | // Make sure we don't rely on some value being still alive in |
79 | | // InterpStack memory. |
80 | 0 | Stk.clear(); |
81 | 0 | #endif |
82 | 0 | return true; |
83 | 0 | } |
84 | | |
85 | 0 | Stk.clear(); |
86 | 0 | return false; |
87 | 0 | } |
88 | | |
89 | 0 | const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); } |
90 | | |
91 | 0 | std::optional<PrimType> Context::classify(QualType T) const { |
92 | 0 | if (T->isBooleanType()) |
93 | 0 | return PT_Bool; |
94 | | |
95 | 0 | if (T->isAnyComplexType()) |
96 | 0 | return std::nullopt; |
97 | | |
98 | 0 | if (T->isSignedIntegerOrEnumerationType()) { |
99 | 0 | switch (Ctx.getIntWidth(T)) { |
100 | 0 | case 64: |
101 | 0 | return PT_Sint64; |
102 | 0 | case 32: |
103 | 0 | return PT_Sint32; |
104 | 0 | case 16: |
105 | 0 | return PT_Sint16; |
106 | 0 | case 8: |
107 | 0 | return PT_Sint8; |
108 | 0 | default: |
109 | 0 | return PT_IntAPS; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | 0 | if (T->isUnsignedIntegerOrEnumerationType()) { |
114 | 0 | switch (Ctx.getIntWidth(T)) { |
115 | 0 | case 64: |
116 | 0 | return PT_Uint64; |
117 | 0 | case 32: |
118 | 0 | return PT_Uint32; |
119 | 0 | case 16: |
120 | 0 | return PT_Uint16; |
121 | 0 | case 8: |
122 | 0 | return PT_Uint8; |
123 | 0 | default: |
124 | 0 | return PT_IntAP; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | if (T->isNullPtrType()) |
129 | 0 | return PT_Ptr; |
130 | | |
131 | 0 | if (T->isFloatingType()) |
132 | 0 | return PT_Float; |
133 | | |
134 | 0 | if (T->isFunctionPointerType() || T->isFunctionReferenceType() || |
135 | 0 | T->isFunctionType() || T->isSpecificBuiltinType(BuiltinType::BoundMember)) |
136 | 0 | return PT_FnPtr; |
137 | | |
138 | 0 | if (T->isReferenceType() || T->isPointerType()) |
139 | 0 | return PT_Ptr; |
140 | | |
141 | 0 | if (const auto *AT = dyn_cast<AtomicType>(T)) |
142 | 0 | return classify(AT->getValueType()); |
143 | | |
144 | 0 | if (const auto *DT = dyn_cast<DecltypeType>(T)) |
145 | 0 | return classify(DT->getUnderlyingType()); |
146 | | |
147 | 0 | if (const auto *DT = dyn_cast<MemberPointerType>(T)) |
148 | 0 | return classify(DT->getPointeeType()); |
149 | | |
150 | 0 | return std::nullopt; |
151 | 0 | } |
152 | | |
153 | 0 | unsigned Context::getCharBit() const { |
154 | 0 | return Ctx.getTargetInfo().getCharWidth(); |
155 | 0 | } |
156 | | |
157 | | /// Simple wrapper around getFloatTypeSemantics() to make code a |
158 | | /// little shorter. |
159 | 0 | const llvm::fltSemantics &Context::getFloatSemantics(QualType T) const { |
160 | 0 | return Ctx.getFloatTypeSemantics(T); |
161 | 0 | } |
162 | | |
163 | 0 | bool Context::Run(State &Parent, const Function *Func, APValue &Result) { |
164 | |
|
165 | 0 | { |
166 | 0 | InterpState State(Parent, *P, Stk, *this); |
167 | 0 | State.Current = new InterpFrame(State, Func, /*Caller=*/nullptr, {}); |
168 | 0 | if (Interpret(State, Result)) { |
169 | 0 | assert(Stk.empty()); |
170 | 0 | return true; |
171 | 0 | } |
172 | | |
173 | | // State gets destroyed here, so the Stk.clear() below doesn't accidentally |
174 | | // remove values the State's destructor might access. |
175 | 0 | } |
176 | | |
177 | 0 | Stk.clear(); |
178 | 0 | return false; |
179 | 0 | } |
180 | | |
181 | 0 | bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) { |
182 | 0 | if (Flag) |
183 | 0 | return *Flag; |
184 | 0 | handleAllErrors(Flag.takeError(), [&Parent](ByteCodeGenError &Err) { |
185 | 0 | Parent.FFDiag(Err.getRange().getBegin(), |
186 | 0 | diag::err_experimental_clang_interp_failed) |
187 | 0 | << Err.getRange(); |
188 | 0 | }); |
189 | 0 | return false; |
190 | 0 | } |
191 | | |
192 | | // TODO: Virtual bases? |
193 | | const CXXMethodDecl * |
194 | | Context::getOverridingFunction(const CXXRecordDecl *DynamicDecl, |
195 | | const CXXRecordDecl *StaticDecl, |
196 | 0 | const CXXMethodDecl *InitialFunction) const { |
197 | |
|
198 | 0 | const CXXRecordDecl *CurRecord = DynamicDecl; |
199 | 0 | const CXXMethodDecl *FoundFunction = InitialFunction; |
200 | 0 | for (;;) { |
201 | 0 | const CXXMethodDecl *Overrider = |
202 | 0 | FoundFunction->getCorrespondingMethodDeclaredInClass(CurRecord, false); |
203 | 0 | if (Overrider) |
204 | 0 | return Overrider; |
205 | | |
206 | | // Common case of only one base class. |
207 | 0 | if (CurRecord->getNumBases() == 1) { |
208 | 0 | CurRecord = CurRecord->bases_begin()->getType()->getAsCXXRecordDecl(); |
209 | 0 | continue; |
210 | 0 | } |
211 | | |
212 | | // Otherwise, go to the base class that will lead to the StaticDecl. |
213 | 0 | for (const CXXBaseSpecifier &Spec : CurRecord->bases()) { |
214 | 0 | const CXXRecordDecl *Base = Spec.getType()->getAsCXXRecordDecl(); |
215 | 0 | if (Base == StaticDecl || Base->isDerivedFrom(StaticDecl)) { |
216 | 0 | CurRecord = Base; |
217 | 0 | break; |
218 | 0 | } |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | 0 | llvm_unreachable( |
223 | 0 | "Couldn't find an overriding function in the class hierarchy?"); |
224 | 0 | return nullptr; |
225 | 0 | } |
226 | | |
227 | 0 | const Function *Context::getOrCreateFunction(const FunctionDecl *FD) { |
228 | 0 | assert(FD); |
229 | 0 | const Function *Func = P->getFunction(FD); |
230 | 0 | bool IsBeingCompiled = Func && Func->isDefined() && !Func->isFullyCompiled(); |
231 | 0 | bool WasNotDefined = Func && !Func->isConstexpr() && !Func->isDefined(); |
232 | |
|
233 | 0 | if (IsBeingCompiled) |
234 | 0 | return Func; |
235 | | |
236 | 0 | if (!Func || WasNotDefined) { |
237 | 0 | if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) |
238 | 0 | Func = *R; |
239 | 0 | else { |
240 | 0 | llvm::consumeError(R.takeError()); |
241 | 0 | return nullptr; |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 0 | return Func; |
246 | 0 | } |