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