Stmt.h revision bb8a897f350f46475f36140480e82289f6baa020
1//===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the Stmt interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_STMT_H
15#define LLVM_CLANG_AST_STMT_H
16
17#include "clang/Basic/LLVM.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/AST/PrettyPrinter.h"
20#include "clang/AST/StmtIterator.h"
21#include "clang/AST/DeclGroup.h"
22#include "clang/AST/ASTContext.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/ADT/SmallVector.h"
25#include <string>
26
27namespace llvm {
28  class FoldingSetNodeID;
29}
30
31namespace clang {
32  class ASTContext;
33  class Expr;
34  class Decl;
35  class ParmVarDecl;
36  class QualType;
37  class IdentifierInfo;
38  class SourceManager;
39  class StringLiteral;
40  class SwitchStmt;
41
42  //===--------------------------------------------------------------------===//
43  // ExprIterator - Iterators for iterating over Stmt* arrays that contain
44  //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
45  //  references to children (to be compatible with StmtIterator).
46  //===--------------------------------------------------------------------===//
47
48  class Stmt;
49  class Expr;
50
51  class ExprIterator {
52    Stmt** I;
53  public:
54    ExprIterator(Stmt** i) : I(i) {}
55    ExprIterator() : I(0) {}
56    ExprIterator& operator++() { ++I; return *this; }
57    ExprIterator operator-(size_t i) { return I-i; }
58    ExprIterator operator+(size_t i) { return I+i; }
59    Expr* operator[](size_t idx);
60    // FIXME: Verify that this will correctly return a signed distance.
61    signed operator-(const ExprIterator& R) const { return I - R.I; }
62    Expr* operator*() const;
63    Expr* operator->() const;
64    bool operator==(const ExprIterator& R) const { return I == R.I; }
65    bool operator!=(const ExprIterator& R) const { return I != R.I; }
66    bool operator>(const ExprIterator& R) const { return I > R.I; }
67    bool operator>=(const ExprIterator& R) const { return I >= R.I; }
68  };
69
70  class ConstExprIterator {
71    const Stmt * const *I;
72  public:
73    ConstExprIterator(const Stmt * const *i) : I(i) {}
74    ConstExprIterator() : I(0) {}
75    ConstExprIterator& operator++() { ++I; return *this; }
76    ConstExprIterator operator+(size_t i) const { return I+i; }
77    ConstExprIterator operator-(size_t i) const { return I-i; }
78    const Expr * operator[](size_t idx) const;
79    signed operator-(const ConstExprIterator& R) const { return I - R.I; }
80    const Expr * operator*() const;
81    const Expr * operator->() const;
82    bool operator==(const ConstExprIterator& R) const { return I == R.I; }
83    bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
84    bool operator>(const ConstExprIterator& R) const { return I > R.I; }
85    bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
86  };
87
88//===----------------------------------------------------------------------===//
89// AST classes for statements.
90//===----------------------------------------------------------------------===//
91
92/// Stmt - This represents one statement.
93///
94class Stmt {
95public:
96  enum StmtClass {
97    NoStmtClass = 0,
98#define STMT(CLASS, PARENT) CLASS##Class,
99#define STMT_RANGE(BASE, FIRST, LAST) \
100        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
101#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
102        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
103#define ABSTRACT_STMT(STMT)
104#include "clang/AST/StmtNodes.inc"
105  };
106
107  // Make vanilla 'new' and 'delete' illegal for Stmts.
108protected:
109  void* operator new(size_t bytes) throw() {
110    llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
111  }
112  void operator delete(void* data) throw() {
113    llvm_unreachable("Stmts cannot be released with regular 'delete'.");
114  }
115
116  class StmtBitfields {
117    friend class Stmt;
118
119    /// \brief The statement class.
120    unsigned sClass : 8;
121  };
122  enum { NumStmtBits = 8 };
123
124  class CompoundStmtBitfields {
125    friend class CompoundStmt;
126    unsigned : NumStmtBits;
127
128    unsigned NumStmts : 32 - NumStmtBits;
129  };
130
131  class ExprBitfields {
132    friend class Expr;
133    friend class DeclRefExpr; // computeDependence
134    friend class InitListExpr; // ctor
135    friend class DesignatedInitExpr; // ctor
136    friend class BlockDeclRefExpr; // ctor
137    friend class ASTStmtReader; // deserialization
138    friend class CXXNewExpr; // ctor
139    friend class DependentScopeDeclRefExpr; // ctor
140    friend class CXXConstructExpr; // ctor
141    friend class CallExpr; // ctor
142    friend class OffsetOfExpr; // ctor
143    friend class ObjCMessageExpr; // ctor
144    friend class ShuffleVectorExpr; // ctor
145    friend class ParenListExpr; // ctor
146    friend class CXXUnresolvedConstructExpr; // ctor
147    friend class CXXDependentScopeMemberExpr; // ctor
148    friend class OverloadExpr; // ctor
149    friend class PseudoObjectExpr; // ctor
150    friend class AtomicExpr; // ctor
151    unsigned : NumStmtBits;
152
153    unsigned ValueKind : 2;
154    unsigned ObjectKind : 2;
155    unsigned TypeDependent : 1;
156    unsigned ValueDependent : 1;
157    unsigned InstantiationDependent : 1;
158    unsigned ContainsUnexpandedParameterPack : 1;
159  };
160  enum { NumExprBits = 16 };
161
162  class CharacterLiteralBitfields {
163    friend class CharacterLiteral;
164    unsigned : NumExprBits;
165
166    unsigned Kind : 2;
167  };
168
169  class FloatingLiteralBitfields {
170    friend class FloatingLiteral;
171    unsigned : NumExprBits;
172
173    unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
174    unsigned IsExact : 1;
175  };
176
177  class UnaryExprOrTypeTraitExprBitfields {
178    friend class UnaryExprOrTypeTraitExpr;
179    unsigned : NumExprBits;
180
181    unsigned Kind : 2;
182    unsigned IsType : 1; // true if operand is a type, false if an expression.
183  };
184
185  class DeclRefExprBitfields {
186    friend class DeclRefExpr;
187    friend class ASTStmtReader; // deserialization
188    unsigned : NumExprBits;
189
190    unsigned HasQualifier : 1;
191    unsigned HasTemplateKWAndArgsInfo : 1;
192    unsigned HasFoundDecl : 1;
193    unsigned HadMultipleCandidates : 1;
194  };
195
196  class CastExprBitfields {
197    friend class CastExpr;
198    unsigned : NumExprBits;
199
200    unsigned Kind : 6;
201    unsigned BasePathSize : 32 - 6 - NumExprBits;
202  };
203
204  class CallExprBitfields {
205    friend class CallExpr;
206    unsigned : NumExprBits;
207
208    unsigned NumPreArgs : 1;
209  };
210
211  class ExprWithCleanupsBitfields {
212    friend class ExprWithCleanups;
213    friend class ASTStmtReader; // deserialization
214
215    unsigned : NumExprBits;
216
217    unsigned NumObjects : 32 - NumExprBits;
218  };
219
220  class PseudoObjectExprBitfields {
221    friend class PseudoObjectExpr;
222    friend class ASTStmtReader; // deserialization
223
224    unsigned : NumExprBits;
225
226    // These don't need to be particularly wide, because they're
227    // strictly limited by the forms of expressions we permit.
228    unsigned NumSubExprs : 8;
229    unsigned ResultIndex : 32 - 8 - NumExprBits;
230  };
231
232  class ObjCIndirectCopyRestoreExprBitfields {
233    friend class ObjCIndirectCopyRestoreExpr;
234    unsigned : NumExprBits;
235
236    unsigned ShouldCopy : 1;
237  };
238
239  class InitListExprBitfields {
240    friend class InitListExpr;
241
242    unsigned : NumExprBits;
243
244    /// Whether this initializer list originally had a GNU array-range
245    /// designator in it. This is a temporary marker used by CodeGen.
246    unsigned HadArrayRangeDesignator : 1;
247
248    /// Whether this initializer list initializes a std::initializer_list
249    /// object.
250    unsigned InitializesStdInitializerList : 1;
251  };
252
253  class TypeTraitExprBitfields {
254    friend class TypeTraitExpr;
255    friend class ASTStmtReader;
256    friend class ASTStmtWriter;
257
258    unsigned : NumExprBits;
259
260    /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
261    unsigned Kind : 8;
262
263    /// \brief If this expression is not value-dependent, this indicates whether
264    /// the trait evaluated true or false.
265    unsigned Value : 1;
266
267    /// \brief The number of arguments to this type trait.
268    unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
269  };
270
271  union {
272    // FIXME: this is wasteful on 64-bit platforms.
273    void *Aligner;
274
275    StmtBitfields StmtBits;
276    CompoundStmtBitfields CompoundStmtBits;
277    ExprBitfields ExprBits;
278    CharacterLiteralBitfields CharacterLiteralBits;
279    FloatingLiteralBitfields FloatingLiteralBits;
280    UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
281    DeclRefExprBitfields DeclRefExprBits;
282    CastExprBitfields CastExprBits;
283    CallExprBitfields CallExprBits;
284    ExprWithCleanupsBitfields ExprWithCleanupsBits;
285    PseudoObjectExprBitfields PseudoObjectExprBits;
286    ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
287    InitListExprBitfields InitListExprBits;
288    TypeTraitExprBitfields TypeTraitExprBits;
289  };
290
291  friend class ASTStmtReader;
292  friend class ASTStmtWriter;
293
294public:
295  // Only allow allocation of Stmts using the allocator in ASTContext
296  // or by doing a placement new.
297  void* operator new(size_t bytes, ASTContext& C,
298                     unsigned alignment = 8) throw() {
299    return ::operator new(bytes, C, alignment);
300  }
301
302  void* operator new(size_t bytes, ASTContext* C,
303                     unsigned alignment = 8) throw() {
304    return ::operator new(bytes, *C, alignment);
305  }
306
307  void* operator new(size_t bytes, void* mem) throw() {
308    return mem;
309  }
310
311  void operator delete(void*, ASTContext&, unsigned) throw() { }
312  void operator delete(void*, ASTContext*, unsigned) throw() { }
313  void operator delete(void*, std::size_t) throw() { }
314  void operator delete(void*, void*) throw() { }
315
316public:
317  /// \brief A placeholder type used to construct an empty shell of a
318  /// type, that will be filled in later (e.g., by some
319  /// de-serialization).
320  struct EmptyShell { };
321
322protected:
323  /// \brief Construct an empty statement.
324  explicit Stmt(StmtClass SC, EmptyShell) {
325    StmtBits.sClass = SC;
326    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
327  }
328
329public:
330  Stmt(StmtClass SC) {
331    StmtBits.sClass = SC;
332    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
333  }
334
335  StmtClass getStmtClass() const {
336    return static_cast<StmtClass>(StmtBits.sClass);
337  }
338  const char *getStmtClassName() const;
339
340  /// SourceLocation tokens are not useful in isolation - they are low level
341  /// value objects created/interpreted by SourceManager. We assume AST
342  /// clients will have a pointer to the respective SourceManager.
343  SourceRange getSourceRange() const;
344
345  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
346  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
347
348  // global temp stats (until we have a per-module visitor)
349  static void addStmtClass(const StmtClass s);
350  static bool CollectingStats(bool Enable = false);
351  static void PrintStats();
352
353  /// dump - This does a local dump of the specified AST fragment.  It dumps the
354  /// specified node and a few nodes underneath it, but not the whole subtree.
355  /// This is useful in a debugger.
356  LLVM_ATTRIBUTE_USED void dump() const;
357  LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
358  void dump(raw_ostream &OS, SourceManager &SM) const;
359
360  /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
361  void dumpAll() const;
362  void dumpAll(SourceManager &SM) const;
363
364  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
365  /// back to its original source language syntax.
366  void dumpPretty(ASTContext& Context) const;
367  void printPretty(raw_ostream &OS, PrinterHelper *Helper,
368                   const PrintingPolicy &Policy,
369                   unsigned Indentation = 0) const {
370    printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
371  }
372  void printPretty(raw_ostream &OS, ASTContext &Context,
373                   PrinterHelper *Helper,
374                   const PrintingPolicy &Policy,
375                   unsigned Indentation = 0) const;
376
377  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
378  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
379  void viewAST() const;
380
381  /// Skip past any implicit AST nodes which might surround this
382  /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
383  Stmt *IgnoreImplicit();
384
385  const Stmt *stripLabelLikeStatements() const;
386  Stmt *stripLabelLikeStatements() {
387    return const_cast<Stmt*>(
388      const_cast<const Stmt*>(this)->stripLabelLikeStatements());
389  }
390
391  // Implement isa<T> support.
392  static bool classof(const Stmt *) { return true; }
393
394  /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
395  ///  contain implicit control-flow in the order their subexpressions
396  ///  are evaluated.  This predicate returns true if this statement has
397  ///  such implicit control-flow.  Such statements are also specially handled
398  ///  within CFGs.
399  bool hasImplicitControlFlow() const;
400
401  /// Child Iterators: All subclasses must implement 'children'
402  /// to permit easy iteration over the substatements/subexpessions of an
403  /// AST node.  This permits easy iteration over all nodes in the AST.
404  typedef StmtIterator       child_iterator;
405  typedef ConstStmtIterator  const_child_iterator;
406
407  typedef StmtRange          child_range;
408  typedef ConstStmtRange     const_child_range;
409
410  child_range children();
411  const_child_range children() const {
412    return const_cast<Stmt*>(this)->children();
413  }
414
415  child_iterator child_begin() { return children().first; }
416  child_iterator child_end() { return children().second; }
417
418  const_child_iterator child_begin() const { return children().first; }
419  const_child_iterator child_end() const { return children().second; }
420
421  /// \brief Produce a unique representation of the given statement.
422  ///
423  /// \brief ID once the profiling operation is complete, will contain
424  /// the unique representation of the given statement.
425  ///
426  /// \brief Context the AST context in which the statement resides
427  ///
428  /// \brief Canonical whether the profile should be based on the canonical
429  /// representation of this statement (e.g., where non-type template
430  /// parameters are identified by index/level rather than their
431  /// declaration pointers) or the exact representation of the statement as
432  /// written in the source.
433  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
434               bool Canonical) const;
435};
436
437/// DeclStmt - Adaptor class for mixing declarations with statements and
438/// expressions. For example, CompoundStmt mixes statements, expressions
439/// and declarations (variables, types). Another example is ForStmt, where
440/// the first statement can be an expression or a declaration.
441///
442class DeclStmt : public Stmt {
443  DeclGroupRef DG;
444  SourceLocation StartLoc, EndLoc;
445
446public:
447  DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
448           SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
449                                    StartLoc(startLoc), EndLoc(endLoc) {}
450
451  /// \brief Build an empty declaration statement.
452  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
453
454  /// isSingleDecl - This method returns true if this DeclStmt refers
455  /// to a single Decl.
456  bool isSingleDecl() const {
457    return DG.isSingleDecl();
458  }
459
460  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
461  Decl *getSingleDecl() { return DG.getSingleDecl(); }
462
463  const DeclGroupRef getDeclGroup() const { return DG; }
464  DeclGroupRef getDeclGroup() { return DG; }
465  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
466
467  SourceLocation getStartLoc() const { return StartLoc; }
468  void setStartLoc(SourceLocation L) { StartLoc = L; }
469  SourceLocation getEndLoc() const { return EndLoc; }
470  void setEndLoc(SourceLocation L) { EndLoc = L; }
471
472  SourceRange getSourceRange() const {
473    return SourceRange(StartLoc, EndLoc);
474  }
475
476  static bool classof(const Stmt *T) {
477    return T->getStmtClass() == DeclStmtClass;
478  }
479  static bool classof(const DeclStmt *) { return true; }
480
481  // Iterators over subexpressions.
482  child_range children() {
483    return child_range(child_iterator(DG.begin(), DG.end()),
484                       child_iterator(DG.end(), DG.end()));
485  }
486
487  typedef DeclGroupRef::iterator decl_iterator;
488  typedef DeclGroupRef::const_iterator const_decl_iterator;
489
490  decl_iterator decl_begin() { return DG.begin(); }
491  decl_iterator decl_end() { return DG.end(); }
492  const_decl_iterator decl_begin() const { return DG.begin(); }
493  const_decl_iterator decl_end() const { return DG.end(); }
494};
495
496/// NullStmt - This is the null statement ";": C99 6.8.3p3.
497///
498class NullStmt : public Stmt {
499  SourceLocation SemiLoc;
500
501  /// \brief True if the null statement was preceded by an empty macro, e.g:
502  /// @code
503  ///   #define CALL(x)
504  ///   CALL(0);
505  /// @endcode
506  bool HasLeadingEmptyMacro;
507public:
508  NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
509    : Stmt(NullStmtClass), SemiLoc(L),
510      HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
511
512  /// \brief Build an empty null statement.
513  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
514      HasLeadingEmptyMacro(false) { }
515
516  SourceLocation getSemiLoc() const { return SemiLoc; }
517  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
518
519  bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
520
521  SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
522
523  static bool classof(const Stmt *T) {
524    return T->getStmtClass() == NullStmtClass;
525  }
526  static bool classof(const NullStmt *) { return true; }
527
528  child_range children() { return child_range(); }
529
530  friend class ASTStmtReader;
531  friend class ASTStmtWriter;
532};
533
534/// CompoundStmt - This represents a group of statements like { stmt stmt }.
535///
536class CompoundStmt : public Stmt {
537  Stmt** Body;
538  SourceLocation LBracLoc, RBracLoc;
539public:
540  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
541               SourceLocation LB, SourceLocation RB)
542  : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
543    CompoundStmtBits.NumStmts = NumStmts;
544    assert(CompoundStmtBits.NumStmts == NumStmts &&
545           "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
546
547    if (NumStmts == 0) {
548      Body = 0;
549      return;
550    }
551
552    Body = new (C) Stmt*[NumStmts];
553    memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
554  }
555
556  // \brief Build an empty compound statement.
557  explicit CompoundStmt(EmptyShell Empty)
558    : Stmt(CompoundStmtClass, Empty), Body(0) {
559    CompoundStmtBits.NumStmts = 0;
560  }
561
562  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
563
564  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
565  unsigned size() const { return CompoundStmtBits.NumStmts; }
566
567  typedef Stmt** body_iterator;
568  body_iterator body_begin() { return Body; }
569  body_iterator body_end() { return Body + size(); }
570  Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
571
572  void setLastStmt(Stmt *S) {
573    assert(!body_empty() && "setLastStmt");
574    Body[size()-1] = S;
575  }
576
577  typedef Stmt* const * const_body_iterator;
578  const_body_iterator body_begin() const { return Body; }
579  const_body_iterator body_end() const { return Body + size(); }
580  const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
581
582  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
583  reverse_body_iterator body_rbegin() {
584    return reverse_body_iterator(body_end());
585  }
586  reverse_body_iterator body_rend() {
587    return reverse_body_iterator(body_begin());
588  }
589
590  typedef std::reverse_iterator<const_body_iterator>
591          const_reverse_body_iterator;
592
593  const_reverse_body_iterator body_rbegin() const {
594    return const_reverse_body_iterator(body_end());
595  }
596
597  const_reverse_body_iterator body_rend() const {
598    return const_reverse_body_iterator(body_begin());
599  }
600
601  SourceRange getSourceRange() const {
602    return SourceRange(LBracLoc, RBracLoc);
603  }
604
605  SourceLocation getLBracLoc() const { return LBracLoc; }
606  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
607  SourceLocation getRBracLoc() const { return RBracLoc; }
608  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
609
610  static bool classof(const Stmt *T) {
611    return T->getStmtClass() == CompoundStmtClass;
612  }
613  static bool classof(const CompoundStmt *) { return true; }
614
615  // Iterators
616  child_range children() {
617    return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
618  }
619
620  const_child_range children() const {
621    return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
622  }
623};
624
625// SwitchCase is the base class for CaseStmt and DefaultStmt,
626class SwitchCase : public Stmt {
627protected:
628  // A pointer to the following CaseStmt or DefaultStmt class,
629  // used by SwitchStmt.
630  SwitchCase *NextSwitchCase;
631
632  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
633
634public:
635  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
636
637  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
638
639  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
640
641  Stmt *getSubStmt();
642  const Stmt *getSubStmt() const {
643    return const_cast<SwitchCase*>(this)->getSubStmt();
644  }
645
646  SourceRange getSourceRange() const { return SourceRange(); }
647
648  static bool classof(const Stmt *T) {
649    return T->getStmtClass() == CaseStmtClass ||
650           T->getStmtClass() == DefaultStmtClass;
651  }
652  static bool classof(const SwitchCase *) { return true; }
653};
654
655class CaseStmt : public SwitchCase {
656  enum { LHS, RHS, SUBSTMT, END_EXPR };
657  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
658                             // GNU "case 1 ... 4" extension
659  SourceLocation CaseLoc;
660  SourceLocation EllipsisLoc;
661  SourceLocation ColonLoc;
662public:
663  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
664           SourceLocation ellipsisLoc, SourceLocation colonLoc)
665    : SwitchCase(CaseStmtClass) {
666    SubExprs[SUBSTMT] = 0;
667    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
668    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
669    CaseLoc = caseLoc;
670    EllipsisLoc = ellipsisLoc;
671    ColonLoc = colonLoc;
672  }
673
674  /// \brief Build an empty switch case statement.
675  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
676
677  SourceLocation getCaseLoc() const { return CaseLoc; }
678  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
679  SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
680  void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
681  SourceLocation getColonLoc() const { return ColonLoc; }
682  void setColonLoc(SourceLocation L) { ColonLoc = L; }
683
684  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
685  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
686  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
687
688  const Expr *getLHS() const {
689    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
690  }
691  const Expr *getRHS() const {
692    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
693  }
694  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
695
696  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
697  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
698  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
699
700
701  SourceRange getSourceRange() const {
702    // Handle deeply nested case statements with iteration instead of recursion.
703    const CaseStmt *CS = this;
704    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
705      CS = CS2;
706
707    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
708  }
709  static bool classof(const Stmt *T) {
710    return T->getStmtClass() == CaseStmtClass;
711  }
712  static bool classof(const CaseStmt *) { return true; }
713
714  // Iterators
715  child_range children() {
716    return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
717  }
718};
719
720class DefaultStmt : public SwitchCase {
721  Stmt* SubStmt;
722  SourceLocation DefaultLoc;
723  SourceLocation ColonLoc;
724public:
725  DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
726    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
727    ColonLoc(CL) {}
728
729  /// \brief Build an empty default statement.
730  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
731
732  Stmt *getSubStmt() { return SubStmt; }
733  const Stmt *getSubStmt() const { return SubStmt; }
734  void setSubStmt(Stmt *S) { SubStmt = S; }
735
736  SourceLocation getDefaultLoc() const { return DefaultLoc; }
737  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
738  SourceLocation getColonLoc() const { return ColonLoc; }
739  void setColonLoc(SourceLocation L) { ColonLoc = L; }
740
741  SourceRange getSourceRange() const {
742    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
743  }
744  static bool classof(const Stmt *T) {
745    return T->getStmtClass() == DefaultStmtClass;
746  }
747  static bool classof(const DefaultStmt *) { return true; }
748
749  // Iterators
750  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
751};
752
753
754/// LabelStmt - Represents a label, which has a substatement.  For example:
755///    foo: return;
756///
757class LabelStmt : public Stmt {
758  LabelDecl *TheDecl;
759  Stmt *SubStmt;
760  SourceLocation IdentLoc;
761public:
762  LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
763    : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
764  }
765
766  // \brief Build an empty label statement.
767  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
768
769  SourceLocation getIdentLoc() const { return IdentLoc; }
770  LabelDecl *getDecl() const { return TheDecl; }
771  void setDecl(LabelDecl *D) { TheDecl = D; }
772  const char *getName() const;
773  Stmt *getSubStmt() { return SubStmt; }
774  const Stmt *getSubStmt() const { return SubStmt; }
775  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
776  void setSubStmt(Stmt *SS) { SubStmt = SS; }
777
778  SourceRange getSourceRange() const {
779    return SourceRange(IdentLoc, SubStmt->getLocEnd());
780  }
781  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
782
783  static bool classof(const Stmt *T) {
784    return T->getStmtClass() == LabelStmtClass;
785  }
786  static bool classof(const LabelStmt *) { return true; }
787};
788
789
790/// IfStmt - This represents an if/then/else.
791///
792class IfStmt : public Stmt {
793  enum { VAR, COND, THEN, ELSE, END_EXPR };
794  Stmt* SubExprs[END_EXPR];
795
796  SourceLocation IfLoc;
797  SourceLocation ElseLoc;
798
799public:
800  IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
801         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
802
803  /// \brief Build an empty if/then/else statement
804  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
805
806  /// \brief Retrieve the variable declared in this "if" statement, if any.
807  ///
808  /// In the following example, "x" is the condition variable.
809  /// \code
810  /// if (int x = foo()) {
811  ///   printf("x is %d", x);
812  /// }
813  /// \endcode
814  VarDecl *getConditionVariable() const;
815  void setConditionVariable(ASTContext &C, VarDecl *V);
816
817  /// If this IfStmt has a condition variable, return the faux DeclStmt
818  /// associated with the creation of that condition variable.
819  const DeclStmt *getConditionVariableDeclStmt() const {
820    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
821  }
822
823  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
824  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
825  const Stmt *getThen() const { return SubExprs[THEN]; }
826  void setThen(Stmt *S) { SubExprs[THEN] = S; }
827  const Stmt *getElse() const { return SubExprs[ELSE]; }
828  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
829
830  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
831  Stmt *getThen() { return SubExprs[THEN]; }
832  Stmt *getElse() { return SubExprs[ELSE]; }
833
834  SourceLocation getIfLoc() const { return IfLoc; }
835  void setIfLoc(SourceLocation L) { IfLoc = L; }
836  SourceLocation getElseLoc() const { return ElseLoc; }
837  void setElseLoc(SourceLocation L) { ElseLoc = L; }
838
839  SourceRange getSourceRange() const {
840    if (SubExprs[ELSE])
841      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
842    else
843      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
844  }
845
846  // Iterators over subexpressions.  The iterators will include iterating
847  // over the initialization expression referenced by the condition variable.
848  child_range children() {
849    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
850  }
851
852  static bool classof(const Stmt *T) {
853    return T->getStmtClass() == IfStmtClass;
854  }
855  static bool classof(const IfStmt *) { return true; }
856};
857
858/// SwitchStmt - This represents a 'switch' stmt.
859///
860class SwitchStmt : public Stmt {
861  enum { VAR, COND, BODY, END_EXPR };
862  Stmt* SubExprs[END_EXPR];
863  // This points to a linked list of case and default statements.
864  SwitchCase *FirstCase;
865  SourceLocation SwitchLoc;
866
867  /// If the SwitchStmt is a switch on an enum value, this records whether
868  /// all the enum values were covered by CaseStmts.  This value is meant to
869  /// be a hint for possible clients.
870  unsigned AllEnumCasesCovered : 1;
871
872public:
873  SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
874
875  /// \brief Build a empty switch statement.
876  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
877
878  /// \brief Retrieve the variable declared in this "switch" statement, if any.
879  ///
880  /// In the following example, "x" is the condition variable.
881  /// \code
882  /// switch (int x = foo()) {
883  ///   case 0: break;
884  ///   // ...
885  /// }
886  /// \endcode
887  VarDecl *getConditionVariable() const;
888  void setConditionVariable(ASTContext &C, VarDecl *V);
889
890  /// If this SwitchStmt has a condition variable, return the faux DeclStmt
891  /// associated with the creation of that condition variable.
892  const DeclStmt *getConditionVariableDeclStmt() const {
893    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
894  }
895
896  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
897  const Stmt *getBody() const { return SubExprs[BODY]; }
898  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
899
900  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
901  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
902  Stmt *getBody() { return SubExprs[BODY]; }
903  void setBody(Stmt *S) { SubExprs[BODY] = S; }
904  SwitchCase *getSwitchCaseList() { return FirstCase; }
905
906  /// \brief Set the case list for this switch statement.
907  ///
908  /// The caller is responsible for incrementing the retain counts on
909  /// all of the SwitchCase statements in this list.
910  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
911
912  SourceLocation getSwitchLoc() const { return SwitchLoc; }
913  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
914
915  void setBody(Stmt *S, SourceLocation SL) {
916    SubExprs[BODY] = S;
917    SwitchLoc = SL;
918  }
919  void addSwitchCase(SwitchCase *SC) {
920    assert(!SC->getNextSwitchCase()
921           && "case/default already added to a switch");
922    SC->setNextSwitchCase(FirstCase);
923    FirstCase = SC;
924  }
925
926  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
927  /// switch over an enum value then all cases have been explicitly covered.
928  void setAllEnumCasesCovered() {
929    AllEnumCasesCovered = 1;
930  }
931
932  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
933  /// have been explicitly covered.
934  bool isAllEnumCasesCovered() const {
935    return (bool) AllEnumCasesCovered;
936  }
937
938  SourceRange getSourceRange() const {
939    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
940  }
941  // Iterators
942  child_range children() {
943    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
944  }
945
946  static bool classof(const Stmt *T) {
947    return T->getStmtClass() == SwitchStmtClass;
948  }
949  static bool classof(const SwitchStmt *) { return true; }
950};
951
952
953/// WhileStmt - This represents a 'while' stmt.
954///
955class WhileStmt : public Stmt {
956  enum { VAR, COND, BODY, END_EXPR };
957  Stmt* SubExprs[END_EXPR];
958  SourceLocation WhileLoc;
959public:
960  WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
961            SourceLocation WL);
962
963  /// \brief Build an empty while statement.
964  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
965
966  /// \brief Retrieve the variable declared in this "while" statement, if any.
967  ///
968  /// In the following example, "x" is the condition variable.
969  /// \code
970  /// while (int x = random()) {
971  ///   // ...
972  /// }
973  /// \endcode
974  VarDecl *getConditionVariable() const;
975  void setConditionVariable(ASTContext &C, VarDecl *V);
976
977  /// If this WhileStmt has a condition variable, return the faux DeclStmt
978  /// associated with the creation of that condition variable.
979  const DeclStmt *getConditionVariableDeclStmt() const {
980    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
981  }
982
983  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
984  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
985  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
986  Stmt *getBody() { return SubExprs[BODY]; }
987  const Stmt *getBody() const { return SubExprs[BODY]; }
988  void setBody(Stmt *S) { SubExprs[BODY] = S; }
989
990  SourceLocation getWhileLoc() const { return WhileLoc; }
991  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
992
993  SourceRange getSourceRange() const {
994    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
995  }
996  static bool classof(const Stmt *T) {
997    return T->getStmtClass() == WhileStmtClass;
998  }
999  static bool classof(const WhileStmt *) { return true; }
1000
1001  // Iterators
1002  child_range children() {
1003    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1004  }
1005};
1006
1007/// DoStmt - This represents a 'do/while' stmt.
1008///
1009class DoStmt : public Stmt {
1010  enum { BODY, COND, END_EXPR };
1011  Stmt* SubExprs[END_EXPR];
1012  SourceLocation DoLoc;
1013  SourceLocation WhileLoc;
1014  SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
1015
1016public:
1017  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1018         SourceLocation RP)
1019    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1020    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1021    SubExprs[BODY] = body;
1022  }
1023
1024  /// \brief Build an empty do-while statement.
1025  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1026
1027  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1028  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1029  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1030  Stmt *getBody() { return SubExprs[BODY]; }
1031  const Stmt *getBody() const { return SubExprs[BODY]; }
1032  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1033
1034  SourceLocation getDoLoc() const { return DoLoc; }
1035  void setDoLoc(SourceLocation L) { DoLoc = L; }
1036  SourceLocation getWhileLoc() const { return WhileLoc; }
1037  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1038
1039  SourceLocation getRParenLoc() const { return RParenLoc; }
1040  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1041
1042  SourceRange getSourceRange() const {
1043    return SourceRange(DoLoc, RParenLoc);
1044  }
1045  static bool classof(const Stmt *T) {
1046    return T->getStmtClass() == DoStmtClass;
1047  }
1048  static bool classof(const DoStmt *) { return true; }
1049
1050  // Iterators
1051  child_range children() {
1052    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1053  }
1054};
1055
1056
1057/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
1058/// the init/cond/inc parts of the ForStmt will be null if they were not
1059/// specified in the source.
1060///
1061class ForStmt : public Stmt {
1062  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1063  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1064  SourceLocation ForLoc;
1065  SourceLocation LParenLoc, RParenLoc;
1066
1067public:
1068  ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1069          Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1070
1071  /// \brief Build an empty for statement.
1072  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1073
1074  Stmt *getInit() { return SubExprs[INIT]; }
1075
1076  /// \brief Retrieve the variable declared in this "for" statement, if any.
1077  ///
1078  /// In the following example, "y" is the condition variable.
1079  /// \code
1080  /// for (int x = random(); int y = mangle(x); ++x) {
1081  ///   // ...
1082  /// }
1083  /// \endcode
1084  VarDecl *getConditionVariable() const;
1085  void setConditionVariable(ASTContext &C, VarDecl *V);
1086
1087  /// If this ForStmt has a condition variable, return the faux DeclStmt
1088  /// associated with the creation of that condition variable.
1089  const DeclStmt *getConditionVariableDeclStmt() const {
1090    return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1091  }
1092
1093  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1094  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1095  Stmt *getBody() { return SubExprs[BODY]; }
1096
1097  const Stmt *getInit() const { return SubExprs[INIT]; }
1098  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1099  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1100  const Stmt *getBody() const { return SubExprs[BODY]; }
1101
1102  void setInit(Stmt *S) { SubExprs[INIT] = S; }
1103  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1104  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1105  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1106
1107  SourceLocation getForLoc() const { return ForLoc; }
1108  void setForLoc(SourceLocation L) { ForLoc = L; }
1109  SourceLocation getLParenLoc() const { return LParenLoc; }
1110  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1111  SourceLocation getRParenLoc() const { return RParenLoc; }
1112  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1113
1114  SourceRange getSourceRange() const {
1115    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1116  }
1117  static bool classof(const Stmt *T) {
1118    return T->getStmtClass() == ForStmtClass;
1119  }
1120  static bool classof(const ForStmt *) { return true; }
1121
1122  // Iterators
1123  child_range children() {
1124    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1125  }
1126};
1127
1128/// GotoStmt - This represents a direct goto.
1129///
1130class GotoStmt : public Stmt {
1131  LabelDecl *Label;
1132  SourceLocation GotoLoc;
1133  SourceLocation LabelLoc;
1134public:
1135  GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1136    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1137
1138  /// \brief Build an empty goto statement.
1139  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1140
1141  LabelDecl *getLabel() const { return Label; }
1142  void setLabel(LabelDecl *D) { Label = D; }
1143
1144  SourceLocation getGotoLoc() const { return GotoLoc; }
1145  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1146  SourceLocation getLabelLoc() const { return LabelLoc; }
1147  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1148
1149  SourceRange getSourceRange() const {
1150    return SourceRange(GotoLoc, LabelLoc);
1151  }
1152  static bool classof(const Stmt *T) {
1153    return T->getStmtClass() == GotoStmtClass;
1154  }
1155  static bool classof(const GotoStmt *) { return true; }
1156
1157  // Iterators
1158  child_range children() { return child_range(); }
1159};
1160
1161/// IndirectGotoStmt - This represents an indirect goto.
1162///
1163class IndirectGotoStmt : public Stmt {
1164  SourceLocation GotoLoc;
1165  SourceLocation StarLoc;
1166  Stmt *Target;
1167public:
1168  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1169                   Expr *target)
1170    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1171      Target((Stmt*)target) {}
1172
1173  /// \brief Build an empty indirect goto statement.
1174  explicit IndirectGotoStmt(EmptyShell Empty)
1175    : Stmt(IndirectGotoStmtClass, Empty) { }
1176
1177  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1178  SourceLocation getGotoLoc() const { return GotoLoc; }
1179  void setStarLoc(SourceLocation L) { StarLoc = L; }
1180  SourceLocation getStarLoc() const { return StarLoc; }
1181
1182  Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1183  const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1184  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1185
1186  /// getConstantTarget - Returns the fixed target of this indirect
1187  /// goto, if one exists.
1188  LabelDecl *getConstantTarget();
1189  const LabelDecl *getConstantTarget() const {
1190    return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1191  }
1192
1193  SourceRange getSourceRange() const {
1194    return SourceRange(GotoLoc, Target->getLocEnd());
1195  }
1196
1197  static bool classof(const Stmt *T) {
1198    return T->getStmtClass() == IndirectGotoStmtClass;
1199  }
1200  static bool classof(const IndirectGotoStmt *) { return true; }
1201
1202  // Iterators
1203  child_range children() { return child_range(&Target, &Target+1); }
1204};
1205
1206
1207/// ContinueStmt - This represents a continue.
1208///
1209class ContinueStmt : public Stmt {
1210  SourceLocation ContinueLoc;
1211public:
1212  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1213
1214  /// \brief Build an empty continue statement.
1215  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1216
1217  SourceLocation getContinueLoc() const { return ContinueLoc; }
1218  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1219
1220  SourceRange getSourceRange() const {
1221    return SourceRange(ContinueLoc);
1222  }
1223
1224  static bool classof(const Stmt *T) {
1225    return T->getStmtClass() == ContinueStmtClass;
1226  }
1227  static bool classof(const ContinueStmt *) { return true; }
1228
1229  // Iterators
1230  child_range children() { return child_range(); }
1231};
1232
1233/// BreakStmt - This represents a break.
1234///
1235class BreakStmt : public Stmt {
1236  SourceLocation BreakLoc;
1237public:
1238  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1239
1240  /// \brief Build an empty break statement.
1241  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1242
1243  SourceLocation getBreakLoc() const { return BreakLoc; }
1244  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1245
1246  SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
1247
1248  static bool classof(const Stmt *T) {
1249    return T->getStmtClass() == BreakStmtClass;
1250  }
1251  static bool classof(const BreakStmt *) { return true; }
1252
1253  // Iterators
1254  child_range children() { return child_range(); }
1255};
1256
1257
1258/// ReturnStmt - This represents a return, optionally of an expression:
1259///   return;
1260///   return 4;
1261///
1262/// Note that GCC allows return with no argument in a function declared to
1263/// return a value, and it allows returning a value in functions declared to
1264/// return void.  We explicitly model this in the AST, which means you can't
1265/// depend on the return type of the function and the presence of an argument.
1266///
1267class ReturnStmt : public Stmt {
1268  Stmt *RetExpr;
1269  SourceLocation RetLoc;
1270  const VarDecl *NRVOCandidate;
1271
1272public:
1273  ReturnStmt(SourceLocation RL)
1274    : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1275
1276  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1277    : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1278      NRVOCandidate(NRVOCandidate) {}
1279
1280  /// \brief Build an empty return expression.
1281  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1282
1283  const Expr *getRetValue() const;
1284  Expr *getRetValue();
1285  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1286
1287  SourceLocation getReturnLoc() const { return RetLoc; }
1288  void setReturnLoc(SourceLocation L) { RetLoc = L; }
1289
1290  /// \brief Retrieve the variable that might be used for the named return
1291  /// value optimization.
1292  ///
1293  /// The optimization itself can only be performed if the variable is
1294  /// also marked as an NRVO object.
1295  const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1296  void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1297
1298  SourceRange getSourceRange() const;
1299
1300  static bool classof(const Stmt *T) {
1301    return T->getStmtClass() == ReturnStmtClass;
1302  }
1303  static bool classof(const ReturnStmt *) { return true; }
1304
1305  // Iterators
1306  child_range children() {
1307    if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1308    return child_range();
1309  }
1310};
1311
1312/// AsmStmt - This represents a GNU inline-assembly statement extension.
1313///
1314class AsmStmt : public Stmt {
1315  SourceLocation AsmLoc, RParenLoc;
1316  StringLiteral *AsmStr;
1317
1318  bool IsSimple;
1319  bool IsVolatile;
1320  bool MSAsm;
1321
1322  unsigned NumOutputs;
1323  unsigned NumInputs;
1324  unsigned NumClobbers;
1325
1326  // FIXME: If we wanted to, we could allocate all of these in one big array.
1327  IdentifierInfo **Names;
1328  StringLiteral **Constraints;
1329  Stmt **Exprs;
1330  StringLiteral **Clobbers;
1331
1332public:
1333  AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1334          bool msasm, unsigned numoutputs, unsigned numinputs,
1335          IdentifierInfo **names, StringLiteral **constraints,
1336          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1337          StringLiteral **clobbers, SourceLocation rparenloc);
1338
1339  /// \brief Build an empty inline-assembly statement.
1340  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1341    Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1342
1343  SourceLocation getAsmLoc() const { return AsmLoc; }
1344  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1345  SourceLocation getRParenLoc() const { return RParenLoc; }
1346  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1347
1348  bool isVolatile() const { return IsVolatile; }
1349  void setVolatile(bool V) { IsVolatile = V; }
1350  bool isSimple() const { return IsSimple; }
1351  void setSimple(bool V) { IsSimple = V; }
1352  bool isMSAsm() const { return MSAsm; }
1353  void setMSAsm(bool V) { MSAsm = V; }
1354
1355  //===--- Asm String Analysis ---===//
1356
1357  const StringLiteral *getAsmString() const { return AsmStr; }
1358  StringLiteral *getAsmString() { return AsmStr; }
1359  void setAsmString(StringLiteral *E) { AsmStr = E; }
1360
1361  /// AsmStringPiece - this is part of a decomposed asm string specification
1362  /// (for use with the AnalyzeAsmString function below).  An asm string is
1363  /// considered to be a concatenation of these parts.
1364  class AsmStringPiece {
1365  public:
1366    enum Kind {
1367      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1368      Operand  // Operand reference, with optional modifier %c4.
1369    };
1370  private:
1371    Kind MyKind;
1372    std::string Str;
1373    unsigned OperandNo;
1374  public:
1375    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1376    AsmStringPiece(unsigned OpNo, char Modifier)
1377      : MyKind(Operand), Str(), OperandNo(OpNo) {
1378      Str += Modifier;
1379    }
1380
1381    bool isString() const { return MyKind == String; }
1382    bool isOperand() const { return MyKind == Operand; }
1383
1384    const std::string &getString() const {
1385      assert(isString());
1386      return Str;
1387    }
1388
1389    unsigned getOperandNo() const {
1390      assert(isOperand());
1391      return OperandNo;
1392    }
1393
1394    /// getModifier - Get the modifier for this operand, if present.  This
1395    /// returns '\0' if there was no modifier.
1396    char getModifier() const {
1397      assert(isOperand());
1398      return Str[0];
1399    }
1400  };
1401
1402  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1403  /// it into pieces.  If the asm string is erroneous, emit errors and return
1404  /// true, otherwise return false.  This handles canonicalization and
1405  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1406  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1407  unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1408                            ASTContext &C, unsigned &DiagOffs) const;
1409
1410
1411  //===--- Output operands ---===//
1412
1413  unsigned getNumOutputs() const { return NumOutputs; }
1414
1415  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1416    return Names[i];
1417  }
1418
1419  StringRef getOutputName(unsigned i) const {
1420    if (IdentifierInfo *II = getOutputIdentifier(i))
1421      return II->getName();
1422
1423    return StringRef();
1424  }
1425
1426  /// getOutputConstraint - Return the constraint string for the specified
1427  /// output operand.  All output constraints are known to be non-empty (either
1428  /// '=' or '+').
1429  StringRef getOutputConstraint(unsigned i) const;
1430
1431  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1432    return Constraints[i];
1433  }
1434  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1435    return Constraints[i];
1436  }
1437
1438  Expr *getOutputExpr(unsigned i);
1439
1440  const Expr *getOutputExpr(unsigned i) const {
1441    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1442  }
1443
1444  /// isOutputPlusConstraint - Return true if the specified output constraint
1445  /// is a "+" constraint (which is both an input and an output) or false if it
1446  /// is an "=" constraint (just an output).
1447  bool isOutputPlusConstraint(unsigned i) const {
1448    return getOutputConstraint(i)[0] == '+';
1449  }
1450
1451  /// getNumPlusOperands - Return the number of output operands that have a "+"
1452  /// constraint.
1453  unsigned getNumPlusOperands() const;
1454
1455  //===--- Input operands ---===//
1456
1457  unsigned getNumInputs() const { return NumInputs; }
1458
1459  IdentifierInfo *getInputIdentifier(unsigned i) const {
1460    return Names[i + NumOutputs];
1461  }
1462
1463  StringRef getInputName(unsigned i) const {
1464    if (IdentifierInfo *II = getInputIdentifier(i))
1465      return II->getName();
1466
1467    return StringRef();
1468  }
1469
1470  /// getInputConstraint - Return the specified input constraint.  Unlike output
1471  /// constraints, these can be empty.
1472  StringRef getInputConstraint(unsigned i) const;
1473
1474  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1475    return Constraints[i + NumOutputs];
1476  }
1477  StringLiteral *getInputConstraintLiteral(unsigned i) {
1478    return Constraints[i + NumOutputs];
1479  }
1480
1481  Expr *getInputExpr(unsigned i);
1482  void setInputExpr(unsigned i, Expr *E);
1483
1484  const Expr *getInputExpr(unsigned i) const {
1485    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1486  }
1487
1488  void setOutputsAndInputsAndClobbers(ASTContext &C,
1489                                      IdentifierInfo **Names,
1490                                      StringLiteral **Constraints,
1491                                      Stmt **Exprs,
1492                                      unsigned NumOutputs,
1493                                      unsigned NumInputs,
1494                                      StringLiteral **Clobbers,
1495                                      unsigned NumClobbers);
1496
1497  //===--- Other ---===//
1498
1499  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1500  /// translate this into a numeric value needed to reference the same operand.
1501  /// This returns -1 if the operand name is invalid.
1502  int getNamedOperand(StringRef SymbolicName) const;
1503
1504  unsigned getNumClobbers() const { return NumClobbers; }
1505  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1506  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1507
1508  SourceRange getSourceRange() const {
1509    return SourceRange(AsmLoc, RParenLoc);
1510  }
1511
1512  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1513  static bool classof(const AsmStmt *) { return true; }
1514
1515  // Input expr iterators.
1516
1517  typedef ExprIterator inputs_iterator;
1518  typedef ConstExprIterator const_inputs_iterator;
1519
1520  inputs_iterator begin_inputs() {
1521    return &Exprs[0] + NumOutputs;
1522  }
1523
1524  inputs_iterator end_inputs() {
1525    return &Exprs[0] + NumOutputs + NumInputs;
1526  }
1527
1528  const_inputs_iterator begin_inputs() const {
1529    return &Exprs[0] + NumOutputs;
1530  }
1531
1532  const_inputs_iterator end_inputs() const {
1533    return &Exprs[0] + NumOutputs + NumInputs;
1534  }
1535
1536  // Output expr iterators.
1537
1538  typedef ExprIterator outputs_iterator;
1539  typedef ConstExprIterator const_outputs_iterator;
1540
1541  outputs_iterator begin_outputs() {
1542    return &Exprs[0];
1543  }
1544  outputs_iterator end_outputs() {
1545    return &Exprs[0] + NumOutputs;
1546  }
1547
1548  const_outputs_iterator begin_outputs() const {
1549    return &Exprs[0];
1550  }
1551  const_outputs_iterator end_outputs() const {
1552    return &Exprs[0] + NumOutputs;
1553  }
1554
1555  child_range children() {
1556    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1557  }
1558};
1559
1560class SEHExceptStmt : public Stmt {
1561  SourceLocation  Loc;
1562  Stmt           *Children[2];
1563
1564  enum { FILTER_EXPR, BLOCK };
1565
1566  SEHExceptStmt(SourceLocation Loc,
1567                Expr *FilterExpr,
1568                Stmt *Block);
1569
1570  friend class ASTReader;
1571  friend class ASTStmtReader;
1572  explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1573
1574public:
1575  static SEHExceptStmt* Create(ASTContext &C,
1576                               SourceLocation ExceptLoc,
1577                               Expr *FilterExpr,
1578                               Stmt *Block);
1579  SourceRange getSourceRange() const {
1580    return SourceRange(getExceptLoc(), getEndLoc());
1581  }
1582
1583  SourceLocation getExceptLoc() const { return Loc; }
1584  SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1585
1586  Expr *getFilterExpr() const {
1587    return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1588  }
1589
1590  CompoundStmt *getBlock() const {
1591    return llvm::cast<CompoundStmt>(Children[BLOCK]);
1592  }
1593
1594  child_range children() {
1595    return child_range(Children,Children+2);
1596  }
1597
1598  static bool classof(const Stmt *T) {
1599    return T->getStmtClass() == SEHExceptStmtClass;
1600  }
1601
1602  static bool classof(SEHExceptStmt *) { return true; }
1603
1604};
1605
1606class SEHFinallyStmt : public Stmt {
1607  SourceLocation  Loc;
1608  Stmt           *Block;
1609
1610  SEHFinallyStmt(SourceLocation Loc,
1611                 Stmt *Block);
1612
1613  friend class ASTReader;
1614  friend class ASTStmtReader;
1615  explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1616
1617public:
1618  static SEHFinallyStmt* Create(ASTContext &C,
1619                                SourceLocation FinallyLoc,
1620                                Stmt *Block);
1621
1622  SourceRange getSourceRange() const {
1623    return SourceRange(getFinallyLoc(), getEndLoc());
1624  }
1625
1626  SourceLocation getFinallyLoc() const { return Loc; }
1627  SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1628
1629  CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1630
1631  child_range children() {
1632    return child_range(&Block,&Block+1);
1633  }
1634
1635  static bool classof(const Stmt *T) {
1636    return T->getStmtClass() == SEHFinallyStmtClass;
1637  }
1638
1639  static bool classof(SEHFinallyStmt *) { return true; }
1640
1641};
1642
1643class SEHTryStmt : public Stmt {
1644  bool            IsCXXTry;
1645  SourceLocation  TryLoc;
1646  Stmt           *Children[2];
1647
1648  enum { TRY = 0, HANDLER = 1 };
1649
1650  SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1651             SourceLocation TryLoc,
1652             Stmt *TryBlock,
1653             Stmt *Handler);
1654
1655  friend class ASTReader;
1656  friend class ASTStmtReader;
1657  explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1658
1659public:
1660  static SEHTryStmt* Create(ASTContext &C,
1661                            bool isCXXTry,
1662                            SourceLocation TryLoc,
1663                            Stmt *TryBlock,
1664                            Stmt *Handler);
1665
1666  SourceRange getSourceRange() const {
1667    return SourceRange(getTryLoc(), getEndLoc());
1668  }
1669
1670  SourceLocation getTryLoc() const { return TryLoc; }
1671  SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1672
1673  bool getIsCXXTry() const { return IsCXXTry; }
1674
1675  CompoundStmt* getTryBlock() const {
1676    return llvm::cast<CompoundStmt>(Children[TRY]);
1677  }
1678
1679  Stmt *getHandler() const { return Children[HANDLER]; }
1680
1681  /// Returns 0 if not defined
1682  SEHExceptStmt  *getExceptHandler() const;
1683  SEHFinallyStmt *getFinallyHandler() const;
1684
1685  child_range children() {
1686    return child_range(Children,Children+2);
1687  }
1688
1689  static bool classof(const Stmt *T) {
1690    return T->getStmtClass() == SEHTryStmtClass;
1691  }
1692
1693  static bool classof(SEHTryStmt *) { return true; }
1694};
1695
1696}  // end namespace clang
1697
1698#endif
1699