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