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