Stmt.h revision dfab34a696d1dba8622248c31aaf605906cb6109
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 593 Stmt *getElement() { return SubExprs[ELEM]; } 594 Expr *getCollection() { 595 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 596 } 597 Stmt *getBody() { return SubExprs[BODY]; } 598 599 const Stmt *getElement() const { return SubExprs[ELEM]; } 600 const Expr *getCollection() const { 601 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 602 } 603 const Stmt *getBody() const { return SubExprs[BODY]; } 604 605 SourceLocation getRParenLoc() const { return RParenLoc; } 606 607 virtual SourceRange getSourceRange() const { 608 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 609 } 610 static bool classof(const Stmt *T) { 611 return T->getStmtClass() == ObjCForCollectionStmtClass; 612 } 613 static bool classof(const ObjCForCollectionStmt *) { return true; } 614 615 // Iterators 616 virtual child_iterator child_begin(); 617 virtual child_iterator child_end(); 618 619 virtual void EmitImpl(llvm::Serializer& S) const; 620 static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D); 621}; 622 623/// GotoStmt - This represents a direct goto. 624/// 625class GotoStmt : public Stmt { 626 LabelStmt *Label; 627 SourceLocation GotoLoc; 628 SourceLocation LabelLoc; 629public: 630 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL) 631 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 632 633 LabelStmt *getLabel() const { return Label; } 634 635 virtual SourceRange getSourceRange() const { 636 return SourceRange(GotoLoc, LabelLoc); 637 } 638 static bool classof(const Stmt *T) { 639 return T->getStmtClass() == GotoStmtClass; 640 } 641 static bool classof(const GotoStmt *) { return true; } 642 643 // Iterators 644 virtual child_iterator child_begin(); 645 virtual child_iterator child_end(); 646 647 virtual void EmitImpl(llvm::Serializer& S) const; 648 static GotoStmt* CreateImpl(llvm::Deserializer& D); 649}; 650 651/// IndirectGotoStmt - This represents an indirect goto. 652/// 653class IndirectGotoStmt : public Stmt { 654 Expr *Target; 655 // FIXME: Add location information (e.g. SourceLocation objects). 656 // When doing so, update the serialization routines. 657public: 658 IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass), Target(target){} 659 660 Expr *getTarget() { return Target; } 661 const Expr *getTarget() const { return Target; } 662 663 virtual SourceRange getSourceRange() const { return SourceRange(); } 664 665 static bool classof(const Stmt *T) { 666 return T->getStmtClass() == IndirectGotoStmtClass; 667 } 668 static bool classof(const IndirectGotoStmt *) { return true; } 669 670 // Iterators 671 virtual child_iterator child_begin(); 672 virtual child_iterator child_end(); 673 674 virtual void EmitImpl(llvm::Serializer& S) const; 675 static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D); 676}; 677 678 679/// ContinueStmt - This represents a continue. 680/// 681class ContinueStmt : public Stmt { 682 SourceLocation ContinueLoc; 683public: 684 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 685 686 virtual SourceRange getSourceRange() const { 687 return SourceRange(ContinueLoc); 688 } 689 static bool classof(const Stmt *T) { 690 return T->getStmtClass() == ContinueStmtClass; 691 } 692 static bool classof(const ContinueStmt *) { return true; } 693 694 // Iterators 695 virtual child_iterator child_begin(); 696 virtual child_iterator child_end(); 697 698 virtual void EmitImpl(llvm::Serializer& S) const; 699 static ContinueStmt* CreateImpl(llvm::Deserializer& D); 700}; 701 702/// BreakStmt - This represents a break. 703/// 704class BreakStmt : public Stmt { 705 SourceLocation BreakLoc; 706public: 707 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 708 709 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } 710 711 static bool classof(const Stmt *T) { 712 return T->getStmtClass() == BreakStmtClass; 713 } 714 static bool classof(const BreakStmt *) { return true; } 715 716 // Iterators 717 virtual child_iterator child_begin(); 718 virtual child_iterator child_end(); 719 720 virtual void EmitImpl(llvm::Serializer& S) const; 721 static BreakStmt* CreateImpl(llvm::Deserializer& D); 722}; 723 724 725/// ReturnStmt - This represents a return, optionally of an expression. 726/// 727class ReturnStmt : public Stmt { 728 Expr *RetExpr; 729 SourceLocation RetLoc; 730public: 731 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), 732 RetExpr(E), RetLoc(RL) {} 733 734 const Expr *getRetValue() const { return RetExpr; } 735 Expr *getRetValue() { return RetExpr; } 736 737 virtual SourceRange getSourceRange() const; 738 739 static bool classof(const Stmt *T) { 740 return T->getStmtClass() == ReturnStmtClass; 741 } 742 static bool classof(const ReturnStmt *) { return true; } 743 744 // Iterators 745 virtual child_iterator child_begin(); 746 virtual child_iterator child_end(); 747 748 virtual void EmitImpl(llvm::Serializer& S) const; 749 static ReturnStmt* CreateImpl(llvm::Deserializer& D); 750}; 751 752/// AsmStmt - This represents a GNU inline-assembly statement extension. 753/// 754class AsmStmt : public Stmt { 755 SourceLocation AsmLoc, RParenLoc; 756 StringLiteral *AsmStr; 757 758 bool IsSimple; 759 bool IsVolatile; 760 761 unsigned NumOutputs; 762 unsigned NumInputs; 763 764 llvm::SmallVector<std::string, 4> Names; 765 llvm::SmallVector<StringLiteral*, 4> Constraints; 766 llvm::SmallVector<Expr*, 4> Exprs; 767 768 llvm::SmallVector<StringLiteral*, 4> Clobbers; 769public: 770 AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile, 771 unsigned numoutputs, unsigned numinputs, 772 std::string *names, StringLiteral **constraints, 773 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers, 774 StringLiteral **clobbers, SourceLocation rparenloc); 775 776 bool isVolatile() const { return IsVolatile; } 777 bool isSimple() const { return IsSimple; } 778 779 unsigned getNumOutputs() const { return NumOutputs; } 780 const std::string &getOutputName(unsigned i) const 781 { return Names[i]; } 782 const StringLiteral *getOutputConstraint(unsigned i) const 783 { return Constraints[i]; } 784 StringLiteral *getOutputConstraint(unsigned i) 785 { return Constraints[i]; } 786 const Expr *getOutputExpr(unsigned i) const { return Exprs[i]; } 787 Expr *getOutputExpr(unsigned i) { return Exprs[i]; } 788 789 unsigned getNumInputs() const { return NumInputs; } 790 const std::string &getInputName(unsigned i) const 791 { return Names[i + NumOutputs]; } 792 StringLiteral *getInputConstraint(unsigned i) 793 { return Constraints[i + NumOutputs]; } 794 const StringLiteral *getInputConstraint(unsigned i) const 795 { return Constraints[i + NumOutputs]; } 796 Expr *getInputExpr(unsigned i) { return Exprs[i + NumOutputs]; } 797 const Expr *getInputExpr(unsigned i) const { return Exprs[i + NumOutputs]; } 798 799 const StringLiteral *getAsmString() const { return AsmStr; } 800 StringLiteral *getAsmString() { return AsmStr; } 801 802 unsigned getNumClobbers() const { return Clobbers.size(); } 803 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 804 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 805 806 virtual SourceRange getSourceRange() const { 807 return SourceRange(AsmLoc, RParenLoc); 808 } 809 810 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 811 static bool classof(const AsmStmt *) { return true; } 812 813 virtual child_iterator child_begin(); 814 virtual child_iterator child_end(); 815 816 virtual void EmitImpl(llvm::Serializer& S) const; 817 static AsmStmt* CreateImpl(llvm::Deserializer& D); 818}; 819 820/// ObjCAtCatchStmt - This represents objective-c's @catch statement. 821class ObjCAtCatchStmt : public Stmt { 822private: 823 enum { SELECTOR, BODY, NEXT_CATCH, END_EXPR }; 824 Stmt *SubExprs[END_EXPR]; 825 SourceLocation AtCatchLoc, RParenLoc; 826 827 // Used by deserialization. 828 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc) 829 : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {} 830 831public: 832 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc, 833 Stmt *catchVarStmtDecl, Stmt *atCatchStmt, Stmt *atCatchList); 834 835 const Stmt *getCatchBody() const { return SubExprs[BODY]; } 836 Stmt *getCatchBody() { return SubExprs[BODY]; } 837 838 const ObjCAtCatchStmt *getNextCatchStmt() const { 839 return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]); 840 } 841 ObjCAtCatchStmt *getNextCatchStmt() { 842 return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]); 843 } 844 845 const Stmt *getCatchParamStmt() const { return SubExprs[SELECTOR]; } 846 Stmt *getCatchParamStmt() { return SubExprs[SELECTOR]; } 847 848 SourceLocation getRParenLoc() const { return RParenLoc; } 849 850 virtual SourceRange getSourceRange() const { 851 return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd()); 852 } 853 854 bool hasEllipsis() const { return getCatchParamStmt() == 0; } 855 856 static bool classof(const Stmt *T) { 857 return T->getStmtClass() == ObjCAtCatchStmtClass; 858 } 859 static bool classof(const ObjCAtCatchStmt *) { return true; } 860 861 virtual child_iterator child_begin(); 862 virtual child_iterator child_end(); 863 864 virtual void EmitImpl(llvm::Serializer& S) const; 865 static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D); 866}; 867 868/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement 869class ObjCAtFinallyStmt : public Stmt { 870 Stmt *AtFinallyStmt; 871 SourceLocation AtFinallyLoc; 872public: 873 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt) 874 : Stmt(ObjCAtFinallyStmtClass), 875 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {} 876 877 const Stmt *getFinallyBody () const { return AtFinallyStmt; } 878 Stmt *getFinallyBody () { return AtFinallyStmt; } 879 880 virtual SourceRange getSourceRange() const { 881 return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd()); 882 } 883 884 static bool classof(const Stmt *T) { 885 return T->getStmtClass() == ObjCAtFinallyStmtClass; 886 } 887 static bool classof(const ObjCAtFinallyStmt *) { return true; } 888 889 virtual child_iterator child_begin(); 890 virtual child_iterator child_end(); 891 892 virtual void EmitImpl(llvm::Serializer& S) const; 893 static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D); 894}; 895 896/// ObjCAtTryStmt - This represent objective-c's over-all 897/// @try ... @catch ... @finally statement. 898class ObjCAtTryStmt : public Stmt { 899private: 900 enum { TRY, CATCH, FINALLY, END_EXPR }; 901 Stmt* SubStmts[END_EXPR]; 902 903 SourceLocation AtTryLoc; 904public: 905 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 906 Stmt *atCatchStmt, 907 Stmt *atFinallyStmt) 908 : Stmt(ObjCAtTryStmtClass) { 909 SubStmts[TRY] = atTryStmt; 910 SubStmts[CATCH] = atCatchStmt; 911 SubStmts[FINALLY] = atFinallyStmt; 912 AtTryLoc = atTryLoc; 913 } 914 915 const Stmt *getTryBody() const { return SubStmts[TRY]; } 916 Stmt *getTryBody() { return SubStmts[TRY]; } 917 const ObjCAtCatchStmt *getCatchStmts() const { 918 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 919 } 920 ObjCAtCatchStmt *getCatchStmts() { 921 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 922 } 923 const ObjCAtFinallyStmt *getFinallyStmt() const { 924 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 925 } 926 ObjCAtFinallyStmt *getFinallyStmt() { 927 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 928 } 929 virtual SourceRange getSourceRange() const { 930 return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd()); 931 } 932 933 static bool classof(const Stmt *T) { 934 return T->getStmtClass() == ObjCAtTryStmtClass; 935 } 936 static bool classof(const ObjCAtTryStmt *) { return true; } 937 938 virtual child_iterator child_begin(); 939 virtual child_iterator child_end(); 940 941 virtual void EmitImpl(llvm::Serializer& S) const; 942 static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D); 943}; 944 945/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement. 946/// Example: @synchronized (sem) { 947/// do-something; 948/// } 949/// 950class ObjCAtSynchronizedStmt : public Stmt { 951private: 952 enum { SYNC_EXPR, SYNC_BODY, END_EXPR }; 953 Stmt* SubStmts[END_EXPR]; 954 SourceLocation AtSynchronizedLoc; 955 956public: 957 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr, 958 Stmt *synchBody) 959 : Stmt(ObjCAtSynchronizedStmtClass) { 960 SubStmts[SYNC_EXPR] = synchExpr; 961 SubStmts[SYNC_BODY] = synchBody; 962 AtSynchronizedLoc = atSynchronizedLoc; 963 } 964 965 const CompoundStmt *getSynchBody() const { 966 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]); 967 } 968 CompoundStmt *getSynchBody() { 969 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]); 970 } 971 972 const Expr *getSynchExpr() const { 973 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]); 974 } 975 Expr *getSynchExpr() { 976 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]); 977 } 978 979 virtual SourceRange getSourceRange() const { 980 return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd()); 981 } 982 983 static bool classof(const Stmt *T) { 984 return T->getStmtClass() == ObjCAtSynchronizedStmtClass; 985 } 986 static bool classof(const ObjCAtSynchronizedStmt *) { return true; } 987 988 virtual child_iterator child_begin(); 989 virtual child_iterator child_end(); 990 991 virtual void EmitImpl(llvm::Serializer& S) const; 992 static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D); 993}; 994 995/// ObjCAtThrowStmt - This represents objective-c's @throw statement. 996class ObjCAtThrowStmt : public Stmt { 997 Stmt *Throw; 998 SourceLocation AtThrowLoc; 999public: 1000 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr) 1001 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) { 1002 AtThrowLoc = atThrowLoc; 1003 } 1004 1005 Expr *const getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); } 1006 1007 virtual SourceRange getSourceRange() const { 1008 if (Throw) 1009 return SourceRange(AtThrowLoc, Throw->getLocEnd()); 1010 else 1011 return SourceRange(AtThrowLoc); 1012 } 1013 1014 static bool classof(const Stmt *T) { 1015 return T->getStmtClass() == ObjCAtThrowStmtClass; 1016 } 1017 static bool classof(const ObjCAtThrowStmt *) { return true; } 1018 1019 virtual child_iterator child_begin(); 1020 virtual child_iterator child_end(); 1021 1022 virtual void EmitImpl(llvm::Serializer& S) const; 1023 static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D); 1024}; 1025 1026} // end namespace clang 1027 1028#endif 1029