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