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