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