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