Stmt.h revision 861ce3178c70cfb0fa50baf685e1ad363538eaa9
1//===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Stmt interface and subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_STMT_H 15#define LLVM_CLANG_AST_STMT_H 16 17#include "llvm/Support/Casting.h" 18#include "llvm/Support/raw_ostream.h" 19#include "clang/Basic/SourceLocation.h" 20#include "clang/AST/StmtIterator.h" 21#include "clang/AST/DeclGroup.h" 22#include "llvm/ADT/SmallVector.h" 23#include "llvm/ADT/iterator.h" 24#include "clang/AST/ASTContext.h" 25#include <string> 26using llvm::dyn_cast_or_null; 27 28namespace clang { 29 class ASTContext; 30 class Expr; 31 class Decl; 32 class ParmVarDecl; 33 class QualType; 34 class IdentifierInfo; 35 class SourceManager; 36 class StringLiteral; 37 class SwitchStmt; 38 class PrinterHelper; 39 40 //===----------------------------------------------------------------------===// 41 // ExprIterator - Iterators for iterating over Stmt* arrays that contain 42 // only Expr*. This is needed because AST nodes use Stmt* arrays to store 43 // references to children (to be compatible with StmtIterator). 44 //===----------------------------------------------------------------------===// 45 46 class Stmt; 47 class Expr; 48 49 class ExprIterator { 50 Stmt** I; 51 public: 52 ExprIterator(Stmt** i) : I(i) {} 53 ExprIterator() : I(0) {} 54 ExprIterator& operator++() { ++I; return *this; } 55 ExprIterator operator-(size_t i) { return I-i; } 56 ExprIterator operator+(size_t i) { return I+i; } 57 Expr* operator[](size_t idx); 58 // FIXME: Verify that this will correctly return a signed distance. 59 signed operator-(const ExprIterator& R) const { return I - R.I; } 60 Expr* operator*() const; 61 Expr* operator->() const; 62 bool operator==(const ExprIterator& R) const { return I == R.I; } 63 bool operator!=(const ExprIterator& R) const { return I != R.I; } 64 bool operator>(const ExprIterator& R) const { return I > R.I; } 65 bool operator>=(const ExprIterator& R) const { return I >= R.I; } 66 }; 67 68 class ConstExprIterator { 69 Stmt* const * I; 70 public: 71 ConstExprIterator(Stmt* const* i) : I(i) {} 72 ConstExprIterator() : I(0) {} 73 ConstExprIterator& operator++() { ++I; return *this; } 74 ConstExprIterator operator+(size_t i) { return I+i; } 75 ConstExprIterator operator-(size_t i) { return I-i; } 76 const Expr * operator[](size_t idx) const; 77 signed operator-(const ConstExprIterator& R) const { return I - R.I; } 78 const Expr * operator*() const; 79 const Expr * operator->() const; 80 bool operator==(const ConstExprIterator& R) const { return I == R.I; } 81 bool operator!=(const ConstExprIterator& R) const { return I != R.I; } 82 bool operator>(const ConstExprIterator& R) const { return I > R.I; } 83 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; } 84 }; 85 86//===----------------------------------------------------------------------===// 87// AST classes for statements. 88//===----------------------------------------------------------------------===// 89 90/// Stmt - This represents one statement. 91/// 92class Stmt { 93public: 94 enum StmtClass { 95 NoStmtClass = 0, 96#define STMT(CLASS, PARENT) CLASS##Class, 97#define FIRST_STMT(CLASS) firstStmtConstant = CLASS##Class, 98#define LAST_STMT(CLASS) lastStmtConstant = CLASS##Class, 99#define FIRST_EXPR(CLASS) firstExprConstant = CLASS##Class, 100#define LAST_EXPR(CLASS) lastExprConstant = CLASS##Class 101#include "clang/AST/StmtNodes.def" 102}; 103private: 104 const StmtClass sClass; 105 106 // Make vanilla 'new' and 'delete' illegal for Stmts. 107protected: 108 void* operator new(size_t bytes) throw() { 109 assert(0 && "Stmts cannot be allocated with regular 'new'."); 110 return 0; 111 } 112 void operator delete(void* data) throw() { 113 assert(0 && "Stmts cannot be released with regular 'delete'."); 114 } 115 116public: 117 // Only allow allocation of Stmts using the allocator in ASTContext 118 // or by doing a placement new. 119 void* operator new(size_t bytes, ASTContext& C, 120 unsigned alignment = 16) throw() { 121 return ::operator new(bytes, C, alignment); 122 } 123 124 void* operator new(size_t bytes, ASTContext* C, 125 unsigned alignment = 16) throw() { 126 return ::operator new(bytes, *C, alignment); 127 } 128 129 void* operator new(size_t bytes, void* mem) throw() { 130 return mem; 131 } 132 133 void operator delete(void*, ASTContext&, unsigned) throw() { } 134 void operator delete(void*, ASTContext*, unsigned) throw() { } 135 void operator delete(void*, std::size_t) throw() { } 136 void operator delete(void*, void*) throw() { } 137 138public: 139 /// \brief A placeholder type used to construct an empty shell of a 140 /// type, that will be filled in later (e.g., by some 141 /// de-serialization). 142 struct EmptyShell { }; 143 144protected: 145 /// DestroyChildren - Invoked by destructors of subclasses of Stmt to 146 /// recursively release child AST nodes. 147 void DestroyChildren(ASTContext& Ctx); 148 149 /// \brief Construct an empty statement. 150 explicit Stmt(StmtClass SC, EmptyShell) : sClass(SC) { 151 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC); 152 } 153 154public: 155 Stmt(StmtClass SC) : sClass(SC) { 156 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC); 157 } 158 virtual ~Stmt() {} 159 160 virtual void Destroy(ASTContext &Ctx); 161 162 StmtClass getStmtClass() const { return sClass; } 163 const char *getStmtClassName() const; 164 165 /// SourceLocation tokens are not useful in isolation - they are low level 166 /// value objects created/interpreted by SourceManager. We assume AST 167 /// clients will have a pointer to the respective SourceManager. 168 virtual SourceRange getSourceRange() const = 0; 169 SourceLocation getLocStart() const { return getSourceRange().getBegin(); } 170 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); } 171 172 // global temp stats (until we have a per-module visitor) 173 static void addStmtClass(const StmtClass s); 174 static bool CollectingStats(bool enable=false); 175 static void PrintStats(); 176 177 /// dump - This does a local dump of the specified AST fragment. It dumps the 178 /// specified node and a few nodes underneath it, but not the whole subtree. 179 /// This is useful in a debugger. 180 void dump() const; 181 void dump(SourceManager &SM) const; 182 183 /// dumpAll - This does a dump of the specified AST fragment and all subtrees. 184 void dumpAll() const; 185 void dumpAll(SourceManager &SM) const; 186 187 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST 188 /// back to its original source language syntax. 189 void dumpPretty() const; 190 void printPretty(llvm::raw_ostream &OS, PrinterHelper* = NULL, unsigned = 0, 191 bool NoIndent=false) const; 192 193 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only 194 /// works on systems with GraphViz (Mac OS X) or dot+gv installed. 195 void viewAST() const; 196 197 // Implement isa<T> support. 198 static bool classof(const Stmt *) { return true; } 199 200 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations) 201 /// contain implicit control-flow in the order their subexpressions 202 /// are evaluated. This predicate returns true if this statement has 203 /// such implicit control-flow. Such statements are also specially handled 204 /// within CFGs. 205 bool hasImplicitControlFlow() const; 206 207 /// Child Iterators: All subclasses must implement child_begin and child_end 208 /// to permit easy iteration over the substatements/subexpessions of an 209 /// AST node. This permits easy iteration over all nodes in the AST. 210 typedef StmtIterator child_iterator; 211 typedef ConstStmtIterator const_child_iterator; 212 213 virtual child_iterator child_begin() = 0; 214 virtual child_iterator child_end() = 0; 215 216 const_child_iterator child_begin() const { 217 return const_child_iterator(const_cast<Stmt*>(this)->child_begin()); 218 } 219 220 const_child_iterator child_end() const { 221 return const_child_iterator(const_cast<Stmt*>(this)->child_end()); 222 } 223}; 224 225/// DeclStmt - Adaptor class for mixing declarations with statements and 226/// expressions. For example, CompoundStmt mixes statements, expressions 227/// and declarations (variables, types). Another example is ForStmt, where 228/// the first statement can be an expression or a declaration. 229/// 230class DeclStmt : public Stmt { 231 DeclGroupRef DG; 232 SourceLocation StartLoc, EndLoc; 233public: 234 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, 235 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg), 236 StartLoc(startLoc), EndLoc(endLoc) {} 237 238 /// \brief Build an empty declaration statement. 239 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { } 240 241 virtual void Destroy(ASTContext& Ctx); 242 243 /// isSingleDecl - This method returns true if this DeclStmt refers 244 /// to a single Decl. 245 bool isSingleDecl() const { 246 return DG.isSingleDecl(); 247 } 248 249 const Decl *getSingleDecl() const { return DG.getSingleDecl(); } 250 Decl *getSingleDecl() { return DG.getSingleDecl(); } 251 252 const DeclGroupRef getDeclGroup() const { return DG; } 253 DeclGroupRef getDeclGroup() { return DG; } 254 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; } 255 256 SourceLocation getStartLoc() const { return StartLoc; } 257 void setStartLoc(SourceLocation L) { StartLoc = L; } 258 SourceLocation getEndLoc() const { return EndLoc; } 259 void setEndLoc(SourceLocation L) { EndLoc = L; } 260 261 SourceRange getSourceRange() const { 262 return SourceRange(StartLoc, EndLoc); 263 } 264 265 static bool classof(const Stmt *T) { 266 return T->getStmtClass() == DeclStmtClass; 267 } 268 static bool classof(const DeclStmt *) { return true; } 269 270 // Iterators over subexpressions. 271 virtual child_iterator child_begin(); 272 virtual child_iterator child_end(); 273 274 typedef DeclGroupRef::iterator decl_iterator; 275 typedef DeclGroupRef::const_iterator const_decl_iterator; 276 277 decl_iterator decl_begin() { return DG.begin(); } 278 decl_iterator decl_end() { return DG.end(); } 279 const_decl_iterator decl_begin() const { return DG.begin(); } 280 const_decl_iterator decl_end() const { return DG.end(); } 281}; 282 283/// NullStmt - This is the null statement ";": C99 6.8.3p3. 284/// 285class NullStmt : public Stmt { 286 SourceLocation SemiLoc; 287public: 288 NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {} 289 290 /// \brief Build an empty null statement. 291 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { } 292 293 NullStmt* Clone(ASTContext &C) const; 294 295 SourceLocation getSemiLoc() const { return SemiLoc; } 296 void setSemiLoc(SourceLocation L) { SemiLoc = L; } 297 298 virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); } 299 300 static bool classof(const Stmt *T) { 301 return T->getStmtClass() == NullStmtClass; 302 } 303 static bool classof(const NullStmt *) { return true; } 304 305 // Iterators 306 virtual child_iterator child_begin(); 307 virtual child_iterator child_end(); 308}; 309 310/// CompoundStmt - This represents a group of statements like { stmt stmt }. 311/// 312class CompoundStmt : public Stmt { 313 Stmt** Body; 314 unsigned NumStmts; 315 SourceLocation LBracLoc, RBracLoc; 316public: 317 CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts, 318 SourceLocation LB, SourceLocation RB) 319 : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) { 320 if (NumStmts == 0) { 321 Body = 0; 322 return; 323 } 324 325 Body = new (C) Stmt*[NumStmts]; 326 memcpy(Body, StmtStart, numStmts * sizeof(*Body)); 327 } 328 329 // \brief Build an empty compound statement. 330 explicit CompoundStmt(EmptyShell Empty) 331 : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { } 332 333 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts); 334 335 bool body_empty() const { return NumStmts == 0; } 336 unsigned size() const { return NumStmts; } 337 338 typedef Stmt** body_iterator; 339 body_iterator body_begin() { return Body; } 340 body_iterator body_end() { return Body + NumStmts; } 341 Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; } 342 343 typedef Stmt* const * const_body_iterator; 344 const_body_iterator body_begin() const { return Body; } 345 const_body_iterator body_end() const { return Body + NumStmts; } 346 const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; } 347 348 typedef std::reverse_iterator<body_iterator> reverse_body_iterator; 349 reverse_body_iterator body_rbegin() { 350 return reverse_body_iterator(body_end()); 351 } 352 reverse_body_iterator body_rend() { 353 return reverse_body_iterator(body_begin()); 354 } 355 356 typedef std::reverse_iterator<const_body_iterator> 357 const_reverse_body_iterator; 358 359 const_reverse_body_iterator body_rbegin() const { 360 return const_reverse_body_iterator(body_end()); 361 } 362 363 const_reverse_body_iterator body_rend() const { 364 return const_reverse_body_iterator(body_begin()); 365 } 366 367 virtual SourceRange getSourceRange() const { 368 return SourceRange(LBracLoc, RBracLoc); 369 } 370 371 SourceLocation getLBracLoc() const { return LBracLoc; } 372 void setLBracLoc(SourceLocation L) { LBracLoc = L; } 373 SourceLocation getRBracLoc() const { return RBracLoc; } 374 void setRBracLoc(SourceLocation L) { RBracLoc = L; } 375 376 static bool classof(const Stmt *T) { 377 return T->getStmtClass() == CompoundStmtClass; 378 } 379 static bool classof(const CompoundStmt *) { return true; } 380 381 // Iterators 382 virtual child_iterator child_begin(); 383 virtual child_iterator child_end(); 384}; 385 386// SwitchCase is the base class for CaseStmt and DefaultStmt, 387class SwitchCase : public Stmt { 388protected: 389 // A pointer to the following CaseStmt or DefaultStmt class, 390 // used by SwitchStmt. 391 SwitchCase *NextSwitchCase; 392 393 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {} 394 395public: 396 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 397 398 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 399 400 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 401 402 Stmt *getSubStmt() { return v_getSubStmt(); } 403 404 virtual SourceRange getSourceRange() const { return SourceRange(); } 405 406 static bool classof(const Stmt *T) { 407 return T->getStmtClass() == CaseStmtClass || 408 T->getStmtClass() == DefaultStmtClass; 409 } 410 static bool classof(const SwitchCase *) { return true; } 411protected: 412 virtual Stmt* v_getSubStmt() = 0; 413}; 414 415class CaseStmt : public SwitchCase { 416 enum { SUBSTMT, LHS, RHS, END_EXPR }; 417 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for 418 // GNU "case 1 ... 4" extension 419 SourceLocation CaseLoc; 420 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 421public: 422 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc) 423 : SwitchCase(CaseStmtClass) { 424 SubExprs[SUBSTMT] = 0; 425 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs); 426 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs); 427 CaseLoc = caseLoc; 428 } 429 430 /// \brief Build an empty switch case statement. 431 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { } 432 433 SourceLocation getCaseLoc() const { return CaseLoc; } 434 void setCaseLoc(SourceLocation L) { CaseLoc = L; } 435 436 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); } 437 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); } 438 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } 439 440 const Expr *getLHS() const { 441 return reinterpret_cast<const Expr*>(SubExprs[LHS]); 442 } 443 const Expr *getRHS() const { 444 return reinterpret_cast<const Expr*>(SubExprs[RHS]); 445 } 446 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; } 447 448 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; } 449 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); } 450 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } 451 452 453 virtual SourceRange getSourceRange() const { 454 // Handle deeply nested case statements with iteration instead of recursion. 455 const CaseStmt *CS = this; 456 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) 457 CS = CS2; 458 459 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd()); 460 } 461 static bool classof(const Stmt *T) { 462 return T->getStmtClass() == CaseStmtClass; 463 } 464 static bool classof(const CaseStmt *) { return true; } 465 466 // Iterators 467 virtual child_iterator child_begin(); 468 virtual child_iterator child_end(); 469}; 470 471class DefaultStmt : public SwitchCase { 472 Stmt* SubStmt; 473 SourceLocation DefaultLoc; 474 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 475public: 476 DefaultStmt(SourceLocation DL, Stmt *substmt) : 477 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {} 478 479 /// \brief Build an empty default statement. 480 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { } 481 482 Stmt *getSubStmt() { return SubStmt; } 483 const Stmt *getSubStmt() const { return SubStmt; } 484 void setSubStmt(Stmt *S) { SubStmt = S; } 485 486 SourceLocation getDefaultLoc() const { return DefaultLoc; } 487 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; } 488 489 virtual SourceRange getSourceRange() const { 490 return SourceRange(DefaultLoc, SubStmt->getLocEnd()); 491 } 492 static bool classof(const Stmt *T) { 493 return T->getStmtClass() == DefaultStmtClass; 494 } 495 static bool classof(const DefaultStmt *) { return true; } 496 497 // Iterators 498 virtual child_iterator child_begin(); 499 virtual child_iterator child_end(); 500}; 501 502class LabelStmt : public Stmt { 503 IdentifierInfo *Label; 504 Stmt *SubStmt; 505 SourceLocation IdentLoc; 506public: 507 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) 508 : Stmt(LabelStmtClass), Label(label), 509 SubStmt(substmt), IdentLoc(IL) {} 510 511 // \brief Build an empty label statement. 512 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { } 513 514 SourceLocation getIdentLoc() const { return IdentLoc; } 515 IdentifierInfo *getID() const { return Label; } 516 void setID(IdentifierInfo *II) { Label = II; } 517 const char *getName() const; 518 Stmt *getSubStmt() { return SubStmt; } 519 const Stmt *getSubStmt() const { return SubStmt; } 520 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 521 void setSubStmt(Stmt *SS) { SubStmt = SS; } 522 523 virtual SourceRange getSourceRange() const { 524 return SourceRange(IdentLoc, SubStmt->getLocEnd()); 525 } 526 static bool classof(const Stmt *T) { 527 return T->getStmtClass() == LabelStmtClass; 528 } 529 static bool classof(const LabelStmt *) { return true; } 530 531 // Iterators 532 virtual child_iterator child_begin(); 533 virtual child_iterator child_end(); 534}; 535 536 537/// IfStmt - This represents an if/then/else. 538/// 539class IfStmt : public Stmt { 540 enum { COND, THEN, ELSE, END_EXPR }; 541 Stmt* SubExprs[END_EXPR]; 542 SourceLocation IfLoc; 543 SourceLocation ElseLoc; 544public: 545 IfStmt(SourceLocation IL, Expr *cond, Stmt *then, 546 SourceLocation EL = SourceLocation(), Stmt *elsev = 0) 547 : Stmt(IfStmtClass) { 548 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 549 SubExprs[THEN] = then; 550 SubExprs[ELSE] = elsev; 551 IfLoc = IL; 552 ElseLoc = EL; 553 } 554 555 /// \brief Build an empty if/then/else statement 556 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { } 557 558 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 559 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 560 const Stmt *getThen() const { return SubExprs[THEN]; } 561 void setThen(Stmt *S) { SubExprs[THEN] = S; } 562 const Stmt *getElse() const { return SubExprs[ELSE]; } 563 void setElse(Stmt *S) { SubExprs[ELSE] = S; } 564 565 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 566 Stmt *getThen() { return SubExprs[THEN]; } 567 Stmt *getElse() { return SubExprs[ELSE]; } 568 569 SourceLocation getIfLoc() const { return IfLoc; } 570 void setIfLoc(SourceLocation L) { IfLoc = L; } 571 SourceLocation getElseLoc() const { return ElseLoc; } 572 void setElseLoc(SourceLocation L) { ElseLoc = L; } 573 574 virtual SourceRange getSourceRange() const { 575 if (SubExprs[ELSE]) 576 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); 577 else 578 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); 579 } 580 581 static bool classof(const Stmt *T) { 582 return T->getStmtClass() == IfStmtClass; 583 } 584 static bool classof(const IfStmt *) { return true; } 585 586 // Iterators 587 virtual child_iterator child_begin(); 588 virtual child_iterator child_end(); 589}; 590 591/// SwitchStmt - This represents a 'switch' stmt. 592/// 593class SwitchStmt : public Stmt { 594 enum { COND, BODY, END_EXPR }; 595 Stmt* SubExprs[END_EXPR]; 596 // This points to a linked list of case and default statements. 597 SwitchCase *FirstCase; 598 SourceLocation SwitchLoc; 599public: 600 SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) { 601 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 602 SubExprs[BODY] = NULL; 603 } 604 605 /// \brief Build a empty switch statement. 606 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { } 607 608 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 609 const Stmt *getBody() const { return SubExprs[BODY]; } 610 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 611 612 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 613 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 614 Stmt *getBody() { return SubExprs[BODY]; } 615 void setBody(Stmt *S) { SubExprs[BODY] = S; } 616 SwitchCase *getSwitchCaseList() { return FirstCase; } 617 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } 618 619 SourceLocation getSwitchLoc() const { return SwitchLoc; } 620 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; } 621 622 void setBody(Stmt *S, SourceLocation SL) { 623 SubExprs[BODY] = S; 624 SwitchLoc = SL; 625 } 626 void addSwitchCase(SwitchCase *SC) { 627 assert(!SC->getNextSwitchCase() && "case/default already added to a switch"); 628 SC->setNextSwitchCase(FirstCase); 629 FirstCase = SC; 630 } 631 virtual SourceRange getSourceRange() const { 632 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); 633 } 634 static bool classof(const Stmt *T) { 635 return T->getStmtClass() == SwitchStmtClass; 636 } 637 static bool classof(const SwitchStmt *) { return true; } 638 639 // Iterators 640 virtual child_iterator child_begin(); 641 virtual child_iterator child_end(); 642}; 643 644 645/// WhileStmt - This represents a 'while' stmt. 646/// 647class WhileStmt : public Stmt { 648 enum { COND, BODY, END_EXPR }; 649 Stmt* SubExprs[END_EXPR]; 650 SourceLocation WhileLoc; 651public: 652 WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) { 653 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 654 SubExprs[BODY] = body; 655 WhileLoc = WL; 656 } 657 658 /// \brief Build an empty while statement. 659 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { } 660 661 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 662 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 663 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 664 Stmt *getBody() { return SubExprs[BODY]; } 665 const Stmt *getBody() const { return SubExprs[BODY]; } 666 void setBody(Stmt *S) { SubExprs[BODY] = S; } 667 668 SourceLocation getWhileLoc() const { return WhileLoc; } 669 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 670 671 virtual SourceRange getSourceRange() const { 672 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); 673 } 674 static bool classof(const Stmt *T) { 675 return T->getStmtClass() == WhileStmtClass; 676 } 677 static bool classof(const WhileStmt *) { return true; } 678 679 // Iterators 680 virtual child_iterator child_begin(); 681 virtual child_iterator child_end(); 682}; 683 684/// DoStmt - This represents a 'do/while' stmt. 685/// 686class DoStmt : public Stmt { 687 enum { COND, BODY, END_EXPR }; 688 Stmt* SubExprs[END_EXPR]; 689 SourceLocation DoLoc; 690 SourceLocation WhileLoc; 691 692public: 693 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL) 694 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL) { 695 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 696 SubExprs[BODY] = body; 697 DoLoc = DL; 698 WhileLoc = WL; 699 } 700 701 /// \brief Build an empty do-while statement. 702 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { } 703 704 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 705 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 706 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 707 Stmt *getBody() { return SubExprs[BODY]; } 708 const Stmt *getBody() const { return SubExprs[BODY]; } 709 void setBody(Stmt *S) { SubExprs[BODY] = S; } 710 711 SourceLocation getDoLoc() const { return DoLoc; } 712 void setDoLoc(SourceLocation L) { DoLoc = L; } 713 SourceLocation getWhileLoc() const { return WhileLoc; } 714 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 715 716 virtual SourceRange getSourceRange() const { 717 return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 718 } 719 static bool classof(const Stmt *T) { 720 return T->getStmtClass() == DoStmtClass; 721 } 722 static bool classof(const DoStmt *) { return true; } 723 724 // Iterators 725 virtual child_iterator child_begin(); 726 virtual child_iterator child_end(); 727}; 728 729 730/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 731/// the init/cond/inc parts of the ForStmt will be null if they were not 732/// specified in the source. 733/// 734class ForStmt : public Stmt { 735 enum { INIT, COND, INC, BODY, END_EXPR }; 736 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 737 SourceLocation ForLoc; 738 SourceLocation LParenLoc, RParenLoc; 739 740public: 741 ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL, 742 SourceLocation LP, SourceLocation RP) 743 : Stmt(ForStmtClass) { 744 SubExprs[INIT] = Init; 745 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 746 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 747 SubExprs[BODY] = Body; 748 ForLoc = FL; 749 LParenLoc = LP; 750 RParenLoc = RP; 751 } 752 753 /// \brief Build an empty for statement. 754 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { } 755 756 Stmt *getInit() { return SubExprs[INIT]; } 757 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 758 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 759 Stmt *getBody() { return SubExprs[BODY]; } 760 761 const Stmt *getInit() const { return SubExprs[INIT]; } 762 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 763 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 764 const Stmt *getBody() const { return SubExprs[BODY]; } 765 766 void setInit(Stmt *S) { SubExprs[INIT] = S; } 767 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 768 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); } 769 void setBody(Stmt *S) { SubExprs[BODY] = S; } 770 771 SourceLocation getForLoc() const { return ForLoc; } 772 void setForLoc(SourceLocation L) { ForLoc = L; } 773 SourceLocation getLParenLoc() const { return LParenLoc; } 774 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 775 SourceLocation getRParenLoc() const { return RParenLoc; } 776 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 777 778 virtual SourceRange getSourceRange() const { 779 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 780 } 781 static bool classof(const Stmt *T) { 782 return T->getStmtClass() == ForStmtClass; 783 } 784 static bool classof(const ForStmt *) { return true; } 785 786 // Iterators 787 virtual child_iterator child_begin(); 788 virtual child_iterator child_end(); 789}; 790 791/// GotoStmt - This represents a direct goto. 792/// 793class GotoStmt : public Stmt { 794 LabelStmt *Label; 795 SourceLocation GotoLoc; 796 SourceLocation LabelLoc; 797public: 798 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL) 799 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 800 801 /// \brief Build an empty goto statement. 802 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { } 803 804 LabelStmt *getLabel() const { return Label; } 805 void setLabel(LabelStmt *S) { Label = S; } 806 807 SourceLocation getGotoLoc() const { return GotoLoc; } 808 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 809 SourceLocation getLabelLoc() const { return LabelLoc; } 810 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 811 812 virtual SourceRange getSourceRange() const { 813 return SourceRange(GotoLoc, LabelLoc); 814 } 815 static bool classof(const Stmt *T) { 816 return T->getStmtClass() == GotoStmtClass; 817 } 818 static bool classof(const GotoStmt *) { return true; } 819 820 // Iterators 821 virtual child_iterator child_begin(); 822 virtual child_iterator child_end(); 823}; 824 825/// IndirectGotoStmt - This represents an indirect goto. 826/// 827class IndirectGotoStmt : public Stmt { 828 SourceLocation GotoLoc; 829 Stmt *Target; 830public: 831 IndirectGotoStmt(SourceLocation gotoLoc, Expr *target) 832 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), Target((Stmt*)target) {} 833 834 /// \brief Build an empty indirect goto statement. 835 explicit IndirectGotoStmt(EmptyShell Empty) 836 : Stmt(IndirectGotoStmtClass, Empty) { } 837 838 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 839 SourceLocation getGotoLoc() const { return GotoLoc; } 840 841 Expr *getTarget(); 842 const Expr *getTarget() const; 843 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); } 844 845 virtual SourceRange getSourceRange() const { 846 return SourceRange(GotoLoc, Target->getLocEnd()); 847 } 848 849 static bool classof(const Stmt *T) { 850 return T->getStmtClass() == IndirectGotoStmtClass; 851 } 852 static bool classof(const IndirectGotoStmt *) { return true; } 853 854 // Iterators 855 virtual child_iterator child_begin(); 856 virtual child_iterator child_end(); 857}; 858 859 860/// ContinueStmt - This represents a continue. 861/// 862class ContinueStmt : public Stmt { 863 SourceLocation ContinueLoc; 864public: 865 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 866 867 /// \brief Build an empty continue statement. 868 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { } 869 870 SourceLocation getContinueLoc() const { return ContinueLoc; } 871 void setContinueLoc(SourceLocation L) { ContinueLoc = L; } 872 873 virtual SourceRange getSourceRange() const { 874 return SourceRange(ContinueLoc); 875 } 876 877 ContinueStmt* Clone(ASTContext &C) const; 878 879 static bool classof(const Stmt *T) { 880 return T->getStmtClass() == ContinueStmtClass; 881 } 882 static bool classof(const ContinueStmt *) { return true; } 883 884 // Iterators 885 virtual child_iterator child_begin(); 886 virtual child_iterator child_end(); 887}; 888 889/// BreakStmt - This represents a break. 890/// 891class BreakStmt : public Stmt { 892 SourceLocation BreakLoc; 893public: 894 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 895 896 /// \brief Build an empty break statement. 897 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { } 898 899 SourceLocation getBreakLoc() const { return BreakLoc; } 900 void setBreakLoc(SourceLocation L) { BreakLoc = L; } 901 902 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } 903 904 BreakStmt* Clone(ASTContext &C) const; 905 906 static bool classof(const Stmt *T) { 907 return T->getStmtClass() == BreakStmtClass; 908 } 909 static bool classof(const BreakStmt *) { return true; } 910 911 // Iterators 912 virtual child_iterator child_begin(); 913 virtual child_iterator child_end(); 914}; 915 916 917/// ReturnStmt - This represents a return, optionally of an expression: 918/// return; 919/// return 4; 920/// 921/// Note that GCC allows return with no argument in a function declared to 922/// return a value, and it allows returning a value in functions declared to 923/// return void. We explicitly model this in the AST, which means you can't 924/// depend on the return type of the function and the presence of an argument. 925/// 926class ReturnStmt : public Stmt { 927 Stmt *RetExpr; 928 SourceLocation RetLoc; 929public: 930 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), 931 RetExpr((Stmt*) E), RetLoc(RL) {} 932 933 /// \brief Build an empty return expression. 934 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { } 935 936 const Expr *getRetValue() const; 937 Expr *getRetValue(); 938 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); } 939 940 SourceLocation getReturnLoc() const { return RetLoc; } 941 void setReturnLoc(SourceLocation L) { RetLoc = L; } 942 943 virtual SourceRange getSourceRange() const; 944 945 static bool classof(const Stmt *T) { 946 return T->getStmtClass() == ReturnStmtClass; 947 } 948 static bool classof(const ReturnStmt *) { return true; } 949 950 // Iterators 951 virtual child_iterator child_begin(); 952 virtual child_iterator child_end(); 953}; 954 955/// AsmStmt - This represents a GNU inline-assembly statement extension. 956/// 957class AsmStmt : public Stmt { 958 SourceLocation AsmLoc, RParenLoc; 959 StringLiteral *AsmStr; 960 961 bool IsSimple; 962 bool IsVolatile; 963 964 unsigned NumOutputs; 965 unsigned NumInputs; 966 967 llvm::SmallVector<std::string, 4> Names; 968 llvm::SmallVector<StringLiteral*, 4> Constraints; 969 llvm::SmallVector<Stmt*, 4> Exprs; 970 971 llvm::SmallVector<StringLiteral*, 4> Clobbers; 972public: 973 AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile, 974 unsigned numoutputs, unsigned numinputs, 975 std::string *names, StringLiteral **constraints, 976 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers, 977 StringLiteral **clobbers, SourceLocation rparenloc); 978 979 /// \brief Build an empty inline-assembly statement. 980 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty) { } 981 982 SourceLocation getAsmLoc() const { return AsmLoc; } 983 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 984 SourceLocation getRParenLoc() const { return RParenLoc; } 985 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 986 987 bool isVolatile() const { return IsVolatile; } 988 void setVolatile(bool V) { IsVolatile = V; } 989 bool isSimple() const { return IsSimple; } 990 void setSimple(bool V) { IsSimple = false; } 991 992 //===--- Asm String Analysis ---===// 993 994 const StringLiteral *getAsmString() const { return AsmStr; } 995 StringLiteral *getAsmString() { return AsmStr; } 996 void setAsmString(StringLiteral *E) { AsmStr = E; } 997 998 /// AsmStringPiece - this is part of a decomposed asm string specification 999 /// (for use with the AnalyzeAsmString function below). An asm string is 1000 /// considered to be a concatenation of these parts. 1001 class AsmStringPiece { 1002 public: 1003 enum Kind { 1004 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 1005 Operand // Operand reference, with optional modifier %c4. 1006 }; 1007 private: 1008 Kind MyKind; 1009 std::string Str; 1010 unsigned OperandNo; 1011 public: 1012 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 1013 AsmStringPiece(unsigned OpNo, char Modifier) 1014 : MyKind(Operand), Str(), OperandNo(OpNo) { 1015 Str += Modifier; 1016 } 1017 1018 bool isString() const { return MyKind == String; } 1019 bool isOperand() const { return MyKind == Operand; } 1020 1021 const std::string &getString() const { 1022 assert(isString()); 1023 return Str; 1024 } 1025 1026 unsigned getOperandNo() const { 1027 assert(isOperand()); 1028 return OperandNo; 1029 } 1030 1031 /// getModifier - Get the modifier for this operand, if present. This 1032 /// returns '\0' if there was no modifier. 1033 char getModifier() const { 1034 assert(isOperand()); 1035 return Str[0]; 1036 } 1037 }; 1038 1039 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 1040 /// it into pieces. If the asm string is erroneous, emit errors and return 1041 /// true, otherwise return false. This handles canonicalization and 1042 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 1043 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 1044 unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces, 1045 ASTContext &C, unsigned &DiagOffs) const; 1046 1047 1048 //===--- Output operands ---===// 1049 1050 unsigned getNumOutputs() const { return NumOutputs; } 1051 1052 const std::string &getOutputName(unsigned i) const { 1053 return Names[i]; 1054 } 1055 1056 /// getOutputConstraint - Return the constraint string for the specified 1057 /// output operand. All output constraints are known to be non-empty (either 1058 /// '=' or '+'). 1059 std::string getOutputConstraint(unsigned i) const; 1060 1061 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 1062 return Constraints[i]; 1063 } 1064 StringLiteral *getOutputConstraintLiteral(unsigned i) { 1065 return Constraints[i]; 1066 } 1067 1068 1069 Expr *getOutputExpr(unsigned i); 1070 1071 const Expr *getOutputExpr(unsigned i) const { 1072 return const_cast<AsmStmt*>(this)->getOutputExpr(i); 1073 } 1074 1075 /// isOutputPlusConstraint - Return true if the specified output constraint 1076 /// is a "+" constraint (which is both an input and an output) or false if it 1077 /// is an "=" constraint (just an output). 1078 bool isOutputPlusConstraint(unsigned i) const { 1079 return getOutputConstraint(i)[0] == '+'; 1080 } 1081 1082 /// getNumPlusOperands - Return the number of output operands that have a "+" 1083 /// constraint. 1084 unsigned getNumPlusOperands() const; 1085 1086 //===--- Input operands ---===// 1087 1088 unsigned getNumInputs() const { return NumInputs; } 1089 1090 const std::string &getInputName(unsigned i) const { 1091 return Names[i + NumOutputs]; 1092 } 1093 1094 /// getInputConstraint - Return the specified input constraint. Unlike output 1095 /// constraints, these can be empty. 1096 std::string getInputConstraint(unsigned i) const; 1097 1098 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 1099 return Constraints[i + NumOutputs]; 1100 } 1101 StringLiteral *getInputConstraintLiteral(unsigned i) { 1102 return Constraints[i + NumOutputs]; 1103 } 1104 1105 1106 Expr *getInputExpr(unsigned i); 1107 1108 const Expr *getInputExpr(unsigned i) const { 1109 return const_cast<AsmStmt*>(this)->getInputExpr(i); 1110 } 1111 1112 void setOutputsAndInputs(unsigned NumOutputs, 1113 unsigned NumInputs, 1114 const std::string *Names, 1115 StringLiteral **Constraints, 1116 Stmt **Exprs); 1117 1118 //===--- Other ---===// 1119 1120 /// getNamedOperand - Given a symbolic operand reference like %[foo], 1121 /// translate this into a numeric value needed to reference the same operand. 1122 /// This returns -1 if the operand name is invalid. 1123 int getNamedOperand(const std::string &SymbolicName) const; 1124 1125 1126 1127 unsigned getNumClobbers() const { return Clobbers.size(); } 1128 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 1129 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 1130 void setClobbers(StringLiteral **Clobbers, unsigned NumClobbers); 1131 1132 virtual SourceRange getSourceRange() const { 1133 return SourceRange(AsmLoc, RParenLoc); 1134 } 1135 1136 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 1137 static bool classof(const AsmStmt *) { return true; } 1138 1139 // Input expr iterators. 1140 1141 typedef ExprIterator inputs_iterator; 1142 typedef ConstExprIterator const_inputs_iterator; 1143 1144 inputs_iterator begin_inputs() { 1145 return &Exprs[0] + NumOutputs; 1146 } 1147 1148 inputs_iterator end_inputs() { 1149 return &Exprs[0] + NumOutputs + NumInputs; 1150 } 1151 1152 const_inputs_iterator begin_inputs() const { 1153 return &Exprs[0] + NumOutputs; 1154 } 1155 1156 const_inputs_iterator end_inputs() const { 1157 return &Exprs[0] + NumOutputs + NumInputs;} 1158 1159 // Output expr iterators. 1160 1161 typedef ExprIterator outputs_iterator; 1162 typedef ConstExprIterator const_outputs_iterator; 1163 1164 outputs_iterator begin_outputs() { return &Exprs[0]; } 1165 outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; } 1166 1167 const_outputs_iterator begin_outputs() const { return &Exprs[0]; } 1168 const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; } 1169 1170 // Input name iterator. 1171 1172 const std::string *begin_output_names() const { 1173 return &Names[0]; 1174 } 1175 1176 const std::string *end_output_names() const { 1177 return &Names[0] + NumOutputs; 1178 } 1179 1180 // Child iterators 1181 1182 virtual child_iterator child_begin(); 1183 virtual child_iterator child_end(); 1184}; 1185 1186} // end namespace clang 1187 1188#endif 1189