Stmt.h revision 7571228ec9b31de7733ae96352ee4eaa8afcf2b5
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 virtual SourceRange getSourceRange() const { 291 return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd()); 292 } 293 static bool classof(const Stmt *T) { 294 return T->getStmtClass() == CaseStmtClass; 295 } 296 static bool classof(const CaseStmt *) { return true; } 297 298 // Iterators 299 virtual child_iterator child_begin(); 300 virtual child_iterator child_end(); 301 302 virtual void EmitImpl(llvm::Serializer& S) const; 303 static CaseStmt* CreateImpl(llvm::Deserializer& D); 304}; 305 306class DefaultStmt : public SwitchCase { 307 Stmt* SubStmt; 308 SourceLocation DefaultLoc; 309public: 310 DefaultStmt(SourceLocation DL, Stmt *substmt) : 311 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {} 312 313 Stmt *getSubStmt() { return SubStmt; } 314 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 315 const Stmt *getSubStmt() const { return SubStmt; } 316 317 SourceLocation getDefaultLoc() const { return DefaultLoc; } 318 319 virtual SourceRange getSourceRange() const { 320 return SourceRange(DefaultLoc, SubStmt->getLocEnd()); 321 } 322 static bool classof(const Stmt *T) { 323 return T->getStmtClass() == DefaultStmtClass; 324 } 325 static bool classof(const DefaultStmt *) { return true; } 326 327 // Iterators 328 virtual child_iterator child_begin(); 329 virtual child_iterator child_end(); 330 331 virtual void EmitImpl(llvm::Serializer& S) const; 332 static DefaultStmt* CreateImpl(llvm::Deserializer& D); 333}; 334 335class LabelStmt : public Stmt { 336 IdentifierInfo *Label; 337 Stmt *SubStmt; 338 SourceLocation IdentLoc; 339public: 340 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) 341 : Stmt(LabelStmtClass), Label(label), 342 SubStmt(substmt), IdentLoc(IL) {} 343 344 SourceLocation getIdentLoc() const { return IdentLoc; } 345 IdentifierInfo *getID() const { return Label; } 346 const char *getName() const; 347 Stmt *getSubStmt() { return SubStmt; } 348 const Stmt *getSubStmt() const { return SubStmt; } 349 350 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 351 void setSubStmt(Stmt *SS) { SubStmt = SS; } 352 353 virtual SourceRange getSourceRange() const { 354 return SourceRange(IdentLoc, SubStmt->getLocEnd()); 355 } 356 static bool classof(const Stmt *T) { 357 return T->getStmtClass() == LabelStmtClass; 358 } 359 static bool classof(const LabelStmt *) { return true; } 360 361 // Iterators 362 virtual child_iterator child_begin(); 363 virtual child_iterator child_end(); 364 365 virtual void EmitImpl(llvm::Serializer& S) const; 366 static LabelStmt* CreateImpl(llvm::Deserializer& D); 367}; 368 369 370/// IfStmt - This represents an if/then/else. 371/// 372class IfStmt : public Stmt { 373 enum { COND, THEN, ELSE, END_EXPR }; 374 Stmt* SubExprs[END_EXPR]; 375 SourceLocation IfLoc; 376public: 377 IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0) 378 : Stmt(IfStmtClass) { 379 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 380 SubExprs[THEN] = then; 381 SubExprs[ELSE] = elsev; 382 IfLoc = IL; 383 } 384 385 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 386 const Stmt *getThen() const { return SubExprs[THEN]; } 387 const Stmt *getElse() const { return SubExprs[ELSE]; } 388 389 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 390 Stmt *getThen() { return SubExprs[THEN]; } 391 Stmt *getElse() { return SubExprs[ELSE]; } 392 393 virtual SourceRange getSourceRange() const { 394 if (SubExprs[ELSE]) 395 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); 396 else 397 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); 398 } 399 400 static bool classof(const Stmt *T) { 401 return T->getStmtClass() == IfStmtClass; 402 } 403 static bool classof(const IfStmt *) { return true; } 404 405 // Iterators 406 virtual child_iterator child_begin(); 407 virtual child_iterator child_end(); 408 409 virtual void EmitImpl(llvm::Serializer& S) const; 410 static IfStmt* CreateImpl(llvm::Deserializer& D); 411}; 412 413/// SwitchStmt - This represents a 'switch' stmt. 414/// 415class SwitchStmt : public Stmt { 416 enum { COND, BODY, END_EXPR }; 417 Stmt* SubExprs[END_EXPR]; 418 // This points to a linked list of case and default statements. 419 SwitchCase *FirstCase; 420 SourceLocation SwitchLoc; 421public: 422 SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) { 423 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 424 SubExprs[BODY] = NULL; 425 } 426 427 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 428 const Stmt *getBody() const { return SubExprs[BODY]; } 429 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 430 431 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 432 Stmt *getBody() { return SubExprs[BODY]; } 433 SwitchCase *getSwitchCaseList() { return FirstCase; } 434 435 void setBody(Stmt *S, SourceLocation SL) { 436 SubExprs[BODY] = S; 437 SwitchLoc = SL; 438 } 439 void addSwitchCase(SwitchCase *SC) { 440 if (FirstCase) 441 SC->setNextSwitchCase(FirstCase); 442 443 FirstCase = SC; 444 } 445 virtual SourceRange getSourceRange() const { 446 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); 447 } 448 static bool classof(const Stmt *T) { 449 return T->getStmtClass() == SwitchStmtClass; 450 } 451 static bool classof(const SwitchStmt *) { return true; } 452 453 // Iterators 454 virtual child_iterator child_begin(); 455 virtual child_iterator child_end(); 456 457 virtual void EmitImpl(llvm::Serializer& S) const; 458 static SwitchStmt* CreateImpl(llvm::Deserializer& D); 459}; 460 461 462/// WhileStmt - This represents a 'while' stmt. 463/// 464class WhileStmt : public Stmt { 465 enum { COND, BODY, END_EXPR }; 466 Stmt* SubExprs[END_EXPR]; 467 SourceLocation WhileLoc; 468public: 469 WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) { 470 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 471 SubExprs[BODY] = body; 472 WhileLoc = WL; 473 } 474 475 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 476 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 477 Stmt *getBody() { return SubExprs[BODY]; } 478 const Stmt *getBody() const { return SubExprs[BODY]; } 479 480 virtual SourceRange getSourceRange() const { 481 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); 482 } 483 static bool classof(const Stmt *T) { 484 return T->getStmtClass() == WhileStmtClass; 485 } 486 static bool classof(const WhileStmt *) { return true; } 487 488 // Iterators 489 virtual child_iterator child_begin(); 490 virtual child_iterator child_end(); 491 492 virtual void EmitImpl(llvm::Serializer& S) const; 493 static WhileStmt* CreateImpl(llvm::Deserializer& D); 494}; 495 496/// DoStmt - This represents a 'do/while' stmt. 497/// 498class DoStmt : public Stmt { 499 enum { COND, BODY, END_EXPR }; 500 Stmt* SubExprs[END_EXPR]; 501 SourceLocation DoLoc; 502public: 503 DoStmt(Stmt *body, Expr *cond, SourceLocation DL) 504 : Stmt(DoStmtClass), DoLoc(DL) { 505 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 506 SubExprs[BODY] = body; 507 DoLoc = DL; 508 } 509 510 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 511 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 512 Stmt *getBody() { return SubExprs[BODY]; } 513 const Stmt *getBody() const { return SubExprs[BODY]; } 514 515 virtual SourceRange getSourceRange() const { 516 return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 517 } 518 static bool classof(const Stmt *T) { 519 return T->getStmtClass() == DoStmtClass; 520 } 521 static bool classof(const DoStmt *) { return true; } 522 523 // Iterators 524 virtual child_iterator child_begin(); 525 virtual child_iterator child_end(); 526 527 virtual void EmitImpl(llvm::Serializer& S) const; 528 static DoStmt* CreateImpl(llvm::Deserializer& D); 529}; 530 531 532/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 533/// the init/cond/inc parts of the ForStmt will be null if they were not 534/// specified in the source. 535/// 536class ForStmt : public Stmt { 537 enum { INIT, COND, INC, BODY, END_EXPR }; 538 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 539 SourceLocation ForLoc; 540public: 541 ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL) 542 : Stmt(ForStmtClass) { 543 SubExprs[INIT] = Init; 544 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 545 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 546 SubExprs[BODY] = Body; 547 ForLoc = FL; 548 } 549 550 Stmt *getInit() { return SubExprs[INIT]; } 551 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 552 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 553 Stmt *getBody() { return SubExprs[BODY]; } 554 555 const Stmt *getInit() const { return SubExprs[INIT]; } 556 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 557 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 558 const Stmt *getBody() const { return SubExprs[BODY]; } 559 560 virtual SourceRange getSourceRange() const { 561 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 562 } 563 static bool classof(const Stmt *T) { 564 return T->getStmtClass() == ForStmtClass; 565 } 566 static bool classof(const ForStmt *) { return true; } 567 568 // Iterators 569 virtual child_iterator child_begin(); 570 virtual child_iterator child_end(); 571 572 virtual void EmitImpl(llvm::Serializer& S) const; 573 static ForStmt* CreateImpl(llvm::Deserializer& D); 574}; 575 576/// ObjCForCollectionStmt - This represents Objective-c's collection statement; 577/// represented as 'for (element 'in' collection-expression)' stmt. 578/// 579class ObjCForCollectionStmt : public Stmt { 580 enum { ELEM, COLLECTION, BODY, END_EXPR }; 581 Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt. 582 SourceLocation ForLoc; 583 SourceLocation RParenLoc; 584public: 585 ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body, 586 SourceLocation FCL, SourceLocation RPL) 587 : Stmt(ObjCForCollectionStmtClass) { 588 SubExprs[ELEM] = Elem; 589 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect); 590 SubExprs[BODY] = Body; 591 ForLoc = FCL; 592 RParenLoc = RPL; 593 } 594 595 Stmt *getElement() { return SubExprs[ELEM]; } 596 Expr *getCollection() { 597 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 598 } 599 Stmt *getBody() { return SubExprs[BODY]; } 600 601 const Stmt *getElement() const { return SubExprs[ELEM]; } 602 const Expr *getCollection() const { 603 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 604 } 605 const Stmt *getBody() const { return SubExprs[BODY]; } 606 607 SourceLocation getRParenLoc() const { return RParenLoc; } 608 609 virtual SourceRange getSourceRange() const { 610 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 611 } 612 static bool classof(const Stmt *T) { 613 return T->getStmtClass() == ObjCForCollectionStmtClass; 614 } 615 static bool classof(const ObjCForCollectionStmt *) { return true; } 616 617 // Iterators 618 virtual child_iterator child_begin(); 619 virtual child_iterator child_end(); 620 621 virtual void EmitImpl(llvm::Serializer& S) const; 622 static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D); 623}; 624 625/// GotoStmt - This represents a direct goto. 626/// 627class GotoStmt : public Stmt { 628 LabelStmt *Label; 629 SourceLocation GotoLoc; 630 SourceLocation LabelLoc; 631public: 632 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL) 633 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 634 635 LabelStmt *getLabel() const { return Label; } 636 637 virtual SourceRange getSourceRange() const { 638 return SourceRange(GotoLoc, LabelLoc); 639 } 640 static bool classof(const Stmt *T) { 641 return T->getStmtClass() == GotoStmtClass; 642 } 643 static bool classof(const GotoStmt *) { return true; } 644 645 // Iterators 646 virtual child_iterator child_begin(); 647 virtual child_iterator child_end(); 648 649 virtual void EmitImpl(llvm::Serializer& S) const; 650 static GotoStmt* CreateImpl(llvm::Deserializer& D); 651}; 652 653/// IndirectGotoStmt - This represents an indirect goto. 654/// 655class IndirectGotoStmt : public Stmt { 656 Expr *Target; 657 // FIXME: Add location information (e.g. SourceLocation objects). 658 // When doing so, update the serialization routines. 659public: 660 IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass), Target(target){} 661 662 Expr *getTarget() { return Target; } 663 const Expr *getTarget() const { return Target; } 664 665 virtual SourceRange getSourceRange() const { return SourceRange(); } 666 667 static bool classof(const Stmt *T) { 668 return T->getStmtClass() == IndirectGotoStmtClass; 669 } 670 static bool classof(const IndirectGotoStmt *) { return true; } 671 672 // Iterators 673 virtual child_iterator child_begin(); 674 virtual child_iterator child_end(); 675 676 virtual void EmitImpl(llvm::Serializer& S) const; 677 static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D); 678}; 679 680 681/// ContinueStmt - This represents a continue. 682/// 683class ContinueStmt : public Stmt { 684 SourceLocation ContinueLoc; 685public: 686 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 687 688 virtual SourceRange getSourceRange() const { 689 return SourceRange(ContinueLoc); 690 } 691 static bool classof(const Stmt *T) { 692 return T->getStmtClass() == ContinueStmtClass; 693 } 694 static bool classof(const ContinueStmt *) { return true; } 695 696 // Iterators 697 virtual child_iterator child_begin(); 698 virtual child_iterator child_end(); 699 700 virtual void EmitImpl(llvm::Serializer& S) const; 701 static ContinueStmt* CreateImpl(llvm::Deserializer& D); 702}; 703 704/// BreakStmt - This represents a break. 705/// 706class BreakStmt : public Stmt { 707 SourceLocation BreakLoc; 708public: 709 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 710 711 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } 712 713 static bool classof(const Stmt *T) { 714 return T->getStmtClass() == BreakStmtClass; 715 } 716 static bool classof(const BreakStmt *) { return true; } 717 718 // Iterators 719 virtual child_iterator child_begin(); 720 virtual child_iterator child_end(); 721 722 virtual void EmitImpl(llvm::Serializer& S) const; 723 static BreakStmt* CreateImpl(llvm::Deserializer& D); 724}; 725 726 727/// ReturnStmt - This represents a return, optionally of an expression. 728/// 729class ReturnStmt : public Stmt { 730 Expr *RetExpr; 731 SourceLocation RetLoc; 732public: 733 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), 734 RetExpr(E), RetLoc(RL) {} 735 736 const Expr *getRetValue() const { return RetExpr; } 737 Expr *getRetValue() { return RetExpr; } 738 739 virtual SourceRange getSourceRange() const; 740 741 static bool classof(const Stmt *T) { 742 return T->getStmtClass() == ReturnStmtClass; 743 } 744 static bool classof(const ReturnStmt *) { return true; } 745 746 // Iterators 747 virtual child_iterator child_begin(); 748 virtual child_iterator child_end(); 749 750 virtual void EmitImpl(llvm::Serializer& S) const; 751 static ReturnStmt* CreateImpl(llvm::Deserializer& D); 752}; 753 754/// AsmStmt - This represents a GNU inline-assembly statement extension. 755/// 756class AsmStmt : public Stmt { 757 SourceLocation AsmLoc, RParenLoc; 758 StringLiteral *AsmStr; 759 760 bool IsVolatile; 761 762 unsigned NumOutputs; 763 unsigned NumInputs; 764 765 llvm::SmallVector<std::string, 4> Names; 766 llvm::SmallVector<StringLiteral*, 4> Constraints; 767 llvm::SmallVector<Expr*, 4> Exprs; 768 769 llvm::SmallVector<StringLiteral*, 4> Clobbers; 770public: 771 AsmStmt(SourceLocation asmloc, 772 bool isvolatile, 773 unsigned numoutputs, 774 unsigned numinputs, 775 std::string *names, 776 StringLiteral **constraints, 777 Expr **exprs, 778 StringLiteral *asmstr, 779 unsigned numclobbers, 780 StringLiteral **clobbers, 781 SourceLocation rparenloc); 782 783 bool isVolatile() const { return IsVolatile; } 784 785 unsigned getNumOutputs() const { return NumOutputs; } 786 const std::string &getOutputName(unsigned i) const 787 { return Names[i]; } 788 const StringLiteral *getOutputConstraint(unsigned i) const 789 { return Constraints[i]; } 790 StringLiteral *getOutputConstraint(unsigned i) 791 { return Constraints[i]; } 792 const Expr *getOutputExpr(unsigned i) const { return Exprs[i]; } 793 Expr *getOutputExpr(unsigned i) { return Exprs[i]; } 794 795 unsigned getNumInputs() const { return NumInputs; } 796 const std::string &getInputName(unsigned i) const 797 { return Names[i + NumOutputs]; } 798 StringLiteral *getInputConstraint(unsigned i) 799 { return Constraints[i + NumOutputs]; } 800 const StringLiteral *getInputConstraint(unsigned i) const 801 { return Constraints[i + NumOutputs]; } 802 Expr *getInputExpr(unsigned i) { return Exprs[i + NumOutputs]; } 803 const Expr *getInputExpr(unsigned i) const { return Exprs[i + NumOutputs]; } 804 805 const StringLiteral *getAsmString() const { return AsmStr; } 806 StringLiteral *getAsmString() { return AsmStr; } 807 808 unsigned getNumClobbers() const { return Clobbers.size(); } 809 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 810 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 811 812 virtual SourceRange getSourceRange() const { 813 return SourceRange(AsmLoc, RParenLoc); 814 } 815 816 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 817 static bool classof(const AsmStmt *) { return true; } 818 819 virtual child_iterator child_begin(); 820 virtual child_iterator child_end(); 821 822 virtual void EmitImpl(llvm::Serializer& S) const; 823 static AsmStmt* CreateImpl(llvm::Deserializer& D); 824}; 825 826/// ObjCAtCatchStmt - This represents objective-c's @catch statement. 827class ObjCAtCatchStmt : public Stmt { 828private: 829 // Points to next @catch statement, or null 830 ObjCAtCatchStmt *NextAtCatchStmt; 831 enum { SELECTOR, BODY, END_EXPR }; 832 Stmt *SubExprs[END_EXPR]; 833 SourceLocation AtCatchLoc, RParenLoc; 834 835 // Used by deserialization. 836 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc) 837 : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {} 838 839public: 840 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc, 841 Stmt *catchVarStmtDecl, Stmt *atCatchStmt, Stmt *atCatchList) 842 : Stmt(ObjCAtCatchStmtClass) { 843 SubExprs[SELECTOR] = catchVarStmtDecl; 844 SubExprs[BODY] = atCatchStmt; 845 if (!atCatchList) 846 NextAtCatchStmt = NULL; 847 else { 848 ObjCAtCatchStmt *AtCatchList = 849 static_cast<ObjCAtCatchStmt*>(atCatchList); 850 while (AtCatchList->NextAtCatchStmt) 851 AtCatchList = AtCatchList->NextAtCatchStmt; 852 AtCatchList->NextAtCatchStmt = this; 853 } 854 AtCatchLoc = atCatchLoc; 855 RParenLoc = rparenloc; 856 } 857 858 const Stmt *getCatchBody() const { return SubExprs[BODY]; } 859 Stmt *getCatchBody() { return SubExprs[BODY]; } 860 const ObjCAtCatchStmt *getNextCatchStmt() const { return NextAtCatchStmt; } 861 ObjCAtCatchStmt *getNextCatchStmt() { return NextAtCatchStmt; } 862 const Stmt *getCatchParamStmt() const { return SubExprs[SELECTOR]; } 863 Stmt *getCatchParamStmt() { return SubExprs[SELECTOR]; } 864 865 SourceLocation getRParenLoc() const { return RParenLoc; } 866 867 virtual SourceRange getSourceRange() const { 868 return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd()); 869 } 870 871 static bool classof(const Stmt *T) { 872 return T->getStmtClass() == ObjCAtCatchStmtClass; 873 } 874 static bool classof(const ObjCAtCatchStmt *) { return true; } 875 876 virtual child_iterator child_begin(); 877 virtual child_iterator child_end(); 878 879 virtual void EmitImpl(llvm::Serializer& S) const; 880 static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D); 881}; 882 883/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement 884class ObjCAtFinallyStmt : public Stmt { 885 Stmt *AtFinallyStmt; 886 SourceLocation AtFinallyLoc; 887public: 888 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt) 889 : Stmt(ObjCAtFinallyStmtClass), 890 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {} 891 892 const Stmt *getFinallyBody () const { return AtFinallyStmt; } 893 Stmt *getFinallyBody () { return AtFinallyStmt; } 894 895 virtual SourceRange getSourceRange() const { 896 return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd()); 897 } 898 899 static bool classof(const Stmt *T) { 900 return T->getStmtClass() == ObjCAtFinallyStmtClass; 901 } 902 static bool classof(const ObjCAtFinallyStmt *) { return true; } 903 904 virtual child_iterator child_begin(); 905 virtual child_iterator child_end(); 906 907 virtual void EmitImpl(llvm::Serializer& S) const; 908 static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D); 909}; 910 911/// ObjCAtTryStmt - This represent objective-c's over-all 912/// @try ... @catch ... @finally statement. 913class ObjCAtTryStmt : public Stmt { 914private: 915 enum { TRY, CATCH, FINALLY, END_EXPR }; 916 Stmt* SubStmts[END_EXPR]; 917 918 SourceLocation AtTryLoc; 919public: 920 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 921 Stmt *atCatchStmt, 922 Stmt *atFinallyStmt) 923 : Stmt(ObjCAtTryStmtClass) { 924 SubStmts[TRY] = atTryStmt; 925 SubStmts[CATCH] = atCatchStmt; 926 SubStmts[FINALLY] = atFinallyStmt; 927 AtTryLoc = atTryLoc; 928 } 929 930 const Stmt *getTryBody() const { return SubStmts[TRY]; } 931 Stmt *getTryBody() { return SubStmts[TRY]; } 932 const ObjCAtCatchStmt *getCatchStmts() const { 933 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 934 } 935 ObjCAtCatchStmt *getCatchStmts() { 936 return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]); 937 } 938 const ObjCAtFinallyStmt *getFinallyStmt() const { 939 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 940 } 941 ObjCAtFinallyStmt *getFinallyStmt() { 942 return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]); 943 } 944 virtual SourceRange getSourceRange() const { 945 return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd()); 946 } 947 948 static bool classof(const Stmt *T) { 949 return T->getStmtClass() == ObjCAtTryStmtClass; 950 } 951 static bool classof(const ObjCAtTryStmt *) { return true; } 952 953 virtual child_iterator child_begin(); 954 virtual child_iterator child_end(); 955 956 virtual void EmitImpl(llvm::Serializer& S) const; 957 static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D); 958}; 959 960/// ObjCAtThrowStmt - This represents objective-c's @throw statement. 961class ObjCAtThrowStmt : public Stmt { 962 Stmt *Throw; 963 SourceLocation AtThrowLoc; 964public: 965 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr) 966 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) { 967 AtThrowLoc = atThrowLoc; 968 } 969 970 Expr *const getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); } 971 972 virtual SourceRange getSourceRange() const { 973 return SourceRange(AtThrowLoc, Throw->getLocEnd()); 974 } 975 976 static bool classof(const Stmt *T) { 977 return T->getStmtClass() == ObjCAtThrowStmtClass; 978 } 979 static bool classof(const ObjCAtThrowStmt *) { return true; } 980 981 virtual child_iterator child_begin(); 982 virtual child_iterator child_end(); 983 984 virtual void EmitImpl(llvm::Serializer& S) const; 985 static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D); 986}; 987 988} // end namespace clang 989 990#endif 991