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