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