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