1 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Stmt interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
17 #include "clang/AST/DeclGroup.h"
18 #include "clang/AST/StmtIterator.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/ErrorHandling.h"
28 class FoldingSetNodeID;
40 struct PrintingPolicy;
48 //===--------------------------------------------------------------------===//
49 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
50 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
51 // references to children (to be compatible with StmtIterator).
52 //===--------------------------------------------------------------------===//
60 ExprIterator(Stmt** i) : I(i) {}
61 ExprIterator() : I(0) {}
62 ExprIterator& operator++() { ++I; return *this; }
63 ExprIterator operator-(size_t i) { return I-i; }
64 ExprIterator operator+(size_t i) { return I+i; }
65 Expr* operator[](size_t idx);
66 // FIXME: Verify that this will correctly return a signed distance.
67 signed operator-(const ExprIterator& R) const { return I - R.I; }
68 Expr* operator*() const;
69 Expr* operator->() const;
70 bool operator==(const ExprIterator& R) const { return I == R.I; }
71 bool operator!=(const ExprIterator& R) const { return I != R.I; }
72 bool operator>(const ExprIterator& R) const { return I > R.I; }
73 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
76 class ConstExprIterator {
77 const Stmt * const *I;
79 ConstExprIterator(const Stmt * const *i) : I(i) {}
80 ConstExprIterator() : I(0) {}
81 ConstExprIterator& operator++() { ++I; return *this; }
82 ConstExprIterator operator+(size_t i) const { return I+i; }
83 ConstExprIterator operator-(size_t i) const { return I-i; }
84 const Expr * operator[](size_t idx) const;
85 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
86 const Expr * operator*() const;
87 const Expr * operator->() const;
88 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
89 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
90 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
91 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
94 //===----------------------------------------------------------------------===//
95 // AST classes for statements.
96 //===----------------------------------------------------------------------===//
98 /// Stmt - This represents one statement.
104 #define STMT(CLASS, PARENT) CLASS##Class,
105 #define STMT_RANGE(BASE, FIRST, LAST) \
106 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
107 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
108 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
109 #define ABSTRACT_STMT(STMT)
110 #include "clang/AST/StmtNodes.inc"
113 // Make vanilla 'new' and 'delete' illegal for Stmts.
115 void* operator new(size_t bytes) throw() {
116 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
118 void operator delete(void* data) throw() {
119 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
122 class StmtBitfields {
125 /// \brief The statement class.
128 enum { NumStmtBits = 8 };
130 class CompoundStmtBitfields {
131 friend class CompoundStmt;
132 unsigned : NumStmtBits;
134 unsigned NumStmts : 32 - NumStmtBits;
137 class ExprBitfields {
139 friend class DeclRefExpr; // computeDependence
140 friend class InitListExpr; // ctor
141 friend class DesignatedInitExpr; // ctor
142 friend class BlockDeclRefExpr; // ctor
143 friend class ASTStmtReader; // deserialization
144 friend class CXXNewExpr; // ctor
145 friend class DependentScopeDeclRefExpr; // ctor
146 friend class CXXConstructExpr; // ctor
147 friend class CallExpr; // ctor
148 friend class OffsetOfExpr; // ctor
149 friend class ObjCMessageExpr; // ctor
150 friend class ObjCArrayLiteral; // ctor
151 friend class ObjCDictionaryLiteral; // ctor
152 friend class ShuffleVectorExpr; // ctor
153 friend class ParenListExpr; // ctor
154 friend class CXXUnresolvedConstructExpr; // ctor
155 friend class CXXDependentScopeMemberExpr; // ctor
156 friend class OverloadExpr; // ctor
157 friend class PseudoObjectExpr; // ctor
158 friend class AtomicExpr; // ctor
159 unsigned : NumStmtBits;
161 unsigned ValueKind : 2;
162 unsigned ObjectKind : 2;
163 unsigned TypeDependent : 1;
164 unsigned ValueDependent : 1;
165 unsigned InstantiationDependent : 1;
166 unsigned ContainsUnexpandedParameterPack : 1;
168 enum { NumExprBits = 16 };
170 class CharacterLiteralBitfields {
171 friend class CharacterLiteral;
172 unsigned : NumExprBits;
177 class FloatingLiteralBitfields {
178 friend class FloatingLiteral;
179 unsigned : NumExprBits;
181 unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
182 unsigned IsExact : 1;
185 class UnaryExprOrTypeTraitExprBitfields {
186 friend class UnaryExprOrTypeTraitExpr;
187 unsigned : NumExprBits;
190 unsigned IsType : 1; // true if operand is a type, false if an expression.
193 class DeclRefExprBitfields {
194 friend class DeclRefExpr;
195 friend class ASTStmtReader; // deserialization
196 unsigned : NumExprBits;
198 unsigned HasQualifier : 1;
199 unsigned HasTemplateKWAndArgsInfo : 1;
200 unsigned HasFoundDecl : 1;
201 unsigned HadMultipleCandidates : 1;
202 unsigned RefersToEnclosingLocal : 1;
205 class CastExprBitfields {
206 friend class CastExpr;
207 unsigned : NumExprBits;
210 unsigned BasePathSize : 32 - 6 - NumExprBits;
213 class CallExprBitfields {
214 friend class CallExpr;
215 unsigned : NumExprBits;
217 unsigned NumPreArgs : 1;
220 class ExprWithCleanupsBitfields {
221 friend class ExprWithCleanups;
222 friend class ASTStmtReader; // deserialization
224 unsigned : NumExprBits;
226 unsigned NumObjects : 32 - NumExprBits;
229 class PseudoObjectExprBitfields {
230 friend class PseudoObjectExpr;
231 friend class ASTStmtReader; // deserialization
233 unsigned : NumExprBits;
235 // These don't need to be particularly wide, because they're
236 // strictly limited by the forms of expressions we permit.
237 unsigned NumSubExprs : 8;
238 unsigned ResultIndex : 32 - 8 - NumExprBits;
241 class ObjCIndirectCopyRestoreExprBitfields {
242 friend class ObjCIndirectCopyRestoreExpr;
243 unsigned : NumExprBits;
245 unsigned ShouldCopy : 1;
248 class InitListExprBitfields {
249 friend class InitListExpr;
251 unsigned : NumExprBits;
253 /// Whether this initializer list originally had a GNU array-range
254 /// designator in it. This is a temporary marker used by CodeGen.
255 unsigned HadArrayRangeDesignator : 1;
257 /// Whether this initializer list initializes a std::initializer_list
259 unsigned InitializesStdInitializerList : 1;
262 class TypeTraitExprBitfields {
263 friend class TypeTraitExpr;
264 friend class ASTStmtReader;
265 friend class ASTStmtWriter;
267 unsigned : NumExprBits;
269 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
272 /// \brief If this expression is not value-dependent, this indicates whether
273 /// the trait evaluated true or false.
276 /// \brief The number of arguments to this type trait.
277 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
281 // FIXME: this is wasteful on 64-bit platforms.
284 StmtBitfields StmtBits;
285 CompoundStmtBitfields CompoundStmtBits;
286 ExprBitfields ExprBits;
287 CharacterLiteralBitfields CharacterLiteralBits;
288 FloatingLiteralBitfields FloatingLiteralBits;
289 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
290 DeclRefExprBitfields DeclRefExprBits;
291 CastExprBitfields CastExprBits;
292 CallExprBitfields CallExprBits;
293 ExprWithCleanupsBitfields ExprWithCleanupsBits;
294 PseudoObjectExprBitfields PseudoObjectExprBits;
295 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
296 InitListExprBitfields InitListExprBits;
297 TypeTraitExprBitfields TypeTraitExprBits;
300 friend class ASTStmtReader;
301 friend class ASTStmtWriter;
304 // Only allow allocation of Stmts using the allocator in ASTContext
305 // or by doing a placement new.
306 void* operator new(size_t bytes, ASTContext& C,
307 unsigned alignment = 8) throw();
309 void* operator new(size_t bytes, ASTContext* C,
310 unsigned alignment = 8) throw();
312 void* operator new(size_t bytes, void* mem) throw() {
316 void operator delete(void*, ASTContext&, unsigned) throw() { }
317 void operator delete(void*, ASTContext*, unsigned) throw() { }
318 void operator delete(void*, std::size_t) throw() { }
319 void operator delete(void*, void*) throw() { }
322 /// \brief A placeholder type used to construct an empty shell of a
323 /// type, that will be filled in later (e.g., by some
324 /// de-serialization).
325 struct EmptyShell { };
328 /// \brief Whether statistic collection is enabled.
329 static bool StatisticsEnabled;
332 /// \brief Construct an empty statement.
333 explicit Stmt(StmtClass SC, EmptyShell) {
334 StmtBits.sClass = SC;
335 if (StatisticsEnabled) Stmt::addStmtClass(SC);
340 StmtBits.sClass = SC;
341 if (StatisticsEnabled) Stmt::addStmtClass(SC);
344 StmtClass getStmtClass() const {
345 return static_cast<StmtClass>(StmtBits.sClass);
347 const char *getStmtClassName() const;
349 /// SourceLocation tokens are not useful in isolation - they are low level
350 /// value objects created/interpreted by SourceManager. We assume AST
351 /// clients will have a pointer to the respective SourceManager.
352 SourceRange getSourceRange() const LLVM_READONLY;
353 SourceLocation getLocStart() const LLVM_READONLY;
354 SourceLocation getLocEnd() const LLVM_READONLY;
356 // global temp stats (until we have a per-module visitor)
357 static void addStmtClass(const StmtClass s);
358 static void EnableStatistics();
359 static void PrintStats();
361 /// \brief Dumps the specified AST fragment and all subtrees to
363 LLVM_ATTRIBUTE_USED void dump() const;
364 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
365 void dump(raw_ostream &OS, SourceManager &SM) const;
367 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
368 /// back to its original source language syntax.
369 void dumpPretty(ASTContext &Context) const;
370 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
371 const PrintingPolicy &Policy,
372 unsigned Indentation = 0) const;
374 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
375 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
376 void viewAST() const;
378 /// Skip past any implicit AST nodes which might surround this
379 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
380 Stmt *IgnoreImplicit();
382 const Stmt *stripLabelLikeStatements() const;
383 Stmt *stripLabelLikeStatements() {
384 return const_cast<Stmt*>(
385 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
388 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
389 /// contain implicit control-flow in the order their subexpressions
390 /// are evaluated. This predicate returns true if this statement has
391 /// such implicit control-flow. Such statements are also specially handled
393 bool hasImplicitControlFlow() const;
395 /// Child Iterators: All subclasses must implement 'children'
396 /// to permit easy iteration over the substatements/subexpessions of an
397 /// AST node. This permits easy iteration over all nodes in the AST.
398 typedef StmtIterator child_iterator;
399 typedef ConstStmtIterator const_child_iterator;
401 typedef StmtRange child_range;
402 typedef ConstStmtRange const_child_range;
404 child_range children();
405 const_child_range children() const {
406 return const_cast<Stmt*>(this)->children();
409 child_iterator child_begin() { return children().first; }
410 child_iterator child_end() { return children().second; }
412 const_child_iterator child_begin() const { return children().first; }
413 const_child_iterator child_end() const { return children().second; }
415 /// \brief Produce a unique representation of the given statement.
417 /// \param ID once the profiling operation is complete, will contain
418 /// the unique representation of the given statement.
420 /// \param Context the AST context in which the statement resides
422 /// \param Canonical whether the profile should be based on the canonical
423 /// representation of this statement (e.g., where non-type template
424 /// parameters are identified by index/level rather than their
425 /// declaration pointers) or the exact representation of the statement as
426 /// written in the source.
427 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
428 bool Canonical) const;
431 /// DeclStmt - Adaptor class for mixing declarations with statements and
432 /// expressions. For example, CompoundStmt mixes statements, expressions
433 /// and declarations (variables, types). Another example is ForStmt, where
434 /// the first statement can be an expression or a declaration.
436 class DeclStmt : public Stmt {
438 SourceLocation StartLoc, EndLoc;
441 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
442 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
443 StartLoc(startLoc), EndLoc(endLoc) {}
445 /// \brief Build an empty declaration statement.
446 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
448 /// isSingleDecl - This method returns true if this DeclStmt refers
449 /// to a single Decl.
450 bool isSingleDecl() const {
451 return DG.isSingleDecl();
454 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
455 Decl *getSingleDecl() { return DG.getSingleDecl(); }
457 const DeclGroupRef getDeclGroup() const { return DG; }
458 DeclGroupRef getDeclGroup() { return DG; }
459 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
461 SourceLocation getStartLoc() const { return StartLoc; }
462 void setStartLoc(SourceLocation L) { StartLoc = L; }
463 SourceLocation getEndLoc() const { return EndLoc; }
464 void setEndLoc(SourceLocation L) { EndLoc = L; }
466 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
467 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
469 static bool classof(const Stmt *T) {
470 return T->getStmtClass() == DeclStmtClass;
473 // Iterators over subexpressions.
474 child_range children() {
475 return child_range(child_iterator(DG.begin(), DG.end()),
476 child_iterator(DG.end(), DG.end()));
479 typedef DeclGroupRef::iterator decl_iterator;
480 typedef DeclGroupRef::const_iterator const_decl_iterator;
482 decl_iterator decl_begin() { return DG.begin(); }
483 decl_iterator decl_end() { return DG.end(); }
484 const_decl_iterator decl_begin() const { return DG.begin(); }
485 const_decl_iterator decl_end() const { return DG.end(); }
487 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
488 reverse_decl_iterator decl_rbegin() {
489 return reverse_decl_iterator(decl_end());
491 reverse_decl_iterator decl_rend() {
492 return reverse_decl_iterator(decl_begin());
496 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
498 class NullStmt : public Stmt {
499 SourceLocation SemiLoc;
501 /// \brief True if the null statement was preceded by an empty macro, e.g:
506 bool HasLeadingEmptyMacro;
508 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
509 : Stmt(NullStmtClass), SemiLoc(L),
510 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
512 /// \brief Build an empty null statement.
513 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
514 HasLeadingEmptyMacro(false) { }
516 SourceLocation getSemiLoc() const { return SemiLoc; }
517 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
519 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
521 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
522 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
524 static bool classof(const Stmt *T) {
525 return T->getStmtClass() == NullStmtClass;
528 child_range children() { return child_range(); }
530 friend class ASTStmtReader;
531 friend class ASTStmtWriter;
534 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
536 class CompoundStmt : public Stmt {
538 SourceLocation LBracLoc, RBracLoc;
540 CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
541 SourceLocation LB, SourceLocation RB);
543 // \brief Build an empty compound statment with a location.
544 explicit CompoundStmt(SourceLocation Loc)
545 : Stmt(CompoundStmtClass), Body(0), LBracLoc(Loc), RBracLoc(Loc) {
546 CompoundStmtBits.NumStmts = 0;
549 // \brief Build an empty compound statement.
550 explicit CompoundStmt(EmptyShell Empty)
551 : Stmt(CompoundStmtClass, Empty), Body(0) {
552 CompoundStmtBits.NumStmts = 0;
555 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
557 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
558 unsigned size() const { return CompoundStmtBits.NumStmts; }
560 typedef Stmt** body_iterator;
561 body_iterator body_begin() { return Body; }
562 body_iterator body_end() { return Body + size(); }
563 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
565 void setLastStmt(Stmt *S) {
566 assert(!body_empty() && "setLastStmt");
570 typedef Stmt* const * const_body_iterator;
571 const_body_iterator body_begin() const { return Body; }
572 const_body_iterator body_end() const { return Body + size(); }
573 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
575 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
576 reverse_body_iterator body_rbegin() {
577 return reverse_body_iterator(body_end());
579 reverse_body_iterator body_rend() {
580 return reverse_body_iterator(body_begin());
583 typedef std::reverse_iterator<const_body_iterator>
584 const_reverse_body_iterator;
586 const_reverse_body_iterator body_rbegin() const {
587 return const_reverse_body_iterator(body_end());
590 const_reverse_body_iterator body_rend() const {
591 return const_reverse_body_iterator(body_begin());
594 SourceLocation getLocStart() const LLVM_READONLY { return LBracLoc; }
595 SourceLocation getLocEnd() const LLVM_READONLY { return RBracLoc; }
597 SourceLocation getLBracLoc() const { return LBracLoc; }
598 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
599 SourceLocation getRBracLoc() const { return RBracLoc; }
600 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
602 static bool classof(const Stmt *T) {
603 return T->getStmtClass() == CompoundStmtClass;
607 child_range children() {
608 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
611 const_child_range children() const {
612 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
616 // SwitchCase is the base class for CaseStmt and DefaultStmt,
617 class SwitchCase : public Stmt {
619 // A pointer to the following CaseStmt or DefaultStmt class,
620 // used by SwitchStmt.
621 SwitchCase *NextSwitchCase;
622 SourceLocation KeywordLoc;
623 SourceLocation ColonLoc;
625 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
626 : Stmt(SC), NextSwitchCase(0), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
628 SwitchCase(StmtClass SC, EmptyShell)
629 : Stmt(SC), NextSwitchCase(0) {}
632 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
634 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
636 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
638 SourceLocation getKeywordLoc() const { return KeywordLoc; }
639 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
640 SourceLocation getColonLoc() const { return ColonLoc; }
641 void setColonLoc(SourceLocation L) { ColonLoc = L; }
644 const Stmt *getSubStmt() const {
645 return const_cast<SwitchCase*>(this)->getSubStmt();
648 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
649 SourceLocation getLocEnd() const LLVM_READONLY;
651 static bool classof(const Stmt *T) {
652 return T->getStmtClass() == CaseStmtClass ||
653 T->getStmtClass() == DefaultStmtClass;
657 class CaseStmt : public SwitchCase {
658 enum { LHS, RHS, SUBSTMT, END_EXPR };
659 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
660 // GNU "case 1 ... 4" extension
661 SourceLocation EllipsisLoc;
663 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
664 SourceLocation ellipsisLoc, SourceLocation colonLoc)
665 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
666 SubExprs[SUBSTMT] = 0;
667 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
668 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
669 EllipsisLoc = ellipsisLoc;
672 /// \brief Build an empty switch case statement.
673 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { }
675 SourceLocation getCaseLoc() const { return KeywordLoc; }
676 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
677 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
678 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
679 SourceLocation getColonLoc() const { return ColonLoc; }
680 void setColonLoc(SourceLocation L) { ColonLoc = L; }
682 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
683 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
684 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
686 const Expr *getLHS() const {
687 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
689 const Expr *getRHS() const {
690 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
692 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
694 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
695 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
696 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
698 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
699 SourceLocation getLocEnd() const LLVM_READONLY {
700 // Handle deeply nested case statements with iteration instead of recursion.
701 const CaseStmt *CS = this;
702 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
705 return CS->getSubStmt()->getLocEnd();
708 static bool classof(const Stmt *T) {
709 return T->getStmtClass() == CaseStmtClass;
713 child_range children() {
714 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
718 class DefaultStmt : public SwitchCase {
721 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
722 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
724 /// \brief Build an empty default statement.
725 explicit DefaultStmt(EmptyShell Empty)
726 : SwitchCase(DefaultStmtClass, Empty) { }
728 Stmt *getSubStmt() { return SubStmt; }
729 const Stmt *getSubStmt() const { return SubStmt; }
730 void setSubStmt(Stmt *S) { SubStmt = S; }
732 SourceLocation getDefaultLoc() const { return KeywordLoc; }
733 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
734 SourceLocation getColonLoc() const { return ColonLoc; }
735 void setColonLoc(SourceLocation L) { ColonLoc = L; }
737 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
738 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
740 static bool classof(const Stmt *T) {
741 return T->getStmtClass() == DefaultStmtClass;
745 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
748 inline SourceLocation SwitchCase::getLocEnd() const {
749 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
750 return CS->getLocEnd();
751 return cast<DefaultStmt>(this)->getLocEnd();
754 /// LabelStmt - Represents a label, which has a substatement. For example:
757 class LabelStmt : public Stmt {
760 SourceLocation IdentLoc;
762 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
763 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
766 // \brief Build an empty label statement.
767 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
769 SourceLocation getIdentLoc() const { return IdentLoc; }
770 LabelDecl *getDecl() const { return TheDecl; }
771 void setDecl(LabelDecl *D) { TheDecl = D; }
772 const char *getName() const;
773 Stmt *getSubStmt() { return SubStmt; }
774 const Stmt *getSubStmt() const { return SubStmt; }
775 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
776 void setSubStmt(Stmt *SS) { SubStmt = SS; }
778 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
779 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
781 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
783 static bool classof(const Stmt *T) {
784 return T->getStmtClass() == LabelStmtClass;
789 /// \brief Represents an attribute applied to a statement.
791 /// Represents an attribute applied to a statement. For example:
792 /// [[omp::for(...)]] for (...) { ... }
794 class AttributedStmt : public Stmt {
796 SourceLocation AttrLoc;
798 const Attr *Attrs[1];
800 friend class ASTStmtReader;
802 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
803 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
804 NumAttrs(Attrs.size()) {
805 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*));
808 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
809 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
810 memset(Attrs, 0, NumAttrs * sizeof(Attr*));
814 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc,
815 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
816 // \brief Build an empty attributed statement.
817 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs);
819 SourceLocation getAttrLoc() const { return AttrLoc; }
820 ArrayRef<const Attr*> getAttrs() const {
821 return ArrayRef<const Attr*>(Attrs, NumAttrs);
823 Stmt *getSubStmt() { return SubStmt; }
824 const Stmt *getSubStmt() const { return SubStmt; }
826 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
827 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
829 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
831 static bool classof(const Stmt *T) {
832 return T->getStmtClass() == AttributedStmtClass;
837 /// IfStmt - This represents an if/then/else.
839 class IfStmt : public Stmt {
840 enum { VAR, COND, THEN, ELSE, END_EXPR };
841 Stmt* SubExprs[END_EXPR];
843 SourceLocation IfLoc;
844 SourceLocation ElseLoc;
847 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
848 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
850 /// \brief Build an empty if/then/else statement
851 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
853 /// \brief Retrieve the variable declared in this "if" statement, if any.
855 /// In the following example, "x" is the condition variable.
857 /// if (int x = foo()) {
858 /// printf("x is %d", x);
861 VarDecl *getConditionVariable() const;
862 void setConditionVariable(ASTContext &C, VarDecl *V);
864 /// If this IfStmt has a condition variable, return the faux DeclStmt
865 /// associated with the creation of that condition variable.
866 const DeclStmt *getConditionVariableDeclStmt() const {
867 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
870 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
871 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
872 const Stmt *getThen() const { return SubExprs[THEN]; }
873 void setThen(Stmt *S) { SubExprs[THEN] = S; }
874 const Stmt *getElse() const { return SubExprs[ELSE]; }
875 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
877 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
878 Stmt *getThen() { return SubExprs[THEN]; }
879 Stmt *getElse() { return SubExprs[ELSE]; }
881 SourceLocation getIfLoc() const { return IfLoc; }
882 void setIfLoc(SourceLocation L) { IfLoc = L; }
883 SourceLocation getElseLoc() const { return ElseLoc; }
884 void setElseLoc(SourceLocation L) { ElseLoc = L; }
886 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
887 SourceLocation getLocEnd() const LLVM_READONLY {
889 return SubExprs[ELSE]->getLocEnd();
891 return SubExprs[THEN]->getLocEnd();
894 // Iterators over subexpressions. The iterators will include iterating
895 // over the initialization expression referenced by the condition variable.
896 child_range children() {
897 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
900 static bool classof(const Stmt *T) {
901 return T->getStmtClass() == IfStmtClass;
905 /// SwitchStmt - This represents a 'switch' stmt.
907 class SwitchStmt : public Stmt {
908 enum { VAR, COND, BODY, END_EXPR };
909 Stmt* SubExprs[END_EXPR];
910 // This points to a linked list of case and default statements.
911 SwitchCase *FirstCase;
912 SourceLocation SwitchLoc;
914 /// If the SwitchStmt is a switch on an enum value, this records whether
915 /// all the enum values were covered by CaseStmts. This value is meant to
916 /// be a hint for possible clients.
917 unsigned AllEnumCasesCovered : 1;
920 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
922 /// \brief Build a empty switch statement.
923 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
925 /// \brief Retrieve the variable declared in this "switch" statement, if any.
927 /// In the following example, "x" is the condition variable.
929 /// switch (int x = foo()) {
934 VarDecl *getConditionVariable() const;
935 void setConditionVariable(ASTContext &C, VarDecl *V);
937 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
938 /// associated with the creation of that condition variable.
939 const DeclStmt *getConditionVariableDeclStmt() const {
940 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
943 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
944 const Stmt *getBody() const { return SubExprs[BODY]; }
945 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
947 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
948 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
949 Stmt *getBody() { return SubExprs[BODY]; }
950 void setBody(Stmt *S) { SubExprs[BODY] = S; }
951 SwitchCase *getSwitchCaseList() { return FirstCase; }
953 /// \brief Set the case list for this switch statement.
955 /// The caller is responsible for incrementing the retain counts on
956 /// all of the SwitchCase statements in this list.
957 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
959 SourceLocation getSwitchLoc() const { return SwitchLoc; }
960 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
962 void setBody(Stmt *S, SourceLocation SL) {
966 void addSwitchCase(SwitchCase *SC) {
967 assert(!SC->getNextSwitchCase()
968 && "case/default already added to a switch");
969 SC->setNextSwitchCase(FirstCase);
973 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
974 /// switch over an enum value then all cases have been explicitly covered.
975 void setAllEnumCasesCovered() {
976 AllEnumCasesCovered = 1;
979 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
980 /// have been explicitly covered.
981 bool isAllEnumCasesCovered() const {
982 return (bool) AllEnumCasesCovered;
985 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
986 SourceLocation getLocEnd() const LLVM_READONLY {
987 return SubExprs[BODY]->getLocEnd();
991 child_range children() {
992 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
995 static bool classof(const Stmt *T) {
996 return T->getStmtClass() == SwitchStmtClass;
1001 /// WhileStmt - This represents a 'while' stmt.
1003 class WhileStmt : public Stmt {
1004 enum { VAR, COND, BODY, END_EXPR };
1005 Stmt* SubExprs[END_EXPR];
1006 SourceLocation WhileLoc;
1008 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1011 /// \brief Build an empty while statement.
1012 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1014 /// \brief Retrieve the variable declared in this "while" statement, if any.
1016 /// In the following example, "x" is the condition variable.
1018 /// while (int x = random()) {
1022 VarDecl *getConditionVariable() const;
1023 void setConditionVariable(ASTContext &C, VarDecl *V);
1025 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1026 /// associated with the creation of that condition variable.
1027 const DeclStmt *getConditionVariableDeclStmt() const {
1028 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1031 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1032 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1033 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1034 Stmt *getBody() { return SubExprs[BODY]; }
1035 const Stmt *getBody() const { return SubExprs[BODY]; }
1036 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1038 SourceLocation getWhileLoc() const { return WhileLoc; }
1039 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1041 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1042 SourceLocation getLocEnd() const LLVM_READONLY {
1043 return SubExprs[BODY]->getLocEnd();
1046 static bool classof(const Stmt *T) {
1047 return T->getStmtClass() == WhileStmtClass;
1051 child_range children() {
1052 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1056 /// DoStmt - This represents a 'do/while' stmt.
1058 class DoStmt : public Stmt {
1059 enum { BODY, COND, END_EXPR };
1060 Stmt* SubExprs[END_EXPR];
1061 SourceLocation DoLoc;
1062 SourceLocation WhileLoc;
1063 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1066 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1068 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1069 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1070 SubExprs[BODY] = body;
1073 /// \brief Build an empty do-while statement.
1074 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1076 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1077 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1078 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1079 Stmt *getBody() { return SubExprs[BODY]; }
1080 const Stmt *getBody() const { return SubExprs[BODY]; }
1081 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1083 SourceLocation getDoLoc() const { return DoLoc; }
1084 void setDoLoc(SourceLocation L) { DoLoc = L; }
1085 SourceLocation getWhileLoc() const { return WhileLoc; }
1086 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1088 SourceLocation getRParenLoc() const { return RParenLoc; }
1089 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1091 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1092 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1094 static bool classof(const Stmt *T) {
1095 return T->getStmtClass() == DoStmtClass;
1099 child_range children() {
1100 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1105 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1106 /// the init/cond/inc parts of the ForStmt will be null if they were not
1107 /// specified in the source.
1109 class ForStmt : public Stmt {
1110 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1111 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1112 SourceLocation ForLoc;
1113 SourceLocation LParenLoc, RParenLoc;
1116 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1117 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1119 /// \brief Build an empty for statement.
1120 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1122 Stmt *getInit() { return SubExprs[INIT]; }
1124 /// \brief Retrieve the variable declared in this "for" statement, if any.
1126 /// In the following example, "y" is the condition variable.
1128 /// for (int x = random(); int y = mangle(x); ++x) {
1132 VarDecl *getConditionVariable() const;
1133 void setConditionVariable(ASTContext &C, VarDecl *V);
1135 /// If this ForStmt has a condition variable, return the faux DeclStmt
1136 /// associated with the creation of that condition variable.
1137 const DeclStmt *getConditionVariableDeclStmt() const {
1138 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1141 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1142 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1143 Stmt *getBody() { return SubExprs[BODY]; }
1145 const Stmt *getInit() const { return SubExprs[INIT]; }
1146 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1147 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1148 const Stmt *getBody() const { return SubExprs[BODY]; }
1150 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1151 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1152 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1153 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1155 SourceLocation getForLoc() const { return ForLoc; }
1156 void setForLoc(SourceLocation L) { ForLoc = L; }
1157 SourceLocation getLParenLoc() const { return LParenLoc; }
1158 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1159 SourceLocation getRParenLoc() const { return RParenLoc; }
1160 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1162 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1163 SourceLocation getLocEnd() const LLVM_READONLY {
1164 return SubExprs[BODY]->getLocEnd();
1167 static bool classof(const Stmt *T) {
1168 return T->getStmtClass() == ForStmtClass;
1172 child_range children() {
1173 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1177 /// GotoStmt - This represents a direct goto.
1179 class GotoStmt : public Stmt {
1181 SourceLocation GotoLoc;
1182 SourceLocation LabelLoc;
1184 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1185 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1187 /// \brief Build an empty goto statement.
1188 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1190 LabelDecl *getLabel() const { return Label; }
1191 void setLabel(LabelDecl *D) { Label = D; }
1193 SourceLocation getGotoLoc() const { return GotoLoc; }
1194 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1195 SourceLocation getLabelLoc() const { return LabelLoc; }
1196 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1198 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1199 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1201 static bool classof(const Stmt *T) {
1202 return T->getStmtClass() == GotoStmtClass;
1206 child_range children() { return child_range(); }
1209 /// IndirectGotoStmt - This represents an indirect goto.
1211 class IndirectGotoStmt : public Stmt {
1212 SourceLocation GotoLoc;
1213 SourceLocation StarLoc;
1216 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1218 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1219 Target((Stmt*)target) {}
1221 /// \brief Build an empty indirect goto statement.
1222 explicit IndirectGotoStmt(EmptyShell Empty)
1223 : Stmt(IndirectGotoStmtClass, Empty) { }
1225 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1226 SourceLocation getGotoLoc() const { return GotoLoc; }
1227 void setStarLoc(SourceLocation L) { StarLoc = L; }
1228 SourceLocation getStarLoc() const { return StarLoc; }
1230 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1231 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1232 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1234 /// getConstantTarget - Returns the fixed target of this indirect
1235 /// goto, if one exists.
1236 LabelDecl *getConstantTarget();
1237 const LabelDecl *getConstantTarget() const {
1238 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1241 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1242 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1244 static bool classof(const Stmt *T) {
1245 return T->getStmtClass() == IndirectGotoStmtClass;
1249 child_range children() { return child_range(&Target, &Target+1); }
1253 /// ContinueStmt - This represents a continue.
1255 class ContinueStmt : public Stmt {
1256 SourceLocation ContinueLoc;
1258 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1260 /// \brief Build an empty continue statement.
1261 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1263 SourceLocation getContinueLoc() const { return ContinueLoc; }
1264 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1266 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1267 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1269 static bool classof(const Stmt *T) {
1270 return T->getStmtClass() == ContinueStmtClass;
1274 child_range children() { return child_range(); }
1277 /// BreakStmt - This represents a break.
1279 class BreakStmt : public Stmt {
1280 SourceLocation BreakLoc;
1282 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1284 /// \brief Build an empty break statement.
1285 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1287 SourceLocation getBreakLoc() const { return BreakLoc; }
1288 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1290 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1291 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1293 static bool classof(const Stmt *T) {
1294 return T->getStmtClass() == BreakStmtClass;
1298 child_range children() { return child_range(); }
1302 /// ReturnStmt - This represents a return, optionally of an expression:
1306 /// Note that GCC allows return with no argument in a function declared to
1307 /// return a value, and it allows returning a value in functions declared to
1308 /// return void. We explicitly model this in the AST, which means you can't
1309 /// depend on the return type of the function and the presence of an argument.
1311 class ReturnStmt : public Stmt {
1313 SourceLocation RetLoc;
1314 const VarDecl *NRVOCandidate;
1317 ReturnStmt(SourceLocation RL)
1318 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1320 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1321 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1322 NRVOCandidate(NRVOCandidate) {}
1324 /// \brief Build an empty return expression.
1325 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1327 const Expr *getRetValue() const;
1328 Expr *getRetValue();
1329 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1331 SourceLocation getReturnLoc() const { return RetLoc; }
1332 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1334 /// \brief Retrieve the variable that might be used for the named return
1335 /// value optimization.
1337 /// The optimization itself can only be performed if the variable is
1338 /// also marked as an NRVO object.
1339 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1340 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1342 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1343 SourceLocation getLocEnd() const LLVM_READONLY {
1344 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1347 static bool classof(const Stmt *T) {
1348 return T->getStmtClass() == ReturnStmtClass;
1352 child_range children() {
1353 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1354 return child_range();
1358 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1360 class AsmStmt : public Stmt {
1362 SourceLocation AsmLoc;
1363 /// \brief True if the assembly statement does not have any input or output
1367 /// \brief If true, treat this inline assembly as having side effects.
1368 /// This assembly statement should not be optimized, deleted or moved.
1371 unsigned NumOutputs;
1373 unsigned NumClobbers;
1375 IdentifierInfo **Names;
1378 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1379 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1380 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1381 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1384 /// \brief Build an empty inline-assembly statement.
1385 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1386 Stmt(SC, Empty), Names(0), Exprs(0) { }
1388 SourceLocation getAsmLoc() const { return AsmLoc; }
1389 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1391 bool isSimple() const { return IsSimple; }
1392 void setSimple(bool V) { IsSimple = V; }
1394 bool isVolatile() const { return IsVolatile; }
1395 void setVolatile(bool V) { IsVolatile = V; }
1397 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1398 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1400 //===--- Asm String Analysis ---===//
1402 /// Assemble final IR asm string.
1403 std::string generateAsmString(ASTContext &C) const;
1405 //===--- Output operands ---===//
1407 unsigned getNumOutputs() const { return NumOutputs; }
1409 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1413 StringRef getOutputName(unsigned i) const {
1414 if (IdentifierInfo *II = getOutputIdentifier(i))
1415 return II->getName();
1420 /// getOutputConstraint - Return the constraint string for the specified
1421 /// output operand. All output constraints are known to be non-empty (either
1423 StringRef getOutputConstraint(unsigned i) const;
1425 /// isOutputPlusConstraint - Return true if the specified output constraint
1426 /// is a "+" constraint (which is both an input and an output) or false if it
1427 /// is an "=" constraint (just an output).
1428 bool isOutputPlusConstraint(unsigned i) const {
1429 return getOutputConstraint(i)[0] == '+';
1432 const Expr *getOutputExpr(unsigned i) const;
1434 /// getNumPlusOperands - Return the number of output operands that have a "+"
1436 unsigned getNumPlusOperands() const;
1438 //===--- Input operands ---===//
1440 unsigned getNumInputs() const { return NumInputs; }
1442 IdentifierInfo *getInputIdentifier(unsigned i) const {
1443 return Names[i + NumOutputs];
1446 StringRef getInputName(unsigned i) const {
1447 if (IdentifierInfo *II = getInputIdentifier(i))
1448 return II->getName();
1453 /// getInputConstraint - Return the specified input constraint. Unlike output
1454 /// constraints, these can be empty.
1455 StringRef getInputConstraint(unsigned i) const;
1457 const Expr *getInputExpr(unsigned i) const;
1459 //===--- Other ---===//
1461 unsigned getNumClobbers() const { return NumClobbers; }
1462 StringRef getClobber(unsigned i) const;
1464 static bool classof(const Stmt *T) {
1465 return T->getStmtClass() == GCCAsmStmtClass ||
1466 T->getStmtClass() == MSAsmStmtClass;
1469 // Input expr iterators.
1471 typedef ExprIterator inputs_iterator;
1472 typedef ConstExprIterator const_inputs_iterator;
1474 inputs_iterator begin_inputs() {
1475 return &Exprs[0] + NumOutputs;
1478 inputs_iterator end_inputs() {
1479 return &Exprs[0] + NumOutputs + NumInputs;
1482 const_inputs_iterator begin_inputs() const {
1483 return &Exprs[0] + NumOutputs;
1486 const_inputs_iterator end_inputs() const {
1487 return &Exprs[0] + NumOutputs + NumInputs;
1490 // Output expr iterators.
1492 typedef ExprIterator outputs_iterator;
1493 typedef ConstExprIterator const_outputs_iterator;
1495 outputs_iterator begin_outputs() {
1498 outputs_iterator end_outputs() {
1499 return &Exprs[0] + NumOutputs;
1502 const_outputs_iterator begin_outputs() const {
1505 const_outputs_iterator end_outputs() const {
1506 return &Exprs[0] + NumOutputs;
1509 child_range children() {
1510 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1514 /// This represents a GCC inline-assembly statement extension.
1516 class GCCAsmStmt : public AsmStmt {
1517 SourceLocation RParenLoc;
1518 StringLiteral *AsmStr;
1520 // FIXME: If we wanted to, we could allocate all of these in one big array.
1521 StringLiteral **Constraints;
1522 StringLiteral **Clobbers;
1525 GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
1526 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1527 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1528 StringLiteral *asmstr, unsigned numclobbers,
1529 StringLiteral **clobbers, SourceLocation rparenloc);
1531 /// \brief Build an empty inline-assembly statement.
1532 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1533 Constraints(0), Clobbers(0) { }
1535 SourceLocation getRParenLoc() const { return RParenLoc; }
1536 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1538 //===--- Asm String Analysis ---===//
1540 const StringLiteral *getAsmString() const { return AsmStr; }
1541 StringLiteral *getAsmString() { return AsmStr; }
1542 void setAsmString(StringLiteral *E) { AsmStr = E; }
1544 /// AsmStringPiece - this is part of a decomposed asm string specification
1545 /// (for use with the AnalyzeAsmString function below). An asm string is
1546 /// considered to be a concatenation of these parts.
1547 class AsmStringPiece {
1550 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1551 Operand // Operand reference, with optional modifier %c4.
1558 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1559 AsmStringPiece(unsigned OpNo, char Modifier)
1560 : MyKind(Operand), Str(), OperandNo(OpNo) {
1564 bool isString() const { return MyKind == String; }
1565 bool isOperand() const { return MyKind == Operand; }
1567 const std::string &getString() const {
1572 unsigned getOperandNo() const {
1573 assert(isOperand());
1577 /// getModifier - Get the modifier for this operand, if present. This
1578 /// returns '\0' if there was no modifier.
1579 char getModifier() const {
1580 assert(isOperand());
1585 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1586 /// it into pieces. If the asm string is erroneous, emit errors and return
1587 /// true, otherwise return false. This handles canonicalization and
1588 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1589 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1590 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1591 ASTContext &C, unsigned &DiagOffs) const;
1593 /// Assemble final IR asm string.
1594 std::string generateAsmString(ASTContext &C) const;
1596 //===--- Output operands ---===//
1598 StringRef getOutputConstraint(unsigned i) const;
1600 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1601 return Constraints[i];
1603 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1604 return Constraints[i];
1607 Expr *getOutputExpr(unsigned i);
1609 const Expr *getOutputExpr(unsigned i) const {
1610 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1613 //===--- Input operands ---===//
1615 StringRef getInputConstraint(unsigned i) const;
1617 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1618 return Constraints[i + NumOutputs];
1620 StringLiteral *getInputConstraintLiteral(unsigned i) {
1621 return Constraints[i + NumOutputs];
1624 Expr *getInputExpr(unsigned i);
1625 void setInputExpr(unsigned i, Expr *E);
1627 const Expr *getInputExpr(unsigned i) const {
1628 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1631 void setOutputsAndInputsAndClobbers(ASTContext &C,
1632 IdentifierInfo **Names,
1633 StringLiteral **Constraints,
1635 unsigned NumOutputs,
1637 StringLiteral **Clobbers,
1638 unsigned NumClobbers);
1640 //===--- Other ---===//
1642 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1643 /// translate this into a numeric value needed to reference the same operand.
1644 /// This returns -1 if the operand name is invalid.
1645 int getNamedOperand(StringRef SymbolicName) const;
1647 StringRef getClobber(unsigned i) const;
1648 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1649 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1653 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1654 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1656 static bool classof(const Stmt *T) {
1657 return T->getStmtClass() == GCCAsmStmtClass;
1661 /// This represents a Microsoft inline-assembly statement extension.
1663 class MSAsmStmt : public AsmStmt {
1664 SourceLocation AsmLoc, LBraceLoc, EndLoc;
1667 unsigned NumAsmToks;
1670 StringRef *Constraints;
1671 StringRef *Clobbers;
1674 MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1675 bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1676 unsigned numoutputs, unsigned numinputs,
1677 ArrayRef<IdentifierInfo*> names, ArrayRef<StringRef> constraints,
1678 ArrayRef<Expr*> exprs, StringRef asmstr,
1679 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1681 /// \brief Build an empty MS-style inline-assembly statement.
1682 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1683 NumAsmToks(0), AsmToks(0), Constraints(0), Clobbers(0) { }
1685 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1686 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1687 SourceLocation getEndLoc() const { return EndLoc; }
1688 void setEndLoc(SourceLocation L) { EndLoc = L; }
1690 bool hasBraces() const { return LBraceLoc.isValid(); }
1692 unsigned getNumAsmToks() { return NumAsmToks; }
1693 Token *getAsmToks() { return AsmToks; }
1695 //===--- Asm String Analysis ---===//
1697 const std::string *getAsmString() const { return &AsmStr; }
1698 std::string *getAsmString() { return &AsmStr; }
1699 void setAsmString(StringRef &E) { AsmStr = E.str(); }
1701 /// Assemble final IR asm string.
1702 std::string generateAsmString(ASTContext &C) const;
1704 //===--- Output operands ---===//
1706 StringRef getOutputConstraint(unsigned i) const {
1707 return Constraints[i];
1710 Expr *getOutputExpr(unsigned i);
1712 const Expr *getOutputExpr(unsigned i) const {
1713 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1716 //===--- Input operands ---===//
1718 StringRef getInputConstraint(unsigned i) const {
1719 return Constraints[i + NumOutputs];
1722 Expr *getInputExpr(unsigned i);
1723 void setInputExpr(unsigned i, Expr *E);
1725 const Expr *getInputExpr(unsigned i) const {
1726 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1729 //===--- Other ---===//
1731 StringRef getClobber(unsigned i) const { return Clobbers[i]; }
1733 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1734 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1736 static bool classof(const Stmt *T) {
1737 return T->getStmtClass() == MSAsmStmtClass;
1740 child_range children() {
1741 return child_range(&Exprs[0], &Exprs[0]);
1745 class SEHExceptStmt : public Stmt {
1749 enum { FILTER_EXPR, BLOCK };
1751 SEHExceptStmt(SourceLocation Loc,
1755 friend class ASTReader;
1756 friend class ASTStmtReader;
1757 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1760 static SEHExceptStmt* Create(ASTContext &C,
1761 SourceLocation ExceptLoc,
1765 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1766 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1768 SourceLocation getExceptLoc() const { return Loc; }
1769 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1771 Expr *getFilterExpr() const {
1772 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1775 CompoundStmt *getBlock() const {
1776 return cast<CompoundStmt>(Children[BLOCK]);
1779 child_range children() {
1780 return child_range(Children,Children+2);
1783 static bool classof(const Stmt *T) {
1784 return T->getStmtClass() == SEHExceptStmtClass;
1789 class SEHFinallyStmt : public Stmt {
1793 SEHFinallyStmt(SourceLocation Loc,
1796 friend class ASTReader;
1797 friend class ASTStmtReader;
1798 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1801 static SEHFinallyStmt* Create(ASTContext &C,
1802 SourceLocation FinallyLoc,
1805 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1806 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1808 SourceLocation getFinallyLoc() const { return Loc; }
1809 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1811 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1813 child_range children() {
1814 return child_range(&Block,&Block+1);
1817 static bool classof(const Stmt *T) {
1818 return T->getStmtClass() == SEHFinallyStmtClass;
1823 class SEHTryStmt : public Stmt {
1825 SourceLocation TryLoc;
1828 enum { TRY = 0, HANDLER = 1 };
1830 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1831 SourceLocation TryLoc,
1835 friend class ASTReader;
1836 friend class ASTStmtReader;
1837 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1840 static SEHTryStmt* Create(ASTContext &C,
1842 SourceLocation TryLoc,
1846 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1847 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1849 SourceLocation getTryLoc() const { return TryLoc; }
1850 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1852 bool getIsCXXTry() const { return IsCXXTry; }
1854 CompoundStmt* getTryBlock() const {
1855 return cast<CompoundStmt>(Children[TRY]);
1858 Stmt *getHandler() const { return Children[HANDLER]; }
1860 /// Returns 0 if not defined
1861 SEHExceptStmt *getExceptHandler() const;
1862 SEHFinallyStmt *getFinallyHandler() const;
1864 child_range children() {
1865 return child_range(Children,Children+2);
1868 static bool classof(const Stmt *T) {
1869 return T->getStmtClass() == SEHTryStmtClass;
1873 } // end namespace clang