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