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