Stmt.h revision cd7d5a9dc558178ed7a66032f888781b3c592e4f
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 "llvm/Support/Casting.h"
18#include "llvm/Support/raw_ostream.h"
19#include "clang/Basic/SourceLocation.h"
20#include "clang/AST/StmtIterator.h"
21#include "clang/AST/DeclGroup.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/iterator.h"
24#include "llvm/Bitcode/SerializationFwd.h"
25#include "clang/AST/ASTContext.h"
26#include <string>
27using llvm::dyn_cast_or_null;
28
29namespace clang {
30  class ASTContext;
31  class Expr;
32  class Decl;
33  class ParmVarDecl;
34  class QualType;
35  class IdentifierInfo;
36  class SourceManager;
37  class StringLiteral;
38  class SwitchStmt;
39  class PrinterHelper;
40
41  //===----------------------------------------------------------------------===//
42  // ExprIterator - Iterators for iterating over Stmt* arrays that contain
43  //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
44  //  references to children (to be compatible with StmtIterator).
45  //===----------------------------------------------------------------------===//
46
47  class Stmt;
48  class Expr;
49
50  class ExprIterator {
51    Stmt** I;
52  public:
53    ExprIterator(Stmt** i) : I(i) {}
54    ExprIterator() : I(0) {}
55    ExprIterator& operator++() { ++I; return *this; }
56    ExprIterator operator-(size_t i) { return I-i; }
57    ExprIterator operator+(size_t i) { return I+i; }
58    Expr* operator[](size_t idx);
59    // FIXME: Verify that this will correctly return a signed distance.
60    signed operator-(const ExprIterator& R) const { return I - R.I; }
61    Expr* operator*() const;
62    Expr* operator->() const;
63    bool operator==(const ExprIterator& R) const { return I == R.I; }
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  };
68
69  class ConstExprIterator {
70    Stmt* const * I;
71  public:
72    ConstExprIterator(Stmt* const* i) : I(i) {}
73    ConstExprIterator() : I(0) {}
74    ConstExprIterator& operator++() { ++I; return *this; }
75    ConstExprIterator operator+(size_t i) { return I+i; }
76    ConstExprIterator operator-(size_t i) { return I-i; }
77    const Expr * operator[](size_t idx) const;
78    signed operator-(const ConstExprIterator& R) const { return I - R.I; }
79    const Expr * operator*() const;
80    const Expr * operator->() const;
81    bool operator==(const ConstExprIterator& R) const { return I == R.I; }
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  };
86
87//===----------------------------------------------------------------------===//
88// AST classes for statements.
89//===----------------------------------------------------------------------===//
90
91/// Stmt - This represents one statement.
92///
93class Stmt {
94public:
95  enum StmtClass {
96    NoStmtClass = 0,
97#define STMT(CLASS, PARENT) CLASS##Class,
98#define FIRST_STMT(CLASS) firstStmtConstant = CLASS##Class,
99#define LAST_STMT(CLASS) lastStmtConstant = CLASS##Class,
100#define FIRST_EXPR(CLASS) firstExprConstant = CLASS##Class,
101#define LAST_EXPR(CLASS) lastExprConstant = CLASS##Class
102#include "clang/AST/StmtNodes.def"
103};
104private:
105  const StmtClass sClass;
106
107  // Make vanilla 'new' and 'delete' illegal for Stmts.
108protected:
109  void* operator new(size_t bytes) throw() {
110    assert(0 && "Stmts cannot be allocated with regular 'new'.");
111    return 0;
112  }
113  void operator delete(void* data) throw() {
114    assert(0 && "Stmts cannot be released with regular 'delete'.");
115  }
116
117public:
118  // Only allow allocation of Stmts using the allocator in ASTContext
119  // or by doing a placement new.
120  void* operator new(size_t bytes, ASTContext& C,
121                     unsigned alignment = 16) throw() {
122    return ::operator new(bytes, C, alignment);
123  }
124
125  void* operator new(size_t bytes, ASTContext* C,
126                     unsigned alignment = 16) throw() {
127    return ::operator new(bytes, *C, alignment);
128  }
129
130  void* operator new(size_t bytes, void* mem) throw() {
131    return mem;
132  }
133
134  void operator delete(void*, ASTContext&, unsigned) throw() { }
135  void operator delete(void*, ASTContext*, unsigned) throw() { }
136  void operator delete(void*, std::size_t) throw() { }
137  void operator delete(void*, void*) throw() { }
138
139public:
140  /// \brief A placeholder type used to construct an empty shell of a
141  /// type, that will be filled in later (e.g., by some
142  /// de-serialization).
143  struct EmptyShell { };
144
145protected:
146  /// DestroyChildren - Invoked by destructors of subclasses of Stmt to
147  ///  recursively release child AST nodes.
148  void DestroyChildren(ASTContext& Ctx);
149
150  /// \brief Construct an empty statement.
151  explicit Stmt(StmtClass SC, EmptyShell) : sClass(SC) {
152    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
153  }
154
155public:
156  Stmt(StmtClass SC) : sClass(SC) {
157    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
158  }
159  virtual ~Stmt() {}
160
161  virtual void Destroy(ASTContext &Ctx);
162
163  StmtClass getStmtClass() const { return sClass; }
164  const char *getStmtClassName() const;
165
166  /// SourceLocation tokens are not useful in isolation - they are low level
167  /// value objects created/interpreted by SourceManager. We assume AST
168  /// clients will have a pointer to the respective SourceManager.
169  virtual SourceRange getSourceRange() const = 0;
170  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
171  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
172
173  // global temp stats (until we have a per-module visitor)
174  static void addStmtClass(const StmtClass s);
175  static bool CollectingStats(bool enable=false);
176  static void PrintStats();
177
178  /// dump - This does a local dump of the specified AST fragment.  It dumps the
179  /// specified node and a few nodes underneath it, but not the whole subtree.
180  /// This is useful in a debugger.
181  void dump() const;
182  void dump(SourceManager &SM) const;
183
184  /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
185  void dumpAll() const;
186  void dumpAll(SourceManager &SM) const;
187
188  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
189  /// back to its original source language syntax.
190  void dumpPretty() const;
191  void printPretty(llvm::raw_ostream &OS, PrinterHelper* = NULL, unsigned = 0,
192                   bool NoIndent=false) const;
193
194  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
195  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
196  void viewAST() const;
197
198  // Implement isa<T> support.
199  static bool classof(const Stmt *) { return true; }
200
201  /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
202  ///  contain implicit control-flow in the order their subexpressions
203  ///  are evaluated.  This predicate returns true if this statement has
204  ///  such implicit control-flow.  Such statements are also specially handled
205  ///  within CFGs.
206  bool hasImplicitControlFlow() const;
207
208  /// Child Iterators: All subclasses must implement child_begin and child_end
209  ///  to permit easy iteration over the substatements/subexpessions of an
210  ///  AST node.  This permits easy iteration over all nodes in the AST.
211  typedef StmtIterator       child_iterator;
212  typedef ConstStmtIterator  const_child_iterator;
213
214  virtual child_iterator child_begin() = 0;
215  virtual child_iterator child_end()   = 0;
216
217  const_child_iterator child_begin() const {
218    return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
219  }
220
221  const_child_iterator child_end() const {
222    return const_child_iterator(const_cast<Stmt*>(this)->child_end());
223  }
224
225  void Emit(llvm::Serializer& S) const;
226  static Stmt* Create(llvm::Deserializer& D, ASTContext& C);
227
228  virtual void EmitImpl(llvm::Serializer& S) const {
229    // This method will eventually be a pure-virtual function.
230    assert (false && "Not implemented.");
231  }
232};
233
234/// DeclStmt - Adaptor class for mixing declarations with statements and
235/// expressions. For example, CompoundStmt mixes statements, expressions
236/// and declarations (variables, types). Another example is ForStmt, where
237/// the first statement can be an expression or a declaration.
238///
239class DeclStmt : public Stmt {
240  DeclGroupRef DG;
241  SourceLocation StartLoc, EndLoc;
242public:
243  DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
244           SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
245                                    StartLoc(startLoc), EndLoc(endLoc) {}
246
247  /// \brief Build an empty declaration statement.
248  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
249
250  virtual void Destroy(ASTContext& Ctx);
251
252  /// isSingleDecl - This method returns true if this DeclStmt refers
253  /// to a single Decl.
254  bool isSingleDecl() const {
255    return DG.isSingleDecl();
256  }
257
258  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
259  Decl *getSingleDecl() { return DG.getSingleDecl(); }
260
261  const DeclGroupRef getDeclGroup() const { return DG; }
262  DeclGroupRef getDeclGroup() { return DG; }
263  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
264
265  SourceLocation getStartLoc() const { return StartLoc; }
266  void setStartLoc(SourceLocation L) { StartLoc = L; }
267  SourceLocation getEndLoc() const { return EndLoc; }
268  void setEndLoc(SourceLocation L) { EndLoc = L; }
269
270  SourceRange getSourceRange() const {
271    return SourceRange(StartLoc, EndLoc);
272  }
273
274  static bool classof(const Stmt *T) {
275    return T->getStmtClass() == DeclStmtClass;
276  }
277  static bool classof(const DeclStmt *) { return true; }
278
279  // Iterators over subexpressions.
280  virtual child_iterator child_begin();
281  virtual child_iterator child_end();
282
283  typedef DeclGroupRef::iterator decl_iterator;
284  typedef DeclGroupRef::const_iterator const_decl_iterator;
285
286  decl_iterator decl_begin() { return DG.begin(); }
287  decl_iterator decl_end() { return DG.end(); }
288  const_decl_iterator decl_begin() const { return DG.begin(); }
289  const_decl_iterator decl_end() const { return DG.end(); }
290
291  // Serialization.
292  virtual void EmitImpl(llvm::Serializer& S) const;
293  static DeclStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
294};
295
296/// NullStmt - This is the null statement ";": C99 6.8.3p3.
297///
298class NullStmt : public Stmt {
299  SourceLocation SemiLoc;
300public:
301  NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
302
303  /// \brief Build an empty null statement.
304  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
305
306  SourceLocation getSemiLoc() const { return SemiLoc; }
307  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
308
309  virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
310
311  static bool classof(const Stmt *T) {
312    return T->getStmtClass() == NullStmtClass;
313  }
314  static bool classof(const NullStmt *) { return true; }
315
316  // Iterators
317  virtual child_iterator child_begin();
318  virtual child_iterator child_end();
319
320  virtual void EmitImpl(llvm::Serializer& S) const;
321  static NullStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
322};
323
324/// CompoundStmt - This represents a group of statements like { stmt stmt }.
325///
326class CompoundStmt : public Stmt {
327  Stmt** Body;
328  unsigned NumStmts;
329  SourceLocation LBracLoc, RBracLoc;
330public:
331  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts,
332                             SourceLocation LB, SourceLocation RB)
333  : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) {
334    if (NumStmts == 0) {
335      Body = 0;
336      return;
337    }
338
339    Body = new (C) Stmt*[NumStmts];
340    memcpy(Body, StmtStart, numStmts * sizeof(*Body));
341  }
342
343  // \brief Build an empty compound statement.
344  explicit CompoundStmt(EmptyShell Empty)
345    : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { }
346
347  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
348
349  bool body_empty() const { return NumStmts == 0; }
350  unsigned size() const { return NumStmts; }
351
352  typedef Stmt** body_iterator;
353  body_iterator body_begin() { return Body; }
354  body_iterator body_end() { return Body + NumStmts; }
355  Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; }
356
357  typedef Stmt* const * const_body_iterator;
358  const_body_iterator body_begin() const { return Body; }
359  const_body_iterator body_end() const { return Body + NumStmts; }
360  const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; }
361
362  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
363  reverse_body_iterator body_rbegin() {
364    return reverse_body_iterator(body_end());
365  }
366  reverse_body_iterator body_rend() {
367    return reverse_body_iterator(body_begin());
368  }
369
370  typedef std::reverse_iterator<const_body_iterator>
371          const_reverse_body_iterator;
372
373  const_reverse_body_iterator body_rbegin() const {
374    return const_reverse_body_iterator(body_end());
375  }
376
377  const_reverse_body_iterator body_rend() const {
378    return const_reverse_body_iterator(body_begin());
379  }
380
381  virtual SourceRange getSourceRange() const {
382    return SourceRange(LBracLoc, RBracLoc);
383  }
384
385  SourceLocation getLBracLoc() const { return LBracLoc; }
386  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
387  SourceLocation getRBracLoc() const { return RBracLoc; }
388  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
389
390  static bool classof(const Stmt *T) {
391    return T->getStmtClass() == CompoundStmtClass;
392  }
393  static bool classof(const CompoundStmt *) { return true; }
394
395  // Iterators
396  virtual child_iterator child_begin();
397  virtual child_iterator child_end();
398
399  virtual void EmitImpl(llvm::Serializer& S) const;
400  static CompoundStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
401};
402
403// SwitchCase is the base class for CaseStmt and DefaultStmt,
404class SwitchCase : public Stmt {
405protected:
406  // A pointer to the following CaseStmt or DefaultStmt class,
407  // used by SwitchStmt.
408  SwitchCase *NextSwitchCase;
409
410  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
411
412public:
413  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
414
415  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
416
417  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
418
419  Stmt *getSubStmt() { return v_getSubStmt(); }
420
421  virtual SourceRange getSourceRange() const { return SourceRange(); }
422
423  static bool classof(const Stmt *T) {
424    return T->getStmtClass() == CaseStmtClass ||
425    T->getStmtClass() == DefaultStmtClass;
426  }
427  static bool classof(const SwitchCase *) { return true; }
428protected:
429  virtual Stmt* v_getSubStmt() = 0;
430};
431
432class CaseStmt : public SwitchCase {
433  enum { SUBSTMT, LHS, RHS, END_EXPR };
434  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
435                             // GNU "case 1 ... 4" extension
436  SourceLocation CaseLoc;
437  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
438public:
439  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc)
440    : SwitchCase(CaseStmtClass) {
441    SubExprs[SUBSTMT] = 0;
442    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
443    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
444    CaseLoc = caseLoc;
445  }
446
447  /// \brief Build an empty switch case statement.
448  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
449
450  SourceLocation getCaseLoc() const { return CaseLoc; }
451  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
452
453  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
454  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
455  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
456
457  const Expr *getLHS() const {
458    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
459  }
460  const Expr *getRHS() const {
461    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
462  }
463  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
464
465  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
466  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
467  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
468
469
470  virtual SourceRange getSourceRange() const {
471    // Handle deeply nested case statements with iteration instead of recursion.
472    const CaseStmt *CS = this;
473    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
474      CS = CS2;
475
476    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
477  }
478  static bool classof(const Stmt *T) {
479    return T->getStmtClass() == CaseStmtClass;
480  }
481  static bool classof(const CaseStmt *) { return true; }
482
483  // Iterators
484  virtual child_iterator child_begin();
485  virtual child_iterator child_end();
486
487  virtual void EmitImpl(llvm::Serializer& S) const;
488  static CaseStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
489};
490
491class DefaultStmt : public SwitchCase {
492  Stmt* SubStmt;
493  SourceLocation DefaultLoc;
494  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
495public:
496  DefaultStmt(SourceLocation DL, Stmt *substmt) :
497    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {}
498
499  /// \brief Build an empty default statement.
500  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
501
502  Stmt *getSubStmt() { return SubStmt; }
503  const Stmt *getSubStmt() const { return SubStmt; }
504  void setSubStmt(Stmt *S) { SubStmt = S; }
505
506  SourceLocation getDefaultLoc() const { return DefaultLoc; }
507  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
508
509  virtual SourceRange getSourceRange() const {
510    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
511  }
512  static bool classof(const Stmt *T) {
513    return T->getStmtClass() == DefaultStmtClass;
514  }
515  static bool classof(const DefaultStmt *) { return true; }
516
517  // Iterators
518  virtual child_iterator child_begin();
519  virtual child_iterator child_end();
520
521  virtual void EmitImpl(llvm::Serializer& S) const;
522  static DefaultStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
523};
524
525class LabelStmt : public Stmt {
526  IdentifierInfo *Label;
527  Stmt *SubStmt;
528  SourceLocation IdentLoc;
529public:
530  LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
531    : Stmt(LabelStmtClass), Label(label),
532      SubStmt(substmt), IdentLoc(IL) {}
533
534  // \brief Build an empty label statement.
535  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
536
537  SourceLocation getIdentLoc() const { return IdentLoc; }
538  IdentifierInfo *getID() const { return Label; }
539  void setID(IdentifierInfo *II) { Label = II; }
540  const char *getName() const;
541  Stmt *getSubStmt() { return SubStmt; }
542  const Stmt *getSubStmt() const { return SubStmt; }
543  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
544  void setSubStmt(Stmt *SS) { SubStmt = SS; }
545
546  virtual SourceRange getSourceRange() const {
547    return SourceRange(IdentLoc, SubStmt->getLocEnd());
548  }
549  static bool classof(const Stmt *T) {
550    return T->getStmtClass() == LabelStmtClass;
551  }
552  static bool classof(const LabelStmt *) { return true; }
553
554  // Iterators
555  virtual child_iterator child_begin();
556  virtual child_iterator child_end();
557
558  virtual void EmitImpl(llvm::Serializer& S) const;
559  static LabelStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
560};
561
562
563/// IfStmt - This represents an if/then/else.
564///
565class IfStmt : public Stmt {
566  enum { COND, THEN, ELSE, END_EXPR };
567  Stmt* SubExprs[END_EXPR];
568  SourceLocation IfLoc;
569public:
570  IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0)
571    : Stmt(IfStmtClass)  {
572    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
573    SubExprs[THEN] = then;
574    SubExprs[ELSE] = elsev;
575    IfLoc = IL;
576  }
577
578  /// \brief Build an empty if/then/else statement
579  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
580
581  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
582  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
583  const Stmt *getThen() const { return SubExprs[THEN]; }
584  void setThen(Stmt *S) { SubExprs[THEN] = S; }
585  const Stmt *getElse() const { return SubExprs[ELSE]; }
586  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
587
588  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
589  Stmt *getThen() { return SubExprs[THEN]; }
590  Stmt *getElse() { return SubExprs[ELSE]; }
591
592  SourceLocation getIfLoc() const { return IfLoc; }
593  void setIfLoc(SourceLocation L) { IfLoc = L; }
594
595  virtual SourceRange getSourceRange() const {
596    if (SubExprs[ELSE])
597      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
598    else
599      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
600  }
601
602  static bool classof(const Stmt *T) {
603    return T->getStmtClass() == IfStmtClass;
604  }
605  static bool classof(const IfStmt *) { return true; }
606
607  // Iterators
608  virtual child_iterator child_begin();
609  virtual child_iterator child_end();
610
611  virtual void EmitImpl(llvm::Serializer& S) const;
612  static IfStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
613};
614
615/// SwitchStmt - This represents a 'switch' stmt.
616///
617class SwitchStmt : public Stmt {
618  enum { COND, BODY, END_EXPR };
619  Stmt* SubExprs[END_EXPR];
620  // This points to a linked list of case and default statements.
621  SwitchCase *FirstCase;
622  SourceLocation SwitchLoc;
623public:
624  SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) {
625      SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
626      SubExprs[BODY] = NULL;
627    }
628
629  /// \brief Build a empty switch statement.
630  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
631
632  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
633  const Stmt *getBody() const { return SubExprs[BODY]; }
634  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
635
636  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
637  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
638  Stmt *getBody() { return SubExprs[BODY]; }
639  void setBody(Stmt *S) { SubExprs[BODY] = S; }
640  SwitchCase *getSwitchCaseList() { return FirstCase; }
641  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
642
643  SourceLocation getSwitchLoc() const { return SwitchLoc; }
644  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
645
646  void setBody(Stmt *S, SourceLocation SL) {
647    SubExprs[BODY] = S;
648    SwitchLoc = SL;
649  }
650  void addSwitchCase(SwitchCase *SC) {
651    assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
652    SC->setNextSwitchCase(FirstCase);
653    FirstCase = SC;
654  }
655  virtual SourceRange getSourceRange() const {
656    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
657  }
658  static bool classof(const Stmt *T) {
659    return T->getStmtClass() == SwitchStmtClass;
660  }
661  static bool classof(const SwitchStmt *) { return true; }
662
663  // Iterators
664  virtual child_iterator child_begin();
665  virtual child_iterator child_end();
666
667  virtual void EmitImpl(llvm::Serializer& S) const;
668  static SwitchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
669};
670
671
672/// WhileStmt - This represents a 'while' stmt.
673///
674class WhileStmt : public Stmt {
675  enum { COND, BODY, END_EXPR };
676  Stmt* SubExprs[END_EXPR];
677  SourceLocation WhileLoc;
678public:
679  WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) {
680    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
681    SubExprs[BODY] = body;
682    WhileLoc = WL;
683  }
684
685  /// \brief Build an empty while statement.
686  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
687
688  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
689  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
690  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
691  Stmt *getBody() { return SubExprs[BODY]; }
692  const Stmt *getBody() const { return SubExprs[BODY]; }
693  void setBody(Stmt *S) { SubExprs[BODY] = S; }
694
695  SourceLocation getWhileLoc() const { return WhileLoc; }
696  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
697
698  virtual SourceRange getSourceRange() const {
699    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
700  }
701  static bool classof(const Stmt *T) {
702    return T->getStmtClass() == WhileStmtClass;
703  }
704  static bool classof(const WhileStmt *) { return true; }
705
706  // Iterators
707  virtual child_iterator child_begin();
708  virtual child_iterator child_end();
709
710  virtual void EmitImpl(llvm::Serializer& S) const;
711  static WhileStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
712};
713
714/// DoStmt - This represents a 'do/while' stmt.
715///
716class DoStmt : public Stmt {
717  enum { COND, BODY, END_EXPR };
718  Stmt* SubExprs[END_EXPR];
719  SourceLocation DoLoc;
720public:
721  DoStmt(Stmt *body, Expr *cond, SourceLocation DL)
722    : Stmt(DoStmtClass), DoLoc(DL) {
723    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
724    SubExprs[BODY] = body;
725    DoLoc = DL;
726  }
727
728  /// \brief Build an empty do-while statement.
729  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
730
731  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
732  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
733  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
734  Stmt *getBody() { return SubExprs[BODY]; }
735  const Stmt *getBody() const { return SubExprs[BODY]; }
736  void setBody(Stmt *S) { SubExprs[BODY] = S; }
737
738  SourceLocation getDoLoc() const { return DoLoc; }
739  void setDoLoc(SourceLocation L) { DoLoc = L; }
740
741  virtual SourceRange getSourceRange() const {
742    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
743  }
744  static bool classof(const Stmt *T) {
745    return T->getStmtClass() == DoStmtClass;
746  }
747  static bool classof(const DoStmt *) { return true; }
748
749  // Iterators
750  virtual child_iterator child_begin();
751  virtual child_iterator child_end();
752
753  virtual void EmitImpl(llvm::Serializer& S) const;
754  static DoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
755};
756
757
758/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
759/// the init/cond/inc parts of the ForStmt will be null if they were not
760/// specified in the source.
761///
762class ForStmt : public Stmt {
763  enum { INIT, COND, INC, BODY, END_EXPR };
764  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
765  SourceLocation ForLoc;
766public:
767  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL)
768    : Stmt(ForStmtClass) {
769    SubExprs[INIT] = Init;
770    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
771    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
772    SubExprs[BODY] = Body;
773    ForLoc = FL;
774  }
775
776  /// \brief Build an empty for statement.
777  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
778
779  Stmt *getInit() { return SubExprs[INIT]; }
780  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
781  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
782  Stmt *getBody() { return SubExprs[BODY]; }
783
784  const Stmt *getInit() const { return SubExprs[INIT]; }
785  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
786  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
787  const Stmt *getBody() const { return SubExprs[BODY]; }
788
789  void setInit(Stmt *S) { SubExprs[INIT] = S; }
790  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
791  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
792  void setBody(Stmt *S) { SubExprs[BODY] = S; }
793
794  SourceLocation getForLoc() const { return ForLoc; }
795  void setForLoc(SourceLocation L) { ForLoc = L; }
796
797  virtual SourceRange getSourceRange() const {
798    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
799  }
800  static bool classof(const Stmt *T) {
801    return T->getStmtClass() == ForStmtClass;
802  }
803  static bool classof(const ForStmt *) { return true; }
804
805  // Iterators
806  virtual child_iterator child_begin();
807  virtual child_iterator child_end();
808
809  virtual void EmitImpl(llvm::Serializer& S) const;
810  static ForStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
811};
812
813/// GotoStmt - This represents a direct goto.
814///
815class GotoStmt : public Stmt {
816  LabelStmt *Label;
817  SourceLocation GotoLoc;
818  SourceLocation LabelLoc;
819public:
820  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
821    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
822
823  /// \brief Build an empty goto statement.
824  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
825
826  LabelStmt *getLabel() const { return Label; }
827  void setLabel(LabelStmt *S) { Label = S; }
828
829  SourceLocation getGotoLoc() const { return GotoLoc; }
830  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
831  SourceLocation getLabelLoc() const { return LabelLoc; }
832  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
833
834  virtual SourceRange getSourceRange() const {
835    return SourceRange(GotoLoc, LabelLoc);
836  }
837  static bool classof(const Stmt *T) {
838    return T->getStmtClass() == GotoStmtClass;
839  }
840  static bool classof(const GotoStmt *) { return true; }
841
842  // Iterators
843  virtual child_iterator child_begin();
844  virtual child_iterator child_end();
845
846  virtual void EmitImpl(llvm::Serializer& S) const;
847  static GotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
848};
849
850/// IndirectGotoStmt - This represents an indirect goto.
851///
852class IndirectGotoStmt : public Stmt {
853  Stmt *Target;
854  // FIXME: Add location information (e.g. SourceLocation objects).
855  //        When doing so, update the PCH serialization routines.
856public:
857  IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass),
858                                   Target((Stmt*)target){}
859
860  /// \brief Build an empty indirect goto statement.
861  explicit IndirectGotoStmt(EmptyShell Empty)
862    : Stmt(IndirectGotoStmtClass, Empty) { }
863
864  Expr *getTarget();
865  const Expr *getTarget() const;
866  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
867
868  virtual SourceRange getSourceRange() const { return SourceRange(); }
869
870  static bool classof(const Stmt *T) {
871    return T->getStmtClass() == IndirectGotoStmtClass;
872  }
873  static bool classof(const IndirectGotoStmt *) { return true; }
874
875  // Iterators
876  virtual child_iterator child_begin();
877  virtual child_iterator child_end();
878
879  virtual void EmitImpl(llvm::Serializer& S) const;
880  static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
881};
882
883
884/// ContinueStmt - This represents a continue.
885///
886class ContinueStmt : public Stmt {
887  SourceLocation ContinueLoc;
888public:
889  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
890
891  /// \brief Build an empty continue statement.
892  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
893
894  SourceLocation getContinueLoc() const { return ContinueLoc; }
895  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
896
897  virtual SourceRange getSourceRange() const {
898    return SourceRange(ContinueLoc);
899  }
900  static bool classof(const Stmt *T) {
901    return T->getStmtClass() == ContinueStmtClass;
902  }
903  static bool classof(const ContinueStmt *) { return true; }
904
905  // Iterators
906  virtual child_iterator child_begin();
907  virtual child_iterator child_end();
908
909  virtual void EmitImpl(llvm::Serializer& S) const;
910  static ContinueStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
911};
912
913/// BreakStmt - This represents a break.
914///
915class BreakStmt : public Stmt {
916  SourceLocation BreakLoc;
917public:
918  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
919
920  /// \brief Build an empty break statement.
921  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
922
923  SourceLocation getBreakLoc() const { return BreakLoc; }
924  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
925
926  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
927
928  static bool classof(const Stmt *T) {
929    return T->getStmtClass() == BreakStmtClass;
930  }
931  static bool classof(const BreakStmt *) { return true; }
932
933  // Iterators
934  virtual child_iterator child_begin();
935  virtual child_iterator child_end();
936
937  virtual void EmitImpl(llvm::Serializer& S) const;
938  static BreakStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
939};
940
941
942/// ReturnStmt - This represents a return, optionally of an expression:
943///   return;
944///   return 4;
945///
946/// Note that GCC allows return with no argument in a function declared to
947/// return a value, and it allows returning a value in functions declared to
948/// return void.  We explicitly model this in the AST, which means you can't
949/// depend on the return type of the function and the presence of an argument.
950///
951class ReturnStmt : public Stmt {
952  Stmt *RetExpr;
953  SourceLocation RetLoc;
954public:
955  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
956    RetExpr((Stmt*) E), RetLoc(RL) {}
957
958  /// \brief Build an empty return expression.
959  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
960
961  const Expr *getRetValue() const;
962  Expr *getRetValue();
963  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
964
965  SourceLocation getReturnLoc() const { return RetLoc; }
966  void setReturnLoc(SourceLocation L) { RetLoc = L; }
967
968  virtual SourceRange getSourceRange() const;
969
970  static bool classof(const Stmt *T) {
971    return T->getStmtClass() == ReturnStmtClass;
972  }
973  static bool classof(const ReturnStmt *) { return true; }
974
975  // Iterators
976  virtual child_iterator child_begin();
977  virtual child_iterator child_end();
978
979  virtual void EmitImpl(llvm::Serializer& S) const;
980  static ReturnStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
981};
982
983/// AsmStmt - This represents a GNU inline-assembly statement extension.
984///
985class AsmStmt : public Stmt {
986  SourceLocation AsmLoc, RParenLoc;
987  StringLiteral *AsmStr;
988
989  bool IsSimple;
990  bool IsVolatile;
991
992  unsigned NumOutputs;
993  unsigned NumInputs;
994
995  llvm::SmallVector<std::string, 4> Names;
996  llvm::SmallVector<StringLiteral*, 4> Constraints;
997  llvm::SmallVector<Stmt*, 4> Exprs;
998
999  llvm::SmallVector<StringLiteral*, 4> Clobbers;
1000public:
1001  AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
1002          unsigned numoutputs, unsigned numinputs,
1003          std::string *names, StringLiteral **constraints,
1004          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1005          StringLiteral **clobbers, SourceLocation rparenloc);
1006
1007  /// \brief Build an empty inline-assembly statement.
1008  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty) { }
1009
1010  SourceLocation getAsmLoc() const { return AsmLoc; }
1011  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1012  SourceLocation getRParenLoc() const { return RParenLoc; }
1013  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1014
1015  bool isVolatile() const { return IsVolatile; }
1016  void setVolatile(bool V) { IsVolatile = V; }
1017  bool isSimple() const { return IsSimple; }
1018  void setSimple(bool V) { IsSimple = false; }
1019
1020  //===--- Asm String Analysis ---===//
1021
1022  const StringLiteral *getAsmString() const { return AsmStr; }
1023  StringLiteral *getAsmString() { return AsmStr; }
1024  void setAsmString(StringLiteral *E) { AsmStr = E; }
1025
1026  /// AsmStringPiece - this is part of a decomposed asm string specification
1027  /// (for use with the AnalyzeAsmString function below).  An asm string is
1028  /// considered to be a concatenation of these parts.
1029  class AsmStringPiece {
1030  public:
1031    enum Kind {
1032      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1033      Operand  // Operand reference, with optional modifier %c4.
1034    };
1035  private:
1036    Kind MyKind;
1037    std::string Str;
1038    unsigned OperandNo;
1039  public:
1040    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1041    AsmStringPiece(unsigned OpNo, char Modifier)
1042      : MyKind(Operand), Str(), OperandNo(OpNo) {
1043      Str += Modifier;
1044    }
1045
1046    bool isString() const { return MyKind == String; }
1047    bool isOperand() const { return MyKind == Operand; }
1048
1049    const std::string &getString() const {
1050      assert(isString());
1051      return Str;
1052    }
1053
1054    unsigned getOperandNo() const {
1055      assert(isOperand());
1056      return OperandNo;
1057    }
1058
1059    /// getModifier - Get the modifier for this operand, if present.  This
1060    /// returns '\0' if there was no modifier.
1061    char getModifier() const {
1062      assert(isOperand());
1063      return Str[0];
1064    }
1065  };
1066
1067  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1068  /// it into pieces.  If the asm string is erroneous, emit errors and return
1069  /// true, otherwise return false.  This handles canonicalization and
1070  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1071  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1072  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1073                            ASTContext &C, unsigned &DiagOffs) const;
1074
1075
1076  //===--- Output operands ---===//
1077
1078  unsigned getNumOutputs() const { return NumOutputs; }
1079
1080  const std::string &getOutputName(unsigned i) const {
1081    return Names[i];
1082  }
1083
1084  /// getOutputConstraint - Return the constraint string for the specified
1085  /// output operand.  All output constraints are known to be non-empty (either
1086  /// '=' or '+').
1087  std::string getOutputConstraint(unsigned i) const;
1088
1089  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1090    return Constraints[i];
1091  }
1092  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1093    return Constraints[i];
1094  }
1095
1096
1097  Expr *getOutputExpr(unsigned i);
1098
1099  const Expr *getOutputExpr(unsigned i) const {
1100    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1101  }
1102
1103  /// isOutputPlusConstraint - Return true if the specified output constraint
1104  /// is a "+" constraint (which is both an input and an output) or false if it
1105  /// is an "=" constraint (just an output).
1106  bool isOutputPlusConstraint(unsigned i) const {
1107    return getOutputConstraint(i)[0] == '+';
1108  }
1109
1110  /// getNumPlusOperands - Return the number of output operands that have a "+"
1111  /// constraint.
1112  unsigned getNumPlusOperands() const;
1113
1114  //===--- Input operands ---===//
1115
1116  unsigned getNumInputs() const { return NumInputs; }
1117
1118  const std::string &getInputName(unsigned i) const {
1119    return Names[i + NumOutputs];
1120  }
1121
1122  /// getInputConstraint - Return the specified input constraint.  Unlike output
1123  /// constraints, these can be empty.
1124  std::string getInputConstraint(unsigned i) const;
1125
1126  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1127    return Constraints[i + NumOutputs];
1128  }
1129  StringLiteral *getInputConstraintLiteral(unsigned i) {
1130    return Constraints[i + NumOutputs];
1131  }
1132
1133
1134  Expr *getInputExpr(unsigned i);
1135
1136  const Expr *getInputExpr(unsigned i) const {
1137    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1138  }
1139
1140  void setOutputsAndInputs(unsigned NumOutputs,
1141                           unsigned NumInputs,
1142                           const std::string *Names,
1143                           StringLiteral **Constraints,
1144                           Stmt **Exprs);
1145
1146  //===--- Other ---===//
1147
1148  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1149  /// translate this into a numeric value needed to reference the same operand.
1150  /// This returns -1 if the operand name is invalid.
1151  int getNamedOperand(const std::string &SymbolicName) const;
1152
1153
1154
1155  unsigned getNumClobbers() const { return Clobbers.size(); }
1156  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1157  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1158  void setClobbers(StringLiteral **Clobbers, unsigned NumClobbers);
1159
1160  virtual SourceRange getSourceRange() const {
1161    return SourceRange(AsmLoc, RParenLoc);
1162  }
1163
1164  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1165  static bool classof(const AsmStmt *) { return true; }
1166
1167  // Input expr iterators.
1168
1169  typedef ExprIterator inputs_iterator;
1170  typedef ConstExprIterator const_inputs_iterator;
1171
1172  inputs_iterator begin_inputs() {
1173    return &Exprs[0] + NumOutputs;
1174  }
1175
1176  inputs_iterator end_inputs() {
1177    return  &Exprs[0] + NumOutputs + NumInputs;
1178  }
1179
1180  const_inputs_iterator begin_inputs() const {
1181    return &Exprs[0] + NumOutputs;
1182  }
1183
1184  const_inputs_iterator end_inputs() const {
1185    return  &Exprs[0] + NumOutputs + NumInputs;}
1186
1187  // Output expr iterators.
1188
1189  typedef ExprIterator outputs_iterator;
1190  typedef ConstExprIterator const_outputs_iterator;
1191
1192  outputs_iterator begin_outputs() { return &Exprs[0]; }
1193  outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; }
1194
1195  const_outputs_iterator begin_outputs() const { return &Exprs[0]; }
1196  const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; }
1197
1198  // Input name iterator.
1199
1200  const std::string *begin_output_names() const {
1201    return &Names[0];
1202  }
1203
1204  const std::string *end_output_names() const {
1205    return &Names[0] + NumOutputs;
1206  }
1207
1208  // Child iterators
1209
1210  virtual child_iterator child_begin();
1211  virtual child_iterator child_end();
1212
1213  virtual void EmitImpl(llvm::Serializer& S) const;
1214  static AsmStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1215};
1216
1217/// ObjCForCollectionStmt - This represents Objective-c's collection statement;
1218/// represented as 'for (element 'in' collection-expression)' stmt.
1219///
1220class ObjCForCollectionStmt : public Stmt {
1221  enum { ELEM, COLLECTION, BODY, END_EXPR };
1222  Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
1223  SourceLocation ForLoc;
1224  SourceLocation RParenLoc;
1225public:
1226  ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
1227                        SourceLocation FCL, SourceLocation RPL);
1228
1229  Stmt *getElement() { return SubExprs[ELEM]; }
1230  Expr *getCollection() {
1231    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
1232  }
1233  Stmt *getBody() { return SubExprs[BODY]; }
1234
1235  const Stmt *getElement() const { return SubExprs[ELEM]; }
1236  const Expr *getCollection() const {
1237    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
1238  }
1239  const Stmt *getBody() const { return SubExprs[BODY]; }
1240
1241  SourceLocation getRParenLoc() const { return RParenLoc; }
1242
1243  virtual SourceRange getSourceRange() const {
1244    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1245  }
1246  static bool classof(const Stmt *T) {
1247    return T->getStmtClass() == ObjCForCollectionStmtClass;
1248  }
1249  static bool classof(const ObjCForCollectionStmt *) { return true; }
1250
1251  // Iterators
1252  virtual child_iterator child_begin();
1253  virtual child_iterator child_end();
1254
1255  virtual void EmitImpl(llvm::Serializer& S) const;
1256  static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1257};
1258
1259/// ObjCAtCatchStmt - This represents objective-c's @catch statement.
1260class ObjCAtCatchStmt : public Stmt {
1261private:
1262  enum { BODY, NEXT_CATCH, END_EXPR };
1263  ParmVarDecl *ExceptionDecl;
1264  Stmt *SubExprs[END_EXPR];
1265  SourceLocation AtCatchLoc, RParenLoc;
1266
1267  // Used by deserialization.
1268  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc)
1269  : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {}
1270
1271public:
1272  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
1273                  ParmVarDecl *catchVarDecl,
1274                  Stmt *atCatchStmt, Stmt *atCatchList);
1275
1276  const Stmt *getCatchBody() const { return SubExprs[BODY]; }
1277  Stmt *getCatchBody() { return SubExprs[BODY]; }
1278
1279  const ObjCAtCatchStmt *getNextCatchStmt() const {
1280    return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
1281  }
1282  ObjCAtCatchStmt *getNextCatchStmt() {
1283    return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
1284  }
1285
1286  const ParmVarDecl *getCatchParamDecl() const {
1287    return ExceptionDecl;
1288  }
1289  ParmVarDecl *getCatchParamDecl() {
1290    return ExceptionDecl;
1291  }
1292
1293  SourceLocation getRParenLoc() const { return RParenLoc; }
1294
1295  virtual SourceRange getSourceRange() const {
1296    return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd());
1297  }
1298
1299  bool hasEllipsis() const { return getCatchParamDecl() == 0; }
1300
1301  static bool classof(const Stmt *T) {
1302    return T->getStmtClass() == ObjCAtCatchStmtClass;
1303  }
1304  static bool classof(const ObjCAtCatchStmt *) { return true; }
1305
1306  virtual child_iterator child_begin();
1307  virtual child_iterator child_end();
1308
1309  virtual void EmitImpl(llvm::Serializer& S) const;
1310  static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1311};
1312
1313/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement
1314class ObjCAtFinallyStmt : public Stmt {
1315  Stmt *AtFinallyStmt;
1316  SourceLocation AtFinallyLoc;
1317public:
1318  ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
1319  : Stmt(ObjCAtFinallyStmtClass),
1320    AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
1321
1322  const Stmt *getFinallyBody () const { return AtFinallyStmt; }
1323  Stmt *getFinallyBody () { return AtFinallyStmt; }
1324
1325  virtual SourceRange getSourceRange() const {
1326    return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
1327  }
1328
1329  static bool classof(const Stmt *T) {
1330    return T->getStmtClass() == ObjCAtFinallyStmtClass;
1331  }
1332  static bool classof(const ObjCAtFinallyStmt *) { return true; }
1333
1334  virtual child_iterator child_begin();
1335  virtual child_iterator child_end();
1336
1337  virtual void EmitImpl(llvm::Serializer& S) const;
1338  static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1339};
1340
1341/// ObjCAtTryStmt - This represent objective-c's over-all
1342/// @try ... @catch ... @finally statement.
1343class ObjCAtTryStmt : public Stmt {
1344private:
1345  enum { TRY, CATCH, FINALLY, END_EXPR };
1346  Stmt* SubStmts[END_EXPR];
1347
1348  SourceLocation AtTryLoc;
1349public:
1350  ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
1351                Stmt *atCatchStmt,
1352                Stmt *atFinallyStmt)
1353  : Stmt(ObjCAtTryStmtClass) {
1354      SubStmts[TRY] = atTryStmt;
1355      SubStmts[CATCH] = atCatchStmt;
1356      SubStmts[FINALLY] = atFinallyStmt;
1357      AtTryLoc = atTryLoc;
1358    }
1359
1360  const Stmt *getTryBody() const { return SubStmts[TRY]; }
1361  Stmt *getTryBody() { return SubStmts[TRY]; }
1362  const ObjCAtCatchStmt *getCatchStmts() const {
1363    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1364  }
1365  ObjCAtCatchStmt *getCatchStmts() {
1366    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1367  }
1368  const ObjCAtFinallyStmt *getFinallyStmt() const {
1369    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1370  }
1371  ObjCAtFinallyStmt *getFinallyStmt() {
1372    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1373  }
1374  virtual SourceRange getSourceRange() const {
1375    return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd());
1376  }
1377
1378  static bool classof(const Stmt *T) {
1379    return T->getStmtClass() == ObjCAtTryStmtClass;
1380  }
1381  static bool classof(const ObjCAtTryStmt *) { return true; }
1382
1383  virtual child_iterator child_begin();
1384  virtual child_iterator child_end();
1385
1386  virtual void EmitImpl(llvm::Serializer& S) const;
1387  static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1388};
1389
1390/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement.
1391/// Example: @synchronized (sem) {
1392///             do-something;
1393///          }
1394///
1395class ObjCAtSynchronizedStmt : public Stmt {
1396private:
1397  enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
1398  Stmt* SubStmts[END_EXPR];
1399  SourceLocation AtSynchronizedLoc;
1400
1401public:
1402  ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
1403                         Stmt *synchBody)
1404  : Stmt(ObjCAtSynchronizedStmtClass) {
1405      SubStmts[SYNC_EXPR] = synchExpr;
1406      SubStmts[SYNC_BODY] = synchBody;
1407      AtSynchronizedLoc = atSynchronizedLoc;
1408    }
1409
1410  const CompoundStmt *getSynchBody() const {
1411    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1412  }
1413  CompoundStmt *getSynchBody() {
1414    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1415  }
1416
1417  const Expr *getSynchExpr() const {
1418    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1419  }
1420  Expr *getSynchExpr() {
1421    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1422  }
1423
1424  virtual SourceRange getSourceRange() const {
1425    return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd());
1426  }
1427
1428  static bool classof(const Stmt *T) {
1429    return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
1430  }
1431  static bool classof(const ObjCAtSynchronizedStmt *) { return true; }
1432
1433  virtual child_iterator child_begin();
1434  virtual child_iterator child_end();
1435
1436  virtual void EmitImpl(llvm::Serializer& S) const;
1437  static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D,
1438                                            ASTContext& C);
1439};
1440
1441/// ObjCAtThrowStmt - This represents objective-c's @throw statement.
1442class ObjCAtThrowStmt : public Stmt {
1443  Stmt *Throw;
1444  SourceLocation AtThrowLoc;
1445public:
1446  ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
1447  : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
1448    AtThrowLoc = atThrowLoc;
1449  }
1450
1451  const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
1452  Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
1453
1454  virtual SourceRange getSourceRange() const {
1455    if (Throw)
1456      return SourceRange(AtThrowLoc, Throw->getLocEnd());
1457    else
1458      return SourceRange(AtThrowLoc);
1459  }
1460
1461  static bool classof(const Stmt *T) {
1462    return T->getStmtClass() == ObjCAtThrowStmtClass;
1463  }
1464  static bool classof(const ObjCAtThrowStmt *) { return true; }
1465
1466  virtual child_iterator child_begin();
1467  virtual child_iterator child_end();
1468
1469  virtual void EmitImpl(llvm::Serializer& S) const;
1470  static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1471};
1472
1473/// CXXCatchStmt - This represents a C++ catch block.
1474class CXXCatchStmt : public Stmt {
1475  SourceLocation CatchLoc;
1476  /// The exception-declaration of the type.
1477  Decl *ExceptionDecl;
1478  /// The handler block.
1479  Stmt *HandlerBlock;
1480
1481public:
1482  CXXCatchStmt(SourceLocation catchLoc, Decl *exDecl, Stmt *handlerBlock)
1483  : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
1484    HandlerBlock(handlerBlock) {}
1485
1486  virtual void Destroy(ASTContext& Ctx);
1487
1488  virtual SourceRange getSourceRange() const {
1489    return SourceRange(CatchLoc, HandlerBlock->getLocEnd());
1490  }
1491
1492  Decl *getExceptionDecl() { return ExceptionDecl; }
1493  QualType getCaughtType();
1494  Stmt *getHandlerBlock() { return HandlerBlock; }
1495
1496  static bool classof(const Stmt *T) {
1497    return T->getStmtClass() == CXXCatchStmtClass;
1498  }
1499  static bool classof(const CXXCatchStmt *) { return true; }
1500
1501  virtual child_iterator child_begin();
1502  virtual child_iterator child_end();
1503
1504  virtual void EmitImpl(llvm::Serializer& S) const;
1505  static CXXCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1506};
1507
1508/// CXXTryStmt - A C++ try block, including all handlers.
1509class CXXTryStmt : public Stmt {
1510  SourceLocation TryLoc;
1511  // First place is the guarded CompoundStatement. Subsequent are the handlers.
1512  // More than three handlers should be rare.
1513  llvm::SmallVector<Stmt*, 4> Stmts;
1514
1515public:
1516  CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
1517             Stmt **handlers, unsigned numHandlers);
1518
1519  virtual SourceRange getSourceRange() const {
1520    return SourceRange(TryLoc, Stmts.back()->getLocEnd());
1521  }
1522
1523  CompoundStmt *getTryBlock() { return llvm::cast<CompoundStmt>(Stmts[0]); }
1524  const CompoundStmt *getTryBlock() const {
1525    return llvm::cast<CompoundStmt>(Stmts[0]);
1526  }
1527
1528  unsigned getNumHandlers() const { return Stmts.size() - 1; }
1529  CXXCatchStmt *getHandler(unsigned i) {
1530    return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
1531  }
1532  const CXXCatchStmt *getHandler(unsigned i) const {
1533    return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
1534  }
1535
1536  static bool classof(const Stmt *T) {
1537    return T->getStmtClass() == CXXTryStmtClass;
1538  }
1539  static bool classof(const CXXTryStmt *) { return true; }
1540
1541  virtual child_iterator child_begin();
1542  virtual child_iterator child_end();
1543
1544  virtual void EmitImpl(llvm::Serializer& S) const;
1545  static CXXTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1546};
1547
1548}  // end namespace clang
1549
1550#endif
1551