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