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