Stmt.h revision 16f0049415ec596504891259e2a83e19871c0d52
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 SourceLocation getSemiLoc() const { return SemiLoc; } 294 void setSemiLoc(SourceLocation L) { SemiLoc = L; } 295 296 virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); } 297 298 static bool classof(const Stmt *T) { 299 return T->getStmtClass() == NullStmtClass; 300 } 301 static bool classof(const NullStmt *) { return true; } 302 303 // Iterators 304 virtual child_iterator child_begin(); 305 virtual child_iterator child_end(); 306}; 307 308/// CompoundStmt - This represents a group of statements like { stmt stmt }. 309/// 310class CompoundStmt : public Stmt { 311 Stmt** Body; 312 unsigned NumStmts; 313 SourceLocation LBracLoc, RBracLoc; 314public: 315 CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts, 316 SourceLocation LB, SourceLocation RB) 317 : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) { 318 if (NumStmts == 0) { 319 Body = 0; 320 return; 321 } 322 323 Body = new (C) Stmt*[NumStmts]; 324 memcpy(Body, StmtStart, numStmts * sizeof(*Body)); 325 } 326 327 // \brief Build an empty compound statement. 328 explicit CompoundStmt(EmptyShell Empty) 329 : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { } 330 331 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts); 332 333 bool body_empty() const { return NumStmts == 0; } 334 unsigned size() const { return NumStmts; } 335 336 typedef Stmt** body_iterator; 337 body_iterator body_begin() { return Body; } 338 body_iterator body_end() { return Body + NumStmts; } 339 Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; } 340 341 typedef Stmt* const * const_body_iterator; 342 const_body_iterator body_begin() const { return Body; } 343 const_body_iterator body_end() const { return Body + NumStmts; } 344 const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; } 345 346 typedef std::reverse_iterator<body_iterator> reverse_body_iterator; 347 reverse_body_iterator body_rbegin() { 348 return reverse_body_iterator(body_end()); 349 } 350 reverse_body_iterator body_rend() { 351 return reverse_body_iterator(body_begin()); 352 } 353 354 typedef std::reverse_iterator<const_body_iterator> 355 const_reverse_body_iterator; 356 357 const_reverse_body_iterator body_rbegin() const { 358 return const_reverse_body_iterator(body_end()); 359 } 360 361 const_reverse_body_iterator body_rend() const { 362 return const_reverse_body_iterator(body_begin()); 363 } 364 365 virtual SourceRange getSourceRange() const { 366 return SourceRange(LBracLoc, RBracLoc); 367 } 368 369 SourceLocation getLBracLoc() const { return LBracLoc; } 370 void setLBracLoc(SourceLocation L) { LBracLoc = L; } 371 SourceLocation getRBracLoc() const { return RBracLoc; } 372 void setRBracLoc(SourceLocation L) { RBracLoc = L; } 373 374 static bool classof(const Stmt *T) { 375 return T->getStmtClass() == CompoundStmtClass; 376 } 377 static bool classof(const CompoundStmt *) { return true; } 378 379 // Iterators 380 virtual child_iterator child_begin(); 381 virtual child_iterator child_end(); 382}; 383 384// SwitchCase is the base class for CaseStmt and DefaultStmt, 385class SwitchCase : public Stmt { 386protected: 387 // A pointer to the following CaseStmt or DefaultStmt class, 388 // used by SwitchStmt. 389 SwitchCase *NextSwitchCase; 390 391 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {} 392 393public: 394 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 395 396 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 397 398 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 399 400 Stmt *getSubStmt() { return v_getSubStmt(); } 401 402 virtual SourceRange getSourceRange() const { return SourceRange(); } 403 404 static bool classof(const Stmt *T) { 405 return T->getStmtClass() == CaseStmtClass || 406 T->getStmtClass() == DefaultStmtClass; 407 } 408 static bool classof(const SwitchCase *) { return true; } 409protected: 410 virtual Stmt* v_getSubStmt() = 0; 411}; 412 413class CaseStmt : public SwitchCase { 414 enum { SUBSTMT, LHS, RHS, END_EXPR }; 415 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for 416 // GNU "case 1 ... 4" extension 417 SourceLocation CaseLoc; 418 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 419public: 420 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc) 421 : SwitchCase(CaseStmtClass) { 422 SubExprs[SUBSTMT] = 0; 423 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs); 424 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs); 425 CaseLoc = caseLoc; 426 } 427 428 /// \brief Build an empty switch case statement. 429 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { } 430 431 SourceLocation getCaseLoc() const { return CaseLoc; } 432 void setCaseLoc(SourceLocation L) { CaseLoc = L; } 433 434 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); } 435 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); } 436 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } 437 438 const Expr *getLHS() const { 439 return reinterpret_cast<const Expr*>(SubExprs[LHS]); 440 } 441 const Expr *getRHS() const { 442 return reinterpret_cast<const Expr*>(SubExprs[RHS]); 443 } 444 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; } 445 446 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; } 447 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); } 448 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } 449 450 451 virtual SourceRange getSourceRange() const { 452 // Handle deeply nested case statements with iteration instead of recursion. 453 const CaseStmt *CS = this; 454 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) 455 CS = CS2; 456 457 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd()); 458 } 459 static bool classof(const Stmt *T) { 460 return T->getStmtClass() == CaseStmtClass; 461 } 462 static bool classof(const CaseStmt *) { return true; } 463 464 // Iterators 465 virtual child_iterator child_begin(); 466 virtual child_iterator child_end(); 467}; 468 469class DefaultStmt : public SwitchCase { 470 Stmt* SubStmt; 471 SourceLocation DefaultLoc; 472 virtual Stmt* v_getSubStmt() { return getSubStmt(); } 473public: 474 DefaultStmt(SourceLocation DL, Stmt *substmt) : 475 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {} 476 477 /// \brief Build an empty default statement. 478 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { } 479 480 Stmt *getSubStmt() { return SubStmt; } 481 const Stmt *getSubStmt() const { return SubStmt; } 482 void setSubStmt(Stmt *S) { SubStmt = S; } 483 484 SourceLocation getDefaultLoc() const { return DefaultLoc; } 485 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; } 486 487 virtual SourceRange getSourceRange() const { 488 return SourceRange(DefaultLoc, SubStmt->getLocEnd()); 489 } 490 static bool classof(const Stmt *T) { 491 return T->getStmtClass() == DefaultStmtClass; 492 } 493 static bool classof(const DefaultStmt *) { return true; } 494 495 // Iterators 496 virtual child_iterator child_begin(); 497 virtual child_iterator child_end(); 498}; 499 500class LabelStmt : public Stmt { 501 IdentifierInfo *Label; 502 Stmt *SubStmt; 503 SourceLocation IdentLoc; 504public: 505 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) 506 : Stmt(LabelStmtClass), Label(label), 507 SubStmt(substmt), IdentLoc(IL) {} 508 509 // \brief Build an empty label statement. 510 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { } 511 512 SourceLocation getIdentLoc() const { return IdentLoc; } 513 IdentifierInfo *getID() const { return Label; } 514 void setID(IdentifierInfo *II) { Label = II; } 515 const char *getName() const; 516 Stmt *getSubStmt() { return SubStmt; } 517 const Stmt *getSubStmt() const { return SubStmt; } 518 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 519 void setSubStmt(Stmt *SS) { SubStmt = SS; } 520 521 virtual SourceRange getSourceRange() const { 522 return SourceRange(IdentLoc, SubStmt->getLocEnd()); 523 } 524 static bool classof(const Stmt *T) { 525 return T->getStmtClass() == LabelStmtClass; 526 } 527 static bool classof(const LabelStmt *) { return true; } 528 529 // Iterators 530 virtual child_iterator child_begin(); 531 virtual child_iterator child_end(); 532}; 533 534 535/// IfStmt - This represents an if/then/else. 536/// 537class IfStmt : public Stmt { 538 enum { COND, THEN, ELSE, END_EXPR }; 539 Stmt* SubExprs[END_EXPR]; 540 SourceLocation IfLoc; 541public: 542 IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0) 543 : Stmt(IfStmtClass) { 544 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 545 SubExprs[THEN] = then; 546 SubExprs[ELSE] = elsev; 547 IfLoc = IL; 548 } 549 550 /// \brief Build an empty if/then/else statement 551 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { } 552 553 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 554 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 555 const Stmt *getThen() const { return SubExprs[THEN]; } 556 void setThen(Stmt *S) { SubExprs[THEN] = S; } 557 const Stmt *getElse() const { return SubExprs[ELSE]; } 558 void setElse(Stmt *S) { SubExprs[ELSE] = S; } 559 560 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 561 Stmt *getThen() { return SubExprs[THEN]; } 562 Stmt *getElse() { return SubExprs[ELSE]; } 563 564 SourceLocation getIfLoc() const { return IfLoc; } 565 void setIfLoc(SourceLocation L) { IfLoc = L; } 566 567 virtual SourceRange getSourceRange() const { 568 if (SubExprs[ELSE]) 569 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); 570 else 571 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); 572 } 573 574 static bool classof(const Stmt *T) { 575 return T->getStmtClass() == IfStmtClass; 576 } 577 static bool classof(const IfStmt *) { return true; } 578 579 // Iterators 580 virtual child_iterator child_begin(); 581 virtual child_iterator child_end(); 582}; 583 584/// SwitchStmt - This represents a 'switch' stmt. 585/// 586class SwitchStmt : public Stmt { 587 enum { COND, BODY, END_EXPR }; 588 Stmt* SubExprs[END_EXPR]; 589 // This points to a linked list of case and default statements. 590 SwitchCase *FirstCase; 591 SourceLocation SwitchLoc; 592public: 593 SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) { 594 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 595 SubExprs[BODY] = NULL; 596 } 597 598 /// \brief Build a empty switch statement. 599 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { } 600 601 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 602 const Stmt *getBody() const { return SubExprs[BODY]; } 603 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 604 605 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 606 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 607 Stmt *getBody() { return SubExprs[BODY]; } 608 void setBody(Stmt *S) { SubExprs[BODY] = S; } 609 SwitchCase *getSwitchCaseList() { return FirstCase; } 610 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } 611 612 SourceLocation getSwitchLoc() const { return SwitchLoc; } 613 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; } 614 615 void setBody(Stmt *S, SourceLocation SL) { 616 SubExprs[BODY] = S; 617 SwitchLoc = SL; 618 } 619 void addSwitchCase(SwitchCase *SC) { 620 assert(!SC->getNextSwitchCase() && "case/default already added to a switch"); 621 SC->setNextSwitchCase(FirstCase); 622 FirstCase = SC; 623 } 624 virtual SourceRange getSourceRange() const { 625 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd()); 626 } 627 static bool classof(const Stmt *T) { 628 return T->getStmtClass() == SwitchStmtClass; 629 } 630 static bool classof(const SwitchStmt *) { return true; } 631 632 // Iterators 633 virtual child_iterator child_begin(); 634 virtual child_iterator child_end(); 635}; 636 637 638/// WhileStmt - This represents a 'while' stmt. 639/// 640class WhileStmt : public Stmt { 641 enum { COND, BODY, END_EXPR }; 642 Stmt* SubExprs[END_EXPR]; 643 SourceLocation WhileLoc; 644public: 645 WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) { 646 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 647 SubExprs[BODY] = body; 648 WhileLoc = WL; 649 } 650 651 /// \brief Build an empty while statement. 652 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { } 653 654 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 655 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 656 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 657 Stmt *getBody() { return SubExprs[BODY]; } 658 const Stmt *getBody() const { return SubExprs[BODY]; } 659 void setBody(Stmt *S) { SubExprs[BODY] = S; } 660 661 SourceLocation getWhileLoc() const { return WhileLoc; } 662 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 663 664 virtual SourceRange getSourceRange() const { 665 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); 666 } 667 static bool classof(const Stmt *T) { 668 return T->getStmtClass() == WhileStmtClass; 669 } 670 static bool classof(const WhileStmt *) { return true; } 671 672 // Iterators 673 virtual child_iterator child_begin(); 674 virtual child_iterator child_end(); 675}; 676 677/// DoStmt - This represents a 'do/while' stmt. 678/// 679class DoStmt : public Stmt { 680 enum { COND, BODY, END_EXPR }; 681 Stmt* SubExprs[END_EXPR]; 682 SourceLocation DoLoc; 683public: 684 DoStmt(Stmt *body, Expr *cond, SourceLocation DL) 685 : Stmt(DoStmtClass), DoLoc(DL) { 686 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 687 SubExprs[BODY] = body; 688 DoLoc = DL; 689 } 690 691 /// \brief Build an empty do-while statement. 692 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { } 693 694 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 695 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 696 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 697 Stmt *getBody() { return SubExprs[BODY]; } 698 const Stmt *getBody() const { return SubExprs[BODY]; } 699 void setBody(Stmt *S) { SubExprs[BODY] = S; } 700 701 SourceLocation getDoLoc() const { return DoLoc; } 702 void setDoLoc(SourceLocation L) { DoLoc = L; } 703 704 virtual SourceRange getSourceRange() const { 705 return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 706 } 707 static bool classof(const Stmt *T) { 708 return T->getStmtClass() == DoStmtClass; 709 } 710 static bool classof(const DoStmt *) { return true; } 711 712 // Iterators 713 virtual child_iterator child_begin(); 714 virtual child_iterator child_end(); 715}; 716 717 718/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 719/// the init/cond/inc parts of the ForStmt will be null if they were not 720/// specified in the source. 721/// 722class ForStmt : public Stmt { 723 enum { INIT, COND, INC, BODY, END_EXPR }; 724 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 725 SourceLocation ForLoc; 726public: 727 ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL) 728 : Stmt(ForStmtClass) { 729 SubExprs[INIT] = Init; 730 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond); 731 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc); 732 SubExprs[BODY] = Body; 733 ForLoc = FL; 734 } 735 736 /// \brief Build an empty for statement. 737 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { } 738 739 Stmt *getInit() { return SubExprs[INIT]; } 740 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 741 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 742 Stmt *getBody() { return SubExprs[BODY]; } 743 744 const Stmt *getInit() const { return SubExprs[INIT]; } 745 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 746 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 747 const Stmt *getBody() const { return SubExprs[BODY]; } 748 749 void setInit(Stmt *S) { SubExprs[INIT] = S; } 750 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 751 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); } 752 void setBody(Stmt *S) { SubExprs[BODY] = S; } 753 754 SourceLocation getForLoc() const { return ForLoc; } 755 void setForLoc(SourceLocation L) { ForLoc = L; } 756 757 virtual SourceRange getSourceRange() const { 758 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 759 } 760 static bool classof(const Stmt *T) { 761 return T->getStmtClass() == ForStmtClass; 762 } 763 static bool classof(const ForStmt *) { return true; } 764 765 // Iterators 766 virtual child_iterator child_begin(); 767 virtual child_iterator child_end(); 768}; 769 770/// GotoStmt - This represents a direct goto. 771/// 772class GotoStmt : public Stmt { 773 LabelStmt *Label; 774 SourceLocation GotoLoc; 775 SourceLocation LabelLoc; 776public: 777 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL) 778 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 779 780 /// \brief Build an empty goto statement. 781 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { } 782 783 LabelStmt *getLabel() const { return Label; } 784 void setLabel(LabelStmt *S) { Label = S; } 785 786 SourceLocation getGotoLoc() const { return GotoLoc; } 787 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 788 SourceLocation getLabelLoc() const { return LabelLoc; } 789 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 790 791 virtual SourceRange getSourceRange() const { 792 return SourceRange(GotoLoc, LabelLoc); 793 } 794 static bool classof(const Stmt *T) { 795 return T->getStmtClass() == GotoStmtClass; 796 } 797 static bool classof(const GotoStmt *) { return true; } 798 799 // Iterators 800 virtual child_iterator child_begin(); 801 virtual child_iterator child_end(); 802}; 803 804/// IndirectGotoStmt - This represents an indirect goto. 805/// 806class IndirectGotoStmt : public Stmt { 807 SourceLocation GotoLoc; 808 Stmt *Target; 809public: 810 IndirectGotoStmt(SourceLocation gotoLoc, Expr *target) 811 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), Target((Stmt*)target) {} 812 813 /// \brief Build an empty indirect goto statement. 814 explicit IndirectGotoStmt(EmptyShell Empty) 815 : Stmt(IndirectGotoStmtClass, Empty) { } 816 817 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 818 SourceLocation getGotoLoc() const { return GotoLoc; } 819 820 Expr *getTarget(); 821 const Expr *getTarget() const; 822 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); } 823 824 virtual SourceRange getSourceRange() const { 825 return SourceRange(GotoLoc, Target->getLocEnd()); 826 } 827 828 static bool classof(const Stmt *T) { 829 return T->getStmtClass() == IndirectGotoStmtClass; 830 } 831 static bool classof(const IndirectGotoStmt *) { return true; } 832 833 // Iterators 834 virtual child_iterator child_begin(); 835 virtual child_iterator child_end(); 836}; 837 838 839/// ContinueStmt - This represents a continue. 840/// 841class ContinueStmt : public Stmt { 842 SourceLocation ContinueLoc; 843public: 844 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 845 846 /// \brief Build an empty continue statement. 847 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { } 848 849 SourceLocation getContinueLoc() const { return ContinueLoc; } 850 void setContinueLoc(SourceLocation L) { ContinueLoc = L; } 851 852 virtual SourceRange getSourceRange() const { 853 return SourceRange(ContinueLoc); 854 } 855 static bool classof(const Stmt *T) { 856 return T->getStmtClass() == ContinueStmtClass; 857 } 858 static bool classof(const ContinueStmt *) { return true; } 859 860 // Iterators 861 virtual child_iterator child_begin(); 862 virtual child_iterator child_end(); 863}; 864 865/// BreakStmt - This represents a break. 866/// 867class BreakStmt : public Stmt { 868 SourceLocation BreakLoc; 869public: 870 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 871 872 /// \brief Build an empty break statement. 873 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { } 874 875 SourceLocation getBreakLoc() const { return BreakLoc; } 876 void setBreakLoc(SourceLocation L) { BreakLoc = L; } 877 878 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); } 879 880 static bool classof(const Stmt *T) { 881 return T->getStmtClass() == BreakStmtClass; 882 } 883 static bool classof(const BreakStmt *) { return true; } 884 885 // Iterators 886 virtual child_iterator child_begin(); 887 virtual child_iterator child_end(); 888}; 889 890 891/// ReturnStmt - This represents a return, optionally of an expression: 892/// return; 893/// return 4; 894/// 895/// Note that GCC allows return with no argument in a function declared to 896/// return a value, and it allows returning a value in functions declared to 897/// return void. We explicitly model this in the AST, which means you can't 898/// depend on the return type of the function and the presence of an argument. 899/// 900class ReturnStmt : public Stmt { 901 Stmt *RetExpr; 902 SourceLocation RetLoc; 903public: 904 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), 905 RetExpr((Stmt*) E), RetLoc(RL) {} 906 907 /// \brief Build an empty return expression. 908 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { } 909 910 const Expr *getRetValue() const; 911 Expr *getRetValue(); 912 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); } 913 914 SourceLocation getReturnLoc() const { return RetLoc; } 915 void setReturnLoc(SourceLocation L) { RetLoc = L; } 916 917 virtual SourceRange getSourceRange() const; 918 919 static bool classof(const Stmt *T) { 920 return T->getStmtClass() == ReturnStmtClass; 921 } 922 static bool classof(const ReturnStmt *) { return true; } 923 924 // Iterators 925 virtual child_iterator child_begin(); 926 virtual child_iterator child_end(); 927}; 928 929/// AsmStmt - This represents a GNU inline-assembly statement extension. 930/// 931class AsmStmt : public Stmt { 932 SourceLocation AsmLoc, RParenLoc; 933 StringLiteral *AsmStr; 934 935 bool IsSimple; 936 bool IsVolatile; 937 938 unsigned NumOutputs; 939 unsigned NumInputs; 940 941 llvm::SmallVector<std::string, 4> Names; 942 llvm::SmallVector<StringLiteral*, 4> Constraints; 943 llvm::SmallVector<Stmt*, 4> Exprs; 944 945 llvm::SmallVector<StringLiteral*, 4> Clobbers; 946public: 947 AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile, 948 unsigned numoutputs, unsigned numinputs, 949 std::string *names, StringLiteral **constraints, 950 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers, 951 StringLiteral **clobbers, SourceLocation rparenloc); 952 953 /// \brief Build an empty inline-assembly statement. 954 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty) { } 955 956 SourceLocation getAsmLoc() const { return AsmLoc; } 957 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 958 SourceLocation getRParenLoc() const { return RParenLoc; } 959 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 960 961 bool isVolatile() const { return IsVolatile; } 962 void setVolatile(bool V) { IsVolatile = V; } 963 bool isSimple() const { return IsSimple; } 964 void setSimple(bool V) { IsSimple = false; } 965 966 //===--- Asm String Analysis ---===// 967 968 const StringLiteral *getAsmString() const { return AsmStr; } 969 StringLiteral *getAsmString() { return AsmStr; } 970 void setAsmString(StringLiteral *E) { AsmStr = E; } 971 972 /// AsmStringPiece - this is part of a decomposed asm string specification 973 /// (for use with the AnalyzeAsmString function below). An asm string is 974 /// considered to be a concatenation of these parts. 975 class AsmStringPiece { 976 public: 977 enum Kind { 978 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 979 Operand // Operand reference, with optional modifier %c4. 980 }; 981 private: 982 Kind MyKind; 983 std::string Str; 984 unsigned OperandNo; 985 public: 986 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 987 AsmStringPiece(unsigned OpNo, char Modifier) 988 : MyKind(Operand), Str(), OperandNo(OpNo) { 989 Str += Modifier; 990 } 991 992 bool isString() const { return MyKind == String; } 993 bool isOperand() const { return MyKind == Operand; } 994 995 const std::string &getString() const { 996 assert(isString()); 997 return Str; 998 } 999 1000 unsigned getOperandNo() const { 1001 assert(isOperand()); 1002 return OperandNo; 1003 } 1004 1005 /// getModifier - Get the modifier for this operand, if present. This 1006 /// returns '\0' if there was no modifier. 1007 char getModifier() const { 1008 assert(isOperand()); 1009 return Str[0]; 1010 } 1011 }; 1012 1013 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 1014 /// it into pieces. If the asm string is erroneous, emit errors and return 1015 /// true, otherwise return false. This handles canonicalization and 1016 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 1017 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 1018 unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces, 1019 ASTContext &C, unsigned &DiagOffs) const; 1020 1021 1022 //===--- Output operands ---===// 1023 1024 unsigned getNumOutputs() const { return NumOutputs; } 1025 1026 const std::string &getOutputName(unsigned i) const { 1027 return Names[i]; 1028 } 1029 1030 /// getOutputConstraint - Return the constraint string for the specified 1031 /// output operand. All output constraints are known to be non-empty (either 1032 /// '=' or '+'). 1033 std::string getOutputConstraint(unsigned i) const; 1034 1035 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 1036 return Constraints[i]; 1037 } 1038 StringLiteral *getOutputConstraintLiteral(unsigned i) { 1039 return Constraints[i]; 1040 } 1041 1042 1043 Expr *getOutputExpr(unsigned i); 1044 1045 const Expr *getOutputExpr(unsigned i) const { 1046 return const_cast<AsmStmt*>(this)->getOutputExpr(i); 1047 } 1048 1049 /// isOutputPlusConstraint - Return true if the specified output constraint 1050 /// is a "+" constraint (which is both an input and an output) or false if it 1051 /// is an "=" constraint (just an output). 1052 bool isOutputPlusConstraint(unsigned i) const { 1053 return getOutputConstraint(i)[0] == '+'; 1054 } 1055 1056 /// getNumPlusOperands - Return the number of output operands that have a "+" 1057 /// constraint. 1058 unsigned getNumPlusOperands() const; 1059 1060 //===--- Input operands ---===// 1061 1062 unsigned getNumInputs() const { return NumInputs; } 1063 1064 const std::string &getInputName(unsigned i) const { 1065 return Names[i + NumOutputs]; 1066 } 1067 1068 /// getInputConstraint - Return the specified input constraint. Unlike output 1069 /// constraints, these can be empty. 1070 std::string getInputConstraint(unsigned i) const; 1071 1072 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 1073 return Constraints[i + NumOutputs]; 1074 } 1075 StringLiteral *getInputConstraintLiteral(unsigned i) { 1076 return Constraints[i + NumOutputs]; 1077 } 1078 1079 1080 Expr *getInputExpr(unsigned i); 1081 1082 const Expr *getInputExpr(unsigned i) const { 1083 return const_cast<AsmStmt*>(this)->getInputExpr(i); 1084 } 1085 1086 void setOutputsAndInputs(unsigned NumOutputs, 1087 unsigned NumInputs, 1088 const std::string *Names, 1089 StringLiteral **Constraints, 1090 Stmt **Exprs); 1091 1092 //===--- Other ---===// 1093 1094 /// getNamedOperand - Given a symbolic operand reference like %[foo], 1095 /// translate this into a numeric value needed to reference the same operand. 1096 /// This returns -1 if the operand name is invalid. 1097 int getNamedOperand(const std::string &SymbolicName) const; 1098 1099 1100 1101 unsigned getNumClobbers() const { return Clobbers.size(); } 1102 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; } 1103 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; } 1104 void setClobbers(StringLiteral **Clobbers, unsigned NumClobbers); 1105 1106 virtual SourceRange getSourceRange() const { 1107 return SourceRange(AsmLoc, RParenLoc); 1108 } 1109 1110 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;} 1111 static bool classof(const AsmStmt *) { return true; } 1112 1113 // Input expr iterators. 1114 1115 typedef ExprIterator inputs_iterator; 1116 typedef ConstExprIterator const_inputs_iterator; 1117 1118 inputs_iterator begin_inputs() { 1119 return &Exprs[0] + NumOutputs; 1120 } 1121 1122 inputs_iterator end_inputs() { 1123 return &Exprs[0] + NumOutputs + NumInputs; 1124 } 1125 1126 const_inputs_iterator begin_inputs() const { 1127 return &Exprs[0] + NumOutputs; 1128 } 1129 1130 const_inputs_iterator end_inputs() const { 1131 return &Exprs[0] + NumOutputs + NumInputs;} 1132 1133 // Output expr iterators. 1134 1135 typedef ExprIterator outputs_iterator; 1136 typedef ConstExprIterator const_outputs_iterator; 1137 1138 outputs_iterator begin_outputs() { return &Exprs[0]; } 1139 outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; } 1140 1141 const_outputs_iterator begin_outputs() const { return &Exprs[0]; } 1142 const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; } 1143 1144 // Input name iterator. 1145 1146 const std::string *begin_output_names() const { 1147 return &Names[0]; 1148 } 1149 1150 const std::string *end_output_names() const { 1151 return &Names[0] + NumOutputs; 1152 } 1153 1154 // Child iterators 1155 1156 virtual child_iterator child_begin(); 1157 virtual child_iterator child_end(); 1158}; 1159 1160} // end namespace clang 1161 1162#endif 1163