Stmt.h revision 4990890fc9428f98bef90ba349203a648c592778
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 unsigned NumAttrs; 801 const Attr *Attrs[1]; 802 803 friend class ASTStmtReader; 804 805 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt) 806 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc), 807 NumAttrs(Attrs.size()) { 808 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*)); 809 } 810 811 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs) 812 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) { 813 memset(Attrs, 0, NumAttrs * sizeof(Attr*)); 814 } 815 816public: 817 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc, 818 ArrayRef<const Attr*> Attrs, Stmt *SubStmt); 819 // \brief Build an empty attributed statement. 820 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs); 821 822 SourceLocation getAttrLoc() const { return AttrLoc; } 823 ArrayRef<const Attr*> getAttrs() const { 824 return ArrayRef<const Attr*>(Attrs, NumAttrs); 825 } 826 Stmt *getSubStmt() { return SubStmt; } 827 const Stmt *getSubStmt() const { return SubStmt; } 828 829 SourceRange getSourceRange() const LLVM_READONLY { 830 return SourceRange(AttrLoc, SubStmt->getLocEnd()); 831 } 832 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 833 834 static bool classof(const Stmt *T) { 835 return T->getStmtClass() == AttributedStmtClass; 836 } 837 static bool classof(const AttributedStmt *) { return true; } 838}; 839 840 841/// IfStmt - This represents an if/then/else. 842/// 843class IfStmt : public Stmt { 844 enum { VAR, COND, THEN, ELSE, END_EXPR }; 845 Stmt* SubExprs[END_EXPR]; 846 847 SourceLocation IfLoc; 848 SourceLocation ElseLoc; 849 850public: 851 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond, 852 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0); 853 854 /// \brief Build an empty if/then/else statement 855 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { } 856 857 /// \brief Retrieve the variable declared in this "if" statement, if any. 858 /// 859 /// In the following example, "x" is the condition variable. 860 /// \code 861 /// if (int x = foo()) { 862 /// printf("x is %d", x); 863 /// } 864 /// \endcode 865 VarDecl *getConditionVariable() const; 866 void setConditionVariable(ASTContext &C, VarDecl *V); 867 868 /// If this IfStmt has a condition variable, return the faux DeclStmt 869 /// associated with the creation of that condition variable. 870 const DeclStmt *getConditionVariableDeclStmt() const { 871 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 872 } 873 874 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 875 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 876 const Stmt *getThen() const { return SubExprs[THEN]; } 877 void setThen(Stmt *S) { SubExprs[THEN] = S; } 878 const Stmt *getElse() const { return SubExprs[ELSE]; } 879 void setElse(Stmt *S) { SubExprs[ELSE] = S; } 880 881 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 882 Stmt *getThen() { return SubExprs[THEN]; } 883 Stmt *getElse() { return SubExprs[ELSE]; } 884 885 SourceLocation getIfLoc() const { return IfLoc; } 886 void setIfLoc(SourceLocation L) { IfLoc = L; } 887 SourceLocation getElseLoc() const { return ElseLoc; } 888 void setElseLoc(SourceLocation L) { ElseLoc = L; } 889 890 SourceRange getSourceRange() const LLVM_READONLY { 891 if (SubExprs[ELSE]) 892 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); 893 else 894 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); 895 } 896 897 // Iterators over subexpressions. The iterators will include iterating 898 // over the initialization expression referenced by the condition variable. 899 child_range children() { 900 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 901 } 902 903 static bool classof(const Stmt *T) { 904 return T->getStmtClass() == IfStmtClass; 905 } 906 static bool classof(const IfStmt *) { return true; } 907}; 908 909/// SwitchStmt - This represents a 'switch' stmt. 910/// 911class SwitchStmt : public Stmt { 912 enum { VAR, COND, BODY, END_EXPR }; 913 Stmt* SubExprs[END_EXPR]; 914 // This points to a linked list of case and default statements. 915 SwitchCase *FirstCase; 916 SourceLocation SwitchLoc; 917 918 /// If the SwitchStmt is a switch on an enum value, this records whether 919 /// all the enum values were covered by CaseStmts. This value is meant to 920 /// be a hint for possible clients. 921 unsigned AllEnumCasesCovered : 1; 922 923public: 924 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond); 925 926 /// \brief Build a empty switch statement. 927 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { } 928 929 /// \brief Retrieve the variable declared in this "switch" statement, if any. 930 /// 931 /// In the following example, "x" is the condition variable. 932 /// \code 933 /// switch (int x = foo()) { 934 /// case 0: break; 935 /// // ... 936 /// } 937 /// \endcode 938 VarDecl *getConditionVariable() const; 939 void setConditionVariable(ASTContext &C, VarDecl *V); 940 941 /// If this SwitchStmt has a condition variable, return the faux DeclStmt 942 /// associated with the creation of that condition variable. 943 const DeclStmt *getConditionVariableDeclStmt() const { 944 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 945 } 946 947 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 948 const Stmt *getBody() const { return SubExprs[BODY]; } 949 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 950 951 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 952 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 953 Stmt *getBody() { return SubExprs[BODY]; } 954 void setBody(Stmt *S) { SubExprs[BODY] = S; } 955 SwitchCase *getSwitchCaseList() { return FirstCase; } 956 957 /// \brief Set the case list for this switch statement. 958 /// 959 /// The caller is responsible for incrementing the retain counts on 960 /// all of the SwitchCase statements in this list. 961 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } 962 963 SourceLocation getSwitchLoc() const { return SwitchLoc; } 964 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; } 965 966 void setBody(Stmt *S, SourceLocation SL) { 967 SubExprs[BODY] = S; 968 SwitchLoc = SL; 969 } 970 void addSwitchCase(SwitchCase *SC) { 971 assert(!SC->getNextSwitchCase() 972 && "case/default already added to a switch"); 973 SC->setNextSwitchCase(FirstCase); 974 FirstCase = SC; 975 } 976 977 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a 978 /// switch over an enum value then all cases have been explicitly covered. 979 void setAllEnumCasesCovered() { 980 AllEnumCasesCovered = 1; 981 } 982 983 /// Returns true if the SwitchStmt is a switch of an enum value and all cases 984 /// have been explicitly covered. 985 bool isAllEnumCasesCovered() const { 986 return (bool) AllEnumCasesCovered; 987 } 988 989 SourceRange getSourceRange() const LLVM_READONLY { 990 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); 991 } 992 // Iterators 993 child_range children() { 994 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 995 } 996 997 static bool classof(const Stmt *T) { 998 return T->getStmtClass() == SwitchStmtClass; 999 } 1000 static bool classof(const SwitchStmt *) { return true; } 1001}; 1002 1003 1004/// WhileStmt - This represents a 'while' stmt. 1005/// 1006class WhileStmt : public Stmt { 1007 enum { VAR, COND, BODY, END_EXPR }; 1008 Stmt* SubExprs[END_EXPR]; 1009 SourceLocation WhileLoc; 1010public: 1011 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body, 1012 SourceLocation WL); 1013 1014 /// \brief Build an empty while statement. 1015 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { } 1016 1017 /// \brief Retrieve the variable declared in this "while" statement, if any. 1018 /// 1019 /// In the following example, "x" is the condition variable. 1020 /// \code 1021 /// while (int x = random()) { 1022 /// // ... 1023 /// } 1024 /// \endcode 1025 VarDecl *getConditionVariable() const; 1026 void setConditionVariable(ASTContext &C, VarDecl *V); 1027 1028 /// If this WhileStmt has a condition variable, return the faux DeclStmt 1029 /// associated with the creation of that condition variable. 1030 const DeclStmt *getConditionVariableDeclStmt() const { 1031 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 1032 } 1033 1034 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 1035 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 1036 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 1037 Stmt *getBody() { return SubExprs[BODY]; } 1038 const Stmt *getBody() const { return SubExprs[BODY]; } 1039 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1040 1041 SourceLocation getWhileLoc() const { return WhileLoc; } 1042 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 1043 1044 SourceRange getSourceRange() const LLVM_READONLY { 1045 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); 1046 } 1047 static bool classof(const Stmt *T) { 1048 return T->getStmtClass() == WhileStmtClass; 1049 } 1050 static bool classof(const WhileStmt *) { return true; } 1051 1052 // Iterators 1053 child_range children() { 1054 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1055 } 1056}; 1057 1058/// DoStmt - This represents a 'do/while' stmt. 1059/// 1060class DoStmt : public Stmt { 1061 enum { BODY, COND, END_EXPR }; 1062 Stmt* SubExprs[END_EXPR]; 1063 SourceLocation DoLoc; 1064 SourceLocation WhileLoc; 1065 SourceLocation RParenLoc; // Location of final ')' in do stmt condition. 1066 1067public: 1068 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL, 1069 SourceLocation RP) 1070 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) { 1071 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 1072 SubExprs[BODY] = body; 1073 } 1074 1075 /// \brief Build an empty do-while statement. 1076 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { } 1077 1078 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 1079 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 1080 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 1081 Stmt *getBody() { return SubExprs[BODY]; } 1082 const Stmt *getBody() const { return SubExprs[BODY]; } 1083 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1084 1085 SourceLocation getDoLoc() const { return DoLoc; } 1086 void setDoLoc(SourceLocation L) { DoLoc = L; } 1087 SourceLocation getWhileLoc() const { return WhileLoc; } 1088 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 1089 1090 SourceLocation getRParenLoc() const { return RParenLoc; } 1091 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1092 1093 SourceRange getSourceRange() const LLVM_READONLY { 1094 return SourceRange(DoLoc, RParenLoc); 1095 } 1096 static bool classof(const Stmt *T) { 1097 return T->getStmtClass() == DoStmtClass; 1098 } 1099 static bool classof(const DoStmt *) { return true; } 1100 1101 // Iterators 1102 child_range children() { 1103 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1104 } 1105}; 1106 1107 1108/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 1109/// the init/cond/inc parts of the ForStmt will be null if they were not 1110/// specified in the source. 1111/// 1112class ForStmt : public Stmt { 1113 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR }; 1114 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 1115 SourceLocation ForLoc; 1116 SourceLocation LParenLoc, RParenLoc; 1117 1118public: 1119 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc, 1120 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP); 1121 1122 /// \brief Build an empty for statement. 1123 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { } 1124 1125 Stmt *getInit() { return SubExprs[INIT]; } 1126 1127 /// \brief Retrieve the variable declared in this "for" statement, if any. 1128 /// 1129 /// In the following example, "y" is the condition variable. 1130 /// \code 1131 /// for (int x = random(); int y = mangle(x); ++x) { 1132 /// // ... 1133 /// } 1134 /// \endcode 1135 VarDecl *getConditionVariable() const; 1136 void setConditionVariable(ASTContext &C, VarDecl *V); 1137 1138 /// If this ForStmt has a condition variable, return the faux DeclStmt 1139 /// associated with the creation of that condition variable. 1140 const DeclStmt *getConditionVariableDeclStmt() const { 1141 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]); 1142 } 1143 1144 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 1145 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 1146 Stmt *getBody() { return SubExprs[BODY]; } 1147 1148 const Stmt *getInit() const { return SubExprs[INIT]; } 1149 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 1150 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 1151 const Stmt *getBody() const { return SubExprs[BODY]; } 1152 1153 void setInit(Stmt *S) { SubExprs[INIT] = S; } 1154 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 1155 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); } 1156 void setBody(Stmt *S) { SubExprs[BODY] = S; } 1157 1158 SourceLocation getForLoc() const { return ForLoc; } 1159 void setForLoc(SourceLocation L) { ForLoc = L; } 1160 SourceLocation getLParenLoc() const { return LParenLoc; } 1161 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1162 SourceLocation getRParenLoc() const { return RParenLoc; } 1163 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1164 1165 SourceRange getSourceRange() const LLVM_READONLY { 1166 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 1167 } 1168 static bool classof(const Stmt *T) { 1169 return T->getStmtClass() == ForStmtClass; 1170 } 1171 static bool classof(const ForStmt *) { return true; } 1172 1173 // Iterators 1174 child_range children() { 1175 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 1176 } 1177}; 1178 1179/// GotoStmt - This represents a direct goto. 1180/// 1181class GotoStmt : public Stmt { 1182 LabelDecl *Label; 1183 SourceLocation GotoLoc; 1184 SourceLocation LabelLoc; 1185public: 1186 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL) 1187 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 1188 1189 /// \brief Build an empty goto statement. 1190 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { } 1191 1192 LabelDecl *getLabel() const { return Label; } 1193 void setLabel(LabelDecl *D) { Label = D; } 1194 1195 SourceLocation getGotoLoc() const { return GotoLoc; } 1196 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 1197 SourceLocation getLabelLoc() const { return LabelLoc; } 1198 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 1199 1200 SourceRange getSourceRange() const LLVM_READONLY { 1201 return SourceRange(GotoLoc, LabelLoc); 1202 } 1203 static bool classof(const Stmt *T) { 1204 return T->getStmtClass() == GotoStmtClass; 1205 } 1206 static bool classof(const GotoStmt *) { return true; } 1207 1208 // Iterators 1209 child_range children() { return child_range(); } 1210}; 1211 1212/// IndirectGotoStmt - This represents an indirect goto. 1213/// 1214class IndirectGotoStmt : public Stmt { 1215 SourceLocation GotoLoc; 1216 SourceLocation StarLoc; 1217 Stmt *Target; 1218public: 1219 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, 1220 Expr *target) 1221 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc), 1222 Target((Stmt*)target) {} 1223 1224 /// \brief Build an empty indirect goto statement. 1225 explicit IndirectGotoStmt(EmptyShell Empty) 1226 : Stmt(IndirectGotoStmtClass, Empty) { } 1227 1228 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 1229 SourceLocation getGotoLoc() const { return GotoLoc; } 1230 void setStarLoc(SourceLocation L) { StarLoc = L; } 1231 SourceLocation getStarLoc() const { return StarLoc; } 1232 1233 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); } 1234 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);} 1235 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); } 1236 1237 /// getConstantTarget - Returns the fixed target of this indirect 1238 /// goto, if one exists. 1239 LabelDecl *getConstantTarget(); 1240 const LabelDecl *getConstantTarget() const { 1241 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget(); 1242 } 1243 1244 SourceRange getSourceRange() const LLVM_READONLY { 1245 return SourceRange(GotoLoc, Target->getLocEnd()); 1246 } 1247 1248 static bool classof(const Stmt *T) { 1249 return T->getStmtClass() == IndirectGotoStmtClass; 1250 } 1251 static bool classof(const IndirectGotoStmt *) { return true; } 1252 1253 // Iterators 1254 child_range children() { return child_range(&Target, &Target+1); } 1255}; 1256 1257 1258/// ContinueStmt - This represents a continue. 1259/// 1260class ContinueStmt : public Stmt { 1261 SourceLocation ContinueLoc; 1262public: 1263 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 1264 1265 /// \brief Build an empty continue statement. 1266 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { } 1267 1268 SourceLocation getContinueLoc() const { return ContinueLoc; } 1269 void setContinueLoc(SourceLocation L) { ContinueLoc = L; } 1270 1271 SourceRange getSourceRange() const LLVM_READONLY { 1272 return SourceRange(ContinueLoc); 1273 } 1274 1275 static bool classof(const Stmt *T) { 1276 return T->getStmtClass() == ContinueStmtClass; 1277 } 1278 static bool classof(const ContinueStmt *) { return true; } 1279 1280 // Iterators 1281 child_range children() { return child_range(); } 1282}; 1283 1284/// BreakStmt - This represents a break. 1285/// 1286class BreakStmt : public Stmt { 1287 SourceLocation BreakLoc; 1288public: 1289 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 1290 1291 /// \brief Build an empty break statement. 1292 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { } 1293 1294 SourceLocation getBreakLoc() const { return BreakLoc; } 1295 void setBreakLoc(SourceLocation L) { BreakLoc = L; } 1296 1297 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); } 1298 1299 static bool classof(const Stmt *T) { 1300 return T->getStmtClass() == BreakStmtClass; 1301 } 1302 static bool classof(const BreakStmt *) { return true; } 1303 1304 // Iterators 1305 child_range children() { return child_range(); } 1306}; 1307 1308 1309/// ReturnStmt - This represents a return, optionally of an expression: 1310/// return; 1311/// return 4; 1312/// 1313/// Note that GCC allows return with no argument in a function declared to 1314/// return a value, and it allows returning a value in functions declared to 1315/// return void. We explicitly model this in the AST, which means you can't 1316/// depend on the return type of the function and the presence of an argument. 1317/// 1318class ReturnStmt : public Stmt { 1319 Stmt *RetExpr; 1320 SourceLocation RetLoc; 1321 const VarDecl *NRVOCandidate; 1322 1323public: 1324 ReturnStmt(SourceLocation RL) 1325 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { } 1326 1327 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate) 1328 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL), 1329 NRVOCandidate(NRVOCandidate) {} 1330 1331 /// \brief Build an empty return expression. 1332 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { } 1333 1334 const Expr *getRetValue() const; 1335 Expr *getRetValue(); 1336 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); } 1337 1338 SourceLocation getReturnLoc() const { return RetLoc; } 1339 void setReturnLoc(SourceLocation L) { RetLoc = L; } 1340 1341 /// \brief Retrieve the variable that might be used for the named return 1342 /// value optimization. 1343 /// 1344 /// The optimization itself can only be performed if the variable is 1345 /// also marked as an NRVO object. 1346 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; } 1347 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; } 1348 1349 SourceRange getSourceRange() const LLVM_READONLY; 1350 1351 static bool classof(const Stmt *T) { 1352 return T->getStmtClass() == ReturnStmtClass; 1353 } 1354 static bool classof(const ReturnStmt *) { return true; } 1355 1356 // Iterators 1357 child_range children() { 1358 if (RetExpr) return child_range(&RetExpr, &RetExpr+1); 1359 return child_range(); 1360 } 1361}; 1362 1363/// AsmStmt - This represents a GNU inline-assembly statement extension. 1364/// 1365class AsmStmt : public Stmt { 1366 SourceLocation AsmLoc, RParenLoc; 1367 StringLiteral *AsmStr; 1368 1369 bool IsSimple; 1370 bool IsVolatile; 1371 bool MSAsm; 1372 1373 unsigned NumOutputs; 1374 unsigned NumInputs; 1375 unsigned NumClobbers; 1376 1377 // FIXME: If we wanted to, we could allocate all of these in one big array. 1378 IdentifierInfo **Names; 1379 StringLiteral **Constraints; 1380 Stmt **Exprs; 1381 StringLiteral **Clobbers; 1382 1383public: 1384 AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile, 1385 bool msasm, unsigned numoutputs, unsigned numinputs, 1386 IdentifierInfo **names, StringLiteral **constraints, 1387 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers, 1388 StringLiteral **clobbers, SourceLocation rparenloc); 1389 1390 /// \brief Build an empty inline-assembly statement. 1391 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty), 1392 Names(0), Constraints(0), Exprs(0), Clobbers(0) { } 1393 1394 SourceLocation getAsmLoc() const { return AsmLoc; } 1395 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 1396 SourceLocation getRParenLoc() const { return RParenLoc; } 1397 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1398 1399 bool isVolatile() const { return IsVolatile; } 1400 void setVolatile(bool V) { IsVolatile = V; } 1401 bool isSimple() const { return IsSimple; } 1402 void setSimple(bool V) { IsSimple = V; } 1403 bool isMSAsm() const { return MSAsm; } 1404 void setMSAsm(bool V) { MSAsm = V; } 1405 1406 //===--- Asm String Analysis ---===// 1407 1408 const StringLiteral *getAsmString() const { return AsmStr; } 1409 StringLiteral *getAsmString() { return AsmStr; } 1410 void setAsmString(StringLiteral *E) { AsmStr = E; } 1411 1412 /// AsmStringPiece - this is part of a decomposed asm string specification 1413 /// (for use with the AnalyzeAsmString function below). An asm string is 1414 /// considered to be a concatenation of these parts. 1415 class AsmStringPiece { 1416 public: 1417 enum Kind { 1418 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 1419 Operand // Operand reference, with optional modifier %c4. 1420 }; 1421 private: 1422 Kind MyKind; 1423 std::string Str; 1424 unsigned OperandNo; 1425 public: 1426 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 1427 AsmStringPiece(unsigned OpNo, char Modifier) 1428 : MyKind(Operand), Str(), OperandNo(OpNo) { 1429 Str += Modifier; 1430 } 1431 1432 bool isString() const { return MyKind == String; } 1433 bool isOperand() const { return MyKind == Operand; } 1434 1435 const std::string &getString() const { 1436 assert(isString()); 1437 return Str; 1438 } 1439 1440 unsigned getOperandNo() const { 1441 assert(isOperand()); 1442 return OperandNo; 1443 } 1444 1445 /// getModifier - Get the modifier for this operand, if present. This 1446 /// returns '\0' if there was no modifier. 1447 char getModifier() const { 1448 assert(isOperand()); 1449 return Str[0]; 1450 } 1451 }; 1452 1453 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 1454 /// it into pieces. If the asm string is erroneous, emit errors and return 1455 /// true, otherwise return false. This handles canonicalization and 1456 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 1457 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 1458 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces, 1459 ASTContext &C, unsigned &DiagOffs) const; 1460 1461 1462 //===--- Output operands ---===// 1463 1464 unsigned getNumOutputs() const { return NumOutputs; } 1465 1466 IdentifierInfo *getOutputIdentifier(unsigned i) const { 1467 return Names[i]; 1468 } 1469 1470 StringRef getOutputName(unsigned i) const { 1471 if (IdentifierInfo *II = getOutputIdentifier(i)) 1472 return II->getName(); 1473 1474 return StringRef(); 1475 } 1476 1477 /// getOutputConstraint - Return the constraint string for the specified 1478 /// output operand. All output constraints are known to be non-empty (either 1479 /// '=' or '+'). 1480 StringRef getOutputConstraint(unsigned i) const; 1481 1482 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 1483 return Constraints[i]; 1484 } 1485 StringLiteral *getOutputConstraintLiteral(unsigned i) { 1486 return Constraints[i]; 1487 } 1488 1489 Expr *getOutputExpr(unsigned i); 1490 1491 const Expr *getOutputExpr(unsigned i) const { 1492 return const_cast<AsmStmt*>(this)->getOutputExpr(i); 1493 } 1494 1495 /// isOutputPlusConstraint - Return true if the specified output constraint 1496 /// is a "+" constraint (which is both an input and an output) or false if it 1497 /// is an "=" constraint (just an output). 1498 bool isOutputPlusConstraint(unsigned i) const { 1499 return getOutputConstraint(i)[0] == '+'; 1500 } 1501 1502 /// getNumPlusOperands - Return the number of output operands that have a "+" 1503 /// constraint. 1504 unsigned getNumPlusOperands() const; 1505 1506 //===--- Input operands ---===// 1507 1508 unsigned getNumInputs() const { return NumInputs; } 1509 1510 IdentifierInfo *getInputIdentifier(unsigned i) const { 1511 return Names[i + NumOutputs]; 1512 } 1513 1514 StringRef getInputName(unsigned i) const { 1515 if (IdentifierInfo *II = getInputIdentifier(i)) 1516 return II->getName(); 1517 1518 return StringRef(); 1519 } 1520 1521 /// getInputConstraint - Return the specified input constraint. Unlike output 1522 /// constraints, these can be empty. 1523 StringRef getInputConstraint(unsigned i) const; 1524 1525 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 1526 return Constraints[i + NumOutputs]; 1527 } 1528 StringLiteral *getInputConstraintLiteral(unsigned i) { 1529 return Constraints[i + NumOutputs]; 1530 } 1531 1532 Expr *getInputExpr(unsigned i); 1533 void setInputExpr(unsigned i, Expr *E); 1534 1535 const Expr *getInputExpr(unsigned i) const { 1536 return const_cast<AsmStmt*>(this)->getInputExpr(i); 1537 } 1538 1539 void setOutputsAndInputsAndClobbers(ASTContext &C, 1540 IdentifierInfo **Names, 1541 StringLiteral **Constraints, 1542 Stmt **Exprs, 1543 unsigned NumOutputs, 1544 unsigned NumInputs, 1545 StringLiteral **Clobbers, 1546 unsigned NumClobbers); 1547 1548 //===--- Other ---===// 1549 1550 /// getNamedOperand - Given a symbolic operand reference like %[foo], 1551 /// translate this into a numeric value needed to reference the same operand. 1552 /// This returns -1 if the operand name is invalid. 1553 int getNamedOperand(StringRef SymbolicName) const; 1554 1555 unsigned getNumClobbers() const { return NumClobbers; } 1556 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 1557 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 1558 1559 SourceRange getSourceRange() const LLVM_READONLY { 1560 return SourceRange(AsmLoc, RParenLoc); 1561 } 1562 1563 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 1564 static bool classof(const AsmStmt *) { return true; } 1565 1566 // Input expr iterators. 1567 1568 typedef ExprIterator inputs_iterator; 1569 typedef ConstExprIterator const_inputs_iterator; 1570 1571 inputs_iterator begin_inputs() { 1572 return &Exprs[0] + NumOutputs; 1573 } 1574 1575 inputs_iterator end_inputs() { 1576 return &Exprs[0] + NumOutputs + NumInputs; 1577 } 1578 1579 const_inputs_iterator begin_inputs() const { 1580 return &Exprs[0] + NumOutputs; 1581 } 1582 1583 const_inputs_iterator end_inputs() const { 1584 return &Exprs[0] + NumOutputs + NumInputs; 1585 } 1586 1587 // Output expr iterators. 1588 1589 typedef ExprIterator outputs_iterator; 1590 typedef ConstExprIterator const_outputs_iterator; 1591 1592 outputs_iterator begin_outputs() { 1593 return &Exprs[0]; 1594 } 1595 outputs_iterator end_outputs() { 1596 return &Exprs[0] + NumOutputs; 1597 } 1598 1599 const_outputs_iterator begin_outputs() const { 1600 return &Exprs[0]; 1601 } 1602 const_outputs_iterator end_outputs() const { 1603 return &Exprs[0] + NumOutputs; 1604 } 1605 1606 child_range children() { 1607 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs); 1608 } 1609}; 1610 1611/// MSAsmStmt - This represents a MS inline-assembly statement extension. 1612/// 1613class MSAsmStmt : public Stmt { 1614 SourceLocation AsmLoc, EndLoc; 1615 std::string AsmStr; 1616 1617 bool IsSimple; 1618 bool IsVolatile; 1619 1620 Stmt **Exprs; 1621 1622public: 1623 MSAsmStmt(ASTContext &C, SourceLocation asmloc, std::string &asmstr, 1624 SourceLocation endloc); 1625 1626 SourceLocation getAsmLoc() const { return AsmLoc; } 1627 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 1628 SourceLocation getEndLoc() const { return EndLoc; } 1629 void setEndLoc(SourceLocation L) { EndLoc = L; } 1630 1631 bool isVolatile() const { return IsVolatile; } 1632 void setVolatile(bool V) { IsVolatile = V; } 1633 bool isSimple() const { return IsSimple; } 1634 void setSimple(bool V) { IsSimple = V; } 1635 1636 //===--- Asm String Analysis ---===// 1637 1638 const std::string *getAsmString() const { return &AsmStr; } 1639 std::string *getAsmString() { return &AsmStr; } 1640 void setAsmString(StringRef &E) { AsmStr = E.str(); } 1641 1642 //===--- Other ---===// 1643 1644 SourceRange getSourceRange() const LLVM_READONLY { 1645 return SourceRange(AsmLoc, EndLoc); 1646 } 1647 static bool classof(const Stmt *T) { 1648 return T->getStmtClass() == MSAsmStmtClass; 1649 } 1650 static bool classof(const MSAsmStmt *) { return true; } 1651 1652 child_range children() { 1653 return child_range(&Exprs[0], &Exprs[0]); 1654 } 1655}; 1656 1657class SEHExceptStmt : public Stmt { 1658 SourceLocation Loc; 1659 Stmt *Children[2]; 1660 1661 enum { FILTER_EXPR, BLOCK }; 1662 1663 SEHExceptStmt(SourceLocation Loc, 1664 Expr *FilterExpr, 1665 Stmt *Block); 1666 1667 friend class ASTReader; 1668 friend class ASTStmtReader; 1669 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { } 1670 1671public: 1672 static SEHExceptStmt* Create(ASTContext &C, 1673 SourceLocation ExceptLoc, 1674 Expr *FilterExpr, 1675 Stmt *Block); 1676 SourceRange getSourceRange() const LLVM_READONLY { 1677 return SourceRange(getExceptLoc(), getEndLoc()); 1678 } 1679 1680 SourceLocation getExceptLoc() const { return Loc; } 1681 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); } 1682 1683 Expr *getFilterExpr() const { 1684 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]); 1685 } 1686 1687 CompoundStmt *getBlock() const { 1688 return llvm::cast<CompoundStmt>(Children[BLOCK]); 1689 } 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() == SEHExceptStmtClass; 1697 } 1698 1699 static bool classof(SEHExceptStmt *) { return true; } 1700 1701}; 1702 1703class SEHFinallyStmt : public Stmt { 1704 SourceLocation Loc; 1705 Stmt *Block; 1706 1707 SEHFinallyStmt(SourceLocation Loc, 1708 Stmt *Block); 1709 1710 friend class ASTReader; 1711 friend class ASTStmtReader; 1712 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { } 1713 1714public: 1715 static SEHFinallyStmt* Create(ASTContext &C, 1716 SourceLocation FinallyLoc, 1717 Stmt *Block); 1718 1719 SourceRange getSourceRange() const LLVM_READONLY { 1720 return SourceRange(getFinallyLoc(), getEndLoc()); 1721 } 1722 1723 SourceLocation getFinallyLoc() const { return Loc; } 1724 SourceLocation getEndLoc() const { return Block->getLocEnd(); } 1725 1726 CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); } 1727 1728 child_range children() { 1729 return child_range(&Block,&Block+1); 1730 } 1731 1732 static bool classof(const Stmt *T) { 1733 return T->getStmtClass() == SEHFinallyStmtClass; 1734 } 1735 1736 static bool classof(SEHFinallyStmt *) { return true; } 1737 1738}; 1739 1740class SEHTryStmt : public Stmt { 1741 bool IsCXXTry; 1742 SourceLocation TryLoc; 1743 Stmt *Children[2]; 1744 1745 enum { TRY = 0, HANDLER = 1 }; 1746 1747 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try' 1748 SourceLocation TryLoc, 1749 Stmt *TryBlock, 1750 Stmt *Handler); 1751 1752 friend class ASTReader; 1753 friend class ASTStmtReader; 1754 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { } 1755 1756public: 1757 static SEHTryStmt* Create(ASTContext &C, 1758 bool isCXXTry, 1759 SourceLocation TryLoc, 1760 Stmt *TryBlock, 1761 Stmt *Handler); 1762 1763 SourceRange getSourceRange() const LLVM_READONLY { 1764 return SourceRange(getTryLoc(), getEndLoc()); 1765 } 1766 1767 SourceLocation getTryLoc() const { return TryLoc; } 1768 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); } 1769 1770 bool getIsCXXTry() const { return IsCXXTry; } 1771 1772 CompoundStmt* getTryBlock() const { 1773 return llvm::cast<CompoundStmt>(Children[TRY]); 1774 } 1775 1776 Stmt *getHandler() const { return Children[HANDLER]; } 1777 1778 /// Returns 0 if not defined 1779 SEHExceptStmt *getExceptHandler() const; 1780 SEHFinallyStmt *getFinallyHandler() const; 1781 1782 child_range children() { 1783 return child_range(Children,Children+2); 1784 } 1785 1786 static bool classof(const Stmt *T) { 1787 return T->getStmtClass() == SEHTryStmtClass; 1788 } 1789 1790 static bool classof(SEHTryStmt *) { return true; } 1791}; 1792 1793} // end namespace clang 1794 1795#endif 1796