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