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;
41 struct PrintingPolicy;
50 //===--------------------------------------------------------------------===//
51 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
52 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
53 // references to children (to be compatible with StmtIterator).
54 //===--------------------------------------------------------------------===//
62 ExprIterator(Stmt** i) : I(i) {}
63 ExprIterator() : I(0) {}
64 ExprIterator& operator++() { ++I; return *this; }
65 ExprIterator operator-(size_t i) { return I-i; }
66 ExprIterator operator+(size_t i) { return I+i; }
67 Expr* operator[](size_t idx);
68 // FIXME: Verify that this will correctly return a signed distance.
69 signed operator-(const ExprIterator& R) const { return I - R.I; }
70 Expr* operator*() const;
71 Expr* operator->() const;
72 bool operator==(const ExprIterator& R) const { return I == R.I; }
73 bool operator!=(const ExprIterator& R) const { return I != R.I; }
74 bool operator>(const ExprIterator& R) const { return I > R.I; }
75 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
78 class ConstExprIterator {
79 const Stmt * const *I;
81 ConstExprIterator(const Stmt * const *i) : I(i) {}
82 ConstExprIterator() : I(0) {}
83 ConstExprIterator& operator++() { ++I; return *this; }
84 ConstExprIterator operator+(size_t i) const { return I+i; }
85 ConstExprIterator operator-(size_t i) const { return I-i; }
86 const Expr * operator[](size_t idx) const;
87 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
88 const Expr * operator*() const;
89 const Expr * operator->() const;
90 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
91 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
92 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
93 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
96 //===----------------------------------------------------------------------===//
97 // AST classes for statements.
98 //===----------------------------------------------------------------------===//
100 /// Stmt - This represents one statement.
106 #define STMT(CLASS, PARENT) CLASS##Class,
107 #define STMT_RANGE(BASE, FIRST, LAST) \
108 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
109 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
110 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
111 #define ABSTRACT_STMT(STMT)
112 #include "clang/AST/StmtNodes.inc"
115 // Make vanilla 'new' and 'delete' illegal for Stmts.
117 void* operator new(size_t bytes) throw() {
118 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
120 void operator delete(void* data) throw() {
121 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
124 class StmtBitfields {
127 /// \brief The statement class.
130 enum { NumStmtBits = 8 };
132 class CompoundStmtBitfields {
133 friend class CompoundStmt;
134 unsigned : NumStmtBits;
136 unsigned NumStmts : 32 - NumStmtBits;
139 class ExprBitfields {
141 friend class DeclRefExpr; // computeDependence
142 friend class InitListExpr; // ctor
143 friend class DesignatedInitExpr; // ctor
144 friend class BlockDeclRefExpr; // ctor
145 friend class ASTStmtReader; // deserialization
146 friend class CXXNewExpr; // ctor
147 friend class DependentScopeDeclRefExpr; // ctor
148 friend class CXXConstructExpr; // ctor
149 friend class CallExpr; // ctor
150 friend class OffsetOfExpr; // ctor
151 friend class ObjCMessageExpr; // ctor
152 friend class ObjCArrayLiteral; // ctor
153 friend class ObjCDictionaryLiteral; // ctor
154 friend class ShuffleVectorExpr; // ctor
155 friend class ParenListExpr; // ctor
156 friend class CXXUnresolvedConstructExpr; // ctor
157 friend class CXXDependentScopeMemberExpr; // ctor
158 friend class OverloadExpr; // ctor
159 friend class PseudoObjectExpr; // ctor
160 friend class AtomicExpr; // ctor
161 unsigned : NumStmtBits;
163 unsigned ValueKind : 2;
164 unsigned ObjectKind : 2;
165 unsigned TypeDependent : 1;
166 unsigned ValueDependent : 1;
167 unsigned InstantiationDependent : 1;
168 unsigned ContainsUnexpandedParameterPack : 1;
170 enum { NumExprBits = 16 };
172 class CharacterLiteralBitfields {
173 friend class CharacterLiteral;
174 unsigned : NumExprBits;
179 enum APFloatSemantics {
188 class FloatingLiteralBitfields {
189 friend class FloatingLiteral;
190 unsigned : NumExprBits;
192 unsigned Semantics : 3; // Provides semantics for APFloat construction
193 unsigned IsExact : 1;
196 class UnaryExprOrTypeTraitExprBitfields {
197 friend class UnaryExprOrTypeTraitExpr;
198 unsigned : NumExprBits;
201 unsigned IsType : 1; // true if operand is a type, false if an expression.
204 class DeclRefExprBitfields {
205 friend class DeclRefExpr;
206 friend class ASTStmtReader; // deserialization
207 unsigned : NumExprBits;
209 unsigned HasQualifier : 1;
210 unsigned HasTemplateKWAndArgsInfo : 1;
211 unsigned HasFoundDecl : 1;
212 unsigned HadMultipleCandidates : 1;
213 unsigned RefersToEnclosingLocal : 1;
216 class CastExprBitfields {
217 friend class CastExpr;
218 unsigned : NumExprBits;
221 unsigned BasePathSize : 32 - 6 - NumExprBits;
224 class CallExprBitfields {
225 friend class CallExpr;
226 unsigned : NumExprBits;
228 unsigned NumPreArgs : 1;
231 class ExprWithCleanupsBitfields {
232 friend class ExprWithCleanups;
233 friend class ASTStmtReader; // deserialization
235 unsigned : NumExprBits;
237 unsigned NumObjects : 32 - NumExprBits;
240 class PseudoObjectExprBitfields {
241 friend class PseudoObjectExpr;
242 friend class ASTStmtReader; // deserialization
244 unsigned : NumExprBits;
246 // These don't need to be particularly wide, because they're
247 // strictly limited by the forms of expressions we permit.
248 unsigned NumSubExprs : 8;
249 unsigned ResultIndex : 32 - 8 - NumExprBits;
252 class ObjCIndirectCopyRestoreExprBitfields {
253 friend class ObjCIndirectCopyRestoreExpr;
254 unsigned : NumExprBits;
256 unsigned ShouldCopy : 1;
259 class InitListExprBitfields {
260 friend class InitListExpr;
262 unsigned : NumExprBits;
264 /// Whether this initializer list originally had a GNU array-range
265 /// designator in it. This is a temporary marker used by CodeGen.
266 unsigned HadArrayRangeDesignator : 1;
268 /// Whether this initializer list initializes a std::initializer_list
270 unsigned InitializesStdInitializerList : 1;
273 class TypeTraitExprBitfields {
274 friend class TypeTraitExpr;
275 friend class ASTStmtReader;
276 friend class ASTStmtWriter;
278 unsigned : NumExprBits;
280 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
283 /// \brief If this expression is not value-dependent, this indicates whether
284 /// the trait evaluated true or false.
287 /// \brief The number of arguments to this type trait.
288 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
292 // FIXME: this is wasteful on 64-bit platforms.
295 StmtBitfields StmtBits;
296 CompoundStmtBitfields CompoundStmtBits;
297 ExprBitfields ExprBits;
298 CharacterLiteralBitfields CharacterLiteralBits;
299 FloatingLiteralBitfields FloatingLiteralBits;
300 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
301 DeclRefExprBitfields DeclRefExprBits;
302 CastExprBitfields CastExprBits;
303 CallExprBitfields CallExprBits;
304 ExprWithCleanupsBitfields ExprWithCleanupsBits;
305 PseudoObjectExprBitfields PseudoObjectExprBits;
306 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
307 InitListExprBitfields InitListExprBits;
308 TypeTraitExprBitfields TypeTraitExprBits;
311 friend class ASTStmtReader;
312 friend class ASTStmtWriter;
315 // Only allow allocation of Stmts using the allocator in ASTContext
316 // or by doing a placement new.
317 void* operator new(size_t bytes, ASTContext& C,
318 unsigned alignment = 8) throw();
320 void* operator new(size_t bytes, ASTContext* C,
321 unsigned alignment = 8) throw();
323 void* operator new(size_t bytes, void* mem) throw() {
327 void operator delete(void*, ASTContext&, unsigned) throw() { }
328 void operator delete(void*, ASTContext*, unsigned) throw() { }
329 void operator delete(void*, std::size_t) throw() { }
330 void operator delete(void*, void*) throw() { }
333 /// \brief A placeholder type used to construct an empty shell of a
334 /// type, that will be filled in later (e.g., by some
335 /// de-serialization).
336 struct EmptyShell { };
339 /// \brief Whether statistic collection is enabled.
340 static bool StatisticsEnabled;
343 /// \brief Construct an empty statement.
344 explicit Stmt(StmtClass SC, EmptyShell) {
345 StmtBits.sClass = SC;
346 if (StatisticsEnabled) Stmt::addStmtClass(SC);
351 StmtBits.sClass = SC;
352 if (StatisticsEnabled) Stmt::addStmtClass(SC);
355 StmtClass getStmtClass() const {
356 return static_cast<StmtClass>(StmtBits.sClass);
358 const char *getStmtClassName() const;
360 /// SourceLocation tokens are not useful in isolation - they are low level
361 /// value objects created/interpreted by SourceManager. We assume AST
362 /// clients will have a pointer to the respective SourceManager.
363 SourceRange getSourceRange() const LLVM_READONLY;
364 SourceLocation getLocStart() const LLVM_READONLY;
365 SourceLocation getLocEnd() const LLVM_READONLY;
367 // global temp stats (until we have a per-module visitor)
368 static void addStmtClass(const StmtClass s);
369 static void EnableStatistics();
370 static void PrintStats();
372 /// \brief Dumps the specified AST fragment and all subtrees to
374 LLVM_ATTRIBUTE_USED void dump() const;
375 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
376 void dump(raw_ostream &OS, SourceManager &SM) const;
378 /// dumpColor - same as dump(), but forces color highlighting.
379 LLVM_ATTRIBUTE_USED void dumpColor() const;
381 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
382 /// back to its original source language syntax.
383 void dumpPretty(ASTContext &Context) const;
384 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
385 const PrintingPolicy &Policy,
386 unsigned Indentation = 0) const;
388 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
389 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
390 void viewAST() const;
392 /// Skip past any implicit AST nodes which might surround this
393 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
394 Stmt *IgnoreImplicit();
396 const Stmt *stripLabelLikeStatements() const;
397 Stmt *stripLabelLikeStatements() {
398 return const_cast<Stmt*>(
399 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
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 /// \param ID once the profiling operation is complete, will contain
432 /// the unique representation of the given statement.
434 /// \param Context the AST context in which the statement resides
436 /// \param 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 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
481 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
483 static bool classof(const Stmt *T) {
484 return T->getStmtClass() == DeclStmtClass;
487 // Iterators over subexpressions.
488 child_range children() {
489 return child_range(child_iterator(DG.begin(), DG.end()),
490 child_iterator(DG.end(), DG.end()));
493 typedef DeclGroupRef::iterator decl_iterator;
494 typedef DeclGroupRef::const_iterator const_decl_iterator;
496 decl_iterator decl_begin() { return DG.begin(); }
497 decl_iterator decl_end() { return DG.end(); }
498 const_decl_iterator decl_begin() const { return DG.begin(); }
499 const_decl_iterator decl_end() const { return DG.end(); }
501 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
502 reverse_decl_iterator decl_rbegin() {
503 return reverse_decl_iterator(decl_end());
505 reverse_decl_iterator decl_rend() {
506 return reverse_decl_iterator(decl_begin());
510 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
512 class NullStmt : public Stmt {
513 SourceLocation SemiLoc;
515 /// \brief True if the null statement was preceded by an empty macro, e.g:
520 bool HasLeadingEmptyMacro;
522 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
523 : Stmt(NullStmtClass), SemiLoc(L),
524 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
526 /// \brief Build an empty null statement.
527 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
528 HasLeadingEmptyMacro(false) { }
530 SourceLocation getSemiLoc() const { return SemiLoc; }
531 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
533 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
535 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
536 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
538 static bool classof(const Stmt *T) {
539 return T->getStmtClass() == NullStmtClass;
542 child_range children() { return child_range(); }
544 friend class ASTStmtReader;
545 friend class ASTStmtWriter;
548 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
550 class CompoundStmt : public Stmt {
552 SourceLocation LBracLoc, RBracLoc;
554 CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
555 SourceLocation LB, SourceLocation RB);
557 // \brief Build an empty compound statment with a location.
558 explicit CompoundStmt(SourceLocation Loc)
559 : Stmt(CompoundStmtClass), Body(0), LBracLoc(Loc), RBracLoc(Loc) {
560 CompoundStmtBits.NumStmts = 0;
563 // \brief Build an empty compound statement.
564 explicit CompoundStmt(EmptyShell Empty)
565 : Stmt(CompoundStmtClass, Empty), Body(0) {
566 CompoundStmtBits.NumStmts = 0;
569 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
571 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
572 unsigned size() const { return CompoundStmtBits.NumStmts; }
574 typedef Stmt** body_iterator;
575 body_iterator body_begin() { return Body; }
576 body_iterator body_end() { return Body + size(); }
577 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
579 void setLastStmt(Stmt *S) {
580 assert(!body_empty() && "setLastStmt");
584 typedef Stmt* const * const_body_iterator;
585 const_body_iterator body_begin() const { return Body; }
586 const_body_iterator body_end() const { return Body + size(); }
587 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
589 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
590 reverse_body_iterator body_rbegin() {
591 return reverse_body_iterator(body_end());
593 reverse_body_iterator body_rend() {
594 return reverse_body_iterator(body_begin());
597 typedef std::reverse_iterator<const_body_iterator>
598 const_reverse_body_iterator;
600 const_reverse_body_iterator body_rbegin() const {
601 return const_reverse_body_iterator(body_end());
604 const_reverse_body_iterator body_rend() const {
605 return const_reverse_body_iterator(body_begin());
608 SourceLocation getLocStart() const LLVM_READONLY { return LBracLoc; }
609 SourceLocation getLocEnd() const LLVM_READONLY { return RBracLoc; }
611 SourceLocation getLBracLoc() const { return LBracLoc; }
612 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
613 SourceLocation getRBracLoc() const { return RBracLoc; }
614 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
616 static bool classof(const Stmt *T) {
617 return T->getStmtClass() == CompoundStmtClass;
621 child_range children() {
622 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
625 const_child_range children() const {
626 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
630 // SwitchCase is the base class for CaseStmt and DefaultStmt,
631 class SwitchCase : public Stmt {
633 // A pointer to the following CaseStmt or DefaultStmt class,
634 // used by SwitchStmt.
635 SwitchCase *NextSwitchCase;
636 SourceLocation KeywordLoc;
637 SourceLocation ColonLoc;
639 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
640 : Stmt(SC), NextSwitchCase(0), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
642 SwitchCase(StmtClass SC, EmptyShell)
643 : Stmt(SC), NextSwitchCase(0) {}
646 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
648 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
650 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
652 SourceLocation getKeywordLoc() const { return KeywordLoc; }
653 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
654 SourceLocation getColonLoc() const { return ColonLoc; }
655 void setColonLoc(SourceLocation L) { ColonLoc = L; }
658 const Stmt *getSubStmt() const {
659 return const_cast<SwitchCase*>(this)->getSubStmt();
662 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
663 SourceLocation getLocEnd() const LLVM_READONLY;
665 static bool classof(const Stmt *T) {
666 return T->getStmtClass() == CaseStmtClass ||
667 T->getStmtClass() == DefaultStmtClass;
671 class CaseStmt : public SwitchCase {
672 enum { LHS, RHS, SUBSTMT, END_EXPR };
673 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
674 // GNU "case 1 ... 4" extension
675 SourceLocation EllipsisLoc;
677 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
678 SourceLocation ellipsisLoc, SourceLocation colonLoc)
679 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
680 SubExprs[SUBSTMT] = 0;
681 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
682 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
683 EllipsisLoc = ellipsisLoc;
686 /// \brief Build an empty switch case statement.
687 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { }
689 SourceLocation getCaseLoc() const { return KeywordLoc; }
690 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
691 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
692 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
693 SourceLocation getColonLoc() const { return ColonLoc; }
694 void setColonLoc(SourceLocation L) { ColonLoc = L; }
696 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
697 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
698 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
700 const Expr *getLHS() const {
701 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
703 const Expr *getRHS() const {
704 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
706 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
708 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
709 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
710 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
712 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
713 SourceLocation getLocEnd() const LLVM_READONLY {
714 // Handle deeply nested case statements with iteration instead of recursion.
715 const CaseStmt *CS = this;
716 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
719 return CS->getSubStmt()->getLocEnd();
722 static bool classof(const Stmt *T) {
723 return T->getStmtClass() == CaseStmtClass;
727 child_range children() {
728 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
732 class DefaultStmt : public SwitchCase {
735 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
736 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
738 /// \brief Build an empty default statement.
739 explicit DefaultStmt(EmptyShell Empty)
740 : SwitchCase(DefaultStmtClass, Empty) { }
742 Stmt *getSubStmt() { return SubStmt; }
743 const Stmt *getSubStmt() const { return SubStmt; }
744 void setSubStmt(Stmt *S) { SubStmt = S; }
746 SourceLocation getDefaultLoc() const { return KeywordLoc; }
747 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
748 SourceLocation getColonLoc() const { return ColonLoc; }
749 void setColonLoc(SourceLocation L) { ColonLoc = L; }
751 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
752 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
754 static bool classof(const Stmt *T) {
755 return T->getStmtClass() == DefaultStmtClass;
759 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
762 inline SourceLocation SwitchCase::getLocEnd() const {
763 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
764 return CS->getLocEnd();
765 return cast<DefaultStmt>(this)->getLocEnd();
768 /// LabelStmt - Represents a label, which has a substatement. For example:
771 class LabelStmt : public Stmt {
774 SourceLocation IdentLoc;
776 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
777 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
780 // \brief Build an empty label statement.
781 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
783 SourceLocation getIdentLoc() const { return IdentLoc; }
784 LabelDecl *getDecl() const { return TheDecl; }
785 void setDecl(LabelDecl *D) { TheDecl = D; }
786 const char *getName() const;
787 Stmt *getSubStmt() { return SubStmt; }
788 const Stmt *getSubStmt() const { return SubStmt; }
789 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
790 void setSubStmt(Stmt *SS) { SubStmt = SS; }
792 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
793 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
795 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
797 static bool classof(const Stmt *T) {
798 return T->getStmtClass() == LabelStmtClass;
803 /// \brief Represents an attribute applied to a statement.
805 /// Represents an attribute applied to a statement. For example:
806 /// [[omp::for(...)]] for (...) { ... }
808 class AttributedStmt : public Stmt {
810 SourceLocation AttrLoc;
812 const Attr *Attrs[1];
814 friend class ASTStmtReader;
816 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
817 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
818 NumAttrs(Attrs.size()) {
819 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*));
822 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
823 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
824 memset(Attrs, 0, NumAttrs * sizeof(Attr*));
828 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc,
829 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
830 // \brief Build an empty attributed statement.
831 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs);
833 SourceLocation getAttrLoc() const { return AttrLoc; }
834 ArrayRef<const Attr*> getAttrs() const {
835 return ArrayRef<const Attr*>(Attrs, NumAttrs);
837 Stmt *getSubStmt() { return SubStmt; }
838 const Stmt *getSubStmt() const { return SubStmt; }
840 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
841 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
843 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
845 static bool classof(const Stmt *T) {
846 return T->getStmtClass() == AttributedStmtClass;
851 /// IfStmt - This represents an if/then/else.
853 class IfStmt : public Stmt {
854 enum { VAR, COND, THEN, ELSE, END_EXPR };
855 Stmt* SubExprs[END_EXPR];
857 SourceLocation IfLoc;
858 SourceLocation ElseLoc;
861 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
862 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
864 /// \brief Build an empty if/then/else statement
865 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
867 /// \brief Retrieve the variable declared in this "if" statement, if any.
869 /// In the following example, "x" is the condition variable.
871 /// if (int x = foo()) {
872 /// printf("x is %d", x);
875 VarDecl *getConditionVariable() const;
876 void setConditionVariable(ASTContext &C, VarDecl *V);
878 /// If this IfStmt has a condition variable, return the faux DeclStmt
879 /// associated with the creation of that condition variable.
880 const DeclStmt *getConditionVariableDeclStmt() const {
881 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
884 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
885 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
886 const Stmt *getThen() const { return SubExprs[THEN]; }
887 void setThen(Stmt *S) { SubExprs[THEN] = S; }
888 const Stmt *getElse() const { return SubExprs[ELSE]; }
889 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
891 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
892 Stmt *getThen() { return SubExprs[THEN]; }
893 Stmt *getElse() { return SubExprs[ELSE]; }
895 SourceLocation getIfLoc() const { return IfLoc; }
896 void setIfLoc(SourceLocation L) { IfLoc = L; }
897 SourceLocation getElseLoc() const { return ElseLoc; }
898 void setElseLoc(SourceLocation L) { ElseLoc = L; }
900 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
901 SourceLocation getLocEnd() const LLVM_READONLY {
903 return SubExprs[ELSE]->getLocEnd();
905 return SubExprs[THEN]->getLocEnd();
908 // Iterators over subexpressions. The iterators will include iterating
909 // over the initialization expression referenced by the condition variable.
910 child_range children() {
911 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
914 static bool classof(const Stmt *T) {
915 return T->getStmtClass() == IfStmtClass;
919 /// SwitchStmt - This represents a 'switch' stmt.
921 class SwitchStmt : public Stmt {
922 enum { VAR, COND, BODY, END_EXPR };
923 Stmt* SubExprs[END_EXPR];
924 // This points to a linked list of case and default statements.
925 SwitchCase *FirstCase;
926 SourceLocation SwitchLoc;
928 /// If the SwitchStmt is a switch on an enum value, this records whether
929 /// all the enum values were covered by CaseStmts. This value is meant to
930 /// be a hint for possible clients.
931 unsigned AllEnumCasesCovered : 1;
934 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
936 /// \brief Build a empty switch statement.
937 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
939 /// \brief Retrieve the variable declared in this "switch" statement, if any.
941 /// In the following example, "x" is the condition variable.
943 /// switch (int x = foo()) {
948 VarDecl *getConditionVariable() const;
949 void setConditionVariable(ASTContext &C, VarDecl *V);
951 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
952 /// associated with the creation of that condition variable.
953 const DeclStmt *getConditionVariableDeclStmt() const {
954 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
957 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
958 const Stmt *getBody() const { return SubExprs[BODY]; }
959 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
961 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
962 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
963 Stmt *getBody() { return SubExprs[BODY]; }
964 void setBody(Stmt *S) { SubExprs[BODY] = S; }
965 SwitchCase *getSwitchCaseList() { return FirstCase; }
967 /// \brief Set the case list for this switch statement.
969 /// The caller is responsible for incrementing the retain counts on
970 /// all of the SwitchCase statements in this list.
971 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
973 SourceLocation getSwitchLoc() const { return SwitchLoc; }
974 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
976 void setBody(Stmt *S, SourceLocation SL) {
980 void addSwitchCase(SwitchCase *SC) {
981 assert(!SC->getNextSwitchCase()
982 && "case/default already added to a switch");
983 SC->setNextSwitchCase(FirstCase);
987 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
988 /// switch over an enum value then all cases have been explicitly covered.
989 void setAllEnumCasesCovered() {
990 AllEnumCasesCovered = 1;
993 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
994 /// have been explicitly covered.
995 bool isAllEnumCasesCovered() const {
996 return (bool) AllEnumCasesCovered;
999 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1000 SourceLocation getLocEnd() const LLVM_READONLY {
1001 return SubExprs[BODY]->getLocEnd();
1005 child_range children() {
1006 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1009 static bool classof(const Stmt *T) {
1010 return T->getStmtClass() == SwitchStmtClass;
1015 /// WhileStmt - This represents a 'while' stmt.
1017 class WhileStmt : public Stmt {
1018 enum { VAR, COND, BODY, END_EXPR };
1019 Stmt* SubExprs[END_EXPR];
1020 SourceLocation WhileLoc;
1022 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1025 /// \brief Build an empty while statement.
1026 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1028 /// \brief Retrieve the variable declared in this "while" statement, if any.
1030 /// In the following example, "x" is the condition variable.
1032 /// while (int x = random()) {
1036 VarDecl *getConditionVariable() const;
1037 void setConditionVariable(ASTContext &C, VarDecl *V);
1039 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1040 /// associated with the creation of that condition variable.
1041 const DeclStmt *getConditionVariableDeclStmt() const {
1042 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1045 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1046 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1047 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1048 Stmt *getBody() { return SubExprs[BODY]; }
1049 const Stmt *getBody() const { return SubExprs[BODY]; }
1050 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1052 SourceLocation getWhileLoc() const { return WhileLoc; }
1053 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1055 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1056 SourceLocation getLocEnd() const LLVM_READONLY {
1057 return SubExprs[BODY]->getLocEnd();
1060 static bool classof(const Stmt *T) {
1061 return T->getStmtClass() == WhileStmtClass;
1065 child_range children() {
1066 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1070 /// DoStmt - This represents a 'do/while' stmt.
1072 class DoStmt : public Stmt {
1073 enum { BODY, COND, END_EXPR };
1074 Stmt* SubExprs[END_EXPR];
1075 SourceLocation DoLoc;
1076 SourceLocation WhileLoc;
1077 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1080 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1082 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1083 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1084 SubExprs[BODY] = body;
1087 /// \brief Build an empty do-while statement.
1088 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1090 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1091 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1092 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1093 Stmt *getBody() { return SubExprs[BODY]; }
1094 const Stmt *getBody() const { return SubExprs[BODY]; }
1095 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1097 SourceLocation getDoLoc() const { return DoLoc; }
1098 void setDoLoc(SourceLocation L) { DoLoc = L; }
1099 SourceLocation getWhileLoc() const { return WhileLoc; }
1100 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1102 SourceLocation getRParenLoc() const { return RParenLoc; }
1103 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1105 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1106 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1108 static bool classof(const Stmt *T) {
1109 return T->getStmtClass() == DoStmtClass;
1113 child_range children() {
1114 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1119 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1120 /// the init/cond/inc parts of the ForStmt will be null if they were not
1121 /// specified in the source.
1123 class ForStmt : public Stmt {
1124 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1125 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1126 SourceLocation ForLoc;
1127 SourceLocation LParenLoc, RParenLoc;
1130 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1131 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1133 /// \brief Build an empty for statement.
1134 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1136 Stmt *getInit() { return SubExprs[INIT]; }
1138 /// \brief Retrieve the variable declared in this "for" statement, if any.
1140 /// In the following example, "y" is the condition variable.
1142 /// for (int x = random(); int y = mangle(x); ++x) {
1146 VarDecl *getConditionVariable() const;
1147 void setConditionVariable(ASTContext &C, VarDecl *V);
1149 /// If this ForStmt has a condition variable, return the faux DeclStmt
1150 /// associated with the creation of that condition variable.
1151 const DeclStmt *getConditionVariableDeclStmt() const {
1152 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1155 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1156 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1157 Stmt *getBody() { return SubExprs[BODY]; }
1159 const Stmt *getInit() const { return SubExprs[INIT]; }
1160 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1161 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1162 const Stmt *getBody() const { return SubExprs[BODY]; }
1164 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1165 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1166 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1167 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1169 SourceLocation getForLoc() const { return ForLoc; }
1170 void setForLoc(SourceLocation L) { ForLoc = L; }
1171 SourceLocation getLParenLoc() const { return LParenLoc; }
1172 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1173 SourceLocation getRParenLoc() const { return RParenLoc; }
1174 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1176 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1177 SourceLocation getLocEnd() const LLVM_READONLY {
1178 return SubExprs[BODY]->getLocEnd();
1181 static bool classof(const Stmt *T) {
1182 return T->getStmtClass() == ForStmtClass;
1186 child_range children() {
1187 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1191 /// GotoStmt - This represents a direct goto.
1193 class GotoStmt : public Stmt {
1195 SourceLocation GotoLoc;
1196 SourceLocation LabelLoc;
1198 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1199 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1201 /// \brief Build an empty goto statement.
1202 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1204 LabelDecl *getLabel() const { return Label; }
1205 void setLabel(LabelDecl *D) { Label = D; }
1207 SourceLocation getGotoLoc() const { return GotoLoc; }
1208 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1209 SourceLocation getLabelLoc() const { return LabelLoc; }
1210 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1212 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1213 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1215 static bool classof(const Stmt *T) {
1216 return T->getStmtClass() == GotoStmtClass;
1220 child_range children() { return child_range(); }
1223 /// IndirectGotoStmt - This represents an indirect goto.
1225 class IndirectGotoStmt : public Stmt {
1226 SourceLocation GotoLoc;
1227 SourceLocation StarLoc;
1230 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1232 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1233 Target((Stmt*)target) {}
1235 /// \brief Build an empty indirect goto statement.
1236 explicit IndirectGotoStmt(EmptyShell Empty)
1237 : Stmt(IndirectGotoStmtClass, Empty) { }
1239 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1240 SourceLocation getGotoLoc() const { return GotoLoc; }
1241 void setStarLoc(SourceLocation L) { StarLoc = L; }
1242 SourceLocation getStarLoc() const { return StarLoc; }
1244 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1245 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1246 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1248 /// getConstantTarget - Returns the fixed target of this indirect
1249 /// goto, if one exists.
1250 LabelDecl *getConstantTarget();
1251 const LabelDecl *getConstantTarget() const {
1252 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1255 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1256 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1258 static bool classof(const Stmt *T) {
1259 return T->getStmtClass() == IndirectGotoStmtClass;
1263 child_range children() { return child_range(&Target, &Target+1); }
1267 /// ContinueStmt - This represents a continue.
1269 class ContinueStmt : public Stmt {
1270 SourceLocation ContinueLoc;
1272 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1274 /// \brief Build an empty continue statement.
1275 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1277 SourceLocation getContinueLoc() const { return ContinueLoc; }
1278 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1280 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1281 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1283 static bool classof(const Stmt *T) {
1284 return T->getStmtClass() == ContinueStmtClass;
1288 child_range children() { return child_range(); }
1291 /// BreakStmt - This represents a break.
1293 class BreakStmt : public Stmt {
1294 SourceLocation BreakLoc;
1296 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1298 /// \brief Build an empty break statement.
1299 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1301 SourceLocation getBreakLoc() const { return BreakLoc; }
1302 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1304 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1305 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1307 static bool classof(const Stmt *T) {
1308 return T->getStmtClass() == BreakStmtClass;
1312 child_range children() { return child_range(); }
1316 /// ReturnStmt - This represents a return, optionally of an expression:
1320 /// Note that GCC allows return with no argument in a function declared to
1321 /// return a value, and it allows returning a value in functions declared to
1322 /// return void. We explicitly model this in the AST, which means you can't
1323 /// depend on the return type of the function and the presence of an argument.
1325 class ReturnStmt : public Stmt {
1327 SourceLocation RetLoc;
1328 const VarDecl *NRVOCandidate;
1331 ReturnStmt(SourceLocation RL)
1332 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1334 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1335 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1336 NRVOCandidate(NRVOCandidate) {}
1338 /// \brief Build an empty return expression.
1339 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1341 const Expr *getRetValue() const;
1342 Expr *getRetValue();
1343 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1345 SourceLocation getReturnLoc() const { return RetLoc; }
1346 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1348 /// \brief Retrieve the variable that might be used for the named return
1349 /// value optimization.
1351 /// The optimization itself can only be performed if the variable is
1352 /// also marked as an NRVO object.
1353 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1354 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1356 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1357 SourceLocation getLocEnd() const LLVM_READONLY {
1358 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1361 static bool classof(const Stmt *T) {
1362 return T->getStmtClass() == ReturnStmtClass;
1366 child_range children() {
1367 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1368 return child_range();
1372 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1374 class AsmStmt : public Stmt {
1376 SourceLocation AsmLoc;
1377 /// \brief True if the assembly statement does not have any input or output
1381 /// \brief If true, treat this inline assembly as having side effects.
1382 /// This assembly statement should not be optimized, deleted or moved.
1385 unsigned NumOutputs;
1387 unsigned NumClobbers;
1389 IdentifierInfo **Names;
1392 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1393 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1394 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1395 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1398 /// \brief Build an empty inline-assembly statement.
1399 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1400 Stmt(SC, Empty), Names(0), Exprs(0) { }
1402 SourceLocation getAsmLoc() const { return AsmLoc; }
1403 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1405 bool isSimple() const { return IsSimple; }
1406 void setSimple(bool V) { IsSimple = V; }
1408 bool isVolatile() const { return IsVolatile; }
1409 void setVolatile(bool V) { IsVolatile = V; }
1411 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1412 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1414 //===--- Asm String Analysis ---===//
1416 /// Assemble final IR asm string.
1417 std::string generateAsmString(ASTContext &C) const;
1419 //===--- Output operands ---===//
1421 unsigned getNumOutputs() const { return NumOutputs; }
1423 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1427 StringRef getOutputName(unsigned i) const {
1428 if (IdentifierInfo *II = getOutputIdentifier(i))
1429 return II->getName();
1434 /// getOutputConstraint - Return the constraint string for the specified
1435 /// output operand. All output constraints are known to be non-empty (either
1437 StringRef getOutputConstraint(unsigned i) const;
1439 /// isOutputPlusConstraint - Return true if the specified output constraint
1440 /// is a "+" constraint (which is both an input and an output) or false if it
1441 /// is an "=" constraint (just an output).
1442 bool isOutputPlusConstraint(unsigned i) const {
1443 return getOutputConstraint(i)[0] == '+';
1446 const Expr *getOutputExpr(unsigned i) const;
1448 /// getNumPlusOperands - Return the number of output operands that have a "+"
1450 unsigned getNumPlusOperands() const;
1452 //===--- Input operands ---===//
1454 unsigned getNumInputs() const { return NumInputs; }
1456 IdentifierInfo *getInputIdentifier(unsigned i) const {
1457 return Names[i + NumOutputs];
1460 StringRef getInputName(unsigned i) const {
1461 if (IdentifierInfo *II = getInputIdentifier(i))
1462 return II->getName();
1467 /// getInputConstraint - Return the specified input constraint. Unlike output
1468 /// constraints, these can be empty.
1469 StringRef getInputConstraint(unsigned i) const;
1471 const Expr *getInputExpr(unsigned i) const;
1473 //===--- Other ---===//
1475 unsigned getNumClobbers() const { return NumClobbers; }
1476 StringRef getClobber(unsigned i) const;
1478 static bool classof(const Stmt *T) {
1479 return T->getStmtClass() == GCCAsmStmtClass ||
1480 T->getStmtClass() == MSAsmStmtClass;
1483 // Input expr iterators.
1485 typedef ExprIterator inputs_iterator;
1486 typedef ConstExprIterator const_inputs_iterator;
1488 inputs_iterator begin_inputs() {
1489 return &Exprs[0] + NumOutputs;
1492 inputs_iterator end_inputs() {
1493 return &Exprs[0] + NumOutputs + NumInputs;
1496 const_inputs_iterator begin_inputs() const {
1497 return &Exprs[0] + NumOutputs;
1500 const_inputs_iterator end_inputs() const {
1501 return &Exprs[0] + NumOutputs + NumInputs;
1504 // Output expr iterators.
1506 typedef ExprIterator outputs_iterator;
1507 typedef ConstExprIterator const_outputs_iterator;
1509 outputs_iterator begin_outputs() {
1512 outputs_iterator end_outputs() {
1513 return &Exprs[0] + NumOutputs;
1516 const_outputs_iterator begin_outputs() const {
1519 const_outputs_iterator end_outputs() const {
1520 return &Exprs[0] + NumOutputs;
1523 child_range children() {
1524 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1528 /// This represents a GCC inline-assembly statement extension.
1530 class GCCAsmStmt : public AsmStmt {
1531 SourceLocation RParenLoc;
1532 StringLiteral *AsmStr;
1534 // FIXME: If we wanted to, we could allocate all of these in one big array.
1535 StringLiteral **Constraints;
1536 StringLiteral **Clobbers;
1539 GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
1540 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1541 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1542 StringLiteral *asmstr, unsigned numclobbers,
1543 StringLiteral **clobbers, SourceLocation rparenloc);
1545 /// \brief Build an empty inline-assembly statement.
1546 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1547 Constraints(0), Clobbers(0) { }
1549 SourceLocation getRParenLoc() const { return RParenLoc; }
1550 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1552 //===--- Asm String Analysis ---===//
1554 const StringLiteral *getAsmString() const { return AsmStr; }
1555 StringLiteral *getAsmString() { return AsmStr; }
1556 void setAsmString(StringLiteral *E) { AsmStr = E; }
1558 /// AsmStringPiece - this is part of a decomposed asm string specification
1559 /// (for use with the AnalyzeAsmString function below). An asm string is
1560 /// considered to be a concatenation of these parts.
1561 class AsmStringPiece {
1564 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1565 Operand // Operand reference, with optional modifier %c4.
1572 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1573 AsmStringPiece(unsigned OpNo, char Modifier)
1574 : MyKind(Operand), Str(), OperandNo(OpNo) {
1578 bool isString() const { return MyKind == String; }
1579 bool isOperand() const { return MyKind == Operand; }
1581 const std::string &getString() const {
1586 unsigned getOperandNo() const {
1587 assert(isOperand());
1591 /// getModifier - Get the modifier for this operand, if present. This
1592 /// returns '\0' if there was no modifier.
1593 char getModifier() const {
1594 assert(isOperand());
1599 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1600 /// it into pieces. If the asm string is erroneous, emit errors and return
1601 /// true, otherwise return false. This handles canonicalization and
1602 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1603 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1604 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1605 ASTContext &C, unsigned &DiagOffs) const;
1607 /// Assemble final IR asm string.
1608 std::string generateAsmString(ASTContext &C) const;
1610 //===--- Output operands ---===//
1612 StringRef getOutputConstraint(unsigned i) const;
1614 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1615 return Constraints[i];
1617 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1618 return Constraints[i];
1621 Expr *getOutputExpr(unsigned i);
1623 const Expr *getOutputExpr(unsigned i) const {
1624 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1627 //===--- Input operands ---===//
1629 StringRef getInputConstraint(unsigned i) const;
1631 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1632 return Constraints[i + NumOutputs];
1634 StringLiteral *getInputConstraintLiteral(unsigned i) {
1635 return Constraints[i + NumOutputs];
1638 Expr *getInputExpr(unsigned i);
1639 void setInputExpr(unsigned i, Expr *E);
1641 const Expr *getInputExpr(unsigned i) const {
1642 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1645 void setOutputsAndInputsAndClobbers(ASTContext &C,
1646 IdentifierInfo **Names,
1647 StringLiteral **Constraints,
1649 unsigned NumOutputs,
1651 StringLiteral **Clobbers,
1652 unsigned NumClobbers);
1654 //===--- Other ---===//
1656 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1657 /// translate this into a numeric value needed to reference the same operand.
1658 /// This returns -1 if the operand name is invalid.
1659 int getNamedOperand(StringRef SymbolicName) const;
1661 StringRef getClobber(unsigned i) const;
1662 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1663 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1667 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1668 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1670 static bool classof(const Stmt *T) {
1671 return T->getStmtClass() == GCCAsmStmtClass;
1675 /// This represents a Microsoft inline-assembly statement extension.
1677 class MSAsmStmt : public AsmStmt {
1678 SourceLocation LBraceLoc, EndLoc;
1681 unsigned NumAsmToks;
1684 StringRef *Constraints;
1685 StringRef *Clobbers;
1688 MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1689 bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1690 unsigned numoutputs, unsigned numinputs,
1691 ArrayRef<IdentifierInfo*> names, ArrayRef<StringRef> constraints,
1692 ArrayRef<Expr*> exprs, StringRef asmstr,
1693 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1695 /// \brief Build an empty MS-style inline-assembly statement.
1696 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1697 NumAsmToks(0), AsmToks(0), Constraints(0), Clobbers(0) { }
1699 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1700 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1701 SourceLocation getEndLoc() const { return EndLoc; }
1702 void setEndLoc(SourceLocation L) { EndLoc = L; }
1704 bool hasBraces() const { return LBraceLoc.isValid(); }
1706 unsigned getNumAsmToks() { return NumAsmToks; }
1707 Token *getAsmToks() { return AsmToks; }
1709 //===--- Asm String Analysis ---===//
1711 const std::string *getAsmString() const { return &AsmStr; }
1712 std::string *getAsmString() { return &AsmStr; }
1713 void setAsmString(StringRef &E) { AsmStr = E.str(); }
1715 /// Assemble final IR asm string.
1716 std::string generateAsmString(ASTContext &C) const;
1718 //===--- Output operands ---===//
1720 StringRef getOutputConstraint(unsigned i) const {
1721 return Constraints[i];
1724 Expr *getOutputExpr(unsigned i);
1726 const Expr *getOutputExpr(unsigned i) const {
1727 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1730 //===--- Input operands ---===//
1732 StringRef getInputConstraint(unsigned i) const {
1733 return Constraints[i + NumOutputs];
1736 Expr *getInputExpr(unsigned i);
1737 void setInputExpr(unsigned i, Expr *E);
1739 const Expr *getInputExpr(unsigned i) const {
1740 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1743 //===--- Other ---===//
1745 StringRef getClobber(unsigned i) const { return Clobbers[i]; }
1747 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1748 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1750 static bool classof(const Stmt *T) {
1751 return T->getStmtClass() == MSAsmStmtClass;
1754 child_range children() {
1755 return child_range(&Exprs[0], &Exprs[0]);
1759 class SEHExceptStmt : public Stmt {
1763 enum { FILTER_EXPR, BLOCK };
1765 SEHExceptStmt(SourceLocation Loc,
1769 friend class ASTReader;
1770 friend class ASTStmtReader;
1771 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1774 static SEHExceptStmt* Create(ASTContext &C,
1775 SourceLocation ExceptLoc,
1779 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1780 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1782 SourceLocation getExceptLoc() const { return Loc; }
1783 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1785 Expr *getFilterExpr() const {
1786 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1789 CompoundStmt *getBlock() const {
1790 return cast<CompoundStmt>(Children[BLOCK]);
1793 child_range children() {
1794 return child_range(Children,Children+2);
1797 static bool classof(const Stmt *T) {
1798 return T->getStmtClass() == SEHExceptStmtClass;
1803 class SEHFinallyStmt : public Stmt {
1807 SEHFinallyStmt(SourceLocation Loc,
1810 friend class ASTReader;
1811 friend class ASTStmtReader;
1812 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1815 static SEHFinallyStmt* Create(ASTContext &C,
1816 SourceLocation FinallyLoc,
1819 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1820 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1822 SourceLocation getFinallyLoc() const { return Loc; }
1823 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1825 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1827 child_range children() {
1828 return child_range(&Block,&Block+1);
1831 static bool classof(const Stmt *T) {
1832 return T->getStmtClass() == SEHFinallyStmtClass;
1837 class SEHTryStmt : public Stmt {
1839 SourceLocation TryLoc;
1842 enum { TRY = 0, HANDLER = 1 };
1844 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1845 SourceLocation TryLoc,
1849 friend class ASTReader;
1850 friend class ASTStmtReader;
1851 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1854 static SEHTryStmt* Create(ASTContext &C,
1856 SourceLocation TryLoc,
1860 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1861 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1863 SourceLocation getTryLoc() const { return TryLoc; }
1864 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1866 bool getIsCXXTry() const { return IsCXXTry; }
1868 CompoundStmt* getTryBlock() const {
1869 return cast<CompoundStmt>(Children[TRY]);
1872 Stmt *getHandler() const { return Children[HANDLER]; }
1874 /// Returns 0 if not defined
1875 SEHExceptStmt *getExceptHandler() const;
1876 SEHFinallyStmt *getFinallyHandler() const;
1878 child_range children() {
1879 return child_range(Children,Children+2);
1882 static bool classof(const Stmt *T) {
1883 return T->getStmtClass() == SEHTryStmtClass;
1887 /// \brief This captures a statement into a function. For example, the following
1888 /// pragma annotated compound statement can be represented as a CapturedStmt,
1889 /// and this compound statement is the body of an anonymous outlined function.
1891 /// #pragma omp parallel
1896 class CapturedStmt : public Stmt {
1898 /// \brief The different capture forms: by 'this' or by reference, etc.
1899 enum VariableCaptureKind {
1904 /// \brief Describes the capture of either a variable or 'this'.
1910 /// \brief Create a new capture.
1912 /// \param Loc The source location associated with this capture.
1914 /// \param Kind The kind of capture (this, ByRef, ...).
1916 /// \param Var The variable being captured, or null if capturing this.
1918 Capture(SourceLocation Loc, VariableCaptureKind Kind, VarDecl *Var = 0)
1919 : Var(Var), Loc(Loc) {
1922 assert(Var == 0 && "'this' capture cannot have a variable!");
1925 assert(Var && "capturing by reference must have a variable!");
1930 /// \brief Determine the kind of capture.
1931 VariableCaptureKind getCaptureKind() const {
1938 /// \brief Retrieve the source location at which the variable or 'this' was
1940 SourceLocation getLocation() const { return Loc; }
1942 /// \brief Determine whether this capture handles the C++ 'this' pointer.
1943 bool capturesThis() const { return Var == 0; }
1945 /// \brief Determine whether this capture handles a variable.
1946 bool capturesVariable() const { return Var != 0; }
1948 /// \brief Retrieve the declaration of the variable being captured.
1950 /// This operation is only valid if this capture does not capture 'this'.
1951 VarDecl *getCapturedVar() const {
1952 assert(!capturesThis() && "No variable available for 'this' capture");
1958 /// \brief The number of variable captured, including 'this'.
1959 unsigned NumCaptures;
1961 /// \brief The implicit outlined function.
1962 FunctionDecl *TheFuncDecl;
1964 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
1965 RecordDecl *TheRecordDecl;
1967 /// \brief Construct a captured statement.
1968 CapturedStmt(Stmt *S, ArrayRef<Capture> Captures,
1969 ArrayRef<Expr *> CaptureInits,
1970 FunctionDecl *FD, RecordDecl *RD);
1972 /// \brief Construct an empty captured statement.
1973 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
1975 Stmt **getStoredStmts() const {
1976 return reinterpret_cast<Stmt **>(const_cast<CapturedStmt *>(this) + 1);
1979 Capture *getStoredCaptures() const;
1982 static CapturedStmt *Create(ASTContext &Context, Stmt *S,
1983 ArrayRef<Capture> Captures,
1984 ArrayRef<Expr *> CaptureInits,
1985 FunctionDecl *FD, RecordDecl *RD);
1987 static CapturedStmt *CreateDeserialized(ASTContext &Context,
1988 unsigned NumCaptures);
1990 /// \brief Retrieve the statement being captured.
1991 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
1992 const Stmt *getCapturedStmt() const {
1993 return const_cast<CapturedStmt *>(this)->getCapturedStmt();
1996 /// \brief Retrieve the outlined function declaration.
1997 const FunctionDecl *getCapturedFunctionDecl() const { return TheFuncDecl; }
1999 /// \brief Retrieve the record declaration for captured variables.
2000 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2002 /// \brief True if this variable has been captured.
2003 bool capturesVariable(const VarDecl *Var) const;
2005 /// \brief An iterator that walks over the captures.
2006 typedef const Capture *capture_iterator;
2008 /// \brief Retrieve an iterator pointing to the first capture.
2009 capture_iterator capture_begin() const { return getStoredCaptures(); }
2011 /// \brief Retrieve an iterator pointing past the end of the sequence of
2013 capture_iterator capture_end() const {
2014 return getStoredCaptures() + NumCaptures;
2017 /// \brief Retrieve the number of captures, including 'this'.
2018 unsigned capture_size() const { return NumCaptures; }
2020 /// \brief Iterator that walks over the capture initialization arguments.
2021 typedef Expr **capture_init_iterator;
2023 /// \brief Retrieve the first initialization argument.
2024 capture_init_iterator capture_init_begin() const {
2025 return reinterpret_cast<Expr **>(getStoredStmts());
2028 /// \brief Retrieve the iterator pointing one past the last initialization
2030 capture_init_iterator capture_init_end() const {
2031 return capture_init_begin() + NumCaptures;
2034 SourceLocation getLocStart() const LLVM_READONLY {
2035 return getCapturedStmt()->getLocStart();
2037 SourceLocation getLocEnd() const LLVM_READONLY {
2038 return getCapturedStmt()->getLocEnd();
2040 SourceRange getSourceRange() const LLVM_READONLY {
2041 return getCapturedStmt()->getSourceRange();
2044 static bool classof(const Stmt *T) {
2045 return T->getStmtClass() == CapturedStmtClass;
2048 child_range children();
2051 } // end namespace clang