1 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Stmt interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
17 #include "clang/AST/DeclGroup.h"
18 #include "clang/AST/StmtIterator.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/Basic/SourceLocation.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/ErrorHandling.h"
28 class FoldingSetNodeID;
40 struct PrintingPolicy;
48 //===--------------------------------------------------------------------===//
49 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
50 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
51 // references to children (to be compatible with StmtIterator).
52 //===--------------------------------------------------------------------===//
60 ExprIterator(Stmt** i) : I(i) {}
61 ExprIterator() : I(0) {}
62 ExprIterator& operator++() { ++I; return *this; }
63 ExprIterator operator-(size_t i) { return I-i; }
64 ExprIterator operator+(size_t i) { return I+i; }
65 Expr* operator[](size_t idx);
66 // FIXME: Verify that this will correctly return a signed distance.
67 signed operator-(const ExprIterator& R) const { return I - R.I; }
68 Expr* operator*() const;
69 Expr* operator->() const;
70 bool operator==(const ExprIterator& R) const { return I == R.I; }
71 bool operator!=(const ExprIterator& R) const { return I != R.I; }
72 bool operator>(const ExprIterator& R) const { return I > R.I; }
73 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
76 class ConstExprIterator {
77 const Stmt * const *I;
79 ConstExprIterator(const Stmt * const *i) : I(i) {}
80 ConstExprIterator() : I(0) {}
81 ConstExprIterator& operator++() { ++I; return *this; }
82 ConstExprIterator operator+(size_t i) const { return I+i; }
83 ConstExprIterator operator-(size_t i) const { return I-i; }
84 const Expr * operator[](size_t idx) const;
85 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
86 const Expr * operator*() const;
87 const Expr * operator->() const;
88 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
89 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
90 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
91 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
94 //===----------------------------------------------------------------------===//
95 // AST classes for statements.
96 //===----------------------------------------------------------------------===//
98 /// Stmt - This represents one statement.
104 #define STMT(CLASS, PARENT) CLASS##Class,
105 #define STMT_RANGE(BASE, FIRST, LAST) \
106 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
107 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
108 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
109 #define ABSTRACT_STMT(STMT)
110 #include "clang/AST/StmtNodes.inc"
113 // Make vanilla 'new' and 'delete' illegal for Stmts.
115 void* operator new(size_t bytes) throw() {
116 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
118 void operator delete(void* data) throw() {
119 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
122 class StmtBitfields {
125 /// \brief The statement class.
128 enum { NumStmtBits = 8 };
130 class CompoundStmtBitfields {
131 friend class CompoundStmt;
132 unsigned : NumStmtBits;
134 unsigned NumStmts : 32 - NumStmtBits;
137 class ExprBitfields {
139 friend class DeclRefExpr; // computeDependence
140 friend class InitListExpr; // ctor
141 friend class DesignatedInitExpr; // ctor
142 friend class BlockDeclRefExpr; // ctor
143 friend class ASTStmtReader; // deserialization
144 friend class CXXNewExpr; // ctor
145 friend class DependentScopeDeclRefExpr; // ctor
146 friend class CXXConstructExpr; // ctor
147 friend class CallExpr; // ctor
148 friend class OffsetOfExpr; // ctor
149 friend class ObjCMessageExpr; // ctor
150 friend class ObjCArrayLiteral; // ctor
151 friend class ObjCDictionaryLiteral; // ctor
152 friend class ShuffleVectorExpr; // ctor
153 friend class ParenListExpr; // ctor
154 friend class CXXUnresolvedConstructExpr; // ctor
155 friend class CXXDependentScopeMemberExpr; // ctor
156 friend class OverloadExpr; // ctor
157 friend class PseudoObjectExpr; // ctor
158 friend class AtomicExpr; // ctor
159 unsigned : NumStmtBits;
161 unsigned ValueKind : 2;
162 unsigned ObjectKind : 2;
163 unsigned TypeDependent : 1;
164 unsigned ValueDependent : 1;
165 unsigned InstantiationDependent : 1;
166 unsigned ContainsUnexpandedParameterPack : 1;
168 enum { NumExprBits = 16 };
170 class CharacterLiteralBitfields {
171 friend class CharacterLiteral;
172 unsigned : NumExprBits;
177 enum APFloatSemantics {
186 class FloatingLiteralBitfields {
187 friend class FloatingLiteral;
188 unsigned : NumExprBits;
190 unsigned Semantics : 3; // Provides semantics for APFloat construction
191 unsigned IsExact : 1;
194 class UnaryExprOrTypeTraitExprBitfields {
195 friend class UnaryExprOrTypeTraitExpr;
196 unsigned : NumExprBits;
199 unsigned IsType : 1; // true if operand is a type, false if an expression.
202 class DeclRefExprBitfields {
203 friend class DeclRefExpr;
204 friend class ASTStmtReader; // deserialization
205 unsigned : NumExprBits;
207 unsigned HasQualifier : 1;
208 unsigned HasTemplateKWAndArgsInfo : 1;
209 unsigned HasFoundDecl : 1;
210 unsigned HadMultipleCandidates : 1;
211 unsigned RefersToEnclosingLocal : 1;
214 class CastExprBitfields {
215 friend class CastExpr;
216 unsigned : NumExprBits;
219 unsigned BasePathSize : 32 - 6 - NumExprBits;
222 class CallExprBitfields {
223 friend class CallExpr;
224 unsigned : NumExprBits;
226 unsigned NumPreArgs : 1;
229 class ExprWithCleanupsBitfields {
230 friend class ExprWithCleanups;
231 friend class ASTStmtReader; // deserialization
233 unsigned : NumExprBits;
235 unsigned NumObjects : 32 - NumExprBits;
238 class PseudoObjectExprBitfields {
239 friend class PseudoObjectExpr;
240 friend class ASTStmtReader; // deserialization
242 unsigned : NumExprBits;
244 // These don't need to be particularly wide, because they're
245 // strictly limited by the forms of expressions we permit.
246 unsigned NumSubExprs : 8;
247 unsigned ResultIndex : 32 - 8 - NumExprBits;
250 class ObjCIndirectCopyRestoreExprBitfields {
251 friend class ObjCIndirectCopyRestoreExpr;
252 unsigned : NumExprBits;
254 unsigned ShouldCopy : 1;
257 class InitListExprBitfields {
258 friend class InitListExpr;
260 unsigned : NumExprBits;
262 /// Whether this initializer list originally had a GNU array-range
263 /// designator in it. This is a temporary marker used by CodeGen.
264 unsigned HadArrayRangeDesignator : 1;
266 /// Whether this initializer list initializes a std::initializer_list
268 unsigned InitializesStdInitializerList : 1;
271 class TypeTraitExprBitfields {
272 friend class TypeTraitExpr;
273 friend class ASTStmtReader;
274 friend class ASTStmtWriter;
276 unsigned : NumExprBits;
278 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
281 /// \brief If this expression is not value-dependent, this indicates whether
282 /// the trait evaluated true or false.
285 /// \brief The number of arguments to this type trait.
286 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
290 // FIXME: this is wasteful on 64-bit platforms.
293 StmtBitfields StmtBits;
294 CompoundStmtBitfields CompoundStmtBits;
295 ExprBitfields ExprBits;
296 CharacterLiteralBitfields CharacterLiteralBits;
297 FloatingLiteralBitfields FloatingLiteralBits;
298 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
299 DeclRefExprBitfields DeclRefExprBits;
300 CastExprBitfields CastExprBits;
301 CallExprBitfields CallExprBits;
302 ExprWithCleanupsBitfields ExprWithCleanupsBits;
303 PseudoObjectExprBitfields PseudoObjectExprBits;
304 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
305 InitListExprBitfields InitListExprBits;
306 TypeTraitExprBitfields TypeTraitExprBits;
309 friend class ASTStmtReader;
310 friend class ASTStmtWriter;
313 // Only allow allocation of Stmts using the allocator in ASTContext
314 // or by doing a placement new.
315 void* operator new(size_t bytes, ASTContext& C,
316 unsigned alignment = 8) throw();
318 void* operator new(size_t bytes, ASTContext* C,
319 unsigned alignment = 8) throw();
321 void* operator new(size_t bytes, void* mem) throw() {
325 void operator delete(void*, ASTContext&, unsigned) throw() { }
326 void operator delete(void*, ASTContext*, unsigned) throw() { }
327 void operator delete(void*, std::size_t) throw() { }
328 void operator delete(void*, void*) throw() { }
331 /// \brief A placeholder type used to construct an empty shell of a
332 /// type, that will be filled in later (e.g., by some
333 /// de-serialization).
334 struct EmptyShell { };
337 /// \brief Whether statistic collection is enabled.
338 static bool StatisticsEnabled;
341 /// \brief Construct an empty statement.
342 explicit Stmt(StmtClass SC, EmptyShell) {
343 StmtBits.sClass = SC;
344 if (StatisticsEnabled) Stmt::addStmtClass(SC);
349 StmtBits.sClass = SC;
350 if (StatisticsEnabled) Stmt::addStmtClass(SC);
353 StmtClass getStmtClass() const {
354 return static_cast<StmtClass>(StmtBits.sClass);
356 const char *getStmtClassName() const;
358 /// SourceLocation tokens are not useful in isolation - they are low level
359 /// value objects created/interpreted by SourceManager. We assume AST
360 /// clients will have a pointer to the respective SourceManager.
361 SourceRange getSourceRange() const LLVM_READONLY;
362 SourceLocation getLocStart() const LLVM_READONLY;
363 SourceLocation getLocEnd() const LLVM_READONLY;
365 // global temp stats (until we have a per-module visitor)
366 static void addStmtClass(const StmtClass s);
367 static void EnableStatistics();
368 static void PrintStats();
370 /// \brief Dumps the specified AST fragment and all subtrees to
372 LLVM_ATTRIBUTE_USED void dump() const;
373 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
374 void dump(raw_ostream &OS, SourceManager &SM) const;
376 /// dumpColor - same as dump(), but forces color highlighting.
377 LLVM_ATTRIBUTE_USED void dumpColor() const;
379 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
380 /// back to its original source language syntax.
381 void dumpPretty(ASTContext &Context) const;
382 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
383 const PrintingPolicy &Policy,
384 unsigned Indentation = 0) const;
386 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
387 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
388 void viewAST() const;
390 /// Skip past any implicit AST nodes which might surround this
391 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
392 Stmt *IgnoreImplicit();
394 const Stmt *stripLabelLikeStatements() const;
395 Stmt *stripLabelLikeStatements() {
396 return const_cast<Stmt*>(
397 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
400 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
401 /// contain implicit control-flow in the order their subexpressions
402 /// are evaluated. This predicate returns true if this statement has
403 /// such implicit control-flow. Such statements are also specially handled
405 bool hasImplicitControlFlow() const;
407 /// Child Iterators: All subclasses must implement 'children'
408 /// to permit easy iteration over the substatements/subexpessions of an
409 /// AST node. This permits easy iteration over all nodes in the AST.
410 typedef StmtIterator child_iterator;
411 typedef ConstStmtIterator const_child_iterator;
413 typedef StmtRange child_range;
414 typedef ConstStmtRange const_child_range;
416 child_range children();
417 const_child_range children() const {
418 return const_cast<Stmt*>(this)->children();
421 child_iterator child_begin() { return children().first; }
422 child_iterator child_end() { return children().second; }
424 const_child_iterator child_begin() const { return children().first; }
425 const_child_iterator child_end() const { return children().second; }
427 /// \brief Produce a unique representation of the given statement.
429 /// \param ID once the profiling operation is complete, will contain
430 /// the unique representation of the given statement.
432 /// \param Context the AST context in which the statement resides
434 /// \param Canonical whether the profile should be based on the canonical
435 /// representation of this statement (e.g., where non-type template
436 /// parameters are identified by index/level rather than their
437 /// declaration pointers) or the exact representation of the statement as
438 /// written in the source.
439 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
440 bool Canonical) const;
443 /// DeclStmt - Adaptor class for mixing declarations with statements and
444 /// expressions. For example, CompoundStmt mixes statements, expressions
445 /// and declarations (variables, types). Another example is ForStmt, where
446 /// the first statement can be an expression or a declaration.
448 class DeclStmt : public Stmt {
450 SourceLocation StartLoc, EndLoc;
453 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
454 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
455 StartLoc(startLoc), EndLoc(endLoc) {}
457 /// \brief Build an empty declaration statement.
458 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
460 /// isSingleDecl - This method returns true if this DeclStmt refers
461 /// to a single Decl.
462 bool isSingleDecl() const {
463 return DG.isSingleDecl();
466 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
467 Decl *getSingleDecl() { return DG.getSingleDecl(); }
469 const DeclGroupRef getDeclGroup() const { return DG; }
470 DeclGroupRef getDeclGroup() { return DG; }
471 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
473 SourceLocation getStartLoc() const { return StartLoc; }
474 void setStartLoc(SourceLocation L) { StartLoc = L; }
475 SourceLocation getEndLoc() const { return EndLoc; }
476 void setEndLoc(SourceLocation L) { EndLoc = L; }
478 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
479 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
481 static bool classof(const Stmt *T) {
482 return T->getStmtClass() == DeclStmtClass;
485 // Iterators over subexpressions.
486 child_range children() {
487 return child_range(child_iterator(DG.begin(), DG.end()),
488 child_iterator(DG.end(), DG.end()));
491 typedef DeclGroupRef::iterator decl_iterator;
492 typedef DeclGroupRef::const_iterator const_decl_iterator;
494 decl_iterator decl_begin() { return DG.begin(); }
495 decl_iterator decl_end() { return DG.end(); }
496 const_decl_iterator decl_begin() const { return DG.begin(); }
497 const_decl_iterator decl_end() const { return DG.end(); }
499 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
500 reverse_decl_iterator decl_rbegin() {
501 return reverse_decl_iterator(decl_end());
503 reverse_decl_iterator decl_rend() {
504 return reverse_decl_iterator(decl_begin());
508 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
510 class NullStmt : public Stmt {
511 SourceLocation SemiLoc;
513 /// \brief True if the null statement was preceded by an empty macro, e.g:
518 bool HasLeadingEmptyMacro;
520 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
521 : Stmt(NullStmtClass), SemiLoc(L),
522 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
524 /// \brief Build an empty null statement.
525 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
526 HasLeadingEmptyMacro(false) { }
528 SourceLocation getSemiLoc() const { return SemiLoc; }
529 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
531 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
533 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
534 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
536 static bool classof(const Stmt *T) {
537 return T->getStmtClass() == NullStmtClass;
540 child_range children() { return child_range(); }
542 friend class ASTStmtReader;
543 friend class ASTStmtWriter;
546 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
548 class CompoundStmt : public Stmt {
550 SourceLocation LBracLoc, RBracLoc;
552 CompoundStmt(ASTContext &C, ArrayRef<Stmt*> Stmts,
553 SourceLocation LB, SourceLocation RB);
555 // \brief Build an empty compound statment with a location.
556 explicit CompoundStmt(SourceLocation Loc)
557 : Stmt(CompoundStmtClass), Body(0), LBracLoc(Loc), RBracLoc(Loc) {
558 CompoundStmtBits.NumStmts = 0;
561 // \brief Build an empty compound statement.
562 explicit CompoundStmt(EmptyShell Empty)
563 : Stmt(CompoundStmtClass, Empty), Body(0) {
564 CompoundStmtBits.NumStmts = 0;
567 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
569 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
570 unsigned size() const { return CompoundStmtBits.NumStmts; }
572 typedef Stmt** body_iterator;
573 body_iterator body_begin() { return Body; }
574 body_iterator body_end() { return Body + size(); }
575 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
577 void setLastStmt(Stmt *S) {
578 assert(!body_empty() && "setLastStmt");
582 typedef Stmt* const * const_body_iterator;
583 const_body_iterator body_begin() const { return Body; }
584 const_body_iterator body_end() const { return Body + size(); }
585 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
587 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
588 reverse_body_iterator body_rbegin() {
589 return reverse_body_iterator(body_end());
591 reverse_body_iterator body_rend() {
592 return reverse_body_iterator(body_begin());
595 typedef std::reverse_iterator<const_body_iterator>
596 const_reverse_body_iterator;
598 const_reverse_body_iterator body_rbegin() const {
599 return const_reverse_body_iterator(body_end());
602 const_reverse_body_iterator body_rend() const {
603 return const_reverse_body_iterator(body_begin());
606 SourceLocation getLocStart() const LLVM_READONLY { return LBracLoc; }
607 SourceLocation getLocEnd() const LLVM_READONLY { return RBracLoc; }
609 SourceLocation getLBracLoc() const { return LBracLoc; }
610 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
611 SourceLocation getRBracLoc() const { return RBracLoc; }
612 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
614 static bool classof(const Stmt *T) {
615 return T->getStmtClass() == CompoundStmtClass;
619 child_range children() {
620 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
623 const_child_range children() const {
624 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
628 // SwitchCase is the base class for CaseStmt and DefaultStmt,
629 class SwitchCase : public Stmt {
631 // A pointer to the following CaseStmt or DefaultStmt class,
632 // used by SwitchStmt.
633 SwitchCase *NextSwitchCase;
634 SourceLocation KeywordLoc;
635 SourceLocation ColonLoc;
637 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
638 : Stmt(SC), NextSwitchCase(0), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
640 SwitchCase(StmtClass SC, EmptyShell)
641 : Stmt(SC), NextSwitchCase(0) {}
644 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
646 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
648 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
650 SourceLocation getKeywordLoc() const { return KeywordLoc; }
651 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
652 SourceLocation getColonLoc() const { return ColonLoc; }
653 void setColonLoc(SourceLocation L) { ColonLoc = L; }
656 const Stmt *getSubStmt() const {
657 return const_cast<SwitchCase*>(this)->getSubStmt();
660 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
661 SourceLocation getLocEnd() const LLVM_READONLY;
663 static bool classof(const Stmt *T) {
664 return T->getStmtClass() == CaseStmtClass ||
665 T->getStmtClass() == DefaultStmtClass;
669 class CaseStmt : public SwitchCase {
670 enum { LHS, RHS, SUBSTMT, END_EXPR };
671 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
672 // GNU "case 1 ... 4" extension
673 SourceLocation EllipsisLoc;
675 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
676 SourceLocation ellipsisLoc, SourceLocation colonLoc)
677 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
678 SubExprs[SUBSTMT] = 0;
679 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
680 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
681 EllipsisLoc = ellipsisLoc;
684 /// \brief Build an empty switch case statement.
685 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { }
687 SourceLocation getCaseLoc() const { return KeywordLoc; }
688 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
689 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
690 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
691 SourceLocation getColonLoc() const { return ColonLoc; }
692 void setColonLoc(SourceLocation L) { ColonLoc = L; }
694 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
695 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
696 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
698 const Expr *getLHS() const {
699 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
701 const Expr *getRHS() const {
702 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
704 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
706 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
707 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
708 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
710 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
711 SourceLocation getLocEnd() const LLVM_READONLY {
712 // Handle deeply nested case statements with iteration instead of recursion.
713 const CaseStmt *CS = this;
714 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
717 return CS->getSubStmt()->getLocEnd();
720 static bool classof(const Stmt *T) {
721 return T->getStmtClass() == CaseStmtClass;
725 child_range children() {
726 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
730 class DefaultStmt : public SwitchCase {
733 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
734 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
736 /// \brief Build an empty default statement.
737 explicit DefaultStmt(EmptyShell Empty)
738 : SwitchCase(DefaultStmtClass, Empty) { }
740 Stmt *getSubStmt() { return SubStmt; }
741 const Stmt *getSubStmt() const { return SubStmt; }
742 void setSubStmt(Stmt *S) { SubStmt = S; }
744 SourceLocation getDefaultLoc() const { return KeywordLoc; }
745 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
746 SourceLocation getColonLoc() const { return ColonLoc; }
747 void setColonLoc(SourceLocation L) { ColonLoc = L; }
749 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
750 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
752 static bool classof(const Stmt *T) {
753 return T->getStmtClass() == DefaultStmtClass;
757 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
760 inline SourceLocation SwitchCase::getLocEnd() const {
761 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
762 return CS->getLocEnd();
763 return cast<DefaultStmt>(this)->getLocEnd();
766 /// LabelStmt - Represents a label, which has a substatement. For example:
769 class LabelStmt : public Stmt {
772 SourceLocation IdentLoc;
774 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
775 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
778 // \brief Build an empty label statement.
779 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
781 SourceLocation getIdentLoc() const { return IdentLoc; }
782 LabelDecl *getDecl() const { return TheDecl; }
783 void setDecl(LabelDecl *D) { TheDecl = D; }
784 const char *getName() const;
785 Stmt *getSubStmt() { return SubStmt; }
786 const Stmt *getSubStmt() const { return SubStmt; }
787 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
788 void setSubStmt(Stmt *SS) { SubStmt = SS; }
790 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
791 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
793 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
795 static bool classof(const Stmt *T) {
796 return T->getStmtClass() == LabelStmtClass;
801 /// \brief Represents an attribute applied to a statement.
803 /// Represents an attribute applied to a statement. For example:
804 /// [[omp::for(...)]] for (...) { ... }
806 class AttributedStmt : public Stmt {
808 SourceLocation AttrLoc;
810 const Attr *Attrs[1];
812 friend class ASTStmtReader;
814 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
815 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
816 NumAttrs(Attrs.size()) {
817 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*));
820 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
821 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
822 memset(Attrs, 0, NumAttrs * sizeof(Attr*));
826 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc,
827 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
828 // \brief Build an empty attributed statement.
829 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs);
831 SourceLocation getAttrLoc() const { return AttrLoc; }
832 ArrayRef<const Attr*> getAttrs() const {
833 return ArrayRef<const Attr*>(Attrs, NumAttrs);
835 Stmt *getSubStmt() { return SubStmt; }
836 const Stmt *getSubStmt() const { return SubStmt; }
838 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
839 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
841 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
843 static bool classof(const Stmt *T) {
844 return T->getStmtClass() == AttributedStmtClass;
849 /// IfStmt - This represents an if/then/else.
851 class IfStmt : public Stmt {
852 enum { VAR, COND, THEN, ELSE, END_EXPR };
853 Stmt* SubExprs[END_EXPR];
855 SourceLocation IfLoc;
856 SourceLocation ElseLoc;
859 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
860 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
862 /// \brief Build an empty if/then/else statement
863 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
865 /// \brief Retrieve the variable declared in this "if" statement, if any.
867 /// In the following example, "x" is the condition variable.
869 /// if (int x = foo()) {
870 /// printf("x is %d", x);
873 VarDecl *getConditionVariable() const;
874 void setConditionVariable(ASTContext &C, VarDecl *V);
876 /// If this IfStmt has a condition variable, return the faux DeclStmt
877 /// associated with the creation of that condition variable.
878 const DeclStmt *getConditionVariableDeclStmt() const {
879 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
882 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
883 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
884 const Stmt *getThen() const { return SubExprs[THEN]; }
885 void setThen(Stmt *S) { SubExprs[THEN] = S; }
886 const Stmt *getElse() const { return SubExprs[ELSE]; }
887 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
889 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
890 Stmt *getThen() { return SubExprs[THEN]; }
891 Stmt *getElse() { return SubExprs[ELSE]; }
893 SourceLocation getIfLoc() const { return IfLoc; }
894 void setIfLoc(SourceLocation L) { IfLoc = L; }
895 SourceLocation getElseLoc() const { return ElseLoc; }
896 void setElseLoc(SourceLocation L) { ElseLoc = L; }
898 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
899 SourceLocation getLocEnd() const LLVM_READONLY {
901 return SubExprs[ELSE]->getLocEnd();
903 return SubExprs[THEN]->getLocEnd();
906 // Iterators over subexpressions. The iterators will include iterating
907 // over the initialization expression referenced by the condition variable.
908 child_range children() {
909 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
912 static bool classof(const Stmt *T) {
913 return T->getStmtClass() == IfStmtClass;
917 /// SwitchStmt - This represents a 'switch' stmt.
919 class SwitchStmt : public Stmt {
920 enum { VAR, COND, BODY, END_EXPR };
921 Stmt* SubExprs[END_EXPR];
922 // This points to a linked list of case and default statements.
923 SwitchCase *FirstCase;
924 SourceLocation SwitchLoc;
926 /// If the SwitchStmt is a switch on an enum value, this records whether
927 /// all the enum values were covered by CaseStmts. This value is meant to
928 /// be a hint for possible clients.
929 unsigned AllEnumCasesCovered : 1;
932 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
934 /// \brief Build a empty switch statement.
935 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
937 /// \brief Retrieve the variable declared in this "switch" statement, if any.
939 /// In the following example, "x" is the condition variable.
941 /// switch (int x = foo()) {
946 VarDecl *getConditionVariable() const;
947 void setConditionVariable(ASTContext &C, VarDecl *V);
949 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
950 /// associated with the creation of that condition variable.
951 const DeclStmt *getConditionVariableDeclStmt() const {
952 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
955 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
956 const Stmt *getBody() const { return SubExprs[BODY]; }
957 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
959 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
960 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
961 Stmt *getBody() { return SubExprs[BODY]; }
962 void setBody(Stmt *S) { SubExprs[BODY] = S; }
963 SwitchCase *getSwitchCaseList() { return FirstCase; }
965 /// \brief Set the case list for this switch statement.
967 /// The caller is responsible for incrementing the retain counts on
968 /// all of the SwitchCase statements in this list.
969 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
971 SourceLocation getSwitchLoc() const { return SwitchLoc; }
972 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
974 void setBody(Stmt *S, SourceLocation SL) {
978 void addSwitchCase(SwitchCase *SC) {
979 assert(!SC->getNextSwitchCase()
980 && "case/default already added to a switch");
981 SC->setNextSwitchCase(FirstCase);
985 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
986 /// switch over an enum value then all cases have been explicitly covered.
987 void setAllEnumCasesCovered() {
988 AllEnumCasesCovered = 1;
991 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
992 /// have been explicitly covered.
993 bool isAllEnumCasesCovered() const {
994 return (bool) AllEnumCasesCovered;
997 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
998 SourceLocation getLocEnd() const LLVM_READONLY {
999 return SubExprs[BODY]->getLocEnd();
1003 child_range children() {
1004 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1007 static bool classof(const Stmt *T) {
1008 return T->getStmtClass() == SwitchStmtClass;
1013 /// WhileStmt - This represents a 'while' stmt.
1015 class WhileStmt : public Stmt {
1016 enum { VAR, COND, BODY, END_EXPR };
1017 Stmt* SubExprs[END_EXPR];
1018 SourceLocation WhileLoc;
1020 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1023 /// \brief Build an empty while statement.
1024 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1026 /// \brief Retrieve the variable declared in this "while" statement, if any.
1028 /// In the following example, "x" is the condition variable.
1030 /// while (int x = random()) {
1034 VarDecl *getConditionVariable() const;
1035 void setConditionVariable(ASTContext &C, VarDecl *V);
1037 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1038 /// associated with the creation of that condition variable.
1039 const DeclStmt *getConditionVariableDeclStmt() const {
1040 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1043 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1044 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1045 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1046 Stmt *getBody() { return SubExprs[BODY]; }
1047 const Stmt *getBody() const { return SubExprs[BODY]; }
1048 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1050 SourceLocation getWhileLoc() const { return WhileLoc; }
1051 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1053 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1054 SourceLocation getLocEnd() const LLVM_READONLY {
1055 return SubExprs[BODY]->getLocEnd();
1058 static bool classof(const Stmt *T) {
1059 return T->getStmtClass() == WhileStmtClass;
1063 child_range children() {
1064 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1068 /// DoStmt - This represents a 'do/while' stmt.
1070 class DoStmt : public Stmt {
1071 enum { BODY, COND, END_EXPR };
1072 Stmt* SubExprs[END_EXPR];
1073 SourceLocation DoLoc;
1074 SourceLocation WhileLoc;
1075 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1078 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1080 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1081 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1082 SubExprs[BODY] = body;
1085 /// \brief Build an empty do-while statement.
1086 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1088 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1089 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1090 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1091 Stmt *getBody() { return SubExprs[BODY]; }
1092 const Stmt *getBody() const { return SubExprs[BODY]; }
1093 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1095 SourceLocation getDoLoc() const { return DoLoc; }
1096 void setDoLoc(SourceLocation L) { DoLoc = L; }
1097 SourceLocation getWhileLoc() const { return WhileLoc; }
1098 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1100 SourceLocation getRParenLoc() const { return RParenLoc; }
1101 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1103 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1104 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1106 static bool classof(const Stmt *T) {
1107 return T->getStmtClass() == DoStmtClass;
1111 child_range children() {
1112 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1117 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1118 /// the init/cond/inc parts of the ForStmt will be null if they were not
1119 /// specified in the source.
1121 class ForStmt : public Stmt {
1122 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1123 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1124 SourceLocation ForLoc;
1125 SourceLocation LParenLoc, RParenLoc;
1128 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1129 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1131 /// \brief Build an empty for statement.
1132 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1134 Stmt *getInit() { return SubExprs[INIT]; }
1136 /// \brief Retrieve the variable declared in this "for" statement, if any.
1138 /// In the following example, "y" is the condition variable.
1140 /// for (int x = random(); int y = mangle(x); ++x) {
1144 VarDecl *getConditionVariable() const;
1145 void setConditionVariable(ASTContext &C, VarDecl *V);
1147 /// If this ForStmt has a condition variable, return the faux DeclStmt
1148 /// associated with the creation of that condition variable.
1149 const DeclStmt *getConditionVariableDeclStmt() const {
1150 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1153 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1154 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1155 Stmt *getBody() { return SubExprs[BODY]; }
1157 const Stmt *getInit() const { return SubExprs[INIT]; }
1158 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1159 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1160 const Stmt *getBody() const { return SubExprs[BODY]; }
1162 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1163 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1164 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1165 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1167 SourceLocation getForLoc() const { return ForLoc; }
1168 void setForLoc(SourceLocation L) { ForLoc = L; }
1169 SourceLocation getLParenLoc() const { return LParenLoc; }
1170 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1171 SourceLocation getRParenLoc() const { return RParenLoc; }
1172 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1174 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1175 SourceLocation getLocEnd() const LLVM_READONLY {
1176 return SubExprs[BODY]->getLocEnd();
1179 static bool classof(const Stmt *T) {
1180 return T->getStmtClass() == ForStmtClass;
1184 child_range children() {
1185 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1189 /// GotoStmt - This represents a direct goto.
1191 class GotoStmt : public Stmt {
1193 SourceLocation GotoLoc;
1194 SourceLocation LabelLoc;
1196 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1197 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1199 /// \brief Build an empty goto statement.
1200 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1202 LabelDecl *getLabel() const { return Label; }
1203 void setLabel(LabelDecl *D) { Label = D; }
1205 SourceLocation getGotoLoc() const { return GotoLoc; }
1206 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1207 SourceLocation getLabelLoc() const { return LabelLoc; }
1208 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1210 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1211 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1213 static bool classof(const Stmt *T) {
1214 return T->getStmtClass() == GotoStmtClass;
1218 child_range children() { return child_range(); }
1221 /// IndirectGotoStmt - This represents an indirect goto.
1223 class IndirectGotoStmt : public Stmt {
1224 SourceLocation GotoLoc;
1225 SourceLocation StarLoc;
1228 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1230 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1231 Target((Stmt*)target) {}
1233 /// \brief Build an empty indirect goto statement.
1234 explicit IndirectGotoStmt(EmptyShell Empty)
1235 : Stmt(IndirectGotoStmtClass, Empty) { }
1237 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1238 SourceLocation getGotoLoc() const { return GotoLoc; }
1239 void setStarLoc(SourceLocation L) { StarLoc = L; }
1240 SourceLocation getStarLoc() const { return StarLoc; }
1242 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1243 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1244 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1246 /// getConstantTarget - Returns the fixed target of this indirect
1247 /// goto, if one exists.
1248 LabelDecl *getConstantTarget();
1249 const LabelDecl *getConstantTarget() const {
1250 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1253 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1254 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1256 static bool classof(const Stmt *T) {
1257 return T->getStmtClass() == IndirectGotoStmtClass;
1261 child_range children() { return child_range(&Target, &Target+1); }
1265 /// ContinueStmt - This represents a continue.
1267 class ContinueStmt : public Stmt {
1268 SourceLocation ContinueLoc;
1270 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1272 /// \brief Build an empty continue statement.
1273 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1275 SourceLocation getContinueLoc() const { return ContinueLoc; }
1276 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1278 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1279 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1281 static bool classof(const Stmt *T) {
1282 return T->getStmtClass() == ContinueStmtClass;
1286 child_range children() { return child_range(); }
1289 /// BreakStmt - This represents a break.
1291 class BreakStmt : public Stmt {
1292 SourceLocation BreakLoc;
1294 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1296 /// \brief Build an empty break statement.
1297 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1299 SourceLocation getBreakLoc() const { return BreakLoc; }
1300 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1302 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1303 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1305 static bool classof(const Stmt *T) {
1306 return T->getStmtClass() == BreakStmtClass;
1310 child_range children() { return child_range(); }
1314 /// ReturnStmt - This represents a return, optionally of an expression:
1318 /// Note that GCC allows return with no argument in a function declared to
1319 /// return a value, and it allows returning a value in functions declared to
1320 /// return void. We explicitly model this in the AST, which means you can't
1321 /// depend on the return type of the function and the presence of an argument.
1323 class ReturnStmt : public Stmt {
1325 SourceLocation RetLoc;
1326 const VarDecl *NRVOCandidate;
1329 ReturnStmt(SourceLocation RL)
1330 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1332 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1333 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1334 NRVOCandidate(NRVOCandidate) {}
1336 /// \brief Build an empty return expression.
1337 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1339 const Expr *getRetValue() const;
1340 Expr *getRetValue();
1341 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1343 SourceLocation getReturnLoc() const { return RetLoc; }
1344 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1346 /// \brief Retrieve the variable that might be used for the named return
1347 /// value optimization.
1349 /// The optimization itself can only be performed if the variable is
1350 /// also marked as an NRVO object.
1351 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1352 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1354 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1355 SourceLocation getLocEnd() const LLVM_READONLY {
1356 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1359 static bool classof(const Stmt *T) {
1360 return T->getStmtClass() == ReturnStmtClass;
1364 child_range children() {
1365 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1366 return child_range();
1370 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1372 class AsmStmt : public Stmt {
1374 SourceLocation AsmLoc;
1375 /// \brief True if the assembly statement does not have any input or output
1379 /// \brief If true, treat this inline assembly as having side effects.
1380 /// This assembly statement should not be optimized, deleted or moved.
1383 unsigned NumOutputs;
1385 unsigned NumClobbers;
1387 IdentifierInfo **Names;
1390 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1391 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1392 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1393 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1396 /// \brief Build an empty inline-assembly statement.
1397 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1398 Stmt(SC, Empty), Names(0), Exprs(0) { }
1400 SourceLocation getAsmLoc() const { return AsmLoc; }
1401 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1403 bool isSimple() const { return IsSimple; }
1404 void setSimple(bool V) { IsSimple = V; }
1406 bool isVolatile() const { return IsVolatile; }
1407 void setVolatile(bool V) { IsVolatile = V; }
1409 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1410 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1412 //===--- Asm String Analysis ---===//
1414 /// Assemble final IR asm string.
1415 std::string generateAsmString(ASTContext &C) const;
1417 //===--- Output operands ---===//
1419 unsigned getNumOutputs() const { return NumOutputs; }
1421 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1425 StringRef getOutputName(unsigned i) const {
1426 if (IdentifierInfo *II = getOutputIdentifier(i))
1427 return II->getName();
1432 /// getOutputConstraint - Return the constraint string for the specified
1433 /// output operand. All output constraints are known to be non-empty (either
1435 StringRef getOutputConstraint(unsigned i) const;
1437 /// isOutputPlusConstraint - Return true if the specified output constraint
1438 /// is a "+" constraint (which is both an input and an output) or false if it
1439 /// is an "=" constraint (just an output).
1440 bool isOutputPlusConstraint(unsigned i) const {
1441 return getOutputConstraint(i)[0] == '+';
1444 const Expr *getOutputExpr(unsigned i) const;
1446 /// getNumPlusOperands - Return the number of output operands that have a "+"
1448 unsigned getNumPlusOperands() const;
1450 //===--- Input operands ---===//
1452 unsigned getNumInputs() const { return NumInputs; }
1454 IdentifierInfo *getInputIdentifier(unsigned i) const {
1455 return Names[i + NumOutputs];
1458 StringRef getInputName(unsigned i) const {
1459 if (IdentifierInfo *II = getInputIdentifier(i))
1460 return II->getName();
1465 /// getInputConstraint - Return the specified input constraint. Unlike output
1466 /// constraints, these can be empty.
1467 StringRef getInputConstraint(unsigned i) const;
1469 const Expr *getInputExpr(unsigned i) const;
1471 //===--- Other ---===//
1473 unsigned getNumClobbers() const { return NumClobbers; }
1474 StringRef getClobber(unsigned i) const;
1476 static bool classof(const Stmt *T) {
1477 return T->getStmtClass() == GCCAsmStmtClass ||
1478 T->getStmtClass() == MSAsmStmtClass;
1481 // Input expr iterators.
1483 typedef ExprIterator inputs_iterator;
1484 typedef ConstExprIterator const_inputs_iterator;
1486 inputs_iterator begin_inputs() {
1487 return &Exprs[0] + NumOutputs;
1490 inputs_iterator end_inputs() {
1491 return &Exprs[0] + NumOutputs + NumInputs;
1494 const_inputs_iterator begin_inputs() const {
1495 return &Exprs[0] + NumOutputs;
1498 const_inputs_iterator end_inputs() const {
1499 return &Exprs[0] + NumOutputs + NumInputs;
1502 // Output expr iterators.
1504 typedef ExprIterator outputs_iterator;
1505 typedef ConstExprIterator const_outputs_iterator;
1507 outputs_iterator begin_outputs() {
1510 outputs_iterator end_outputs() {
1511 return &Exprs[0] + NumOutputs;
1514 const_outputs_iterator begin_outputs() const {
1517 const_outputs_iterator end_outputs() const {
1518 return &Exprs[0] + NumOutputs;
1521 child_range children() {
1522 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1526 /// This represents a GCC inline-assembly statement extension.
1528 class GCCAsmStmt : public AsmStmt {
1529 SourceLocation RParenLoc;
1530 StringLiteral *AsmStr;
1532 // FIXME: If we wanted to, we could allocate all of these in one big array.
1533 StringLiteral **Constraints;
1534 StringLiteral **Clobbers;
1537 GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
1538 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1539 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1540 StringLiteral *asmstr, unsigned numclobbers,
1541 StringLiteral **clobbers, SourceLocation rparenloc);
1543 /// \brief Build an empty inline-assembly statement.
1544 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1545 Constraints(0), Clobbers(0) { }
1547 SourceLocation getRParenLoc() const { return RParenLoc; }
1548 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1550 //===--- Asm String Analysis ---===//
1552 const StringLiteral *getAsmString() const { return AsmStr; }
1553 StringLiteral *getAsmString() { return AsmStr; }
1554 void setAsmString(StringLiteral *E) { AsmStr = E; }
1556 /// AsmStringPiece - this is part of a decomposed asm string specification
1557 /// (for use with the AnalyzeAsmString function below). An asm string is
1558 /// considered to be a concatenation of these parts.
1559 class AsmStringPiece {
1562 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1563 Operand // Operand reference, with optional modifier %c4.
1570 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1571 AsmStringPiece(unsigned OpNo, char Modifier)
1572 : MyKind(Operand), Str(), OperandNo(OpNo) {
1576 bool isString() const { return MyKind == String; }
1577 bool isOperand() const { return MyKind == Operand; }
1579 const std::string &getString() const {
1584 unsigned getOperandNo() const {
1585 assert(isOperand());
1589 /// getModifier - Get the modifier for this operand, if present. This
1590 /// returns '\0' if there was no modifier.
1591 char getModifier() const {
1592 assert(isOperand());
1597 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1598 /// it into pieces. If the asm string is erroneous, emit errors and return
1599 /// true, otherwise return false. This handles canonicalization and
1600 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1601 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1602 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1603 ASTContext &C, unsigned &DiagOffs) const;
1605 /// Assemble final IR asm string.
1606 std::string generateAsmString(ASTContext &C) const;
1608 //===--- Output operands ---===//
1610 StringRef getOutputConstraint(unsigned i) const;
1612 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1613 return Constraints[i];
1615 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1616 return Constraints[i];
1619 Expr *getOutputExpr(unsigned i);
1621 const Expr *getOutputExpr(unsigned i) const {
1622 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1625 //===--- Input operands ---===//
1627 StringRef getInputConstraint(unsigned i) const;
1629 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1630 return Constraints[i + NumOutputs];
1632 StringLiteral *getInputConstraintLiteral(unsigned i) {
1633 return Constraints[i + NumOutputs];
1636 Expr *getInputExpr(unsigned i);
1637 void setInputExpr(unsigned i, Expr *E);
1639 const Expr *getInputExpr(unsigned i) const {
1640 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1643 void setOutputsAndInputsAndClobbers(ASTContext &C,
1644 IdentifierInfo **Names,
1645 StringLiteral **Constraints,
1647 unsigned NumOutputs,
1649 StringLiteral **Clobbers,
1650 unsigned NumClobbers);
1652 //===--- Other ---===//
1654 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1655 /// translate this into a numeric value needed to reference the same operand.
1656 /// This returns -1 if the operand name is invalid.
1657 int getNamedOperand(StringRef SymbolicName) const;
1659 StringRef getClobber(unsigned i) const;
1660 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1661 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1665 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1666 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1668 static bool classof(const Stmt *T) {
1669 return T->getStmtClass() == GCCAsmStmtClass;
1673 /// This represents a Microsoft inline-assembly statement extension.
1675 class MSAsmStmt : public AsmStmt {
1676 SourceLocation LBraceLoc, EndLoc;
1679 unsigned NumAsmToks;
1682 StringRef *Constraints;
1683 StringRef *Clobbers;
1686 MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1687 bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1688 unsigned numoutputs, unsigned numinputs,
1689 ArrayRef<IdentifierInfo*> names, ArrayRef<StringRef> constraints,
1690 ArrayRef<Expr*> exprs, StringRef asmstr,
1691 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1693 /// \brief Build an empty MS-style inline-assembly statement.
1694 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1695 NumAsmToks(0), AsmToks(0), Constraints(0), Clobbers(0) { }
1697 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1698 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1699 SourceLocation getEndLoc() const { return EndLoc; }
1700 void setEndLoc(SourceLocation L) { EndLoc = L; }
1702 bool hasBraces() const { return LBraceLoc.isValid(); }
1704 unsigned getNumAsmToks() { return NumAsmToks; }
1705 Token *getAsmToks() { return AsmToks; }
1707 //===--- Asm String Analysis ---===//
1709 const std::string *getAsmString() const { return &AsmStr; }
1710 std::string *getAsmString() { return &AsmStr; }
1711 void setAsmString(StringRef &E) { AsmStr = E.str(); }
1713 /// Assemble final IR asm string.
1714 std::string generateAsmString(ASTContext &C) const;
1716 //===--- Output operands ---===//
1718 StringRef getOutputConstraint(unsigned i) const {
1719 return Constraints[i];
1722 Expr *getOutputExpr(unsigned i);
1724 const Expr *getOutputExpr(unsigned i) const {
1725 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1728 //===--- Input operands ---===//
1730 StringRef getInputConstraint(unsigned i) const {
1731 return Constraints[i + NumOutputs];
1734 Expr *getInputExpr(unsigned i);
1735 void setInputExpr(unsigned i, Expr *E);
1737 const Expr *getInputExpr(unsigned i) const {
1738 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1741 //===--- Other ---===//
1743 StringRef getClobber(unsigned i) const { return Clobbers[i]; }
1745 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1746 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1748 static bool classof(const Stmt *T) {
1749 return T->getStmtClass() == MSAsmStmtClass;
1752 child_range children() {
1753 return child_range(&Exprs[0], &Exprs[0]);
1757 class SEHExceptStmt : public Stmt {
1761 enum { FILTER_EXPR, BLOCK };
1763 SEHExceptStmt(SourceLocation Loc,
1767 friend class ASTReader;
1768 friend class ASTStmtReader;
1769 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1772 static SEHExceptStmt* Create(ASTContext &C,
1773 SourceLocation ExceptLoc,
1777 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1778 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1780 SourceLocation getExceptLoc() const { return Loc; }
1781 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1783 Expr *getFilterExpr() const {
1784 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1787 CompoundStmt *getBlock() const {
1788 return cast<CompoundStmt>(Children[BLOCK]);
1791 child_range children() {
1792 return child_range(Children,Children+2);
1795 static bool classof(const Stmt *T) {
1796 return T->getStmtClass() == SEHExceptStmtClass;
1801 class SEHFinallyStmt : public Stmt {
1805 SEHFinallyStmt(SourceLocation Loc,
1808 friend class ASTReader;
1809 friend class ASTStmtReader;
1810 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1813 static SEHFinallyStmt* Create(ASTContext &C,
1814 SourceLocation FinallyLoc,
1817 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1818 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1820 SourceLocation getFinallyLoc() const { return Loc; }
1821 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1823 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1825 child_range children() {
1826 return child_range(&Block,&Block+1);
1829 static bool classof(const Stmt *T) {
1830 return T->getStmtClass() == SEHFinallyStmtClass;
1835 class SEHTryStmt : public Stmt {
1837 SourceLocation TryLoc;
1840 enum { TRY = 0, HANDLER = 1 };
1842 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1843 SourceLocation TryLoc,
1847 friend class ASTReader;
1848 friend class ASTStmtReader;
1849 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1852 static SEHTryStmt* Create(ASTContext &C,
1854 SourceLocation TryLoc,
1858 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1859 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1861 SourceLocation getTryLoc() const { return TryLoc; }
1862 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1864 bool getIsCXXTry() const { return IsCXXTry; }
1866 CompoundStmt* getTryBlock() const {
1867 return cast<CompoundStmt>(Children[TRY]);
1870 Stmt *getHandler() const { return Children[HANDLER]; }
1872 /// Returns 0 if not defined
1873 SEHExceptStmt *getExceptHandler() const;
1874 SEHFinallyStmt *getFinallyHandler() const;
1876 child_range children() {
1877 return child_range(Children,Children+2);
1880 static bool classof(const Stmt *T) {
1881 return T->getStmtClass() == SEHTryStmtClass;
1885 } // end namespace clang