Stmt.h revision 025452fa0eda63e150cfaeebe64f0a19c96b3a06
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 virtual void Destroy(ASTContext& Ctx); 248 249 /// isSingleDecl - This method returns true if this DeclStmt refers 250 /// to a single Decl. 251 bool isSingleDecl() const { 252 return DG.isSingleDecl(); 253 } 254 255 const Decl *getSingleDecl() const { return DG.getSingleDecl(); } 256 Decl *getSingleDecl() { return DG.getSingleDecl(); } 257 258 const DeclGroupRef getDeclGroup() const { return DG; } 259 DeclGroupRef getDeclGroup() { return DG; } 260 261 SourceLocation getStartLoc() const { return StartLoc; } 262 SourceLocation getEndLoc() const { return EndLoc; } 263 264 SourceRange getSourceRange() const { 265 return SourceRange(StartLoc, EndLoc); 266 } 267 268 static bool classof(const Stmt *T) { 269 return T->getStmtClass() == DeclStmtClass; 270 } 271 static bool classof(const DeclStmt *) { return true; } 272 273 // Iterators over subexpressions. 274 virtual child_iterator child_begin(); 275 virtual child_iterator child_end(); 276 277 typedef DeclGroupRef::iterator decl_iterator; 278 typedef DeclGroupRef::const_iterator const_decl_iterator; 279 280 decl_iterator decl_begin() { return DG.begin(); } 281 decl_iterator decl_end() { return DG.end(); } 282 const_decl_iterator decl_begin() const { return DG.begin(); } 283 const_decl_iterator decl_end() const { return DG.end(); } 284 285 // Serialization. 286 virtual void EmitImpl(llvm::Serializer& S) const; 287 static DeclStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 288}; 289 290/// NullStmt - This is the null statement ";": C99 6.8.3p3. 291/// 292class NullStmt : public Stmt { 293 SourceLocation SemiLoc; 294public: 295 NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {} 296 297 /// \brief Build an empty null statement. 298 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { } 299 300 SourceLocation getSemiLoc() const { return SemiLoc; } 301 void setSemiLoc(SourceLocation L) { SemiLoc = L; } 302 303 virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); } 304 305 static bool classof(const Stmt *T) { 306 return T->getStmtClass() == NullStmtClass; 307 } 308 static bool classof(const NullStmt *) { return true; } 309 310 // Iterators 311 virtual child_iterator child_begin(); 312 virtual child_iterator child_end(); 313 314 virtual void EmitImpl(llvm::Serializer& S) const; 315 static NullStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 316}; 317 318/// CompoundStmt - This represents a group of statements like { stmt stmt }. 319/// 320class CompoundStmt : public Stmt { 321 Stmt** Body; 322 unsigned NumStmts; 323 SourceLocation LBracLoc, RBracLoc; 324public: 325 CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts, 326 SourceLocation LB, SourceLocation RB) 327 : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) { 328 if (NumStmts == 0) { 329 Body = 0; 330 return; 331 } 332 333 Body = new (C) Stmt*[NumStmts]; 334 memcpy(Body, StmtStart, numStmts * sizeof(*Body)); 335 } 336 337 // \brief Build an empty compound statement. 338 explicit CompoundStmt(EmptyShell Empty) 339 : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { } 340 341 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts); 342 343 bool body_empty() const { return NumStmts == 0; } 344 unsigned size() const { return NumStmts; } 345 346 typedef Stmt** body_iterator; 347 body_iterator body_begin() { return Body; } 348 body_iterator body_end() { return Body + NumStmts; } 349 Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; } 350 351 typedef Stmt* const * const_body_iterator; 352 const_body_iterator body_begin() const { return Body; } 353 const_body_iterator body_end() const { return Body + NumStmts; } 354 const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; } 355 356 typedef std::reverse_iterator<body_iterator> reverse_body_iterator; 357 reverse_body_iterator body_rbegin() { 358 return reverse_body_iterator(body_end()); 359 } 360 reverse_body_iterator body_rend() { 361 return reverse_body_iterator(body_begin()); 362 } 363 364 typedef std::reverse_iterator<const_body_iterator> 365 const_reverse_body_iterator; 366 367 const_reverse_body_iterator body_rbegin() const { 368 return const_reverse_body_iterator(body_end()); 369 } 370 371 const_reverse_body_iterator body_rend() const { 372 return const_reverse_body_iterator(body_begin()); 373 } 374 375 virtual SourceRange getSourceRange() const { 376 return SourceRange(LBracLoc, RBracLoc); 377 } 378 379 SourceLocation getLBracLoc() const { return LBracLoc; } 380 void setLBracLoc(SourceLocation L) { LBracLoc = L; } 381 SourceLocation getRBracLoc() const { return RBracLoc; } 382 void setRBracLoc(SourceLocation L) { RBracLoc = L; } 383 384 static bool classof(const Stmt *T) { 385 return T->getStmtClass() == CompoundStmtClass; 386 } 387 static bool classof(const CompoundStmt *) { return true; } 388 389 // Iterators 390 virtual child_iterator child_begin(); 391 virtual child_iterator child_end(); 392 393 virtual void EmitImpl(llvm::Serializer& S) const; 394 static CompoundStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 395}; 396 397// SwitchCase is the base class for CaseStmt and DefaultStmt, 398class SwitchCase : public Stmt { 399protected: 400 // A pointer to the following CaseStmt or DefaultStmt class, 401 // used by SwitchStmt. 402 SwitchCase *NextSwitchCase; 403 404 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {} 405 406public: 407 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 408 409 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 410 411 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 412 413 Stmt *getSubStmt() { return v_getSubStmt(); } 414 415 virtual SourceRange getSourceRange() const { return SourceRange(); } 416 417 static bool classof(const Stmt *T) { 418 return T->getStmtClass() == CaseStmtClass || 419 T->getStmtClass() == DefaultStmtClass; 420 } 421 static bool classof(const SwitchCase *) { return true; } 422protected: 423 virtual Stmt* v_getSubStmt() = 0; 424}; 425 426class CaseStmt : public SwitchCase { 427 enum { SUBSTMT, LHS, RHS, END_EXPR }; 428 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for 429 // GNU "case 1 ... 4" extension 430 SourceLocation CaseLoc; 431 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 432public: 433 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc) 434 : SwitchCase(CaseStmtClass) { 435 SubExprs[SUBSTMT] = 0; 436 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs); 437 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs); 438 CaseLoc = caseLoc; 439 } 440 441 /// \brief Build an empty switch case statement. 442 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { } 443 444 SourceLocation getCaseLoc() const { return CaseLoc; } 445 void setCaseLoc(SourceLocation L) { CaseLoc = L; } 446 447 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); } 448 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); } 449 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } 450 451 const Expr *getLHS() const { 452 return reinterpret_cast<const Expr*>(SubExprs[LHS]); 453 } 454 const Expr *getRHS() const { 455 return reinterpret_cast<const Expr*>(SubExprs[RHS]); 456 } 457 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; } 458 459 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; } 460 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); } 461 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } 462 463 464 virtual SourceRange getSourceRange() const { 465 // Handle deeply nested case statements with iteration instead of recursion. 466 const CaseStmt *CS = this; 467 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) 468 CS = CS2; 469 470 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd()); 471 } 472 static bool classof(const Stmt *T) { 473 return T->getStmtClass() == CaseStmtClass; 474 } 475 static bool classof(const CaseStmt *) { return true; } 476 477 // Iterators 478 virtual child_iterator child_begin(); 479 virtual child_iterator child_end(); 480 481 virtual void EmitImpl(llvm::Serializer& S) const; 482 static CaseStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 483}; 484 485class DefaultStmt : public SwitchCase { 486 Stmt* SubStmt; 487 SourceLocation DefaultLoc; 488 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 489public: 490 DefaultStmt(SourceLocation DL, Stmt *substmt) : 491 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {} 492 493 /// \brief Build an empty default statement. 494 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { } 495 496 Stmt *getSubStmt() { return SubStmt; } 497 const Stmt *getSubStmt() const { return SubStmt; } 498 void setSubStmt(Stmt *S) { SubStmt = S; } 499 500 SourceLocation getDefaultLoc() const { return DefaultLoc; } 501 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; } 502 503 virtual SourceRange getSourceRange() const { 504 return SourceRange(DefaultLoc, SubStmt->getLocEnd()); 505 } 506 static bool classof(const Stmt *T) { 507 return T->getStmtClass() == DefaultStmtClass; 508 } 509 static bool classof(const DefaultStmt *) { return true; } 510 511 // Iterators 512 virtual child_iterator child_begin(); 513 virtual child_iterator child_end(); 514 515 virtual void EmitImpl(llvm::Serializer& S) const; 516 static DefaultStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 517}; 518 519class LabelStmt : public Stmt { 520 IdentifierInfo *Label; 521 Stmt *SubStmt; 522 SourceLocation IdentLoc; 523public: 524 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) 525 : Stmt(LabelStmtClass), Label(label), 526 SubStmt(substmt), IdentLoc(IL) {} 527 528 SourceLocation getIdentLoc() const { return IdentLoc; } 529 IdentifierInfo *getID() const { return Label; } 530 const char *getName() const; 531 Stmt *getSubStmt() { return SubStmt; } 532 const Stmt *getSubStmt() const { return SubStmt; } 533 534 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 535 void setSubStmt(Stmt *SS) { SubStmt = SS; } 536 537 virtual SourceRange getSourceRange() const { 538 return SourceRange(IdentLoc, SubStmt->getLocEnd()); 539 } 540 static bool classof(const Stmt *T) { 541 return T->getStmtClass() == LabelStmtClass; 542 } 543 static bool classof(const LabelStmt *) { return true; } 544 545 // Iterators 546 virtual child_iterator child_begin(); 547 virtual child_iterator child_end(); 548 549 virtual void EmitImpl(llvm::Serializer& S) const; 550 static LabelStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 551}; 552 553 554/// IfStmt - This represents an if/then/else. 555/// 556class IfStmt : public Stmt { 557 enum { COND, THEN, ELSE, END_EXPR }; 558 Stmt* SubExprs[END_EXPR]; 559 SourceLocation IfLoc; 560public: 561 IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0) 562 : Stmt(IfStmtClass) { 563 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 564 SubExprs[THEN] = then; 565 SubExprs[ELSE] = elsev; 566 IfLoc = IL; 567 } 568 569 /// \brief Build an empty if/then/else statement 570 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { } 571 572 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 573 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 574 const Stmt *getThen() const { return SubExprs[THEN]; } 575 void setThen(Stmt *S) { SubExprs[THEN] = S; } 576 const Stmt *getElse() const { return SubExprs[ELSE]; } 577 void setElse(Stmt *S) { SubExprs[ELSE] = S; } 578 579 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 580 Stmt *getThen() { return SubExprs[THEN]; } 581 Stmt *getElse() { return SubExprs[ELSE]; } 582 583 SourceLocation getIfLoc() const { return IfLoc; } 584 void setIfLoc(SourceLocation L) { IfLoc = L; } 585 586 virtual SourceRange getSourceRange() const { 587 if (SubExprs[ELSE]) 588 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); 589 else 590 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); 591 } 592 593 static bool classof(const Stmt *T) { 594 return T->getStmtClass() == IfStmtClass; 595 } 596 static bool classof(const IfStmt *) { return true; } 597 598 // Iterators 599 virtual child_iterator child_begin(); 600 virtual child_iterator child_end(); 601 602 virtual void EmitImpl(llvm::Serializer& S) const; 603 static IfStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 604}; 605 606/// SwitchStmt - This represents a 'switch' stmt. 607/// 608class SwitchStmt : public Stmt { 609 enum { COND, BODY, END_EXPR }; 610 Stmt* SubExprs[END_EXPR]; 611 // This points to a linked list of case and default statements. 612 SwitchCase *FirstCase; 613 SourceLocation SwitchLoc; 614public: 615 SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) { 616 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 617 SubExprs[BODY] = NULL; 618 } 619 620 /// \brief Build a empty switch statement. 621 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { } 622 623 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 624 const Stmt *getBody() const { return SubExprs[BODY]; } 625 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 626 627 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 628 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 629 Stmt *getBody() { return SubExprs[BODY]; } 630 void setBody(Stmt *S) { SubExprs[BODY] = S; } 631 SwitchCase *getSwitchCaseList() { return FirstCase; } 632 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } 633 634 SourceLocation getSwitchLoc() const { return SwitchLoc; } 635 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; } 636 637 void setBody(Stmt *S, SourceLocation SL) { 638 SubExprs[BODY] = S; 639 SwitchLoc = SL; 640 } 641 void addSwitchCase(SwitchCase *SC) { 642 assert(!SC->getNextSwitchCase() && "case/default already added to a switch"); 643 SC->setNextSwitchCase(FirstCase); 644 FirstCase = SC; 645 } 646 virtual SourceRange getSourceRange() const { 647 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); 648 } 649 static bool classof(const Stmt *T) { 650 return T->getStmtClass() == SwitchStmtClass; 651 } 652 static bool classof(const SwitchStmt *) { return true; } 653 654 // Iterators 655 virtual child_iterator child_begin(); 656 virtual child_iterator child_end(); 657 658 virtual void EmitImpl(llvm::Serializer& S) const; 659 static SwitchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 660}; 661 662 663/// WhileStmt - This represents a 'while' stmt. 664/// 665class WhileStmt : public Stmt { 666 enum { COND, BODY, END_EXPR }; 667 Stmt* SubExprs[END_EXPR]; 668 SourceLocation WhileLoc; 669public: 670 WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) { 671 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 672 SubExprs[BODY] = body; 673 WhileLoc = WL; 674 } 675 676 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 677 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 678 Stmt *getBody() { return SubExprs[BODY]; } 679 const Stmt *getBody() const { return SubExprs[BODY]; } 680 681 virtual SourceRange getSourceRange() const { 682 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); 683 } 684 static bool classof(const Stmt *T) { 685 return T->getStmtClass() == WhileStmtClass; 686 } 687 static bool classof(const WhileStmt *) { return true; } 688 689 // Iterators 690 virtual child_iterator child_begin(); 691 virtual child_iterator child_end(); 692 693 virtual void EmitImpl(llvm::Serializer& S) const; 694 static WhileStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 695}; 696 697/// DoStmt - This represents a 'do/while' stmt. 698/// 699class DoStmt : public Stmt { 700 enum { COND, BODY, END_EXPR }; 701 Stmt* SubExprs[END_EXPR]; 702 SourceLocation DoLoc; 703public: 704 DoStmt(Stmt *body, Expr *cond, SourceLocation DL) 705 : Stmt(DoStmtClass), DoLoc(DL) { 706 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 707 SubExprs[BODY] = body; 708 DoLoc = DL; 709 } 710 711 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 712 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 713 Stmt *getBody() { return SubExprs[BODY]; } 714 const Stmt *getBody() const { return SubExprs[BODY]; } 715 716 virtual SourceRange getSourceRange() const { 717 return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 718 } 719 static bool classof(const Stmt *T) { 720 return T->getStmtClass() == DoStmtClass; 721 } 722 static bool classof(const DoStmt *) { return true; } 723 724 // Iterators 725 virtual child_iterator child_begin(); 726 virtual child_iterator child_end(); 727 728 virtual void EmitImpl(llvm::Serializer& S) const; 729 static DoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 730}; 731 732 733/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 734/// the init/cond/inc parts of the ForStmt will be null if they were not 735/// specified in the source. 736/// 737class ForStmt : public Stmt { 738 enum { INIT, COND, INC, BODY, END_EXPR }; 739 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 740 SourceLocation ForLoc; 741public: 742 ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL) 743 : Stmt(ForStmtClass) { 744 SubExprs[INIT] = Init; 745 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 746 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 747 SubExprs[BODY] = Body; 748 ForLoc = FL; 749 } 750 751 Stmt *getInit() { return SubExprs[INIT]; } 752 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 753 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 754 Stmt *getBody() { return SubExprs[BODY]; } 755 756 const Stmt *getInit() const { return SubExprs[INIT]; } 757 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 758 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 759 const Stmt *getBody() const { return SubExprs[BODY]; } 760 761 virtual SourceRange getSourceRange() const { 762 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 763 } 764 static bool classof(const Stmt *T) { 765 return T->getStmtClass() == ForStmtClass; 766 } 767 static bool classof(const ForStmt *) { return true; } 768 769 // Iterators 770 virtual child_iterator child_begin(); 771 virtual child_iterator child_end(); 772 773 virtual void EmitImpl(llvm::Serializer& S) const; 774 static ForStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 775}; 776 777/// GotoStmt - This represents a direct goto. 778/// 779class GotoStmt : public Stmt { 780 LabelStmt *Label; 781 SourceLocation GotoLoc; 782 SourceLocation LabelLoc; 783public: 784 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL) 785 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 786 787 LabelStmt *getLabel() const { return Label; } 788 789 virtual SourceRange getSourceRange() const { 790 return SourceRange(GotoLoc, LabelLoc); 791 } 792 static bool classof(const Stmt *T) { 793 return T->getStmtClass() == GotoStmtClass; 794 } 795 static bool classof(const GotoStmt *) { return true; } 796 797 // Iterators 798 virtual child_iterator child_begin(); 799 virtual child_iterator child_end(); 800 801 virtual void EmitImpl(llvm::Serializer& S) const; 802 static GotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 803}; 804 805/// IndirectGotoStmt - This represents an indirect goto. 806/// 807class IndirectGotoStmt : public Stmt { 808 Stmt *Target; 809 // FIXME: Add location information (e.g. SourceLocation objects). 810 // When doing so, update the serialization routines. 811public: 812 IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass), 813 Target((Stmt*)target){} 814 815 Expr *getTarget(); 816 const Expr *getTarget() const; 817 818 virtual SourceRange getSourceRange() const { return SourceRange(); } 819 820 static bool classof(const Stmt *T) { 821 return T->getStmtClass() == IndirectGotoStmtClass; 822 } 823 static bool classof(const IndirectGotoStmt *) { return true; } 824 825 // Iterators 826 virtual child_iterator child_begin(); 827 virtual child_iterator child_end(); 828 829 virtual void EmitImpl(llvm::Serializer& S) const; 830 static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 831}; 832 833 834/// ContinueStmt - This represents a continue. 835/// 836class ContinueStmt : public Stmt { 837 SourceLocation ContinueLoc; 838public: 839 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 840 841 virtual SourceRange getSourceRange() const { 842 return SourceRange(ContinueLoc); 843 } 844 static bool classof(const Stmt *T) { 845 return T->getStmtClass() == ContinueStmtClass; 846 } 847 static bool classof(const ContinueStmt *) { return true; } 848 849 // Iterators 850 virtual child_iterator child_begin(); 851 virtual child_iterator child_end(); 852 853 virtual void EmitImpl(llvm::Serializer& S) const; 854 static ContinueStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 855}; 856 857/// BreakStmt - This represents a break. 858/// 859class BreakStmt : public Stmt { 860 SourceLocation BreakLoc; 861public: 862 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 863 864 /// \brief Build an empty break statement. 865 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { } 866 867 SourceLocation getBreakLoc() const { return BreakLoc; } 868 void setBreakLoc(SourceLocation L) { BreakLoc = L; } 869 870 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } 871 872 static bool classof(const Stmt *T) { 873 return T->getStmtClass() == BreakStmtClass; 874 } 875 static bool classof(const BreakStmt *) { return true; } 876 877 // Iterators 878 virtual child_iterator child_begin(); 879 virtual child_iterator child_end(); 880 881 virtual void EmitImpl(llvm::Serializer& S) const; 882 static BreakStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 883}; 884 885 886/// ReturnStmt - This represents a return, optionally of an expression: 887/// return; 888/// return 4; 889/// 890/// Note that GCC allows return with no argument in a function declared to 891/// return a value, and it allows returning a value in functions declared to 892/// return void. We explicitly model this in the AST, which means you can't 893/// depend on the return type of the function and the presence of an argument. 894/// 895class ReturnStmt : public Stmt { 896 Stmt *RetExpr; 897 SourceLocation RetLoc; 898public: 899 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), 900 RetExpr((Stmt*) E), RetLoc(RL) {} 901 902 const Expr *getRetValue() const; 903 Expr *getRetValue(); 904 905 virtual SourceRange getSourceRange() const; 906 907 static bool classof(const Stmt *T) { 908 return T->getStmtClass() == ReturnStmtClass; 909 } 910 static bool classof(const ReturnStmt *) { return true; } 911 912 // Iterators 913 virtual child_iterator child_begin(); 914 virtual child_iterator child_end(); 915 916 virtual void EmitImpl(llvm::Serializer& S) const; 917 static ReturnStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 918}; 919 920/// AsmStmt - This represents a GNU inline-assembly statement extension. 921/// 922class AsmStmt : public Stmt { 923 SourceLocation AsmLoc, RParenLoc; 924 StringLiteral *AsmStr; 925 926 bool IsSimple; 927 bool IsVolatile; 928 929 unsigned NumOutputs; 930 unsigned NumInputs; 931 932 llvm::SmallVector<std::string, 4> Names; 933 llvm::SmallVector<StringLiteral*, 4> Constraints; 934 llvm::SmallVector<Stmt*, 4> Exprs; 935 936 llvm::SmallVector<StringLiteral*, 4> Clobbers; 937public: 938 AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile, 939 unsigned numoutputs, unsigned numinputs, 940 std::string *names, StringLiteral **constraints, 941 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers, 942 StringLiteral **clobbers, SourceLocation rparenloc); 943 944 bool isVolatile() const { return IsVolatile; } 945 bool isSimple() const { return IsSimple; } 946 947 //===--- Asm String Analysis ---===// 948 949 const StringLiteral *getAsmString() const { return AsmStr; } 950 StringLiteral *getAsmString() { return AsmStr; } 951 952 /// AsmStringPiece - this is part of a decomposed asm string specification 953 /// (for use with the AnalyzeAsmString function below). An asm string is 954 /// considered to be a concatenation of these parts. 955 class AsmStringPiece { 956 public: 957 enum Kind { 958 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 959 Operand // Operand reference, with optional modifier %c4. 960 }; 961 private: 962 Kind MyKind; 963 std::string Str; 964 unsigned OperandNo; 965 public: 966 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 967 AsmStringPiece(unsigned OpNo, char Modifier) 968 : MyKind(Operand), Str(), OperandNo(OpNo) { 969 Str += Modifier; 970 } 971 972 bool isString() const { return MyKind == String; } 973 bool isOperand() const { return MyKind == Operand; } 974 975 const std::string &getString() const { 976 assert(isString()); 977 return Str; 978 } 979 980 unsigned getOperandNo() const { 981 assert(isOperand()); 982 return OperandNo; 983 } 984 985 /// getModifier - Get the modifier for this operand, if present. This 986 /// returns '\0' if there was no modifier. 987 char getModifier() const { 988 assert(isOperand()); 989 return Str[0]; 990 } 991 }; 992 993 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 994 /// it into pieces. If the asm string is erroneous, emit errors and return 995 /// true, otherwise return false. This handles canonicalization and 996 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 997 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 998 unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces, 999 ASTContext &C, unsigned &DiagOffs) const; 1000 1001 1002 //===--- Output operands ---===// 1003 1004 unsigned getNumOutputs() const { return NumOutputs; } 1005 1006 const std::string &getOutputName(unsigned i) const { 1007 return Names[i]; 1008 } 1009 1010 /// getOutputConstraint - Return the constraint string for the specified 1011 /// output operand. All output constraints are known to be non-empty (either 1012 /// '=' or '+'). 1013 std::string getOutputConstraint(unsigned i) const; 1014 1015 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 1016 return Constraints[i]; 1017 } 1018 StringLiteral *getOutputConstraintLiteral(unsigned i) { 1019 return Constraints[i]; 1020 } 1021 1022 1023 Expr *getOutputExpr(unsigned i); 1024 1025 const Expr *getOutputExpr(unsigned i) const { 1026 return const_cast<AsmStmt*>(this)->getOutputExpr(i); 1027 } 1028 1029 /// isOutputPlusConstraint - Return true if the specified output constraint 1030 /// is a "+" constraint (which is both an input and an output) or false if it 1031 /// is an "=" constraint (just an output). 1032 bool isOutputPlusConstraint(unsigned i) const { 1033 return getOutputConstraint(i)[0] == '+'; 1034 } 1035 1036 /// getNumPlusOperands - Return the number of output operands that have a "+" 1037 /// constraint. 1038 unsigned getNumPlusOperands() const; 1039 1040 //===--- Input operands ---===// 1041 1042 unsigned getNumInputs() const { return NumInputs; } 1043 1044 const std::string &getInputName(unsigned i) const { 1045 return Names[i + NumOutputs]; 1046 } 1047 1048 /// getInputConstraint - Return the specified input constraint. Unlike output 1049 /// constraints, these can be empty. 1050 std::string getInputConstraint(unsigned i) const; 1051 1052 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 1053 return Constraints[i + NumOutputs]; 1054 } 1055 StringLiteral *getInputConstraintLiteral(unsigned i) { 1056 return Constraints[i + NumOutputs]; 1057 } 1058 1059 1060 Expr *getInputExpr(unsigned i); 1061 1062 const Expr *getInputExpr(unsigned i) const { 1063 return const_cast<AsmStmt*>(this)->getInputExpr(i); 1064 } 1065 1066 //===--- Other ---===// 1067 1068 /// getNamedOperand - Given a symbolic operand reference like %[foo], 1069 /// translate this into a numeric value needed to reference the same operand. 1070 /// This returns -1 if the operand name is invalid. 1071 int getNamedOperand(const std::string &SymbolicName) const; 1072 1073 1074 1075 unsigned getNumClobbers() const { return Clobbers.size(); } 1076 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 1077 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 1078 1079 virtual SourceRange getSourceRange() const { 1080 return SourceRange(AsmLoc, RParenLoc); 1081 } 1082 1083 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 1084 static bool classof(const AsmStmt *) { return true; } 1085 1086 // Input expr iterators. 1087 1088 typedef ExprIterator inputs_iterator; 1089 typedef ConstExprIterator const_inputs_iterator; 1090 1091 inputs_iterator begin_inputs() { 1092 return &Exprs[0] + NumOutputs; 1093 } 1094 1095 inputs_iterator end_inputs() { 1096 return &Exprs[0] + NumOutputs + NumInputs; 1097 } 1098 1099 const_inputs_iterator begin_inputs() const { 1100 return &Exprs[0] + NumOutputs; 1101 } 1102 1103 const_inputs_iterator end_inputs() const { 1104 return &Exprs[0] + NumOutputs + NumInputs;} 1105 1106 // Output expr iterators. 1107 1108 typedef ExprIterator outputs_iterator; 1109 typedef ConstExprIterator const_outputs_iterator; 1110 1111 outputs_iterator begin_outputs() { return &Exprs[0]; } 1112 outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; } 1113 1114 const_outputs_iterator begin_outputs() const { return &Exprs[0]; } 1115 const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; } 1116 1117 // Input name iterator. 1118 1119 const std::string *begin_output_names() const { 1120 return &Names[0]; 1121 } 1122 1123 const std::string *end_output_names() const { 1124 return &Names[0] + NumOutputs; 1125 } 1126 1127 // Child iterators 1128 1129 virtual child_iterator child_begin(); 1130 virtual child_iterator child_end(); 1131 1132 virtual void EmitImpl(llvm::Serializer& S) const; 1133 static AsmStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1134}; 1135 1136/// ObjCForCollectionStmt - This represents Objective-c's collection statement; 1137/// represented as 'for (element 'in' collection-expression)' stmt. 1138/// 1139class ObjCForCollectionStmt : public Stmt { 1140 enum { ELEM, COLLECTION, BODY, END_EXPR }; 1141 Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt. 1142 SourceLocation ForLoc; 1143 SourceLocation RParenLoc; 1144public: 1145 ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body, 1146 SourceLocation FCL, SourceLocation RPL); 1147 1148 Stmt *getElement() { return SubExprs[ELEM]; } 1149 Expr *getCollection() { 1150 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 1151 } 1152 Stmt *getBody() { return SubExprs[BODY]; } 1153 1154 const Stmt *getElement() const { return SubExprs[ELEM]; } 1155 const Expr *getCollection() const { 1156 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 1157 } 1158 const Stmt *getBody() const { return SubExprs[BODY]; } 1159 1160 SourceLocation getRParenLoc() const { return RParenLoc; } 1161 1162 virtual SourceRange getSourceRange() const { 1163 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 1164 } 1165 static bool classof(const Stmt *T) { 1166 return T->getStmtClass() == ObjCForCollectionStmtClass; 1167 } 1168 static bool classof(const ObjCForCollectionStmt *) { return true; } 1169 1170 // Iterators 1171 virtual child_iterator child_begin(); 1172 virtual child_iterator child_end(); 1173 1174 virtual void EmitImpl(llvm::Serializer& S) const; 1175 static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1176}; 1177 1178/// ObjCAtCatchStmt - This represents objective-c's @catch statement. 1179class ObjCAtCatchStmt : public Stmt { 1180private: 1181 enum { BODY, NEXT_CATCH, END_EXPR }; 1182 ParmVarDecl *ExceptionDecl; 1183 Stmt *SubExprs[END_EXPR]; 1184 SourceLocation AtCatchLoc, RParenLoc; 1185 1186 // Used by deserialization. 1187 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc) 1188 : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {} 1189 1190public: 1191 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc, 1192 ParmVarDecl *catchVarDecl, 1193 Stmt *atCatchStmt, Stmt *atCatchList); 1194 1195 const Stmt *getCatchBody() const { return SubExprs[BODY]; } 1196 Stmt *getCatchBody() { return SubExprs[BODY]; } 1197 1198 const ObjCAtCatchStmt *getNextCatchStmt() const { 1199 return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]); 1200 } 1201 ObjCAtCatchStmt *getNextCatchStmt() { 1202 return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]); 1203 } 1204 1205 const ParmVarDecl *getCatchParamDecl() const { 1206 return ExceptionDecl; 1207 } 1208 ParmVarDecl *getCatchParamDecl() { 1209 return ExceptionDecl; 1210 } 1211 1212 SourceLocation getRParenLoc() const { return RParenLoc; } 1213 1214 virtual SourceRange getSourceRange() const { 1215 return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd()); 1216 } 1217 1218 bool hasEllipsis() const { return getCatchParamDecl() == 0; } 1219 1220 static bool classof(const Stmt *T) { 1221 return T->getStmtClass() == ObjCAtCatchStmtClass; 1222 } 1223 static bool classof(const ObjCAtCatchStmt *) { return true; } 1224 1225 virtual child_iterator child_begin(); 1226 virtual child_iterator child_end(); 1227 1228 virtual void EmitImpl(llvm::Serializer& S) const; 1229 static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1230}; 1231 1232/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement 1233class ObjCAtFinallyStmt : public Stmt { 1234 Stmt *AtFinallyStmt; 1235 SourceLocation AtFinallyLoc; 1236public: 1237 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt) 1238 : Stmt(ObjCAtFinallyStmtClass), 1239 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {} 1240 1241 const Stmt *getFinallyBody () const { return AtFinallyStmt; } 1242 Stmt *getFinallyBody () { return AtFinallyStmt; } 1243 1244 virtual SourceRange getSourceRange() const { 1245 return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd()); 1246 } 1247 1248 static bool classof(const Stmt *T) { 1249 return T->getStmtClass() == ObjCAtFinallyStmtClass; 1250 } 1251 static bool classof(const ObjCAtFinallyStmt *) { return true; } 1252 1253 virtual child_iterator child_begin(); 1254 virtual child_iterator child_end(); 1255 1256 virtual void EmitImpl(llvm::Serializer& S) const; 1257 static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1258}; 1259 1260/// ObjCAtTryStmt - This represent objective-c's over-all 1261/// @try ... @catch ... @finally statement. 1262class ObjCAtTryStmt : public Stmt { 1263private: 1264 enum { TRY, CATCH, FINALLY, END_EXPR }; 1265 Stmt* SubStmts[END_EXPR]; 1266 1267 SourceLocation AtTryLoc; 1268public: 1269 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 1270 Stmt *atCatchStmt, 1271 Stmt *atFinallyStmt) 1272 : Stmt(ObjCAtTryStmtClass) { 1273 SubStmts[TRY] = atTryStmt; 1274 SubStmts[CATCH] = atCatchStmt; 1275 SubStmts[FINALLY] = atFinallyStmt; 1276 AtTryLoc = atTryLoc; 1277 } 1278 1279 const Stmt *getTryBody() const { return SubStmts[TRY]; } 1280 Stmt *getTryBody() { return SubStmts[TRY]; } 1281 const ObjCAtCatchStmt *getCatchStmts() const { 1282 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 1283 } 1284 ObjCAtCatchStmt *getCatchStmts() { 1285 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 1286 } 1287 const ObjCAtFinallyStmt *getFinallyStmt() const { 1288 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 1289 } 1290 ObjCAtFinallyStmt *getFinallyStmt() { 1291 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 1292 } 1293 virtual SourceRange getSourceRange() const { 1294 return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd()); 1295 } 1296 1297 static bool classof(const Stmt *T) { 1298 return T->getStmtClass() == ObjCAtTryStmtClass; 1299 } 1300 static bool classof(const ObjCAtTryStmt *) { return true; } 1301 1302 virtual child_iterator child_begin(); 1303 virtual child_iterator child_end(); 1304 1305 virtual void EmitImpl(llvm::Serializer& S) const; 1306 static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1307}; 1308 1309/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement. 1310/// Example: @synchronized (sem) { 1311/// do-something; 1312/// } 1313/// 1314class ObjCAtSynchronizedStmt : public Stmt { 1315private: 1316 enum { SYNC_EXPR, SYNC_BODY, END_EXPR }; 1317 Stmt* SubStmts[END_EXPR]; 1318 SourceLocation AtSynchronizedLoc; 1319 1320public: 1321 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr, 1322 Stmt *synchBody) 1323 : Stmt(ObjCAtSynchronizedStmtClass) { 1324 SubStmts[SYNC_EXPR] = synchExpr; 1325 SubStmts[SYNC_BODY] = synchBody; 1326 AtSynchronizedLoc = atSynchronizedLoc; 1327 } 1328 1329 const CompoundStmt *getSynchBody() const { 1330 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]); 1331 } 1332 CompoundStmt *getSynchBody() { 1333 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]); 1334 } 1335 1336 const Expr *getSynchExpr() const { 1337 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]); 1338 } 1339 Expr *getSynchExpr() { 1340 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]); 1341 } 1342 1343 virtual SourceRange getSourceRange() const { 1344 return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd()); 1345 } 1346 1347 static bool classof(const Stmt *T) { 1348 return T->getStmtClass() == ObjCAtSynchronizedStmtClass; 1349 } 1350 static bool classof(const ObjCAtSynchronizedStmt *) { return true; } 1351 1352 virtual child_iterator child_begin(); 1353 virtual child_iterator child_end(); 1354 1355 virtual void EmitImpl(llvm::Serializer& S) const; 1356 static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D, 1357 ASTContext& C); 1358}; 1359 1360/// ObjCAtThrowStmt - This represents objective-c's @throw statement. 1361class ObjCAtThrowStmt : public Stmt { 1362 Stmt *Throw; 1363 SourceLocation AtThrowLoc; 1364public: 1365 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr) 1366 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) { 1367 AtThrowLoc = atThrowLoc; 1368 } 1369 1370 const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); } 1371 Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); } 1372 1373 virtual SourceRange getSourceRange() const { 1374 if (Throw) 1375 return SourceRange(AtThrowLoc, Throw->getLocEnd()); 1376 else 1377 return SourceRange(AtThrowLoc); 1378 } 1379 1380 static bool classof(const Stmt *T) { 1381 return T->getStmtClass() == ObjCAtThrowStmtClass; 1382 } 1383 static bool classof(const ObjCAtThrowStmt *) { return true; } 1384 1385 virtual child_iterator child_begin(); 1386 virtual child_iterator child_end(); 1387 1388 virtual void EmitImpl(llvm::Serializer& S) const; 1389 static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1390}; 1391 1392/// CXXCatchStmt - This represents a C++ catch block. 1393class CXXCatchStmt : public Stmt { 1394 SourceLocation CatchLoc; 1395 /// The exception-declaration of the type. 1396 Decl *ExceptionDecl; 1397 /// The handler block. 1398 Stmt *HandlerBlock; 1399 1400public: 1401 CXXCatchStmt(SourceLocation catchLoc, Decl *exDecl, Stmt *handlerBlock) 1402 : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl), 1403 HandlerBlock(handlerBlock) {} 1404 1405 virtual void Destroy(ASTContext& Ctx); 1406 1407 virtual SourceRange getSourceRange() const { 1408 return SourceRange(CatchLoc, HandlerBlock->getLocEnd()); 1409 } 1410 1411 Decl *getExceptionDecl() { return ExceptionDecl; } 1412 QualType getCaughtType(); 1413 Stmt *getHandlerBlock() { return HandlerBlock; } 1414 1415 static bool classof(const Stmt *T) { 1416 return T->getStmtClass() == CXXCatchStmtClass; 1417 } 1418 static bool classof(const CXXCatchStmt *) { return true; } 1419 1420 virtual child_iterator child_begin(); 1421 virtual child_iterator child_end(); 1422 1423 virtual void EmitImpl(llvm::Serializer& S) const; 1424 static CXXCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1425}; 1426 1427/// CXXTryStmt - A C++ try block, including all handlers. 1428class CXXTryStmt : public Stmt { 1429 SourceLocation TryLoc; 1430 // First place is the guarded CompoundStatement. Subsequent are the handlers. 1431 // More than three handlers should be rare. 1432 llvm::SmallVector<Stmt*, 4> Stmts; 1433 1434public: 1435 CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, 1436 Stmt **handlers, unsigned numHandlers); 1437 1438 virtual SourceRange getSourceRange() const { 1439 return SourceRange(TryLoc, Stmts.back()->getLocEnd()); 1440 } 1441 1442 CompoundStmt *getTryBlock() { return llvm::cast<CompoundStmt>(Stmts[0]); } 1443 const CompoundStmt *getTryBlock() const { 1444 return llvm::cast<CompoundStmt>(Stmts[0]); 1445 } 1446 1447 unsigned getNumHandlers() const { return Stmts.size() - 1; } 1448 CXXCatchStmt *getHandler(unsigned i) { 1449 return llvm::cast<CXXCatchStmt>(Stmts[i + 1]); 1450 } 1451 const CXXCatchStmt *getHandler(unsigned i) const { 1452 return llvm::cast<CXXCatchStmt>(Stmts[i + 1]); 1453 } 1454 1455 static bool classof(const Stmt *T) { 1456 return T->getStmtClass() == CXXTryStmtClass; 1457 } 1458 static bool classof(const CXXTryStmt *) { return true; } 1459 1460 virtual child_iterator child_begin(); 1461 virtual child_iterator child_end(); 1462 1463 virtual void EmitImpl(llvm::Serializer& S) const; 1464 static CXXTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1465}; 1466 1467} // end namespace clang 1468 1469#endif 1470