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