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