1 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the Stmt interface and subclasses. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_AST_STMT_H 15 #define LLVM_CLANG_AST_STMT_H 16 17 #include "clang/AST/DeclGroup.h" 18 #include "clang/AST/StmtIterator.h" 19 #include "clang/Basic/CapturedStmt.h" 20 #include "clang/Basic/IdentifierTable.h" 21 #include "clang/Basic/LLVM.h" 22 #include "clang/Basic/SourceLocation.h" 23 #include "llvm/ADT/ArrayRef.h" 24 #include "llvm/ADT/PointerIntPair.h" 25 #include "llvm/ADT/iterator.h" 26 #include "llvm/Support/Compiler.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include <string> 29 30 namespace llvm { 31 class FoldingSetNodeID; 32 } 33 34 namespace clang { 35 class ASTContext; 36 class Attr; 37 class CapturedDecl; 38 class Decl; 39 class Expr; 40 class IdentifierInfo; 41 class LabelDecl; 42 class ParmVarDecl; 43 class PrinterHelper; 44 struct PrintingPolicy; 45 class QualType; 46 class RecordDecl; 47 class SourceManager; 48 class StringLiteral; 49 class SwitchStmt; 50 class Token; 51 class VarDecl; 52 53 //===----------------------------------------------------------------------===// 54 // AST classes for statements. 55 //===----------------------------------------------------------------------===// 56 57 /// Stmt - This represents one statement. 58 /// 59 class LLVM_ALIGNAS(LLVM_PTR_SIZE) Stmt { 60 public: 61 enum StmtClass { 62 NoStmtClass = 0, 63 #define STMT(CLASS, PARENT) CLASS##Class, 64 #define STMT_RANGE(BASE, FIRST, LAST) \ 65 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class, 66 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \ 67 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class 68 #define ABSTRACT_STMT(STMT) 69 #include "clang/AST/StmtNodes.inc" 70 }; 71 72 // Make vanilla 'new' and 'delete' illegal for Stmts. 73 protected: 74 void *operator new(size_t bytes) LLVM_NOEXCEPT { 75 llvm_unreachable("Stmts cannot be allocated with regular 'new'."); 76 } 77 void operator delete(void *data) LLVM_NOEXCEPT { 78 llvm_unreachable("Stmts cannot be released with regular 'delete'."); 79 } 80 81 class StmtBitfields { 82 friend class Stmt; 83 84 /// \brief The statement class. 85 unsigned sClass : 8; 86 }; 87 enum { NumStmtBits = 8 }; 88 89 class CompoundStmtBitfields { 90 friend class CompoundStmt; 91 unsigned : NumStmtBits; 92 93 unsigned NumStmts : 32 - NumStmtBits; 94 }; 95 96 class IfStmtBitfields { 97 friend class IfStmt; 98 unsigned : NumStmtBits; 99 100 unsigned IsConstexpr : 1; 101 }; 102 103 class ExprBitfields { 104 friend class Expr; 105 friend class DeclRefExpr; // computeDependence 106 friend class InitListExpr; // ctor 107 friend class DesignatedInitExpr; // ctor 108 friend class BlockDeclRefExpr; // ctor 109 friend class ASTStmtReader; // deserialization 110 friend class CXXNewExpr; // ctor 111 friend class DependentScopeDeclRefExpr; // ctor 112 friend class CXXConstructExpr; // ctor 113 friend class CallExpr; // ctor 114 friend class OffsetOfExpr; // ctor 115 friend class ObjCMessageExpr; // ctor 116 friend class ObjCArrayLiteral; // ctor 117 friend class ObjCDictionaryLiteral; // ctor 118 friend class ShuffleVectorExpr; // ctor 119 friend class ParenListExpr; // ctor 120 friend class CXXUnresolvedConstructExpr; // ctor 121 friend class CXXDependentScopeMemberExpr; // ctor 122 friend class OverloadExpr; // ctor 123 friend class PseudoObjectExpr; // ctor 124 friend class AtomicExpr; // ctor 125 friend class OpaqueValueExpr; // ctor 126 unsigned : NumStmtBits; 127 128 unsigned ValueKind : 2; 129 unsigned ObjectKind : 2; 130 unsigned TypeDependent : 1; 131 unsigned ValueDependent : 1; 132 unsigned InstantiationDependent : 1; 133 unsigned ContainsUnexpandedParameterPack : 1; 134 }; 135 enum { NumExprBits = 16 }; 136 137 class CharacterLiteralBitfields { 138 friend class CharacterLiteral; 139 unsigned : NumExprBits; 140 141 unsigned Kind : 3; 142 }; 143 144 enum APFloatSemantics { 145 IEEEhalf, 146 IEEEsingle, 147 IEEEdouble, 148 x87DoubleExtended, 149 IEEEquad, 150 PPCDoubleDouble 151 }; 152 153 class FloatingLiteralBitfields { 154 friend class FloatingLiteral; 155 unsigned : NumExprBits; 156 157 unsigned Semantics : 3; // Provides semantics for APFloat construction 158 unsigned IsExact : 1; 159 }; 160 161 class UnaryExprOrTypeTraitExprBitfields { 162 friend class UnaryExprOrTypeTraitExpr; 163 unsigned : NumExprBits; 164 165 unsigned Kind : 2; 166 unsigned IsType : 1; // true if operand is a type, false if an expression. 167 }; 168 169 class DeclRefExprBitfields { 170 friend class DeclRefExpr; 171 friend class ASTStmtReader; // deserialization 172 unsigned : NumExprBits; 173 174 unsigned HasQualifier : 1; 175 unsigned HasTemplateKWAndArgsInfo : 1; 176 unsigned HasFoundDecl : 1; 177 unsigned HadMultipleCandidates : 1; 178 unsigned RefersToEnclosingVariableOrCapture : 1; 179 }; 180 181 class CastExprBitfields { 182 friend class CastExpr; 183 unsigned : NumExprBits; 184 185 unsigned Kind : 6; 186 unsigned BasePathSize : 32 - 6 - NumExprBits; 187 }; 188 189 class CallExprBitfields { 190 friend class CallExpr; 191 unsigned : NumExprBits; 192 193 unsigned NumPreArgs : 1; 194 }; 195 196 class ExprWithCleanupsBitfields { 197 friend class ExprWithCleanups; 198 friend class ASTStmtReader; // deserialization 199 200 unsigned : NumExprBits; 201 202 // When false, it must not have side effects. 203 unsigned CleanupsHaveSideEffects : 1; 204 205 unsigned NumObjects : 32 - 1 - NumExprBits; 206 }; 207 208 class PseudoObjectExprBitfields { 209 friend class PseudoObjectExpr; 210 friend class ASTStmtReader; // deserialization 211 212 unsigned : NumExprBits; 213 214 // These don't need to be particularly wide, because they're 215 // strictly limited by the forms of expressions we permit. 216 unsigned NumSubExprs : 8; 217 unsigned ResultIndex : 32 - 8 - NumExprBits; 218 }; 219 220 class ObjCIndirectCopyRestoreExprBitfields { 221 friend class ObjCIndirectCopyRestoreExpr; 222 unsigned : NumExprBits; 223 224 unsigned ShouldCopy : 1; 225 }; 226 227 class InitListExprBitfields { 228 friend class InitListExpr; 229 230 unsigned : NumExprBits; 231 232 /// Whether this initializer list originally had a GNU array-range 233 /// designator in it. This is a temporary marker used by CodeGen. 234 unsigned HadArrayRangeDesignator : 1; 235 }; 236 237 class TypeTraitExprBitfields { 238 friend class TypeTraitExpr; 239 friend class ASTStmtReader; 240 friend class ASTStmtWriter; 241 242 unsigned : NumExprBits; 243 244 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator. 245 unsigned Kind : 8; 246 247 /// \brief If this expression is not value-dependent, this indicates whether 248 /// the trait evaluated true or false. 249 unsigned Value : 1; 250 251 /// \brief The number of arguments to this type trait. 252 unsigned NumArgs : 32 - 8 - 1 - NumExprBits; 253 }; 254 255 union { 256 StmtBitfields StmtBits; 257 CompoundStmtBitfields CompoundStmtBits; 258 IfStmtBitfields IfStmtBits; 259 ExprBitfields ExprBits; 260 CharacterLiteralBitfields CharacterLiteralBits; 261 FloatingLiteralBitfields FloatingLiteralBits; 262 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits; 263 DeclRefExprBitfields DeclRefExprBits; 264 CastExprBitfields CastExprBits; 265 CallExprBitfields CallExprBits; 266 ExprWithCleanupsBitfields ExprWithCleanupsBits; 267 PseudoObjectExprBitfields PseudoObjectExprBits; 268 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits; 269 InitListExprBitfields InitListExprBits; 270 TypeTraitExprBitfields TypeTraitExprBits; 271 }; 272 273 friend class ASTStmtReader; 274 friend class ASTStmtWriter; 275 276 public: 277 // Only allow allocation of Stmts using the allocator in ASTContext 278 // or by doing a placement new. 279 void* operator new(size_t bytes, const ASTContext& C, 280 unsigned alignment = 8); 281 282 void* operator new(size_t bytes, const ASTContext* C, 283 unsigned alignment = 8) { 284 return operator new(bytes, *C, alignment); 285 } 286 287 void *operator new(size_t bytes, void *mem) LLVM_NOEXCEPT { return mem; } 288 289 void operator delete(void *, const ASTContext &, unsigned) LLVM_NOEXCEPT {} 290 void operator delete(void *, const ASTContext *, unsigned) LLVM_NOEXCEPT {} 291 void operator delete(void *, size_t) LLVM_NOEXCEPT {} 292 void operator delete(void *, void *) LLVM_NOEXCEPT {} 293 294 public: 295 /// \brief A placeholder type used to construct an empty shell of a 296 /// type, that will be filled in later (e.g., by some 297 /// de-serialization). 298 struct EmptyShell { }; 299 300 protected: 301 /// Iterator for iterating over Stmt * arrays that contain only Expr * 302 /// 303 /// This is needed because AST nodes use Stmt* arrays to store 304 /// references to children (to be compatible with StmtIterator). 305 struct ExprIterator 306 : llvm::iterator_adaptor_base<ExprIterator, Stmt **, 307 std::random_access_iterator_tag, Expr *> { 308 ExprIterator() : iterator_adaptor_base(nullptr) {} 309 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {} 310 311 reference operator*() const { 312 assert((*I)->getStmtClass() >= firstExprConstant && 313 (*I)->getStmtClass() <= lastExprConstant); 314 return *reinterpret_cast<Expr **>(I); 315 } 316 }; 317 318 /// Const iterator for iterating over Stmt * arrays that contain only Expr * 319 struct ConstExprIterator 320 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *, 321 std::random_access_iterator_tag, 322 const Expr *const> { 323 ConstExprIterator() : iterator_adaptor_base(nullptr) {} 324 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {} 325 326 reference operator*() const { 327 assert((*I)->getStmtClass() >= firstExprConstant && 328 (*I)->getStmtClass() <= lastExprConstant); 329 return *reinterpret_cast<const Expr *const *>(I); 330 } 331 }; 332 333 private: 334 /// \brief Whether statistic collection is enabled. 335 static bool StatisticsEnabled; 336 337 protected: 338 /// \brief Construct an empty statement. 339 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {} 340 341 public: 342 Stmt(StmtClass SC) { 343 static_assert(sizeof(*this) % llvm::AlignOf<void *>::Alignment == 0, 344 "Insufficient alignment!"); 345 StmtBits.sClass = SC; 346 if (StatisticsEnabled) Stmt::addStmtClass(SC); 347 } 348 349 StmtClass getStmtClass() const { 350 return static_cast<StmtClass>(StmtBits.sClass); 351 } 352 const char *getStmtClassName() const; 353 354 /// SourceLocation tokens are not useful in isolation - they are low level 355 /// value objects created/interpreted by SourceManager. We assume AST 356 /// clients will have a pointer to the respective SourceManager. 357 SourceRange getSourceRange() const LLVM_READONLY; 358 SourceLocation getLocStart() const LLVM_READONLY; 359 SourceLocation getLocEnd() const LLVM_READONLY; 360 361 // global temp stats (until we have a per-module visitor) 362 static void addStmtClass(const StmtClass s); 363 static void EnableStatistics(); 364 static void PrintStats(); 365 366 /// \brief Dumps the specified AST fragment and all subtrees to 367 /// \c llvm::errs(). 368 void dump() const; 369 void dump(SourceManager &SM) const; 370 void dump(raw_ostream &OS, SourceManager &SM) const; 371 void dump(raw_ostream &OS) const; 372 373 /// dumpColor - same as dump(), but forces color highlighting. 374 void dumpColor() const; 375 376 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST 377 /// back to its original source language syntax. 378 void dumpPretty(const ASTContext &Context) const; 379 void printPretty(raw_ostream &OS, PrinterHelper *Helper, 380 const PrintingPolicy &Policy, 381 unsigned Indentation = 0) const; 382 383 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only 384 /// works on systems with GraphViz (Mac OS X) or dot+gv installed. 385 void viewAST() const; 386 387 /// Skip past any implicit AST nodes which might surround this 388 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes. 389 Stmt *IgnoreImplicit(); 390 391 /// \brief Skip no-op (attributed, compound) container stmts and skip captured 392 /// stmt at the top, if \a IgnoreCaptured is true. 393 Stmt *IgnoreContainers(bool IgnoreCaptured = false); 394 395 const Stmt *stripLabelLikeStatements() const; 396 Stmt *stripLabelLikeStatements() { 397 return const_cast<Stmt*>( 398 const_cast<const Stmt*>(this)->stripLabelLikeStatements()); 399 } 400 401 /// Child Iterators: All subclasses must implement 'children' 402 /// to permit easy iteration over the substatements/subexpessions of an 403 /// AST node. This permits easy iteration over all nodes in the AST. 404 typedef StmtIterator child_iterator; 405 typedef ConstStmtIterator const_child_iterator; 406 407 typedef llvm::iterator_range<child_iterator> child_range; 408 typedef llvm::iterator_range<const_child_iterator> const_child_range; 409 410 child_range children(); 411 const_child_range children() const { 412 auto Children = const_cast<Stmt *>(this)->children(); 413 return const_child_range(Children.begin(), Children.end()); 414 } 415 416 child_iterator child_begin() { return children().begin(); } 417 child_iterator child_end() { return children().end(); } 418 419 const_child_iterator child_begin() const { return children().begin(); } 420 const_child_iterator child_end() const { return children().end(); } 421 422 /// \brief Produce a unique representation of the given statement. 423 /// 424 /// \param ID once the profiling operation is complete, will contain 425 /// the unique representation of the given statement. 426 /// 427 /// \param Context the AST context in which the statement resides 428 /// 429 /// \param Canonical whether the profile should be based on the canonical 430 /// representation of this statement (e.g., where non-type template 431 /// parameters are identified by index/level rather than their 432 /// declaration pointers) or the exact representation of the statement as 433 /// written in the source. 434 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 435 bool Canonical) const; 436 }; 437 438 /// DeclStmt - Adaptor class for mixing declarations with statements and 439 /// expressions. For example, CompoundStmt mixes statements, expressions 440 /// and declarations (variables, types). Another example is ForStmt, where 441 /// the first statement can be an expression or a declaration. 442 /// 443 class DeclStmt : public Stmt { 444 DeclGroupRef DG; 445 SourceLocation StartLoc, EndLoc; 446 447 public: 448 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, 449 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg), 450 StartLoc(startLoc), EndLoc(endLoc) {} 451 452 /// \brief Build an empty declaration statement. 453 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { } 454 455 /// isSingleDecl - This method returns true if this DeclStmt refers 456 /// to a single Decl. 457 bool isSingleDecl() const { 458 return DG.isSingleDecl(); 459 } 460 461 const Decl *getSingleDecl() const { return DG.getSingleDecl(); } 462 Decl *getSingleDecl() { return DG.getSingleDecl(); } 463 464 const DeclGroupRef getDeclGroup() const { return DG; } 465 DeclGroupRef getDeclGroup() { return DG; } 466 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; } 467 468 SourceLocation getStartLoc() const { return StartLoc; } 469 void setStartLoc(SourceLocation L) { StartLoc = L; } 470 SourceLocation getEndLoc() const { return EndLoc; } 471 void setEndLoc(SourceLocation L) { EndLoc = L; } 472 473 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; } 474 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; } 475 476 static bool classof(const Stmt *T) { 477 return T->getStmtClass() == DeclStmtClass; 478 } 479 480 // Iterators over subexpressions. 481 child_range children() { 482 return child_range(child_iterator(DG.begin(), DG.end()), 483 child_iterator(DG.end(), DG.end())); 484 } 485 486 typedef DeclGroupRef::iterator decl_iterator; 487 typedef DeclGroupRef::const_iterator const_decl_iterator; 488 typedef llvm::iterator_range<decl_iterator> decl_range; 489 typedef llvm::iterator_range<const_decl_iterator> decl_const_range; 490 491 decl_range decls() { return decl_range(decl_begin(), decl_end()); } 492 decl_const_range decls() const { 493 return decl_const_range(decl_begin(), decl_end()); 494 } 495 decl_iterator decl_begin() { return DG.begin(); } 496 decl_iterator decl_end() { return DG.end(); } 497 const_decl_iterator decl_begin() const { return DG.begin(); } 498 const_decl_iterator decl_end() const { return DG.end(); } 499 500 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator; 501 reverse_decl_iterator decl_rbegin() { 502 return reverse_decl_iterator(decl_end()); 503 } 504 reverse_decl_iterator decl_rend() { 505 return reverse_decl_iterator(decl_begin()); 506 } 507 }; 508 509 /// NullStmt - This is the null statement ";": C99 6.8.3p3. 510 /// 511 class NullStmt : public Stmt { 512 SourceLocation SemiLoc; 513 514 /// \brief True if the null statement was preceded by an empty macro, e.g: 515 /// @code 516 /// #define CALL(x) 517 /// CALL(0); 518 /// @endcode 519 bool HasLeadingEmptyMacro; 520 public: 521 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false) 522 : Stmt(NullStmtClass), SemiLoc(L), 523 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {} 524 525 /// \brief Build an empty null statement. 526 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty), 527 HasLeadingEmptyMacro(false) { } 528 529 SourceLocation getSemiLoc() const { return SemiLoc; } 530 void setSemiLoc(SourceLocation L) { SemiLoc = L; } 531 532 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; } 533 534 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; } 535 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; } 536 537 static bool classof(const Stmt *T) { 538 return T->getStmtClass() == NullStmtClass; 539 } 540 541 child_range children() { 542 return child_range(child_iterator(), child_iterator()); 543 } 544 545 friend class ASTStmtReader; 546 friend class ASTStmtWriter; 547 }; 548 549 /// CompoundStmt - This represents a group of statements like { stmt stmt }. 550 /// 551 class CompoundStmt : public Stmt { 552 Stmt** Body; 553 SourceLocation LBraceLoc, RBraceLoc; 554 555 friend class ASTStmtReader; 556 557 public: 558 CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts, 559 SourceLocation LB, SourceLocation RB); 560 561 // \brief Build an empty compound statement with a location. 562 explicit CompoundStmt(SourceLocation Loc) 563 : Stmt(CompoundStmtClass), Body(nullptr), LBraceLoc(Loc), RBraceLoc(Loc) { 564 CompoundStmtBits.NumStmts = 0; 565 } 566 567 // \brief Build an empty compound statement. 568 explicit CompoundStmt(EmptyShell Empty) 569 : Stmt(CompoundStmtClass, Empty), Body(nullptr) { 570 CompoundStmtBits.NumStmts = 0; 571 } 572 573 void setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts); 574 575 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; } 576 unsigned size() const { return CompoundStmtBits.NumStmts; } 577 578 typedef Stmt** body_iterator; 579 typedef llvm::iterator_range<body_iterator> body_range; 580 581 body_range body() { return body_range(body_begin(), body_end()); } 582 body_iterator body_begin() { return Body; } 583 body_iterator body_end() { return Body + size(); } 584 Stmt *body_front() { return !body_empty() ? Body[0] : nullptr; } 585 Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; } 586 587 void setLastStmt(Stmt *S) { 588 assert(!body_empty() && "setLastStmt"); 589 Body[size()-1] = S; 590 } 591 592 typedef Stmt* const * const_body_iterator; 593 typedef llvm::iterator_range<const_body_iterator> body_const_range; 594 595 body_const_range body() const { 596 return body_const_range(body_begin(), body_end()); 597 } 598 const_body_iterator body_begin() const { return Body; } 599 const_body_iterator body_end() const { return Body + size(); } 600 const Stmt *body_front() const { 601 return !body_empty() ? Body[0] : nullptr; 602 } 603 const Stmt *body_back() const { 604 return !body_empty() ? Body[size() - 1] : nullptr; 605 } 606 607 typedef std::reverse_iterator<body_iterator> reverse_body_iterator; 608 reverse_body_iterator body_rbegin() { 609 return reverse_body_iterator(body_end()); 610 } 611 reverse_body_iterator body_rend() { 612 return reverse_body_iterator(body_begin()); 613 } 614 615 typedef std::reverse_iterator<const_body_iterator> 616 const_reverse_body_iterator; 617 618 const_reverse_body_iterator body_rbegin() const { 619 return const_reverse_body_iterator(body_end()); 620 } 621 622 const_reverse_body_iterator body_rend() const { 623 return const_reverse_body_iterator(body_begin()); 624 } 625 626 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; } 627 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; } 628 629 SourceLocation getLBracLoc() const { return LBraceLoc; } 630 SourceLocation getRBracLoc() const { return RBraceLoc; } 631 632 static bool classof(const Stmt *T) { 633 return T->getStmtClass() == CompoundStmtClass; 634 } 635 636 // Iterators 637 child_range children() { 638 return child_range(Body, Body + CompoundStmtBits.NumStmts); 639 } 640 641 const_child_range children() const { 642 return const_child_range(child_iterator(Body), 643 child_iterator(Body + CompoundStmtBits.NumStmts)); 644 } 645 }; 646 647 // SwitchCase is the base class for CaseStmt and DefaultStmt, 648 class SwitchCase : public Stmt { 649 protected: 650 // A pointer to the following CaseStmt or DefaultStmt class, 651 // used by SwitchStmt. 652 SwitchCase *NextSwitchCase; 653 SourceLocation KeywordLoc; 654 SourceLocation ColonLoc; 655 656 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc) 657 : Stmt(SC), NextSwitchCase(nullptr), KeywordLoc(KWLoc), ColonLoc(ColonLoc) { 658 } 659 660 SwitchCase(StmtClass SC, EmptyShell) 661 : Stmt(SC), NextSwitchCase(nullptr) {} 662 663 public: 664 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 665 666 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 667 668 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 669 670 SourceLocation getKeywordLoc() const { return KeywordLoc; } 671 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; } 672 SourceLocation getColonLoc() const { return ColonLoc; } 673 void setColonLoc(SourceLocation L) { ColonLoc = L; } 674 675 Stmt *getSubStmt(); 676 const Stmt *getSubStmt() const { 677 return const_cast<SwitchCase*>(this)->getSubStmt(); 678 } 679 680 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } 681 SourceLocation getLocEnd() const LLVM_READONLY; 682 683 static bool classof(const Stmt *T) { 684 return T->getStmtClass() == CaseStmtClass || 685 T->getStmtClass() == DefaultStmtClass; 686 } 687 }; 688 689 class CaseStmt : public SwitchCase { 690 SourceLocation EllipsisLoc; 691 enum { LHS, RHS, SUBSTMT, END_EXPR }; 692 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for 693 // GNU "case 1 ... 4" extension 694 public: 695 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc, 696 SourceLocation ellipsisLoc, SourceLocation colonLoc) 697 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) { 698 SubExprs[SUBSTMT] = nullptr; 699 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs); 700 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs); 701 EllipsisLoc = ellipsisLoc; 702 } 703 704 /// \brief Build an empty switch case statement. 705 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { } 706 707 SourceLocation getCaseLoc() const { return KeywordLoc; } 708 void setCaseLoc(SourceLocation L) { KeywordLoc = L; } 709 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 710 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; } 711 SourceLocation getColonLoc() const { return ColonLoc; } 712 void setColonLoc(SourceLocation L) { ColonLoc = L; } 713 714 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); } 715 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); } 716 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } 717 718 const Expr *getLHS() const { 719 return reinterpret_cast<const Expr*>(SubExprs[LHS]); 720 } 721 const Expr *getRHS() const { 722 return reinterpret_cast<const Expr*>(SubExprs[RHS]); 723 } 724 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; } 725 726 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; } 727 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); } 728 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } 729 730 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } 731 SourceLocation getLocEnd() const LLVM_READONLY { 732 // Handle deeply nested case statements with iteration instead of recursion. 733 const CaseStmt *CS = this; 734 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) 735 CS = CS2; 736 737 return CS->getSubStmt()->getLocEnd(); 738 } 739 740 static bool classof(const Stmt *T) { 741 return T->getStmtClass() == CaseStmtClass; 742 } 743 744 // Iterators 745 child_range children() { 746 return child_range(&SubExprs[0], &SubExprs[END_EXPR]); 747 } 748 }; 749 750 class DefaultStmt : public SwitchCase { 751 Stmt* SubStmt; 752 public: 753 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) : 754 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {} 755 756 /// \brief Build an empty default statement. 757 explicit DefaultStmt(EmptyShell Empty) 758 : SwitchCase(DefaultStmtClass, Empty) { } 759 760 Stmt *getSubStmt() { return SubStmt; } 761 const Stmt *getSubStmt() const { return SubStmt; } 762 void setSubStmt(Stmt *S) { SubStmt = S; } 763 764 SourceLocation getDefaultLoc() const { return KeywordLoc; } 765 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; } 766 SourceLocation getColonLoc() const { return ColonLoc; } 767 void setColonLoc(SourceLocation L) { ColonLoc = L; } 768 769 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } 770 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 771 772 static bool classof(const Stmt *T) { 773 return T->getStmtClass() == DefaultStmtClass; 774 } 775 776 // Iterators 777 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 778 }; 779 780 inline SourceLocation SwitchCase::getLocEnd() const { 781 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this)) 782 return CS->getLocEnd(); 783 return cast<DefaultStmt>(this)->getLocEnd(); 784 } 785 786 /// LabelStmt - Represents a label, which has a substatement. For example: 787 /// foo: return; 788 /// 789 class LabelStmt : public Stmt { 790 SourceLocation IdentLoc; 791 LabelDecl *TheDecl; 792 Stmt *SubStmt; 793 794 public: 795 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt) 796 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) { 797 static_assert(sizeof(LabelStmt) == 798 2 * sizeof(SourceLocation) + 2 * sizeof(void *), 799 "LabelStmt too big"); 800 } 801 802 // \brief Build an empty label statement. 803 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { } 804 805 SourceLocation getIdentLoc() const { return IdentLoc; } 806 LabelDecl *getDecl() const { return TheDecl; } 807 void setDecl(LabelDecl *D) { TheDecl = D; } 808 const char *getName() const; 809 Stmt *getSubStmt() { return SubStmt; } 810 const Stmt *getSubStmt() const { return SubStmt; } 811 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 812 void setSubStmt(Stmt *SS) { SubStmt = SS; } 813 814 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; } 815 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 816 817 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 818 819 static bool classof(const Stmt *T) { 820 return T->getStmtClass() == LabelStmtClass; 821 } 822 }; 823 824 825 /// \brief Represents an attribute applied to a statement. 826 /// 827 /// Represents an attribute applied to a statement. For example: 828 /// [[omp::for(...)]] for (...) { ... } 829 /// 830 class AttributedStmt : public Stmt { 831 Stmt *SubStmt; 832 SourceLocation AttrLoc; 833 unsigned NumAttrs; 834 835 friend class ASTStmtReader; 836 837 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt) 838 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc), 839 NumAttrs(Attrs.size()) { 840 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr()); 841 } 842 843 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs) 844 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) { 845 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr); 846 } 847 848 const Attr *const *getAttrArrayPtr() const { 849 return reinterpret_cast<const Attr *const *>(this + 1); 850 } 851 const Attr **getAttrArrayPtr() { 852 return reinterpret_cast<const Attr **>(this + 1); 853 } 854 855 public: 856 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc, 857 ArrayRef<const Attr*> Attrs, Stmt *SubStmt); 858 // \brief Build an empty attributed statement. 859 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs); 860 861 SourceLocation getAttrLoc() const { return AttrLoc; } 862 ArrayRef<const Attr*> getAttrs() const { 863 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs); 864 } 865 Stmt *getSubStmt() { return SubStmt; } 866 const Stmt *getSubStmt() const { return SubStmt; } 867 868 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; } 869 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 870 871 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 872 873 static bool classof(const Stmt *T) { 874 return T->getStmtClass() == AttributedStmtClass; 875 } 876 }; 877 878 879 /// IfStmt - This represents an if/then/else. 880 /// 881 class IfStmt : public Stmt { 882 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR }; 883 Stmt* SubExprs[END_EXPR]; 884 885 SourceLocation IfLoc; 886 SourceLocation ElseLoc; 887 888 public: 889 IfStmt(const ASTContext &C, SourceLocation IL, 890 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond, 891 Stmt *then, SourceLocation EL = SourceLocation(), 892 Stmt *elsev = nullptr); 893 894 /// \brief Build an empty if/then/else statement 895 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { } 896 897 /// \brief Retrieve the variable declared in this "if" statement, if any. 898 /// 899 /// In the following example, "x" is the condition variable. 900 /// \code 901 /// if (int x = foo()) { 902 /// printf("x is %d", x); 903 /// } 904 /// \endcode 905 VarDecl *getConditionVariable() const; 906 void setConditionVariable(const ASTContext &C, VarDecl *V); 907 908 /// If this IfStmt has a condition variable, return the faux DeclStmt 909 /// associated with the creation of that condition variable. 910 const DeclStmt *getConditionVariableDeclStmt() const { 911 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 912 } 913 914 Stmt *getInit() { return SubExprs[INIT]; } 915 const Stmt *getInit() const { return SubExprs[INIT]; } 916 void setInit(Stmt *S) { SubExprs[INIT] = S; } 917 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 918 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 919 const Stmt *getThen() const { return SubExprs[THEN]; } 920 void setThen(Stmt *S) { SubExprs[THEN] = S; } 921 const Stmt *getElse() const { return SubExprs[ELSE]; } 922 void setElse(Stmt *S) { SubExprs[ELSE] = S; } 923 924 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 925 Stmt *getThen() { return SubExprs[THEN]; } 926 Stmt *getElse() { return SubExprs[ELSE]; } 927 928 SourceLocation getIfLoc() const { return IfLoc; } 929 void setIfLoc(SourceLocation L) { IfLoc = L; } 930 SourceLocation getElseLoc() const { return ElseLoc; } 931 void setElseLoc(SourceLocation L) { ElseLoc = L; } 932 933 bool isConstexpr() const { return IfStmtBits.IsConstexpr; } 934 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; } 935 936 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; } 937 SourceLocation getLocEnd() const LLVM_READONLY { 938 if (SubExprs[ELSE]) 939 return SubExprs[ELSE]->getLocEnd(); 940 else 941 return SubExprs[THEN]->getLocEnd(); 942 } 943 944 // Iterators over subexpressions. The iterators will include iterating 945 // over the initialization expression referenced by the condition variable. 946 child_range children() { 947 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 948 } 949 950 static bool classof(const Stmt *T) { 951 return T->getStmtClass() == IfStmtClass; 952 } 953 }; 954 955 /// SwitchStmt - This represents a 'switch' stmt. 956 /// 957 class SwitchStmt : public Stmt { 958 SourceLocation SwitchLoc; 959 enum { INIT, VAR, COND, BODY, END_EXPR }; 960 Stmt* SubExprs[END_EXPR]; 961 // This points to a linked list of case and default statements and, if the 962 // SwitchStmt is a switch on an enum value, records whether all the enum 963 // values were covered by CaseStmts. The coverage information value is meant 964 // to be a hint for possible clients. 965 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase; 966 967 public: 968 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond); 969 970 /// \brief Build a empty switch statement. 971 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { } 972 973 /// \brief Retrieve the variable declared in this "switch" statement, if any. 974 /// 975 /// In the following example, "x" is the condition variable. 976 /// \code 977 /// switch (int x = foo()) { 978 /// case 0: break; 979 /// // ... 980 /// } 981 /// \endcode 982 VarDecl *getConditionVariable() const; 983 void setConditionVariable(const ASTContext &C, VarDecl *V); 984 985 /// If this SwitchStmt has a condition variable, return the faux DeclStmt 986 /// associated with the creation of that condition variable. 987 const DeclStmt *getConditionVariableDeclStmt() const { 988 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 989 } 990 991 Stmt *getInit() { return SubExprs[INIT]; } 992 const Stmt *getInit() const { return SubExprs[INIT]; } 993 void setInit(Stmt *S) { SubExprs[INIT] = S; } 994 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 995 const Stmt *getBody() const { return SubExprs[BODY]; } 996 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); } 997 998 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 999 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 1000 Stmt *getBody() { return SubExprs[BODY]; } 1001 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1002 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); } 1003 1004 /// \brief Set the case list for this switch statement. 1005 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); } 1006 1007 SourceLocation getSwitchLoc() const { return SwitchLoc; } 1008 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; } 1009 1010 void setBody(Stmt *S, SourceLocation SL) { 1011 SubExprs[BODY] = S; 1012 SwitchLoc = SL; 1013 } 1014 void addSwitchCase(SwitchCase *SC) { 1015 assert(!SC->getNextSwitchCase() 1016 && "case/default already added to a switch"); 1017 SC->setNextSwitchCase(FirstCase.getPointer()); 1018 FirstCase.setPointer(SC); 1019 } 1020 1021 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a 1022 /// switch over an enum value then all cases have been explicitly covered. 1023 void setAllEnumCasesCovered() { FirstCase.setInt(true); } 1024 1025 /// Returns true if the SwitchStmt is a switch of an enum value and all cases 1026 /// have been explicitly covered. 1027 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); } 1028 1029 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; } 1030 SourceLocation getLocEnd() const LLVM_READONLY { 1031 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd(); 1032 } 1033 1034 // Iterators 1035 child_range children() { 1036 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1037 } 1038 1039 static bool classof(const Stmt *T) { 1040 return T->getStmtClass() == SwitchStmtClass; 1041 } 1042 }; 1043 1044 1045 /// WhileStmt - This represents a 'while' stmt. 1046 /// 1047 class WhileStmt : public Stmt { 1048 SourceLocation WhileLoc; 1049 enum { VAR, COND, BODY, END_EXPR }; 1050 Stmt* SubExprs[END_EXPR]; 1051 public: 1052 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body, 1053 SourceLocation WL); 1054 1055 /// \brief Build an empty while statement. 1056 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { } 1057 1058 /// \brief Retrieve the variable declared in this "while" statement, if any. 1059 /// 1060 /// In the following example, "x" is the condition variable. 1061 /// \code 1062 /// while (int x = random()) { 1063 /// // ... 1064 /// } 1065 /// \endcode 1066 VarDecl *getConditionVariable() const; 1067 void setConditionVariable(const ASTContext &C, VarDecl *V); 1068 1069 /// If this WhileStmt has a condition variable, return the faux DeclStmt 1070 /// associated with the creation of that condition variable. 1071 const DeclStmt *getConditionVariableDeclStmt() const { 1072 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 1073 } 1074 1075 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 1076 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 1077 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 1078 Stmt *getBody() { return SubExprs[BODY]; } 1079 const Stmt *getBody() const { return SubExprs[BODY]; } 1080 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1081 1082 SourceLocation getWhileLoc() const { return WhileLoc; } 1083 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 1084 1085 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; } 1086 SourceLocation getLocEnd() const LLVM_READONLY { 1087 return SubExprs[BODY]->getLocEnd(); 1088 } 1089 1090 static bool classof(const Stmt *T) { 1091 return T->getStmtClass() == WhileStmtClass; 1092 } 1093 1094 // Iterators 1095 child_range children() { 1096 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1097 } 1098 }; 1099 1100 /// DoStmt - This represents a 'do/while' stmt. 1101 /// 1102 class DoStmt : public Stmt { 1103 SourceLocation DoLoc; 1104 enum { BODY, COND, END_EXPR }; 1105 Stmt* SubExprs[END_EXPR]; 1106 SourceLocation WhileLoc; 1107 SourceLocation RParenLoc; // Location of final ')' in do stmt condition. 1108 1109 public: 1110 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL, 1111 SourceLocation RP) 1112 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) { 1113 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 1114 SubExprs[BODY] = body; 1115 } 1116 1117 /// \brief Build an empty do-while statement. 1118 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { } 1119 1120 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 1121 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 1122 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 1123 Stmt *getBody() { return SubExprs[BODY]; } 1124 const Stmt *getBody() const { return SubExprs[BODY]; } 1125 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1126 1127 SourceLocation getDoLoc() const { return DoLoc; } 1128 void setDoLoc(SourceLocation L) { DoLoc = L; } 1129 SourceLocation getWhileLoc() const { return WhileLoc; } 1130 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 1131 1132 SourceLocation getRParenLoc() const { return RParenLoc; } 1133 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1134 1135 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; } 1136 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 1137 1138 static bool classof(const Stmt *T) { 1139 return T->getStmtClass() == DoStmtClass; 1140 } 1141 1142 // Iterators 1143 child_range children() { 1144 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1145 } 1146 }; 1147 1148 1149 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 1150 /// the init/cond/inc parts of the ForStmt will be null if they were not 1151 /// specified in the source. 1152 /// 1153 class ForStmt : public Stmt { 1154 SourceLocation ForLoc; 1155 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR }; 1156 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 1157 SourceLocation LParenLoc, RParenLoc; 1158 1159 public: 1160 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, 1161 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP, 1162 SourceLocation RP); 1163 1164 /// \brief Build an empty for statement. 1165 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { } 1166 1167 Stmt *getInit() { return SubExprs[INIT]; } 1168 1169 /// \brief Retrieve the variable declared in this "for" statement, if any. 1170 /// 1171 /// In the following example, "y" is the condition variable. 1172 /// \code 1173 /// for (int x = random(); int y = mangle(x); ++x) { 1174 /// // ... 1175 /// } 1176 /// \endcode 1177 VarDecl *getConditionVariable() const; 1178 void setConditionVariable(const ASTContext &C, VarDecl *V); 1179 1180 /// If this ForStmt has a condition variable, return the faux DeclStmt 1181 /// associated with the creation of that condition variable. 1182 const DeclStmt *getConditionVariableDeclStmt() const { 1183 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]); 1184 } 1185 1186 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 1187 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 1188 Stmt *getBody() { return SubExprs[BODY]; } 1189 1190 const Stmt *getInit() const { return SubExprs[INIT]; } 1191 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 1192 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 1193 const Stmt *getBody() const { return SubExprs[BODY]; } 1194 1195 void setInit(Stmt *S) { SubExprs[INIT] = S; } 1196 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 1197 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); } 1198 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1199 1200 SourceLocation getForLoc() const { return ForLoc; } 1201 void setForLoc(SourceLocation L) { ForLoc = L; } 1202 SourceLocation getLParenLoc() const { return LParenLoc; } 1203 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1204 SourceLocation getRParenLoc() const { return RParenLoc; } 1205 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1206 1207 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; } 1208 SourceLocation getLocEnd() const LLVM_READONLY { 1209 return SubExprs[BODY]->getLocEnd(); 1210 } 1211 1212 static bool classof(const Stmt *T) { 1213 return T->getStmtClass() == ForStmtClass; 1214 } 1215 1216 // Iterators 1217 child_range children() { 1218 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1219 } 1220 }; 1221 1222 /// GotoStmt - This represents a direct goto. 1223 /// 1224 class GotoStmt : public Stmt { 1225 LabelDecl *Label; 1226 SourceLocation GotoLoc; 1227 SourceLocation LabelLoc; 1228 public: 1229 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL) 1230 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 1231 1232 /// \brief Build an empty goto statement. 1233 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { } 1234 1235 LabelDecl *getLabel() const { return Label; } 1236 void setLabel(LabelDecl *D) { Label = D; } 1237 1238 SourceLocation getGotoLoc() const { return GotoLoc; } 1239 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 1240 SourceLocation getLabelLoc() const { return LabelLoc; } 1241 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 1242 1243 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; } 1244 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; } 1245 1246 static bool classof(const Stmt *T) { 1247 return T->getStmtClass() == GotoStmtClass; 1248 } 1249 1250 // Iterators 1251 child_range children() { 1252 return child_range(child_iterator(), child_iterator()); 1253 } 1254 }; 1255 1256 /// IndirectGotoStmt - This represents an indirect goto. 1257 /// 1258 class IndirectGotoStmt : public Stmt { 1259 SourceLocation GotoLoc; 1260 SourceLocation StarLoc; 1261 Stmt *Target; 1262 public: 1263 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, 1264 Expr *target) 1265 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc), 1266 Target((Stmt*)target) {} 1267 1268 /// \brief Build an empty indirect goto statement. 1269 explicit IndirectGotoStmt(EmptyShell Empty) 1270 : Stmt(IndirectGotoStmtClass, Empty) { } 1271 1272 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 1273 SourceLocation getGotoLoc() const { return GotoLoc; } 1274 void setStarLoc(SourceLocation L) { StarLoc = L; } 1275 SourceLocation getStarLoc() const { return StarLoc; } 1276 1277 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); } 1278 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);} 1279 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); } 1280 1281 /// getConstantTarget - Returns the fixed target of this indirect 1282 /// goto, if one exists. 1283 LabelDecl *getConstantTarget(); 1284 const LabelDecl *getConstantTarget() const { 1285 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget(); 1286 } 1287 1288 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; } 1289 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); } 1290 1291 static bool classof(const Stmt *T) { 1292 return T->getStmtClass() == IndirectGotoStmtClass; 1293 } 1294 1295 // Iterators 1296 child_range children() { return child_range(&Target, &Target+1); } 1297 }; 1298 1299 1300 /// ContinueStmt - This represents a continue. 1301 /// 1302 class ContinueStmt : public Stmt { 1303 SourceLocation ContinueLoc; 1304 public: 1305 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 1306 1307 /// \brief Build an empty continue statement. 1308 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { } 1309 1310 SourceLocation getContinueLoc() const { return ContinueLoc; } 1311 void setContinueLoc(SourceLocation L) { ContinueLoc = L; } 1312 1313 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; } 1314 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; } 1315 1316 static bool classof(const Stmt *T) { 1317 return T->getStmtClass() == ContinueStmtClass; 1318 } 1319 1320 // Iterators 1321 child_range children() { 1322 return child_range(child_iterator(), child_iterator()); 1323 } 1324 }; 1325 1326 /// BreakStmt - This represents a break. 1327 /// 1328 class BreakStmt : public Stmt { 1329 SourceLocation BreakLoc; 1330 1331 public: 1332 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) { 1333 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation), 1334 "BreakStmt too large"); 1335 } 1336 1337 /// \brief Build an empty break statement. 1338 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { } 1339 1340 SourceLocation getBreakLoc() const { return BreakLoc; } 1341 void setBreakLoc(SourceLocation L) { BreakLoc = L; } 1342 1343 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; } 1344 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; } 1345 1346 static bool classof(const Stmt *T) { 1347 return T->getStmtClass() == BreakStmtClass; 1348 } 1349 1350 // Iterators 1351 child_range children() { 1352 return child_range(child_iterator(), child_iterator()); 1353 } 1354 }; 1355 1356 1357 /// ReturnStmt - This represents a return, optionally of an expression: 1358 /// return; 1359 /// return 4; 1360 /// 1361 /// Note that GCC allows return with no argument in a function declared to 1362 /// return a value, and it allows returning a value in functions declared to 1363 /// return void. We explicitly model this in the AST, which means you can't 1364 /// depend on the return type of the function and the presence of an argument. 1365 /// 1366 class ReturnStmt : public Stmt { 1367 SourceLocation RetLoc; 1368 Stmt *RetExpr; 1369 const VarDecl *NRVOCandidate; 1370 1371 public: 1372 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {} 1373 1374 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate) 1375 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E), 1376 NRVOCandidate(NRVOCandidate) {} 1377 1378 /// \brief Build an empty return expression. 1379 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { } 1380 1381 const Expr *getRetValue() const; 1382 Expr *getRetValue(); 1383 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); } 1384 1385 SourceLocation getReturnLoc() const { return RetLoc; } 1386 void setReturnLoc(SourceLocation L) { RetLoc = L; } 1387 1388 /// \brief Retrieve the variable that might be used for the named return 1389 /// value optimization. 1390 /// 1391 /// The optimization itself can only be performed if the variable is 1392 /// also marked as an NRVO object. 1393 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; } 1394 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; } 1395 1396 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; } 1397 SourceLocation getLocEnd() const LLVM_READONLY { 1398 return RetExpr ? RetExpr->getLocEnd() : RetLoc; 1399 } 1400 1401 static bool classof(const Stmt *T) { 1402 return T->getStmtClass() == ReturnStmtClass; 1403 } 1404 1405 // Iterators 1406 child_range children() { 1407 if (RetExpr) return child_range(&RetExpr, &RetExpr+1); 1408 return child_range(child_iterator(), child_iterator()); 1409 } 1410 }; 1411 1412 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt. 1413 /// 1414 class AsmStmt : public Stmt { 1415 protected: 1416 SourceLocation AsmLoc; 1417 /// \brief True if the assembly statement does not have any input or output 1418 /// operands. 1419 bool IsSimple; 1420 1421 /// \brief If true, treat this inline assembly as having side effects. 1422 /// This assembly statement should not be optimized, deleted or moved. 1423 bool IsVolatile; 1424 1425 unsigned NumOutputs; 1426 unsigned NumInputs; 1427 unsigned NumClobbers; 1428 1429 Stmt **Exprs; 1430 1431 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile, 1432 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) : 1433 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile), 1434 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { } 1435 1436 friend class ASTStmtReader; 1437 1438 public: 1439 /// \brief Build an empty inline-assembly statement. 1440 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : 1441 Stmt(SC, Empty), Exprs(nullptr) { } 1442 1443 SourceLocation getAsmLoc() const { return AsmLoc; } 1444 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 1445 1446 bool isSimple() const { return IsSimple; } 1447 void setSimple(bool V) { IsSimple = V; } 1448 1449 bool isVolatile() const { return IsVolatile; } 1450 void setVolatile(bool V) { IsVolatile = V; } 1451 1452 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); } 1453 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); } 1454 1455 //===--- Asm String Analysis ---===// 1456 1457 /// Assemble final IR asm string. 1458 std::string generateAsmString(const ASTContext &C) const; 1459 1460 //===--- Output operands ---===// 1461 1462 unsigned getNumOutputs() const { return NumOutputs; } 1463 1464 /// getOutputConstraint - Return the constraint string for the specified 1465 /// output operand. All output constraints are known to be non-empty (either 1466 /// '=' or '+'). 1467 StringRef getOutputConstraint(unsigned i) const; 1468 1469 /// isOutputPlusConstraint - Return true if the specified output constraint 1470 /// is a "+" constraint (which is both an input and an output) or false if it 1471 /// is an "=" constraint (just an output). 1472 bool isOutputPlusConstraint(unsigned i) const { 1473 return getOutputConstraint(i)[0] == '+'; 1474 } 1475 1476 const Expr *getOutputExpr(unsigned i) const; 1477 1478 /// getNumPlusOperands - Return the number of output operands that have a "+" 1479 /// constraint. 1480 unsigned getNumPlusOperands() const; 1481 1482 //===--- Input operands ---===// 1483 1484 unsigned getNumInputs() const { return NumInputs; } 1485 1486 /// getInputConstraint - Return the specified input constraint. Unlike output 1487 /// constraints, these can be empty. 1488 StringRef getInputConstraint(unsigned i) const; 1489 1490 const Expr *getInputExpr(unsigned i) const; 1491 1492 //===--- Other ---===// 1493 1494 unsigned getNumClobbers() const { return NumClobbers; } 1495 StringRef getClobber(unsigned i) const; 1496 1497 static bool classof(const Stmt *T) { 1498 return T->getStmtClass() == GCCAsmStmtClass || 1499 T->getStmtClass() == MSAsmStmtClass; 1500 } 1501 1502 // Input expr iterators. 1503 1504 typedef ExprIterator inputs_iterator; 1505 typedef ConstExprIterator const_inputs_iterator; 1506 typedef llvm::iterator_range<inputs_iterator> inputs_range; 1507 typedef llvm::iterator_range<const_inputs_iterator> inputs_const_range; 1508 1509 inputs_iterator begin_inputs() { 1510 return &Exprs[0] + NumOutputs; 1511 } 1512 1513 inputs_iterator end_inputs() { 1514 return &Exprs[0] + NumOutputs + NumInputs; 1515 } 1516 1517 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); } 1518 1519 const_inputs_iterator begin_inputs() const { 1520 return &Exprs[0] + NumOutputs; 1521 } 1522 1523 const_inputs_iterator end_inputs() const { 1524 return &Exprs[0] + NumOutputs + NumInputs; 1525 } 1526 1527 inputs_const_range inputs() const { 1528 return inputs_const_range(begin_inputs(), end_inputs()); 1529 } 1530 1531 // Output expr iterators. 1532 1533 typedef ExprIterator outputs_iterator; 1534 typedef ConstExprIterator const_outputs_iterator; 1535 typedef llvm::iterator_range<outputs_iterator> outputs_range; 1536 typedef llvm::iterator_range<const_outputs_iterator> outputs_const_range; 1537 1538 outputs_iterator begin_outputs() { 1539 return &Exprs[0]; 1540 } 1541 outputs_iterator end_outputs() { 1542 return &Exprs[0] + NumOutputs; 1543 } 1544 outputs_range outputs() { 1545 return outputs_range(begin_outputs(), end_outputs()); 1546 } 1547 1548 const_outputs_iterator begin_outputs() const { 1549 return &Exprs[0]; 1550 } 1551 const_outputs_iterator end_outputs() const { 1552 return &Exprs[0] + NumOutputs; 1553 } 1554 outputs_const_range outputs() const { 1555 return outputs_const_range(begin_outputs(), end_outputs()); 1556 } 1557 1558 child_range children() { 1559 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs); 1560 } 1561 }; 1562 1563 /// This represents a GCC inline-assembly statement extension. 1564 /// 1565 class GCCAsmStmt : public AsmStmt { 1566 SourceLocation RParenLoc; 1567 StringLiteral *AsmStr; 1568 1569 // FIXME: If we wanted to, we could allocate all of these in one big array. 1570 StringLiteral **Constraints; 1571 StringLiteral **Clobbers; 1572 IdentifierInfo **Names; 1573 1574 friend class ASTStmtReader; 1575 1576 public: 1577 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple, 1578 bool isvolatile, unsigned numoutputs, unsigned numinputs, 1579 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs, 1580 StringLiteral *asmstr, unsigned numclobbers, 1581 StringLiteral **clobbers, SourceLocation rparenloc); 1582 1583 /// \brief Build an empty inline-assembly statement. 1584 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty), 1585 Constraints(nullptr), Clobbers(nullptr), Names(nullptr) { } 1586 1587 SourceLocation getRParenLoc() const { return RParenLoc; } 1588 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1589 1590 //===--- Asm String Analysis ---===// 1591 1592 const StringLiteral *getAsmString() const { return AsmStr; } 1593 StringLiteral *getAsmString() { return AsmStr; } 1594 void setAsmString(StringLiteral *E) { AsmStr = E; } 1595 1596 /// AsmStringPiece - this is part of a decomposed asm string specification 1597 /// (for use with the AnalyzeAsmString function below). An asm string is 1598 /// considered to be a concatenation of these parts. 1599 class AsmStringPiece { 1600 public: 1601 enum Kind { 1602 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 1603 Operand // Operand reference, with optional modifier %c4. 1604 }; 1605 private: 1606 Kind MyKind; 1607 std::string Str; 1608 unsigned OperandNo; 1609 1610 // Source range for operand references. 1611 CharSourceRange Range; 1612 public: 1613 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 1614 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin, 1615 SourceLocation End) 1616 : MyKind(Operand), Str(S), OperandNo(OpNo), 1617 Range(CharSourceRange::getCharRange(Begin, End)) { 1618 } 1619 1620 bool isString() const { return MyKind == String; } 1621 bool isOperand() const { return MyKind == Operand; } 1622 1623 const std::string &getString() const { 1624 return Str; 1625 } 1626 1627 unsigned getOperandNo() const { 1628 assert(isOperand()); 1629 return OperandNo; 1630 } 1631 1632 CharSourceRange getRange() const { 1633 assert(isOperand() && "Range is currently used only for Operands."); 1634 return Range; 1635 } 1636 1637 /// getModifier - Get the modifier for this operand, if present. This 1638 /// returns '\0' if there was no modifier. 1639 char getModifier() const; 1640 }; 1641 1642 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 1643 /// it into pieces. If the asm string is erroneous, emit errors and return 1644 /// true, otherwise return false. This handles canonicalization and 1645 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 1646 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 1647 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces, 1648 const ASTContext &C, unsigned &DiagOffs) const; 1649 1650 /// Assemble final IR asm string. 1651 std::string generateAsmString(const ASTContext &C) const; 1652 1653 //===--- Output operands ---===// 1654 1655 IdentifierInfo *getOutputIdentifier(unsigned i) const { 1656 return Names[i]; 1657 } 1658 1659 StringRef getOutputName(unsigned i) const { 1660 if (IdentifierInfo *II = getOutputIdentifier(i)) 1661 return II->getName(); 1662 1663 return StringRef(); 1664 } 1665 1666 StringRef getOutputConstraint(unsigned i) const; 1667 1668 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 1669 return Constraints[i]; 1670 } 1671 StringLiteral *getOutputConstraintLiteral(unsigned i) { 1672 return Constraints[i]; 1673 } 1674 1675 Expr *getOutputExpr(unsigned i); 1676 1677 const Expr *getOutputExpr(unsigned i) const { 1678 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i); 1679 } 1680 1681 //===--- Input operands ---===// 1682 1683 IdentifierInfo *getInputIdentifier(unsigned i) const { 1684 return Names[i + NumOutputs]; 1685 } 1686 1687 StringRef getInputName(unsigned i) const { 1688 if (IdentifierInfo *II = getInputIdentifier(i)) 1689 return II->getName(); 1690 1691 return StringRef(); 1692 } 1693 1694 StringRef getInputConstraint(unsigned i) const; 1695 1696 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 1697 return Constraints[i + NumOutputs]; 1698 } 1699 StringLiteral *getInputConstraintLiteral(unsigned i) { 1700 return Constraints[i + NumOutputs]; 1701 } 1702 1703 Expr *getInputExpr(unsigned i); 1704 void setInputExpr(unsigned i, Expr *E); 1705 1706 const Expr *getInputExpr(unsigned i) const { 1707 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i); 1708 } 1709 1710 private: 1711 void setOutputsAndInputsAndClobbers(const ASTContext &C, 1712 IdentifierInfo **Names, 1713 StringLiteral **Constraints, 1714 Stmt **Exprs, 1715 unsigned NumOutputs, 1716 unsigned NumInputs, 1717 StringLiteral **Clobbers, 1718 unsigned NumClobbers); 1719 public: 1720 1721 //===--- Other ---===// 1722 1723 /// getNamedOperand - Given a symbolic operand reference like %[foo], 1724 /// translate this into a numeric value needed to reference the same operand. 1725 /// This returns -1 if the operand name is invalid. 1726 int getNamedOperand(StringRef SymbolicName) const; 1727 1728 StringRef getClobber(unsigned i) const; 1729 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; } 1730 const StringLiteral *getClobberStringLiteral(unsigned i) const { 1731 return Clobbers[i]; 1732 } 1733 1734 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; } 1735 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 1736 1737 static bool classof(const Stmt *T) { 1738 return T->getStmtClass() == GCCAsmStmtClass; 1739 } 1740 }; 1741 1742 /// This represents a Microsoft inline-assembly statement extension. 1743 /// 1744 class MSAsmStmt : public AsmStmt { 1745 SourceLocation LBraceLoc, EndLoc; 1746 StringRef AsmStr; 1747 1748 unsigned NumAsmToks; 1749 1750 Token *AsmToks; 1751 StringRef *Constraints; 1752 StringRef *Clobbers; 1753 1754 friend class ASTStmtReader; 1755 1756 public: 1757 MSAsmStmt(const ASTContext &C, SourceLocation asmloc, 1758 SourceLocation lbraceloc, bool issimple, bool isvolatile, 1759 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs, 1760 ArrayRef<StringRef> constraints, 1761 ArrayRef<Expr*> exprs, StringRef asmstr, 1762 ArrayRef<StringRef> clobbers, SourceLocation endloc); 1763 1764 /// \brief Build an empty MS-style inline-assembly statement. 1765 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty), 1766 NumAsmToks(0), AsmToks(nullptr), Constraints(nullptr), Clobbers(nullptr) { } 1767 1768 SourceLocation getLBraceLoc() const { return LBraceLoc; } 1769 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; } 1770 SourceLocation getEndLoc() const { return EndLoc; } 1771 void setEndLoc(SourceLocation L) { EndLoc = L; } 1772 1773 bool hasBraces() const { return LBraceLoc.isValid(); } 1774 1775 unsigned getNumAsmToks() { return NumAsmToks; } 1776 Token *getAsmToks() { return AsmToks; } 1777 1778 //===--- Asm String Analysis ---===// 1779 StringRef getAsmString() const { return AsmStr; } 1780 1781 /// Assemble final IR asm string. 1782 std::string generateAsmString(const ASTContext &C) const; 1783 1784 //===--- Output operands ---===// 1785 1786 StringRef getOutputConstraint(unsigned i) const { 1787 assert(i < NumOutputs); 1788 return Constraints[i]; 1789 } 1790 1791 Expr *getOutputExpr(unsigned i); 1792 1793 const Expr *getOutputExpr(unsigned i) const { 1794 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i); 1795 } 1796 1797 //===--- Input operands ---===// 1798 1799 StringRef getInputConstraint(unsigned i) const { 1800 assert(i < NumInputs); 1801 return Constraints[i + NumOutputs]; 1802 } 1803 1804 Expr *getInputExpr(unsigned i); 1805 void setInputExpr(unsigned i, Expr *E); 1806 1807 const Expr *getInputExpr(unsigned i) const { 1808 return const_cast<MSAsmStmt*>(this)->getInputExpr(i); 1809 } 1810 1811 //===--- Other ---===// 1812 1813 ArrayRef<StringRef> getAllConstraints() const { 1814 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs); 1815 } 1816 ArrayRef<StringRef> getClobbers() const { 1817 return llvm::makeArrayRef(Clobbers, NumClobbers); 1818 } 1819 ArrayRef<Expr*> getAllExprs() const { 1820 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs), 1821 NumInputs + NumOutputs); 1822 } 1823 1824 StringRef getClobber(unsigned i) const { return getClobbers()[i]; } 1825 1826 private: 1827 void initialize(const ASTContext &C, StringRef AsmString, 1828 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints, 1829 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers); 1830 public: 1831 1832 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; } 1833 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; } 1834 1835 static bool classof(const Stmt *T) { 1836 return T->getStmtClass() == MSAsmStmtClass; 1837 } 1838 1839 child_range children() { 1840 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]); 1841 } 1842 }; 1843 1844 class SEHExceptStmt : public Stmt { 1845 SourceLocation Loc; 1846 Stmt *Children[2]; 1847 1848 enum { FILTER_EXPR, BLOCK }; 1849 1850 SEHExceptStmt(SourceLocation Loc, 1851 Expr *FilterExpr, 1852 Stmt *Block); 1853 1854 friend class ASTReader; 1855 friend class ASTStmtReader; 1856 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { } 1857 1858 public: 1859 static SEHExceptStmt* Create(const ASTContext &C, 1860 SourceLocation ExceptLoc, 1861 Expr *FilterExpr, 1862 Stmt *Block); 1863 1864 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); } 1865 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 1866 1867 SourceLocation getExceptLoc() const { return Loc; } 1868 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); } 1869 1870 Expr *getFilterExpr() const { 1871 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]); 1872 } 1873 1874 CompoundStmt *getBlock() const { 1875 return cast<CompoundStmt>(Children[BLOCK]); 1876 } 1877 1878 child_range children() { 1879 return child_range(Children,Children+2); 1880 } 1881 1882 static bool classof(const Stmt *T) { 1883 return T->getStmtClass() == SEHExceptStmtClass; 1884 } 1885 1886 }; 1887 1888 class SEHFinallyStmt : public Stmt { 1889 SourceLocation Loc; 1890 Stmt *Block; 1891 1892 SEHFinallyStmt(SourceLocation Loc, 1893 Stmt *Block); 1894 1895 friend class ASTReader; 1896 friend class ASTStmtReader; 1897 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { } 1898 1899 public: 1900 static SEHFinallyStmt* Create(const ASTContext &C, 1901 SourceLocation FinallyLoc, 1902 Stmt *Block); 1903 1904 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); } 1905 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 1906 1907 SourceLocation getFinallyLoc() const { return Loc; } 1908 SourceLocation getEndLoc() const { return Block->getLocEnd(); } 1909 1910 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); } 1911 1912 child_range children() { 1913 return child_range(&Block,&Block+1); 1914 } 1915 1916 static bool classof(const Stmt *T) { 1917 return T->getStmtClass() == SEHFinallyStmtClass; 1918 } 1919 1920 }; 1921 1922 class SEHTryStmt : public Stmt { 1923 bool IsCXXTry; 1924 SourceLocation TryLoc; 1925 Stmt *Children[2]; 1926 1927 enum { TRY = 0, HANDLER = 1 }; 1928 1929 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try' 1930 SourceLocation TryLoc, 1931 Stmt *TryBlock, 1932 Stmt *Handler); 1933 1934 friend class ASTReader; 1935 friend class ASTStmtReader; 1936 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { } 1937 1938 public: 1939 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry, 1940 SourceLocation TryLoc, Stmt *TryBlock, 1941 Stmt *Handler); 1942 1943 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); } 1944 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 1945 1946 SourceLocation getTryLoc() const { return TryLoc; } 1947 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); } 1948 1949 bool getIsCXXTry() const { return IsCXXTry; } 1950 1951 CompoundStmt* getTryBlock() const { 1952 return cast<CompoundStmt>(Children[TRY]); 1953 } 1954 1955 Stmt *getHandler() const { return Children[HANDLER]; } 1956 1957 /// Returns 0 if not defined 1958 SEHExceptStmt *getExceptHandler() const; 1959 SEHFinallyStmt *getFinallyHandler() const; 1960 1961 child_range children() { 1962 return child_range(Children,Children+2); 1963 } 1964 1965 static bool classof(const Stmt *T) { 1966 return T->getStmtClass() == SEHTryStmtClass; 1967 } 1968 }; 1969 1970 /// Represents a __leave statement. 1971 /// 1972 class SEHLeaveStmt : public Stmt { 1973 SourceLocation LeaveLoc; 1974 public: 1975 explicit SEHLeaveStmt(SourceLocation LL) 1976 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {} 1977 1978 /// \brief Build an empty __leave statement. 1979 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) { } 1980 1981 SourceLocation getLeaveLoc() const { return LeaveLoc; } 1982 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; } 1983 1984 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; } 1985 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; } 1986 1987 static bool classof(const Stmt *T) { 1988 return T->getStmtClass() == SEHLeaveStmtClass; 1989 } 1990 1991 // Iterators 1992 child_range children() { 1993 return child_range(child_iterator(), child_iterator()); 1994 } 1995 }; 1996 1997 /// \brief This captures a statement into a function. For example, the following 1998 /// pragma annotated compound statement can be represented as a CapturedStmt, 1999 /// and this compound statement is the body of an anonymous outlined function. 2000 /// @code 2001 /// #pragma omp parallel 2002 /// { 2003 /// compute(); 2004 /// } 2005 /// @endcode 2006 class CapturedStmt : public Stmt { 2007 public: 2008 /// \brief The different capture forms: by 'this', by reference, capture for 2009 /// variable-length array type etc. 2010 enum VariableCaptureKind { 2011 VCK_This, 2012 VCK_ByRef, 2013 VCK_ByCopy, 2014 VCK_VLAType, 2015 }; 2016 2017 /// \brief Describes the capture of either a variable, or 'this', or 2018 /// variable-length array type. 2019 class Capture { 2020 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind; 2021 SourceLocation Loc; 2022 2023 public: 2024 /// \brief Create a new capture. 2025 /// 2026 /// \param Loc The source location associated with this capture. 2027 /// 2028 /// \param Kind The kind of capture (this, ByRef, ...). 2029 /// 2030 /// \param Var The variable being captured, or null if capturing this. 2031 /// 2032 Capture(SourceLocation Loc, VariableCaptureKind Kind, 2033 VarDecl *Var = nullptr); 2034 2035 /// \brief Determine the kind of capture. 2036 VariableCaptureKind getCaptureKind() const; 2037 2038 /// \brief Retrieve the source location at which the variable or 'this' was 2039 /// first used. 2040 SourceLocation getLocation() const { return Loc; } 2041 2042 /// \brief Determine whether this capture handles the C++ 'this' pointer. 2043 bool capturesThis() const { return getCaptureKind() == VCK_This; } 2044 2045 /// \brief Determine whether this capture handles a variable (by reference). 2046 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; } 2047 2048 /// \brief Determine whether this capture handles a variable by copy. 2049 bool capturesVariableByCopy() const { 2050 return getCaptureKind() == VCK_ByCopy; 2051 } 2052 2053 /// \brief Determine whether this capture handles a variable-length array 2054 /// type. 2055 bool capturesVariableArrayType() const { 2056 return getCaptureKind() == VCK_VLAType; 2057 } 2058 2059 /// \brief Retrieve the declaration of the variable being captured. 2060 /// 2061 /// This operation is only valid if this capture captures a variable. 2062 VarDecl *getCapturedVar() const; 2063 2064 friend class ASTStmtReader; 2065 }; 2066 2067 private: 2068 /// \brief The number of variable captured, including 'this'. 2069 unsigned NumCaptures; 2070 2071 /// \brief The pointer part is the implicit the outlined function and the 2072 /// int part is the captured region kind, 'CR_Default' etc. 2073 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind; 2074 2075 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl. 2076 RecordDecl *TheRecordDecl; 2077 2078 /// \brief Construct a captured statement. 2079 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures, 2080 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD); 2081 2082 /// \brief Construct an empty captured statement. 2083 CapturedStmt(EmptyShell Empty, unsigned NumCaptures); 2084 2085 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); } 2086 2087 Stmt *const *getStoredStmts() const { 2088 return reinterpret_cast<Stmt *const *>(this + 1); 2089 } 2090 2091 Capture *getStoredCaptures() const; 2092 2093 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; } 2094 2095 public: 2096 static CapturedStmt *Create(const ASTContext &Context, Stmt *S, 2097 CapturedRegionKind Kind, 2098 ArrayRef<Capture> Captures, 2099 ArrayRef<Expr *> CaptureInits, 2100 CapturedDecl *CD, RecordDecl *RD); 2101 2102 static CapturedStmt *CreateDeserialized(const ASTContext &Context, 2103 unsigned NumCaptures); 2104 2105 /// \brief Retrieve the statement being captured. 2106 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; } 2107 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; } 2108 2109 /// \brief Retrieve the outlined function declaration. 2110 CapturedDecl *getCapturedDecl(); 2111 const CapturedDecl *getCapturedDecl() const; 2112 2113 /// \brief Set the outlined function declaration. 2114 void setCapturedDecl(CapturedDecl *D); 2115 2116 /// \brief Retrieve the captured region kind. 2117 CapturedRegionKind getCapturedRegionKind() const; 2118 2119 /// \brief Set the captured region kind. 2120 void setCapturedRegionKind(CapturedRegionKind Kind); 2121 2122 /// \brief Retrieve the record declaration for captured variables. 2123 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; } 2124 2125 /// \brief Set the record declaration for captured variables. 2126 void setCapturedRecordDecl(RecordDecl *D) { 2127 assert(D && "null RecordDecl"); 2128 TheRecordDecl = D; 2129 } 2130 2131 /// \brief True if this variable has been captured. 2132 bool capturesVariable(const VarDecl *Var) const; 2133 2134 /// \brief An iterator that walks over the captures. 2135 typedef Capture *capture_iterator; 2136 typedef const Capture *const_capture_iterator; 2137 typedef llvm::iterator_range<capture_iterator> capture_range; 2138 typedef llvm::iterator_range<const_capture_iterator> capture_const_range; 2139 2140 capture_range captures() { 2141 return capture_range(capture_begin(), capture_end()); 2142 } 2143 capture_const_range captures() const { 2144 return capture_const_range(capture_begin(), capture_end()); 2145 } 2146 2147 /// \brief Retrieve an iterator pointing to the first capture. 2148 capture_iterator capture_begin() { return getStoredCaptures(); } 2149 const_capture_iterator capture_begin() const { return getStoredCaptures(); } 2150 2151 /// \brief Retrieve an iterator pointing past the end of the sequence of 2152 /// captures. 2153 capture_iterator capture_end() const { 2154 return getStoredCaptures() + NumCaptures; 2155 } 2156 2157 /// \brief Retrieve the number of captures, including 'this'. 2158 unsigned capture_size() const { return NumCaptures; } 2159 2160 /// \brief Iterator that walks over the capture initialization arguments. 2161 typedef Expr **capture_init_iterator; 2162 typedef llvm::iterator_range<capture_init_iterator> capture_init_range; 2163 2164 /// \brief Const iterator that walks over the capture initialization 2165 /// arguments. 2166 typedef Expr *const *const_capture_init_iterator; 2167 typedef llvm::iterator_range<const_capture_init_iterator> 2168 const_capture_init_range; 2169 2170 capture_init_range capture_inits() { 2171 return capture_init_range(capture_init_begin(), capture_init_end()); 2172 } 2173 2174 const_capture_init_range capture_inits() const { 2175 return const_capture_init_range(capture_init_begin(), capture_init_end()); 2176 } 2177 2178 /// \brief Retrieve the first initialization argument. 2179 capture_init_iterator capture_init_begin() { 2180 return reinterpret_cast<Expr **>(getStoredStmts()); 2181 } 2182 2183 const_capture_init_iterator capture_init_begin() const { 2184 return reinterpret_cast<Expr *const *>(getStoredStmts()); 2185 } 2186 2187 /// \brief Retrieve the iterator pointing one past the last initialization 2188 /// argument. 2189 capture_init_iterator capture_init_end() { 2190 return capture_init_begin() + NumCaptures; 2191 } 2192 2193 const_capture_init_iterator capture_init_end() const { 2194 return capture_init_begin() + NumCaptures; 2195 } 2196 2197 SourceLocation getLocStart() const LLVM_READONLY { 2198 return getCapturedStmt()->getLocStart(); 2199 } 2200 SourceLocation getLocEnd() const LLVM_READONLY { 2201 return getCapturedStmt()->getLocEnd(); 2202 } 2203 SourceRange getSourceRange() const LLVM_READONLY { 2204 return getCapturedStmt()->getSourceRange(); 2205 } 2206 2207 static bool classof(const Stmt *T) { 2208 return T->getStmtClass() == CapturedStmtClass; 2209 } 2210 2211 child_range children(); 2212 2213 friend class ASTStmtReader; 2214 }; 2215 2216 } // end namespace clang 2217 2218 #endif 2219