1//===--- Compiler.h - Code generator for expressions -----*- 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// Defines the constexpr bytecode compiler.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
14#define LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
15
16#include "ByteCodeEmitter.h"
17#include "EvalEmitter.h"
18#include "Pointer.h"
19#include "PrimType.h"
20#include "Record.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/StmtVisitor.h"
24
25namespace clang {
26class QualType;
27
28namespace interp {
29
30template <class Emitter> class LocalScope;
31template <class Emitter> class DestructorScope;
32template <class Emitter> class VariableScope;
33template <class Emitter> class DeclScope;
34template <class Emitter> class InitLinkScope;
35template <class Emitter> class InitStackScope;
36template <class Emitter> class OptionScope;
37template <class Emitter> class ArrayIndexScope;
38template <class Emitter> class SourceLocScope;
39template <class Emitter> class LoopScope;
40template <class Emitter> class LabelScope;
41template <class Emitter> class SwitchScope;
42template <class Emitter> class StmtExprScope;
43template <class Emitter> class LocOverrideScope;
44
45template <class Emitter> class Compiler;
46struct InitLink {
47public:
48 enum {
49 K_This = 0,
50 K_Field = 1,
51 K_Temp = 2,
52 K_Decl = 3,
53 K_Elem = 5,
54 K_RVO = 6,
55 K_InitList = 7,
56 K_DIE = 8,
57 };
58
59 static InitLink This() { return InitLink{K_This}; }
60 static InitLink InitList() { return InitLink{K_InitList}; }
61 static InitLink RVO() { return InitLink{K_RVO}; }
62 static InitLink DIE() { return InitLink{K_DIE}; }
63 static InitLink Field(unsigned Offset) {
64 InitLink IL{K_Field};
65 IL.Offset = Offset;
66 return IL;
67 }
68 static InitLink Temp(unsigned Offset) {
69 InitLink IL{K_Temp};
70 IL.Offset = Offset;
71 return IL;
72 }
73 static InitLink Decl(const ValueDecl *D) {
74 InitLink IL{K_Decl};
75 IL.D = D;
76 return IL;
77 }
78 static InitLink Elem(unsigned Index) {
79 InitLink IL{K_Elem};
80 IL.Offset = Index;
81 return IL;
82 }
83
84 InitLink(uint8_t Kind) : Kind(Kind) {}
85 template <class Emitter>
86 bool emit(Compiler<Emitter> *Ctx, const Expr *E) const;
87
88 uint32_t Kind;
89 union {
90 unsigned Offset;
91 const ValueDecl *D;
92 };
93};
94
95/// State encapsulating if a the variable creation has been successful,
96/// unsuccessful, or no variable has been created at all.
97struct VarCreationState {
98 std::optional<bool> S = std::nullopt;
99 VarCreationState() = default;
100 VarCreationState(bool b) : S(b) {}
101 static VarCreationState NotCreated() { return VarCreationState(); }
102
103 operator bool() const { return S && *S; }
104 bool notCreated() const { return !S; }
105};
106
107enum class ScopeKind { Block, FullExpression, Call };
108
109/// Compilation context for expressions.
110template <class Emitter>
111class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
112 public Emitter {
113protected:
114 // Aliases for types defined in the emitter.
115 using LabelTy = typename Emitter::LabelTy;
116 using AddrTy = typename Emitter::AddrTy;
117 using OptLabelTy = UnsignedOrNone;
118 using CaseMap = llvm::DenseMap<const SwitchCase *, LabelTy>;
119
120 struct LabelInfo {
121 const Stmt *Name;
122 const VariableScope<Emitter> *BreakOrContinueScope;
123 OptLabelTy BreakLabel;
124 OptLabelTy ContinueLabel;
125 OptLabelTy DefaultLabel;
126 LabelInfo(const Stmt *Name, OptLabelTy BreakLabel, OptLabelTy ContinueLabel,
127 OptLabelTy DefaultLabel,
128 const VariableScope<Emitter> *BreakOrContinueScope)
129 : Name(Name), BreakOrContinueScope(BreakOrContinueScope),
130 BreakLabel(BreakLabel), ContinueLabel(ContinueLabel),
131 DefaultLabel(DefaultLabel) {}
132 };
133
134 /// Current compilation context.
135 Context &Ctx;
136 /// Program to link to.
137 Program &P;
138
139public:
140 /// Initializes the compiler and the backend emitter.
141 template <typename... Tys>
142 Compiler(Context &Ctx, Program &P, Tys &&...Args)
143 : Emitter(Ctx, P, Args...), Ctx(Ctx), P(P) {}
144
145 // Expressions.
146 bool VisitCastExpr(const CastExpr *E);
147 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E);
148 bool VisitIntegerLiteral(const IntegerLiteral *E);
149 bool VisitFloatingLiteral(const FloatingLiteral *E);
150 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
151 bool VisitFixedPointLiteral(const FixedPointLiteral *E);
152 bool VisitParenExpr(const ParenExpr *E);
153 bool VisitBinaryOperator(const BinaryOperator *E);
154 bool VisitLogicalBinOp(const BinaryOperator *E);
155 bool VisitPointerArithBinOp(const BinaryOperator *E);
156 bool VisitComplexBinOp(const BinaryOperator *E);
157 bool VisitVectorBinOp(const BinaryOperator *E);
158 bool VisitFixedPointBinOp(const BinaryOperator *E);
159 bool VisitFixedPointUnaryOperator(const UnaryOperator *E);
160 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E);
161 bool VisitCallExpr(const CallExpr *E);
162 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinID);
163 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E);
164 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E);
165 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E);
166 bool VisitGNUNullExpr(const GNUNullExpr *E);
167 bool VisitCXXThisExpr(const CXXThisExpr *E);
168 bool VisitUnaryOperator(const UnaryOperator *E);
169 bool VisitVectorUnaryOperator(const UnaryOperator *E);
170 bool VisitComplexUnaryOperator(const UnaryOperator *E);
171 bool VisitDeclRefExpr(const DeclRefExpr *E);
172 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E);
173 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E);
174 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
175 bool VisitInitListExpr(const InitListExpr *E);
176 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
177 bool VisitConstantExpr(const ConstantExpr *E);
178 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
179 bool VisitMemberExpr(const MemberExpr *E);
180 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E);
181 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
182 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E);
183 bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E);
184 bool VisitStringLiteral(const StringLiteral *E);
185 bool VisitObjCStringLiteral(const ObjCStringLiteral *E);
186 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
187 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E);
188 bool VisitCharacterLiteral(const CharacterLiteral *E);
189 bool VisitCompoundAssignOperator(const CompoundAssignOperator *E);
190 bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E);
191 bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E);
192 bool VisitExprWithCleanups(const ExprWithCleanups *E);
193 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
194 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E);
195 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
196 bool VisitTypeTraitExpr(const TypeTraitExpr *E);
197 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E);
198 bool VisitLambdaExpr(const LambdaExpr *E);
199 bool VisitPredefinedExpr(const PredefinedExpr *E);
200 bool VisitCXXThrowExpr(const CXXThrowExpr *E);
201 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E);
202 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E);
203 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
204 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
205 bool VisitSourceLocExpr(const SourceLocExpr *E);
206 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
207 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
208 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
209 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E);
210 bool VisitChooseExpr(const ChooseExpr *E);
211 bool VisitEmbedExpr(const EmbedExpr *E);
212 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E);
213 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
214 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E);
215 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
216 bool VisitRequiresExpr(const RequiresExpr *E);
217 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
218 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E);
219 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E);
220 bool VisitPackIndexingExpr(const PackIndexingExpr *E);
221 bool VisitRecoveryExpr(const RecoveryExpr *E);
222 bool VisitAddrLabelExpr(const AddrLabelExpr *E);
223 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
224 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
225 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
226 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
227 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
228 bool VisitStmtExpr(const StmtExpr *E);
229 bool VisitCXXNewExpr(const CXXNewExpr *E);
230 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
231 bool VisitBlockExpr(const BlockExpr *E);
232 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
233
234 // Statements.
235 bool visitCompoundStmt(const CompoundStmt *S);
236 bool visitDeclStmt(const DeclStmt *DS, bool EvaluateConditionDecl = false);
237 bool visitReturnStmt(const ReturnStmt *RS);
238 bool visitIfStmt(const IfStmt *IS);
239 bool visitWhileStmt(const WhileStmt *S);
240 bool visitDoStmt(const DoStmt *S);
241 bool visitForStmt(const ForStmt *S);
242 bool visitCXXForRangeStmt(const CXXForRangeStmt *S);
243 bool visitBreakStmt(const BreakStmt *S);
244 bool visitContinueStmt(const ContinueStmt *S);
245 bool visitSwitchStmt(const SwitchStmt *S);
246 bool visitCaseStmt(const CaseStmt *S);
247 bool visitDefaultStmt(const DefaultStmt *S);
248 bool visitAttributedStmt(const AttributedStmt *S);
249 bool visitCXXTryStmt(const CXXTryStmt *S);
250
251protected:
252 bool visitStmt(const Stmt *S);
253 bool visitExpr(const Expr *E, bool DestroyToplevelScope) override;
254 bool visitFunc(const FunctionDecl *F) override;
255
256 bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
257 bool ConstantContext) override;
258
259protected:
260 /// Emits scope cleanup instructions.
261 bool emitCleanup();
262
263 /// Returns a record type from a record or pointer type.
264 const RecordType *getRecordTy(QualType Ty);
265
266 /// Returns a record from a record or pointer type.
267 Record *getRecord(QualType Ty);
268 Record *getRecord(const RecordDecl *RD);
269
270 /// Returns a function for the given FunctionDecl.
271 /// If the function does not exist yet, it is compiled.
272 const Function *getFunction(const FunctionDecl *FD);
273
274 OptPrimType classify(const Expr *E) const { return Ctx.classify(E); }
275 OptPrimType classify(QualType Ty) const { return Ctx.classify(T: Ty); }
276 bool canClassify(const Expr *E) const { return Ctx.canClassify(E); }
277 bool canClassify(QualType T) const { return Ctx.canClassify(T); }
278
279 /// Classifies a known primitive type.
280 PrimType classifyPrim(QualType Ty) const {
281 if (auto T = classify(Ty)) {
282 return *T;
283 }
284 llvm_unreachable("not a primitive type");
285 }
286 /// Classifies a known primitive expression.
287 PrimType classifyPrim(const Expr *E) const {
288 if (auto T = classify(E))
289 return *T;
290 llvm_unreachable("not a primitive type");
291 }
292
293 /// Evaluates an expression and places the result on the stack. If the
294 /// expression is of composite type, a local variable will be created
295 /// and a pointer to said variable will be placed on the stack.
296 bool visit(const Expr *E) override;
297 /// Compiles an initializer. This is like visit() but it will never
298 /// create a variable and instead rely on a variable already having
299 /// been created. visitInitializer() then relies on a pointer to this
300 /// variable being on top of the stack.
301 bool visitInitializer(const Expr *E);
302 bool visitAsLValue(const Expr *E);
303 /// Evaluates an expression for side effects and discards the result.
304 bool discard(const Expr *E);
305 /// Just pass evaluation on to \p E. This leaves all the parsing flags
306 /// intact.
307 bool delegate(const Expr *E);
308 /// Creates and initializes a variable from the given decl.
309 VarCreationState visitVarDecl(const VarDecl *VD, const Expr *Init,
310 bool Toplevel = false,
311 bool IsConstexprUnknown = false);
312 VarCreationState visitDecl(const VarDecl *VD,
313 bool IsConstexprUnknown = false);
314 /// Visit an APValue.
315 bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E);
316 bool visitAPValueInitializer(const APValue &Val, const Expr *E, QualType T);
317 /// Visit the given decl as if we have a reference to it.
318 bool visitDeclRef(const ValueDecl *D, const Expr *E);
319
320 /// Visits an expression and converts it to a boolean.
321 bool visitBool(const Expr *E);
322
323 bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *ArrayFiller,
324 const Expr *E);
325 bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
326 OptPrimType InitT);
327 bool visitCallArgs(ArrayRef<const Expr *> Args, const FunctionDecl *FuncDecl,
328 bool Activate, bool IsOperatorCall);
329
330 /// Creates a local primitive value.
331 unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
332 bool IsVolatile = false,
333 ScopeKind SC = ScopeKind::Block,
334 bool IsConstexprUnknown = false);
335
336 /// Allocates a space storing a local given its type.
337 UnsignedOrNone allocateLocal(DeclTy &&Decl, QualType Ty = QualType(),
338 ScopeKind = ScopeKind::Block,
339 bool IsConstexprUnknown = false);
340 UnsignedOrNone allocateTemporary(const Expr *E);
341
342private:
343 friend class VariableScope<Emitter>;
344 friend class LocalScope<Emitter>;
345 friend class DestructorScope<Emitter>;
346 friend class DeclScope<Emitter>;
347 friend class InitLinkScope<Emitter>;
348 friend class InitStackScope<Emitter>;
349 friend class OptionScope<Emitter>;
350 friend class ArrayIndexScope<Emitter>;
351 friend class SourceLocScope<Emitter>;
352 friend struct InitLink;
353 friend class LoopScope<Emitter>;
354 friend class LabelScope<Emitter>;
355 friend class SwitchScope<Emitter>;
356 friend class StmtExprScope<Emitter>;
357 friend class LocOverrideScope<Emitter>;
358
359 /// Emits a zero initializer.
360 bool visitZeroInitializer(PrimType T, QualType QT, const Expr *E);
361 bool visitZeroRecordInitializer(const Record *R, const Expr *E);
362 bool visitZeroArrayInitializer(QualType T, const Expr *E);
363 bool visitAssignment(const Expr *LHS, const Expr *RHS, const Expr *E);
364
365 /// Emits an APSInt constant.
366 bool emitConst(const llvm::APSInt &Value, PrimType Ty, const Expr *E);
367 bool emitConst(const llvm::APInt &Value, PrimType Ty, const Expr *E);
368 bool emitConst(const llvm::APSInt &Value, const Expr *E);
369 bool emitConst(const llvm::APInt &Value, const Expr *E) {
370 return emitConst(Value, classifyPrim(E), E);
371 }
372
373 /// Emits an integer constant.
374 template <typename T> bool emitConst(T Value, PrimType Ty, const Expr *E);
375 template <typename T> bool emitConst(T Value, const Expr *E);
376 bool emitBool(bool V, const Expr *E) override {
377 return this->emitConst(V, E);
378 }
379
380 llvm::RoundingMode getRoundingMode(const Expr *E) const {
381 FPOptions FPO = E->getFPFeaturesInEffect(LO: Ctx.getLangOpts());
382
383 if (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic)
384 return llvm::RoundingMode::NearestTiesToEven;
385
386 return FPO.getRoundingMode();
387 }
388
389 uint32_t getFPOptions(const Expr *E) const {
390 return E->getFPFeaturesInEffect(LO: Ctx.getLangOpts()).getAsOpaqueInt();
391 }
392
393 bool emitPrimCast(PrimType FromT, PrimType ToT, QualType ToQT, const Expr *E);
394 bool emitIntegralCast(PrimType FromT, PrimType ToT, QualType ToQT,
395 const Expr *E);
396 PrimType classifyComplexElementType(QualType T) const {
397 assert(T->isAnyComplexType());
398
399 QualType ElemType = T->getAs<ComplexType>()->getElementType();
400
401 return *this->classify(ElemType);
402 }
403
404 PrimType classifyVectorElementType(QualType T) const {
405 assert(T->isVectorType());
406 return *this->classify(T->getAs<VectorType>()->getElementType());
407 }
408
409 bool emitComplexReal(const Expr *SubExpr);
410 bool emitComplexBoolCast(const Expr *E);
411 bool emitComplexComparison(const Expr *LHS, const Expr *RHS,
412 const BinaryOperator *E);
413 bool emitRecordDestructionPop(const Record *R, SourceInfo Loc);
414 bool emitDestructionPop(const Descriptor *Desc, SourceInfo Loc);
415 bool emitDummyPtr(const DeclTy &D, const Expr *E);
416 bool emitFloat(const APFloat &F, const Expr *E);
417 unsigned collectBaseOffset(const QualType BaseType,
418 const QualType DerivedType);
419 bool emitLambdaStaticInvokerBody(const CXXMethodDecl *MD);
420 bool emitBuiltinBitCast(const CastExpr *E);
421 bool compileConstructor(const CXXConstructorDecl *Ctor);
422 bool compileDestructor(const CXXDestructorDecl *Dtor);
423 bool compileUnionAssignmentOperator(const CXXMethodDecl *MD);
424
425 bool checkLiteralType(const Expr *E);
426 bool maybeEmitDeferredVarInit(const VarDecl *VD);
427
428 bool refersToUnion(const Expr *E);
429
430protected:
431 /// Variable to storage mapping.
432 llvm::DenseMap<const ValueDecl *, Scope::Local> Locals;
433
434 /// OpaqueValueExpr to location mapping.
435 llvm::DenseMap<const OpaqueValueExpr *, unsigned> OpaqueExprs;
436
437 /// Current scope.
438 VariableScope<Emitter> *VarScope = nullptr;
439
440 /// Current argument index. Needed to emit ArrayInitIndexExpr.
441 std::optional<uint64_t> ArrayIndex;
442
443 /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
444 const Expr *SourceLocDefaultExpr = nullptr;
445
446 /// Flag indicating if return value is to be discarded.
447 bool DiscardResult = false;
448
449 bool InStmtExpr = false;
450 bool ToLValue = false;
451
452 /// Flag inidicating if we're initializing an already created
453 /// variable. This is set in visitInitializer().
454 bool Initializing = false;
455 const ValueDecl *InitializingDecl = nullptr;
456
457 llvm::SmallVector<InitLink> InitStack;
458 bool InitStackActive = false;
459
460 /// Type of the expression returned by the function.
461 OptPrimType ReturnType;
462
463 /// Switch case mapping.
464 CaseMap CaseLabels;
465 /// Stack of label information for loops and switch statements.
466 llvm::SmallVector<LabelInfo> LabelInfoStack;
467
468 const FunctionDecl *CompilingFunction = nullptr;
469};
470
471extern template class Compiler<ByteCodeEmitter>;
472extern template class Compiler<EvalEmitter>;
473
474/// Scope chain managing the variable lifetimes.
475template <class Emitter> class VariableScope {
476public:
477 VariableScope(Compiler<Emitter> *Ctx, ScopeKind Kind = ScopeKind::Block)
478 : Ctx(Ctx), Parent(Ctx->VarScope), Kind(Kind) {
479 if (Parent)
480 this->LocalsAlwaysEnabled = Parent->LocalsAlwaysEnabled;
481 Ctx->VarScope = this;
482 }
483
484 virtual ~VariableScope() { Ctx->VarScope = this->Parent; }
485
486 virtual void addLocal(Scope::Local Local) {
487 llvm_unreachable("Shouldn't be called");
488 }
489 /// Like addExtended, but adds to the nearest scope of the given kind.
490 void addForScopeKind(const Scope::Local &Local, ScopeKind Kind) {
491 VariableScope *P = this;
492 while (P) {
493 if (P->Kind == Kind) {
494 P->addLocal(Local);
495 return;
496 }
497 P = P->Parent;
498 if (!P)
499 break;
500 }
501
502 // Add to this scope.
503 this->addLocal(Local);
504 }
505
506 virtual bool emitDestructors(const Expr *E = nullptr) { return true; }
507 virtual bool destroyLocals(const Expr *E = nullptr) { return true; }
508 virtual void forceInit() {}
509 VariableScope *getParent() const { return Parent; }
510 ScopeKind getKind() const { return Kind; }
511
512 /// Whether locals added to this scope are enabled by default.
513 /// This is almost always true, except for the two branches
514 /// of a conditional operator.
515 bool LocalsAlwaysEnabled = true;
516
517protected:
518 /// Compiler instance.
519 Compiler<Emitter> *Ctx;
520 /// Link to the parent scope.
521 VariableScope *Parent;
522 ScopeKind Kind;
523};
524
525/// Generic scope for local variables.
526template <class Emitter> class LocalScope : public VariableScope<Emitter> {
527public:
528 LocalScope(Compiler<Emitter> *Ctx, ScopeKind Kind = ScopeKind::Block)
529 : VariableScope<Emitter>(Ctx, Kind) {}
530
531 /// Emit a Destroy op for this scope.
532 ~LocalScope() override {
533 if (!Idx)
534 return;
535 this->Ctx->emitDestroy(*Idx, SourceInfo{});
536 removeStoredOpaqueValues();
537 }
538 /// Explicit destruction of local variables.
539 bool destroyLocals(const Expr *E = nullptr) override {
540 if (!Idx)
541 return true;
542
543 // NB: We are *not* resetting Idx here as to allow multiple
544 // calls to destroyLocals().
545 bool Success = this->emitDestructors(E);
546 this->Ctx->emitDestroy(*Idx, E);
547 return Success;
548 }
549
550 void addLocal(Scope::Local Local) override {
551 if (!Idx) {
552 Idx = static_cast<unsigned>(this->Ctx->Descriptors.size());
553 this->Ctx->Descriptors.emplace_back();
554 this->Ctx->emitInitScope(*Idx, {});
555 }
556
557 Local.EnabledByDefault = this->LocalsAlwaysEnabled;
558 this->Ctx->Descriptors[*Idx].emplace_back(Local);
559 }
560
561 /// Force-initialize this scope. Usually, scopes are lazily initialized when
562 /// the first local variable is created, but in scenarios with conditonal
563 /// operators, we need to ensure scope is initialized just in case one of the
564 /// arms will create a local and the other won't. In such a case, the
565 /// InitScope() op would be part of the arm that created the local.
566 void forceInit() override {
567 if (!Idx) {
568 Idx = static_cast<unsigned>(this->Ctx->Descriptors.size());
569 this->Ctx->Descriptors.emplace_back();
570 this->Ctx->emitInitScope(*Idx, {});
571 }
572 }
573
574 bool emitDestructors(const Expr *E = nullptr) override {
575 if (!Idx)
576 return true;
577
578 // Emit destructor calls for local variables of record
579 // type with a destructor.
580 for (Scope::Local &Local : llvm::reverse(this->Ctx->Descriptors[*Idx])) {
581 if (Local.Desc->hasTrivialDtor())
582 continue;
583
584 if (!Local.EnabledByDefault) {
585 typename Emitter::LabelTy EndLabel = this->Ctx->getLabel();
586 if (!this->Ctx->emitGetLocalEnabled(Local.Offset, E))
587 return false;
588 if (!this->Ctx->jumpFalse(EndLabel))
589 return false;
590
591 if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))
592 return false;
593
594 if (!this->Ctx->emitDestructionPop(Local.Desc, Local.Desc->getLoc()))
595 return false;
596
597 this->Ctx->emitLabel(EndLabel);
598 } else {
599 if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))
600 return false;
601 if (!this->Ctx->emitDestructionPop(Local.Desc, Local.Desc->getLoc()))
602 return false;
603 }
604
605 removeIfStoredOpaqueValue(Local);
606 }
607 return true;
608 }
609
610 void removeStoredOpaqueValues() {
611 if (!Idx)
612 return;
613
614 for (const Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
615 removeIfStoredOpaqueValue(Local);
616 }
617 }
618
619 void removeIfStoredOpaqueValue(const Scope::Local &Local) {
620 if (const auto *OVE =
621 llvm::dyn_cast_if_present<OpaqueValueExpr>(Val: Local.Desc->asExpr())) {
622 if (auto It = this->Ctx->OpaqueExprs.find(OVE);
623 It != this->Ctx->OpaqueExprs.end())
624 this->Ctx->OpaqueExprs.erase(It);
625 };
626 }
627
628 /// Index of the scope in the chain.
629 UnsignedOrNone Idx = std::nullopt;
630};
631
632template <class Emitter> class ArrayIndexScope final {
633public:
634 ArrayIndexScope(Compiler<Emitter> *Ctx, uint64_t Index) : Ctx(Ctx) {
635 OldArrayIndex = Ctx->ArrayIndex;
636 Ctx->ArrayIndex = Index;
637 }
638
639 ~ArrayIndexScope() { Ctx->ArrayIndex = OldArrayIndex; }
640
641private:
642 Compiler<Emitter> *Ctx;
643 std::optional<uint64_t> OldArrayIndex;
644};
645
646template <class Emitter> class SourceLocScope final {
647public:
648 SourceLocScope(Compiler<Emitter> *Ctx, const Expr *DefaultExpr) : Ctx(Ctx) {
649 assert(DefaultExpr);
650 // We only switch if the current SourceLocDefaultExpr is null.
651 if (!Ctx->SourceLocDefaultExpr) {
652 Enabled = true;
653 Ctx->SourceLocDefaultExpr = DefaultExpr;
654 }
655 }
656
657 ~SourceLocScope() {
658 if (Enabled)
659 Ctx->SourceLocDefaultExpr = nullptr;
660 }
661
662private:
663 Compiler<Emitter> *Ctx;
664 bool Enabled = false;
665};
666
667template <class Emitter> class InitLinkScope final {
668public:
669 InitLinkScope(Compiler<Emitter> *Ctx, InitLink &&Link) : Ctx(Ctx) {
670 Ctx->InitStack.push_back(std::move(Link));
671 }
672
673 ~InitLinkScope() { this->Ctx->InitStack.pop_back(); }
674
675public:
676 Compiler<Emitter> *Ctx;
677};
678
679template <class Emitter> class InitStackScope final {
680public:
681 InitStackScope(Compiler<Emitter> *Ctx, bool Active)
682 : Ctx(Ctx), OldValue(Ctx->InitStackActive), Active(Active) {
683 Ctx->InitStackActive = Active;
684 if (Active)
685 Ctx->InitStack.push_back(InitLink::DIE());
686 }
687
688 ~InitStackScope() {
689 this->Ctx->InitStackActive = OldValue;
690 if (Active)
691 Ctx->InitStack.pop_back();
692 }
693
694private:
695 Compiler<Emitter> *Ctx;
696 bool OldValue;
697 bool Active;
698};
699
700} // namespace interp
701} // namespace clang
702
703#endif
704