Stmt.h revision 20dabe8b8ff7ed6d2d0158fee43755a4bc7642c3
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 "clang/Basic/SourceLocation.h" 19#include "clang/AST/StmtIterator.h" 20#include "llvm/ADT/SmallVector.h" 21#include "llvm/ADT/iterator" 22#include "llvm/Bitcode/SerializationFwd.h" 23#include <iosfwd> 24#include <string> 25 26using llvm::dyn_cast_or_null; 27 28namespace clang { 29 class Expr; 30 class Decl; 31 class ScopedDecl; 32 class IdentifierInfo; 33 class SourceManager; 34 class StringLiteral; 35 class SwitchStmt; 36 class PrinterHelper; 37 38/// Stmt - This represents one statement. 39/// 40class Stmt { 41public: 42 enum StmtClass { 43#define STMT(N, CLASS, PARENT) CLASS##Class = N, 44#define FIRST_STMT(N) firstStmtConstant = N, 45#define LAST_STMT(N) lastStmtConstant = N, 46#define FIRST_EXPR(N) firstExprConstant = N, 47#define LAST_EXPR(N) lastExprConstant = N 48#include "clang/AST/StmtNodes.def" 49}; 50private: 51 const StmtClass sClass; 52public: 53 Stmt(StmtClass SC) : sClass(SC) { 54 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC); 55 } 56 virtual ~Stmt() {} 57 58 StmtClass getStmtClass() const { return sClass; } 59 const char *getStmtClassName() const; 60 61 /// SourceLocation tokens are not useful in isolation - they are low level 62 /// value objects created/interpreted by SourceManager. We assume AST 63 /// clients will have a pointer to the respective SourceManager. 64 virtual SourceRange getSourceRange() const = 0; 65 SourceLocation getLocStart() const { return getSourceRange().getBegin(); } 66 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } 67 68 // global temp stats (until we have a per-module visitor) 69 static void addStmtClass(const StmtClass s); 70 static bool CollectingStats(bool enable=false); 71 static void PrintStats(); 72 73 /// dump - This does a local dump of the specified AST fragment. It dumps the 74 /// specified node and a few nodes underneath it, but not the whole subtree. 75 /// This is useful in a debugger. 76 void dump() const; 77 void dump(SourceManager &SM) const; 78 79 /// dumpAll - This does a dump of the specified AST fragment and all subtrees. 80 void dumpAll() const; 81 void dumpAll(SourceManager &SM) const; 82 83 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST 84 /// back to its original source language syntax. 85 void dumpPretty() const; 86 void printPretty(std::ostream &OS, PrinterHelper* = NULL) const; 87 88 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only 89 /// works on systems with GraphViz (Mac OS X) or dot+gv installed. 90 void viewAST() const; 91 92 // Implement isa<T> support. 93 static bool classof(const Stmt *) { return true; } 94 95 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations) 96 /// contain implicit control-flow in the order their subexpressions 97 /// are evaluated. This predicate returns true if this statement has 98 /// such implicit control-flow. Such statements are also specially handled 99 /// within CFGs. 100 bool hasImplicitControlFlow() const; 101 102 /// Child Iterators: All subclasses must implement child_begin and child_end 103 /// to permit easy iteration over the substatements/subexpessions of an 104 /// AST node. This permits easy iteration over all nodes in the AST. 105 typedef StmtIterator child_iterator; 106 typedef ConstStmtIterator const_child_iterator; 107 108 virtual child_iterator child_begin() = 0; 109 virtual child_iterator child_end() = 0; 110 111 const_child_iterator child_begin() const { 112 return const_child_iterator(const_cast<Stmt*>(this)->child_begin()); 113 } 114 115 const_child_iterator child_end() const { 116 return const_child_iterator(const_cast<Stmt*>(this)->child_end()); 117 } 118 119 void Emit(llvm::Serializer& S) const; 120 static Stmt* Create(llvm::Deserializer& D); 121 122 virtual void EmitImpl(llvm::Serializer& S) const { 123 // This method will eventually be a pure-virtual function. 124 assert (false && "Not implemented."); 125 } 126}; 127 128/// DeclStmt - Adaptor class for mixing declarations with statements and 129/// expressions. For example, CompoundStmt mixes statements, expressions 130/// and declarations (variables, types). Another example is ForStmt, where 131/// the first statement can be an expression or a declaration. 132/// 133class DeclStmt : public Stmt { 134 ScopedDecl *TheDecl; 135public: 136 DeclStmt(ScopedDecl *D) : Stmt(DeclStmtClass), TheDecl(D) {} 137 138 const ScopedDecl *getDecl() const { return TheDecl; } 139 ScopedDecl *getDecl() { return TheDecl; } 140 141 virtual SourceRange getSourceRange() const { return SourceRange(); } 142 143 static bool classof(const Stmt *T) { 144 return T->getStmtClass() == DeclStmtClass; 145 } 146 static bool classof(const DeclStmt *) { return true; } 147 148 // Iterators 149 virtual child_iterator child_begin(); 150 virtual child_iterator child_end(); 151 152 virtual void EmitImpl(llvm::Serializer& S) const; 153 static DeclStmt* CreateImpl(llvm::Deserializer& D); 154}; 155 156/// NullStmt - This is the null statement ";": C99 6.8.3p3. 157/// 158class NullStmt : public Stmt { 159 SourceLocation SemiLoc; 160public: 161 NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {} 162 163 SourceLocation getSemiLoc() const { return SemiLoc; } 164 165 virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); } 166 167 static bool classof(const Stmt *T) { 168 return T->getStmtClass() == NullStmtClass; 169 } 170 static bool classof(const NullStmt *) { return true; } 171 172 // Iterators 173 virtual child_iterator child_begin(); 174 virtual child_iterator child_end(); 175 176 virtual void EmitImpl(llvm::Serializer& S) const; 177 static NullStmt* CreateImpl(llvm::Deserializer& D); 178}; 179 180/// CompoundStmt - This represents a group of statements like { stmt stmt }. 181/// 182class CompoundStmt : public Stmt { 183 llvm::SmallVector<Stmt*, 16> Body; 184 SourceLocation LBracLoc, RBracLoc; 185public: 186 CompoundStmt(Stmt **StmtStart, unsigned NumStmts, 187 SourceLocation LB, SourceLocation RB) 188 : Stmt(CompoundStmtClass), Body(StmtStart, StmtStart+NumStmts), 189 LBracLoc(LB), RBracLoc(RB) {} 190 191 bool body_empty() const { return Body.empty(); } 192 193 typedef llvm::SmallVector<Stmt*, 16>::iterator body_iterator; 194 body_iterator body_begin() { return Body.begin(); } 195 body_iterator body_end() { return Body.end(); } 196 Stmt *body_back() { return Body.back(); } 197 198 typedef llvm::SmallVector<Stmt*, 16>::const_iterator const_body_iterator; 199 const_body_iterator body_begin() const { return Body.begin(); } 200 const_body_iterator body_end() const { return Body.end(); } 201 const Stmt *body_back() const { return Body.back(); } 202 203 typedef llvm::SmallVector<Stmt*, 16>::reverse_iterator reverse_body_iterator; 204 reverse_body_iterator body_rbegin() { return Body.rbegin(); } 205 reverse_body_iterator body_rend() { return Body.rend(); } 206 207 typedef llvm::SmallVector<Stmt*, 16>::const_reverse_iterator 208 const_reverse_body_iterator; 209 const_reverse_body_iterator body_rbegin() const { return Body.rbegin(); } 210 const_reverse_body_iterator body_rend() const { return Body.rend(); } 211 212 void push_back(Stmt *S) { Body.push_back(S); } 213 214 virtual SourceRange getSourceRange() const { 215 return SourceRange(LBracLoc, RBracLoc); 216 } 217 218 SourceLocation getLBracLoc() const { return LBracLoc; } 219 SourceLocation getRBracLoc() const { return RBracLoc; } 220 221 static bool classof(const Stmt *T) { 222 return T->getStmtClass() == CompoundStmtClass; 223 } 224 static bool classof(const CompoundStmt *) { return true; } 225 226 // Iterators 227 virtual child_iterator child_begin(); 228 virtual child_iterator child_end(); 229 230 virtual void EmitImpl(llvm::Serializer& S) const; 231 static CompoundStmt* CreateImpl(llvm::Deserializer& D); 232}; 233 234// SwitchCase is the base class for CaseStmt and DefaultStmt, 235class SwitchCase : public Stmt { 236protected: 237 // A pointer to the following CaseStmt or DefaultStmt class, 238 // used by SwitchStmt. 239 SwitchCase *NextSwitchCase; 240 241 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {} 242 243public: 244 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 245 246 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 247 248 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 249 250 virtual Stmt* v_getSubStmt() = 0; 251 Stmt *getSubStmt() { return v_getSubStmt(); } 252 253 virtual SourceRange getSourceRange() const { return SourceRange(); } 254 255 static bool classof(const Stmt *T) { 256 return T->getStmtClass() == CaseStmtClass || 257 T->getStmtClass() == DefaultStmtClass; 258 } 259 static bool classof(const SwitchCase *) { return true; } 260}; 261 262class CaseStmt : public SwitchCase { 263 enum { SUBSTMT, LHS, RHS, END_EXPR }; 264 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for 265 // GNU "case 1 ... 4" extension 266 SourceLocation CaseLoc; 267public: 268 CaseStmt(Expr *lhs, Expr *rhs, Stmt *substmt, SourceLocation caseLoc) 269 : SwitchCase(CaseStmtClass) { 270 SubExprs[SUBSTMT] = substmt; 271 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs); 272 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs); 273 CaseLoc = caseLoc; 274 } 275 276 SourceLocation getCaseLoc() const { return CaseLoc; } 277 278 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); } 279 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); } 280 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } 281 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 282 const Expr *getLHS() const { 283 return reinterpret_cast<const Expr*>(SubExprs[LHS]); 284 } 285 const Expr *getRHS() const { 286 return reinterpret_cast<const Expr*>(SubExprs[RHS]); 287 } 288 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; } 289 290 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; } 291 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); } 292 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } 293 294 295 virtual SourceRange getSourceRange() const { 296 return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd()); 297 } 298 static bool classof(const Stmt *T) { 299 return T->getStmtClass() == CaseStmtClass; 300 } 301 static bool classof(const CaseStmt *) { return true; } 302 303 // Iterators 304 virtual child_iterator child_begin(); 305 virtual child_iterator child_end(); 306 307 virtual void EmitImpl(llvm::Serializer& S) const; 308 static CaseStmt* CreateImpl(llvm::Deserializer& D); 309}; 310 311class DefaultStmt : public SwitchCase { 312 Stmt* SubStmt; 313 SourceLocation DefaultLoc; 314public: 315 DefaultStmt(SourceLocation DL, Stmt *substmt) : 316 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {} 317 318 Stmt *getSubStmt() { return SubStmt; } 319 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 320 const Stmt *getSubStmt() const { return SubStmt; } 321 322 SourceLocation getDefaultLoc() const { return DefaultLoc; } 323 324 virtual SourceRange getSourceRange() const { 325 return SourceRange(DefaultLoc, SubStmt->getLocEnd()); 326 } 327 static bool classof(const Stmt *T) { 328 return T->getStmtClass() == DefaultStmtClass; 329 } 330 static bool classof(const DefaultStmt *) { return true; } 331 332 // Iterators 333 virtual child_iterator child_begin(); 334 virtual child_iterator child_end(); 335 336 virtual void EmitImpl(llvm::Serializer& S) const; 337 static DefaultStmt* CreateImpl(llvm::Deserializer& D); 338}; 339 340class LabelStmt : public Stmt { 341 IdentifierInfo *Label; 342 Stmt *SubStmt; 343 SourceLocation IdentLoc; 344public: 345 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) 346 : Stmt(LabelStmtClass), Label(label), 347 SubStmt(substmt), IdentLoc(IL) {} 348 349 SourceLocation getIdentLoc() const { return IdentLoc; } 350 IdentifierInfo *getID() const { return Label; } 351 const char *getName() const; 352 Stmt *getSubStmt() { return SubStmt; } 353 const Stmt *getSubStmt() const { return SubStmt; } 354 355 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 356 void setSubStmt(Stmt *SS) { SubStmt = SS; } 357 358 virtual SourceRange getSourceRange() const { 359 return SourceRange(IdentLoc, SubStmt->getLocEnd()); 360 } 361 static bool classof(const Stmt *T) { 362 return T->getStmtClass() == LabelStmtClass; 363 } 364 static bool classof(const LabelStmt *) { return true; } 365 366 // Iterators 367 virtual child_iterator child_begin(); 368 virtual child_iterator child_end(); 369 370 virtual void EmitImpl(llvm::Serializer& S) const; 371 static LabelStmt* CreateImpl(llvm::Deserializer& D); 372}; 373 374 375/// IfStmt - This represents an if/then/else. 376/// 377class IfStmt : public Stmt { 378 enum { COND, THEN, ELSE, END_EXPR }; 379 Stmt* SubExprs[END_EXPR]; 380 SourceLocation IfLoc; 381public: 382 IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0) 383 : Stmt(IfStmtClass) { 384 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 385 SubExprs[THEN] = then; 386 SubExprs[ELSE] = elsev; 387 IfLoc = IL; 388 } 389 390 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 391 const Stmt *getThen() const { return SubExprs[THEN]; } 392 const Stmt *getElse() const { return SubExprs[ELSE]; } 393 394 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 395 Stmt *getThen() { return SubExprs[THEN]; } 396 Stmt *getElse() { return SubExprs[ELSE]; } 397 398 virtual SourceRange getSourceRange() const { 399 if (SubExprs[ELSE]) 400 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); 401 else 402 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); 403 } 404 405 static bool classof(const Stmt *T) { 406 return T->getStmtClass() == IfStmtClass; 407 } 408 static bool classof(const IfStmt *) { return true; } 409 410 // Iterators 411 virtual child_iterator child_begin(); 412 virtual child_iterator child_end(); 413 414 virtual void EmitImpl(llvm::Serializer& S) const; 415 static IfStmt* CreateImpl(llvm::Deserializer& D); 416}; 417 418/// SwitchStmt - This represents a 'switch' stmt. 419/// 420class SwitchStmt : public Stmt { 421 enum { COND, BODY, END_EXPR }; 422 Stmt* SubExprs[END_EXPR]; 423 // This points to a linked list of case and default statements. 424 SwitchCase *FirstCase; 425 SourceLocation SwitchLoc; 426public: 427 SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) { 428 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 429 SubExprs[BODY] = NULL; 430 } 431 432 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 433 const Stmt *getBody() const { return SubExprs[BODY]; } 434 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 435 436 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 437 Stmt *getBody() { return SubExprs[BODY]; } 438 SwitchCase *getSwitchCaseList() { return FirstCase; } 439 440 void setBody(Stmt *S, SourceLocation SL) { 441 SubExprs[BODY] = S; 442 SwitchLoc = SL; 443 } 444 void addSwitchCase(SwitchCase *SC) { 445 if (FirstCase) 446 SC->setNextSwitchCase(FirstCase); 447 448 FirstCase = SC; 449 } 450 virtual SourceRange getSourceRange() const { 451 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); 452 } 453 static bool classof(const Stmt *T) { 454 return T->getStmtClass() == SwitchStmtClass; 455 } 456 static bool classof(const SwitchStmt *) { return true; } 457 458 // Iterators 459 virtual child_iterator child_begin(); 460 virtual child_iterator child_end(); 461 462 virtual void EmitImpl(llvm::Serializer& S) const; 463 static SwitchStmt* CreateImpl(llvm::Deserializer& D); 464}; 465 466 467/// WhileStmt - This represents a 'while' stmt. 468/// 469class WhileStmt : public Stmt { 470 enum { COND, BODY, END_EXPR }; 471 Stmt* SubExprs[END_EXPR]; 472 SourceLocation WhileLoc; 473public: 474 WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) { 475 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 476 SubExprs[BODY] = body; 477 WhileLoc = WL; 478 } 479 480 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 481 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 482 Stmt *getBody() { return SubExprs[BODY]; } 483 const Stmt *getBody() const { return SubExprs[BODY]; } 484 485 virtual SourceRange getSourceRange() const { 486 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); 487 } 488 static bool classof(const Stmt *T) { 489 return T->getStmtClass() == WhileStmtClass; 490 } 491 static bool classof(const WhileStmt *) { return true; } 492 493 // Iterators 494 virtual child_iterator child_begin(); 495 virtual child_iterator child_end(); 496 497 virtual void EmitImpl(llvm::Serializer& S) const; 498 static WhileStmt* CreateImpl(llvm::Deserializer& D); 499}; 500 501/// DoStmt - This represents a 'do/while' stmt. 502/// 503class DoStmt : public Stmt { 504 enum { COND, BODY, END_EXPR }; 505 Stmt* SubExprs[END_EXPR]; 506 SourceLocation DoLoc; 507public: 508 DoStmt(Stmt *body, Expr *cond, SourceLocation DL) 509 : Stmt(DoStmtClass), DoLoc(DL) { 510 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 511 SubExprs[BODY] = body; 512 DoLoc = DL; 513 } 514 515 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 516 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 517 Stmt *getBody() { return SubExprs[BODY]; } 518 const Stmt *getBody() const { return SubExprs[BODY]; } 519 520 virtual SourceRange getSourceRange() const { 521 return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 522 } 523 static bool classof(const Stmt *T) { 524 return T->getStmtClass() == DoStmtClass; 525 } 526 static bool classof(const DoStmt *) { return true; } 527 528 // Iterators 529 virtual child_iterator child_begin(); 530 virtual child_iterator child_end(); 531 532 virtual void EmitImpl(llvm::Serializer& S) const; 533 static DoStmt* CreateImpl(llvm::Deserializer& D); 534}; 535 536 537/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 538/// the init/cond/inc parts of the ForStmt will be null if they were not 539/// specified in the source. 540/// 541class ForStmt : public Stmt { 542 enum { INIT, COND, INC, BODY, END_EXPR }; 543 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 544 SourceLocation ForLoc; 545public: 546 ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL) 547 : Stmt(ForStmtClass) { 548 SubExprs[INIT] = Init; 549 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 550 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 551 SubExprs[BODY] = Body; 552 ForLoc = FL; 553 } 554 555 Stmt *getInit() { return SubExprs[INIT]; } 556 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 557 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 558 Stmt *getBody() { return SubExprs[BODY]; } 559 560 const Stmt *getInit() const { return SubExprs[INIT]; } 561 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 562 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 563 const Stmt *getBody() const { return SubExprs[BODY]; } 564 565 virtual SourceRange getSourceRange() const { 566 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 567 } 568 static bool classof(const Stmt *T) { 569 return T->getStmtClass() == ForStmtClass; 570 } 571 static bool classof(const ForStmt *) { return true; } 572 573 // Iterators 574 virtual child_iterator child_begin(); 575 virtual child_iterator child_end(); 576 577 virtual void EmitImpl(llvm::Serializer& S) const; 578 static ForStmt* CreateImpl(llvm::Deserializer& D); 579}; 580 581/// ObjCForCollectionStmt - This represents Objective-c's collection statement; 582/// represented as 'for (element 'in' collection-expression)' stmt. 583/// 584class ObjCForCollectionStmt : public Stmt { 585 enum { ELEM, COLLECTION, BODY, END_EXPR }; 586 Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt. 587 SourceLocation ForLoc; 588 SourceLocation RParenLoc; 589public: 590 ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body, 591 SourceLocation FCL, SourceLocation RPL) 592 : Stmt(ObjCForCollectionStmtClass) { 593 SubExprs[ELEM] = Elem; 594 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect); 595 SubExprs[BODY] = Body; 596 ForLoc = FCL; 597 RParenLoc = RPL; 598 } 599 600 Stmt *getElement() { return SubExprs[ELEM]; } 601 Expr *getCollection() { 602 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 603 } 604 Stmt *getBody() { return SubExprs[BODY]; } 605 606 const Stmt *getElement() const { return SubExprs[ELEM]; } 607 const Expr *getCollection() const { 608 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 609 } 610 const Stmt *getBody() const { return SubExprs[BODY]; } 611 612 SourceLocation getRParenLoc() const { return RParenLoc; } 613 614 virtual SourceRange getSourceRange() const { 615 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 616 } 617 static bool classof(const Stmt *T) { 618 return T->getStmtClass() == ObjCForCollectionStmtClass; 619 } 620 static bool classof(const ObjCForCollectionStmt *) { return true; } 621 622 // Iterators 623 virtual child_iterator child_begin(); 624 virtual child_iterator child_end(); 625 626 virtual void EmitImpl(llvm::Serializer& S) const; 627 static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D); 628}; 629 630/// GotoStmt - This represents a direct goto. 631/// 632class GotoStmt : public Stmt { 633 LabelStmt *Label; 634 SourceLocation GotoLoc; 635 SourceLocation LabelLoc; 636public: 637 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL) 638 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 639 640 LabelStmt *getLabel() const { return Label; } 641 642 virtual SourceRange getSourceRange() const { 643 return SourceRange(GotoLoc, LabelLoc); 644 } 645 static bool classof(const Stmt *T) { 646 return T->getStmtClass() == GotoStmtClass; 647 } 648 static bool classof(const GotoStmt *) { return true; } 649 650 // Iterators 651 virtual child_iterator child_begin(); 652 virtual child_iterator child_end(); 653 654 virtual void EmitImpl(llvm::Serializer& S) const; 655 static GotoStmt* CreateImpl(llvm::Deserializer& D); 656}; 657 658/// IndirectGotoStmt - This represents an indirect goto. 659/// 660class IndirectGotoStmt : public Stmt { 661 Expr *Target; 662 // FIXME: Add location information (e.g. SourceLocation objects). 663 // When doing so, update the serialization routines. 664public: 665 IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass), Target(target){} 666 667 Expr *getTarget() { return Target; } 668 const Expr *getTarget() const { return Target; } 669 670 virtual SourceRange getSourceRange() const { return SourceRange(); } 671 672 static bool classof(const Stmt *T) { 673 return T->getStmtClass() == IndirectGotoStmtClass; 674 } 675 static bool classof(const IndirectGotoStmt *) { return true; } 676 677 // Iterators 678 virtual child_iterator child_begin(); 679 virtual child_iterator child_end(); 680 681 virtual void EmitImpl(llvm::Serializer& S) const; 682 static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D); 683}; 684 685 686/// ContinueStmt - This represents a continue. 687/// 688class ContinueStmt : public Stmt { 689 SourceLocation ContinueLoc; 690public: 691 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 692 693 virtual SourceRange getSourceRange() const { 694 return SourceRange(ContinueLoc); 695 } 696 static bool classof(const Stmt *T) { 697 return T->getStmtClass() == ContinueStmtClass; 698 } 699 static bool classof(const ContinueStmt *) { return true; } 700 701 // Iterators 702 virtual child_iterator child_begin(); 703 virtual child_iterator child_end(); 704 705 virtual void EmitImpl(llvm::Serializer& S) const; 706 static ContinueStmt* CreateImpl(llvm::Deserializer& D); 707}; 708 709/// BreakStmt - This represents a break. 710/// 711class BreakStmt : public Stmt { 712 SourceLocation BreakLoc; 713public: 714 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 715 716 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } 717 718 static bool classof(const Stmt *T) { 719 return T->getStmtClass() == BreakStmtClass; 720 } 721 static bool classof(const BreakStmt *) { return true; } 722 723 // Iterators 724 virtual child_iterator child_begin(); 725 virtual child_iterator child_end(); 726 727 virtual void EmitImpl(llvm::Serializer& S) const; 728 static BreakStmt* CreateImpl(llvm::Deserializer& D); 729}; 730 731 732/// ReturnStmt - This represents a return, optionally of an expression. 733/// 734class ReturnStmt : public Stmt { 735 Expr *RetExpr; 736 SourceLocation RetLoc; 737public: 738 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), 739 RetExpr(E), RetLoc(RL) {} 740 741 const Expr *getRetValue() const { return RetExpr; } 742 Expr *getRetValue() { return RetExpr; } 743 744 virtual SourceRange getSourceRange() const; 745 746 static bool classof(const Stmt *T) { 747 return T->getStmtClass() == ReturnStmtClass; 748 } 749 static bool classof(const ReturnStmt *) { return true; } 750 751 // Iterators 752 virtual child_iterator child_begin(); 753 virtual child_iterator child_end(); 754 755 virtual void EmitImpl(llvm::Serializer& S) const; 756 static ReturnStmt* CreateImpl(llvm::Deserializer& D); 757}; 758 759/// AsmStmt - This represents a GNU inline-assembly statement extension. 760/// 761class AsmStmt : public Stmt { 762 SourceLocation AsmLoc, RParenLoc; 763 StringLiteral *AsmStr; 764 765 bool IsVolatile; 766 767 unsigned NumOutputs; 768 unsigned NumInputs; 769 770 llvm::SmallVector<std::string, 4> Names; 771 llvm::SmallVector<StringLiteral*, 4> Constraints; 772 llvm::SmallVector<Expr*, 4> Exprs; 773 774 llvm::SmallVector<StringLiteral*, 4> Clobbers; 775public: 776 AsmStmt(SourceLocation asmloc, 777 bool isvolatile, 778 unsigned numoutputs, 779 unsigned numinputs, 780 std::string *names, 781 StringLiteral **constraints, 782 Expr **exprs, 783 StringLiteral *asmstr, 784 unsigned numclobbers, 785 StringLiteral **clobbers, 786 SourceLocation rparenloc); 787 788 bool isVolatile() const { return IsVolatile; } 789 790 unsigned getNumOutputs() const { return NumOutputs; } 791 const std::string &getOutputName(unsigned i) const 792 { return Names[i]; } 793 const StringLiteral *getOutputConstraint(unsigned i) const 794 { return Constraints[i]; } 795 StringLiteral *getOutputConstraint(unsigned i) 796 { return Constraints[i]; } 797 const Expr *getOutputExpr(unsigned i) const { return Exprs[i]; } 798 Expr *getOutputExpr(unsigned i) { return Exprs[i]; } 799 800 unsigned getNumInputs() const { return NumInputs; } 801 const std::string &getInputName(unsigned i) const 802 { return Names[i + NumOutputs]; } 803 StringLiteral *getInputConstraint(unsigned i) 804 { return Constraints[i + NumOutputs]; } 805 const StringLiteral *getInputConstraint(unsigned i) const 806 { return Constraints[i + NumOutputs]; } 807 Expr *getInputExpr(unsigned i) { return Exprs[i + NumOutputs]; } 808 const Expr *getInputExpr(unsigned i) const { return Exprs[i + NumOutputs]; } 809 810 const StringLiteral *getAsmString() const { return AsmStr; } 811 StringLiteral *getAsmString() { return AsmStr; } 812 813 unsigned getNumClobbers() const { return Clobbers.size(); } 814 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 815 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 816 817 virtual SourceRange getSourceRange() const { 818 return SourceRange(AsmLoc, RParenLoc); 819 } 820 821 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 822 static bool classof(const AsmStmt *) { return true; } 823 824 virtual child_iterator child_begin(); 825 virtual child_iterator child_end(); 826 827 virtual void EmitImpl(llvm::Serializer& S) const; 828 static AsmStmt* CreateImpl(llvm::Deserializer& D); 829}; 830 831/// ObjCAtCatchStmt - This represents objective-c's @catch statement. 832class ObjCAtCatchStmt : public Stmt { 833private: 834 // Points to next @catch statement, or null 835 ObjCAtCatchStmt *NextAtCatchStmt; 836 enum { SELECTOR, BODY, END_EXPR }; 837 Stmt *SubExprs[END_EXPR]; 838 SourceLocation AtCatchLoc, RParenLoc; 839 840 // Used by deserialization. 841 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc) 842 : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {} 843 844public: 845 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc, 846 Stmt *catchVarStmtDecl, Stmt *atCatchStmt, Stmt *atCatchList) 847 : Stmt(ObjCAtCatchStmtClass) { 848 SubExprs[SELECTOR] = catchVarStmtDecl; 849 SubExprs[BODY] = atCatchStmt; 850 if (!atCatchList) 851 NextAtCatchStmt = NULL; 852 else { 853 ObjCAtCatchStmt *AtCatchList = 854 static_cast<ObjCAtCatchStmt*>(atCatchList); 855 while (AtCatchList->NextAtCatchStmt) 856 AtCatchList = AtCatchList->NextAtCatchStmt; 857 AtCatchList->NextAtCatchStmt = this; 858 } 859 AtCatchLoc = atCatchLoc; 860 RParenLoc = rparenloc; 861 } 862 863 const Stmt *getCatchBody() const { return SubExprs[BODY]; } 864 Stmt *getCatchBody() { return SubExprs[BODY]; } 865 const ObjCAtCatchStmt *getNextCatchStmt() const { return NextAtCatchStmt; } 866 ObjCAtCatchStmt *getNextCatchStmt() { return NextAtCatchStmt; } 867 const Stmt *getCatchParamStmt() const { return SubExprs[SELECTOR]; } 868 Stmt *getCatchParamStmt() { return SubExprs[SELECTOR]; } 869 870 SourceLocation getRParenLoc() const { return RParenLoc; } 871 872 virtual SourceRange getSourceRange() const { 873 return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd()); 874 } 875 876 static bool classof(const Stmt *T) { 877 return T->getStmtClass() == ObjCAtCatchStmtClass; 878 } 879 static bool classof(const ObjCAtCatchStmt *) { return true; } 880 881 virtual child_iterator child_begin(); 882 virtual child_iterator child_end(); 883 884 virtual void EmitImpl(llvm::Serializer& S) const; 885 static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D); 886}; 887 888/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement 889class ObjCAtFinallyStmt : public Stmt { 890 Stmt *AtFinallyStmt; 891 SourceLocation AtFinallyLoc; 892public: 893 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt) 894 : Stmt(ObjCAtFinallyStmtClass), 895 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {} 896 897 const Stmt *getFinallyBody () const { return AtFinallyStmt; } 898 Stmt *getFinallyBody () { return AtFinallyStmt; } 899 900 virtual SourceRange getSourceRange() const { 901 return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd()); 902 } 903 904 static bool classof(const Stmt *T) { 905 return T->getStmtClass() == ObjCAtFinallyStmtClass; 906 } 907 static bool classof(const ObjCAtFinallyStmt *) { return true; } 908 909 virtual child_iterator child_begin(); 910 virtual child_iterator child_end(); 911 912 virtual void EmitImpl(llvm::Serializer& S) const; 913 static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D); 914}; 915 916/// ObjCAtTryStmt - This represent objective-c's over-all 917/// @try ... @catch ... @finally statement. 918class ObjCAtTryStmt : public Stmt { 919private: 920 enum { TRY, CATCH, FINALLY, END_EXPR }; 921 Stmt* SubStmts[END_EXPR]; 922 923 SourceLocation AtTryLoc; 924public: 925 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 926 Stmt *atCatchStmt, 927 Stmt *atFinallyStmt) 928 : Stmt(ObjCAtTryStmtClass) { 929 SubStmts[TRY] = atTryStmt; 930 SubStmts[CATCH] = atCatchStmt; 931 SubStmts[FINALLY] = atFinallyStmt; 932 AtTryLoc = atTryLoc; 933 } 934 935 const Stmt *getTryBody() const { return SubStmts[TRY]; } 936 Stmt *getTryBody() { return SubStmts[TRY]; } 937 const ObjCAtCatchStmt *getCatchStmts() const { 938 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 939 } 940 ObjCAtCatchStmt *getCatchStmts() { 941 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 942 } 943 const ObjCAtFinallyStmt *getFinallyStmt() const { 944 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 945 } 946 ObjCAtFinallyStmt *getFinallyStmt() { 947 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 948 } 949 virtual SourceRange getSourceRange() const { 950 return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd()); 951 } 952 953 static bool classof(const Stmt *T) { 954 return T->getStmtClass() == ObjCAtTryStmtClass; 955 } 956 static bool classof(const ObjCAtTryStmt *) { return true; } 957 958 virtual child_iterator child_begin(); 959 virtual child_iterator child_end(); 960 961 virtual void EmitImpl(llvm::Serializer& S) const; 962 static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D); 963}; 964 965/// ObjCAtThrowStmt - This represents objective-c's @throw statement. 966class ObjCAtThrowStmt : public Stmt { 967 Stmt *Throw; 968 SourceLocation AtThrowLoc; 969public: 970 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr) 971 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) { 972 AtThrowLoc = atThrowLoc; 973 } 974 975 Expr *const getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); } 976 977 virtual SourceRange getSourceRange() const { 978 return SourceRange(AtThrowLoc, Throw->getLocEnd()); 979 } 980 981 static bool classof(const Stmt *T) { 982 return T->getStmtClass() == ObjCAtThrowStmtClass; 983 } 984 static bool classof(const ObjCAtThrowStmt *) { return true; } 985 986 virtual child_iterator child_begin(); 987 virtual child_iterator child_end(); 988 989 virtual void EmitImpl(llvm::Serializer& S) const; 990 static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D); 991}; 992 993} // end namespace clang 994 995#endif 996