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