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