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