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