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/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/ASTContext.h"
23 #include "clang/AST/Attr.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/raw_ostream.h"
30 class FoldingSetNodeID;
44 //===--------------------------------------------------------------------===//
45 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
46 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
47 // references to children (to be compatible with StmtIterator).
48 //===--------------------------------------------------------------------===//
56 ExprIterator(Stmt** i) : I(i) {}
57 ExprIterator() : I(0) {}
58 ExprIterator& operator++() { ++I; return *this; }
59 ExprIterator operator-(size_t i) { return I-i; }
60 ExprIterator operator+(size_t i) { return I+i; }
61 Expr* operator[](size_t idx);
62 // FIXME: Verify that this will correctly return a signed distance.
63 signed operator-(const ExprIterator& R) const { return I - R.I; }
64 Expr* operator*() const;
65 Expr* operator->() const;
66 bool operator==(const ExprIterator& R) const { return I == R.I; }
67 bool operator!=(const ExprIterator& R) const { return I != R.I; }
68 bool operator>(const ExprIterator& R) const { return I > R.I; }
69 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
72 class ConstExprIterator {
73 const Stmt * const *I;
75 ConstExprIterator(const Stmt * const *i) : I(i) {}
76 ConstExprIterator() : I(0) {}
77 ConstExprIterator& operator++() { ++I; return *this; }
78 ConstExprIterator operator+(size_t i) const { return I+i; }
79 ConstExprIterator operator-(size_t i) const { return I-i; }
80 const Expr * operator[](size_t idx) const;
81 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
82 const Expr * operator*() const;
83 const Expr * operator->() const;
84 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
85 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
86 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
87 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
90 //===----------------------------------------------------------------------===//
91 // AST classes for statements.
92 //===----------------------------------------------------------------------===//
94 /// Stmt - This represents one statement.
100 #define STMT(CLASS, PARENT) CLASS##Class,
101 #define STMT_RANGE(BASE, FIRST, LAST) \
102 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
103 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
104 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
105 #define ABSTRACT_STMT(STMT)
106 #include "clang/AST/StmtNodes.inc"
109 // Make vanilla 'new' and 'delete' illegal for Stmts.
111 void* operator new(size_t bytes) throw() {
112 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
114 void operator delete(void* data) throw() {
115 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
118 class StmtBitfields {
121 /// \brief The statement class.
124 enum { NumStmtBits = 8 };
126 class CompoundStmtBitfields {
127 friend class CompoundStmt;
128 unsigned : NumStmtBits;
130 unsigned NumStmts : 32 - NumStmtBits;
133 class ExprBitfields {
135 friend class DeclRefExpr; // computeDependence
136 friend class InitListExpr; // ctor
137 friend class DesignatedInitExpr; // ctor
138 friend class BlockDeclRefExpr; // ctor
139 friend class ASTStmtReader; // deserialization
140 friend class CXXNewExpr; // ctor
141 friend class DependentScopeDeclRefExpr; // ctor
142 friend class CXXConstructExpr; // ctor
143 friend class CallExpr; // ctor
144 friend class OffsetOfExpr; // ctor
145 friend class ObjCMessageExpr; // ctor
146 friend class ObjCArrayLiteral; // ctor
147 friend class ObjCDictionaryLiteral; // ctor
148 friend class ShuffleVectorExpr; // ctor
149 friend class ParenListExpr; // ctor
150 friend class CXXUnresolvedConstructExpr; // ctor
151 friend class CXXDependentScopeMemberExpr; // ctor
152 friend class OverloadExpr; // ctor
153 friend class PseudoObjectExpr; // ctor
154 friend class AtomicExpr; // ctor
155 unsigned : NumStmtBits;
157 unsigned ValueKind : 2;
158 unsigned ObjectKind : 2;
159 unsigned TypeDependent : 1;
160 unsigned ValueDependent : 1;
161 unsigned InstantiationDependent : 1;
162 unsigned ContainsUnexpandedParameterPack : 1;
164 enum { NumExprBits = 16 };
166 class CharacterLiteralBitfields {
167 friend class CharacterLiteral;
168 unsigned : NumExprBits;
173 class FloatingLiteralBitfields {
174 friend class FloatingLiteral;
175 unsigned : NumExprBits;
177 unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
178 unsigned IsExact : 1;
181 class UnaryExprOrTypeTraitExprBitfields {
182 friend class UnaryExprOrTypeTraitExpr;
183 unsigned : NumExprBits;
186 unsigned IsType : 1; // true if operand is a type, false if an expression.
189 class DeclRefExprBitfields {
190 friend class DeclRefExpr;
191 friend class ASTStmtReader; // deserialization
192 unsigned : NumExprBits;
194 unsigned HasQualifier : 1;
195 unsigned HasTemplateKWAndArgsInfo : 1;
196 unsigned HasFoundDecl : 1;
197 unsigned HadMultipleCandidates : 1;
198 unsigned RefersToEnclosingLocal : 1;
201 class CastExprBitfields {
202 friend class CastExpr;
203 unsigned : NumExprBits;
206 unsigned BasePathSize : 32 - 6 - NumExprBits;
209 class CallExprBitfields {
210 friend class CallExpr;
211 unsigned : NumExprBits;
213 unsigned NumPreArgs : 1;
216 class ExprWithCleanupsBitfields {
217 friend class ExprWithCleanups;
218 friend class ASTStmtReader; // deserialization
220 unsigned : NumExprBits;
222 unsigned NumObjects : 32 - NumExprBits;
225 class PseudoObjectExprBitfields {
226 friend class PseudoObjectExpr;
227 friend class ASTStmtReader; // deserialization
229 unsigned : NumExprBits;
231 // These don't need to be particularly wide, because they're
232 // strictly limited by the forms of expressions we permit.
233 unsigned NumSubExprs : 8;
234 unsigned ResultIndex : 32 - 8 - NumExprBits;
237 class ObjCIndirectCopyRestoreExprBitfields {
238 friend class ObjCIndirectCopyRestoreExpr;
239 unsigned : NumExprBits;
241 unsigned ShouldCopy : 1;
244 class InitListExprBitfields {
245 friend class InitListExpr;
247 unsigned : NumExprBits;
249 /// Whether this initializer list originally had a GNU array-range
250 /// designator in it. This is a temporary marker used by CodeGen.
251 unsigned HadArrayRangeDesignator : 1;
253 /// Whether this initializer list initializes a std::initializer_list
255 unsigned InitializesStdInitializerList : 1;
258 class TypeTraitExprBitfields {
259 friend class TypeTraitExpr;
260 friend class ASTStmtReader;
261 friend class ASTStmtWriter;
263 unsigned : NumExprBits;
265 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
268 /// \brief If this expression is not value-dependent, this indicates whether
269 /// the trait evaluated true or false.
272 /// \brief The number of arguments to this type trait.
273 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
277 // FIXME: this is wasteful on 64-bit platforms.
280 StmtBitfields StmtBits;
281 CompoundStmtBitfields CompoundStmtBits;
282 ExprBitfields ExprBits;
283 CharacterLiteralBitfields CharacterLiteralBits;
284 FloatingLiteralBitfields FloatingLiteralBits;
285 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
286 DeclRefExprBitfields DeclRefExprBits;
287 CastExprBitfields CastExprBits;
288 CallExprBitfields CallExprBits;
289 ExprWithCleanupsBitfields ExprWithCleanupsBits;
290 PseudoObjectExprBitfields PseudoObjectExprBits;
291 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
292 InitListExprBitfields InitListExprBits;
293 TypeTraitExprBitfields TypeTraitExprBits;
296 friend class ASTStmtReader;
297 friend class ASTStmtWriter;
300 // Only allow allocation of Stmts using the allocator in ASTContext
301 // or by doing a placement new.
302 void* operator new(size_t bytes, ASTContext& C,
303 unsigned alignment = 8) throw() {
304 return ::operator new(bytes, C, alignment);
307 void* operator new(size_t bytes, ASTContext* C,
308 unsigned alignment = 8) throw() {
309 return ::operator new(bytes, *C, alignment);
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 /// dump - This does a local dump of the specified AST fragment. It dumps the
362 /// specified node and a few nodes underneath it, but not the whole subtree.
363 /// This is useful in a debugger.
364 LLVM_ATTRIBUTE_USED void dump() const;
365 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
366 void dump(raw_ostream &OS, SourceManager &SM) const;
368 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
369 void dumpAll() const;
370 void dumpAll(SourceManager &SM) const;
372 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
373 /// back to its original source language syntax.
374 void dumpPretty(ASTContext& Context) const;
375 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
376 const PrintingPolicy &Policy,
377 unsigned Indentation = 0) const {
378 printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
380 void printPretty(raw_ostream &OS, ASTContext &Context,
381 PrinterHelper *Helper,
382 const PrintingPolicy &Policy,
383 unsigned Indentation = 0) const;
385 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
386 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
387 void viewAST() const;
389 /// Skip past any implicit AST nodes which might surround this
390 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
391 Stmt *IgnoreImplicit();
393 const Stmt *stripLabelLikeStatements() const;
394 Stmt *stripLabelLikeStatements() {
395 return const_cast<Stmt*>(
396 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
399 // Implement isa<T> support.
400 static bool classof(const Stmt *) { return true; }
402 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
403 /// contain implicit control-flow in the order their subexpressions
404 /// are evaluated. This predicate returns true if this statement has
405 /// such implicit control-flow. Such statements are also specially handled
407 bool hasImplicitControlFlow() const;
409 /// Child Iterators: All subclasses must implement 'children'
410 /// to permit easy iteration over the substatements/subexpessions of an
411 /// AST node. This permits easy iteration over all nodes in the AST.
412 typedef StmtIterator child_iterator;
413 typedef ConstStmtIterator const_child_iterator;
415 typedef StmtRange child_range;
416 typedef ConstStmtRange const_child_range;
418 child_range children();
419 const_child_range children() const {
420 return const_cast<Stmt*>(this)->children();
423 child_iterator child_begin() { return children().first; }
424 child_iterator child_end() { return children().second; }
426 const_child_iterator child_begin() const { return children().first; }
427 const_child_iterator child_end() const { return children().second; }
429 /// \brief Produce a unique representation of the given statement.
431 /// \brief ID once the profiling operation is complete, will contain
432 /// the unique representation of the given statement.
434 /// \brief Context the AST context in which the statement resides
436 /// \brief Canonical whether the profile should be based on the canonical
437 /// representation of this statement (e.g., where non-type template
438 /// parameters are identified by index/level rather than their
439 /// declaration pointers) or the exact representation of the statement as
440 /// written in the source.
441 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
442 bool Canonical) const;
445 /// DeclStmt - Adaptor class for mixing declarations with statements and
446 /// expressions. For example, CompoundStmt mixes statements, expressions
447 /// and declarations (variables, types). Another example is ForStmt, where
448 /// the first statement can be an expression or a declaration.
450 class DeclStmt : public Stmt {
452 SourceLocation StartLoc, EndLoc;
455 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
456 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
457 StartLoc(startLoc), EndLoc(endLoc) {}
459 /// \brief Build an empty declaration statement.
460 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
462 /// isSingleDecl - This method returns true if this DeclStmt refers
463 /// to a single Decl.
464 bool isSingleDecl() const {
465 return DG.isSingleDecl();
468 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
469 Decl *getSingleDecl() { return DG.getSingleDecl(); }
471 const DeclGroupRef getDeclGroup() const { return DG; }
472 DeclGroupRef getDeclGroup() { return DG; }
473 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
475 SourceLocation getStartLoc() const { return StartLoc; }
476 void setStartLoc(SourceLocation L) { StartLoc = L; }
477 SourceLocation getEndLoc() const { return EndLoc; }
478 void setEndLoc(SourceLocation L) { EndLoc = L; }
480 SourceRange getSourceRange() const LLVM_READONLY {
481 return SourceRange(StartLoc, EndLoc);
484 static bool classof(const Stmt *T) {
485 return T->getStmtClass() == DeclStmtClass;
487 static bool classof(const DeclStmt *) { return true; }
489 // Iterators over subexpressions.
490 child_range children() {
491 return child_range(child_iterator(DG.begin(), DG.end()),
492 child_iterator(DG.end(), DG.end()));
495 typedef DeclGroupRef::iterator decl_iterator;
496 typedef DeclGroupRef::const_iterator const_decl_iterator;
498 decl_iterator decl_begin() { return DG.begin(); }
499 decl_iterator decl_end() { return DG.end(); }
500 const_decl_iterator decl_begin() const { return DG.begin(); }
501 const_decl_iterator decl_end() const { return DG.end(); }
504 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
506 class NullStmt : public Stmt {
507 SourceLocation SemiLoc;
509 /// \brief True if the null statement was preceded by an empty macro, e.g:
514 bool HasLeadingEmptyMacro;
516 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
517 : Stmt(NullStmtClass), SemiLoc(L),
518 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
520 /// \brief Build an empty null statement.
521 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
522 HasLeadingEmptyMacro(false) { }
524 SourceLocation getSemiLoc() const { return SemiLoc; }
525 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
527 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
529 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
531 static bool classof(const Stmt *T) {
532 return T->getStmtClass() == NullStmtClass;
534 static bool classof(const NullStmt *) { return true; }
536 child_range children() { return child_range(); }
538 friend class ASTStmtReader;
539 friend class ASTStmtWriter;
542 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
544 class CompoundStmt : public Stmt {
546 SourceLocation LBracLoc, RBracLoc;
548 CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
549 SourceLocation LB, SourceLocation RB)
550 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
551 CompoundStmtBits.NumStmts = NumStmts;
552 assert(CompoundStmtBits.NumStmts == NumStmts &&
553 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
560 Body = new (C) Stmt*[NumStmts];
561 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
564 // \brief Build an empty compound statement.
565 explicit CompoundStmt(EmptyShell Empty)
566 : Stmt(CompoundStmtClass, Empty), Body(0) {
567 CompoundStmtBits.NumStmts = 0;
570 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
572 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
573 unsigned size() const { return CompoundStmtBits.NumStmts; }
575 typedef Stmt** body_iterator;
576 body_iterator body_begin() { return Body; }
577 body_iterator body_end() { return Body + size(); }
578 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
580 void setLastStmt(Stmt *S) {
581 assert(!body_empty() && "setLastStmt");
585 typedef Stmt* const * const_body_iterator;
586 const_body_iterator body_begin() const { return Body; }
587 const_body_iterator body_end() const { return Body + size(); }
588 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
590 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
591 reverse_body_iterator body_rbegin() {
592 return reverse_body_iterator(body_end());
594 reverse_body_iterator body_rend() {
595 return reverse_body_iterator(body_begin());
598 typedef std::reverse_iterator<const_body_iterator>
599 const_reverse_body_iterator;
601 const_reverse_body_iterator body_rbegin() const {
602 return const_reverse_body_iterator(body_end());
605 const_reverse_body_iterator body_rend() const {
606 return const_reverse_body_iterator(body_begin());
609 SourceRange getSourceRange() const LLVM_READONLY {
610 return SourceRange(LBracLoc, RBracLoc);
613 SourceLocation getLBracLoc() const { return LBracLoc; }
614 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
615 SourceLocation getRBracLoc() const { return RBracLoc; }
616 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
618 static bool classof(const Stmt *T) {
619 return T->getStmtClass() == CompoundStmtClass;
621 static bool classof(const CompoundStmt *) { return true; }
624 child_range children() {
625 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
628 const_child_range children() const {
629 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
633 // SwitchCase is the base class for CaseStmt and DefaultStmt,
634 class SwitchCase : public Stmt {
636 // A pointer to the following CaseStmt or DefaultStmt class,
637 // used by SwitchStmt.
638 SwitchCase *NextSwitchCase;
640 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
643 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
645 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
647 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
650 const Stmt *getSubStmt() const {
651 return const_cast<SwitchCase*>(this)->getSubStmt();
654 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
656 static bool classof(const Stmt *T) {
657 return T->getStmtClass() == CaseStmtClass ||
658 T->getStmtClass() == DefaultStmtClass;
660 static bool classof(const SwitchCase *) { return true; }
663 class CaseStmt : public SwitchCase {
664 enum { LHS, RHS, SUBSTMT, END_EXPR };
665 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
666 // GNU "case 1 ... 4" extension
667 SourceLocation CaseLoc;
668 SourceLocation EllipsisLoc;
669 SourceLocation ColonLoc;
671 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
672 SourceLocation ellipsisLoc, SourceLocation colonLoc)
673 : SwitchCase(CaseStmtClass) {
674 SubExprs[SUBSTMT] = 0;
675 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
676 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
678 EllipsisLoc = ellipsisLoc;
682 /// \brief Build an empty switch case statement.
683 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
685 SourceLocation getCaseLoc() const { return CaseLoc; }
686 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
687 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
688 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
689 SourceLocation getColonLoc() const { return ColonLoc; }
690 void setColonLoc(SourceLocation L) { ColonLoc = L; }
692 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
693 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
694 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
696 const Expr *getLHS() const {
697 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
699 const Expr *getRHS() const {
700 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
702 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
704 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
705 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
706 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
709 SourceRange getSourceRange() const LLVM_READONLY {
710 // Handle deeply nested case statements with iteration instead of recursion.
711 const CaseStmt *CS = this;
712 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
715 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
717 static bool classof(const Stmt *T) {
718 return T->getStmtClass() == CaseStmtClass;
720 static bool classof(const CaseStmt *) { return true; }
723 child_range children() {
724 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
728 class DefaultStmt : public SwitchCase {
730 SourceLocation DefaultLoc;
731 SourceLocation ColonLoc;
733 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
734 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
737 /// \brief Build an empty default statement.
738 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
740 Stmt *getSubStmt() { return SubStmt; }
741 const Stmt *getSubStmt() const { return SubStmt; }
742 void setSubStmt(Stmt *S) { SubStmt = S; }
744 SourceLocation getDefaultLoc() const { return DefaultLoc; }
745 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
746 SourceLocation getColonLoc() const { return ColonLoc; }
747 void setColonLoc(SourceLocation L) { ColonLoc = L; }
749 SourceRange getSourceRange() const LLVM_READONLY {
750 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
752 static bool classof(const Stmt *T) {
753 return T->getStmtClass() == DefaultStmtClass;
755 static bool classof(const DefaultStmt *) { return true; }
758 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
762 /// LabelStmt - Represents a label, which has a substatement. For example:
765 class LabelStmt : public Stmt {
768 SourceLocation IdentLoc;
770 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
771 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
774 // \brief Build an empty label statement.
775 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
777 SourceLocation getIdentLoc() const { return IdentLoc; }
778 LabelDecl *getDecl() const { return TheDecl; }
779 void setDecl(LabelDecl *D) { TheDecl = D; }
780 const char *getName() const;
781 Stmt *getSubStmt() { return SubStmt; }
782 const Stmt *getSubStmt() const { return SubStmt; }
783 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
784 void setSubStmt(Stmt *SS) { SubStmt = SS; }
786 SourceRange getSourceRange() const LLVM_READONLY {
787 return SourceRange(IdentLoc, SubStmt->getLocEnd());
789 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
791 static bool classof(const Stmt *T) {
792 return T->getStmtClass() == LabelStmtClass;
794 static bool classof(const LabelStmt *) { return true; }
798 /// \brief Represents an attribute applied to a statement.
800 /// Represents an attribute applied to a statement. For example:
801 /// [[omp::for(...)]] for (...) { ... }
803 class AttributedStmt : public Stmt {
805 SourceLocation AttrLoc;
807 // TODO: It can be done as Attr *Attrs[1]; and variable size array as in
810 friend class ASTStmtReader;
813 AttributedStmt(SourceLocation loc, const AttrVec &attrs, Stmt *substmt)
814 : Stmt(AttributedStmtClass), SubStmt(substmt), AttrLoc(loc), Attrs(attrs) {
817 // \brief Build an empty attributed statement.
818 explicit AttributedStmt(EmptyShell Empty)
819 : Stmt(AttributedStmtClass, Empty) {
822 SourceLocation getAttrLoc() const { return AttrLoc; }
823 const AttrVec &getAttrs() const { return Attrs; }
824 Stmt *getSubStmt() { return SubStmt; }
825 const Stmt *getSubStmt() const { return SubStmt; }
827 SourceRange getSourceRange() const LLVM_READONLY {
828 return SourceRange(AttrLoc, SubStmt->getLocEnd());
830 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
832 static bool classof(const Stmt *T) {
833 return T->getStmtClass() == AttributedStmtClass;
835 static bool classof(const AttributedStmt *) { return true; }
839 /// IfStmt - This represents an if/then/else.
841 class IfStmt : public Stmt {
842 enum { VAR, COND, THEN, ELSE, END_EXPR };
843 Stmt* SubExprs[END_EXPR];
845 SourceLocation IfLoc;
846 SourceLocation ElseLoc;
849 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
850 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
852 /// \brief Build an empty if/then/else statement
853 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
855 /// \brief Retrieve the variable declared in this "if" statement, if any.
857 /// In the following example, "x" is the condition variable.
859 /// if (int x = foo()) {
860 /// printf("x is %d", x);
863 VarDecl *getConditionVariable() const;
864 void setConditionVariable(ASTContext &C, VarDecl *V);
866 /// If this IfStmt has a condition variable, return the faux DeclStmt
867 /// associated with the creation of that condition variable.
868 const DeclStmt *getConditionVariableDeclStmt() const {
869 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
872 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
873 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
874 const Stmt *getThen() const { return SubExprs[THEN]; }
875 void setThen(Stmt *S) { SubExprs[THEN] = S; }
876 const Stmt *getElse() const { return SubExprs[ELSE]; }
877 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
879 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
880 Stmt *getThen() { return SubExprs[THEN]; }
881 Stmt *getElse() { return SubExprs[ELSE]; }
883 SourceLocation getIfLoc() const { return IfLoc; }
884 void setIfLoc(SourceLocation L) { IfLoc = L; }
885 SourceLocation getElseLoc() const { return ElseLoc; }
886 void setElseLoc(SourceLocation L) { ElseLoc = L; }
888 SourceRange getSourceRange() const LLVM_READONLY {
890 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
892 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
895 // Iterators over subexpressions. The iterators will include iterating
896 // over the initialization expression referenced by the condition variable.
897 child_range children() {
898 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
901 static bool classof(const Stmt *T) {
902 return T->getStmtClass() == IfStmtClass;
904 static bool classof(const IfStmt *) { return true; }
907 /// SwitchStmt - This represents a 'switch' stmt.
909 class SwitchStmt : public Stmt {
910 enum { VAR, COND, BODY, END_EXPR };
911 Stmt* SubExprs[END_EXPR];
912 // This points to a linked list of case and default statements.
913 SwitchCase *FirstCase;
914 SourceLocation SwitchLoc;
916 /// If the SwitchStmt is a switch on an enum value, this records whether
917 /// all the enum values were covered by CaseStmts. This value is meant to
918 /// be a hint for possible clients.
919 unsigned AllEnumCasesCovered : 1;
922 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
924 /// \brief Build a empty switch statement.
925 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
927 /// \brief Retrieve the variable declared in this "switch" statement, if any.
929 /// In the following example, "x" is the condition variable.
931 /// switch (int x = foo()) {
936 VarDecl *getConditionVariable() const;
937 void setConditionVariable(ASTContext &C, VarDecl *V);
939 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
940 /// associated with the creation of that condition variable.
941 const DeclStmt *getConditionVariableDeclStmt() const {
942 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
945 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
946 const Stmt *getBody() const { return SubExprs[BODY]; }
947 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
949 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
950 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
951 Stmt *getBody() { return SubExprs[BODY]; }
952 void setBody(Stmt *S) { SubExprs[BODY] = S; }
953 SwitchCase *getSwitchCaseList() { return FirstCase; }
955 /// \brief Set the case list for this switch statement.
957 /// The caller is responsible for incrementing the retain counts on
958 /// all of the SwitchCase statements in this list.
959 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
961 SourceLocation getSwitchLoc() const { return SwitchLoc; }
962 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
964 void setBody(Stmt *S, SourceLocation SL) {
968 void addSwitchCase(SwitchCase *SC) {
969 assert(!SC->getNextSwitchCase()
970 && "case/default already added to a switch");
971 SC->setNextSwitchCase(FirstCase);
975 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
976 /// switch over an enum value then all cases have been explicitly covered.
977 void setAllEnumCasesCovered() {
978 AllEnumCasesCovered = 1;
981 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
982 /// have been explicitly covered.
983 bool isAllEnumCasesCovered() const {
984 return (bool) AllEnumCasesCovered;
987 SourceRange getSourceRange() const LLVM_READONLY {
988 return SourceRange(SwitchLoc, 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;
998 static bool classof(const SwitchStmt *) { return true; }
1002 /// WhileStmt - This represents a 'while' stmt.
1004 class WhileStmt : public Stmt {
1005 enum { VAR, COND, BODY, END_EXPR };
1006 Stmt* SubExprs[END_EXPR];
1007 SourceLocation WhileLoc;
1009 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1012 /// \brief Build an empty while statement.
1013 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1015 /// \brief Retrieve the variable declared in this "while" statement, if any.
1017 /// In the following example, "x" is the condition variable.
1019 /// while (int x = random()) {
1023 VarDecl *getConditionVariable() const;
1024 void setConditionVariable(ASTContext &C, VarDecl *V);
1026 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1027 /// associated with the creation of that condition variable.
1028 const DeclStmt *getConditionVariableDeclStmt() const {
1029 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1032 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1033 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1034 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1035 Stmt *getBody() { return SubExprs[BODY]; }
1036 const Stmt *getBody() const { return SubExprs[BODY]; }
1037 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1039 SourceLocation getWhileLoc() const { return WhileLoc; }
1040 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1042 SourceRange getSourceRange() const LLVM_READONLY {
1043 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
1045 static bool classof(const Stmt *T) {
1046 return T->getStmtClass() == WhileStmtClass;
1048 static bool classof(const WhileStmt *) { return true; }
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 SourceRange getSourceRange() const LLVM_READONLY {
1092 return SourceRange(DoLoc, RParenLoc);
1094 static bool classof(const Stmt *T) {
1095 return T->getStmtClass() == DoStmtClass;
1097 static bool classof(const DoStmt *) { return true; }
1100 child_range children() {
1101 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1106 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1107 /// the init/cond/inc parts of the ForStmt will be null if they were not
1108 /// specified in the source.
1110 class ForStmt : public Stmt {
1111 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1112 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1113 SourceLocation ForLoc;
1114 SourceLocation LParenLoc, RParenLoc;
1117 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1118 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1120 /// \brief Build an empty for statement.
1121 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1123 Stmt *getInit() { return SubExprs[INIT]; }
1125 /// \brief Retrieve the variable declared in this "for" statement, if any.
1127 /// In the following example, "y" is the condition variable.
1129 /// for (int x = random(); int y = mangle(x); ++x) {
1133 VarDecl *getConditionVariable() const;
1134 void setConditionVariable(ASTContext &C, VarDecl *V);
1136 /// If this ForStmt has a condition variable, return the faux DeclStmt
1137 /// associated with the creation of that condition variable.
1138 const DeclStmt *getConditionVariableDeclStmt() const {
1139 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1142 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1143 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1144 Stmt *getBody() { return SubExprs[BODY]; }
1146 const Stmt *getInit() const { return SubExprs[INIT]; }
1147 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1148 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1149 const Stmt *getBody() const { return SubExprs[BODY]; }
1151 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1152 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1153 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1154 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1156 SourceLocation getForLoc() const { return ForLoc; }
1157 void setForLoc(SourceLocation L) { ForLoc = L; }
1158 SourceLocation getLParenLoc() const { return LParenLoc; }
1159 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1160 SourceLocation getRParenLoc() const { return RParenLoc; }
1161 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1163 SourceRange getSourceRange() const LLVM_READONLY {
1164 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1166 static bool classof(const Stmt *T) {
1167 return T->getStmtClass() == ForStmtClass;
1169 static bool classof(const ForStmt *) { return true; }
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 SourceRange getSourceRange() const LLVM_READONLY {
1199 return SourceRange(GotoLoc, LabelLoc);
1201 static bool classof(const Stmt *T) {
1202 return T->getStmtClass() == GotoStmtClass;
1204 static bool classof(const GotoStmt *) { return true; }
1207 child_range children() { return child_range(); }
1210 /// IndirectGotoStmt - This represents an indirect goto.
1212 class IndirectGotoStmt : public Stmt {
1213 SourceLocation GotoLoc;
1214 SourceLocation StarLoc;
1217 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1219 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1220 Target((Stmt*)target) {}
1222 /// \brief Build an empty indirect goto statement.
1223 explicit IndirectGotoStmt(EmptyShell Empty)
1224 : Stmt(IndirectGotoStmtClass, Empty) { }
1226 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1227 SourceLocation getGotoLoc() const { return GotoLoc; }
1228 void setStarLoc(SourceLocation L) { StarLoc = L; }
1229 SourceLocation getStarLoc() const { return StarLoc; }
1231 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1232 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1233 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1235 /// getConstantTarget - Returns the fixed target of this indirect
1236 /// goto, if one exists.
1237 LabelDecl *getConstantTarget();
1238 const LabelDecl *getConstantTarget() const {
1239 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1242 SourceRange getSourceRange() const LLVM_READONLY {
1243 return SourceRange(GotoLoc, Target->getLocEnd());
1246 static bool classof(const Stmt *T) {
1247 return T->getStmtClass() == IndirectGotoStmtClass;
1249 static bool classof(const IndirectGotoStmt *) { return true; }
1252 child_range children() { return child_range(&Target, &Target+1); }
1256 /// ContinueStmt - This represents a continue.
1258 class ContinueStmt : public Stmt {
1259 SourceLocation ContinueLoc;
1261 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1263 /// \brief Build an empty continue statement.
1264 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1266 SourceLocation getContinueLoc() const { return ContinueLoc; }
1267 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1269 SourceRange getSourceRange() const LLVM_READONLY {
1270 return SourceRange(ContinueLoc);
1273 static bool classof(const Stmt *T) {
1274 return T->getStmtClass() == ContinueStmtClass;
1276 static bool classof(const ContinueStmt *) { return true; }
1279 child_range children() { return child_range(); }
1282 /// BreakStmt - This represents a break.
1284 class BreakStmt : public Stmt {
1285 SourceLocation BreakLoc;
1287 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1289 /// \brief Build an empty break statement.
1290 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1292 SourceLocation getBreakLoc() const { return BreakLoc; }
1293 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1295 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
1297 static bool classof(const Stmt *T) {
1298 return T->getStmtClass() == BreakStmtClass;
1300 static bool classof(const BreakStmt *) { return true; }
1303 child_range children() { return child_range(); }
1307 /// ReturnStmt - This represents a return, optionally of an expression:
1311 /// Note that GCC allows return with no argument in a function declared to
1312 /// return a value, and it allows returning a value in functions declared to
1313 /// return void. We explicitly model this in the AST, which means you can't
1314 /// depend on the return type of the function and the presence of an argument.
1316 class ReturnStmt : public Stmt {
1318 SourceLocation RetLoc;
1319 const VarDecl *NRVOCandidate;
1322 ReturnStmt(SourceLocation RL)
1323 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1325 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1326 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1327 NRVOCandidate(NRVOCandidate) {}
1329 /// \brief Build an empty return expression.
1330 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1332 const Expr *getRetValue() const;
1333 Expr *getRetValue();
1334 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1336 SourceLocation getReturnLoc() const { return RetLoc; }
1337 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1339 /// \brief Retrieve the variable that might be used for the named return
1340 /// value optimization.
1342 /// The optimization itself can only be performed if the variable is
1343 /// also marked as an NRVO object.
1344 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1345 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1347 SourceRange getSourceRange() const LLVM_READONLY;
1349 static bool classof(const Stmt *T) {
1350 return T->getStmtClass() == ReturnStmtClass;
1352 static bool classof(const ReturnStmt *) { return true; }
1355 child_range children() {
1356 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1357 return child_range();
1361 /// AsmStmt - This represents a GNU inline-assembly statement extension.
1363 class AsmStmt : public Stmt {
1364 SourceLocation AsmLoc, RParenLoc;
1365 StringLiteral *AsmStr;
1371 unsigned NumOutputs;
1373 unsigned NumClobbers;
1375 // FIXME: If we wanted to, we could allocate all of these in one big array.
1376 IdentifierInfo **Names;
1377 StringLiteral **Constraints;
1379 StringLiteral **Clobbers;
1382 AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1383 bool msasm, unsigned numoutputs, unsigned numinputs,
1384 IdentifierInfo **names, StringLiteral **constraints,
1385 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1386 StringLiteral **clobbers, SourceLocation rparenloc);
1388 /// \brief Build an empty inline-assembly statement.
1389 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1390 Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1392 SourceLocation getAsmLoc() const { return AsmLoc; }
1393 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1394 SourceLocation getRParenLoc() const { return RParenLoc; }
1395 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1397 bool isVolatile() const { return IsVolatile; }
1398 void setVolatile(bool V) { IsVolatile = V; }
1399 bool isSimple() const { return IsSimple; }
1400 void setSimple(bool V) { IsSimple = V; }
1401 bool isMSAsm() const { return MSAsm; }
1402 void setMSAsm(bool V) { MSAsm = V; }
1404 //===--- Asm String Analysis ---===//
1406 const StringLiteral *getAsmString() const { return AsmStr; }
1407 StringLiteral *getAsmString() { return AsmStr; }
1408 void setAsmString(StringLiteral *E) { AsmStr = E; }
1410 /// AsmStringPiece - this is part of a decomposed asm string specification
1411 /// (for use with the AnalyzeAsmString function below). An asm string is
1412 /// considered to be a concatenation of these parts.
1413 class AsmStringPiece {
1416 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1417 Operand // Operand reference, with optional modifier %c4.
1424 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1425 AsmStringPiece(unsigned OpNo, char Modifier)
1426 : MyKind(Operand), Str(), OperandNo(OpNo) {
1430 bool isString() const { return MyKind == String; }
1431 bool isOperand() const { return MyKind == Operand; }
1433 const std::string &getString() const {
1438 unsigned getOperandNo() const {
1439 assert(isOperand());
1443 /// getModifier - Get the modifier for this operand, if present. This
1444 /// returns '\0' if there was no modifier.
1445 char getModifier() const {
1446 assert(isOperand());
1451 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1452 /// it into pieces. If the asm string is erroneous, emit errors and return
1453 /// true, otherwise return false. This handles canonicalization and
1454 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1455 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1456 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1457 ASTContext &C, unsigned &DiagOffs) const;
1460 //===--- Output operands ---===//
1462 unsigned getNumOutputs() const { return NumOutputs; }
1464 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1468 StringRef getOutputName(unsigned i) const {
1469 if (IdentifierInfo *II = getOutputIdentifier(i))
1470 return II->getName();
1475 /// getOutputConstraint - Return the constraint string for the specified
1476 /// output operand. All output constraints are known to be non-empty (either
1478 StringRef getOutputConstraint(unsigned i) const;
1480 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1481 return Constraints[i];
1483 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1484 return Constraints[i];
1487 Expr *getOutputExpr(unsigned i);
1489 const Expr *getOutputExpr(unsigned i) const {
1490 return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1493 /// isOutputPlusConstraint - Return true if the specified output constraint
1494 /// is a "+" constraint (which is both an input and an output) or false if it
1495 /// is an "=" constraint (just an output).
1496 bool isOutputPlusConstraint(unsigned i) const {
1497 return getOutputConstraint(i)[0] == '+';
1500 /// getNumPlusOperands - Return the number of output operands that have a "+"
1502 unsigned getNumPlusOperands() const;
1504 //===--- Input operands ---===//
1506 unsigned getNumInputs() const { return NumInputs; }
1508 IdentifierInfo *getInputIdentifier(unsigned i) const {
1509 return Names[i + NumOutputs];
1512 StringRef getInputName(unsigned i) const {
1513 if (IdentifierInfo *II = getInputIdentifier(i))
1514 return II->getName();
1519 /// getInputConstraint - Return the specified input constraint. Unlike output
1520 /// constraints, these can be empty.
1521 StringRef getInputConstraint(unsigned i) const;
1523 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1524 return Constraints[i + NumOutputs];
1526 StringLiteral *getInputConstraintLiteral(unsigned i) {
1527 return Constraints[i + NumOutputs];
1530 Expr *getInputExpr(unsigned i);
1531 void setInputExpr(unsigned i, Expr *E);
1533 const Expr *getInputExpr(unsigned i) const {
1534 return const_cast<AsmStmt*>(this)->getInputExpr(i);
1537 void setOutputsAndInputsAndClobbers(ASTContext &C,
1538 IdentifierInfo **Names,
1539 StringLiteral **Constraints,
1541 unsigned NumOutputs,
1543 StringLiteral **Clobbers,
1544 unsigned NumClobbers);
1546 //===--- Other ---===//
1548 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1549 /// translate this into a numeric value needed to reference the same operand.
1550 /// This returns -1 if the operand name is invalid.
1551 int getNamedOperand(StringRef SymbolicName) const;
1553 unsigned getNumClobbers() const { return NumClobbers; }
1554 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1555 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1557 SourceRange getSourceRange() const LLVM_READONLY {
1558 return SourceRange(AsmLoc, RParenLoc);
1561 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1562 static bool classof(const AsmStmt *) { return true; }
1564 // Input expr iterators.
1566 typedef ExprIterator inputs_iterator;
1567 typedef ConstExprIterator const_inputs_iterator;
1569 inputs_iterator begin_inputs() {
1570 return &Exprs[0] + NumOutputs;
1573 inputs_iterator end_inputs() {
1574 return &Exprs[0] + NumOutputs + NumInputs;
1577 const_inputs_iterator begin_inputs() const {
1578 return &Exprs[0] + NumOutputs;
1581 const_inputs_iterator end_inputs() const {
1582 return &Exprs[0] + NumOutputs + NumInputs;
1585 // Output expr iterators.
1587 typedef ExprIterator outputs_iterator;
1588 typedef ConstExprIterator const_outputs_iterator;
1590 outputs_iterator begin_outputs() {
1593 outputs_iterator end_outputs() {
1594 return &Exprs[0] + NumOutputs;
1597 const_outputs_iterator begin_outputs() const {
1600 const_outputs_iterator end_outputs() const {
1601 return &Exprs[0] + NumOutputs;
1604 child_range children() {
1605 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1609 class SEHExceptStmt : public Stmt {
1613 enum { FILTER_EXPR, BLOCK };
1615 SEHExceptStmt(SourceLocation Loc,
1619 friend class ASTReader;
1620 friend class ASTStmtReader;
1621 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1624 static SEHExceptStmt* Create(ASTContext &C,
1625 SourceLocation ExceptLoc,
1628 SourceRange getSourceRange() const LLVM_READONLY {
1629 return SourceRange(getExceptLoc(), getEndLoc());
1632 SourceLocation getExceptLoc() const { return Loc; }
1633 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1635 Expr *getFilterExpr() const {
1636 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1639 CompoundStmt *getBlock() const {
1640 return llvm::cast<CompoundStmt>(Children[BLOCK]);
1643 child_range children() {
1644 return child_range(Children,Children+2);
1647 static bool classof(const Stmt *T) {
1648 return T->getStmtClass() == SEHExceptStmtClass;
1651 static bool classof(SEHExceptStmt *) { return true; }
1655 class SEHFinallyStmt : public Stmt {
1659 SEHFinallyStmt(SourceLocation Loc,
1662 friend class ASTReader;
1663 friend class ASTStmtReader;
1664 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1667 static SEHFinallyStmt* Create(ASTContext &C,
1668 SourceLocation FinallyLoc,
1671 SourceRange getSourceRange() const LLVM_READONLY {
1672 return SourceRange(getFinallyLoc(), getEndLoc());
1675 SourceLocation getFinallyLoc() const { return Loc; }
1676 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1678 CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1680 child_range children() {
1681 return child_range(&Block,&Block+1);
1684 static bool classof(const Stmt *T) {
1685 return T->getStmtClass() == SEHFinallyStmtClass;
1688 static bool classof(SEHFinallyStmt *) { return true; }
1692 class SEHTryStmt : public Stmt {
1694 SourceLocation TryLoc;
1697 enum { TRY = 0, HANDLER = 1 };
1699 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1700 SourceLocation TryLoc,
1704 friend class ASTReader;
1705 friend class ASTStmtReader;
1706 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1709 static SEHTryStmt* Create(ASTContext &C,
1711 SourceLocation TryLoc,
1715 SourceRange getSourceRange() const LLVM_READONLY {
1716 return SourceRange(getTryLoc(), getEndLoc());
1719 SourceLocation getTryLoc() const { return TryLoc; }
1720 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1722 bool getIsCXXTry() const { return IsCXXTry; }
1724 CompoundStmt* getTryBlock() const {
1725 return llvm::cast<CompoundStmt>(Children[TRY]);
1728 Stmt *getHandler() const { return Children[HANDLER]; }
1730 /// Returns 0 if not defined
1731 SEHExceptStmt *getExceptHandler() const;
1732 SEHFinallyStmt *getFinallyHandler() const;
1734 child_range children() {
1735 return child_range(Children,Children+2);
1738 static bool classof(const Stmt *T) {
1739 return T->getStmtClass() == SEHTryStmtClass;
1742 static bool classof(SEHTryStmt *) { return true; }
1745 } // end namespace clang