clang API Documentation
00001 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the Stmt interface and subclasses. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_AST_STMT_H 00015 #define LLVM_CLANG_AST_STMT_H 00016 00017 #include "clang/AST/DeclGroup.h" 00018 #include "clang/AST/StmtIterator.h" 00019 #include "clang/Basic/CapturedStmt.h" 00020 #include "clang/Basic/IdentifierTable.h" 00021 #include "clang/Basic/LLVM.h" 00022 #include "clang/Basic/SourceLocation.h" 00023 #include "llvm/ADT/ArrayRef.h" 00024 #include "llvm/ADT/PointerIntPair.h" 00025 #include "llvm/Support/Compiler.h" 00026 #include "llvm/Support/ErrorHandling.h" 00027 #include <string> 00028 00029 namespace llvm { 00030 class FoldingSetNodeID; 00031 } 00032 00033 namespace clang { 00034 class ASTContext; 00035 class Attr; 00036 class CapturedDecl; 00037 class Decl; 00038 class Expr; 00039 class IdentifierInfo; 00040 class LabelDecl; 00041 class ParmVarDecl; 00042 class PrinterHelper; 00043 struct PrintingPolicy; 00044 class QualType; 00045 class RecordDecl; 00046 class SourceManager; 00047 class StringLiteral; 00048 class SwitchStmt; 00049 class Token; 00050 class VarDecl; 00051 00052 //===--------------------------------------------------------------------===// 00053 // ExprIterator - Iterators for iterating over Stmt* arrays that contain 00054 // only Expr*. This is needed because AST nodes use Stmt* arrays to store 00055 // references to children (to be compatible with StmtIterator). 00056 //===--------------------------------------------------------------------===// 00057 00058 class Stmt; 00059 class Expr; 00060 00061 class ExprIterator { 00062 Stmt** I; 00063 public: 00064 ExprIterator(Stmt** i) : I(i) {} 00065 ExprIterator() : I(nullptr) {} 00066 ExprIterator& operator++() { ++I; return *this; } 00067 ExprIterator operator-(size_t i) { return I-i; } 00068 ExprIterator operator+(size_t i) { return I+i; } 00069 Expr* operator[](size_t idx); 00070 // FIXME: Verify that this will correctly return a signed distance. 00071 signed operator-(const ExprIterator& R) const { return I - R.I; } 00072 Expr* operator*() const; 00073 Expr* operator->() const; 00074 bool operator==(const ExprIterator& R) const { return I == R.I; } 00075 bool operator!=(const ExprIterator& R) const { return I != R.I; } 00076 bool operator>(const ExprIterator& R) const { return I > R.I; } 00077 bool operator>=(const ExprIterator& R) const { return I >= R.I; } 00078 }; 00079 00080 class ConstExprIterator { 00081 const Stmt * const *I; 00082 public: 00083 ConstExprIterator(const Stmt * const *i) : I(i) {} 00084 ConstExprIterator() : I(nullptr) {} 00085 ConstExprIterator& operator++() { ++I; return *this; } 00086 ConstExprIterator operator+(size_t i) const { return I+i; } 00087 ConstExprIterator operator-(size_t i) const { return I-i; } 00088 const Expr * operator[](size_t idx) const; 00089 signed operator-(const ConstExprIterator& R) const { return I - R.I; } 00090 const Expr * operator*() const; 00091 const Expr * operator->() const; 00092 bool operator==(const ConstExprIterator& R) const { return I == R.I; } 00093 bool operator!=(const ConstExprIterator& R) const { return I != R.I; } 00094 bool operator>(const ConstExprIterator& R) const { return I > R.I; } 00095 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; } 00096 }; 00097 00098 //===----------------------------------------------------------------------===// 00099 // AST classes for statements. 00100 //===----------------------------------------------------------------------===// 00101 00102 /// Stmt - This represents one statement. 00103 /// 00104 class Stmt { 00105 public: 00106 enum StmtClass { 00107 NoStmtClass = 0, 00108 #define STMT(CLASS, PARENT) CLASS##Class, 00109 #define STMT_RANGE(BASE, FIRST, LAST) \ 00110 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class, 00111 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \ 00112 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class 00113 #define ABSTRACT_STMT(STMT) 00114 #include "clang/AST/StmtNodes.inc" 00115 }; 00116 00117 // Make vanilla 'new' and 'delete' illegal for Stmts. 00118 protected: 00119 void* operator new(size_t bytes) throw() { 00120 llvm_unreachable("Stmts cannot be allocated with regular 'new'."); 00121 } 00122 void operator delete(void* data) throw() { 00123 llvm_unreachable("Stmts cannot be released with regular 'delete'."); 00124 } 00125 00126 class StmtBitfields { 00127 friend class Stmt; 00128 00129 /// \brief The statement class. 00130 unsigned sClass : 8; 00131 }; 00132 enum { NumStmtBits = 8 }; 00133 00134 class CompoundStmtBitfields { 00135 friend class CompoundStmt; 00136 unsigned : NumStmtBits; 00137 00138 unsigned NumStmts : 32 - NumStmtBits; 00139 }; 00140 00141 class ExprBitfields { 00142 friend class Expr; 00143 friend class DeclRefExpr; // computeDependence 00144 friend class InitListExpr; // ctor 00145 friend class DesignatedInitExpr; // ctor 00146 friend class BlockDeclRefExpr; // ctor 00147 friend class ASTStmtReader; // deserialization 00148 friend class CXXNewExpr; // ctor 00149 friend class DependentScopeDeclRefExpr; // ctor 00150 friend class CXXConstructExpr; // ctor 00151 friend class CallExpr; // ctor 00152 friend class OffsetOfExpr; // ctor 00153 friend class ObjCMessageExpr; // ctor 00154 friend class ObjCArrayLiteral; // ctor 00155 friend class ObjCDictionaryLiteral; // ctor 00156 friend class ShuffleVectorExpr; // ctor 00157 friend class ParenListExpr; // ctor 00158 friend class CXXUnresolvedConstructExpr; // ctor 00159 friend class CXXDependentScopeMemberExpr; // ctor 00160 friend class OverloadExpr; // ctor 00161 friend class PseudoObjectExpr; // ctor 00162 friend class AtomicExpr; // ctor 00163 unsigned : NumStmtBits; 00164 00165 unsigned ValueKind : 2; 00166 unsigned ObjectKind : 2; 00167 unsigned TypeDependent : 1; 00168 unsigned ValueDependent : 1; 00169 unsigned InstantiationDependent : 1; 00170 unsigned ContainsUnexpandedParameterPack : 1; 00171 }; 00172 enum { NumExprBits = 16 }; 00173 00174 class CharacterLiteralBitfields { 00175 friend class CharacterLiteral; 00176 unsigned : NumExprBits; 00177 00178 unsigned Kind : 2; 00179 }; 00180 00181 enum APFloatSemantics { 00182 IEEEhalf, 00183 IEEEsingle, 00184 IEEEdouble, 00185 x87DoubleExtended, 00186 IEEEquad, 00187 PPCDoubleDouble 00188 }; 00189 00190 class FloatingLiteralBitfields { 00191 friend class FloatingLiteral; 00192 unsigned : NumExprBits; 00193 00194 unsigned Semantics : 3; // Provides semantics for APFloat construction 00195 unsigned IsExact : 1; 00196 }; 00197 00198 class UnaryExprOrTypeTraitExprBitfields { 00199 friend class UnaryExprOrTypeTraitExpr; 00200 unsigned : NumExprBits; 00201 00202 unsigned Kind : 2; 00203 unsigned IsType : 1; // true if operand is a type, false if an expression. 00204 }; 00205 00206 class DeclRefExprBitfields { 00207 friend class DeclRefExpr; 00208 friend class ASTStmtReader; // deserialization 00209 unsigned : NumExprBits; 00210 00211 unsigned HasQualifier : 1; 00212 unsigned HasTemplateKWAndArgsInfo : 1; 00213 unsigned HasFoundDecl : 1; 00214 unsigned HadMultipleCandidates : 1; 00215 unsigned RefersToEnclosingLocal : 1; 00216 }; 00217 00218 class CastExprBitfields { 00219 friend class CastExpr; 00220 unsigned : NumExprBits; 00221 00222 unsigned Kind : 6; 00223 unsigned BasePathSize : 32 - 6 - NumExprBits; 00224 }; 00225 00226 class CallExprBitfields { 00227 friend class CallExpr; 00228 unsigned : NumExprBits; 00229 00230 unsigned NumPreArgs : 1; 00231 }; 00232 00233 class ExprWithCleanupsBitfields { 00234 friend class ExprWithCleanups; 00235 friend class ASTStmtReader; // deserialization 00236 00237 unsigned : NumExprBits; 00238 00239 unsigned NumObjects : 32 - NumExprBits; 00240 }; 00241 00242 class PseudoObjectExprBitfields { 00243 friend class PseudoObjectExpr; 00244 friend class ASTStmtReader; // deserialization 00245 00246 unsigned : NumExprBits; 00247 00248 // These don't need to be particularly wide, because they're 00249 // strictly limited by the forms of expressions we permit. 00250 unsigned NumSubExprs : 8; 00251 unsigned ResultIndex : 32 - 8 - NumExprBits; 00252 }; 00253 00254 class ObjCIndirectCopyRestoreExprBitfields { 00255 friend class ObjCIndirectCopyRestoreExpr; 00256 unsigned : NumExprBits; 00257 00258 unsigned ShouldCopy : 1; 00259 }; 00260 00261 class InitListExprBitfields { 00262 friend class InitListExpr; 00263 00264 unsigned : NumExprBits; 00265 00266 /// Whether this initializer list originally had a GNU array-range 00267 /// designator in it. This is a temporary marker used by CodeGen. 00268 unsigned HadArrayRangeDesignator : 1; 00269 }; 00270 00271 class TypeTraitExprBitfields { 00272 friend class TypeTraitExpr; 00273 friend class ASTStmtReader; 00274 friend class ASTStmtWriter; 00275 00276 unsigned : NumExprBits; 00277 00278 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator. 00279 unsigned Kind : 8; 00280 00281 /// \brief If this expression is not value-dependent, this indicates whether 00282 /// the trait evaluated true or false. 00283 unsigned Value : 1; 00284 00285 /// \brief The number of arguments to this type trait. 00286 unsigned NumArgs : 32 - 8 - 1 - NumExprBits; 00287 }; 00288 00289 union { 00290 // FIXME: this is wasteful on 64-bit platforms. 00291 void *Aligner; 00292 00293 StmtBitfields StmtBits; 00294 CompoundStmtBitfields CompoundStmtBits; 00295 ExprBitfields ExprBits; 00296 CharacterLiteralBitfields CharacterLiteralBits; 00297 FloatingLiteralBitfields FloatingLiteralBits; 00298 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits; 00299 DeclRefExprBitfields DeclRefExprBits; 00300 CastExprBitfields CastExprBits; 00301 CallExprBitfields CallExprBits; 00302 ExprWithCleanupsBitfields ExprWithCleanupsBits; 00303 PseudoObjectExprBitfields PseudoObjectExprBits; 00304 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits; 00305 InitListExprBitfields InitListExprBits; 00306 TypeTraitExprBitfields TypeTraitExprBits; 00307 }; 00308 00309 friend class ASTStmtReader; 00310 friend class ASTStmtWriter; 00311 00312 public: 00313 // Only allow allocation of Stmts using the allocator in ASTContext 00314 // or by doing a placement new. 00315 void* operator new(size_t bytes, const ASTContext& C, 00316 unsigned alignment = 8); 00317 00318 void* operator new(size_t bytes, const ASTContext* C, 00319 unsigned alignment = 8) { 00320 return operator new(bytes, *C, alignment); 00321 } 00322 00323 void* operator new(size_t bytes, void* mem) throw() { 00324 return mem; 00325 } 00326 00327 void operator delete(void*, const ASTContext&, unsigned) throw() { } 00328 void operator delete(void*, const ASTContext*, unsigned) throw() { } 00329 void operator delete(void*, size_t) throw() { } 00330 void operator delete(void*, void*) throw() { } 00331 00332 public: 00333 /// \brief A placeholder type used to construct an empty shell of a 00334 /// type, that will be filled in later (e.g., by some 00335 /// de-serialization). 00336 struct EmptyShell { }; 00337 00338 private: 00339 /// \brief Whether statistic collection is enabled. 00340 static bool StatisticsEnabled; 00341 00342 protected: 00343 /// \brief Construct an empty statement. 00344 explicit Stmt(StmtClass SC, EmptyShell) { 00345 StmtBits.sClass = SC; 00346 if (StatisticsEnabled) Stmt::addStmtClass(SC); 00347 } 00348 00349 public: 00350 Stmt(StmtClass SC) { 00351 StmtBits.sClass = SC; 00352 if (StatisticsEnabled) Stmt::addStmtClass(SC); 00353 } 00354 00355 StmtClass getStmtClass() const { 00356 return static_cast<StmtClass>(StmtBits.sClass); 00357 } 00358 const char *getStmtClassName() const; 00359 00360 /// SourceLocation tokens are not useful in isolation - they are low level 00361 /// value objects created/interpreted by SourceManager. We assume AST 00362 /// clients will have a pointer to the respective SourceManager. 00363 SourceRange getSourceRange() const LLVM_READONLY; 00364 SourceLocation getLocStart() const LLVM_READONLY; 00365 SourceLocation getLocEnd() const LLVM_READONLY; 00366 00367 // global temp stats (until we have a per-module visitor) 00368 static void addStmtClass(const StmtClass s); 00369 static void EnableStatistics(); 00370 static void PrintStats(); 00371 00372 /// \brief Dumps the specified AST fragment and all subtrees to 00373 /// \c llvm::errs(). 00374 void dump() const; 00375 void dump(SourceManager &SM) const; 00376 void dump(raw_ostream &OS, SourceManager &SM) const; 00377 00378 /// dumpColor - same as dump(), but forces color highlighting. 00379 void dumpColor() const; 00380 00381 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST 00382 /// back to its original source language syntax. 00383 void dumpPretty(const ASTContext &Context) const; 00384 void printPretty(raw_ostream &OS, PrinterHelper *Helper, 00385 const PrintingPolicy &Policy, 00386 unsigned Indentation = 0) const; 00387 00388 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only 00389 /// works on systems with GraphViz (Mac OS X) or dot+gv installed. 00390 void viewAST() const; 00391 00392 /// Skip past any implicit AST nodes which might surround this 00393 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes. 00394 Stmt *IgnoreImplicit(); 00395 00396 /// \brief Skip no-op (attributed, compound) container stmts and skip captured 00397 /// stmt at the top, if \a IgnoreCaptured is true. 00398 Stmt *IgnoreContainers(bool IgnoreCaptured = false); 00399 00400 const Stmt *stripLabelLikeStatements() const; 00401 Stmt *stripLabelLikeStatements() { 00402 return const_cast<Stmt*>( 00403 const_cast<const Stmt*>(this)->stripLabelLikeStatements()); 00404 } 00405 00406 /// Child Iterators: All subclasses must implement 'children' 00407 /// to permit easy iteration over the substatements/subexpessions of an 00408 /// AST node. This permits easy iteration over all nodes in the AST. 00409 typedef StmtIterator child_iterator; 00410 typedef ConstStmtIterator const_child_iterator; 00411 00412 typedef StmtRange child_range; 00413 typedef ConstStmtRange const_child_range; 00414 00415 child_range children(); 00416 const_child_range children() const { 00417 return const_cast<Stmt*>(this)->children(); 00418 } 00419 00420 child_iterator child_begin() { return children().first; } 00421 child_iterator child_end() { return children().second; } 00422 00423 const_child_iterator child_begin() const { return children().first; } 00424 const_child_iterator child_end() const { return children().second; } 00425 00426 /// \brief Produce a unique representation of the given statement. 00427 /// 00428 /// \param ID once the profiling operation is complete, will contain 00429 /// the unique representation of the given statement. 00430 /// 00431 /// \param Context the AST context in which the statement resides 00432 /// 00433 /// \param Canonical whether the profile should be based on the canonical 00434 /// representation of this statement (e.g., where non-type template 00435 /// parameters are identified by index/level rather than their 00436 /// declaration pointers) or the exact representation of the statement as 00437 /// written in the source. 00438 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, 00439 bool Canonical) const; 00440 }; 00441 00442 /// DeclStmt - Adaptor class for mixing declarations with statements and 00443 /// expressions. For example, CompoundStmt mixes statements, expressions 00444 /// and declarations (variables, types). Another example is ForStmt, where 00445 /// the first statement can be an expression or a declaration. 00446 /// 00447 class DeclStmt : public Stmt { 00448 DeclGroupRef DG; 00449 SourceLocation StartLoc, EndLoc; 00450 00451 public: 00452 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, 00453 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg), 00454 StartLoc(startLoc), EndLoc(endLoc) {} 00455 00456 /// \brief Build an empty declaration statement. 00457 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { } 00458 00459 /// isSingleDecl - This method returns true if this DeclStmt refers 00460 /// to a single Decl. 00461 bool isSingleDecl() const { 00462 return DG.isSingleDecl(); 00463 } 00464 00465 const Decl *getSingleDecl() const { return DG.getSingleDecl(); } 00466 Decl *getSingleDecl() { return DG.getSingleDecl(); } 00467 00468 const DeclGroupRef getDeclGroup() const { return DG; } 00469 DeclGroupRef getDeclGroup() { return DG; } 00470 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; } 00471 00472 SourceLocation getStartLoc() const { return StartLoc; } 00473 void setStartLoc(SourceLocation L) { StartLoc = L; } 00474 SourceLocation getEndLoc() const { return EndLoc; } 00475 void setEndLoc(SourceLocation L) { EndLoc = L; } 00476 00477 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; } 00478 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; } 00479 00480 static bool classof(const Stmt *T) { 00481 return T->getStmtClass() == DeclStmtClass; 00482 } 00483 00484 // Iterators over subexpressions. 00485 child_range children() { 00486 return child_range(child_iterator(DG.begin(), DG.end()), 00487 child_iterator(DG.end(), DG.end())); 00488 } 00489 00490 typedef DeclGroupRef::iterator decl_iterator; 00491 typedef DeclGroupRef::const_iterator const_decl_iterator; 00492 typedef llvm::iterator_range<decl_iterator> decl_range; 00493 typedef llvm::iterator_range<const_decl_iterator> decl_const_range; 00494 00495 decl_range decls() { return decl_range(decl_begin(), decl_end()); } 00496 decl_const_range decls() const { 00497 return decl_const_range(decl_begin(), decl_end()); 00498 } 00499 decl_iterator decl_begin() { return DG.begin(); } 00500 decl_iterator decl_end() { return DG.end(); } 00501 const_decl_iterator decl_begin() const { return DG.begin(); } 00502 const_decl_iterator decl_end() const { return DG.end(); } 00503 00504 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator; 00505 reverse_decl_iterator decl_rbegin() { 00506 return reverse_decl_iterator(decl_end()); 00507 } 00508 reverse_decl_iterator decl_rend() { 00509 return reverse_decl_iterator(decl_begin()); 00510 } 00511 }; 00512 00513 /// NullStmt - This is the null statement ";": C99 6.8.3p3. 00514 /// 00515 class NullStmt : public Stmt { 00516 SourceLocation SemiLoc; 00517 00518 /// \brief True if the null statement was preceded by an empty macro, e.g: 00519 /// @code 00520 /// #define CALL(x) 00521 /// CALL(0); 00522 /// @endcode 00523 bool HasLeadingEmptyMacro; 00524 public: 00525 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false) 00526 : Stmt(NullStmtClass), SemiLoc(L), 00527 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {} 00528 00529 /// \brief Build an empty null statement. 00530 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty), 00531 HasLeadingEmptyMacro(false) { } 00532 00533 SourceLocation getSemiLoc() const { return SemiLoc; } 00534 void setSemiLoc(SourceLocation L) { SemiLoc = L; } 00535 00536 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; } 00537 00538 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; } 00539 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; } 00540 00541 static bool classof(const Stmt *T) { 00542 return T->getStmtClass() == NullStmtClass; 00543 } 00544 00545 child_range children() { return child_range(); } 00546 00547 friend class ASTStmtReader; 00548 friend class ASTStmtWriter; 00549 }; 00550 00551 /// CompoundStmt - This represents a group of statements like { stmt stmt }. 00552 /// 00553 class CompoundStmt : public Stmt { 00554 Stmt** Body; 00555 SourceLocation LBraceLoc, RBraceLoc; 00556 00557 friend class ASTStmtReader; 00558 00559 public: 00560 CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts, 00561 SourceLocation LB, SourceLocation RB); 00562 00563 // \brief Build an empty compound statement with a location. 00564 explicit CompoundStmt(SourceLocation Loc) 00565 : Stmt(CompoundStmtClass), Body(nullptr), LBraceLoc(Loc), RBraceLoc(Loc) { 00566 CompoundStmtBits.NumStmts = 0; 00567 } 00568 00569 // \brief Build an empty compound statement. 00570 explicit CompoundStmt(EmptyShell Empty) 00571 : Stmt(CompoundStmtClass, Empty), Body(nullptr) { 00572 CompoundStmtBits.NumStmts = 0; 00573 } 00574 00575 void setStmts(const ASTContext &C, Stmt **Stmts, unsigned NumStmts); 00576 00577 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; } 00578 unsigned size() const { return CompoundStmtBits.NumStmts; } 00579 00580 typedef Stmt** body_iterator; 00581 typedef llvm::iterator_range<body_iterator> body_range; 00582 00583 body_range body() { return body_range(body_begin(), body_end()); } 00584 body_iterator body_begin() { return Body; } 00585 body_iterator body_end() { return Body + size(); } 00586 Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; } 00587 00588 void setLastStmt(Stmt *S) { 00589 assert(!body_empty() && "setLastStmt"); 00590 Body[size()-1] = S; 00591 } 00592 00593 typedef Stmt* const * const_body_iterator; 00594 typedef llvm::iterator_range<const_body_iterator> body_const_range; 00595 00596 body_const_range body() const { 00597 return body_const_range(body_begin(), body_end()); 00598 } 00599 const_body_iterator body_begin() const { return Body; } 00600 const_body_iterator body_end() const { return Body + size(); } 00601 const Stmt *body_back() const { 00602 return !body_empty() ? Body[size() - 1] : nullptr; 00603 } 00604 00605 typedef std::reverse_iterator<body_iterator> reverse_body_iterator; 00606 reverse_body_iterator body_rbegin() { 00607 return reverse_body_iterator(body_end()); 00608 } 00609 reverse_body_iterator body_rend() { 00610 return reverse_body_iterator(body_begin()); 00611 } 00612 00613 typedef std::reverse_iterator<const_body_iterator> 00614 const_reverse_body_iterator; 00615 00616 const_reverse_body_iterator body_rbegin() const { 00617 return const_reverse_body_iterator(body_end()); 00618 } 00619 00620 const_reverse_body_iterator body_rend() const { 00621 return const_reverse_body_iterator(body_begin()); 00622 } 00623 00624 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; } 00625 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; } 00626 00627 SourceLocation getLBracLoc() const { return LBraceLoc; } 00628 SourceLocation getRBracLoc() const { return RBraceLoc; } 00629 00630 static bool classof(const Stmt *T) { 00631 return T->getStmtClass() == CompoundStmtClass; 00632 } 00633 00634 // Iterators 00635 child_range children() { 00636 return child_range(Body, Body + CompoundStmtBits.NumStmts); 00637 } 00638 00639 const_child_range children() const { 00640 return child_range(Body, Body + CompoundStmtBits.NumStmts); 00641 } 00642 }; 00643 00644 // SwitchCase is the base class for CaseStmt and DefaultStmt, 00645 class SwitchCase : public Stmt { 00646 protected: 00647 // A pointer to the following CaseStmt or DefaultStmt class, 00648 // used by SwitchStmt. 00649 SwitchCase *NextSwitchCase; 00650 SourceLocation KeywordLoc; 00651 SourceLocation ColonLoc; 00652 00653 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc) 00654 : Stmt(SC), NextSwitchCase(nullptr), KeywordLoc(KWLoc), ColonLoc(ColonLoc) { 00655 } 00656 00657 SwitchCase(StmtClass SC, EmptyShell) 00658 : Stmt(SC), NextSwitchCase(nullptr) {} 00659 00660 public: 00661 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } 00662 00663 SwitchCase *getNextSwitchCase() { return NextSwitchCase; } 00664 00665 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } 00666 00667 SourceLocation getKeywordLoc() const { return KeywordLoc; } 00668 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; } 00669 SourceLocation getColonLoc() const { return ColonLoc; } 00670 void setColonLoc(SourceLocation L) { ColonLoc = L; } 00671 00672 Stmt *getSubStmt(); 00673 const Stmt *getSubStmt() const { 00674 return const_cast<SwitchCase*>(this)->getSubStmt(); 00675 } 00676 00677 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } 00678 SourceLocation getLocEnd() const LLVM_READONLY; 00679 00680 static bool classof(const Stmt *T) { 00681 return T->getStmtClass() == CaseStmtClass || 00682 T->getStmtClass() == DefaultStmtClass; 00683 } 00684 }; 00685 00686 class CaseStmt : public SwitchCase { 00687 enum { LHS, RHS, SUBSTMT, END_EXPR }; 00688 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for 00689 // GNU "case 1 ... 4" extension 00690 SourceLocation EllipsisLoc; 00691 public: 00692 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc, 00693 SourceLocation ellipsisLoc, SourceLocation colonLoc) 00694 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) { 00695 SubExprs[SUBSTMT] = nullptr; 00696 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs); 00697 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs); 00698 EllipsisLoc = ellipsisLoc; 00699 } 00700 00701 /// \brief Build an empty switch case statement. 00702 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { } 00703 00704 SourceLocation getCaseLoc() const { return KeywordLoc; } 00705 void setCaseLoc(SourceLocation L) { KeywordLoc = L; } 00706 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 00707 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; } 00708 SourceLocation getColonLoc() const { return ColonLoc; } 00709 void setColonLoc(SourceLocation L) { ColonLoc = L; } 00710 00711 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); } 00712 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); } 00713 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } 00714 00715 const Expr *getLHS() const { 00716 return reinterpret_cast<const Expr*>(SubExprs[LHS]); 00717 } 00718 const Expr *getRHS() const { 00719 return reinterpret_cast<const Expr*>(SubExprs[RHS]); 00720 } 00721 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; } 00722 00723 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; } 00724 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); } 00725 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); } 00726 00727 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } 00728 SourceLocation getLocEnd() const LLVM_READONLY { 00729 // Handle deeply nested case statements with iteration instead of recursion. 00730 const CaseStmt *CS = this; 00731 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt())) 00732 CS = CS2; 00733 00734 return CS->getSubStmt()->getLocEnd(); 00735 } 00736 00737 static bool classof(const Stmt *T) { 00738 return T->getStmtClass() == CaseStmtClass; 00739 } 00740 00741 // Iterators 00742 child_range children() { 00743 return child_range(&SubExprs[0], &SubExprs[END_EXPR]); 00744 } 00745 }; 00746 00747 class DefaultStmt : public SwitchCase { 00748 Stmt* SubStmt; 00749 public: 00750 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) : 00751 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {} 00752 00753 /// \brief Build an empty default statement. 00754 explicit DefaultStmt(EmptyShell Empty) 00755 : SwitchCase(DefaultStmtClass, Empty) { } 00756 00757 Stmt *getSubStmt() { return SubStmt; } 00758 const Stmt *getSubStmt() const { return SubStmt; } 00759 void setSubStmt(Stmt *S) { SubStmt = S; } 00760 00761 SourceLocation getDefaultLoc() const { return KeywordLoc; } 00762 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; } 00763 SourceLocation getColonLoc() const { return ColonLoc; } 00764 void setColonLoc(SourceLocation L) { ColonLoc = L; } 00765 00766 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } 00767 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 00768 00769 static bool classof(const Stmt *T) { 00770 return T->getStmtClass() == DefaultStmtClass; 00771 } 00772 00773 // Iterators 00774 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 00775 }; 00776 00777 inline SourceLocation SwitchCase::getLocEnd() const { 00778 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this)) 00779 return CS->getLocEnd(); 00780 return cast<DefaultStmt>(this)->getLocEnd(); 00781 } 00782 00783 /// LabelStmt - Represents a label, which has a substatement. For example: 00784 /// foo: return; 00785 /// 00786 class LabelStmt : public Stmt { 00787 LabelDecl *TheDecl; 00788 Stmt *SubStmt; 00789 SourceLocation IdentLoc; 00790 public: 00791 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt) 00792 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) { 00793 } 00794 00795 // \brief Build an empty label statement. 00796 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { } 00797 00798 SourceLocation getIdentLoc() const { return IdentLoc; } 00799 LabelDecl *getDecl() const { return TheDecl; } 00800 void setDecl(LabelDecl *D) { TheDecl = D; } 00801 const char *getName() const; 00802 Stmt *getSubStmt() { return SubStmt; } 00803 const Stmt *getSubStmt() const { return SubStmt; } 00804 void setIdentLoc(SourceLocation L) { IdentLoc = L; } 00805 void setSubStmt(Stmt *SS) { SubStmt = SS; } 00806 00807 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; } 00808 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 00809 00810 child_range children() { return child_range(&SubStmt, &SubStmt+1); } 00811 00812 static bool classof(const Stmt *T) { 00813 return T->getStmtClass() == LabelStmtClass; 00814 } 00815 }; 00816 00817 00818 /// \brief Represents an attribute applied to a statement. 00819 /// 00820 /// Represents an attribute applied to a statement. For example: 00821 /// [[omp::for(...)]] for (...) { ... } 00822 /// 00823 class AttributedStmt : public Stmt { 00824 Stmt *SubStmt; 00825 SourceLocation AttrLoc; 00826 unsigned NumAttrs; 00827 00828 friend class ASTStmtReader; 00829 00830 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt) 00831 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc), 00832 NumAttrs(Attrs.size()) { 00833 memcpy(getAttrArrayPtr(), Attrs.data(), Attrs.size() * sizeof(Attr *)); 00834 } 00835 00836 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs) 00837 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) { 00838 memset(getAttrArrayPtr(), 0, NumAttrs * sizeof(Attr *)); 00839 } 00840 00841 Attr *const *getAttrArrayPtr() const { 00842 return reinterpret_cast<Attr *const *>(this + 1); 00843 } 00844 Attr **getAttrArrayPtr() { return reinterpret_cast<Attr **>(this + 1); } 00845 00846 public: 00847 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc, 00848 ArrayRef<const Attr*> Attrs, Stmt *SubStmt); 00849 // \brief Build an empty attributed statement. 00850 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs); 00851 00852 SourceLocation getAttrLoc() const { return AttrLoc; } 00853 ArrayRef<const Attr*> getAttrs() const { 00854 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs); 00855 } 00856 Stmt *getSubStmt() { return SubStmt; } 00857 const Stmt *getSubStmt() const { return SubStmt; } 00858 00859 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; } 00860 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 00861 00862 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 00863 00864 static bool classof(const Stmt *T) { 00865 return T->getStmtClass() == AttributedStmtClass; 00866 } 00867 }; 00868 00869 00870 /// IfStmt - This represents an if/then/else. 00871 /// 00872 class IfStmt : public Stmt { 00873 enum { VAR, COND, THEN, ELSE, END_EXPR }; 00874 Stmt* SubExprs[END_EXPR]; 00875 00876 SourceLocation IfLoc; 00877 SourceLocation ElseLoc; 00878 00879 public: 00880 IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond, 00881 Stmt *then, SourceLocation EL = SourceLocation(), 00882 Stmt *elsev = nullptr); 00883 00884 /// \brief Build an empty if/then/else statement 00885 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { } 00886 00887 /// \brief Retrieve the variable declared in this "if" statement, if any. 00888 /// 00889 /// In the following example, "x" is the condition variable. 00890 /// \code 00891 /// if (int x = foo()) { 00892 /// printf("x is %d", x); 00893 /// } 00894 /// \endcode 00895 VarDecl *getConditionVariable() const; 00896 void setConditionVariable(const ASTContext &C, VarDecl *V); 00897 00898 /// If this IfStmt has a condition variable, return the faux DeclStmt 00899 /// associated with the creation of that condition variable. 00900 const DeclStmt *getConditionVariableDeclStmt() const { 00901 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 00902 } 00903 00904 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 00905 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 00906 const Stmt *getThen() const { return SubExprs[THEN]; } 00907 void setThen(Stmt *S) { SubExprs[THEN] = S; } 00908 const Stmt *getElse() const { return SubExprs[ELSE]; } 00909 void setElse(Stmt *S) { SubExprs[ELSE] = S; } 00910 00911 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 00912 Stmt *getThen() { return SubExprs[THEN]; } 00913 Stmt *getElse() { return SubExprs[ELSE]; } 00914 00915 SourceLocation getIfLoc() const { return IfLoc; } 00916 void setIfLoc(SourceLocation L) { IfLoc = L; } 00917 SourceLocation getElseLoc() const { return ElseLoc; } 00918 void setElseLoc(SourceLocation L) { ElseLoc = L; } 00919 00920 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; } 00921 SourceLocation getLocEnd() const LLVM_READONLY { 00922 if (SubExprs[ELSE]) 00923 return SubExprs[ELSE]->getLocEnd(); 00924 else 00925 return SubExprs[THEN]->getLocEnd(); 00926 } 00927 00928 // Iterators over subexpressions. The iterators will include iterating 00929 // over the initialization expression referenced by the condition variable. 00930 child_range children() { 00931 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 00932 } 00933 00934 static bool classof(const Stmt *T) { 00935 return T->getStmtClass() == IfStmtClass; 00936 } 00937 }; 00938 00939 /// SwitchStmt - This represents a 'switch' stmt. 00940 /// 00941 class SwitchStmt : public Stmt { 00942 enum { VAR, COND, BODY, END_EXPR }; 00943 Stmt* SubExprs[END_EXPR]; 00944 // This points to a linked list of case and default statements. 00945 SwitchCase *FirstCase; 00946 SourceLocation SwitchLoc; 00947 00948 /// If the SwitchStmt is a switch on an enum value, this records whether 00949 /// all the enum values were covered by CaseStmts. This value is meant to 00950 /// be a hint for possible clients. 00951 unsigned AllEnumCasesCovered : 1; 00952 00953 public: 00954 SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond); 00955 00956 /// \brief Build a empty switch statement. 00957 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { } 00958 00959 /// \brief Retrieve the variable declared in this "switch" statement, if any. 00960 /// 00961 /// In the following example, "x" is the condition variable. 00962 /// \code 00963 /// switch (int x = foo()) { 00964 /// case 0: break; 00965 /// // ... 00966 /// } 00967 /// \endcode 00968 VarDecl *getConditionVariable() const; 00969 void setConditionVariable(const ASTContext &C, VarDecl *V); 00970 00971 /// If this SwitchStmt has a condition variable, return the faux DeclStmt 00972 /// associated with the creation of that condition variable. 00973 const DeclStmt *getConditionVariableDeclStmt() const { 00974 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 00975 } 00976 00977 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 00978 const Stmt *getBody() const { return SubExprs[BODY]; } 00979 const SwitchCase *getSwitchCaseList() const { return FirstCase; } 00980 00981 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);} 00982 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); } 00983 Stmt *getBody() { return SubExprs[BODY]; } 00984 void setBody(Stmt *S) { SubExprs[BODY] = S; } 00985 SwitchCase *getSwitchCaseList() { return FirstCase; } 00986 00987 /// \brief Set the case list for this switch statement. 00988 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; } 00989 00990 SourceLocation getSwitchLoc() const { return SwitchLoc; } 00991 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; } 00992 00993 void setBody(Stmt *S, SourceLocation SL) { 00994 SubExprs[BODY] = S; 00995 SwitchLoc = SL; 00996 } 00997 void addSwitchCase(SwitchCase *SC) { 00998 assert(!SC->getNextSwitchCase() 00999 && "case/default already added to a switch"); 01000 SC->setNextSwitchCase(FirstCase); 01001 FirstCase = SC; 01002 } 01003 01004 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a 01005 /// switch over an enum value then all cases have been explicitly covered. 01006 void setAllEnumCasesCovered() { 01007 AllEnumCasesCovered = 1; 01008 } 01009 01010 /// Returns true if the SwitchStmt is a switch of an enum value and all cases 01011 /// have been explicitly covered. 01012 bool isAllEnumCasesCovered() const { 01013 return (bool) AllEnumCasesCovered; 01014 } 01015 01016 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; } 01017 SourceLocation getLocEnd() const LLVM_READONLY { 01018 return SubExprs[BODY]->getLocEnd(); 01019 } 01020 01021 // Iterators 01022 child_range children() { 01023 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 01024 } 01025 01026 static bool classof(const Stmt *T) { 01027 return T->getStmtClass() == SwitchStmtClass; 01028 } 01029 }; 01030 01031 01032 /// WhileStmt - This represents a 'while' stmt. 01033 /// 01034 class WhileStmt : public Stmt { 01035 enum { VAR, COND, BODY, END_EXPR }; 01036 Stmt* SubExprs[END_EXPR]; 01037 SourceLocation WhileLoc; 01038 public: 01039 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body, 01040 SourceLocation WL); 01041 01042 /// \brief Build an empty while statement. 01043 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { } 01044 01045 /// \brief Retrieve the variable declared in this "while" statement, if any. 01046 /// 01047 /// In the following example, "x" is the condition variable. 01048 /// \code 01049 /// while (int x = random()) { 01050 /// // ... 01051 /// } 01052 /// \endcode 01053 VarDecl *getConditionVariable() const; 01054 void setConditionVariable(const ASTContext &C, VarDecl *V); 01055 01056 /// If this WhileStmt has a condition variable, return the faux DeclStmt 01057 /// associated with the creation of that condition variable. 01058 const DeclStmt *getConditionVariableDeclStmt() const { 01059 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]); 01060 } 01061 01062 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 01063 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 01064 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 01065 Stmt *getBody() { return SubExprs[BODY]; } 01066 const Stmt *getBody() const { return SubExprs[BODY]; } 01067 void setBody(Stmt *S) { SubExprs[BODY] = S; } 01068 01069 SourceLocation getWhileLoc() const { return WhileLoc; } 01070 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 01071 01072 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; } 01073 SourceLocation getLocEnd() const LLVM_READONLY { 01074 return SubExprs[BODY]->getLocEnd(); 01075 } 01076 01077 static bool classof(const Stmt *T) { 01078 return T->getStmtClass() == WhileStmtClass; 01079 } 01080 01081 // Iterators 01082 child_range children() { 01083 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 01084 } 01085 }; 01086 01087 /// DoStmt - This represents a 'do/while' stmt. 01088 /// 01089 class DoStmt : public Stmt { 01090 enum { BODY, COND, END_EXPR }; 01091 Stmt* SubExprs[END_EXPR]; 01092 SourceLocation DoLoc; 01093 SourceLocation WhileLoc; 01094 SourceLocation RParenLoc; // Location of final ')' in do stmt condition. 01095 01096 public: 01097 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL, 01098 SourceLocation RP) 01099 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) { 01100 SubExprs[COND] = reinterpret_cast<Stmt*>(cond); 01101 SubExprs[BODY] = body; 01102 } 01103 01104 /// \brief Build an empty do-while statement. 01105 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { } 01106 01107 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 01108 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 01109 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 01110 Stmt *getBody() { return SubExprs[BODY]; } 01111 const Stmt *getBody() const { return SubExprs[BODY]; } 01112 void setBody(Stmt *S) { SubExprs[BODY] = S; } 01113 01114 SourceLocation getDoLoc() const { return DoLoc; } 01115 void setDoLoc(SourceLocation L) { DoLoc = L; } 01116 SourceLocation getWhileLoc() const { return WhileLoc; } 01117 void setWhileLoc(SourceLocation L) { WhileLoc = L; } 01118 01119 SourceLocation getRParenLoc() const { return RParenLoc; } 01120 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 01121 01122 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; } 01123 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 01124 01125 static bool classof(const Stmt *T) { 01126 return T->getStmtClass() == DoStmtClass; 01127 } 01128 01129 // Iterators 01130 child_range children() { 01131 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 01132 } 01133 }; 01134 01135 01136 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of 01137 /// the init/cond/inc parts of the ForStmt will be null if they were not 01138 /// specified in the source. 01139 /// 01140 class ForStmt : public Stmt { 01141 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR }; 01142 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. 01143 SourceLocation ForLoc; 01144 SourceLocation LParenLoc, RParenLoc; 01145 01146 public: 01147 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, 01148 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP, 01149 SourceLocation RP); 01150 01151 /// \brief Build an empty for statement. 01152 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { } 01153 01154 Stmt *getInit() { return SubExprs[INIT]; } 01155 01156 /// \brief Retrieve the variable declared in this "for" statement, if any. 01157 /// 01158 /// In the following example, "y" is the condition variable. 01159 /// \code 01160 /// for (int x = random(); int y = mangle(x); ++x) { 01161 /// // ... 01162 /// } 01163 /// \endcode 01164 VarDecl *getConditionVariable() const; 01165 void setConditionVariable(const ASTContext &C, VarDecl *V); 01166 01167 /// If this ForStmt has a condition variable, return the faux DeclStmt 01168 /// associated with the creation of that condition variable. 01169 const DeclStmt *getConditionVariableDeclStmt() const { 01170 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]); 01171 } 01172 01173 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); } 01174 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); } 01175 Stmt *getBody() { return SubExprs[BODY]; } 01176 01177 const Stmt *getInit() const { return SubExprs[INIT]; } 01178 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);} 01179 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); } 01180 const Stmt *getBody() const { return SubExprs[BODY]; } 01181 01182 void setInit(Stmt *S) { SubExprs[INIT] = S; } 01183 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); } 01184 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); } 01185 void setBody(Stmt *S) { SubExprs[BODY] = S; } 01186 01187 SourceLocation getForLoc() const { return ForLoc; } 01188 void setForLoc(SourceLocation L) { ForLoc = L; } 01189 SourceLocation getLParenLoc() const { return LParenLoc; } 01190 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 01191 SourceLocation getRParenLoc() const { return RParenLoc; } 01192 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 01193 01194 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; } 01195 SourceLocation getLocEnd() const LLVM_READONLY { 01196 return SubExprs[BODY]->getLocEnd(); 01197 } 01198 01199 static bool classof(const Stmt *T) { 01200 return T->getStmtClass() == ForStmtClass; 01201 } 01202 01203 // Iterators 01204 child_range children() { 01205 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR); 01206 } 01207 }; 01208 01209 /// GotoStmt - This represents a direct goto. 01210 /// 01211 class GotoStmt : public Stmt { 01212 LabelDecl *Label; 01213 SourceLocation GotoLoc; 01214 SourceLocation LabelLoc; 01215 public: 01216 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL) 01217 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {} 01218 01219 /// \brief Build an empty goto statement. 01220 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { } 01221 01222 LabelDecl *getLabel() const { return Label; } 01223 void setLabel(LabelDecl *D) { Label = D; } 01224 01225 SourceLocation getGotoLoc() const { return GotoLoc; } 01226 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 01227 SourceLocation getLabelLoc() const { return LabelLoc; } 01228 void setLabelLoc(SourceLocation L) { LabelLoc = L; } 01229 01230 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; } 01231 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; } 01232 01233 static bool classof(const Stmt *T) { 01234 return T->getStmtClass() == GotoStmtClass; 01235 } 01236 01237 // Iterators 01238 child_range children() { return child_range(); } 01239 }; 01240 01241 /// IndirectGotoStmt - This represents an indirect goto. 01242 /// 01243 class IndirectGotoStmt : public Stmt { 01244 SourceLocation GotoLoc; 01245 SourceLocation StarLoc; 01246 Stmt *Target; 01247 public: 01248 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, 01249 Expr *target) 01250 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc), 01251 Target((Stmt*)target) {} 01252 01253 /// \brief Build an empty indirect goto statement. 01254 explicit IndirectGotoStmt(EmptyShell Empty) 01255 : Stmt(IndirectGotoStmtClass, Empty) { } 01256 01257 void setGotoLoc(SourceLocation L) { GotoLoc = L; } 01258 SourceLocation getGotoLoc() const { return GotoLoc; } 01259 void setStarLoc(SourceLocation L) { StarLoc = L; } 01260 SourceLocation getStarLoc() const { return StarLoc; } 01261 01262 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); } 01263 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);} 01264 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); } 01265 01266 /// getConstantTarget - Returns the fixed target of this indirect 01267 /// goto, if one exists. 01268 LabelDecl *getConstantTarget(); 01269 const LabelDecl *getConstantTarget() const { 01270 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget(); 01271 } 01272 01273 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; } 01274 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); } 01275 01276 static bool classof(const Stmt *T) { 01277 return T->getStmtClass() == IndirectGotoStmtClass; 01278 } 01279 01280 // Iterators 01281 child_range children() { return child_range(&Target, &Target+1); } 01282 }; 01283 01284 01285 /// ContinueStmt - This represents a continue. 01286 /// 01287 class ContinueStmt : public Stmt { 01288 SourceLocation ContinueLoc; 01289 public: 01290 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {} 01291 01292 /// \brief Build an empty continue statement. 01293 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { } 01294 01295 SourceLocation getContinueLoc() const { return ContinueLoc; } 01296 void setContinueLoc(SourceLocation L) { ContinueLoc = L; } 01297 01298 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; } 01299 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; } 01300 01301 static bool classof(const Stmt *T) { 01302 return T->getStmtClass() == ContinueStmtClass; 01303 } 01304 01305 // Iterators 01306 child_range children() { return child_range(); } 01307 }; 01308 01309 /// BreakStmt - This represents a break. 01310 /// 01311 class BreakStmt : public Stmt { 01312 SourceLocation BreakLoc; 01313 public: 01314 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {} 01315 01316 /// \brief Build an empty break statement. 01317 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { } 01318 01319 SourceLocation getBreakLoc() const { return BreakLoc; } 01320 void setBreakLoc(SourceLocation L) { BreakLoc = L; } 01321 01322 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; } 01323 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; } 01324 01325 static bool classof(const Stmt *T) { 01326 return T->getStmtClass() == BreakStmtClass; 01327 } 01328 01329 // Iterators 01330 child_range children() { return child_range(); } 01331 }; 01332 01333 01334 /// ReturnStmt - This represents a return, optionally of an expression: 01335 /// return; 01336 /// return 4; 01337 /// 01338 /// Note that GCC allows return with no argument in a function declared to 01339 /// return a value, and it allows returning a value in functions declared to 01340 /// return void. We explicitly model this in the AST, which means you can't 01341 /// depend on the return type of the function and the presence of an argument. 01342 /// 01343 class ReturnStmt : public Stmt { 01344 Stmt *RetExpr; 01345 SourceLocation RetLoc; 01346 const VarDecl *NRVOCandidate; 01347 01348 public: 01349 ReturnStmt(SourceLocation RL) 01350 : Stmt(ReturnStmtClass), RetExpr(nullptr), RetLoc(RL), 01351 NRVOCandidate(nullptr) {} 01352 01353 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate) 01354 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL), 01355 NRVOCandidate(NRVOCandidate) {} 01356 01357 /// \brief Build an empty return expression. 01358 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { } 01359 01360 const Expr *getRetValue() const; 01361 Expr *getRetValue(); 01362 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); } 01363 01364 SourceLocation getReturnLoc() const { return RetLoc; } 01365 void setReturnLoc(SourceLocation L) { RetLoc = L; } 01366 01367 /// \brief Retrieve the variable that might be used for the named return 01368 /// value optimization. 01369 /// 01370 /// The optimization itself can only be performed if the variable is 01371 /// also marked as an NRVO object. 01372 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; } 01373 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; } 01374 01375 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; } 01376 SourceLocation getLocEnd() const LLVM_READONLY { 01377 return RetExpr ? RetExpr->getLocEnd() : RetLoc; 01378 } 01379 01380 static bool classof(const Stmt *T) { 01381 return T->getStmtClass() == ReturnStmtClass; 01382 } 01383 01384 // Iterators 01385 child_range children() { 01386 if (RetExpr) return child_range(&RetExpr, &RetExpr+1); 01387 return child_range(); 01388 } 01389 }; 01390 01391 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt. 01392 /// 01393 class AsmStmt : public Stmt { 01394 protected: 01395 SourceLocation AsmLoc; 01396 /// \brief True if the assembly statement does not have any input or output 01397 /// operands. 01398 bool IsSimple; 01399 01400 /// \brief If true, treat this inline assembly as having side effects. 01401 /// This assembly statement should not be optimized, deleted or moved. 01402 bool IsVolatile; 01403 01404 unsigned NumOutputs; 01405 unsigned NumInputs; 01406 unsigned NumClobbers; 01407 01408 Stmt **Exprs; 01409 01410 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile, 01411 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) : 01412 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile), 01413 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { } 01414 01415 friend class ASTStmtReader; 01416 01417 public: 01418 /// \brief Build an empty inline-assembly statement. 01419 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : 01420 Stmt(SC, Empty), Exprs(nullptr) { } 01421 01422 SourceLocation getAsmLoc() const { return AsmLoc; } 01423 void setAsmLoc(SourceLocation L) { AsmLoc = L; } 01424 01425 bool isSimple() const { return IsSimple; } 01426 void setSimple(bool V) { IsSimple = V; } 01427 01428 bool isVolatile() const { return IsVolatile; } 01429 void setVolatile(bool V) { IsVolatile = V; } 01430 01431 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); } 01432 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); } 01433 01434 //===--- Asm String Analysis ---===// 01435 01436 /// Assemble final IR asm string. 01437 std::string generateAsmString(const ASTContext &C) const; 01438 01439 //===--- Output operands ---===// 01440 01441 unsigned getNumOutputs() const { return NumOutputs; } 01442 01443 /// getOutputConstraint - Return the constraint string for the specified 01444 /// output operand. All output constraints are known to be non-empty (either 01445 /// '=' or '+'). 01446 StringRef getOutputConstraint(unsigned i) const; 01447 01448 /// isOutputPlusConstraint - Return true if the specified output constraint 01449 /// is a "+" constraint (which is both an input and an output) or false if it 01450 /// is an "=" constraint (just an output). 01451 bool isOutputPlusConstraint(unsigned i) const { 01452 return getOutputConstraint(i)[0] == '+'; 01453 } 01454 01455 const Expr *getOutputExpr(unsigned i) const; 01456 01457 /// getNumPlusOperands - Return the number of output operands that have a "+" 01458 /// constraint. 01459 unsigned getNumPlusOperands() const; 01460 01461 //===--- Input operands ---===// 01462 01463 unsigned getNumInputs() const { return NumInputs; } 01464 01465 /// getInputConstraint - Return the specified input constraint. Unlike output 01466 /// constraints, these can be empty. 01467 StringRef getInputConstraint(unsigned i) const; 01468 01469 const Expr *getInputExpr(unsigned i) const; 01470 01471 //===--- Other ---===// 01472 01473 unsigned getNumClobbers() const { return NumClobbers; } 01474 StringRef getClobber(unsigned i) const; 01475 01476 static bool classof(const Stmt *T) { 01477 return T->getStmtClass() == GCCAsmStmtClass || 01478 T->getStmtClass() == MSAsmStmtClass; 01479 } 01480 01481 // Input expr iterators. 01482 01483 typedef ExprIterator inputs_iterator; 01484 typedef ConstExprIterator const_inputs_iterator; 01485 typedef llvm::iterator_range<inputs_iterator> inputs_range; 01486 typedef llvm::iterator_range<const_inputs_iterator> inputs_const_range; 01487 01488 inputs_iterator begin_inputs() { 01489 return &Exprs[0] + NumOutputs; 01490 } 01491 01492 inputs_iterator end_inputs() { 01493 return &Exprs[0] + NumOutputs + NumInputs; 01494 } 01495 01496 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); } 01497 01498 const_inputs_iterator begin_inputs() const { 01499 return &Exprs[0] + NumOutputs; 01500 } 01501 01502 const_inputs_iterator end_inputs() const { 01503 return &Exprs[0] + NumOutputs + NumInputs; 01504 } 01505 01506 inputs_const_range inputs() const { 01507 return inputs_const_range(begin_inputs(), end_inputs()); 01508 } 01509 01510 // Output expr iterators. 01511 01512 typedef ExprIterator outputs_iterator; 01513 typedef ConstExprIterator const_outputs_iterator; 01514 typedef llvm::iterator_range<outputs_iterator> outputs_range; 01515 typedef llvm::iterator_range<const_outputs_iterator> outputs_const_range; 01516 01517 outputs_iterator begin_outputs() { 01518 return &Exprs[0]; 01519 } 01520 outputs_iterator end_outputs() { 01521 return &Exprs[0] + NumOutputs; 01522 } 01523 outputs_range outputs() { 01524 return outputs_range(begin_outputs(), end_outputs()); 01525 } 01526 01527 const_outputs_iterator begin_outputs() const { 01528 return &Exprs[0]; 01529 } 01530 const_outputs_iterator end_outputs() const { 01531 return &Exprs[0] + NumOutputs; 01532 } 01533 outputs_const_range outputs() const { 01534 return outputs_const_range(begin_outputs(), end_outputs()); 01535 } 01536 01537 child_range children() { 01538 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs); 01539 } 01540 }; 01541 01542 /// This represents a GCC inline-assembly statement extension. 01543 /// 01544 class GCCAsmStmt : public AsmStmt { 01545 SourceLocation RParenLoc; 01546 StringLiteral *AsmStr; 01547 01548 // FIXME: If we wanted to, we could allocate all of these in one big array. 01549 StringLiteral **Constraints; 01550 StringLiteral **Clobbers; 01551 IdentifierInfo **Names; 01552 01553 friend class ASTStmtReader; 01554 01555 public: 01556 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple, 01557 bool isvolatile, unsigned numoutputs, unsigned numinputs, 01558 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs, 01559 StringLiteral *asmstr, unsigned numclobbers, 01560 StringLiteral **clobbers, SourceLocation rparenloc); 01561 01562 /// \brief Build an empty inline-assembly statement. 01563 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty), 01564 Constraints(nullptr), Clobbers(nullptr), Names(nullptr) { } 01565 01566 SourceLocation getRParenLoc() const { return RParenLoc; } 01567 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 01568 01569 //===--- Asm String Analysis ---===// 01570 01571 const StringLiteral *getAsmString() const { return AsmStr; } 01572 StringLiteral *getAsmString() { return AsmStr; } 01573 void setAsmString(StringLiteral *E) { AsmStr = E; } 01574 01575 /// AsmStringPiece - this is part of a decomposed asm string specification 01576 /// (for use with the AnalyzeAsmString function below). An asm string is 01577 /// considered to be a concatenation of these parts. 01578 class AsmStringPiece { 01579 public: 01580 enum Kind { 01581 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%". 01582 Operand // Operand reference, with optional modifier %c4. 01583 }; 01584 private: 01585 Kind MyKind; 01586 std::string Str; 01587 unsigned OperandNo; 01588 01589 // Source range for operand references. 01590 CharSourceRange Range; 01591 public: 01592 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {} 01593 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin, 01594 SourceLocation End) 01595 : MyKind(Operand), Str(S), OperandNo(OpNo), 01596 Range(CharSourceRange::getCharRange(Begin, End)) { 01597 } 01598 01599 bool isString() const { return MyKind == String; } 01600 bool isOperand() const { return MyKind == Operand; } 01601 01602 const std::string &getString() const { 01603 return Str; 01604 } 01605 01606 unsigned getOperandNo() const { 01607 assert(isOperand()); 01608 return OperandNo; 01609 } 01610 01611 CharSourceRange getRange() const { 01612 assert(isOperand() && "Range is currently used only for Operands."); 01613 return Range; 01614 } 01615 01616 /// getModifier - Get the modifier for this operand, if present. This 01617 /// returns '\0' if there was no modifier. 01618 char getModifier() const; 01619 }; 01620 01621 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing 01622 /// it into pieces. If the asm string is erroneous, emit errors and return 01623 /// true, otherwise return false. This handles canonicalization and 01624 /// translation of strings from GCC syntax to LLVM IR syntax, and handles 01625 //// flattening of named references like %[foo] to Operand AsmStringPiece's. 01626 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces, 01627 const ASTContext &C, unsigned &DiagOffs) const; 01628 01629 /// Assemble final IR asm string. 01630 std::string generateAsmString(const ASTContext &C) const; 01631 01632 //===--- Output operands ---===// 01633 01634 IdentifierInfo *getOutputIdentifier(unsigned i) const { 01635 return Names[i]; 01636 } 01637 01638 StringRef getOutputName(unsigned i) const { 01639 if (IdentifierInfo *II = getOutputIdentifier(i)) 01640 return II->getName(); 01641 01642 return StringRef(); 01643 } 01644 01645 StringRef getOutputConstraint(unsigned i) const; 01646 01647 const StringLiteral *getOutputConstraintLiteral(unsigned i) const { 01648 return Constraints[i]; 01649 } 01650 StringLiteral *getOutputConstraintLiteral(unsigned i) { 01651 return Constraints[i]; 01652 } 01653 01654 Expr *getOutputExpr(unsigned i); 01655 01656 const Expr *getOutputExpr(unsigned i) const { 01657 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i); 01658 } 01659 01660 //===--- Input operands ---===// 01661 01662 IdentifierInfo *getInputIdentifier(unsigned i) const { 01663 return Names[i + NumOutputs]; 01664 } 01665 01666 StringRef getInputName(unsigned i) const { 01667 if (IdentifierInfo *II = getInputIdentifier(i)) 01668 return II->getName(); 01669 01670 return StringRef(); 01671 } 01672 01673 StringRef getInputConstraint(unsigned i) const; 01674 01675 const StringLiteral *getInputConstraintLiteral(unsigned i) const { 01676 return Constraints[i + NumOutputs]; 01677 } 01678 StringLiteral *getInputConstraintLiteral(unsigned i) { 01679 return Constraints[i + NumOutputs]; 01680 } 01681 01682 Expr *getInputExpr(unsigned i); 01683 void setInputExpr(unsigned i, Expr *E); 01684 01685 const Expr *getInputExpr(unsigned i) const { 01686 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i); 01687 } 01688 01689 private: 01690 void setOutputsAndInputsAndClobbers(const ASTContext &C, 01691 IdentifierInfo **Names, 01692 StringLiteral **Constraints, 01693 Stmt **Exprs, 01694 unsigned NumOutputs, 01695 unsigned NumInputs, 01696 StringLiteral **Clobbers, 01697 unsigned NumClobbers); 01698 public: 01699 01700 //===--- Other ---===// 01701 01702 /// getNamedOperand - Given a symbolic operand reference like %[foo], 01703 /// translate this into a numeric value needed to reference the same operand. 01704 /// This returns -1 if the operand name is invalid. 01705 int getNamedOperand(StringRef SymbolicName) const; 01706 01707 StringRef getClobber(unsigned i) const; 01708 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; } 01709 const StringLiteral *getClobberStringLiteral(unsigned i) const { 01710 return Clobbers[i]; 01711 } 01712 01713 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; } 01714 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 01715 01716 static bool classof(const Stmt *T) { 01717 return T->getStmtClass() == GCCAsmStmtClass; 01718 } 01719 }; 01720 01721 /// This represents a Microsoft inline-assembly statement extension. 01722 /// 01723 class MSAsmStmt : public AsmStmt { 01724 SourceLocation LBraceLoc, EndLoc; 01725 StringRef AsmStr; 01726 01727 unsigned NumAsmToks; 01728 01729 Token *AsmToks; 01730 StringRef *Constraints; 01731 StringRef *Clobbers; 01732 01733 friend class ASTStmtReader; 01734 01735 public: 01736 MSAsmStmt(const ASTContext &C, SourceLocation asmloc, 01737 SourceLocation lbraceloc, bool issimple, bool isvolatile, 01738 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs, 01739 ArrayRef<StringRef> constraints, 01740 ArrayRef<Expr*> exprs, StringRef asmstr, 01741 ArrayRef<StringRef> clobbers, SourceLocation endloc); 01742 01743 /// \brief Build an empty MS-style inline-assembly statement. 01744 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty), 01745 NumAsmToks(0), AsmToks(nullptr), Constraints(nullptr), Clobbers(nullptr) { } 01746 01747 SourceLocation getLBraceLoc() const { return LBraceLoc; } 01748 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; } 01749 SourceLocation getEndLoc() const { return EndLoc; } 01750 void setEndLoc(SourceLocation L) { EndLoc = L; } 01751 01752 bool hasBraces() const { return LBraceLoc.isValid(); } 01753 01754 unsigned getNumAsmToks() { return NumAsmToks; } 01755 Token *getAsmToks() { return AsmToks; } 01756 01757 //===--- Asm String Analysis ---===// 01758 StringRef getAsmString() const { return AsmStr; } 01759 01760 /// Assemble final IR asm string. 01761 std::string generateAsmString(const ASTContext &C) const; 01762 01763 //===--- Output operands ---===// 01764 01765 StringRef getOutputConstraint(unsigned i) const { 01766 assert(i < NumOutputs); 01767 return Constraints[i]; 01768 } 01769 01770 Expr *getOutputExpr(unsigned i); 01771 01772 const Expr *getOutputExpr(unsigned i) const { 01773 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i); 01774 } 01775 01776 //===--- Input operands ---===// 01777 01778 StringRef getInputConstraint(unsigned i) const { 01779 assert(i < NumInputs); 01780 return Constraints[i + NumOutputs]; 01781 } 01782 01783 Expr *getInputExpr(unsigned i); 01784 void setInputExpr(unsigned i, Expr *E); 01785 01786 const Expr *getInputExpr(unsigned i) const { 01787 return const_cast<MSAsmStmt*>(this)->getInputExpr(i); 01788 } 01789 01790 //===--- Other ---===// 01791 01792 ArrayRef<StringRef> getAllConstraints() const { 01793 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs); 01794 } 01795 ArrayRef<StringRef> getClobbers() const { 01796 return llvm::makeArrayRef(Clobbers, NumClobbers); 01797 } 01798 ArrayRef<Expr*> getAllExprs() const { 01799 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs), 01800 NumInputs + NumOutputs); 01801 } 01802 01803 StringRef getClobber(unsigned i) const { return getClobbers()[i]; } 01804 01805 private: 01806 void initialize(const ASTContext &C, StringRef AsmString, 01807 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints, 01808 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers); 01809 public: 01810 01811 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; } 01812 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; } 01813 01814 static bool classof(const Stmt *T) { 01815 return T->getStmtClass() == MSAsmStmtClass; 01816 } 01817 01818 child_range children() { 01819 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]); 01820 } 01821 }; 01822 01823 class SEHExceptStmt : public Stmt { 01824 SourceLocation Loc; 01825 Stmt *Children[2]; 01826 01827 enum { FILTER_EXPR, BLOCK }; 01828 01829 SEHExceptStmt(SourceLocation Loc, 01830 Expr *FilterExpr, 01831 Stmt *Block); 01832 01833 friend class ASTReader; 01834 friend class ASTStmtReader; 01835 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { } 01836 01837 public: 01838 static SEHExceptStmt* Create(const ASTContext &C, 01839 SourceLocation ExceptLoc, 01840 Expr *FilterExpr, 01841 Stmt *Block); 01842 01843 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); } 01844 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 01845 01846 SourceLocation getExceptLoc() const { return Loc; } 01847 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); } 01848 01849 Expr *getFilterExpr() const { 01850 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]); 01851 } 01852 01853 CompoundStmt *getBlock() const { 01854 return cast<CompoundStmt>(Children[BLOCK]); 01855 } 01856 01857 child_range children() { 01858 return child_range(Children,Children+2); 01859 } 01860 01861 static bool classof(const Stmt *T) { 01862 return T->getStmtClass() == SEHExceptStmtClass; 01863 } 01864 01865 }; 01866 01867 class SEHFinallyStmt : public Stmt { 01868 SourceLocation Loc; 01869 Stmt *Block; 01870 01871 SEHFinallyStmt(SourceLocation Loc, 01872 Stmt *Block); 01873 01874 friend class ASTReader; 01875 friend class ASTStmtReader; 01876 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { } 01877 01878 public: 01879 static SEHFinallyStmt* Create(const ASTContext &C, 01880 SourceLocation FinallyLoc, 01881 Stmt *Block); 01882 01883 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); } 01884 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 01885 01886 SourceLocation getFinallyLoc() const { return Loc; } 01887 SourceLocation getEndLoc() const { return Block->getLocEnd(); } 01888 01889 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); } 01890 01891 child_range children() { 01892 return child_range(&Block,&Block+1); 01893 } 01894 01895 static bool classof(const Stmt *T) { 01896 return T->getStmtClass() == SEHFinallyStmtClass; 01897 } 01898 01899 }; 01900 01901 class SEHTryStmt : public Stmt { 01902 bool IsCXXTry; 01903 SourceLocation TryLoc; 01904 Stmt *Children[2]; 01905 01906 enum { TRY = 0, HANDLER = 1 }; 01907 01908 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try' 01909 SourceLocation TryLoc, 01910 Stmt *TryBlock, 01911 Stmt *Handler); 01912 01913 friend class ASTReader; 01914 friend class ASTStmtReader; 01915 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { } 01916 01917 public: 01918 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry, 01919 SourceLocation TryLoc, Stmt *TryBlock, 01920 Stmt *Handler); 01921 01922 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); } 01923 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 01924 01925 SourceLocation getTryLoc() const { return TryLoc; } 01926 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); } 01927 01928 bool getIsCXXTry() const { return IsCXXTry; } 01929 01930 CompoundStmt* getTryBlock() const { 01931 return cast<CompoundStmt>(Children[TRY]); 01932 } 01933 01934 Stmt *getHandler() const { return Children[HANDLER]; } 01935 01936 /// Returns 0 if not defined 01937 SEHExceptStmt *getExceptHandler() const; 01938 SEHFinallyStmt *getFinallyHandler() const; 01939 01940 child_range children() { 01941 return child_range(Children,Children+2); 01942 } 01943 01944 static bool classof(const Stmt *T) { 01945 return T->getStmtClass() == SEHTryStmtClass; 01946 } 01947 }; 01948 01949 /// Represents a __leave statement. 01950 /// 01951 class SEHLeaveStmt : public Stmt { 01952 SourceLocation LeaveLoc; 01953 public: 01954 explicit SEHLeaveStmt(SourceLocation LL) 01955 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {} 01956 01957 /// \brief Build an empty __leave statement. 01958 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) { } 01959 01960 SourceLocation getLeaveLoc() const { return LeaveLoc; } 01961 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; } 01962 01963 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; } 01964 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; } 01965 01966 static bool classof(const Stmt *T) { 01967 return T->getStmtClass() == SEHLeaveStmtClass; 01968 } 01969 01970 // Iterators 01971 child_range children() { return child_range(); } 01972 }; 01973 01974 /// \brief This captures a statement into a function. For example, the following 01975 /// pragma annotated compound statement can be represented as a CapturedStmt, 01976 /// and this compound statement is the body of an anonymous outlined function. 01977 /// @code 01978 /// #pragma omp parallel 01979 /// { 01980 /// compute(); 01981 /// } 01982 /// @endcode 01983 class CapturedStmt : public Stmt { 01984 public: 01985 /// \brief The different capture forms: by 'this', by reference, capture for 01986 /// variable-length array type etc. 01987 enum VariableCaptureKind { 01988 VCK_This, 01989 VCK_ByRef, 01990 VCK_VLAType, 01991 }; 01992 01993 /// \brief Describes the capture of either a variable, or 'this', or 01994 /// variable-length array type. 01995 class Capture { 01996 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind; 01997 SourceLocation Loc; 01998 01999 public: 02000 /// \brief Create a new capture. 02001 /// 02002 /// \param Loc The source location associated with this capture. 02003 /// 02004 /// \param Kind The kind of capture (this, ByRef, ...). 02005 /// 02006 /// \param Var The variable being captured, or null if capturing this. 02007 /// 02008 Capture(SourceLocation Loc, VariableCaptureKind Kind, 02009 VarDecl *Var = nullptr) 02010 : VarAndKind(Var, Kind), Loc(Loc) { 02011 switch (Kind) { 02012 case VCK_This: 02013 assert(!Var && "'this' capture cannot have a variable!"); 02014 break; 02015 case VCK_ByRef: 02016 assert(Var && "capturing by reference must have a variable!"); 02017 break; 02018 case VCK_VLAType: 02019 assert(!Var && 02020 "Variable-length array type capture cannot have a variable!"); 02021 break; 02022 } 02023 } 02024 02025 /// \brief Determine the kind of capture. 02026 VariableCaptureKind getCaptureKind() const { return VarAndKind.getInt(); } 02027 02028 /// \brief Retrieve the source location at which the variable or 'this' was 02029 /// first used. 02030 SourceLocation getLocation() const { return Loc; } 02031 02032 /// \brief Determine whether this capture handles the C++ 'this' pointer. 02033 bool capturesThis() const { return getCaptureKind() == VCK_This; } 02034 02035 /// \brief Determine whether this capture handles a variable. 02036 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; } 02037 02038 /// \brief Determine whether this capture handles a variable-length array 02039 /// type. 02040 bool capturesVariableArrayType() const { 02041 return getCaptureKind() == VCK_VLAType; 02042 } 02043 02044 /// \brief Retrieve the declaration of the variable being captured. 02045 /// 02046 /// This operation is only valid if this capture captures a variable. 02047 VarDecl *getCapturedVar() const { 02048 assert(capturesVariable() && 02049 "No variable available for 'this' or VAT capture"); 02050 return VarAndKind.getPointer(); 02051 } 02052 friend class ASTStmtReader; 02053 }; 02054 02055 private: 02056 /// \brief The number of variable captured, including 'this'. 02057 unsigned NumCaptures; 02058 02059 /// \brief The pointer part is the implicit the outlined function and the 02060 /// int part is the captured region kind, 'CR_Default' etc. 02061 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind; 02062 02063 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl. 02064 RecordDecl *TheRecordDecl; 02065 02066 /// \brief Construct a captured statement. 02067 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures, 02068 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD); 02069 02070 /// \brief Construct an empty captured statement. 02071 CapturedStmt(EmptyShell Empty, unsigned NumCaptures); 02072 02073 Stmt **getStoredStmts() const { 02074 return reinterpret_cast<Stmt **>(const_cast<CapturedStmt *>(this) + 1); 02075 } 02076 02077 Capture *getStoredCaptures() const; 02078 02079 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; } 02080 02081 public: 02082 static CapturedStmt *Create(const ASTContext &Context, Stmt *S, 02083 CapturedRegionKind Kind, 02084 ArrayRef<Capture> Captures, 02085 ArrayRef<Expr *> CaptureInits, 02086 CapturedDecl *CD, RecordDecl *RD); 02087 02088 static CapturedStmt *CreateDeserialized(const ASTContext &Context, 02089 unsigned NumCaptures); 02090 02091 /// \brief Retrieve the statement being captured. 02092 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; } 02093 const Stmt *getCapturedStmt() const { 02094 return const_cast<CapturedStmt *>(this)->getCapturedStmt(); 02095 } 02096 02097 /// \brief Retrieve the outlined function declaration. 02098 CapturedDecl *getCapturedDecl() { return CapDeclAndKind.getPointer(); } 02099 const CapturedDecl *getCapturedDecl() const { 02100 return const_cast<CapturedStmt *>(this)->getCapturedDecl(); 02101 } 02102 02103 /// \brief Set the outlined function declaration. 02104 void setCapturedDecl(CapturedDecl *D) { 02105 assert(D && "null CapturedDecl"); 02106 CapDeclAndKind.setPointer(D); 02107 } 02108 02109 /// \brief Retrieve the captured region kind. 02110 CapturedRegionKind getCapturedRegionKind() const { 02111 return CapDeclAndKind.getInt(); 02112 } 02113 02114 /// \brief Set the captured region kind. 02115 void setCapturedRegionKind(CapturedRegionKind Kind) { 02116 CapDeclAndKind.setInt(Kind); 02117 } 02118 02119 /// \brief Retrieve the record declaration for captured variables. 02120 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; } 02121 02122 /// \brief Set the record declaration for captured variables. 02123 void setCapturedRecordDecl(RecordDecl *D) { 02124 assert(D && "null RecordDecl"); 02125 TheRecordDecl = D; 02126 } 02127 02128 /// \brief True if this variable has been captured. 02129 bool capturesVariable(const VarDecl *Var) const; 02130 02131 /// \brief An iterator that walks over the captures. 02132 typedef Capture *capture_iterator; 02133 typedef const Capture *const_capture_iterator; 02134 typedef llvm::iterator_range<capture_iterator> capture_range; 02135 typedef llvm::iterator_range<const_capture_iterator> capture_const_range; 02136 02137 capture_range captures() { 02138 return capture_range(capture_begin(), capture_end()); 02139 } 02140 capture_const_range captures() const { 02141 return capture_const_range(capture_begin(), capture_end()); 02142 } 02143 02144 /// \brief Retrieve an iterator pointing to the first capture. 02145 capture_iterator capture_begin() { return getStoredCaptures(); } 02146 const_capture_iterator capture_begin() const { return getStoredCaptures(); } 02147 02148 /// \brief Retrieve an iterator pointing past the end of the sequence of 02149 /// captures. 02150 capture_iterator capture_end() const { 02151 return getStoredCaptures() + NumCaptures; 02152 } 02153 02154 /// \brief Retrieve the number of captures, including 'this'. 02155 unsigned capture_size() const { return NumCaptures; } 02156 02157 /// \brief Iterator that walks over the capture initialization arguments. 02158 typedef Expr **capture_init_iterator; 02159 typedef llvm::iterator_range<capture_init_iterator> capture_init_range; 02160 02161 capture_init_range capture_inits() const { 02162 return capture_init_range(capture_init_begin(), capture_init_end()); 02163 } 02164 02165 /// \brief Retrieve the first initialization argument. 02166 capture_init_iterator capture_init_begin() const { 02167 return reinterpret_cast<Expr **>(getStoredStmts()); 02168 } 02169 02170 /// \brief Retrieve the iterator pointing one past the last initialization 02171 /// argument. 02172 capture_init_iterator capture_init_end() const { 02173 return capture_init_begin() + NumCaptures; 02174 } 02175 02176 SourceLocation getLocStart() const LLVM_READONLY { 02177 return getCapturedStmt()->getLocStart(); 02178 } 02179 SourceLocation getLocEnd() const LLVM_READONLY { 02180 return getCapturedStmt()->getLocEnd(); 02181 } 02182 SourceRange getSourceRange() const LLVM_READONLY { 02183 return getCapturedStmt()->getSourceRange(); 02184 } 02185 02186 static bool classof(const Stmt *T) { 02187 return T->getStmtClass() == CapturedStmtClass; 02188 } 02189 02190 child_range children(); 02191 02192 friend class ASTStmtReader; 02193 }; 02194 02195 } // end namespace clang 02196 02197 #endif