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