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