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