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