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