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