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