1 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Stmt interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
17 #include "clang/Basic/LLVM.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/AST/PrettyPrinter.h"
20 #include "clang/AST/StmtIterator.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/Attr.h"
23 #include "clang/Lex/Token.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/raw_ostream.h"
30 class FoldingSetNodeID;
46 //===--------------------------------------------------------------------===//
47 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
48 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
49 // references to children (to be compatible with StmtIterator).
50 //===--------------------------------------------------------------------===//
58 ExprIterator(Stmt** i) : I(i) {}
59 ExprIterator() : I(0) {}
60 ExprIterator& operator++() { ++I; return *this; }
61 ExprIterator operator-(size_t i) { return I-i; }
62 ExprIterator operator+(size_t i) { return I+i; }
63 Expr* operator[](size_t idx);
64 // FIXME: Verify that this will correctly return a signed distance.
65 signed operator-(const ExprIterator& R) const { return I - R.I; }
66 Expr* operator*() const;
67 Expr* operator->() const;
68 bool operator==(const ExprIterator& R) const { return I == R.I; }
69 bool operator!=(const ExprIterator& R) const { return I != R.I; }
70 bool operator>(const ExprIterator& R) const { return I > R.I; }
71 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
74 class ConstExprIterator {
75 const Stmt * const *I;
77 ConstExprIterator(const Stmt * const *i) : I(i) {}
78 ConstExprIterator() : I(0) {}
79 ConstExprIterator& operator++() { ++I; return *this; }
80 ConstExprIterator operator+(size_t i) const { return I+i; }
81 ConstExprIterator operator-(size_t i) const { return I-i; }
82 const Expr * operator[](size_t idx) const;
83 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
84 const Expr * operator*() const;
85 const Expr * operator->() const;
86 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
87 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
88 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
89 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
92 //===----------------------------------------------------------------------===//
93 // AST classes for statements.
94 //===----------------------------------------------------------------------===//
96 /// Stmt - This represents one statement.
102 #define STMT(CLASS, PARENT) CLASS##Class,
103 #define STMT_RANGE(BASE, FIRST, LAST) \
104 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
105 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
106 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
107 #define ABSTRACT_STMT(STMT)
108 #include "clang/AST/StmtNodes.inc"
111 // Make vanilla 'new' and 'delete' illegal for Stmts.
113 void* operator new(size_t bytes) throw() {
114 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
116 void operator delete(void* data) throw() {
117 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
120 class StmtBitfields {
123 /// \brief The statement class.
126 enum { NumStmtBits = 8 };
128 class CompoundStmtBitfields {
129 friend class CompoundStmt;
130 unsigned : NumStmtBits;
132 unsigned NumStmts : 32 - NumStmtBits;
135 class ExprBitfields {
137 friend class DeclRefExpr; // computeDependence
138 friend class InitListExpr; // ctor
139 friend class DesignatedInitExpr; // ctor
140 friend class BlockDeclRefExpr; // ctor
141 friend class ASTStmtReader; // deserialization
142 friend class CXXNewExpr; // ctor
143 friend class DependentScopeDeclRefExpr; // ctor
144 friend class CXXConstructExpr; // ctor
145 friend class CallExpr; // ctor
146 friend class OffsetOfExpr; // ctor
147 friend class ObjCMessageExpr; // ctor
148 friend class ObjCArrayLiteral; // ctor
149 friend class ObjCDictionaryLiteral; // ctor
150 friend class ShuffleVectorExpr; // ctor
151 friend class ParenListExpr; // ctor
152 friend class CXXUnresolvedConstructExpr; // ctor
153 friend class CXXDependentScopeMemberExpr; // ctor
154 friend class OverloadExpr; // ctor
155 friend class PseudoObjectExpr; // ctor
156 friend class AtomicExpr; // ctor
157 unsigned : NumStmtBits;
159 unsigned ValueKind : 2;
160 unsigned ObjectKind : 2;
161 unsigned TypeDependent : 1;
162 unsigned ValueDependent : 1;
163 unsigned InstantiationDependent : 1;
164 unsigned ContainsUnexpandedParameterPack : 1;
166 enum { NumExprBits = 16 };
168 class CharacterLiteralBitfields {
169 friend class CharacterLiteral;
170 unsigned : NumExprBits;
175 class FloatingLiteralBitfields {
176 friend class FloatingLiteral;
177 unsigned : NumExprBits;
179 unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
180 unsigned IsExact : 1;
183 class UnaryExprOrTypeTraitExprBitfields {
184 friend class UnaryExprOrTypeTraitExpr;
185 unsigned : NumExprBits;
188 unsigned IsType : 1; // true if operand is a type, false if an expression.
191 class DeclRefExprBitfields {
192 friend class DeclRefExpr;
193 friend class ASTStmtReader; // deserialization
194 unsigned : NumExprBits;
196 unsigned HasQualifier : 1;
197 unsigned HasTemplateKWAndArgsInfo : 1;
198 unsigned HasFoundDecl : 1;
199 unsigned HadMultipleCandidates : 1;
200 unsigned RefersToEnclosingLocal : 1;
203 class CastExprBitfields {
204 friend class CastExpr;
205 unsigned : NumExprBits;
208 unsigned BasePathSize : 32 - 6 - NumExprBits;
211 class CallExprBitfields {
212 friend class CallExpr;
213 unsigned : NumExprBits;
215 unsigned NumPreArgs : 1;
218 class ExprWithCleanupsBitfields {
219 friend class ExprWithCleanups;
220 friend class ASTStmtReader; // deserialization
222 unsigned : NumExprBits;
224 unsigned NumObjects : 32 - NumExprBits;
227 class PseudoObjectExprBitfields {
228 friend class PseudoObjectExpr;
229 friend class ASTStmtReader; // deserialization
231 unsigned : NumExprBits;
233 // These don't need to be particularly wide, because they're
234 // strictly limited by the forms of expressions we permit.
235 unsigned NumSubExprs : 8;
236 unsigned ResultIndex : 32 - 8 - NumExprBits;
239 class ObjCIndirectCopyRestoreExprBitfields {
240 friend class ObjCIndirectCopyRestoreExpr;
241 unsigned : NumExprBits;
243 unsigned ShouldCopy : 1;
246 class InitListExprBitfields {
247 friend class InitListExpr;
249 unsigned : NumExprBits;
251 /// Whether this initializer list originally had a GNU array-range
252 /// designator in it. This is a temporary marker used by CodeGen.
253 unsigned HadArrayRangeDesignator : 1;
255 /// Whether this initializer list initializes a std::initializer_list
257 unsigned InitializesStdInitializerList : 1;
260 class TypeTraitExprBitfields {
261 friend class TypeTraitExpr;
262 friend class ASTStmtReader;
263 friend class ASTStmtWriter;
265 unsigned : NumExprBits;
267 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
270 /// \brief If this expression is not value-dependent, this indicates whether
271 /// the trait evaluated true or false.
274 /// \brief The number of arguments to this type trait.
275 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
279 // FIXME: this is wasteful on 64-bit platforms.
282 StmtBitfields StmtBits;
283 CompoundStmtBitfields CompoundStmtBits;
284 ExprBitfields ExprBits;
285 CharacterLiteralBitfields CharacterLiteralBits;
286 FloatingLiteralBitfields FloatingLiteralBits;
287 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
288 DeclRefExprBitfields DeclRefExprBits;
289 CastExprBitfields CastExprBits;
290 CallExprBitfields CallExprBits;
291 ExprWithCleanupsBitfields ExprWithCleanupsBits;
292 PseudoObjectExprBitfields PseudoObjectExprBits;
293 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
294 InitListExprBitfields InitListExprBits;
295 TypeTraitExprBitfields TypeTraitExprBits;
298 friend class ASTStmtReader;
299 friend class ASTStmtWriter;
302 // Only allow allocation of Stmts using the allocator in ASTContext
303 // or by doing a placement new.
304 void* operator new(size_t bytes, ASTContext& C,
305 unsigned alignment = 8) throw() {
306 return ::operator new(bytes, C, alignment);
309 void* operator new(size_t bytes, ASTContext* C,
310 unsigned alignment = 8) throw() {
311 return ::operator new(bytes, *C, alignment);
314 void* operator new(size_t bytes, void* mem) throw() {
318 void operator delete(void*, ASTContext&, unsigned) throw() { }
319 void operator delete(void*, ASTContext*, unsigned) throw() { }
320 void operator delete(void*, std::size_t) throw() { }
321 void operator delete(void*, void*) throw() { }
324 /// \brief A placeholder type used to construct an empty shell of a
325 /// type, that will be filled in later (e.g., by some
326 /// de-serialization).
327 struct EmptyShell { };
330 /// \brief Whether statistic collection is enabled.
331 static bool StatisticsEnabled;
334 /// \brief Construct an empty statement.
335 explicit Stmt(StmtClass SC, EmptyShell) {
336 StmtBits.sClass = SC;
337 if (StatisticsEnabled) Stmt::addStmtClass(SC);
342 StmtBits.sClass = SC;
343 if (StatisticsEnabled) Stmt::addStmtClass(SC);
346 StmtClass getStmtClass() const {
347 return static_cast<StmtClass>(StmtBits.sClass);
349 const char *getStmtClassName() const;
351 /// SourceLocation tokens are not useful in isolation - they are low level
352 /// value objects created/interpreted by SourceManager. We assume AST
353 /// clients will have a pointer to the respective SourceManager.
354 SourceRange getSourceRange() const LLVM_READONLY;
355 SourceLocation getLocStart() const LLVM_READONLY;
356 SourceLocation getLocEnd() const LLVM_READONLY;
358 // global temp stats (until we have a per-module visitor)
359 static void addStmtClass(const StmtClass s);
360 static void EnableStatistics();
361 static void PrintStats();
363 /// dump - This does a local dump of the specified AST fragment. It dumps the
364 /// specified node and a few nodes underneath it, but not the whole subtree.
365 /// This is useful in a debugger.
366 LLVM_ATTRIBUTE_USED void dump() const;
367 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
368 void dump(raw_ostream &OS, SourceManager &SM) const;
370 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
371 void dumpAll() const;
372 void dumpAll(SourceManager &SM) const;
374 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
375 /// back to its original source language syntax.
376 void dumpPretty(ASTContext &Context) const;
377 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
378 const PrintingPolicy &Policy,
379 unsigned Indentation = 0) const;
381 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
382 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
383 void viewAST() const;
385 /// Skip past any implicit AST nodes which might surround this
386 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
387 Stmt *IgnoreImplicit();
389 const Stmt *stripLabelLikeStatements() const;
390 Stmt *stripLabelLikeStatements() {
391 return const_cast<Stmt*>(
392 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
395 // Implement isa<T> support.
396 static bool classof(const Stmt *) { return true; }
398 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
399 /// contain implicit control-flow in the order their subexpressions
400 /// are evaluated. This predicate returns true if this statement has
401 /// such implicit control-flow. Such statements are also specially handled
403 bool hasImplicitControlFlow() const;
405 /// Child Iterators: All subclasses must implement 'children'
406 /// to permit easy iteration over the substatements/subexpessions of an
407 /// AST node. This permits easy iteration over all nodes in the AST.
408 typedef StmtIterator child_iterator;
409 typedef ConstStmtIterator const_child_iterator;
411 typedef StmtRange child_range;
412 typedef ConstStmtRange const_child_range;
414 child_range children();
415 const_child_range children() const {
416 return const_cast<Stmt*>(this)->children();
419 child_iterator child_begin() { return children().first; }
420 child_iterator child_end() { return children().second; }
422 const_child_iterator child_begin() const { return children().first; }
423 const_child_iterator child_end() const { return children().second; }
425 /// \brief Produce a unique representation of the given statement.
427 /// \brief ID once the profiling operation is complete, will contain
428 /// the unique representation of the given statement.
430 /// \brief Context the AST context in which the statement resides
432 /// \brief Canonical whether the profile should be based on the canonical
433 /// representation of this statement (e.g., where non-type template
434 /// parameters are identified by index/level rather than their
435 /// declaration pointers) or the exact representation of the statement as
436 /// written in the source.
437 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
438 bool Canonical) const;
441 /// DeclStmt - Adaptor class for mixing declarations with statements and
442 /// expressions. For example, CompoundStmt mixes statements, expressions
443 /// and declarations (variables, types). Another example is ForStmt, where
444 /// the first statement can be an expression or a declaration.
446 class DeclStmt : public Stmt {
448 SourceLocation StartLoc, EndLoc;
451 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
452 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
453 StartLoc(startLoc), EndLoc(endLoc) {}
455 /// \brief Build an empty declaration statement.
456 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
458 /// isSingleDecl - This method returns true if this DeclStmt refers
459 /// to a single Decl.
460 bool isSingleDecl() const {
461 return DG.isSingleDecl();
464 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
465 Decl *getSingleDecl() { return DG.getSingleDecl(); }
467 const DeclGroupRef getDeclGroup() const { return DG; }
468 DeclGroupRef getDeclGroup() { return DG; }
469 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
471 SourceLocation getStartLoc() const { return StartLoc; }
472 void setStartLoc(SourceLocation L) { StartLoc = L; }
473 SourceLocation getEndLoc() const { return EndLoc; }
474 void setEndLoc(SourceLocation L) { EndLoc = L; }
476 SourceRange getSourceRange() const LLVM_READONLY {
477 return SourceRange(StartLoc, EndLoc);
480 static bool classof(const Stmt *T) {
481 return T->getStmtClass() == DeclStmtClass;
483 static bool classof(const DeclStmt *) { return true; }
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 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
535 static bool classof(const Stmt *T) {
536 return T->getStmtClass() == NullStmtClass;
538 static bool classof(const NullStmt *) { return true; }
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, Stmt **StmtStart, unsigned NumStmts,
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 SourceRange getSourceRange() const LLVM_READONLY {
607 return SourceRange(LBracLoc, RBracLoc);
610 SourceLocation getLBracLoc() const { return LBracLoc; }
611 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
612 SourceLocation getRBracLoc() const { return RBracLoc; }
613 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
615 static bool classof(const Stmt *T) {
616 return T->getStmtClass() == CompoundStmtClass;
618 static bool classof(const CompoundStmt *) { return true; }
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;
637 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
640 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
642 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
644 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
647 const Stmt *getSubStmt() const {
648 return const_cast<SwitchCase*>(this)->getSubStmt();
651 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
653 static bool classof(const Stmt *T) {
654 return T->getStmtClass() == CaseStmtClass ||
655 T->getStmtClass() == DefaultStmtClass;
657 static bool classof(const SwitchCase *) { return true; }
660 class CaseStmt : public SwitchCase {
661 enum { LHS, RHS, SUBSTMT, END_EXPR };
662 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
663 // GNU "case 1 ... 4" extension
664 SourceLocation CaseLoc;
665 SourceLocation EllipsisLoc;
666 SourceLocation ColonLoc;
668 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
669 SourceLocation ellipsisLoc, SourceLocation colonLoc)
670 : SwitchCase(CaseStmtClass) {
671 SubExprs[SUBSTMT] = 0;
672 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
673 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
675 EllipsisLoc = ellipsisLoc;
679 /// \brief Build an empty switch case statement.
680 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
682 SourceLocation getCaseLoc() const { return CaseLoc; }
683 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
684 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
685 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
686 SourceLocation getColonLoc() const { return ColonLoc; }
687 void setColonLoc(SourceLocation L) { ColonLoc = L; }
689 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
690 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
691 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
693 const Expr *getLHS() const {
694 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
696 const Expr *getRHS() const {
697 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
699 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
701 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
702 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
703 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
706 SourceRange getSourceRange() const LLVM_READONLY {
707 // Handle deeply nested case statements with iteration instead of recursion.
708 const CaseStmt *CS = this;
709 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
712 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
714 static bool classof(const Stmt *T) {
715 return T->getStmtClass() == CaseStmtClass;
717 static bool classof(const CaseStmt *) { return true; }
720 child_range children() {
721 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
725 class DefaultStmt : public SwitchCase {
727 SourceLocation DefaultLoc;
728 SourceLocation ColonLoc;
730 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
731 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
734 /// \brief Build an empty default statement.
735 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
737 Stmt *getSubStmt() { return SubStmt; }
738 const Stmt *getSubStmt() const { return SubStmt; }
739 void setSubStmt(Stmt *S) { SubStmt = S; }
741 SourceLocation getDefaultLoc() const { return DefaultLoc; }
742 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
743 SourceLocation getColonLoc() const { return ColonLoc; }
744 void setColonLoc(SourceLocation L) { ColonLoc = L; }
746 SourceRange getSourceRange() const LLVM_READONLY {
747 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
749 static bool classof(const Stmt *T) {
750 return T->getStmtClass() == DefaultStmtClass;
752 static bool classof(const DefaultStmt *) { return true; }
755 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
759 /// LabelStmt - Represents a label, which has a substatement. For example:
762 class LabelStmt : public Stmt {
765 SourceLocation IdentLoc;
767 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
768 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
771 // \brief Build an empty label statement.
772 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
774 SourceLocation getIdentLoc() const { return IdentLoc; }
775 LabelDecl *getDecl() const { return TheDecl; }
776 void setDecl(LabelDecl *D) { TheDecl = D; }
777 const char *getName() const;
778 Stmt *getSubStmt() { return SubStmt; }
779 const Stmt *getSubStmt() const { return SubStmt; }
780 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
781 void setSubStmt(Stmt *SS) { SubStmt = SS; }
783 SourceRange getSourceRange() const LLVM_READONLY {
784 return SourceRange(IdentLoc, SubStmt->getLocEnd());
786 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
788 static bool classof(const Stmt *T) {
789 return T->getStmtClass() == LabelStmtClass;
791 static bool classof(const LabelStmt *) { return true; }
795 /// \brief Represents an attribute applied to a statement.
797 /// Represents an attribute applied to a statement. For example:
798 /// [[omp::for(...)]] for (...) { ... }
800 class AttributedStmt : public Stmt {
802 SourceLocation AttrLoc;
804 const Attr *Attrs[1];
806 friend class ASTStmtReader;
808 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
809 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
810 NumAttrs(Attrs.size()) {
811 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*));
814 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
815 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
816 memset(Attrs, 0, NumAttrs * sizeof(Attr*));
820 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc,
821 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
822 // \brief Build an empty attributed statement.
823 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs);
825 SourceLocation getAttrLoc() const { return AttrLoc; }
826 ArrayRef<const Attr*> getAttrs() const {
827 return ArrayRef<const Attr*>(Attrs, NumAttrs);
829 Stmt *getSubStmt() { return SubStmt; }
830 const Stmt *getSubStmt() const { return SubStmt; }
832 SourceRange getSourceRange() const LLVM_READONLY {
833 return SourceRange(AttrLoc, SubStmt->getLocEnd());
835 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
837 static bool classof(const Stmt *T) {
838 return T->getStmtClass() == AttributedStmtClass;
840 static bool classof(const AttributedStmt *) { return true; }
844 /// IfStmt - This represents an if/then/else.
846 class IfStmt : public Stmt {
847 enum { VAR, COND, THEN, ELSE, END_EXPR };
848 Stmt* SubExprs[END_EXPR];
850 SourceLocation IfLoc;
851 SourceLocation ElseLoc;
854 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
855 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
857 /// \brief Build an empty if/then/else statement
858 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
860 /// \brief Retrieve the variable declared in this "if" statement, if any.
862 /// In the following example, "x" is the condition variable.
864 /// if (int x = foo()) {
865 /// printf("x is %d", x);
868 VarDecl *getConditionVariable() const;
869 void setConditionVariable(ASTContext &C, VarDecl *V);
871 /// If this IfStmt has a condition variable, return the faux DeclStmt
872 /// associated with the creation of that condition variable.
873 const DeclStmt *getConditionVariableDeclStmt() const {
874 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
877 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
878 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
879 const Stmt *getThen() const { return SubExprs[THEN]; }
880 void setThen(Stmt *S) { SubExprs[THEN] = S; }
881 const Stmt *getElse() const { return SubExprs[ELSE]; }
882 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
884 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
885 Stmt *getThen() { return SubExprs[THEN]; }
886 Stmt *getElse() { return SubExprs[ELSE]; }
888 SourceLocation getIfLoc() const { return IfLoc; }
889 void setIfLoc(SourceLocation L) { IfLoc = L; }
890 SourceLocation getElseLoc() const { return ElseLoc; }
891 void setElseLoc(SourceLocation L) { ElseLoc = L; }
893 SourceRange getSourceRange() const LLVM_READONLY {
895 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
897 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
900 // Iterators over subexpressions. The iterators will include iterating
901 // over the initialization expression referenced by the condition variable.
902 child_range children() {
903 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
906 static bool classof(const Stmt *T) {
907 return T->getStmtClass() == IfStmtClass;
909 static bool classof(const IfStmt *) { return true; }
912 /// SwitchStmt - This represents a 'switch' stmt.
914 class SwitchStmt : public Stmt {
915 enum { VAR, COND, BODY, END_EXPR };
916 Stmt* SubExprs[END_EXPR];
917 // This points to a linked list of case and default statements.
918 SwitchCase *FirstCase;
919 SourceLocation SwitchLoc;
921 /// If the SwitchStmt is a switch on an enum value, this records whether
922 /// all the enum values were covered by CaseStmts. This value is meant to
923 /// be a hint for possible clients.
924 unsigned AllEnumCasesCovered : 1;
927 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
929 /// \brief Build a empty switch statement.
930 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
932 /// \brief Retrieve the variable declared in this "switch" statement, if any.
934 /// In the following example, "x" is the condition variable.
936 /// switch (int x = foo()) {
941 VarDecl *getConditionVariable() const;
942 void setConditionVariable(ASTContext &C, VarDecl *V);
944 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
945 /// associated with the creation of that condition variable.
946 const DeclStmt *getConditionVariableDeclStmt() const {
947 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
950 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
951 const Stmt *getBody() const { return SubExprs[BODY]; }
952 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
954 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
955 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
956 Stmt *getBody() { return SubExprs[BODY]; }
957 void setBody(Stmt *S) { SubExprs[BODY] = S; }
958 SwitchCase *getSwitchCaseList() { return FirstCase; }
960 /// \brief Set the case list for this switch statement.
962 /// The caller is responsible for incrementing the retain counts on
963 /// all of the SwitchCase statements in this list.
964 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
966 SourceLocation getSwitchLoc() const { return SwitchLoc; }
967 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
969 void setBody(Stmt *S, SourceLocation SL) {
973 void addSwitchCase(SwitchCase *SC) {
974 assert(!SC->getNextSwitchCase()
975 && "case/default already added to a switch");
976 SC->setNextSwitchCase(FirstCase);
980 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
981 /// switch over an enum value then all cases have been explicitly covered.
982 void setAllEnumCasesCovered() {
983 AllEnumCasesCovered = 1;
986 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
987 /// have been explicitly covered.
988 bool isAllEnumCasesCovered() const {
989 return (bool) AllEnumCasesCovered;
992 SourceRange getSourceRange() const LLVM_READONLY {
993 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
996 child_range children() {
997 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1000 static bool classof(const Stmt *T) {
1001 return T->getStmtClass() == SwitchStmtClass;
1003 static bool classof(const SwitchStmt *) { return true; }
1007 /// WhileStmt - This represents a 'while' stmt.
1009 class WhileStmt : public Stmt {
1010 enum { VAR, COND, BODY, END_EXPR };
1011 Stmt* SubExprs[END_EXPR];
1012 SourceLocation WhileLoc;
1014 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1017 /// \brief Build an empty while statement.
1018 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1020 /// \brief Retrieve the variable declared in this "while" statement, if any.
1022 /// In the following example, "x" is the condition variable.
1024 /// while (int x = random()) {
1028 VarDecl *getConditionVariable() const;
1029 void setConditionVariable(ASTContext &C, VarDecl *V);
1031 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1032 /// associated with the creation of that condition variable.
1033 const DeclStmt *getConditionVariableDeclStmt() const {
1034 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1037 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1038 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1039 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1040 Stmt *getBody() { return SubExprs[BODY]; }
1041 const Stmt *getBody() const { return SubExprs[BODY]; }
1042 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1044 SourceLocation getWhileLoc() const { return WhileLoc; }
1045 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1047 SourceRange getSourceRange() const LLVM_READONLY {
1048 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
1050 static bool classof(const Stmt *T) {
1051 return T->getStmtClass() == WhileStmtClass;
1053 static bool classof(const WhileStmt *) { return true; }
1056 child_range children() {
1057 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1061 /// DoStmt - This represents a 'do/while' stmt.
1063 class DoStmt : public Stmt {
1064 enum { BODY, COND, END_EXPR };
1065 Stmt* SubExprs[END_EXPR];
1066 SourceLocation DoLoc;
1067 SourceLocation WhileLoc;
1068 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1071 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1073 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1074 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1075 SubExprs[BODY] = body;
1078 /// \brief Build an empty do-while statement.
1079 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1081 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1082 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1083 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1084 Stmt *getBody() { return SubExprs[BODY]; }
1085 const Stmt *getBody() const { return SubExprs[BODY]; }
1086 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1088 SourceLocation getDoLoc() const { return DoLoc; }
1089 void setDoLoc(SourceLocation L) { DoLoc = L; }
1090 SourceLocation getWhileLoc() const { return WhileLoc; }
1091 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1093 SourceLocation getRParenLoc() const { return RParenLoc; }
1094 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1096 SourceRange getSourceRange() const LLVM_READONLY {
1097 return SourceRange(DoLoc, RParenLoc);
1099 static bool classof(const Stmt *T) {
1100 return T->getStmtClass() == DoStmtClass;
1102 static bool classof(const DoStmt *) { return true; }
1105 child_range children() {
1106 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1111 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1112 /// the init/cond/inc parts of the ForStmt will be null if they were not
1113 /// specified in the source.
1115 class ForStmt : public Stmt {
1116 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1117 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1118 SourceLocation ForLoc;
1119 SourceLocation LParenLoc, RParenLoc;
1122 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1123 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1125 /// \brief Build an empty for statement.
1126 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1128 Stmt *getInit() { return SubExprs[INIT]; }
1130 /// \brief Retrieve the variable declared in this "for" statement, if any.
1132 /// In the following example, "y" is the condition variable.
1134 /// for (int x = random(); int y = mangle(x); ++x) {
1138 VarDecl *getConditionVariable() const;
1139 void setConditionVariable(ASTContext &C, VarDecl *V);
1141 /// If this ForStmt has a condition variable, return the faux DeclStmt
1142 /// associated with the creation of that condition variable.
1143 const DeclStmt *getConditionVariableDeclStmt() const {
1144 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1147 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1148 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1149 Stmt *getBody() { return SubExprs[BODY]; }
1151 const Stmt *getInit() const { return SubExprs[INIT]; }
1152 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1153 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1154 const Stmt *getBody() const { return SubExprs[BODY]; }
1156 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1157 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1158 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1159 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1161 SourceLocation getForLoc() const { return ForLoc; }
1162 void setForLoc(SourceLocation L) { ForLoc = L; }
1163 SourceLocation getLParenLoc() const { return LParenLoc; }
1164 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1165 SourceLocation getRParenLoc() const { return RParenLoc; }
1166 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1168 SourceRange getSourceRange() const LLVM_READONLY {
1169 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1171 static bool classof(const Stmt *T) {
1172 return T->getStmtClass() == ForStmtClass;
1174 static bool classof(const ForStmt *) { return true; }
1177 child_range children() {
1178 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1182 /// GotoStmt - This represents a direct goto.
1184 class GotoStmt : public Stmt {
1186 SourceLocation GotoLoc;
1187 SourceLocation LabelLoc;
1189 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1190 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1192 /// \brief Build an empty goto statement.
1193 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1195 LabelDecl *getLabel() const { return Label; }
1196 void setLabel(LabelDecl *D) { Label = D; }
1198 SourceLocation getGotoLoc() const { return GotoLoc; }
1199 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1200 SourceLocation getLabelLoc() const { return LabelLoc; }
1201 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1203 SourceRange getSourceRange() const LLVM_READONLY {
1204 return SourceRange(GotoLoc, LabelLoc);
1206 static bool classof(const Stmt *T) {
1207 return T->getStmtClass() == GotoStmtClass;
1209 static bool classof(const GotoStmt *) { return true; }
1212 child_range children() { return child_range(); }
1215 /// IndirectGotoStmt - This represents an indirect goto.
1217 class IndirectGotoStmt : public Stmt {
1218 SourceLocation GotoLoc;
1219 SourceLocation StarLoc;
1222 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1224 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1225 Target((Stmt*)target) {}
1227 /// \brief Build an empty indirect goto statement.
1228 explicit IndirectGotoStmt(EmptyShell Empty)
1229 : Stmt(IndirectGotoStmtClass, Empty) { }
1231 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1232 SourceLocation getGotoLoc() const { return GotoLoc; }
1233 void setStarLoc(SourceLocation L) { StarLoc = L; }
1234 SourceLocation getStarLoc() const { return StarLoc; }
1236 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1237 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1238 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1240 /// getConstantTarget - Returns the fixed target of this indirect
1241 /// goto, if one exists.
1242 LabelDecl *getConstantTarget();
1243 const LabelDecl *getConstantTarget() const {
1244 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1247 SourceRange getSourceRange() const LLVM_READONLY {
1248 return SourceRange(GotoLoc, Target->getLocEnd());
1251 static bool classof(const Stmt *T) {
1252 return T->getStmtClass() == IndirectGotoStmtClass;
1254 static bool classof(const IndirectGotoStmt *) { return true; }
1257 child_range children() { return child_range(&Target, &Target+1); }
1261 /// ContinueStmt - This represents a continue.
1263 class ContinueStmt : public Stmt {
1264 SourceLocation ContinueLoc;
1266 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1268 /// \brief Build an empty continue statement.
1269 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1271 SourceLocation getContinueLoc() const { return ContinueLoc; }
1272 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1274 SourceRange getSourceRange() const LLVM_READONLY {
1275 return SourceRange(ContinueLoc);
1278 static bool classof(const Stmt *T) {
1279 return T->getStmtClass() == ContinueStmtClass;
1281 static bool classof(const ContinueStmt *) { return true; }
1284 child_range children() { return child_range(); }
1287 /// BreakStmt - This represents a break.
1289 class BreakStmt : public Stmt {
1290 SourceLocation BreakLoc;
1292 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1294 /// \brief Build an empty break statement.
1295 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1297 SourceLocation getBreakLoc() const { return BreakLoc; }
1298 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1300 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
1302 static bool classof(const Stmt *T) {
1303 return T->getStmtClass() == BreakStmtClass;
1305 static bool classof(const BreakStmt *) { return true; }
1308 child_range children() { return child_range(); }
1312 /// ReturnStmt - This represents a return, optionally of an expression:
1316 /// Note that GCC allows return with no argument in a function declared to
1317 /// return a value, and it allows returning a value in functions declared to
1318 /// return void. We explicitly model this in the AST, which means you can't
1319 /// depend on the return type of the function and the presence of an argument.
1321 class ReturnStmt : public Stmt {
1323 SourceLocation RetLoc;
1324 const VarDecl *NRVOCandidate;
1327 ReturnStmt(SourceLocation RL)
1328 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1330 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1331 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1332 NRVOCandidate(NRVOCandidate) {}
1334 /// \brief Build an empty return expression.
1335 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1337 const Expr *getRetValue() const;
1338 Expr *getRetValue();
1339 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1341 SourceLocation getReturnLoc() const { return RetLoc; }
1342 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1344 /// \brief Retrieve the variable that might be used for the named return
1345 /// value optimization.
1347 /// The optimization itself can only be performed if the variable is
1348 /// also marked as an NRVO object.
1349 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1350 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1352 SourceRange getSourceRange() const LLVM_READONLY;
1354 static bool classof(const Stmt *T) {
1355 return T->getStmtClass() == ReturnStmtClass;
1357 static bool classof(const ReturnStmt *) { return true; }
1360 child_range children() {
1361 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1362 return child_range();
1366 /// AsmStmt - This represents a GNU inline-assembly statement extension.
1368 class AsmStmt : public Stmt {
1369 SourceLocation AsmLoc, RParenLoc;
1370 StringLiteral *AsmStr;
1376 unsigned NumOutputs;
1378 unsigned NumClobbers;
1380 // FIXME: If we wanted to, we could allocate all of these in one big array.
1381 IdentifierInfo **Names;
1382 StringLiteral **Constraints;
1384 StringLiteral **Clobbers;
1387 AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1388 bool msasm, unsigned numoutputs, unsigned numinputs,
1389 IdentifierInfo **names, StringLiteral **constraints,
1390 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1391 StringLiteral **clobbers, SourceLocation rparenloc);
1393 /// \brief Build an empty inline-assembly statement.
1394 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1395 Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1397 SourceLocation getAsmLoc() const { return AsmLoc; }
1398 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1399 SourceLocation getRParenLoc() const { return RParenLoc; }
1400 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1402 bool isVolatile() const { return IsVolatile; }
1403 void setVolatile(bool V) { IsVolatile = V; }
1404 bool isSimple() const { return IsSimple; }
1405 void setSimple(bool V) { IsSimple = V; }
1406 bool isMSAsm() const { return MSAsm; }
1407 void setMSAsm(bool V) { MSAsm = V; }
1409 //===--- Asm String Analysis ---===//
1411 const StringLiteral *getAsmString() const { return AsmStr; }
1412 StringLiteral *getAsmString() { return AsmStr; }
1413 void setAsmString(StringLiteral *E) { AsmStr = E; }
1415 /// AsmStringPiece - this is part of a decomposed asm string specification
1416 /// (for use with the AnalyzeAsmString function below). An asm string is
1417 /// considered to be a concatenation of these parts.
1418 class AsmStringPiece {
1421 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1422 Operand // Operand reference, with optional modifier %c4.
1429 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1430 AsmStringPiece(unsigned OpNo, char Modifier)
1431 : MyKind(Operand), Str(), OperandNo(OpNo) {
1435 bool isString() const { return MyKind == String; }
1436 bool isOperand() const { return MyKind == Operand; }
1438 const std::string &getString() const {
1443 unsigned getOperandNo() const {
1444 assert(isOperand());
1448 /// getModifier - Get the modifier for this operand, if present. This
1449 /// returns '\0' if there was no modifier.
1450 char getModifier() const {
1451 assert(isOperand());
1456 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1457 /// it into pieces. If the asm string is erroneous, emit errors and return
1458 /// true, otherwise return false. This handles canonicalization and
1459 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1460 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1461 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1462 ASTContext &C, unsigned &DiagOffs) const;
1465 //===--- Output operands ---===//
1467 unsigned getNumOutputs() const { return NumOutputs; }
1469 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1473 StringRef getOutputName(unsigned i) const {
1474 if (IdentifierInfo *II = getOutputIdentifier(i))
1475 return II->getName();
1480 /// getOutputConstraint - Return the constraint string for the specified
1481 /// output operand. All output constraints are known to be non-empty (either
1483 StringRef getOutputConstraint(unsigned i) const;
1485 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1486 return Constraints[i];
1488 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1489 return Constraints[i];
1492 Expr *getOutputExpr(unsigned i);
1494 const Expr *getOutputExpr(unsigned i) const {
1495 return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1498 /// isOutputPlusConstraint - Return true if the specified output constraint
1499 /// is a "+" constraint (which is both an input and an output) or false if it
1500 /// is an "=" constraint (just an output).
1501 bool isOutputPlusConstraint(unsigned i) const {
1502 return getOutputConstraint(i)[0] == '+';
1505 /// getNumPlusOperands - Return the number of output operands that have a "+"
1507 unsigned getNumPlusOperands() const;
1509 //===--- Input operands ---===//
1511 unsigned getNumInputs() const { return NumInputs; }
1513 IdentifierInfo *getInputIdentifier(unsigned i) const {
1514 return Names[i + NumOutputs];
1517 StringRef getInputName(unsigned i) const {
1518 if (IdentifierInfo *II = getInputIdentifier(i))
1519 return II->getName();
1524 /// getInputConstraint - Return the specified input constraint. Unlike output
1525 /// constraints, these can be empty.
1526 StringRef getInputConstraint(unsigned i) const;
1528 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1529 return Constraints[i + NumOutputs];
1531 StringLiteral *getInputConstraintLiteral(unsigned i) {
1532 return Constraints[i + NumOutputs];
1535 Expr *getInputExpr(unsigned i);
1536 void setInputExpr(unsigned i, Expr *E);
1538 const Expr *getInputExpr(unsigned i) const {
1539 return const_cast<AsmStmt*>(this)->getInputExpr(i);
1542 void setOutputsAndInputsAndClobbers(ASTContext &C,
1543 IdentifierInfo **Names,
1544 StringLiteral **Constraints,
1546 unsigned NumOutputs,
1548 StringLiteral **Clobbers,
1549 unsigned NumClobbers);
1551 //===--- Other ---===//
1553 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1554 /// translate this into a numeric value needed to reference the same operand.
1555 /// This returns -1 if the operand name is invalid.
1556 int getNamedOperand(StringRef SymbolicName) const;
1558 unsigned getNumClobbers() const { return NumClobbers; }
1559 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1560 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1562 SourceRange getSourceRange() const LLVM_READONLY {
1563 return SourceRange(AsmLoc, RParenLoc);
1566 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1567 static bool classof(const AsmStmt *) { return true; }
1569 // Input expr iterators.
1571 typedef ExprIterator inputs_iterator;
1572 typedef ConstExprIterator const_inputs_iterator;
1574 inputs_iterator begin_inputs() {
1575 return &Exprs[0] + NumOutputs;
1578 inputs_iterator end_inputs() {
1579 return &Exprs[0] + NumOutputs + NumInputs;
1582 const_inputs_iterator begin_inputs() const {
1583 return &Exprs[0] + NumOutputs;
1586 const_inputs_iterator end_inputs() const {
1587 return &Exprs[0] + NumOutputs + NumInputs;
1590 // Output expr iterators.
1592 typedef ExprIterator outputs_iterator;
1593 typedef ConstExprIterator const_outputs_iterator;
1595 outputs_iterator begin_outputs() {
1598 outputs_iterator end_outputs() {
1599 return &Exprs[0] + NumOutputs;
1602 const_outputs_iterator begin_outputs() const {
1605 const_outputs_iterator end_outputs() const {
1606 return &Exprs[0] + NumOutputs;
1609 child_range children() {
1610 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1614 /// MSAsmStmt - This represents a MS inline-assembly statement extension.
1616 class MSAsmStmt : public Stmt {
1617 SourceLocation AsmLoc, LBraceLoc, EndLoc;
1623 unsigned NumAsmToks;
1625 unsigned NumOutputs;
1626 unsigned NumClobbers;
1629 IdentifierInfo **Names;
1631 StringRef *Clobbers;
1634 MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1635 bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1636 ArrayRef<IdentifierInfo*> inputs, ArrayRef<IdentifierInfo*> outputs,
1637 StringRef asmstr, ArrayRef<StringRef> clobbers,
1638 SourceLocation endloc);
1640 SourceLocation getAsmLoc() const { return AsmLoc; }
1641 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1642 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1643 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1644 SourceLocation getEndLoc() const { return EndLoc; }
1645 void setEndLoc(SourceLocation L) { EndLoc = L; }
1647 bool hasBraces() const { return LBraceLoc.isValid(); }
1649 unsigned getNumAsmToks() { return NumAsmToks; }
1650 Token *getAsmToks() { return AsmToks; }
1652 bool isVolatile() const { return IsVolatile; }
1653 void setVolatile(bool V) { IsVolatile = V; }
1654 bool isSimple() const { return IsSimple; }
1655 void setSimple(bool V) { IsSimple = V; }
1657 //===--- Asm String Analysis ---===//
1659 const std::string *getAsmString() const { return &AsmStr; }
1660 std::string *getAsmString() { return &AsmStr; }
1661 void setAsmString(StringRef &E) { AsmStr = E.str(); }
1663 //===--- Other ---===//
1665 unsigned getNumClobbers() const { return NumClobbers; }
1666 StringRef getClobber(unsigned i) const { return Clobbers[i]; }
1668 SourceRange getSourceRange() const LLVM_READONLY {
1669 return SourceRange(AsmLoc, EndLoc);
1671 static bool classof(const Stmt *T) {
1672 return T->getStmtClass() == MSAsmStmtClass;
1674 static bool classof(const MSAsmStmt *) { return true; }
1676 child_range children() {
1677 return child_range(&Exprs[0], &Exprs[0]);
1681 class SEHExceptStmt : public Stmt {
1685 enum { FILTER_EXPR, BLOCK };
1687 SEHExceptStmt(SourceLocation Loc,
1691 friend class ASTReader;
1692 friend class ASTStmtReader;
1693 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1696 static SEHExceptStmt* Create(ASTContext &C,
1697 SourceLocation ExceptLoc,
1700 SourceRange getSourceRange() const LLVM_READONLY {
1701 return SourceRange(getExceptLoc(), getEndLoc());
1704 SourceLocation getExceptLoc() const { return Loc; }
1705 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1707 Expr *getFilterExpr() const {
1708 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1711 CompoundStmt *getBlock() const {
1712 return llvm::cast<CompoundStmt>(Children[BLOCK]);
1715 child_range children() {
1716 return child_range(Children,Children+2);
1719 static bool classof(const Stmt *T) {
1720 return T->getStmtClass() == SEHExceptStmtClass;
1723 static bool classof(SEHExceptStmt *) { return true; }
1727 class SEHFinallyStmt : public Stmt {
1731 SEHFinallyStmt(SourceLocation Loc,
1734 friend class ASTReader;
1735 friend class ASTStmtReader;
1736 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1739 static SEHFinallyStmt* Create(ASTContext &C,
1740 SourceLocation FinallyLoc,
1743 SourceRange getSourceRange() const LLVM_READONLY {
1744 return SourceRange(getFinallyLoc(), getEndLoc());
1747 SourceLocation getFinallyLoc() const { return Loc; }
1748 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1750 CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1752 child_range children() {
1753 return child_range(&Block,&Block+1);
1756 static bool classof(const Stmt *T) {
1757 return T->getStmtClass() == SEHFinallyStmtClass;
1760 static bool classof(SEHFinallyStmt *) { return true; }
1764 class SEHTryStmt : public Stmt {
1766 SourceLocation TryLoc;
1769 enum { TRY = 0, HANDLER = 1 };
1771 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1772 SourceLocation TryLoc,
1776 friend class ASTReader;
1777 friend class ASTStmtReader;
1778 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1781 static SEHTryStmt* Create(ASTContext &C,
1783 SourceLocation TryLoc,
1787 SourceRange getSourceRange() const LLVM_READONLY {
1788 return SourceRange(getTryLoc(), getEndLoc());
1791 SourceLocation getTryLoc() const { return TryLoc; }
1792 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1794 bool getIsCXXTry() const { return IsCXXTry; }
1796 CompoundStmt* getTryBlock() const {
1797 return llvm::cast<CompoundStmt>(Children[TRY]);
1800 Stmt *getHandler() const { return Children[HANDLER]; }
1802 /// Returns 0 if not defined
1803 SEHExceptStmt *getExceptHandler() const;
1804 SEHFinallyStmt *getFinallyHandler() const;
1806 child_range children() {
1807 return child_range(Children,Children+2);
1810 static bool classof(const Stmt *T) {
1811 return T->getStmtClass() == SEHTryStmtClass;
1814 static bool classof(SEHTryStmt *) { return true; }
1817 } // end namespace clang