1 //===- Stmt.h - Classes for representing statements -------------*- 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 // This file defines the Stmt interface and subclasses. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_AST_STMT_H 14 #define LLVM_CLANG_AST_STMT_H 15 16 #include "clang/AST/APValue.h" 17 #include "clang/AST/DeclGroup.h" 18 #include "clang/AST/DependenceFlags.h" 19 #include "clang/AST/OperationKinds.h" 20 #include "clang/AST/StmtIterator.h" 21 #include "clang/Basic/CapturedStmt.h" 22 #include "clang/Basic/IdentifierTable.h" 23 #include "clang/Basic/LLVM.h" 24 #include "clang/Basic/Lambda.h" 25 #include "clang/Basic/LangOptions.h" 26 #include "clang/Basic/OperatorKinds.h" 27 #include "clang/Basic/SourceLocation.h" 28 #include "clang/Basic/Specifiers.h" 29 #include "clang/Basic/TypeTraits.h" 30 #include "llvm/ADT/APFloat.h" 31 #include "llvm/ADT/ArrayRef.h" 32 #include "llvm/ADT/BitmaskEnum.h" 33 #include "llvm/ADT/PointerIntPair.h" 34 #include "llvm/ADT/StringRef.h" 35 #include "llvm/ADT/iterator.h" 36 #include "llvm/ADT/iterator_range.h" 37 #include "llvm/Support/Casting.h" 38 #include "llvm/Support/Compiler.h" 39 #include "llvm/Support/ErrorHandling.h" 40 #include <algorithm> 41 #include <cassert> 42 #include <cstddef> 43 #include <iterator> 44 #include <optional> 45 #include <string> 46 47 namespace llvm { 48 49 class FoldingSetNodeID; 50 51 } // namespace llvm 52 53 namespace clang { 54 55 class ASTContext; 56 class Attr; 57 class CapturedDecl; 58 class Decl; 59 class Expr; 60 class AddrLabelExpr; 61 class LabelDecl; 62 class ODRHash; 63 class PrinterHelper; 64 struct PrintingPolicy; 65 class RecordDecl; 66 class SourceManager; 67 class StringLiteral; 68 class Token; 69 class VarDecl; 70 enum class CharacterLiteralKind; 71 enum class ConstantResultStorageKind; 72 enum class CXXConstructionKind; 73 enum class CXXNewInitializationStyle; 74 enum class PredefinedIdentKind; 75 enum class SourceLocIdentKind; 76 enum class StringLiteralKind; 77 78 //===----------------------------------------------------------------------===// 79 // AST classes for statements. 80 //===----------------------------------------------------------------------===// 81 82 /// Stmt - This represents one statement. 83 /// 84 class alignas(void *) Stmt { 85 public: 86 enum StmtClass { 87 NoStmtClass = 0, 88 #define STMT(CLASS, PARENT) CLASS##Class, 89 #define STMT_RANGE(BASE, FIRST, LAST) \ 90 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class, 91 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \ 92 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class 93 #define ABSTRACT_STMT(STMT) 94 #include "clang/AST/StmtNodes.inc" 95 }; 96 97 // Make vanilla 'new' and 'delete' illegal for Stmts. 98 protected: 99 friend class ASTStmtReader; 100 friend class ASTStmtWriter; 101 102 void *operator new(size_t bytes) noexcept { 103 llvm_unreachable("Stmts cannot be allocated with regular 'new'."); 104 } 105 106 void operator delete(void *data) noexcept { 107 llvm_unreachable("Stmts cannot be released with regular 'delete'."); 108 } 109 110 //===--- Statement bitfields classes ---===// 111 112 class StmtBitfields { 113 friend class ASTStmtReader; 114 friend class ASTStmtWriter; 115 friend class Stmt; 116 117 /// The statement class. 118 LLVM_PREFERRED_TYPE(StmtClass) 119 unsigned sClass : 8; 120 }; 121 enum { NumStmtBits = 8 }; 122 123 class NullStmtBitfields { 124 friend class ASTStmtReader; 125 friend class ASTStmtWriter; 126 friend class NullStmt; 127 128 LLVM_PREFERRED_TYPE(StmtBitfields) 129 unsigned : NumStmtBits; 130 131 /// True if the null statement was preceded by an empty macro, e.g: 132 /// @code 133 /// #define CALL(x) 134 /// CALL(0); 135 /// @endcode 136 LLVM_PREFERRED_TYPE(bool) 137 unsigned HasLeadingEmptyMacro : 1; 138 139 /// The location of the semi-colon. 140 SourceLocation SemiLoc; 141 }; 142 143 class CompoundStmtBitfields { 144 friend class ASTStmtReader; 145 friend class CompoundStmt; 146 147 LLVM_PREFERRED_TYPE(StmtBitfields) 148 unsigned : NumStmtBits; 149 150 /// True if the compound statement has one or more pragmas that set some 151 /// floating-point features. 152 LLVM_PREFERRED_TYPE(bool) 153 unsigned HasFPFeatures : 1; 154 155 unsigned NumStmts; 156 }; 157 158 class LabelStmtBitfields { 159 friend class LabelStmt; 160 161 LLVM_PREFERRED_TYPE(StmtBitfields) 162 unsigned : NumStmtBits; 163 164 SourceLocation IdentLoc; 165 }; 166 167 class AttributedStmtBitfields { 168 friend class ASTStmtReader; 169 friend class AttributedStmt; 170 171 LLVM_PREFERRED_TYPE(StmtBitfields) 172 unsigned : NumStmtBits; 173 174 /// Number of attributes. 175 unsigned NumAttrs : 32 - NumStmtBits; 176 177 /// The location of the attribute. 178 SourceLocation AttrLoc; 179 }; 180 181 class IfStmtBitfields { 182 friend class ASTStmtReader; 183 friend class IfStmt; 184 185 LLVM_PREFERRED_TYPE(StmtBitfields) 186 unsigned : NumStmtBits; 187 188 /// Whether this is a constexpr if, or a consteval if, or neither. 189 LLVM_PREFERRED_TYPE(IfStatementKind) 190 unsigned Kind : 3; 191 192 /// True if this if statement has storage for an else statement. 193 LLVM_PREFERRED_TYPE(bool) 194 unsigned HasElse : 1; 195 196 /// True if this if statement has storage for a variable declaration. 197 LLVM_PREFERRED_TYPE(bool) 198 unsigned HasVar : 1; 199 200 /// True if this if statement has storage for an init statement. 201 LLVM_PREFERRED_TYPE(bool) 202 unsigned HasInit : 1; 203 204 /// The location of the "if". 205 SourceLocation IfLoc; 206 }; 207 208 class SwitchStmtBitfields { 209 friend class SwitchStmt; 210 211 LLVM_PREFERRED_TYPE(StmtBitfields) 212 unsigned : NumStmtBits; 213 214 /// True if the SwitchStmt has storage for an init statement. 215 LLVM_PREFERRED_TYPE(bool) 216 unsigned HasInit : 1; 217 218 /// True if the SwitchStmt has storage for a condition variable. 219 LLVM_PREFERRED_TYPE(bool) 220 unsigned HasVar : 1; 221 222 /// If the SwitchStmt is a switch on an enum value, records whether all 223 /// the enum values were covered by CaseStmts. The coverage information 224 /// value is meant to be a hint for possible clients. 225 LLVM_PREFERRED_TYPE(bool) 226 unsigned AllEnumCasesCovered : 1; 227 228 /// The location of the "switch". 229 SourceLocation SwitchLoc; 230 }; 231 232 class WhileStmtBitfields { 233 friend class ASTStmtReader; 234 friend class WhileStmt; 235 236 LLVM_PREFERRED_TYPE(StmtBitfields) 237 unsigned : NumStmtBits; 238 239 /// True if the WhileStmt has storage for a condition variable. 240 LLVM_PREFERRED_TYPE(bool) 241 unsigned HasVar : 1; 242 243 /// The location of the "while". 244 SourceLocation WhileLoc; 245 }; 246 247 class DoStmtBitfields { 248 friend class DoStmt; 249 250 LLVM_PREFERRED_TYPE(StmtBitfields) 251 unsigned : NumStmtBits; 252 253 /// The location of the "do". 254 SourceLocation DoLoc; 255 }; 256 257 class ForStmtBitfields { 258 friend class ForStmt; 259 260 LLVM_PREFERRED_TYPE(StmtBitfields) 261 unsigned : NumStmtBits; 262 263 /// The location of the "for". 264 SourceLocation ForLoc; 265 }; 266 267 class GotoStmtBitfields { 268 friend class GotoStmt; 269 friend class IndirectGotoStmt; 270 271 LLVM_PREFERRED_TYPE(StmtBitfields) 272 unsigned : NumStmtBits; 273 274 /// The location of the "goto". 275 SourceLocation GotoLoc; 276 }; 277 278 class ContinueStmtBitfields { 279 friend class ContinueStmt; 280 281 LLVM_PREFERRED_TYPE(StmtBitfields) 282 unsigned : NumStmtBits; 283 284 /// The location of the "continue". 285 SourceLocation ContinueLoc; 286 }; 287 288 class BreakStmtBitfields { 289 friend class BreakStmt; 290 291 LLVM_PREFERRED_TYPE(StmtBitfields) 292 unsigned : NumStmtBits; 293 294 /// The location of the "break". 295 SourceLocation BreakLoc; 296 }; 297 298 class ReturnStmtBitfields { 299 friend class ReturnStmt; 300 301 LLVM_PREFERRED_TYPE(StmtBitfields) 302 unsigned : NumStmtBits; 303 304 /// True if this ReturnStmt has storage for an NRVO candidate. 305 LLVM_PREFERRED_TYPE(bool) 306 unsigned HasNRVOCandidate : 1; 307 308 /// The location of the "return". 309 SourceLocation RetLoc; 310 }; 311 312 class SwitchCaseBitfields { 313 friend class SwitchCase; 314 friend class CaseStmt; 315 316 LLVM_PREFERRED_TYPE(StmtBitfields) 317 unsigned : NumStmtBits; 318 319 /// Used by CaseStmt to store whether it is a case statement 320 /// of the form case LHS ... RHS (a GNU extension). 321 LLVM_PREFERRED_TYPE(bool) 322 unsigned CaseStmtIsGNURange : 1; 323 324 /// The location of the "case" or "default" keyword. 325 SourceLocation KeywordLoc; 326 }; 327 328 //===--- Expression bitfields classes ---===// 329 330 class ExprBitfields { 331 friend class ASTStmtReader; // deserialization 332 friend class AtomicExpr; // ctor 333 friend class BlockDeclRefExpr; // ctor 334 friend class CallExpr; // ctor 335 friend class CXXConstructExpr; // ctor 336 friend class CXXDependentScopeMemberExpr; // ctor 337 friend class CXXNewExpr; // ctor 338 friend class CXXUnresolvedConstructExpr; // ctor 339 friend class DeclRefExpr; // computeDependence 340 friend class DependentScopeDeclRefExpr; // ctor 341 friend class DesignatedInitExpr; // ctor 342 friend class Expr; 343 friend class InitListExpr; // ctor 344 friend class ObjCArrayLiteral; // ctor 345 friend class ObjCDictionaryLiteral; // ctor 346 friend class ObjCMessageExpr; // ctor 347 friend class OffsetOfExpr; // ctor 348 friend class OpaqueValueExpr; // ctor 349 friend class OverloadExpr; // ctor 350 friend class ParenListExpr; // ctor 351 friend class PseudoObjectExpr; // ctor 352 friend class ShuffleVectorExpr; // ctor 353 354 LLVM_PREFERRED_TYPE(StmtBitfields) 355 unsigned : NumStmtBits; 356 357 LLVM_PREFERRED_TYPE(ExprValueKind) 358 unsigned ValueKind : 2; 359 LLVM_PREFERRED_TYPE(ExprObjectKind) 360 unsigned ObjectKind : 3; 361 LLVM_PREFERRED_TYPE(ExprDependence) 362 unsigned Dependent : llvm::BitWidth<ExprDependence>; 363 }; 364 enum { NumExprBits = NumStmtBits + 5 + llvm::BitWidth<ExprDependence> }; 365 366 class ConstantExprBitfields { 367 friend class ASTStmtReader; 368 friend class ASTStmtWriter; 369 friend class ConstantExpr; 370 371 LLVM_PREFERRED_TYPE(ExprBitfields) 372 unsigned : NumExprBits; 373 374 /// The kind of result that is tail-allocated. 375 LLVM_PREFERRED_TYPE(ConstantResultStorageKind) 376 unsigned ResultKind : 2; 377 378 /// The kind of Result as defined by APValue::ValueKind. 379 LLVM_PREFERRED_TYPE(APValue::ValueKind) 380 unsigned APValueKind : 4; 381 382 /// When ResultKind == ConstantResultStorageKind::Int64, true if the 383 /// tail-allocated integer is unsigned. 384 LLVM_PREFERRED_TYPE(bool) 385 unsigned IsUnsigned : 1; 386 387 /// When ResultKind == ConstantResultStorageKind::Int64. the BitWidth of the 388 /// tail-allocated integer. 7 bits because it is the minimal number of bits 389 /// to represent a value from 0 to 64 (the size of the tail-allocated 390 /// integer). 391 unsigned BitWidth : 7; 392 393 /// When ResultKind == ConstantResultStorageKind::APValue, true if the 394 /// ASTContext will cleanup the tail-allocated APValue. 395 LLVM_PREFERRED_TYPE(bool) 396 unsigned HasCleanup : 1; 397 398 /// True if this ConstantExpr was created for immediate invocation. 399 LLVM_PREFERRED_TYPE(bool) 400 unsigned IsImmediateInvocation : 1; 401 }; 402 403 class PredefinedExprBitfields { 404 friend class ASTStmtReader; 405 friend class PredefinedExpr; 406 407 LLVM_PREFERRED_TYPE(ExprBitfields) 408 unsigned : NumExprBits; 409 410 LLVM_PREFERRED_TYPE(PredefinedIdentKind) 411 unsigned Kind : 4; 412 413 /// True if this PredefinedExpr has a trailing "StringLiteral *" 414 /// for the predefined identifier. 415 LLVM_PREFERRED_TYPE(bool) 416 unsigned HasFunctionName : 1; 417 418 /// True if this PredefinedExpr should be treated as a StringLiteral (for 419 /// MSVC compatibility). 420 LLVM_PREFERRED_TYPE(bool) 421 unsigned IsTransparent : 1; 422 423 /// The location of this PredefinedExpr. 424 SourceLocation Loc; 425 }; 426 427 class DeclRefExprBitfields { 428 friend class ASTStmtReader; // deserialization 429 friend class DeclRefExpr; 430 431 LLVM_PREFERRED_TYPE(ExprBitfields) 432 unsigned : NumExprBits; 433 434 LLVM_PREFERRED_TYPE(bool) 435 unsigned HasQualifier : 1; 436 LLVM_PREFERRED_TYPE(bool) 437 unsigned HasTemplateKWAndArgsInfo : 1; 438 LLVM_PREFERRED_TYPE(bool) 439 unsigned HasFoundDecl : 1; 440 LLVM_PREFERRED_TYPE(bool) 441 unsigned HadMultipleCandidates : 1; 442 LLVM_PREFERRED_TYPE(bool) 443 unsigned RefersToEnclosingVariableOrCapture : 1; 444 LLVM_PREFERRED_TYPE(bool) 445 unsigned CapturedByCopyInLambdaWithExplicitObjectParameter : 1; 446 LLVM_PREFERRED_TYPE(NonOdrUseReason) 447 unsigned NonOdrUseReason : 2; 448 LLVM_PREFERRED_TYPE(bool) 449 unsigned IsImmediateEscalating : 1; 450 451 /// The location of the declaration name itself. 452 SourceLocation Loc; 453 }; 454 455 456 class FloatingLiteralBitfields { 457 friend class FloatingLiteral; 458 459 LLVM_PREFERRED_TYPE(ExprBitfields) 460 unsigned : NumExprBits; 461 462 static_assert( 463 llvm::APFloat::S_MaxSemantics < 16, 464 "Too many Semantics enum values to fit in bitfield of size 4"); 465 LLVM_PREFERRED_TYPE(llvm::APFloat::Semantics) 466 unsigned Semantics : 4; // Provides semantics for APFloat construction 467 LLVM_PREFERRED_TYPE(bool) 468 unsigned IsExact : 1; 469 }; 470 471 class StringLiteralBitfields { 472 friend class ASTStmtReader; 473 friend class StringLiteral; 474 475 LLVM_PREFERRED_TYPE(ExprBitfields) 476 unsigned : NumExprBits; 477 478 /// The kind of this string literal. 479 /// One of the enumeration values of StringLiteral::StringKind. 480 LLVM_PREFERRED_TYPE(StringLiteralKind) 481 unsigned Kind : 3; 482 483 /// The width of a single character in bytes. Only values of 1, 2, 484 /// and 4 bytes are supported. StringLiteral::mapCharByteWidth maps 485 /// the target + string kind to the appropriate CharByteWidth. 486 unsigned CharByteWidth : 3; 487 488 LLVM_PREFERRED_TYPE(bool) 489 unsigned IsPascal : 1; 490 491 /// The number of concatenated token this string is made of. 492 /// This is the number of trailing SourceLocation. 493 unsigned NumConcatenated; 494 }; 495 496 class CharacterLiteralBitfields { 497 friend class CharacterLiteral; 498 499 LLVM_PREFERRED_TYPE(ExprBitfields) 500 unsigned : NumExprBits; 501 502 LLVM_PREFERRED_TYPE(CharacterLiteralKind) 503 unsigned Kind : 3; 504 }; 505 506 class UnaryOperatorBitfields { 507 friend class UnaryOperator; 508 509 LLVM_PREFERRED_TYPE(ExprBitfields) 510 unsigned : NumExprBits; 511 512 LLVM_PREFERRED_TYPE(UnaryOperatorKind) 513 unsigned Opc : 5; 514 LLVM_PREFERRED_TYPE(bool) 515 unsigned CanOverflow : 1; 516 // 517 /// This is only meaningful for operations on floating point 518 /// types when additional values need to be in trailing storage. 519 /// It is 0 otherwise. 520 LLVM_PREFERRED_TYPE(bool) 521 unsigned HasFPFeatures : 1; 522 523 SourceLocation Loc; 524 }; 525 526 class UnaryExprOrTypeTraitExprBitfields { 527 friend class UnaryExprOrTypeTraitExpr; 528 529 LLVM_PREFERRED_TYPE(ExprBitfields) 530 unsigned : NumExprBits; 531 532 LLVM_PREFERRED_TYPE(UnaryExprOrTypeTrait) 533 unsigned Kind : 3; 534 LLVM_PREFERRED_TYPE(bool) 535 unsigned IsType : 1; // true if operand is a type, false if an expression. 536 }; 537 538 class ArrayOrMatrixSubscriptExprBitfields { 539 friend class ArraySubscriptExpr; 540 friend class MatrixSubscriptExpr; 541 542 LLVM_PREFERRED_TYPE(ExprBitfields) 543 unsigned : NumExprBits; 544 545 SourceLocation RBracketLoc; 546 }; 547 548 class CallExprBitfields { 549 friend class CallExpr; 550 551 LLVM_PREFERRED_TYPE(ExprBitfields) 552 unsigned : NumExprBits; 553 554 unsigned NumPreArgs : 1; 555 556 /// True if the callee of the call expression was found using ADL. 557 LLVM_PREFERRED_TYPE(bool) 558 unsigned UsesADL : 1; 559 560 /// True if the call expression has some floating-point features. 561 LLVM_PREFERRED_TYPE(bool) 562 unsigned HasFPFeatures : 1; 563 564 /// Padding used to align OffsetToTrailingObjects to a byte multiple. 565 unsigned : 24 - 3 - NumExprBits; 566 567 /// The offset in bytes from the this pointer to the start of the 568 /// trailing objects belonging to CallExpr. Intentionally byte sized 569 /// for faster access. 570 unsigned OffsetToTrailingObjects : 8; 571 }; 572 enum { NumCallExprBits = 32 }; 573 574 class MemberExprBitfields { 575 friend class ASTStmtReader; 576 friend class MemberExpr; 577 578 LLVM_PREFERRED_TYPE(ExprBitfields) 579 unsigned : NumExprBits; 580 581 /// IsArrow - True if this is "X->F", false if this is "X.F". 582 LLVM_PREFERRED_TYPE(bool) 583 unsigned IsArrow : 1; 584 585 /// True if this member expression used a nested-name-specifier to 586 /// refer to the member, e.g., "x->Base::f", or found its member via 587 /// a using declaration. When true, a MemberExprNameQualifier 588 /// structure is allocated immediately after the MemberExpr. 589 LLVM_PREFERRED_TYPE(bool) 590 unsigned HasQualifierOrFoundDecl : 1; 591 592 /// True if this member expression specified a template keyword 593 /// and/or a template argument list explicitly, e.g., x->f<int>, 594 /// x->template f, x->template f<int>. 595 /// When true, an ASTTemplateKWAndArgsInfo structure and its 596 /// TemplateArguments (if any) are present. 597 LLVM_PREFERRED_TYPE(bool) 598 unsigned HasTemplateKWAndArgsInfo : 1; 599 600 /// True if this member expression refers to a method that 601 /// was resolved from an overloaded set having size greater than 1. 602 LLVM_PREFERRED_TYPE(bool) 603 unsigned HadMultipleCandidates : 1; 604 605 /// Value of type NonOdrUseReason indicating why this MemberExpr does 606 /// not constitute an odr-use of the named declaration. Meaningful only 607 /// when naming a static member. 608 LLVM_PREFERRED_TYPE(NonOdrUseReason) 609 unsigned NonOdrUseReason : 2; 610 611 /// This is the location of the -> or . in the expression. 612 SourceLocation OperatorLoc; 613 }; 614 615 class CastExprBitfields { 616 friend class CastExpr; 617 friend class ImplicitCastExpr; 618 619 LLVM_PREFERRED_TYPE(ExprBitfields) 620 unsigned : NumExprBits; 621 622 LLVM_PREFERRED_TYPE(CastKind) 623 unsigned Kind : 7; 624 LLVM_PREFERRED_TYPE(bool) 625 unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr. 626 627 /// True if the call expression has some floating-point features. 628 LLVM_PREFERRED_TYPE(bool) 629 unsigned HasFPFeatures : 1; 630 631 /// The number of CXXBaseSpecifiers in the cast. 14 bits would be enough 632 /// here. ([implimits] Direct and indirect base classes [16384]). 633 unsigned BasePathSize; 634 }; 635 636 class BinaryOperatorBitfields { 637 friend class BinaryOperator; 638 639 LLVM_PREFERRED_TYPE(ExprBitfields) 640 unsigned : NumExprBits; 641 642 LLVM_PREFERRED_TYPE(BinaryOperatorKind) 643 unsigned Opc : 6; 644 645 /// This is only meaningful for operations on floating point 646 /// types when additional values need to be in trailing storage. 647 /// It is 0 otherwise. 648 LLVM_PREFERRED_TYPE(bool) 649 unsigned HasFPFeatures : 1; 650 651 SourceLocation OpLoc; 652 }; 653 654 class InitListExprBitfields { 655 friend class InitListExpr; 656 657 LLVM_PREFERRED_TYPE(ExprBitfields) 658 unsigned : NumExprBits; 659 660 /// Whether this initializer list originally had a GNU array-range 661 /// designator in it. This is a temporary marker used by CodeGen. 662 LLVM_PREFERRED_TYPE(bool) 663 unsigned HadArrayRangeDesignator : 1; 664 }; 665 666 class ParenListExprBitfields { 667 friend class ASTStmtReader; 668 friend class ParenListExpr; 669 670 LLVM_PREFERRED_TYPE(ExprBitfields) 671 unsigned : NumExprBits; 672 673 /// The number of expressions in the paren list. 674 unsigned NumExprs; 675 }; 676 677 class GenericSelectionExprBitfields { 678 friend class ASTStmtReader; 679 friend class GenericSelectionExpr; 680 681 LLVM_PREFERRED_TYPE(ExprBitfields) 682 unsigned : NumExprBits; 683 684 /// The location of the "_Generic". 685 SourceLocation GenericLoc; 686 }; 687 688 class PseudoObjectExprBitfields { 689 friend class ASTStmtReader; // deserialization 690 friend class PseudoObjectExpr; 691 692 LLVM_PREFERRED_TYPE(ExprBitfields) 693 unsigned : NumExprBits; 694 695 unsigned NumSubExprs : 16; 696 unsigned ResultIndex : 16; 697 }; 698 699 class SourceLocExprBitfields { 700 friend class ASTStmtReader; 701 friend class SourceLocExpr; 702 703 LLVM_PREFERRED_TYPE(ExprBitfields) 704 unsigned : NumExprBits; 705 706 /// The kind of source location builtin represented by the SourceLocExpr. 707 /// Ex. __builtin_LINE, __builtin_FUNCTION, etc. 708 LLVM_PREFERRED_TYPE(SourceLocIdentKind) 709 unsigned Kind : 3; 710 }; 711 712 class StmtExprBitfields { 713 friend class ASTStmtReader; 714 friend class StmtExpr; 715 716 LLVM_PREFERRED_TYPE(ExprBitfields) 717 unsigned : NumExprBits; 718 719 /// The number of levels of template parameters enclosing this statement 720 /// expression. Used to determine if a statement expression remains 721 /// dependent after instantiation. 722 unsigned TemplateDepth; 723 }; 724 725 //===--- C++ Expression bitfields classes ---===// 726 727 class CXXOperatorCallExprBitfields { 728 friend class ASTStmtReader; 729 friend class CXXOperatorCallExpr; 730 731 LLVM_PREFERRED_TYPE(CallExprBitfields) 732 unsigned : NumCallExprBits; 733 734 /// The kind of this overloaded operator. One of the enumerator 735 /// value of OverloadedOperatorKind. 736 LLVM_PREFERRED_TYPE(OverloadedOperatorKind) 737 unsigned OperatorKind : 6; 738 }; 739 740 class CXXRewrittenBinaryOperatorBitfields { 741 friend class ASTStmtReader; 742 friend class CXXRewrittenBinaryOperator; 743 744 LLVM_PREFERRED_TYPE(CallExprBitfields) 745 unsigned : NumCallExprBits; 746 747 LLVM_PREFERRED_TYPE(bool) 748 unsigned IsReversed : 1; 749 }; 750 751 class CXXBoolLiteralExprBitfields { 752 friend class CXXBoolLiteralExpr; 753 754 LLVM_PREFERRED_TYPE(ExprBitfields) 755 unsigned : NumExprBits; 756 757 /// The value of the boolean literal. 758 LLVM_PREFERRED_TYPE(bool) 759 unsigned Value : 1; 760 761 /// The location of the boolean literal. 762 SourceLocation Loc; 763 }; 764 765 class CXXNullPtrLiteralExprBitfields { 766 friend class CXXNullPtrLiteralExpr; 767 768 LLVM_PREFERRED_TYPE(ExprBitfields) 769 unsigned : NumExprBits; 770 771 /// The location of the null pointer literal. 772 SourceLocation Loc; 773 }; 774 775 class CXXThisExprBitfields { 776 friend class CXXThisExpr; 777 778 LLVM_PREFERRED_TYPE(ExprBitfields) 779 unsigned : NumExprBits; 780 781 /// Whether this is an implicit "this". 782 LLVM_PREFERRED_TYPE(bool) 783 unsigned IsImplicit : 1; 784 785 /// The location of the "this". 786 SourceLocation Loc; 787 }; 788 789 class CXXThrowExprBitfields { 790 friend class ASTStmtReader; 791 friend class CXXThrowExpr; 792 793 LLVM_PREFERRED_TYPE(ExprBitfields) 794 unsigned : NumExprBits; 795 796 /// Whether the thrown variable (if any) is in scope. 797 LLVM_PREFERRED_TYPE(bool) 798 unsigned IsThrownVariableInScope : 1; 799 800 /// The location of the "throw". 801 SourceLocation ThrowLoc; 802 }; 803 804 class CXXDefaultArgExprBitfields { 805 friend class ASTStmtReader; 806 friend class CXXDefaultArgExpr; 807 808 LLVM_PREFERRED_TYPE(ExprBitfields) 809 unsigned : NumExprBits; 810 811 /// Whether this CXXDefaultArgExpr rewrote its argument and stores a copy. 812 LLVM_PREFERRED_TYPE(bool) 813 unsigned HasRewrittenInit : 1; 814 815 /// The location where the default argument expression was used. 816 SourceLocation Loc; 817 }; 818 819 class CXXDefaultInitExprBitfields { 820 friend class ASTStmtReader; 821 friend class CXXDefaultInitExpr; 822 823 LLVM_PREFERRED_TYPE(ExprBitfields) 824 unsigned : NumExprBits; 825 826 /// Whether this CXXDefaultInitExprBitfields rewrote its argument and stores 827 /// a copy. 828 LLVM_PREFERRED_TYPE(bool) 829 unsigned HasRewrittenInit : 1; 830 831 /// The location where the default initializer expression was used. 832 SourceLocation Loc; 833 }; 834 835 class CXXScalarValueInitExprBitfields { 836 friend class ASTStmtReader; 837 friend class CXXScalarValueInitExpr; 838 839 LLVM_PREFERRED_TYPE(ExprBitfields) 840 unsigned : NumExprBits; 841 842 SourceLocation RParenLoc; 843 }; 844 845 class CXXNewExprBitfields { 846 friend class ASTStmtReader; 847 friend class ASTStmtWriter; 848 friend class CXXNewExpr; 849 850 LLVM_PREFERRED_TYPE(ExprBitfields) 851 unsigned : NumExprBits; 852 853 /// Was the usage ::new, i.e. is the global new to be used? 854 LLVM_PREFERRED_TYPE(bool) 855 unsigned IsGlobalNew : 1; 856 857 /// Do we allocate an array? If so, the first trailing "Stmt *" is the 858 /// size expression. 859 LLVM_PREFERRED_TYPE(bool) 860 unsigned IsArray : 1; 861 862 /// Should the alignment be passed to the allocation function? 863 LLVM_PREFERRED_TYPE(bool) 864 unsigned ShouldPassAlignment : 1; 865 866 /// If this is an array allocation, does the usual deallocation 867 /// function for the allocated type want to know the allocated size? 868 LLVM_PREFERRED_TYPE(bool) 869 unsigned UsualArrayDeleteWantsSize : 1; 870 871 /// What kind of initializer do we have? Could be none, parens, or braces. 872 /// In storage, we distinguish between "none, and no initializer expr", and 873 /// "none, but an implicit initializer expr". 874 LLVM_PREFERRED_TYPE(CXXNewInitializationStyle) 875 unsigned StoredInitializationStyle : 2; 876 877 /// True if the allocated type was expressed as a parenthesized type-id. 878 LLVM_PREFERRED_TYPE(bool) 879 unsigned IsParenTypeId : 1; 880 881 /// The number of placement new arguments. 882 unsigned NumPlacementArgs; 883 }; 884 885 class CXXDeleteExprBitfields { 886 friend class ASTStmtReader; 887 friend class CXXDeleteExpr; 888 889 LLVM_PREFERRED_TYPE(ExprBitfields) 890 unsigned : NumExprBits; 891 892 /// Is this a forced global delete, i.e. "::delete"? 893 LLVM_PREFERRED_TYPE(bool) 894 unsigned GlobalDelete : 1; 895 896 /// Is this the array form of delete, i.e. "delete[]"? 897 LLVM_PREFERRED_TYPE(bool) 898 unsigned ArrayForm : 1; 899 900 /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is 901 /// applied to pointer-to-array type (ArrayFormAsWritten will be false 902 /// while ArrayForm will be true). 903 LLVM_PREFERRED_TYPE(bool) 904 unsigned ArrayFormAsWritten : 1; 905 906 /// Does the usual deallocation function for the element type require 907 /// a size_t argument? 908 LLVM_PREFERRED_TYPE(bool) 909 unsigned UsualArrayDeleteWantsSize : 1; 910 911 /// Location of the expression. 912 SourceLocation Loc; 913 }; 914 915 class TypeTraitExprBitfields { 916 friend class ASTStmtReader; 917 friend class ASTStmtWriter; 918 friend class TypeTraitExpr; 919 920 LLVM_PREFERRED_TYPE(ExprBitfields) 921 unsigned : NumExprBits; 922 923 /// The kind of type trait, which is a value of a TypeTrait enumerator. 924 LLVM_PREFERRED_TYPE(TypeTrait) 925 unsigned Kind : 8; 926 927 /// If this expression is not value-dependent, this indicates whether 928 /// the trait evaluated true or false. 929 LLVM_PREFERRED_TYPE(bool) 930 unsigned Value : 1; 931 932 /// The number of arguments to this type trait. According to [implimits] 933 /// 8 bits would be enough, but we require (and test for) at least 16 bits 934 /// to mirror FunctionType. 935 unsigned NumArgs; 936 }; 937 938 class DependentScopeDeclRefExprBitfields { 939 friend class ASTStmtReader; 940 friend class ASTStmtWriter; 941 friend class DependentScopeDeclRefExpr; 942 943 LLVM_PREFERRED_TYPE(ExprBitfields) 944 unsigned : NumExprBits; 945 946 /// Whether the name includes info for explicit template 947 /// keyword and arguments. 948 LLVM_PREFERRED_TYPE(bool) 949 unsigned HasTemplateKWAndArgsInfo : 1; 950 }; 951 952 class CXXConstructExprBitfields { 953 friend class ASTStmtReader; 954 friend class CXXConstructExpr; 955 956 LLVM_PREFERRED_TYPE(ExprBitfields) 957 unsigned : NumExprBits; 958 959 LLVM_PREFERRED_TYPE(bool) 960 unsigned Elidable : 1; 961 LLVM_PREFERRED_TYPE(bool) 962 unsigned HadMultipleCandidates : 1; 963 LLVM_PREFERRED_TYPE(bool) 964 unsigned ListInitialization : 1; 965 LLVM_PREFERRED_TYPE(bool) 966 unsigned StdInitListInitialization : 1; 967 LLVM_PREFERRED_TYPE(bool) 968 unsigned ZeroInitialization : 1; 969 LLVM_PREFERRED_TYPE(CXXConstructionKind) 970 unsigned ConstructionKind : 3; 971 LLVM_PREFERRED_TYPE(bool) 972 unsigned IsImmediateEscalating : 1; 973 974 SourceLocation Loc; 975 }; 976 977 class ExprWithCleanupsBitfields { 978 friend class ASTStmtReader; // deserialization 979 friend class ExprWithCleanups; 980 981 LLVM_PREFERRED_TYPE(ExprBitfields) 982 unsigned : NumExprBits; 983 984 // When false, it must not have side effects. 985 LLVM_PREFERRED_TYPE(bool) 986 unsigned CleanupsHaveSideEffects : 1; 987 988 unsigned NumObjects : 32 - 1 - NumExprBits; 989 }; 990 991 class CXXUnresolvedConstructExprBitfields { 992 friend class ASTStmtReader; 993 friend class CXXUnresolvedConstructExpr; 994 995 LLVM_PREFERRED_TYPE(ExprBitfields) 996 unsigned : NumExprBits; 997 998 /// The number of arguments used to construct the type. 999 unsigned NumArgs; 1000 }; 1001 1002 class CXXDependentScopeMemberExprBitfields { 1003 friend class ASTStmtReader; 1004 friend class CXXDependentScopeMemberExpr; 1005 1006 LLVM_PREFERRED_TYPE(ExprBitfields) 1007 unsigned : NumExprBits; 1008 1009 /// Whether this member expression used the '->' operator or 1010 /// the '.' operator. 1011 LLVM_PREFERRED_TYPE(bool) 1012 unsigned IsArrow : 1; 1013 1014 /// Whether this member expression has info for explicit template 1015 /// keyword and arguments. 1016 LLVM_PREFERRED_TYPE(bool) 1017 unsigned HasTemplateKWAndArgsInfo : 1; 1018 1019 /// See getFirstQualifierFoundInScope() and the comment listing 1020 /// the trailing objects. 1021 LLVM_PREFERRED_TYPE(bool) 1022 unsigned HasFirstQualifierFoundInScope : 1; 1023 1024 /// The location of the '->' or '.' operator. 1025 SourceLocation OperatorLoc; 1026 }; 1027 1028 class OverloadExprBitfields { 1029 friend class ASTStmtReader; 1030 friend class OverloadExpr; 1031 1032 LLVM_PREFERRED_TYPE(ExprBitfields) 1033 unsigned : NumExprBits; 1034 1035 /// Whether the name includes info for explicit template 1036 /// keyword and arguments. 1037 LLVM_PREFERRED_TYPE(bool) 1038 unsigned HasTemplateKWAndArgsInfo : 1; 1039 1040 /// Padding used by the derived classes to store various bits. If you 1041 /// need to add some data here, shrink this padding and add your data 1042 /// above. NumOverloadExprBits also needs to be updated. 1043 unsigned : 32 - NumExprBits - 1; 1044 1045 /// The number of results. 1046 unsigned NumResults; 1047 }; 1048 enum { NumOverloadExprBits = NumExprBits + 1 }; 1049 1050 class UnresolvedLookupExprBitfields { 1051 friend class ASTStmtReader; 1052 friend class UnresolvedLookupExpr; 1053 1054 LLVM_PREFERRED_TYPE(OverloadExprBitfields) 1055 unsigned : NumOverloadExprBits; 1056 1057 /// True if these lookup results should be extended by 1058 /// argument-dependent lookup if this is the operand of a function call. 1059 LLVM_PREFERRED_TYPE(bool) 1060 unsigned RequiresADL : 1; 1061 1062 /// True if these lookup results are overloaded. This is pretty trivially 1063 /// rederivable if we urgently need to kill this field. 1064 LLVM_PREFERRED_TYPE(bool) 1065 unsigned Overloaded : 1; 1066 }; 1067 static_assert(sizeof(UnresolvedLookupExprBitfields) <= 4, 1068 "UnresolvedLookupExprBitfields must be <= than 4 bytes to" 1069 "avoid trashing OverloadExprBitfields::NumResults!"); 1070 1071 class UnresolvedMemberExprBitfields { 1072 friend class ASTStmtReader; 1073 friend class UnresolvedMemberExpr; 1074 1075 LLVM_PREFERRED_TYPE(OverloadExprBitfields) 1076 unsigned : NumOverloadExprBits; 1077 1078 /// Whether this member expression used the '->' operator or 1079 /// the '.' operator. 1080 LLVM_PREFERRED_TYPE(bool) 1081 unsigned IsArrow : 1; 1082 1083 /// Whether the lookup results contain an unresolved using declaration. 1084 LLVM_PREFERRED_TYPE(bool) 1085 unsigned HasUnresolvedUsing : 1; 1086 }; 1087 static_assert(sizeof(UnresolvedMemberExprBitfields) <= 4, 1088 "UnresolvedMemberExprBitfields must be <= than 4 bytes to" 1089 "avoid trashing OverloadExprBitfields::NumResults!"); 1090 1091 class CXXNoexceptExprBitfields { 1092 friend class ASTStmtReader; 1093 friend class CXXNoexceptExpr; 1094 1095 LLVM_PREFERRED_TYPE(ExprBitfields) 1096 unsigned : NumExprBits; 1097 1098 LLVM_PREFERRED_TYPE(bool) 1099 unsigned Value : 1; 1100 }; 1101 1102 class SubstNonTypeTemplateParmExprBitfields { 1103 friend class ASTStmtReader; 1104 friend class SubstNonTypeTemplateParmExpr; 1105 1106 LLVM_PREFERRED_TYPE(ExprBitfields) 1107 unsigned : NumExprBits; 1108 1109 /// The location of the non-type template parameter reference. 1110 SourceLocation NameLoc; 1111 }; 1112 1113 class LambdaExprBitfields { 1114 friend class ASTStmtReader; 1115 friend class ASTStmtWriter; 1116 friend class LambdaExpr; 1117 1118 LLVM_PREFERRED_TYPE(ExprBitfields) 1119 unsigned : NumExprBits; 1120 1121 /// The default capture kind, which is a value of type 1122 /// LambdaCaptureDefault. 1123 LLVM_PREFERRED_TYPE(LambdaCaptureDefault) 1124 unsigned CaptureDefault : 2; 1125 1126 /// Whether this lambda had an explicit parameter list vs. an 1127 /// implicit (and empty) parameter list. 1128 LLVM_PREFERRED_TYPE(bool) 1129 unsigned ExplicitParams : 1; 1130 1131 /// Whether this lambda had the result type explicitly specified. 1132 LLVM_PREFERRED_TYPE(bool) 1133 unsigned ExplicitResultType : 1; 1134 1135 /// The number of captures. 1136 unsigned NumCaptures : 16; 1137 }; 1138 1139 class RequiresExprBitfields { 1140 friend class ASTStmtReader; 1141 friend class ASTStmtWriter; 1142 friend class RequiresExpr; 1143 1144 LLVM_PREFERRED_TYPE(ExprBitfields) 1145 unsigned : NumExprBits; 1146 1147 LLVM_PREFERRED_TYPE(bool) 1148 unsigned IsSatisfied : 1; 1149 SourceLocation RequiresKWLoc; 1150 }; 1151 1152 //===--- C++ Coroutines bitfields classes ---===// 1153 1154 class CoawaitExprBitfields { 1155 friend class CoawaitExpr; 1156 1157 LLVM_PREFERRED_TYPE(ExprBitfields) 1158 unsigned : NumExprBits; 1159 1160 LLVM_PREFERRED_TYPE(bool) 1161 unsigned IsImplicit : 1; 1162 }; 1163 1164 //===--- Obj-C Expression bitfields classes ---===// 1165 1166 class ObjCIndirectCopyRestoreExprBitfields { 1167 friend class ObjCIndirectCopyRestoreExpr; 1168 1169 LLVM_PREFERRED_TYPE(ExprBitfields) 1170 unsigned : NumExprBits; 1171 1172 LLVM_PREFERRED_TYPE(bool) 1173 unsigned ShouldCopy : 1; 1174 }; 1175 1176 //===--- Clang Extensions bitfields classes ---===// 1177 1178 class OpaqueValueExprBitfields { 1179 friend class ASTStmtReader; 1180 friend class OpaqueValueExpr; 1181 1182 LLVM_PREFERRED_TYPE(ExprBitfields) 1183 unsigned : NumExprBits; 1184 1185 /// The OVE is a unique semantic reference to its source expression if this 1186 /// bit is set to true. 1187 LLVM_PREFERRED_TYPE(bool) 1188 unsigned IsUnique : 1; 1189 1190 SourceLocation Loc; 1191 }; 1192 1193 union { 1194 // Same order as in StmtNodes.td. 1195 // Statements 1196 StmtBitfields StmtBits; 1197 NullStmtBitfields NullStmtBits; 1198 CompoundStmtBitfields CompoundStmtBits; 1199 LabelStmtBitfields LabelStmtBits; 1200 AttributedStmtBitfields AttributedStmtBits; 1201 IfStmtBitfields IfStmtBits; 1202 SwitchStmtBitfields SwitchStmtBits; 1203 WhileStmtBitfields WhileStmtBits; 1204 DoStmtBitfields DoStmtBits; 1205 ForStmtBitfields ForStmtBits; 1206 GotoStmtBitfields GotoStmtBits; 1207 ContinueStmtBitfields ContinueStmtBits; 1208 BreakStmtBitfields BreakStmtBits; 1209 ReturnStmtBitfields ReturnStmtBits; 1210 SwitchCaseBitfields SwitchCaseBits; 1211 1212 // Expressions 1213 ExprBitfields ExprBits; 1214 ConstantExprBitfields ConstantExprBits; 1215 PredefinedExprBitfields PredefinedExprBits; 1216 DeclRefExprBitfields DeclRefExprBits; 1217 FloatingLiteralBitfields FloatingLiteralBits; 1218 StringLiteralBitfields StringLiteralBits; 1219 CharacterLiteralBitfields CharacterLiteralBits; 1220 UnaryOperatorBitfields UnaryOperatorBits; 1221 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits; 1222 ArrayOrMatrixSubscriptExprBitfields ArrayOrMatrixSubscriptExprBits; 1223 CallExprBitfields CallExprBits; 1224 MemberExprBitfields MemberExprBits; 1225 CastExprBitfields CastExprBits; 1226 BinaryOperatorBitfields BinaryOperatorBits; 1227 InitListExprBitfields InitListExprBits; 1228 ParenListExprBitfields ParenListExprBits; 1229 GenericSelectionExprBitfields GenericSelectionExprBits; 1230 PseudoObjectExprBitfields PseudoObjectExprBits; 1231 SourceLocExprBitfields SourceLocExprBits; 1232 1233 // GNU Extensions. 1234 StmtExprBitfields StmtExprBits; 1235 1236 // C++ Expressions 1237 CXXOperatorCallExprBitfields CXXOperatorCallExprBits; 1238 CXXRewrittenBinaryOperatorBitfields CXXRewrittenBinaryOperatorBits; 1239 CXXBoolLiteralExprBitfields CXXBoolLiteralExprBits; 1240 CXXNullPtrLiteralExprBitfields CXXNullPtrLiteralExprBits; 1241 CXXThisExprBitfields CXXThisExprBits; 1242 CXXThrowExprBitfields CXXThrowExprBits; 1243 CXXDefaultArgExprBitfields CXXDefaultArgExprBits; 1244 CXXDefaultInitExprBitfields CXXDefaultInitExprBits; 1245 CXXScalarValueInitExprBitfields CXXScalarValueInitExprBits; 1246 CXXNewExprBitfields CXXNewExprBits; 1247 CXXDeleteExprBitfields CXXDeleteExprBits; 1248 TypeTraitExprBitfields TypeTraitExprBits; 1249 DependentScopeDeclRefExprBitfields DependentScopeDeclRefExprBits; 1250 CXXConstructExprBitfields CXXConstructExprBits; 1251 ExprWithCleanupsBitfields ExprWithCleanupsBits; 1252 CXXUnresolvedConstructExprBitfields CXXUnresolvedConstructExprBits; 1253 CXXDependentScopeMemberExprBitfields CXXDependentScopeMemberExprBits; 1254 OverloadExprBitfields OverloadExprBits; 1255 UnresolvedLookupExprBitfields UnresolvedLookupExprBits; 1256 UnresolvedMemberExprBitfields UnresolvedMemberExprBits; 1257 CXXNoexceptExprBitfields CXXNoexceptExprBits; 1258 SubstNonTypeTemplateParmExprBitfields SubstNonTypeTemplateParmExprBits; 1259 LambdaExprBitfields LambdaExprBits; 1260 RequiresExprBitfields RequiresExprBits; 1261 1262 // C++ Coroutines expressions 1263 CoawaitExprBitfields CoawaitBits; 1264 1265 // Obj-C Expressions 1266 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits; 1267 1268 // Clang Extensions 1269 OpaqueValueExprBitfields OpaqueValueExprBits; 1270 }; 1271 1272 public: 1273 // Only allow allocation of Stmts using the allocator in ASTContext 1274 // or by doing a placement new. 1275 void* operator new(size_t bytes, const ASTContext& C, 1276 unsigned alignment = 8); 1277 1278 void* operator new(size_t bytes, const ASTContext* C, 1279 unsigned alignment = 8) { 1280 return operator new(bytes, *C, alignment); 1281 } 1282 1283 void *operator new(size_t bytes, void *mem) noexcept { return mem; } 1284 1285 void operator delete(void *, const ASTContext &, unsigned) noexcept {} 1286 void operator delete(void *, const ASTContext *, unsigned) noexcept {} 1287 void operator delete(void *, size_t) noexcept {} 1288 void operator delete(void *, void *) noexcept {} 1289 1290 public: 1291 /// A placeholder type used to construct an empty shell of a 1292 /// type, that will be filled in later (e.g., by some 1293 /// de-serialization). 1294 struct EmptyShell {}; 1295 1296 /// The likelihood of a branch being taken. 1297 enum Likelihood { 1298 LH_Unlikely = -1, ///< Branch has the [[unlikely]] attribute. 1299 LH_None, ///< No attribute set or branches of the IfStmt have 1300 ///< the same attribute. 1301 LH_Likely ///< Branch has the [[likely]] attribute. 1302 }; 1303 1304 protected: 1305 /// Iterator for iterating over Stmt * arrays that contain only T *. 1306 /// 1307 /// This is needed because AST nodes use Stmt* arrays to store 1308 /// references to children (to be compatible with StmtIterator). 1309 template<typename T, typename TPtr = T *, typename StmtPtr = Stmt *> 1310 struct CastIterator 1311 : llvm::iterator_adaptor_base<CastIterator<T, TPtr, StmtPtr>, StmtPtr *, 1312 std::random_access_iterator_tag, TPtr> { 1313 using Base = typename CastIterator::iterator_adaptor_base; 1314 1315 CastIterator() : Base(nullptr) {} 1316 CastIterator(StmtPtr *I) : Base(I) {} 1317 1318 typename Base::value_type operator*() const { 1319 return cast_or_null<T>(*this->I); 1320 } 1321 }; 1322 1323 /// Const iterator for iterating over Stmt * arrays that contain only T *. 1324 template <typename T> 1325 using ConstCastIterator = CastIterator<T, const T *const, const Stmt *const>; 1326 1327 using ExprIterator = CastIterator<Expr>; 1328 using ConstExprIterator = ConstCastIterator<Expr>; 1329 1330 private: 1331 /// Whether statistic collection is enabled. 1332 static bool StatisticsEnabled; 1333 1334 protected: 1335 /// Construct an empty statement. 1336 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {} 1337 1338 public: 1339 Stmt() = delete; 1340 Stmt(const Stmt &) = delete; 1341 Stmt(Stmt &&) = delete; 1342 Stmt &operator=(const Stmt &) = delete; 1343 Stmt &operator=(Stmt &&) = delete; 1344 1345 Stmt(StmtClass SC) { 1346 static_assert(sizeof(*this) <= 8, 1347 "changing bitfields changed sizeof(Stmt)"); 1348 static_assert(sizeof(*this) % alignof(void *) == 0, 1349 "Insufficient alignment!"); 1350 StmtBits.sClass = SC; 1351 if (StatisticsEnabled) Stmt::addStmtClass(SC); 1352 } 1353 1354 StmtClass getStmtClass() const { 1355 return static_cast<StmtClass>(StmtBits.sClass); 1356 } 1357 1358 const char *getStmtClassName() const; 1359 1360 /// SourceLocation tokens are not useful in isolation - they are low level 1361 /// value objects created/interpreted by SourceManager. We assume AST 1362 /// clients will have a pointer to the respective SourceManager. 1363 SourceRange getSourceRange() const LLVM_READONLY; 1364 SourceLocation getBeginLoc() const LLVM_READONLY; 1365 SourceLocation getEndLoc() const LLVM_READONLY; 1366 1367 // global temp stats (until we have a per-module visitor) 1368 static void addStmtClass(const StmtClass s); 1369 static void EnableStatistics(); 1370 static void PrintStats(); 1371 1372 /// \returns the likelihood of a set of attributes. 1373 static Likelihood getLikelihood(ArrayRef<const Attr *> Attrs); 1374 1375 /// \returns the likelihood of a statement. 1376 static Likelihood getLikelihood(const Stmt *S); 1377 1378 /// \returns the likelihood attribute of a statement. 1379 static const Attr *getLikelihoodAttr(const Stmt *S); 1380 1381 /// \returns the likelihood of the 'then' branch of an 'if' statement. The 1382 /// 'else' branch is required to determine whether both branches specify the 1383 /// same likelihood, which affects the result. 1384 static Likelihood getLikelihood(const Stmt *Then, const Stmt *Else); 1385 1386 /// \returns whether the likelihood of the branches of an if statement are 1387 /// conflicting. When the first element is \c true there's a conflict and 1388 /// the Attr's are the conflicting attributes of the Then and Else Stmt. 1389 static std::tuple<bool, const Attr *, const Attr *> 1390 determineLikelihoodConflict(const Stmt *Then, const Stmt *Else); 1391 1392 /// Dumps the specified AST fragment and all subtrees to 1393 /// \c llvm::errs(). 1394 void dump() const; 1395 void dump(raw_ostream &OS, const ASTContext &Context) const; 1396 1397 /// \return Unique reproducible object identifier 1398 int64_t getID(const ASTContext &Context) const; 1399 1400 /// dumpColor - same as dump(), but forces color highlighting. 1401 void dumpColor() const; 1402 1403 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST 1404 /// back to its original source language syntax. 1405 void dumpPretty(const ASTContext &Context) const; 1406 void printPretty(raw_ostream &OS, PrinterHelper *Helper, 1407 const PrintingPolicy &Policy, unsigned Indentation = 0, 1408 StringRef NewlineSymbol = "\n", 1409 const ASTContext *Context = nullptr) const; 1410 void printPrettyControlled(raw_ostream &OS, PrinterHelper *Helper, 1411 const PrintingPolicy &Policy, 1412 unsigned Indentation = 0, 1413 StringRef NewlineSymbol = "\n", 1414 const ASTContext *Context = nullptr) const; 1415 1416 /// Pretty-prints in JSON format. 1417 void printJson(raw_ostream &Out, PrinterHelper *Helper, 1418 const PrintingPolicy &Policy, bool AddQuotes) const; 1419 1420 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only 1421 /// works on systems with GraphViz (Mac OS X) or dot+gv installed. 1422 void viewAST() const; 1423 1424 /// Skip no-op (attributed, compound) container stmts and skip captured 1425 /// stmt at the top, if \a IgnoreCaptured is true. 1426 Stmt *IgnoreContainers(bool IgnoreCaptured = false); 1427 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const { 1428 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured); 1429 } 1430 1431 const Stmt *stripLabelLikeStatements() const; 1432 Stmt *stripLabelLikeStatements() { 1433 return const_cast<Stmt*>( 1434 const_cast<const Stmt*>(this)->stripLabelLikeStatements()); 1435 } 1436 1437 /// Child Iterators: All subclasses must implement 'children' 1438 /// to permit easy iteration over the substatements/subexpressions of an 1439 /// AST node. This permits easy iteration over all nodes in the AST. 1440 using child_iterator = StmtIterator; 1441 using const_child_iterator = ConstStmtIterator; 1442 1443 using child_range = llvm::iterator_range<child_iterator>; 1444 using const_child_range = llvm::iterator_range<const_child_iterator>; 1445 1446 child_range children(); 1447 1448 const_child_range children() const { 1449 auto Children = const_cast<Stmt *>(this)->children(); 1450 return const_child_range(Children.begin(), Children.end()); 1451 } 1452 1453 child_iterator child_begin() { return children().begin(); } 1454 child_iterator child_end() { return children().end(); } 1455 1456 const_child_iterator child_begin() const { return children().begin(); } 1457 const_child_iterator child_end() const { return children().end(); } 1458 1459 /// Produce a unique representation of the given statement. 1460 /// 1461 /// \param ID once the profiling operation is complete, will contain 1462 /// the unique representation of the given statement. 1463 /// 1464 /// \param Context the AST context in which the statement resides 1465 /// 1466 /// \param Canonical whether the profile should be based on the canonical 1467 /// representation of this statement (e.g., where non-type template 1468 /// parameters are identified by index/level rather than their 1469 /// declaration pointers) or the exact representation of the statement as 1470 /// written in the source. 1471 /// \param ProfileLambdaExpr whether or not to profile lambda expressions. 1472 /// When false, the lambda expressions are never considered to be equal to 1473 /// other lambda expressions. When true, the lambda expressions with the same 1474 /// implementation will be considered to be the same. ProfileLambdaExpr should 1475 /// only be true when we try to merge two declarations within modules. 1476 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 1477 bool Canonical, bool ProfileLambdaExpr = false) const; 1478 1479 /// Calculate a unique representation for a statement that is 1480 /// stable across compiler invocations. 1481 /// 1482 /// \param ID profile information will be stored in ID. 1483 /// 1484 /// \param Hash an ODRHash object which will be called where pointers would 1485 /// have been used in the Profile function. 1486 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const; 1487 }; 1488 1489 /// DeclStmt - Adaptor class for mixing declarations with statements and 1490 /// expressions. For example, CompoundStmt mixes statements, expressions 1491 /// and declarations (variables, types). Another example is ForStmt, where 1492 /// the first statement can be an expression or a declaration. 1493 class DeclStmt : public Stmt { 1494 DeclGroupRef DG; 1495 SourceLocation StartLoc, EndLoc; 1496 1497 public: 1498 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc) 1499 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {} 1500 1501 /// Build an empty declaration statement. 1502 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {} 1503 1504 /// isSingleDecl - This method returns true if this DeclStmt refers 1505 /// to a single Decl. 1506 bool isSingleDecl() const { return DG.isSingleDecl(); } 1507 1508 const Decl *getSingleDecl() const { return DG.getSingleDecl(); } 1509 Decl *getSingleDecl() { return DG.getSingleDecl(); } 1510 1511 const DeclGroupRef getDeclGroup() const { return DG; } 1512 DeclGroupRef getDeclGroup() { return DG; } 1513 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; } 1514 1515 void setStartLoc(SourceLocation L) { StartLoc = L; } 1516 SourceLocation getEndLoc() const { return EndLoc; } 1517 void setEndLoc(SourceLocation L) { EndLoc = L; } 1518 1519 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; } 1520 1521 static bool classof(const Stmt *T) { 1522 return T->getStmtClass() == DeclStmtClass; 1523 } 1524 1525 // Iterators over subexpressions. 1526 child_range children() { 1527 return child_range(child_iterator(DG.begin(), DG.end()), 1528 child_iterator(DG.end(), DG.end())); 1529 } 1530 1531 const_child_range children() const { 1532 auto Children = const_cast<DeclStmt *>(this)->children(); 1533 return const_child_range(Children); 1534 } 1535 1536 using decl_iterator = DeclGroupRef::iterator; 1537 using const_decl_iterator = DeclGroupRef::const_iterator; 1538 using decl_range = llvm::iterator_range<decl_iterator>; 1539 using decl_const_range = llvm::iterator_range<const_decl_iterator>; 1540 1541 decl_range decls() { return decl_range(decl_begin(), decl_end()); } 1542 1543 decl_const_range decls() const { 1544 return decl_const_range(decl_begin(), decl_end()); 1545 } 1546 1547 decl_iterator decl_begin() { return DG.begin(); } 1548 decl_iterator decl_end() { return DG.end(); } 1549 const_decl_iterator decl_begin() const { return DG.begin(); } 1550 const_decl_iterator decl_end() const { return DG.end(); } 1551 1552 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>; 1553 1554 reverse_decl_iterator decl_rbegin() { 1555 return reverse_decl_iterator(decl_end()); 1556 } 1557 1558 reverse_decl_iterator decl_rend() { 1559 return reverse_decl_iterator(decl_begin()); 1560 } 1561 }; 1562 1563 /// NullStmt - This is the null statement ";": C99 6.8.3p3. 1564 /// 1565 class NullStmt : public Stmt { 1566 public: 1567 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false) 1568 : Stmt(NullStmtClass) { 1569 NullStmtBits.HasLeadingEmptyMacro = hasLeadingEmptyMacro; 1570 setSemiLoc(L); 1571 } 1572 1573 /// Build an empty null statement. 1574 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {} 1575 1576 SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; } 1577 void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; } 1578 1579 bool hasLeadingEmptyMacro() const { 1580 return NullStmtBits.HasLeadingEmptyMacro; 1581 } 1582 1583 SourceLocation getBeginLoc() const { return getSemiLoc(); } 1584 SourceLocation getEndLoc() const { return getSemiLoc(); } 1585 1586 static bool classof(const Stmt *T) { 1587 return T->getStmtClass() == NullStmtClass; 1588 } 1589 1590 child_range children() { 1591 return child_range(child_iterator(), child_iterator()); 1592 } 1593 1594 const_child_range children() const { 1595 return const_child_range(const_child_iterator(), const_child_iterator()); 1596 } 1597 }; 1598 1599 /// CompoundStmt - This represents a group of statements like { stmt stmt }. 1600 class CompoundStmt final 1601 : public Stmt, 1602 private llvm::TrailingObjects<CompoundStmt, Stmt *, FPOptionsOverride> { 1603 friend class ASTStmtReader; 1604 friend TrailingObjects; 1605 1606 /// The location of the opening "{". 1607 SourceLocation LBraceLoc; 1608 1609 /// The location of the closing "}". 1610 SourceLocation RBraceLoc; 1611 1612 CompoundStmt(ArrayRef<Stmt *> Stmts, FPOptionsOverride FPFeatures, 1613 SourceLocation LB, SourceLocation RB); 1614 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {} 1615 1616 void setStmts(ArrayRef<Stmt *> Stmts); 1617 1618 /// Set FPOptionsOverride in trailing storage. Used only by Serialization. 1619 void setStoredFPFeatures(FPOptionsOverride F) { 1620 assert(hasStoredFPFeatures()); 1621 *getTrailingObjects<FPOptionsOverride>() = F; 1622 } 1623 1624 size_t numTrailingObjects(OverloadToken<Stmt *>) const { 1625 return CompoundStmtBits.NumStmts; 1626 } 1627 1628 public: 1629 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts, 1630 FPOptionsOverride FPFeatures, SourceLocation LB, 1631 SourceLocation RB); 1632 1633 // Build an empty compound statement with a location. 1634 explicit CompoundStmt(SourceLocation Loc) 1635 : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) { 1636 CompoundStmtBits.NumStmts = 0; 1637 CompoundStmtBits.HasFPFeatures = 0; 1638 } 1639 1640 // Build an empty compound statement. 1641 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts, 1642 bool HasFPFeatures); 1643 1644 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; } 1645 unsigned size() const { return CompoundStmtBits.NumStmts; } 1646 1647 bool hasStoredFPFeatures() const { return CompoundStmtBits.HasFPFeatures; } 1648 1649 /// Get FPOptionsOverride from trailing storage. 1650 FPOptionsOverride getStoredFPFeatures() const { 1651 assert(hasStoredFPFeatures()); 1652 return *getTrailingObjects<FPOptionsOverride>(); 1653 } 1654 1655 using body_iterator = Stmt **; 1656 using body_range = llvm::iterator_range<body_iterator>; 1657 1658 body_range body() { return body_range(body_begin(), body_end()); } 1659 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); } 1660 body_iterator body_end() { return body_begin() + size(); } 1661 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; } 1662 1663 Stmt *body_back() { 1664 return !body_empty() ? body_begin()[size() - 1] : nullptr; 1665 } 1666 1667 using const_body_iterator = Stmt *const *; 1668 using body_const_range = llvm::iterator_range<const_body_iterator>; 1669 1670 body_const_range body() const { 1671 return body_const_range(body_begin(), body_end()); 1672 } 1673 1674 const_body_iterator body_begin() const { 1675 return getTrailingObjects<Stmt *>(); 1676 } 1677 1678 const_body_iterator body_end() const { return body_begin() + size(); } 1679 1680 const Stmt *body_front() const { 1681 return !body_empty() ? body_begin()[0] : nullptr; 1682 } 1683 1684 const Stmt *body_back() const { 1685 return !body_empty() ? body_begin()[size() - 1] : nullptr; 1686 } 1687 1688 using reverse_body_iterator = std::reverse_iterator<body_iterator>; 1689 1690 reverse_body_iterator body_rbegin() { 1691 return reverse_body_iterator(body_end()); 1692 } 1693 1694 reverse_body_iterator body_rend() { 1695 return reverse_body_iterator(body_begin()); 1696 } 1697 1698 using const_reverse_body_iterator = 1699 std::reverse_iterator<const_body_iterator>; 1700 1701 const_reverse_body_iterator body_rbegin() const { 1702 return const_reverse_body_iterator(body_end()); 1703 } 1704 1705 const_reverse_body_iterator body_rend() const { 1706 return const_reverse_body_iterator(body_begin()); 1707 } 1708 1709 // Get the Stmt that StmtExpr would consider to be the result of this 1710 // compound statement. This is used by StmtExpr to properly emulate the GCC 1711 // compound expression extension, which ignores trailing NullStmts when 1712 // getting the result of the expression. 1713 // i.e. ({ 5;;; }) 1714 // ^^ ignored 1715 // If we don't find something that isn't a NullStmt, just return the last 1716 // Stmt. 1717 Stmt *getStmtExprResult() { 1718 for (auto *B : llvm::reverse(body())) { 1719 if (!isa<NullStmt>(B)) 1720 return B; 1721 } 1722 return body_back(); 1723 } 1724 1725 const Stmt *getStmtExprResult() const { 1726 return const_cast<CompoundStmt *>(this)->getStmtExprResult(); 1727 } 1728 1729 SourceLocation getBeginLoc() const { return LBraceLoc; } 1730 SourceLocation getEndLoc() const { return RBraceLoc; } 1731 1732 SourceLocation getLBracLoc() const { return LBraceLoc; } 1733 SourceLocation getRBracLoc() const { return RBraceLoc; } 1734 1735 static bool classof(const Stmt *T) { 1736 return T->getStmtClass() == CompoundStmtClass; 1737 } 1738 1739 // Iterators 1740 child_range children() { return child_range(body_begin(), body_end()); } 1741 1742 const_child_range children() const { 1743 return const_child_range(body_begin(), body_end()); 1744 } 1745 }; 1746 1747 // SwitchCase is the base class for CaseStmt and DefaultStmt, 1748 class SwitchCase : public Stmt { 1749 protected: 1750 /// The location of the ":". 1751 SourceLocation ColonLoc; 1752 1753 // The location of the "case" or "default" keyword. Stored in SwitchCaseBits. 1754 // SourceLocation KeywordLoc; 1755 1756 /// A pointer to the following CaseStmt or DefaultStmt class, 1757 /// used by SwitchStmt. 1758 SwitchCase *NextSwitchCase = nullptr; 1759 1760 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc) 1761 : Stmt(SC), ColonLoc(ColonLoc) { 1762 setKeywordLoc(KWLoc); 1763 } 1764 1765 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {} 1766 1767 public: 1768 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 1769 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 1770 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 1771 1772 SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; } 1773 void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; } 1774 SourceLocation getColonLoc() const { return ColonLoc; } 1775 void setColonLoc(SourceLocation L) { ColonLoc = L; } 1776 1777 inline Stmt *getSubStmt(); 1778 const Stmt *getSubStmt() const { 1779 return const_cast<SwitchCase *>(this)->getSubStmt(); 1780 } 1781 1782 SourceLocation getBeginLoc() const { return getKeywordLoc(); } 1783 inline SourceLocation getEndLoc() const LLVM_READONLY; 1784 1785 static bool classof(const Stmt *T) { 1786 return T->getStmtClass() == CaseStmtClass || 1787 T->getStmtClass() == DefaultStmtClass; 1788 } 1789 }; 1790 1791 /// CaseStmt - Represent a case statement. It can optionally be a GNU case 1792 /// statement of the form LHS ... RHS representing a range of cases. 1793 class CaseStmt final 1794 : public SwitchCase, 1795 private llvm::TrailingObjects<CaseStmt, Stmt *, SourceLocation> { 1796 friend TrailingObjects; 1797 1798 // CaseStmt is followed by several trailing objects, some of which optional. 1799 // Note that it would be more convenient to put the optional trailing objects 1800 // at the end but this would impact children(). 1801 // The trailing objects are in order: 1802 // 1803 // * A "Stmt *" for the LHS of the case statement. Always present. 1804 // 1805 // * A "Stmt *" for the RHS of the case statement. This is a GNU extension 1806 // which allow ranges in cases statement of the form LHS ... RHS. 1807 // Present if and only if caseStmtIsGNURange() is true. 1808 // 1809 // * A "Stmt *" for the substatement of the case statement. Always present. 1810 // 1811 // * A SourceLocation for the location of the ... if this is a case statement 1812 // with a range. Present if and only if caseStmtIsGNURange() is true. 1813 enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 }; 1814 enum { NumMandatoryStmtPtr = 2 }; 1815 1816 unsigned numTrailingObjects(OverloadToken<Stmt *>) const { 1817 return NumMandatoryStmtPtr + caseStmtIsGNURange(); 1818 } 1819 1820 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { 1821 return caseStmtIsGNURange(); 1822 } 1823 1824 unsigned lhsOffset() const { return LhsOffset; } 1825 unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); } 1826 unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; } 1827 1828 /// Build a case statement assuming that the storage for the 1829 /// trailing objects has been properly allocated. 1830 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc, 1831 SourceLocation ellipsisLoc, SourceLocation colonLoc) 1832 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) { 1833 // Handle GNU case statements of the form LHS ... RHS. 1834 bool IsGNURange = rhs != nullptr; 1835 SwitchCaseBits.CaseStmtIsGNURange = IsGNURange; 1836 setLHS(lhs); 1837 setSubStmt(nullptr); 1838 if (IsGNURange) { 1839 setRHS(rhs); 1840 setEllipsisLoc(ellipsisLoc); 1841 } 1842 } 1843 1844 /// Build an empty switch case statement. 1845 explicit CaseStmt(EmptyShell Empty, bool CaseStmtIsGNURange) 1846 : SwitchCase(CaseStmtClass, Empty) { 1847 SwitchCaseBits.CaseStmtIsGNURange = CaseStmtIsGNURange; 1848 } 1849 1850 public: 1851 /// Build a case statement. 1852 static CaseStmt *Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs, 1853 SourceLocation caseLoc, SourceLocation ellipsisLoc, 1854 SourceLocation colonLoc); 1855 1856 /// Build an empty case statement. 1857 static CaseStmt *CreateEmpty(const ASTContext &Ctx, bool CaseStmtIsGNURange); 1858 1859 /// True if this case statement is of the form case LHS ... RHS, which 1860 /// is a GNU extension. In this case the RHS can be obtained with getRHS() 1861 /// and the location of the ellipsis can be obtained with getEllipsisLoc(). 1862 bool caseStmtIsGNURange() const { return SwitchCaseBits.CaseStmtIsGNURange; } 1863 1864 SourceLocation getCaseLoc() const { return getKeywordLoc(); } 1865 void setCaseLoc(SourceLocation L) { setKeywordLoc(L); } 1866 1867 /// Get the location of the ... in a case statement of the form LHS ... RHS. 1868 SourceLocation getEllipsisLoc() const { 1869 return caseStmtIsGNURange() ? *getTrailingObjects<SourceLocation>() 1870 : SourceLocation(); 1871 } 1872 1873 /// Set the location of the ... in a case statement of the form LHS ... RHS. 1874 /// Assert that this case statement is of this form. 1875 void setEllipsisLoc(SourceLocation L) { 1876 assert( 1877 caseStmtIsGNURange() && 1878 "setEllipsisLoc but this is not a case stmt of the form LHS ... RHS!"); 1879 *getTrailingObjects<SourceLocation>() = L; 1880 } 1881 1882 Expr *getLHS() { 1883 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]); 1884 } 1885 1886 const Expr *getLHS() const { 1887 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]); 1888 } 1889 1890 void setLHS(Expr *Val) { 1891 getTrailingObjects<Stmt *>()[lhsOffset()] = reinterpret_cast<Stmt *>(Val); 1892 } 1893 1894 Expr *getRHS() { 1895 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>( 1896 getTrailingObjects<Stmt *>()[rhsOffset()]) 1897 : nullptr; 1898 } 1899 1900 const Expr *getRHS() const { 1901 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>( 1902 getTrailingObjects<Stmt *>()[rhsOffset()]) 1903 : nullptr; 1904 } 1905 1906 void setRHS(Expr *Val) { 1907 assert(caseStmtIsGNURange() && 1908 "setRHS but this is not a case stmt of the form LHS ... RHS!"); 1909 getTrailingObjects<Stmt *>()[rhsOffset()] = reinterpret_cast<Stmt *>(Val); 1910 } 1911 1912 Stmt *getSubStmt() { return getTrailingObjects<Stmt *>()[subStmtOffset()]; } 1913 const Stmt *getSubStmt() const { 1914 return getTrailingObjects<Stmt *>()[subStmtOffset()]; 1915 } 1916 1917 void setSubStmt(Stmt *S) { 1918 getTrailingObjects<Stmt *>()[subStmtOffset()] = S; 1919 } 1920 1921 SourceLocation getBeginLoc() const { return getKeywordLoc(); } 1922 SourceLocation getEndLoc() const LLVM_READONLY { 1923 // Handle deeply nested case statements with iteration instead of recursion. 1924 const CaseStmt *CS = this; 1925 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) 1926 CS = CS2; 1927 1928 return CS->getSubStmt()->getEndLoc(); 1929 } 1930 1931 static bool classof(const Stmt *T) { 1932 return T->getStmtClass() == CaseStmtClass; 1933 } 1934 1935 // Iterators 1936 child_range children() { 1937 return child_range(getTrailingObjects<Stmt *>(), 1938 getTrailingObjects<Stmt *>() + 1939 numTrailingObjects(OverloadToken<Stmt *>())); 1940 } 1941 1942 const_child_range children() const { 1943 return const_child_range(getTrailingObjects<Stmt *>(), 1944 getTrailingObjects<Stmt *>() + 1945 numTrailingObjects(OverloadToken<Stmt *>())); 1946 } 1947 }; 1948 1949 class DefaultStmt : public SwitchCase { 1950 Stmt *SubStmt; 1951 1952 public: 1953 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) 1954 : SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {} 1955 1956 /// Build an empty default statement. 1957 explicit DefaultStmt(EmptyShell Empty) 1958 : SwitchCase(DefaultStmtClass, Empty) {} 1959 1960 Stmt *getSubStmt() { return SubStmt; } 1961 const Stmt *getSubStmt() const { return SubStmt; } 1962 void setSubStmt(Stmt *S) { SubStmt = S; } 1963 1964 SourceLocation getDefaultLoc() const { return getKeywordLoc(); } 1965 void setDefaultLoc(SourceLocation L) { setKeywordLoc(L); } 1966 1967 SourceLocation getBeginLoc() const { return getKeywordLoc(); } 1968 SourceLocation getEndLoc() const LLVM_READONLY { 1969 return SubStmt->getEndLoc(); 1970 } 1971 1972 static bool classof(const Stmt *T) { 1973 return T->getStmtClass() == DefaultStmtClass; 1974 } 1975 1976 // Iterators 1977 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 1978 1979 const_child_range children() const { 1980 return const_child_range(&SubStmt, &SubStmt + 1); 1981 } 1982 }; 1983 1984 SourceLocation SwitchCase::getEndLoc() const { 1985 if (const auto *CS = dyn_cast<CaseStmt>(this)) 1986 return CS->getEndLoc(); 1987 else if (const auto *DS = dyn_cast<DefaultStmt>(this)) 1988 return DS->getEndLoc(); 1989 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!"); 1990 } 1991 1992 Stmt *SwitchCase::getSubStmt() { 1993 if (auto *CS = dyn_cast<CaseStmt>(this)) 1994 return CS->getSubStmt(); 1995 else if (auto *DS = dyn_cast<DefaultStmt>(this)) 1996 return DS->getSubStmt(); 1997 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!"); 1998 } 1999 2000 /// Represents a statement that could possibly have a value and type. This 2001 /// covers expression-statements, as well as labels and attributed statements. 2002 /// 2003 /// Value statements have a special meaning when they are the last non-null 2004 /// statement in a GNU statement expression, where they determine the value 2005 /// of the statement expression. 2006 class ValueStmt : public Stmt { 2007 protected: 2008 using Stmt::Stmt; 2009 2010 public: 2011 const Expr *getExprStmt() const; 2012 Expr *getExprStmt() { 2013 const ValueStmt *ConstThis = this; 2014 return const_cast<Expr*>(ConstThis->getExprStmt()); 2015 } 2016 2017 static bool classof(const Stmt *T) { 2018 return T->getStmtClass() >= firstValueStmtConstant && 2019 T->getStmtClass() <= lastValueStmtConstant; 2020 } 2021 }; 2022 2023 /// LabelStmt - Represents a label, which has a substatement. For example: 2024 /// foo: return; 2025 class LabelStmt : public ValueStmt { 2026 LabelDecl *TheDecl; 2027 Stmt *SubStmt; 2028 bool SideEntry = false; 2029 2030 public: 2031 /// Build a label statement. 2032 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt) 2033 : ValueStmt(LabelStmtClass), TheDecl(D), SubStmt(substmt) { 2034 setIdentLoc(IL); 2035 } 2036 2037 /// Build an empty label statement. 2038 explicit LabelStmt(EmptyShell Empty) : ValueStmt(LabelStmtClass, Empty) {} 2039 2040 SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; } 2041 void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; } 2042 2043 LabelDecl *getDecl() const { return TheDecl; } 2044 void setDecl(LabelDecl *D) { TheDecl = D; } 2045 2046 const char *getName() const; 2047 Stmt *getSubStmt() { return SubStmt; } 2048 2049 const Stmt *getSubStmt() const { return SubStmt; } 2050 void setSubStmt(Stmt *SS) { SubStmt = SS; } 2051 2052 SourceLocation getBeginLoc() const { return getIdentLoc(); } 2053 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();} 2054 2055 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 2056 2057 const_child_range children() const { 2058 return const_child_range(&SubStmt, &SubStmt + 1); 2059 } 2060 2061 static bool classof(const Stmt *T) { 2062 return T->getStmtClass() == LabelStmtClass; 2063 } 2064 bool isSideEntry() const { return SideEntry; } 2065 void setSideEntry(bool SE) { SideEntry = SE; } 2066 }; 2067 2068 /// Represents an attribute applied to a statement. 2069 /// 2070 /// Represents an attribute applied to a statement. For example: 2071 /// [[omp::for(...)]] for (...) { ... } 2072 class AttributedStmt final 2073 : public ValueStmt, 2074 private llvm::TrailingObjects<AttributedStmt, const Attr *> { 2075 friend class ASTStmtReader; 2076 friend TrailingObjects; 2077 2078 Stmt *SubStmt; 2079 2080 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr *> Attrs, 2081 Stmt *SubStmt) 2082 : ValueStmt(AttributedStmtClass), SubStmt(SubStmt) { 2083 AttributedStmtBits.NumAttrs = Attrs.size(); 2084 AttributedStmtBits.AttrLoc = Loc; 2085 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr()); 2086 } 2087 2088 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs) 2089 : ValueStmt(AttributedStmtClass, Empty) { 2090 AttributedStmtBits.NumAttrs = NumAttrs; 2091 AttributedStmtBits.AttrLoc = SourceLocation{}; 2092 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr); 2093 } 2094 2095 const Attr *const *getAttrArrayPtr() const { 2096 return getTrailingObjects<const Attr *>(); 2097 } 2098 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); } 2099 2100 public: 2101 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc, 2102 ArrayRef<const Attr *> Attrs, Stmt *SubStmt); 2103 2104 // Build an empty attributed statement. 2105 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs); 2106 2107 SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; } 2108 ArrayRef<const Attr *> getAttrs() const { 2109 return llvm::ArrayRef(getAttrArrayPtr(), AttributedStmtBits.NumAttrs); 2110 } 2111 2112 Stmt *getSubStmt() { return SubStmt; } 2113 const Stmt *getSubStmt() const { return SubStmt; } 2114 2115 SourceLocation getBeginLoc() const { return getAttrLoc(); } 2116 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();} 2117 2118 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 2119 2120 const_child_range children() const { 2121 return const_child_range(&SubStmt, &SubStmt + 1); 2122 } 2123 2124 static bool classof(const Stmt *T) { 2125 return T->getStmtClass() == AttributedStmtClass; 2126 } 2127 }; 2128 2129 /// IfStmt - This represents an if/then/else. 2130 class IfStmt final 2131 : public Stmt, 2132 private llvm::TrailingObjects<IfStmt, Stmt *, SourceLocation> { 2133 friend TrailingObjects; 2134 2135 // IfStmt is followed by several trailing objects, some of which optional. 2136 // Note that it would be more convenient to put the optional trailing 2137 // objects at then end but this would change the order of the children. 2138 // The trailing objects are in order: 2139 // 2140 // * A "Stmt *" for the init statement. 2141 // Present if and only if hasInitStorage(). 2142 // 2143 // * A "Stmt *" for the condition variable. 2144 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *". 2145 // 2146 // * A "Stmt *" for the condition. 2147 // Always present. This is in fact a "Expr *". 2148 // 2149 // * A "Stmt *" for the then statement. 2150 // Always present. 2151 // 2152 // * A "Stmt *" for the else statement. 2153 // Present if and only if hasElseStorage(). 2154 // 2155 // * A "SourceLocation" for the location of the "else". 2156 // Present if and only if hasElseStorage(). 2157 enum { InitOffset = 0, ThenOffsetFromCond = 1, ElseOffsetFromCond = 2 }; 2158 enum { NumMandatoryStmtPtr = 2 }; 2159 SourceLocation LParenLoc; 2160 SourceLocation RParenLoc; 2161 2162 unsigned numTrailingObjects(OverloadToken<Stmt *>) const { 2163 return NumMandatoryStmtPtr + hasElseStorage() + hasVarStorage() + 2164 hasInitStorage(); 2165 } 2166 2167 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { 2168 return hasElseStorage(); 2169 } 2170 2171 unsigned initOffset() const { return InitOffset; } 2172 unsigned varOffset() const { return InitOffset + hasInitStorage(); } 2173 unsigned condOffset() const { 2174 return InitOffset + hasInitStorage() + hasVarStorage(); 2175 } 2176 unsigned thenOffset() const { return condOffset() + ThenOffsetFromCond; } 2177 unsigned elseOffset() const { return condOffset() + ElseOffsetFromCond; } 2178 2179 /// Build an if/then/else statement. 2180 IfStmt(const ASTContext &Ctx, SourceLocation IL, IfStatementKind Kind, 2181 Stmt *Init, VarDecl *Var, Expr *Cond, SourceLocation LParenLoc, 2182 SourceLocation RParenLoc, Stmt *Then, SourceLocation EL, Stmt *Else); 2183 2184 /// Build an empty if/then/else statement. 2185 explicit IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit); 2186 2187 public: 2188 /// Create an IfStmt. 2189 static IfStmt *Create(const ASTContext &Ctx, SourceLocation IL, 2190 IfStatementKind Kind, Stmt *Init, VarDecl *Var, 2191 Expr *Cond, SourceLocation LPL, SourceLocation RPL, 2192 Stmt *Then, SourceLocation EL = SourceLocation(), 2193 Stmt *Else = nullptr); 2194 2195 /// Create an empty IfStmt optionally with storage for an else statement, 2196 /// condition variable and init expression. 2197 static IfStmt *CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar, 2198 bool HasInit); 2199 2200 /// True if this IfStmt has the storage for an init statement. 2201 bool hasInitStorage() const { return IfStmtBits.HasInit; } 2202 2203 /// True if this IfStmt has storage for a variable declaration. 2204 bool hasVarStorage() const { return IfStmtBits.HasVar; } 2205 2206 /// True if this IfStmt has storage for an else statement. 2207 bool hasElseStorage() const { return IfStmtBits.HasElse; } 2208 2209 Expr *getCond() { 2210 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]); 2211 } 2212 2213 const Expr *getCond() const { 2214 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]); 2215 } 2216 2217 void setCond(Expr *Cond) { 2218 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond); 2219 } 2220 2221 Stmt *getThen() { return getTrailingObjects<Stmt *>()[thenOffset()]; } 2222 const Stmt *getThen() const { 2223 return getTrailingObjects<Stmt *>()[thenOffset()]; 2224 } 2225 2226 void setThen(Stmt *Then) { 2227 getTrailingObjects<Stmt *>()[thenOffset()] = Then; 2228 } 2229 2230 Stmt *getElse() { 2231 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()] 2232 : nullptr; 2233 } 2234 2235 const Stmt *getElse() const { 2236 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()] 2237 : nullptr; 2238 } 2239 2240 void setElse(Stmt *Else) { 2241 assert(hasElseStorage() && 2242 "This if statement has no storage for an else statement!"); 2243 getTrailingObjects<Stmt *>()[elseOffset()] = Else; 2244 } 2245 2246 /// Retrieve the variable declared in this "if" statement, if any. 2247 /// 2248 /// In the following example, "x" is the condition variable. 2249 /// \code 2250 /// if (int x = foo()) { 2251 /// printf("x is %d", x); 2252 /// } 2253 /// \endcode 2254 VarDecl *getConditionVariable(); 2255 const VarDecl *getConditionVariable() const { 2256 return const_cast<IfStmt *>(this)->getConditionVariable(); 2257 } 2258 2259 /// Set the condition variable for this if statement. 2260 /// The if statement must have storage for the condition variable. 2261 void setConditionVariable(const ASTContext &Ctx, VarDecl *V); 2262 2263 /// If this IfStmt has a condition variable, return the faux DeclStmt 2264 /// associated with the creation of that condition variable. 2265 DeclStmt *getConditionVariableDeclStmt() { 2266 return hasVarStorage() ? static_cast<DeclStmt *>( 2267 getTrailingObjects<Stmt *>()[varOffset()]) 2268 : nullptr; 2269 } 2270 2271 const DeclStmt *getConditionVariableDeclStmt() const { 2272 return hasVarStorage() ? static_cast<DeclStmt *>( 2273 getTrailingObjects<Stmt *>()[varOffset()]) 2274 : nullptr; 2275 } 2276 2277 void setConditionVariableDeclStmt(DeclStmt *CondVar) { 2278 assert(hasVarStorage()); 2279 getTrailingObjects<Stmt *>()[varOffset()] = CondVar; 2280 } 2281 2282 Stmt *getInit() { 2283 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()] 2284 : nullptr; 2285 } 2286 2287 const Stmt *getInit() const { 2288 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()] 2289 : nullptr; 2290 } 2291 2292 void setInit(Stmt *Init) { 2293 assert(hasInitStorage() && 2294 "This if statement has no storage for an init statement!"); 2295 getTrailingObjects<Stmt *>()[initOffset()] = Init; 2296 } 2297 2298 SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; } 2299 void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; } 2300 2301 SourceLocation getElseLoc() const { 2302 return hasElseStorage() ? *getTrailingObjects<SourceLocation>() 2303 : SourceLocation(); 2304 } 2305 2306 void setElseLoc(SourceLocation ElseLoc) { 2307 assert(hasElseStorage() && 2308 "This if statement has no storage for an else statement!"); 2309 *getTrailingObjects<SourceLocation>() = ElseLoc; 2310 } 2311 2312 bool isConsteval() const { 2313 return getStatementKind() == IfStatementKind::ConstevalNonNegated || 2314 getStatementKind() == IfStatementKind::ConstevalNegated; 2315 } 2316 2317 bool isNonNegatedConsteval() const { 2318 return getStatementKind() == IfStatementKind::ConstevalNonNegated; 2319 } 2320 2321 bool isNegatedConsteval() const { 2322 return getStatementKind() == IfStatementKind::ConstevalNegated; 2323 } 2324 2325 bool isConstexpr() const { 2326 return getStatementKind() == IfStatementKind::Constexpr; 2327 } 2328 2329 void setStatementKind(IfStatementKind Kind) { 2330 IfStmtBits.Kind = static_cast<unsigned>(Kind); 2331 } 2332 2333 IfStatementKind getStatementKind() const { 2334 return static_cast<IfStatementKind>(IfStmtBits.Kind); 2335 } 2336 2337 /// If this is an 'if constexpr', determine which substatement will be taken. 2338 /// Otherwise, or if the condition is value-dependent, returns std::nullopt. 2339 std::optional<const Stmt *> getNondiscardedCase(const ASTContext &Ctx) const; 2340 std::optional<Stmt *> getNondiscardedCase(const ASTContext &Ctx); 2341 2342 bool isObjCAvailabilityCheck() const; 2343 2344 SourceLocation getBeginLoc() const { return getIfLoc(); } 2345 SourceLocation getEndLoc() const LLVM_READONLY { 2346 if (getElse()) 2347 return getElse()->getEndLoc(); 2348 return getThen()->getEndLoc(); 2349 } 2350 SourceLocation getLParenLoc() const { return LParenLoc; } 2351 void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; } 2352 SourceLocation getRParenLoc() const { return RParenLoc; } 2353 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; } 2354 2355 // Iterators over subexpressions. The iterators will include iterating 2356 // over the initialization expression referenced by the condition variable. 2357 child_range children() { 2358 // We always store a condition, but there is none for consteval if 2359 // statements, so skip it. 2360 return child_range(getTrailingObjects<Stmt *>() + 2361 (isConsteval() ? thenOffset() : 0), 2362 getTrailingObjects<Stmt *>() + 2363 numTrailingObjects(OverloadToken<Stmt *>())); 2364 } 2365 2366 const_child_range children() const { 2367 // We always store a condition, but there is none for consteval if 2368 // statements, so skip it. 2369 return const_child_range(getTrailingObjects<Stmt *>() + 2370 (isConsteval() ? thenOffset() : 0), 2371 getTrailingObjects<Stmt *>() + 2372 numTrailingObjects(OverloadToken<Stmt *>())); 2373 } 2374 2375 static bool classof(const Stmt *T) { 2376 return T->getStmtClass() == IfStmtClass; 2377 } 2378 }; 2379 2380 /// SwitchStmt - This represents a 'switch' stmt. 2381 class SwitchStmt final : public Stmt, 2382 private llvm::TrailingObjects<SwitchStmt, Stmt *> { 2383 friend TrailingObjects; 2384 2385 /// Points to a linked list of case and default statements. 2386 SwitchCase *FirstCase = nullptr; 2387 2388 // SwitchStmt is followed by several trailing objects, 2389 // some of which optional. Note that it would be more convenient to 2390 // put the optional trailing objects at the end but this would change 2391 // the order in children(). 2392 // The trailing objects are in order: 2393 // 2394 // * A "Stmt *" for the init statement. 2395 // Present if and only if hasInitStorage(). 2396 // 2397 // * A "Stmt *" for the condition variable. 2398 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *". 2399 // 2400 // * A "Stmt *" for the condition. 2401 // Always present. This is in fact an "Expr *". 2402 // 2403 // * A "Stmt *" for the body. 2404 // Always present. 2405 enum { InitOffset = 0, BodyOffsetFromCond = 1 }; 2406 enum { NumMandatoryStmtPtr = 2 }; 2407 SourceLocation LParenLoc; 2408 SourceLocation RParenLoc; 2409 2410 unsigned numTrailingObjects(OverloadToken<Stmt *>) const { 2411 return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage(); 2412 } 2413 2414 unsigned initOffset() const { return InitOffset; } 2415 unsigned varOffset() const { return InitOffset + hasInitStorage(); } 2416 unsigned condOffset() const { 2417 return InitOffset + hasInitStorage() + hasVarStorage(); 2418 } 2419 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; } 2420 2421 /// Build a switch statement. 2422 SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, Expr *Cond, 2423 SourceLocation LParenLoc, SourceLocation RParenLoc); 2424 2425 /// Build a empty switch statement. 2426 explicit SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar); 2427 2428 public: 2429 /// Create a switch statement. 2430 static SwitchStmt *Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, 2431 Expr *Cond, SourceLocation LParenLoc, 2432 SourceLocation RParenLoc); 2433 2434 /// Create an empty switch statement optionally with storage for 2435 /// an init expression and a condition variable. 2436 static SwitchStmt *CreateEmpty(const ASTContext &Ctx, bool HasInit, 2437 bool HasVar); 2438 2439 /// True if this SwitchStmt has storage for an init statement. 2440 bool hasInitStorage() const { return SwitchStmtBits.HasInit; } 2441 2442 /// True if this SwitchStmt has storage for a condition variable. 2443 bool hasVarStorage() const { return SwitchStmtBits.HasVar; } 2444 2445 Expr *getCond() { 2446 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]); 2447 } 2448 2449 const Expr *getCond() const { 2450 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]); 2451 } 2452 2453 void setCond(Expr *Cond) { 2454 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond); 2455 } 2456 2457 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; } 2458 const Stmt *getBody() const { 2459 return getTrailingObjects<Stmt *>()[bodyOffset()]; 2460 } 2461 2462 void setBody(Stmt *Body) { 2463 getTrailingObjects<Stmt *>()[bodyOffset()] = Body; 2464 } 2465 2466 Stmt *getInit() { 2467 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()] 2468 : nullptr; 2469 } 2470 2471 const Stmt *getInit() const { 2472 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()] 2473 : nullptr; 2474 } 2475 2476 void setInit(Stmt *Init) { 2477 assert(hasInitStorage() && 2478 "This switch statement has no storage for an init statement!"); 2479 getTrailingObjects<Stmt *>()[initOffset()] = Init; 2480 } 2481 2482 /// Retrieve the variable declared in this "switch" statement, if any. 2483 /// 2484 /// In the following example, "x" is the condition variable. 2485 /// \code 2486 /// switch (int x = foo()) { 2487 /// case 0: break; 2488 /// // ... 2489 /// } 2490 /// \endcode 2491 VarDecl *getConditionVariable(); 2492 const VarDecl *getConditionVariable() const { 2493 return const_cast<SwitchStmt *>(this)->getConditionVariable(); 2494 } 2495 2496 /// Set the condition variable in this switch statement. 2497 /// The switch statement must have storage for it. 2498 void setConditionVariable(const ASTContext &Ctx, VarDecl *VD); 2499 2500 /// If this SwitchStmt has a condition variable, return the faux DeclStmt 2501 /// associated with the creation of that condition variable. 2502 DeclStmt *getConditionVariableDeclStmt() { 2503 return hasVarStorage() ? static_cast<DeclStmt *>( 2504 getTrailingObjects<Stmt *>()[varOffset()]) 2505 : nullptr; 2506 } 2507 2508 const DeclStmt *getConditionVariableDeclStmt() const { 2509 return hasVarStorage() ? static_cast<DeclStmt *>( 2510 getTrailingObjects<Stmt *>()[varOffset()]) 2511 : nullptr; 2512 } 2513 2514 void setConditionVariableDeclStmt(DeclStmt *CondVar) { 2515 assert(hasVarStorage()); 2516 getTrailingObjects<Stmt *>()[varOffset()] = CondVar; 2517 } 2518 2519 SwitchCase *getSwitchCaseList() { return FirstCase; } 2520 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 2521 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } 2522 2523 SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; } 2524 void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; } 2525 SourceLocation getLParenLoc() const { return LParenLoc; } 2526 void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; } 2527 SourceLocation getRParenLoc() const { return RParenLoc; } 2528 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; } 2529 2530 void setBody(Stmt *S, SourceLocation SL) { 2531 setBody(S); 2532 setSwitchLoc(SL); 2533 } 2534 2535 void addSwitchCase(SwitchCase *SC) { 2536 assert(!SC->getNextSwitchCase() && 2537 "case/default already added to a switch"); 2538 SC->setNextSwitchCase(FirstCase); 2539 FirstCase = SC; 2540 } 2541 2542 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a 2543 /// switch over an enum value then all cases have been explicitly covered. 2544 void setAllEnumCasesCovered() { SwitchStmtBits.AllEnumCasesCovered = true; } 2545 2546 /// Returns true if the SwitchStmt is a switch of an enum value and all cases 2547 /// have been explicitly covered. 2548 bool isAllEnumCasesCovered() const { 2549 return SwitchStmtBits.AllEnumCasesCovered; 2550 } 2551 2552 SourceLocation getBeginLoc() const { return getSwitchLoc(); } 2553 SourceLocation getEndLoc() const LLVM_READONLY { 2554 return getBody() ? getBody()->getEndLoc() 2555 : reinterpret_cast<const Stmt *>(getCond())->getEndLoc(); 2556 } 2557 2558 // Iterators 2559 child_range children() { 2560 return child_range(getTrailingObjects<Stmt *>(), 2561 getTrailingObjects<Stmt *>() + 2562 numTrailingObjects(OverloadToken<Stmt *>())); 2563 } 2564 2565 const_child_range children() const { 2566 return const_child_range(getTrailingObjects<Stmt *>(), 2567 getTrailingObjects<Stmt *>() + 2568 numTrailingObjects(OverloadToken<Stmt *>())); 2569 } 2570 2571 static bool classof(const Stmt *T) { 2572 return T->getStmtClass() == SwitchStmtClass; 2573 } 2574 }; 2575 2576 /// WhileStmt - This represents a 'while' stmt. 2577 class WhileStmt final : public Stmt, 2578 private llvm::TrailingObjects<WhileStmt, Stmt *> { 2579 friend TrailingObjects; 2580 2581 // WhileStmt is followed by several trailing objects, 2582 // some of which optional. Note that it would be more 2583 // convenient to put the optional trailing object at the end 2584 // but this would affect children(). 2585 // The trailing objects are in order: 2586 // 2587 // * A "Stmt *" for the condition variable. 2588 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *". 2589 // 2590 // * A "Stmt *" for the condition. 2591 // Always present. This is in fact an "Expr *". 2592 // 2593 // * A "Stmt *" for the body. 2594 // Always present. 2595 // 2596 enum { VarOffset = 0, BodyOffsetFromCond = 1 }; 2597 enum { NumMandatoryStmtPtr = 2 }; 2598 2599 SourceLocation LParenLoc, RParenLoc; 2600 2601 unsigned varOffset() const { return VarOffset; } 2602 unsigned condOffset() const { return VarOffset + hasVarStorage(); } 2603 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; } 2604 2605 unsigned numTrailingObjects(OverloadToken<Stmt *>) const { 2606 return NumMandatoryStmtPtr + hasVarStorage(); 2607 } 2608 2609 /// Build a while statement. 2610 WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, Stmt *Body, 2611 SourceLocation WL, SourceLocation LParenLoc, 2612 SourceLocation RParenLoc); 2613 2614 /// Build an empty while statement. 2615 explicit WhileStmt(EmptyShell Empty, bool HasVar); 2616 2617 public: 2618 /// Create a while statement. 2619 static WhileStmt *Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, 2620 Stmt *Body, SourceLocation WL, 2621 SourceLocation LParenLoc, SourceLocation RParenLoc); 2622 2623 /// Create an empty while statement optionally with storage for 2624 /// a condition variable. 2625 static WhileStmt *CreateEmpty(const ASTContext &Ctx, bool HasVar); 2626 2627 /// True if this WhileStmt has storage for a condition variable. 2628 bool hasVarStorage() const { return WhileStmtBits.HasVar; } 2629 2630 Expr *getCond() { 2631 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]); 2632 } 2633 2634 const Expr *getCond() const { 2635 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]); 2636 } 2637 2638 void setCond(Expr *Cond) { 2639 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond); 2640 } 2641 2642 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; } 2643 const Stmt *getBody() const { 2644 return getTrailingObjects<Stmt *>()[bodyOffset()]; 2645 } 2646 2647 void setBody(Stmt *Body) { 2648 getTrailingObjects<Stmt *>()[bodyOffset()] = Body; 2649 } 2650 2651 /// Retrieve the variable declared in this "while" statement, if any. 2652 /// 2653 /// In the following example, "x" is the condition variable. 2654 /// \code 2655 /// while (int x = random()) { 2656 /// // ... 2657 /// } 2658 /// \endcode 2659 VarDecl *getConditionVariable(); 2660 const VarDecl *getConditionVariable() const { 2661 return const_cast<WhileStmt *>(this)->getConditionVariable(); 2662 } 2663 2664 /// Set the condition variable of this while statement. 2665 /// The while statement must have storage for it. 2666 void setConditionVariable(const ASTContext &Ctx, VarDecl *V); 2667 2668 /// If this WhileStmt has a condition variable, return the faux DeclStmt 2669 /// associated with the creation of that condition variable. 2670 DeclStmt *getConditionVariableDeclStmt() { 2671 return hasVarStorage() ? static_cast<DeclStmt *>( 2672 getTrailingObjects<Stmt *>()[varOffset()]) 2673 : nullptr; 2674 } 2675 2676 const DeclStmt *getConditionVariableDeclStmt() const { 2677 return hasVarStorage() ? static_cast<DeclStmt *>( 2678 getTrailingObjects<Stmt *>()[varOffset()]) 2679 : nullptr; 2680 } 2681 2682 void setConditionVariableDeclStmt(DeclStmt *CondVar) { 2683 assert(hasVarStorage()); 2684 getTrailingObjects<Stmt *>()[varOffset()] = CondVar; 2685 } 2686 2687 SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; } 2688 void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; } 2689 2690 SourceLocation getLParenLoc() const { return LParenLoc; } 2691 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2692 SourceLocation getRParenLoc() const { return RParenLoc; } 2693 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2694 2695 SourceLocation getBeginLoc() const { return getWhileLoc(); } 2696 SourceLocation getEndLoc() const LLVM_READONLY { 2697 return getBody()->getEndLoc(); 2698 } 2699 2700 static bool classof(const Stmt *T) { 2701 return T->getStmtClass() == WhileStmtClass; 2702 } 2703 2704 // Iterators 2705 child_range children() { 2706 return child_range(getTrailingObjects<Stmt *>(), 2707 getTrailingObjects<Stmt *>() + 2708 numTrailingObjects(OverloadToken<Stmt *>())); 2709 } 2710 2711 const_child_range children() const { 2712 return const_child_range(getTrailingObjects<Stmt *>(), 2713 getTrailingObjects<Stmt *>() + 2714 numTrailingObjects(OverloadToken<Stmt *>())); 2715 } 2716 }; 2717 2718 /// DoStmt - This represents a 'do/while' stmt. 2719 class DoStmt : public Stmt { 2720 enum { BODY, COND, END_EXPR }; 2721 Stmt *SubExprs[END_EXPR]; 2722 SourceLocation WhileLoc; 2723 SourceLocation RParenLoc; // Location of final ')' in do stmt condition. 2724 2725 public: 2726 DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL, 2727 SourceLocation RP) 2728 : Stmt(DoStmtClass), WhileLoc(WL), RParenLoc(RP) { 2729 setCond(Cond); 2730 setBody(Body); 2731 setDoLoc(DL); 2732 } 2733 2734 /// Build an empty do-while statement. 2735 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {} 2736 2737 Expr *getCond() { return reinterpret_cast<Expr *>(SubExprs[COND]); } 2738 const Expr *getCond() const { 2739 return reinterpret_cast<Expr *>(SubExprs[COND]); 2740 } 2741 2742 void setCond(Expr *Cond) { SubExprs[COND] = reinterpret_cast<Stmt *>(Cond); } 2743 2744 Stmt *getBody() { return SubExprs[BODY]; } 2745 const Stmt *getBody() const { return SubExprs[BODY]; } 2746 void setBody(Stmt *Body) { SubExprs[BODY] = Body; } 2747 2748 SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; } 2749 void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; } 2750 SourceLocation getWhileLoc() const { return WhileLoc; } 2751 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 2752 SourceLocation getRParenLoc() const { return RParenLoc; } 2753 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2754 2755 SourceLocation getBeginLoc() const { return getDoLoc(); } 2756 SourceLocation getEndLoc() const { return getRParenLoc(); } 2757 2758 static bool classof(const Stmt *T) { 2759 return T->getStmtClass() == DoStmtClass; 2760 } 2761 2762 // Iterators 2763 child_range children() { 2764 return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); 2765 } 2766 2767 const_child_range children() const { 2768 return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); 2769 } 2770 }; 2771 2772 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 2773 /// the init/cond/inc parts of the ForStmt will be null if they were not 2774 /// specified in the source. 2775 class ForStmt : public Stmt { 2776 friend class ASTStmtReader; 2777 2778 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR }; 2779 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 2780 SourceLocation LParenLoc, RParenLoc; 2781 2782 public: 2783 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, 2784 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP, 2785 SourceLocation RP); 2786 2787 /// Build an empty for statement. 2788 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {} 2789 2790 Stmt *getInit() { return SubExprs[INIT]; } 2791 2792 /// Retrieve the variable declared in this "for" statement, if any. 2793 /// 2794 /// In the following example, "y" is the condition variable. 2795 /// \code 2796 /// for (int x = random(); int y = mangle(x); ++x) { 2797 /// // ... 2798 /// } 2799 /// \endcode 2800 VarDecl *getConditionVariable() const; 2801 void setConditionVariable(const ASTContext &C, VarDecl *V); 2802 2803 /// If this ForStmt has a condition variable, return the faux DeclStmt 2804 /// associated with the creation of that condition variable. 2805 DeclStmt *getConditionVariableDeclStmt() { 2806 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]); 2807 } 2808 2809 const DeclStmt *getConditionVariableDeclStmt() const { 2810 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]); 2811 } 2812 2813 void setConditionVariableDeclStmt(DeclStmt *CondVar) { 2814 SubExprs[CONDVAR] = CondVar; 2815 } 2816 2817 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 2818 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 2819 Stmt *getBody() { return SubExprs[BODY]; } 2820 2821 const Stmt *getInit() const { return SubExprs[INIT]; } 2822 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 2823 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 2824 const Stmt *getBody() const { return SubExprs[BODY]; } 2825 2826 void setInit(Stmt *S) { SubExprs[INIT] = S; } 2827 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 2828 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); } 2829 void setBody(Stmt *S) { SubExprs[BODY] = S; } 2830 2831 SourceLocation getForLoc() const { return ForStmtBits.ForLoc; } 2832 void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; } 2833 SourceLocation getLParenLoc() const { return LParenLoc; } 2834 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2835 SourceLocation getRParenLoc() const { return RParenLoc; } 2836 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2837 2838 SourceLocation getBeginLoc() const { return getForLoc(); } 2839 SourceLocation getEndLoc() const { return getBody()->getEndLoc(); } 2840 2841 static bool classof(const Stmt *T) { 2842 return T->getStmtClass() == ForStmtClass; 2843 } 2844 2845 // Iterators 2846 child_range children() { 2847 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 2848 } 2849 2850 const_child_range children() const { 2851 return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR); 2852 } 2853 }; 2854 2855 /// GotoStmt - This represents a direct goto. 2856 class GotoStmt : public Stmt { 2857 LabelDecl *Label; 2858 SourceLocation LabelLoc; 2859 2860 public: 2861 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL) 2862 : Stmt(GotoStmtClass), Label(label), LabelLoc(LL) { 2863 setGotoLoc(GL); 2864 } 2865 2866 /// Build an empty goto statement. 2867 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {} 2868 2869 LabelDecl *getLabel() const { return Label; } 2870 void setLabel(LabelDecl *D) { Label = D; } 2871 2872 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; } 2873 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; } 2874 SourceLocation getLabelLoc() const { return LabelLoc; } 2875 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 2876 2877 SourceLocation getBeginLoc() const { return getGotoLoc(); } 2878 SourceLocation getEndLoc() const { return getLabelLoc(); } 2879 2880 static bool classof(const Stmt *T) { 2881 return T->getStmtClass() == GotoStmtClass; 2882 } 2883 2884 // Iterators 2885 child_range children() { 2886 return child_range(child_iterator(), child_iterator()); 2887 } 2888 2889 const_child_range children() const { 2890 return const_child_range(const_child_iterator(), const_child_iterator()); 2891 } 2892 }; 2893 2894 /// IndirectGotoStmt - This represents an indirect goto. 2895 class IndirectGotoStmt : public Stmt { 2896 SourceLocation StarLoc; 2897 Stmt *Target; 2898 2899 public: 2900 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, Expr *target) 2901 : Stmt(IndirectGotoStmtClass), StarLoc(starLoc) { 2902 setTarget(target); 2903 setGotoLoc(gotoLoc); 2904 } 2905 2906 /// Build an empty indirect goto statement. 2907 explicit IndirectGotoStmt(EmptyShell Empty) 2908 : Stmt(IndirectGotoStmtClass, Empty) {} 2909 2910 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; } 2911 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; } 2912 void setStarLoc(SourceLocation L) { StarLoc = L; } 2913 SourceLocation getStarLoc() const { return StarLoc; } 2914 2915 Expr *getTarget() { return reinterpret_cast<Expr *>(Target); } 2916 const Expr *getTarget() const { 2917 return reinterpret_cast<const Expr *>(Target); 2918 } 2919 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt *>(E); } 2920 2921 /// getConstantTarget - Returns the fixed target of this indirect 2922 /// goto, if one exists. 2923 LabelDecl *getConstantTarget(); 2924 const LabelDecl *getConstantTarget() const { 2925 return const_cast<IndirectGotoStmt *>(this)->getConstantTarget(); 2926 } 2927 2928 SourceLocation getBeginLoc() const { return getGotoLoc(); } 2929 SourceLocation getEndLoc() const LLVM_READONLY { return Target->getEndLoc(); } 2930 2931 static bool classof(const Stmt *T) { 2932 return T->getStmtClass() == IndirectGotoStmtClass; 2933 } 2934 2935 // Iterators 2936 child_range children() { return child_range(&Target, &Target + 1); } 2937 2938 const_child_range children() const { 2939 return const_child_range(&Target, &Target + 1); 2940 } 2941 }; 2942 2943 /// ContinueStmt - This represents a continue. 2944 class ContinueStmt : public Stmt { 2945 public: 2946 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) { 2947 setContinueLoc(CL); 2948 } 2949 2950 /// Build an empty continue statement. 2951 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {} 2952 2953 SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; } 2954 void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; } 2955 2956 SourceLocation getBeginLoc() const { return getContinueLoc(); } 2957 SourceLocation getEndLoc() const { return getContinueLoc(); } 2958 2959 static bool classof(const Stmt *T) { 2960 return T->getStmtClass() == ContinueStmtClass; 2961 } 2962 2963 // Iterators 2964 child_range children() { 2965 return child_range(child_iterator(), child_iterator()); 2966 } 2967 2968 const_child_range children() const { 2969 return const_child_range(const_child_iterator(), const_child_iterator()); 2970 } 2971 }; 2972 2973 /// BreakStmt - This represents a break. 2974 class BreakStmt : public Stmt { 2975 public: 2976 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass) { 2977 setBreakLoc(BL); 2978 } 2979 2980 /// Build an empty break statement. 2981 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {} 2982 2983 SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; } 2984 void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; } 2985 2986 SourceLocation getBeginLoc() const { return getBreakLoc(); } 2987 SourceLocation getEndLoc() const { return getBreakLoc(); } 2988 2989 static bool classof(const Stmt *T) { 2990 return T->getStmtClass() == BreakStmtClass; 2991 } 2992 2993 // Iterators 2994 child_range children() { 2995 return child_range(child_iterator(), child_iterator()); 2996 } 2997 2998 const_child_range children() const { 2999 return const_child_range(const_child_iterator(), const_child_iterator()); 3000 } 3001 }; 3002 3003 /// ReturnStmt - This represents a return, optionally of an expression: 3004 /// return; 3005 /// return 4; 3006 /// 3007 /// Note that GCC allows return with no argument in a function declared to 3008 /// return a value, and it allows returning a value in functions declared to 3009 /// return void. We explicitly model this in the AST, which means you can't 3010 /// depend on the return type of the function and the presence of an argument. 3011 class ReturnStmt final 3012 : public Stmt, 3013 private llvm::TrailingObjects<ReturnStmt, const VarDecl *> { 3014 friend TrailingObjects; 3015 3016 /// The return expression. 3017 Stmt *RetExpr; 3018 3019 // ReturnStmt is followed optionally by a trailing "const VarDecl *" 3020 // for the NRVO candidate. Present if and only if hasNRVOCandidate(). 3021 3022 /// True if this ReturnStmt has storage for an NRVO candidate. 3023 bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; } 3024 3025 unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const { 3026 return hasNRVOCandidate(); 3027 } 3028 3029 /// Build a return statement. 3030 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate); 3031 3032 /// Build an empty return statement. 3033 explicit ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate); 3034 3035 public: 3036 /// Create a return statement. 3037 static ReturnStmt *Create(const ASTContext &Ctx, SourceLocation RL, Expr *E, 3038 const VarDecl *NRVOCandidate); 3039 3040 /// Create an empty return statement, optionally with 3041 /// storage for an NRVO candidate. 3042 static ReturnStmt *CreateEmpty(const ASTContext &Ctx, bool HasNRVOCandidate); 3043 3044 Expr *getRetValue() { return reinterpret_cast<Expr *>(RetExpr); } 3045 const Expr *getRetValue() const { return reinterpret_cast<Expr *>(RetExpr); } 3046 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt *>(E); } 3047 3048 /// Retrieve the variable that might be used for the named return 3049 /// value optimization. 3050 /// 3051 /// The optimization itself can only be performed if the variable is 3052 /// also marked as an NRVO object. 3053 const VarDecl *getNRVOCandidate() const { 3054 return hasNRVOCandidate() ? *getTrailingObjects<const VarDecl *>() 3055 : nullptr; 3056 } 3057 3058 /// Set the variable that might be used for the named return value 3059 /// optimization. The return statement must have storage for it, 3060 /// which is the case if and only if hasNRVOCandidate() is true. 3061 void setNRVOCandidate(const VarDecl *Var) { 3062 assert(hasNRVOCandidate() && 3063 "This return statement has no storage for an NRVO candidate!"); 3064 *getTrailingObjects<const VarDecl *>() = Var; 3065 } 3066 3067 SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; } 3068 void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; } 3069 3070 SourceLocation getBeginLoc() const { return getReturnLoc(); } 3071 SourceLocation getEndLoc() const LLVM_READONLY { 3072 return RetExpr ? RetExpr->getEndLoc() : getReturnLoc(); 3073 } 3074 3075 static bool classof(const Stmt *T) { 3076 return T->getStmtClass() == ReturnStmtClass; 3077 } 3078 3079 // Iterators 3080 child_range children() { 3081 if (RetExpr) 3082 return child_range(&RetExpr, &RetExpr + 1); 3083 return child_range(child_iterator(), child_iterator()); 3084 } 3085 3086 const_child_range children() const { 3087 if (RetExpr) 3088 return const_child_range(&RetExpr, &RetExpr + 1); 3089 return const_child_range(const_child_iterator(), const_child_iterator()); 3090 } 3091 }; 3092 3093 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt. 3094 class AsmStmt : public Stmt { 3095 protected: 3096 friend class ASTStmtReader; 3097 3098 SourceLocation AsmLoc; 3099 3100 /// True if the assembly statement does not have any input or output 3101 /// operands. 3102 bool IsSimple; 3103 3104 /// If true, treat this inline assembly as having side effects. 3105 /// This assembly statement should not be optimized, deleted or moved. 3106 bool IsVolatile; 3107 3108 unsigned NumOutputs; 3109 unsigned NumInputs; 3110 unsigned NumClobbers; 3111 3112 Stmt **Exprs = nullptr; 3113 3114 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile, 3115 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) 3116 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile), 3117 NumOutputs(numoutputs), NumInputs(numinputs), 3118 NumClobbers(numclobbers) {} 3119 3120 public: 3121 /// Build an empty inline-assembly statement. 3122 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {} 3123 3124 SourceLocation getAsmLoc() const { return AsmLoc; } 3125 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 3126 3127 bool isSimple() const { return IsSimple; } 3128 void setSimple(bool V) { IsSimple = V; } 3129 3130 bool isVolatile() const { return IsVolatile; } 3131 void setVolatile(bool V) { IsVolatile = V; } 3132 3133 SourceLocation getBeginLoc() const LLVM_READONLY { return {}; } 3134 SourceLocation getEndLoc() const LLVM_READONLY { return {}; } 3135 3136 //===--- Asm String Analysis ---===// 3137 3138 /// Assemble final IR asm string. 3139 std::string generateAsmString(const ASTContext &C) const; 3140 3141 //===--- Output operands ---===// 3142 3143 unsigned getNumOutputs() const { return NumOutputs; } 3144 3145 /// getOutputConstraint - Return the constraint string for the specified 3146 /// output operand. All output constraints are known to be non-empty (either 3147 /// '=' or '+'). 3148 StringRef getOutputConstraint(unsigned i) const; 3149 3150 /// isOutputPlusConstraint - Return true if the specified output constraint 3151 /// is a "+" constraint (which is both an input and an output) or false if it 3152 /// is an "=" constraint (just an output). 3153 bool isOutputPlusConstraint(unsigned i) const { 3154 return getOutputConstraint(i)[0] == '+'; 3155 } 3156 3157 const Expr *getOutputExpr(unsigned i) const; 3158 3159 /// getNumPlusOperands - Return the number of output operands that have a "+" 3160 /// constraint. 3161 unsigned getNumPlusOperands() const; 3162 3163 //===--- Input operands ---===// 3164 3165 unsigned getNumInputs() const { return NumInputs; } 3166 3167 /// getInputConstraint - Return the specified input constraint. Unlike output 3168 /// constraints, these can be empty. 3169 StringRef getInputConstraint(unsigned i) const; 3170 3171 const Expr *getInputExpr(unsigned i) const; 3172 3173 //===--- Other ---===// 3174 3175 unsigned getNumClobbers() const { return NumClobbers; } 3176 StringRef getClobber(unsigned i) const; 3177 3178 static bool classof(const Stmt *T) { 3179 return T->getStmtClass() == GCCAsmStmtClass || 3180 T->getStmtClass() == MSAsmStmtClass; 3181 } 3182 3183 // Input expr iterators. 3184 3185 using inputs_iterator = ExprIterator; 3186 using const_inputs_iterator = ConstExprIterator; 3187 using inputs_range = llvm::iterator_range<inputs_iterator>; 3188 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>; 3189 3190 inputs_iterator begin_inputs() { 3191 return &Exprs[0] + NumOutputs; 3192 } 3193 3194 inputs_iterator end_inputs() { 3195 return &Exprs[0] + NumOutputs + NumInputs; 3196 } 3197 3198 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); } 3199 3200 const_inputs_iterator begin_inputs() const { 3201 return &Exprs[0] + NumOutputs; 3202 } 3203 3204 const_inputs_iterator end_inputs() const { 3205 return &Exprs[0] + NumOutputs + NumInputs; 3206 } 3207 3208 inputs_const_range inputs() const { 3209 return inputs_const_range(begin_inputs(), end_inputs()); 3210 } 3211 3212 // Output expr iterators. 3213 3214 using outputs_iterator = ExprIterator; 3215 using const_outputs_iterator = ConstExprIterator; 3216 using outputs_range = llvm::iterator_range<outputs_iterator>; 3217 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>; 3218 3219 outputs_iterator begin_outputs() { 3220 return &Exprs[0]; 3221 } 3222 3223 outputs_iterator end_outputs() { 3224 return &Exprs[0] + NumOutputs; 3225 } 3226 3227 outputs_range outputs() { 3228 return outputs_range(begin_outputs(), end_outputs()); 3229 } 3230 3231 const_outputs_iterator begin_outputs() const { 3232 return &Exprs[0]; 3233 } 3234 3235 const_outputs_iterator end_outputs() const { 3236 return &Exprs[0] + NumOutputs; 3237 } 3238 3239 outputs_const_range outputs() const { 3240 return outputs_const_range(begin_outputs(), end_outputs()); 3241 } 3242 3243 child_range children() { 3244 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs); 3245 } 3246 3247 const_child_range children() const { 3248 return const_child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs); 3249 } 3250 }; 3251 3252 /// This represents a GCC inline-assembly statement extension. 3253 class GCCAsmStmt : public AsmStmt { 3254 friend class ASTStmtReader; 3255 3256 SourceLocation RParenLoc; 3257 StringLiteral *AsmStr; 3258 3259 // FIXME: If we wanted to, we could allocate all of these in one big array. 3260 StringLiteral **Constraints = nullptr; 3261 StringLiteral **Clobbers = nullptr; 3262 IdentifierInfo **Names = nullptr; 3263 unsigned NumLabels = 0; 3264 3265 public: 3266 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple, 3267 bool isvolatile, unsigned numoutputs, unsigned numinputs, 3268 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs, 3269 StringLiteral *asmstr, unsigned numclobbers, 3270 StringLiteral **clobbers, unsigned numlabels, 3271 SourceLocation rparenloc); 3272 3273 /// Build an empty inline-assembly statement. 3274 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {} 3275 3276 SourceLocation getRParenLoc() const { return RParenLoc; } 3277 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3278 3279 //===--- Asm String Analysis ---===// 3280 3281 const StringLiteral *getAsmString() const { return AsmStr; } 3282 StringLiteral *getAsmString() { return AsmStr; } 3283 void setAsmString(StringLiteral *E) { AsmStr = E; } 3284 3285 /// AsmStringPiece - this is part of a decomposed asm string specification 3286 /// (for use with the AnalyzeAsmString function below). An asm string is 3287 /// considered to be a concatenation of these parts. 3288 class AsmStringPiece { 3289 public: 3290 enum Kind { 3291 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 3292 Operand // Operand reference, with optional modifier %c4. 3293 }; 3294 3295 private: 3296 Kind MyKind; 3297 std::string Str; 3298 unsigned OperandNo; 3299 3300 // Source range for operand references. 3301 CharSourceRange Range; 3302 3303 public: 3304 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 3305 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin, 3306 SourceLocation End) 3307 : MyKind(Operand), Str(S), OperandNo(OpNo), 3308 Range(CharSourceRange::getCharRange(Begin, End)) {} 3309 3310 bool isString() const { return MyKind == String; } 3311 bool isOperand() const { return MyKind == Operand; } 3312 3313 const std::string &getString() const { return Str; } 3314 3315 unsigned getOperandNo() const { 3316 assert(isOperand()); 3317 return OperandNo; 3318 } 3319 3320 CharSourceRange getRange() const { 3321 assert(isOperand() && "Range is currently used only for Operands."); 3322 return Range; 3323 } 3324 3325 /// getModifier - Get the modifier for this operand, if present. This 3326 /// returns '\0' if there was no modifier. 3327 char getModifier() const; 3328 }; 3329 3330 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 3331 /// it into pieces. If the asm string is erroneous, emit errors and return 3332 /// true, otherwise return false. This handles canonicalization and 3333 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 3334 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 3335 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces, 3336 const ASTContext &C, unsigned &DiagOffs) const; 3337 3338 /// Assemble final IR asm string. 3339 std::string generateAsmString(const ASTContext &C) const; 3340 3341 //===--- Output operands ---===// 3342 3343 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; } 3344 3345 StringRef getOutputName(unsigned i) const { 3346 if (IdentifierInfo *II = getOutputIdentifier(i)) 3347 return II->getName(); 3348 3349 return {}; 3350 } 3351 3352 StringRef getOutputConstraint(unsigned i) const; 3353 3354 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 3355 return Constraints[i]; 3356 } 3357 StringLiteral *getOutputConstraintLiteral(unsigned i) { 3358 return Constraints[i]; 3359 } 3360 3361 Expr *getOutputExpr(unsigned i); 3362 3363 const Expr *getOutputExpr(unsigned i) const { 3364 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i); 3365 } 3366 3367 //===--- Input operands ---===// 3368 3369 IdentifierInfo *getInputIdentifier(unsigned i) const { 3370 return Names[i + NumOutputs]; 3371 } 3372 3373 StringRef getInputName(unsigned i) const { 3374 if (IdentifierInfo *II = getInputIdentifier(i)) 3375 return II->getName(); 3376 3377 return {}; 3378 } 3379 3380 StringRef getInputConstraint(unsigned i) const; 3381 3382 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 3383 return Constraints[i + NumOutputs]; 3384 } 3385 StringLiteral *getInputConstraintLiteral(unsigned i) { 3386 return Constraints[i + NumOutputs]; 3387 } 3388 3389 Expr *getInputExpr(unsigned i); 3390 void setInputExpr(unsigned i, Expr *E); 3391 3392 const Expr *getInputExpr(unsigned i) const { 3393 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i); 3394 } 3395 3396 //===--- Labels ---===// 3397 3398 bool isAsmGoto() const { 3399 return NumLabels > 0; 3400 } 3401 3402 unsigned getNumLabels() const { 3403 return NumLabels; 3404 } 3405 3406 IdentifierInfo *getLabelIdentifier(unsigned i) const { 3407 return Names[i + NumOutputs + NumInputs]; 3408 } 3409 3410 AddrLabelExpr *getLabelExpr(unsigned i) const; 3411 StringRef getLabelName(unsigned i) const; 3412 using labels_iterator = CastIterator<AddrLabelExpr>; 3413 using const_labels_iterator = ConstCastIterator<AddrLabelExpr>; 3414 using labels_range = llvm::iterator_range<labels_iterator>; 3415 using labels_const_range = llvm::iterator_range<const_labels_iterator>; 3416 3417 labels_iterator begin_labels() { 3418 return &Exprs[0] + NumOutputs + NumInputs; 3419 } 3420 3421 labels_iterator end_labels() { 3422 return &Exprs[0] + NumOutputs + NumInputs + NumLabels; 3423 } 3424 3425 labels_range labels() { 3426 return labels_range(begin_labels(), end_labels()); 3427 } 3428 3429 const_labels_iterator begin_labels() const { 3430 return &Exprs[0] + NumOutputs + NumInputs; 3431 } 3432 3433 const_labels_iterator end_labels() const { 3434 return &Exprs[0] + NumOutputs + NumInputs + NumLabels; 3435 } 3436 3437 labels_const_range labels() const { 3438 return labels_const_range(begin_labels(), end_labels()); 3439 } 3440 3441 private: 3442 void setOutputsAndInputsAndClobbers(const ASTContext &C, 3443 IdentifierInfo **Names, 3444 StringLiteral **Constraints, 3445 Stmt **Exprs, 3446 unsigned NumOutputs, 3447 unsigned NumInputs, 3448 unsigned NumLabels, 3449 StringLiteral **Clobbers, 3450 unsigned NumClobbers); 3451 3452 public: 3453 //===--- Other ---===// 3454 3455 /// getNamedOperand - Given a symbolic operand reference like %[foo], 3456 /// translate this into a numeric value needed to reference the same operand. 3457 /// This returns -1 if the operand name is invalid. 3458 int getNamedOperand(StringRef SymbolicName) const; 3459 3460 StringRef getClobber(unsigned i) const; 3461 3462 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; } 3463 const StringLiteral *getClobberStringLiteral(unsigned i) const { 3464 return Clobbers[i]; 3465 } 3466 3467 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; } 3468 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 3469 3470 static bool classof(const Stmt *T) { 3471 return T->getStmtClass() == GCCAsmStmtClass; 3472 } 3473 }; 3474 3475 /// This represents a Microsoft inline-assembly statement extension. 3476 class MSAsmStmt : public AsmStmt { 3477 friend class ASTStmtReader; 3478 3479 SourceLocation LBraceLoc, EndLoc; 3480 StringRef AsmStr; 3481 3482 unsigned NumAsmToks = 0; 3483 3484 Token *AsmToks = nullptr; 3485 StringRef *Constraints = nullptr; 3486 StringRef *Clobbers = nullptr; 3487 3488 public: 3489 MSAsmStmt(const ASTContext &C, SourceLocation asmloc, 3490 SourceLocation lbraceloc, bool issimple, bool isvolatile, 3491 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs, 3492 ArrayRef<StringRef> constraints, 3493 ArrayRef<Expr*> exprs, StringRef asmstr, 3494 ArrayRef<StringRef> clobbers, SourceLocation endloc); 3495 3496 /// Build an empty MS-style inline-assembly statement. 3497 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {} 3498 3499 SourceLocation getLBraceLoc() const { return LBraceLoc; } 3500 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; } 3501 SourceLocation getEndLoc() const { return EndLoc; } 3502 void setEndLoc(SourceLocation L) { EndLoc = L; } 3503 3504 bool hasBraces() const { return LBraceLoc.isValid(); } 3505 3506 unsigned getNumAsmToks() { return NumAsmToks; } 3507 Token *getAsmToks() { return AsmToks; } 3508 3509 //===--- Asm String Analysis ---===// 3510 StringRef getAsmString() const { return AsmStr; } 3511 3512 /// Assemble final IR asm string. 3513 std::string generateAsmString(const ASTContext &C) const; 3514 3515 //===--- Output operands ---===// 3516 3517 StringRef getOutputConstraint(unsigned i) const { 3518 assert(i < NumOutputs); 3519 return Constraints[i]; 3520 } 3521 3522 Expr *getOutputExpr(unsigned i); 3523 3524 const Expr *getOutputExpr(unsigned i) const { 3525 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i); 3526 } 3527 3528 //===--- Input operands ---===// 3529 3530 StringRef getInputConstraint(unsigned i) const { 3531 assert(i < NumInputs); 3532 return Constraints[i + NumOutputs]; 3533 } 3534 3535 Expr *getInputExpr(unsigned i); 3536 void setInputExpr(unsigned i, Expr *E); 3537 3538 const Expr *getInputExpr(unsigned i) const { 3539 return const_cast<MSAsmStmt*>(this)->getInputExpr(i); 3540 } 3541 3542 //===--- Other ---===// 3543 3544 ArrayRef<StringRef> getAllConstraints() const { 3545 return llvm::ArrayRef(Constraints, NumInputs + NumOutputs); 3546 } 3547 3548 ArrayRef<StringRef> getClobbers() const { 3549 return llvm::ArrayRef(Clobbers, NumClobbers); 3550 } 3551 3552 ArrayRef<Expr*> getAllExprs() const { 3553 return llvm::ArrayRef(reinterpret_cast<Expr **>(Exprs), 3554 NumInputs + NumOutputs); 3555 } 3556 3557 StringRef getClobber(unsigned i) const { return getClobbers()[i]; } 3558 3559 private: 3560 void initialize(const ASTContext &C, StringRef AsmString, 3561 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints, 3562 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers); 3563 3564 public: 3565 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; } 3566 3567 static bool classof(const Stmt *T) { 3568 return T->getStmtClass() == MSAsmStmtClass; 3569 } 3570 3571 child_range children() { 3572 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]); 3573 } 3574 3575 const_child_range children() const { 3576 return const_child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]); 3577 } 3578 }; 3579 3580 class SEHExceptStmt : public Stmt { 3581 friend class ASTReader; 3582 friend class ASTStmtReader; 3583 3584 SourceLocation Loc; 3585 Stmt *Children[2]; 3586 3587 enum { FILTER_EXPR, BLOCK }; 3588 3589 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block); 3590 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {} 3591 3592 public: 3593 static SEHExceptStmt* Create(const ASTContext &C, 3594 SourceLocation ExceptLoc, 3595 Expr *FilterExpr, 3596 Stmt *Block); 3597 3598 SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); } 3599 3600 SourceLocation getExceptLoc() const { return Loc; } 3601 SourceLocation getEndLoc() const { return getBlock()->getEndLoc(); } 3602 3603 Expr *getFilterExpr() const { 3604 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]); 3605 } 3606 3607 CompoundStmt *getBlock() const { 3608 return cast<CompoundStmt>(Children[BLOCK]); 3609 } 3610 3611 child_range children() { 3612 return child_range(Children, Children+2); 3613 } 3614 3615 const_child_range children() const { 3616 return const_child_range(Children, Children + 2); 3617 } 3618 3619 static bool classof(const Stmt *T) { 3620 return T->getStmtClass() == SEHExceptStmtClass; 3621 } 3622 }; 3623 3624 class SEHFinallyStmt : public Stmt { 3625 friend class ASTReader; 3626 friend class ASTStmtReader; 3627 3628 SourceLocation Loc; 3629 Stmt *Block; 3630 3631 SEHFinallyStmt(SourceLocation Loc, Stmt *Block); 3632 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {} 3633 3634 public: 3635 static SEHFinallyStmt* Create(const ASTContext &C, 3636 SourceLocation FinallyLoc, 3637 Stmt *Block); 3638 3639 SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); } 3640 3641 SourceLocation getFinallyLoc() const { return Loc; } 3642 SourceLocation getEndLoc() const { return Block->getEndLoc(); } 3643 3644 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); } 3645 3646 child_range children() { 3647 return child_range(&Block,&Block+1); 3648 } 3649 3650 const_child_range children() const { 3651 return const_child_range(&Block, &Block + 1); 3652 } 3653 3654 static bool classof(const Stmt *T) { 3655 return T->getStmtClass() == SEHFinallyStmtClass; 3656 } 3657 }; 3658 3659 class SEHTryStmt : public Stmt { 3660 friend class ASTReader; 3661 friend class ASTStmtReader; 3662 3663 bool IsCXXTry; 3664 SourceLocation TryLoc; 3665 Stmt *Children[2]; 3666 3667 enum { TRY = 0, HANDLER = 1 }; 3668 3669 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try' 3670 SourceLocation TryLoc, 3671 Stmt *TryBlock, 3672 Stmt *Handler); 3673 3674 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {} 3675 3676 public: 3677 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry, 3678 SourceLocation TryLoc, Stmt *TryBlock, 3679 Stmt *Handler); 3680 3681 SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); } 3682 3683 SourceLocation getTryLoc() const { return TryLoc; } 3684 SourceLocation getEndLoc() const { return Children[HANDLER]->getEndLoc(); } 3685 3686 bool getIsCXXTry() const { return IsCXXTry; } 3687 3688 CompoundStmt* getTryBlock() const { 3689 return cast<CompoundStmt>(Children[TRY]); 3690 } 3691 3692 Stmt *getHandler() const { return Children[HANDLER]; } 3693 3694 /// Returns 0 if not defined 3695 SEHExceptStmt *getExceptHandler() const; 3696 SEHFinallyStmt *getFinallyHandler() const; 3697 3698 child_range children() { 3699 return child_range(Children, Children+2); 3700 } 3701 3702 const_child_range children() const { 3703 return const_child_range(Children, Children + 2); 3704 } 3705 3706 static bool classof(const Stmt *T) { 3707 return T->getStmtClass() == SEHTryStmtClass; 3708 } 3709 }; 3710 3711 /// Represents a __leave statement. 3712 class SEHLeaveStmt : public Stmt { 3713 SourceLocation LeaveLoc; 3714 3715 public: 3716 explicit SEHLeaveStmt(SourceLocation LL) 3717 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {} 3718 3719 /// Build an empty __leave statement. 3720 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {} 3721 3722 SourceLocation getLeaveLoc() const { return LeaveLoc; } 3723 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; } 3724 3725 SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; } 3726 SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; } 3727 3728 static bool classof(const Stmt *T) { 3729 return T->getStmtClass() == SEHLeaveStmtClass; 3730 } 3731 3732 // Iterators 3733 child_range children() { 3734 return child_range(child_iterator(), child_iterator()); 3735 } 3736 3737 const_child_range children() const { 3738 return const_child_range(const_child_iterator(), const_child_iterator()); 3739 } 3740 }; 3741 3742 /// This captures a statement into a function. For example, the following 3743 /// pragma annotated compound statement can be represented as a CapturedStmt, 3744 /// and this compound statement is the body of an anonymous outlined function. 3745 /// @code 3746 /// #pragma omp parallel 3747 /// { 3748 /// compute(); 3749 /// } 3750 /// @endcode 3751 class CapturedStmt : public Stmt { 3752 public: 3753 /// The different capture forms: by 'this', by reference, capture for 3754 /// variable-length array type etc. 3755 enum VariableCaptureKind { 3756 VCK_This, 3757 VCK_ByRef, 3758 VCK_ByCopy, 3759 VCK_VLAType, 3760 }; 3761 3762 /// Describes the capture of either a variable, or 'this', or 3763 /// variable-length array type. 3764 class Capture { 3765 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind; 3766 SourceLocation Loc; 3767 3768 Capture() = default; 3769 3770 public: 3771 friend class ASTStmtReader; 3772 friend class CapturedStmt; 3773 3774 /// Create a new capture. 3775 /// 3776 /// \param Loc The source location associated with this capture. 3777 /// 3778 /// \param Kind The kind of capture (this, ByRef, ...). 3779 /// 3780 /// \param Var The variable being captured, or null if capturing this. 3781 Capture(SourceLocation Loc, VariableCaptureKind Kind, 3782 VarDecl *Var = nullptr); 3783 3784 /// Determine the kind of capture. 3785 VariableCaptureKind getCaptureKind() const; 3786 3787 /// Retrieve the source location at which the variable or 'this' was 3788 /// first used. 3789 SourceLocation getLocation() const { return Loc; } 3790 3791 /// Determine whether this capture handles the C++ 'this' pointer. 3792 bool capturesThis() const { return getCaptureKind() == VCK_This; } 3793 3794 /// Determine whether this capture handles a variable (by reference). 3795 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; } 3796 3797 /// Determine whether this capture handles a variable by copy. 3798 bool capturesVariableByCopy() const { 3799 return getCaptureKind() == VCK_ByCopy; 3800 } 3801 3802 /// Determine whether this capture handles a variable-length array 3803 /// type. 3804 bool capturesVariableArrayType() const { 3805 return getCaptureKind() == VCK_VLAType; 3806 } 3807 3808 /// Retrieve the declaration of the variable being captured. 3809 /// 3810 /// This operation is only valid if this capture captures a variable. 3811 VarDecl *getCapturedVar() const; 3812 }; 3813 3814 private: 3815 /// The number of variable captured, including 'this'. 3816 unsigned NumCaptures; 3817 3818 /// The pointer part is the implicit the outlined function and the 3819 /// int part is the captured region kind, 'CR_Default' etc. 3820 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind; 3821 3822 /// The record for captured variables, a RecordDecl or CXXRecordDecl. 3823 RecordDecl *TheRecordDecl = nullptr; 3824 3825 /// Construct a captured statement. 3826 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures, 3827 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD); 3828 3829 /// Construct an empty captured statement. 3830 CapturedStmt(EmptyShell Empty, unsigned NumCaptures); 3831 3832 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); } 3833 3834 Stmt *const *getStoredStmts() const { 3835 return reinterpret_cast<Stmt *const *>(this + 1); 3836 } 3837 3838 Capture *getStoredCaptures() const; 3839 3840 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; } 3841 3842 public: 3843 friend class ASTStmtReader; 3844 3845 static CapturedStmt *Create(const ASTContext &Context, Stmt *S, 3846 CapturedRegionKind Kind, 3847 ArrayRef<Capture> Captures, 3848 ArrayRef<Expr *> CaptureInits, 3849 CapturedDecl *CD, RecordDecl *RD); 3850 3851 static CapturedStmt *CreateDeserialized(const ASTContext &Context, 3852 unsigned NumCaptures); 3853 3854 /// Retrieve the statement being captured. 3855 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; } 3856 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; } 3857 3858 /// Retrieve the outlined function declaration. 3859 CapturedDecl *getCapturedDecl(); 3860 const CapturedDecl *getCapturedDecl() const; 3861 3862 /// Set the outlined function declaration. 3863 void setCapturedDecl(CapturedDecl *D); 3864 3865 /// Retrieve the captured region kind. 3866 CapturedRegionKind getCapturedRegionKind() const; 3867 3868 /// Set the captured region kind. 3869 void setCapturedRegionKind(CapturedRegionKind Kind); 3870 3871 /// Retrieve the record declaration for captured variables. 3872 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; } 3873 3874 /// Set the record declaration for captured variables. 3875 void setCapturedRecordDecl(RecordDecl *D) { 3876 assert(D && "null RecordDecl"); 3877 TheRecordDecl = D; 3878 } 3879 3880 /// True if this variable has been captured. 3881 bool capturesVariable(const VarDecl *Var) const; 3882 3883 /// An iterator that walks over the captures. 3884 using capture_iterator = Capture *; 3885 using const_capture_iterator = const Capture *; 3886 using capture_range = llvm::iterator_range<capture_iterator>; 3887 using capture_const_range = llvm::iterator_range<const_capture_iterator>; 3888 3889 capture_range captures() { 3890 return capture_range(capture_begin(), capture_end()); 3891 } 3892 capture_const_range captures() const { 3893 return capture_const_range(capture_begin(), capture_end()); 3894 } 3895 3896 /// Retrieve an iterator pointing to the first capture. 3897 capture_iterator capture_begin() { return getStoredCaptures(); } 3898 const_capture_iterator capture_begin() const { return getStoredCaptures(); } 3899 3900 /// Retrieve an iterator pointing past the end of the sequence of 3901 /// captures. 3902 capture_iterator capture_end() const { 3903 return getStoredCaptures() + NumCaptures; 3904 } 3905 3906 /// Retrieve the number of captures, including 'this'. 3907 unsigned capture_size() const { return NumCaptures; } 3908 3909 /// Iterator that walks over the capture initialization arguments. 3910 using capture_init_iterator = Expr **; 3911 using capture_init_range = llvm::iterator_range<capture_init_iterator>; 3912 3913 /// Const iterator that walks over the capture initialization 3914 /// arguments. 3915 using const_capture_init_iterator = Expr *const *; 3916 using const_capture_init_range = 3917 llvm::iterator_range<const_capture_init_iterator>; 3918 3919 capture_init_range capture_inits() { 3920 return capture_init_range(capture_init_begin(), capture_init_end()); 3921 } 3922 3923 const_capture_init_range capture_inits() const { 3924 return const_capture_init_range(capture_init_begin(), capture_init_end()); 3925 } 3926 3927 /// Retrieve the first initialization argument. 3928 capture_init_iterator capture_init_begin() { 3929 return reinterpret_cast<Expr **>(getStoredStmts()); 3930 } 3931 3932 const_capture_init_iterator capture_init_begin() const { 3933 return reinterpret_cast<Expr *const *>(getStoredStmts()); 3934 } 3935 3936 /// Retrieve the iterator pointing one past the last initialization 3937 /// argument. 3938 capture_init_iterator capture_init_end() { 3939 return capture_init_begin() + NumCaptures; 3940 } 3941 3942 const_capture_init_iterator capture_init_end() const { 3943 return capture_init_begin() + NumCaptures; 3944 } 3945 3946 SourceLocation getBeginLoc() const LLVM_READONLY { 3947 return getCapturedStmt()->getBeginLoc(); 3948 } 3949 3950 SourceLocation getEndLoc() const LLVM_READONLY { 3951 return getCapturedStmt()->getEndLoc(); 3952 } 3953 3954 SourceRange getSourceRange() const LLVM_READONLY { 3955 return getCapturedStmt()->getSourceRange(); 3956 } 3957 3958 static bool classof(const Stmt *T) { 3959 return T->getStmtClass() == CapturedStmtClass; 3960 } 3961 3962 child_range children(); 3963 3964 const_child_range children() const; 3965 }; 3966 3967 } // namespace clang 3968 3969 #endif // LLVM_CLANG_AST_STMT_H 3970