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