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