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