Stmt.h revision d921cf976b4769af8d06d6763a2547dadf7940ab
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  virtual void Destroy(ASTContext& Ctx);
248
249  /// isSingleDecl - This method returns true if this DeclStmt refers
250  /// to a single Decl.
251  bool isSingleDecl() const {
252    return DG.isSingleDecl();
253  }
254
255  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
256  Decl *getSingleDecl() { return DG.getSingleDecl(); }
257
258  const DeclGroupRef getDeclGroup() const { return DG; }
259  DeclGroupRef getDeclGroup() { return DG; }
260
261  SourceLocation getStartLoc() const { return StartLoc; }
262  SourceLocation getEndLoc() const { return EndLoc; }
263
264  SourceRange getSourceRange() const {
265    return SourceRange(StartLoc, EndLoc);
266  }
267
268  static bool classof(const Stmt *T) {
269    return T->getStmtClass() == DeclStmtClass;
270  }
271  static bool classof(const DeclStmt *) { return true; }
272
273  // Iterators over subexpressions.
274  virtual child_iterator child_begin();
275  virtual child_iterator child_end();
276
277  typedef DeclGroupRef::iterator decl_iterator;
278  typedef DeclGroupRef::const_iterator const_decl_iterator;
279
280  decl_iterator decl_begin() { return DG.begin(); }
281  decl_iterator decl_end() { return DG.end(); }
282  const_decl_iterator decl_begin() const { return DG.begin(); }
283  const_decl_iterator decl_end() const { return DG.end(); }
284
285  // Serialization.
286  virtual void EmitImpl(llvm::Serializer& S) const;
287  static DeclStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
288};
289
290/// NullStmt - This is the null statement ";": C99 6.8.3p3.
291///
292class NullStmt : public Stmt {
293  SourceLocation SemiLoc;
294public:
295  NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
296
297  /// \brief Build an empty null statement.
298  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
299
300  SourceLocation getSemiLoc() const { return SemiLoc; }
301  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
302
303  virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
304
305  static bool classof(const Stmt *T) {
306    return T->getStmtClass() == NullStmtClass;
307  }
308  static bool classof(const NullStmt *) { return true; }
309
310  // Iterators
311  virtual child_iterator child_begin();
312  virtual child_iterator child_end();
313
314  virtual void EmitImpl(llvm::Serializer& S) const;
315  static NullStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
316};
317
318/// CompoundStmt - This represents a group of statements like { stmt stmt }.
319///
320class CompoundStmt : public Stmt {
321  Stmt** Body;
322  unsigned NumStmts;
323  SourceLocation LBracLoc, RBracLoc;
324public:
325  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts,
326                             SourceLocation LB, SourceLocation RB)
327  : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) {
328    if (NumStmts == 0) {
329      Body = 0;
330      return;
331    }
332
333    Body = new (C) Stmt*[NumStmts];
334    memcpy(Body, StmtStart, numStmts * sizeof(*Body));
335  }
336
337  // \brief Build an empty compound statement.
338  explicit CompoundStmt(EmptyShell Empty)
339    : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { }
340
341  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
342
343  bool body_empty() const { return NumStmts == 0; }
344  unsigned size() const { return NumStmts; }
345
346  typedef Stmt** body_iterator;
347  body_iterator body_begin() { return Body; }
348  body_iterator body_end() { return Body + NumStmts; }
349  Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; }
350
351  typedef Stmt* const * const_body_iterator;
352  const_body_iterator body_begin() const { return Body; }
353  const_body_iterator body_end() const { return Body + NumStmts; }
354  const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; }
355
356  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
357  reverse_body_iterator body_rbegin() {
358    return reverse_body_iterator(body_end());
359  }
360  reverse_body_iterator body_rend() {
361    return reverse_body_iterator(body_begin());
362  }
363
364  typedef std::reverse_iterator<const_body_iterator>
365          const_reverse_body_iterator;
366
367  const_reverse_body_iterator body_rbegin() const {
368    return const_reverse_body_iterator(body_end());
369  }
370
371  const_reverse_body_iterator body_rend() const {
372    return const_reverse_body_iterator(body_begin());
373  }
374
375  virtual SourceRange getSourceRange() const {
376    return SourceRange(LBracLoc, RBracLoc);
377  }
378
379  SourceLocation getLBracLoc() const { return LBracLoc; }
380  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
381  SourceLocation getRBracLoc() const { return RBracLoc; }
382  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
383
384  static bool classof(const Stmt *T) {
385    return T->getStmtClass() == CompoundStmtClass;
386  }
387  static bool classof(const CompoundStmt *) { return true; }
388
389  // Iterators
390  virtual child_iterator child_begin();
391  virtual child_iterator child_end();
392
393  virtual void EmitImpl(llvm::Serializer& S) const;
394  static CompoundStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
395};
396
397// SwitchCase is the base class for CaseStmt and DefaultStmt,
398class SwitchCase : public Stmt {
399protected:
400  // A pointer to the following CaseStmt or DefaultStmt class,
401  // used by SwitchStmt.
402  SwitchCase *NextSwitchCase;
403
404  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
405
406public:
407  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
408
409  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
410
411  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
412
413  Stmt *getSubStmt() { return v_getSubStmt(); }
414
415  virtual SourceRange getSourceRange() const { return SourceRange(); }
416
417  static bool classof(const Stmt *T) {
418    return T->getStmtClass() == CaseStmtClass ||
419    T->getStmtClass() == DefaultStmtClass;
420  }
421  static bool classof(const SwitchCase *) { return true; }
422protected:
423  virtual Stmt* v_getSubStmt() = 0;
424};
425
426class CaseStmt : public SwitchCase {
427  enum { SUBSTMT, LHS, RHS, END_EXPR };
428  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
429                             // GNU "case 1 ... 4" extension
430  SourceLocation CaseLoc;
431  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
432public:
433  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc)
434    : SwitchCase(CaseStmtClass) {
435    SubExprs[SUBSTMT] = 0;
436    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
437    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
438    CaseLoc = caseLoc;
439  }
440
441  /// \brief Build an empty switch case statement.
442  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
443
444  SourceLocation getCaseLoc() const { return CaseLoc; }
445  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
446
447  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
448  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
449  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
450
451  const Expr *getLHS() const {
452    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
453  }
454  const Expr *getRHS() const {
455    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
456  }
457  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
458
459  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
460  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
461  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
462
463
464  virtual SourceRange getSourceRange() const {
465    // Handle deeply nested case statements with iteration instead of recursion.
466    const CaseStmt *CS = this;
467    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
468      CS = CS2;
469
470    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
471  }
472  static bool classof(const Stmt *T) {
473    return T->getStmtClass() == CaseStmtClass;
474  }
475  static bool classof(const CaseStmt *) { return true; }
476
477  // Iterators
478  virtual child_iterator child_begin();
479  virtual child_iterator child_end();
480
481  virtual void EmitImpl(llvm::Serializer& S) const;
482  static CaseStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
483};
484
485class DefaultStmt : public SwitchCase {
486  Stmt* SubStmt;
487  SourceLocation DefaultLoc;
488  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
489public:
490  DefaultStmt(SourceLocation DL, Stmt *substmt) :
491    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {}
492
493  /// \brief Build an empty default statement.
494  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
495
496  Stmt *getSubStmt() { return SubStmt; }
497  const Stmt *getSubStmt() const { return SubStmt; }
498  void setSubStmt(Stmt *S) { SubStmt = S; }
499
500  SourceLocation getDefaultLoc() const { return DefaultLoc; }
501  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
502
503  virtual SourceRange getSourceRange() const {
504    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
505  }
506  static bool classof(const Stmt *T) {
507    return T->getStmtClass() == DefaultStmtClass;
508  }
509  static bool classof(const DefaultStmt *) { return true; }
510
511  // Iterators
512  virtual child_iterator child_begin();
513  virtual child_iterator child_end();
514
515  virtual void EmitImpl(llvm::Serializer& S) const;
516  static DefaultStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
517};
518
519class LabelStmt : public Stmt {
520  IdentifierInfo *Label;
521  Stmt *SubStmt;
522  SourceLocation IdentLoc;
523public:
524  LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
525    : Stmt(LabelStmtClass), Label(label),
526      SubStmt(substmt), IdentLoc(IL) {}
527
528  SourceLocation getIdentLoc() const { return IdentLoc; }
529  IdentifierInfo *getID() const { return Label; }
530  const char *getName() const;
531  Stmt *getSubStmt() { return SubStmt; }
532  const Stmt *getSubStmt() const { return SubStmt; }
533
534  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
535  void setSubStmt(Stmt *SS) { SubStmt = SS; }
536
537  virtual SourceRange getSourceRange() const {
538    return SourceRange(IdentLoc, SubStmt->getLocEnd());
539  }
540  static bool classof(const Stmt *T) {
541    return T->getStmtClass() == LabelStmtClass;
542  }
543  static bool classof(const LabelStmt *) { return true; }
544
545  // Iterators
546  virtual child_iterator child_begin();
547  virtual child_iterator child_end();
548
549  virtual void EmitImpl(llvm::Serializer& S) const;
550  static LabelStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
551};
552
553
554/// IfStmt - This represents an if/then/else.
555///
556class IfStmt : public Stmt {
557  enum { COND, THEN, ELSE, END_EXPR };
558  Stmt* SubExprs[END_EXPR];
559  SourceLocation IfLoc;
560public:
561  IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0)
562    : Stmt(IfStmtClass)  {
563    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
564    SubExprs[THEN] = then;
565    SubExprs[ELSE] = elsev;
566    IfLoc = IL;
567  }
568
569  /// \brief Build an empty if/then/else statement
570  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
571
572  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
573  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
574  const Stmt *getThen() const { return SubExprs[THEN]; }
575  void setThen(Stmt *S) { SubExprs[THEN] = S; }
576  const Stmt *getElse() const { return SubExprs[ELSE]; }
577  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
578
579  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
580  Stmt *getThen() { return SubExprs[THEN]; }
581  Stmt *getElse() { return SubExprs[ELSE]; }
582
583  SourceLocation getIfLoc() const { return IfLoc; }
584  void setIfLoc(SourceLocation L) { IfLoc = L; }
585
586  virtual SourceRange getSourceRange() const {
587    if (SubExprs[ELSE])
588      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
589    else
590      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
591  }
592
593  static bool classof(const Stmt *T) {
594    return T->getStmtClass() == IfStmtClass;
595  }
596  static bool classof(const IfStmt *) { return true; }
597
598  // Iterators
599  virtual child_iterator child_begin();
600  virtual child_iterator child_end();
601
602  virtual void EmitImpl(llvm::Serializer& S) const;
603  static IfStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
604};
605
606/// SwitchStmt - This represents a 'switch' stmt.
607///
608class SwitchStmt : public Stmt {
609  enum { COND, BODY, END_EXPR };
610  Stmt* SubExprs[END_EXPR];
611  // This points to a linked list of case and default statements.
612  SwitchCase *FirstCase;
613  SourceLocation SwitchLoc;
614public:
615  SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) {
616      SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
617      SubExprs[BODY] = NULL;
618    }
619
620  /// \brief Build a empty switch statement.
621  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
622
623  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
624  const Stmt *getBody() const { return SubExprs[BODY]; }
625  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
626
627  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
628  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
629  Stmt *getBody() { return SubExprs[BODY]; }
630  void setBody(Stmt *S) { SubExprs[BODY] = S; }
631  SwitchCase *getSwitchCaseList() { return FirstCase; }
632  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
633
634  SourceLocation getSwitchLoc() const { return SwitchLoc; }
635  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
636
637  void setBody(Stmt *S, SourceLocation SL) {
638    SubExprs[BODY] = S;
639    SwitchLoc = SL;
640  }
641  void addSwitchCase(SwitchCase *SC) {
642    assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
643    SC->setNextSwitchCase(FirstCase);
644    FirstCase = SC;
645  }
646  virtual SourceRange getSourceRange() const {
647    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
648  }
649  static bool classof(const Stmt *T) {
650    return T->getStmtClass() == SwitchStmtClass;
651  }
652  static bool classof(const SwitchStmt *) { return true; }
653
654  // Iterators
655  virtual child_iterator child_begin();
656  virtual child_iterator child_end();
657
658  virtual void EmitImpl(llvm::Serializer& S) const;
659  static SwitchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
660};
661
662
663/// WhileStmt - This represents a 'while' stmt.
664///
665class WhileStmt : public Stmt {
666  enum { COND, BODY, END_EXPR };
667  Stmt* SubExprs[END_EXPR];
668  SourceLocation WhileLoc;
669public:
670  WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) {
671    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
672    SubExprs[BODY] = body;
673    WhileLoc = WL;
674  }
675
676  /// \brief Build an empty while statement.
677  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
678
679  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
680  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
681  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
682  Stmt *getBody() { return SubExprs[BODY]; }
683  const Stmt *getBody() const { return SubExprs[BODY]; }
684  void setBody(Stmt *S) { SubExprs[BODY] = S; }
685
686  SourceLocation getWhileLoc() const { return WhileLoc; }
687  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
688
689  virtual SourceRange getSourceRange() const {
690    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
691  }
692  static bool classof(const Stmt *T) {
693    return T->getStmtClass() == WhileStmtClass;
694  }
695  static bool classof(const WhileStmt *) { return true; }
696
697  // Iterators
698  virtual child_iterator child_begin();
699  virtual child_iterator child_end();
700
701  virtual void EmitImpl(llvm::Serializer& S) const;
702  static WhileStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
703};
704
705/// DoStmt - This represents a 'do/while' stmt.
706///
707class DoStmt : public Stmt {
708  enum { COND, BODY, END_EXPR };
709  Stmt* SubExprs[END_EXPR];
710  SourceLocation DoLoc;
711public:
712  DoStmt(Stmt *body, Expr *cond, SourceLocation DL)
713    : Stmt(DoStmtClass), DoLoc(DL) {
714    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
715    SubExprs[BODY] = body;
716    DoLoc = DL;
717  }
718
719  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
720  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
721  Stmt *getBody() { return SubExprs[BODY]; }
722  const Stmt *getBody() const { return SubExprs[BODY]; }
723
724  virtual SourceRange getSourceRange() const {
725    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
726  }
727  static bool classof(const Stmt *T) {
728    return T->getStmtClass() == DoStmtClass;
729  }
730  static bool classof(const DoStmt *) { return true; }
731
732  // Iterators
733  virtual child_iterator child_begin();
734  virtual child_iterator child_end();
735
736  virtual void EmitImpl(llvm::Serializer& S) const;
737  static DoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
738};
739
740
741/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
742/// the init/cond/inc parts of the ForStmt will be null if they were not
743/// specified in the source.
744///
745class ForStmt : public Stmt {
746  enum { INIT, COND, INC, BODY, END_EXPR };
747  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
748  SourceLocation ForLoc;
749public:
750  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL)
751    : Stmt(ForStmtClass) {
752    SubExprs[INIT] = Init;
753    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
754    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
755    SubExprs[BODY] = Body;
756    ForLoc = FL;
757  }
758
759  Stmt *getInit() { return SubExprs[INIT]; }
760  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
761  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
762  Stmt *getBody() { return SubExprs[BODY]; }
763
764  const Stmt *getInit() const { return SubExprs[INIT]; }
765  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
766  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
767  const Stmt *getBody() const { return SubExprs[BODY]; }
768
769  virtual SourceRange getSourceRange() const {
770    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
771  }
772  static bool classof(const Stmt *T) {
773    return T->getStmtClass() == ForStmtClass;
774  }
775  static bool classof(const ForStmt *) { return true; }
776
777  // Iterators
778  virtual child_iterator child_begin();
779  virtual child_iterator child_end();
780
781  virtual void EmitImpl(llvm::Serializer& S) const;
782  static ForStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
783};
784
785/// GotoStmt - This represents a direct goto.
786///
787class GotoStmt : public Stmt {
788  LabelStmt *Label;
789  SourceLocation GotoLoc;
790  SourceLocation LabelLoc;
791public:
792  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
793    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
794
795  LabelStmt *getLabel() const { return Label; }
796
797  virtual SourceRange getSourceRange() const {
798    return SourceRange(GotoLoc, LabelLoc);
799  }
800  static bool classof(const Stmt *T) {
801    return T->getStmtClass() == GotoStmtClass;
802  }
803  static bool classof(const GotoStmt *) { 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 GotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
811};
812
813/// IndirectGotoStmt - This represents an indirect goto.
814///
815class IndirectGotoStmt : public Stmt {
816  Stmt *Target;
817  // FIXME: Add location information (e.g. SourceLocation objects).
818  //        When doing so, update the serialization routines.
819public:
820  IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass),
821                                   Target((Stmt*)target){}
822
823  Expr *getTarget();
824  const Expr *getTarget() const;
825
826  virtual SourceRange getSourceRange() const { return SourceRange(); }
827
828  static bool classof(const Stmt *T) {
829    return T->getStmtClass() == IndirectGotoStmtClass;
830  }
831  static bool classof(const IndirectGotoStmt *) { return true; }
832
833  // Iterators
834  virtual child_iterator child_begin();
835  virtual child_iterator child_end();
836
837  virtual void EmitImpl(llvm::Serializer& S) const;
838  static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
839};
840
841
842/// ContinueStmt - This represents a continue.
843///
844class ContinueStmt : public Stmt {
845  SourceLocation ContinueLoc;
846public:
847  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
848
849  /// \brief Build an empty continue statement.
850  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
851
852  SourceLocation getContinueLoc() const { return ContinueLoc; }
853  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
854
855  virtual SourceRange getSourceRange() const {
856    return SourceRange(ContinueLoc);
857  }
858  static bool classof(const Stmt *T) {
859    return T->getStmtClass() == ContinueStmtClass;
860  }
861  static bool classof(const ContinueStmt *) { return true; }
862
863  // Iterators
864  virtual child_iterator child_begin();
865  virtual child_iterator child_end();
866
867  virtual void EmitImpl(llvm::Serializer& S) const;
868  static ContinueStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
869};
870
871/// BreakStmt - This represents a break.
872///
873class BreakStmt : public Stmt {
874  SourceLocation BreakLoc;
875public:
876  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
877
878  /// \brief Build an empty break statement.
879  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
880
881  SourceLocation getBreakLoc() const { return BreakLoc; }
882  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
883
884  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
885
886  static bool classof(const Stmt *T) {
887    return T->getStmtClass() == BreakStmtClass;
888  }
889  static bool classof(const BreakStmt *) { return true; }
890
891  // Iterators
892  virtual child_iterator child_begin();
893  virtual child_iterator child_end();
894
895  virtual void EmitImpl(llvm::Serializer& S) const;
896  static BreakStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
897};
898
899
900/// ReturnStmt - This represents a return, optionally of an expression:
901///   return;
902///   return 4;
903///
904/// Note that GCC allows return with no argument in a function declared to
905/// return a value, and it allows returning a value in functions declared to
906/// return void.  We explicitly model this in the AST, which means you can't
907/// depend on the return type of the function and the presence of an argument.
908///
909class ReturnStmt : public Stmt {
910  Stmt *RetExpr;
911  SourceLocation RetLoc;
912public:
913  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
914    RetExpr((Stmt*) E), RetLoc(RL) {}
915
916  const Expr *getRetValue() const;
917  Expr *getRetValue();
918
919  virtual SourceRange getSourceRange() const;
920
921  static bool classof(const Stmt *T) {
922    return T->getStmtClass() == ReturnStmtClass;
923  }
924  static bool classof(const ReturnStmt *) { return true; }
925
926  // Iterators
927  virtual child_iterator child_begin();
928  virtual child_iterator child_end();
929
930  virtual void EmitImpl(llvm::Serializer& S) const;
931  static ReturnStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
932};
933
934/// AsmStmt - This represents a GNU inline-assembly statement extension.
935///
936class AsmStmt : public Stmt {
937  SourceLocation AsmLoc, RParenLoc;
938  StringLiteral *AsmStr;
939
940  bool IsSimple;
941  bool IsVolatile;
942
943  unsigned NumOutputs;
944  unsigned NumInputs;
945
946  llvm::SmallVector<std::string, 4> Names;
947  llvm::SmallVector<StringLiteral*, 4> Constraints;
948  llvm::SmallVector<Stmt*, 4> Exprs;
949
950  llvm::SmallVector<StringLiteral*, 4> Clobbers;
951public:
952  AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
953          unsigned numoutputs, unsigned numinputs,
954          std::string *names, StringLiteral **constraints,
955          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
956          StringLiteral **clobbers, SourceLocation rparenloc);
957
958  bool isVolatile() const { return IsVolatile; }
959  bool isSimple() const { return IsSimple; }
960
961  //===--- Asm String Analysis ---===//
962
963  const StringLiteral *getAsmString() const { return AsmStr; }
964  StringLiteral *getAsmString() { return AsmStr; }
965
966  /// AsmStringPiece - this is part of a decomposed asm string specification
967  /// (for use with the AnalyzeAsmString function below).  An asm string is
968  /// considered to be a concatenation of these parts.
969  class AsmStringPiece {
970  public:
971    enum Kind {
972      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
973      Operand  // Operand reference, with optional modifier %c4.
974    };
975  private:
976    Kind MyKind;
977    std::string Str;
978    unsigned OperandNo;
979  public:
980    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
981    AsmStringPiece(unsigned OpNo, char Modifier)
982      : MyKind(Operand), Str(), OperandNo(OpNo) {
983      Str += Modifier;
984    }
985
986    bool isString() const { return MyKind == String; }
987    bool isOperand() const { return MyKind == Operand; }
988
989    const std::string &getString() const {
990      assert(isString());
991      return Str;
992    }
993
994    unsigned getOperandNo() const {
995      assert(isOperand());
996      return OperandNo;
997    }
998
999    /// getModifier - Get the modifier for this operand, if present.  This
1000    /// returns '\0' if there was no modifier.
1001    char getModifier() const {
1002      assert(isOperand());
1003      return Str[0];
1004    }
1005  };
1006
1007  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1008  /// it into pieces.  If the asm string is erroneous, emit errors and return
1009  /// true, otherwise return false.  This handles canonicalization and
1010  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1011  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1012  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1013                            ASTContext &C, unsigned &DiagOffs) const;
1014
1015
1016  //===--- Output operands ---===//
1017
1018  unsigned getNumOutputs() const { return NumOutputs; }
1019
1020  const std::string &getOutputName(unsigned i) const {
1021    return Names[i];
1022  }
1023
1024  /// getOutputConstraint - Return the constraint string for the specified
1025  /// output operand.  All output constraints are known to be non-empty (either
1026  /// '=' or '+').
1027  std::string getOutputConstraint(unsigned i) const;
1028
1029  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1030    return Constraints[i];
1031  }
1032  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1033    return Constraints[i];
1034  }
1035
1036
1037  Expr *getOutputExpr(unsigned i);
1038
1039  const Expr *getOutputExpr(unsigned i) const {
1040    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1041  }
1042
1043  /// isOutputPlusConstraint - Return true if the specified output constraint
1044  /// is a "+" constraint (which is both an input and an output) or false if it
1045  /// is an "=" constraint (just an output).
1046  bool isOutputPlusConstraint(unsigned i) const {
1047    return getOutputConstraint(i)[0] == '+';
1048  }
1049
1050  /// getNumPlusOperands - Return the number of output operands that have a "+"
1051  /// constraint.
1052  unsigned getNumPlusOperands() const;
1053
1054  //===--- Input operands ---===//
1055
1056  unsigned getNumInputs() const { return NumInputs; }
1057
1058  const std::string &getInputName(unsigned i) const {
1059    return Names[i + NumOutputs];
1060  }
1061
1062  /// getInputConstraint - Return the specified input constraint.  Unlike output
1063  /// constraints, these can be empty.
1064  std::string getInputConstraint(unsigned i) const;
1065
1066  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1067    return Constraints[i + NumOutputs];
1068  }
1069  StringLiteral *getInputConstraintLiteral(unsigned i) {
1070    return Constraints[i + NumOutputs];
1071  }
1072
1073
1074  Expr *getInputExpr(unsigned i);
1075
1076  const Expr *getInputExpr(unsigned i) const {
1077    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1078  }
1079
1080  //===--- Other ---===//
1081
1082  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1083  /// translate this into a numeric value needed to reference the same operand.
1084  /// This returns -1 if the operand name is invalid.
1085  int getNamedOperand(const std::string &SymbolicName) const;
1086
1087
1088
1089  unsigned getNumClobbers() const { return Clobbers.size(); }
1090  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1091  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1092
1093  virtual SourceRange getSourceRange() const {
1094    return SourceRange(AsmLoc, RParenLoc);
1095  }
1096
1097  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1098  static bool classof(const AsmStmt *) { return true; }
1099
1100  // Input expr iterators.
1101
1102  typedef ExprIterator inputs_iterator;
1103  typedef ConstExprIterator const_inputs_iterator;
1104
1105  inputs_iterator begin_inputs() {
1106    return &Exprs[0] + NumOutputs;
1107  }
1108
1109  inputs_iterator end_inputs() {
1110    return  &Exprs[0] + NumOutputs + NumInputs;
1111  }
1112
1113  const_inputs_iterator begin_inputs() const {
1114    return &Exprs[0] + NumOutputs;
1115  }
1116
1117  const_inputs_iterator end_inputs() const {
1118    return  &Exprs[0] + NumOutputs + NumInputs;}
1119
1120  // Output expr iterators.
1121
1122  typedef ExprIterator outputs_iterator;
1123  typedef ConstExprIterator const_outputs_iterator;
1124
1125  outputs_iterator begin_outputs() { return &Exprs[0]; }
1126  outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; }
1127
1128  const_outputs_iterator begin_outputs() const { return &Exprs[0]; }
1129  const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; }
1130
1131  // Input name iterator.
1132
1133  const std::string *begin_output_names() const {
1134    return &Names[0];
1135  }
1136
1137  const std::string *end_output_names() const {
1138    return &Names[0] + NumOutputs;
1139  }
1140
1141  // Child iterators
1142
1143  virtual child_iterator child_begin();
1144  virtual child_iterator child_end();
1145
1146  virtual void EmitImpl(llvm::Serializer& S) const;
1147  static AsmStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1148};
1149
1150/// ObjCForCollectionStmt - This represents Objective-c's collection statement;
1151/// represented as 'for (element 'in' collection-expression)' stmt.
1152///
1153class ObjCForCollectionStmt : public Stmt {
1154  enum { ELEM, COLLECTION, BODY, END_EXPR };
1155  Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
1156  SourceLocation ForLoc;
1157  SourceLocation RParenLoc;
1158public:
1159  ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
1160                        SourceLocation FCL, SourceLocation RPL);
1161
1162  Stmt *getElement() { return SubExprs[ELEM]; }
1163  Expr *getCollection() {
1164    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
1165  }
1166  Stmt *getBody() { return SubExprs[BODY]; }
1167
1168  const Stmt *getElement() const { return SubExprs[ELEM]; }
1169  const Expr *getCollection() const {
1170    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
1171  }
1172  const Stmt *getBody() const { return SubExprs[BODY]; }
1173
1174  SourceLocation getRParenLoc() const { return RParenLoc; }
1175
1176  virtual SourceRange getSourceRange() const {
1177    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1178  }
1179  static bool classof(const Stmt *T) {
1180    return T->getStmtClass() == ObjCForCollectionStmtClass;
1181  }
1182  static bool classof(const ObjCForCollectionStmt *) { return true; }
1183
1184  // Iterators
1185  virtual child_iterator child_begin();
1186  virtual child_iterator child_end();
1187
1188  virtual void EmitImpl(llvm::Serializer& S) const;
1189  static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1190};
1191
1192/// ObjCAtCatchStmt - This represents objective-c's @catch statement.
1193class ObjCAtCatchStmt : public Stmt {
1194private:
1195  enum { BODY, NEXT_CATCH, END_EXPR };
1196  ParmVarDecl *ExceptionDecl;
1197  Stmt *SubExprs[END_EXPR];
1198  SourceLocation AtCatchLoc, RParenLoc;
1199
1200  // Used by deserialization.
1201  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc)
1202  : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {}
1203
1204public:
1205  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
1206                  ParmVarDecl *catchVarDecl,
1207                  Stmt *atCatchStmt, Stmt *atCatchList);
1208
1209  const Stmt *getCatchBody() const { return SubExprs[BODY]; }
1210  Stmt *getCatchBody() { return SubExprs[BODY]; }
1211
1212  const ObjCAtCatchStmt *getNextCatchStmt() const {
1213    return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
1214  }
1215  ObjCAtCatchStmt *getNextCatchStmt() {
1216    return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
1217  }
1218
1219  const ParmVarDecl *getCatchParamDecl() const {
1220    return ExceptionDecl;
1221  }
1222  ParmVarDecl *getCatchParamDecl() {
1223    return ExceptionDecl;
1224  }
1225
1226  SourceLocation getRParenLoc() const { return RParenLoc; }
1227
1228  virtual SourceRange getSourceRange() const {
1229    return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd());
1230  }
1231
1232  bool hasEllipsis() const { return getCatchParamDecl() == 0; }
1233
1234  static bool classof(const Stmt *T) {
1235    return T->getStmtClass() == ObjCAtCatchStmtClass;
1236  }
1237  static bool classof(const ObjCAtCatchStmt *) { return true; }
1238
1239  virtual child_iterator child_begin();
1240  virtual child_iterator child_end();
1241
1242  virtual void EmitImpl(llvm::Serializer& S) const;
1243  static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1244};
1245
1246/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement
1247class ObjCAtFinallyStmt : public Stmt {
1248  Stmt *AtFinallyStmt;
1249  SourceLocation AtFinallyLoc;
1250public:
1251  ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
1252  : Stmt(ObjCAtFinallyStmtClass),
1253    AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
1254
1255  const Stmt *getFinallyBody () const { return AtFinallyStmt; }
1256  Stmt *getFinallyBody () { return AtFinallyStmt; }
1257
1258  virtual SourceRange getSourceRange() const {
1259    return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
1260  }
1261
1262  static bool classof(const Stmt *T) {
1263    return T->getStmtClass() == ObjCAtFinallyStmtClass;
1264  }
1265  static bool classof(const ObjCAtFinallyStmt *) { return true; }
1266
1267  virtual child_iterator child_begin();
1268  virtual child_iterator child_end();
1269
1270  virtual void EmitImpl(llvm::Serializer& S) const;
1271  static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1272};
1273
1274/// ObjCAtTryStmt - This represent objective-c's over-all
1275/// @try ... @catch ... @finally statement.
1276class ObjCAtTryStmt : public Stmt {
1277private:
1278  enum { TRY, CATCH, FINALLY, END_EXPR };
1279  Stmt* SubStmts[END_EXPR];
1280
1281  SourceLocation AtTryLoc;
1282public:
1283  ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
1284                Stmt *atCatchStmt,
1285                Stmt *atFinallyStmt)
1286  : Stmt(ObjCAtTryStmtClass) {
1287      SubStmts[TRY] = atTryStmt;
1288      SubStmts[CATCH] = atCatchStmt;
1289      SubStmts[FINALLY] = atFinallyStmt;
1290      AtTryLoc = atTryLoc;
1291    }
1292
1293  const Stmt *getTryBody() const { return SubStmts[TRY]; }
1294  Stmt *getTryBody() { return SubStmts[TRY]; }
1295  const ObjCAtCatchStmt *getCatchStmts() const {
1296    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1297  }
1298  ObjCAtCatchStmt *getCatchStmts() {
1299    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1300  }
1301  const ObjCAtFinallyStmt *getFinallyStmt() const {
1302    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1303  }
1304  ObjCAtFinallyStmt *getFinallyStmt() {
1305    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1306  }
1307  virtual SourceRange getSourceRange() const {
1308    return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd());
1309  }
1310
1311  static bool classof(const Stmt *T) {
1312    return T->getStmtClass() == ObjCAtTryStmtClass;
1313  }
1314  static bool classof(const ObjCAtTryStmt *) { return true; }
1315
1316  virtual child_iterator child_begin();
1317  virtual child_iterator child_end();
1318
1319  virtual void EmitImpl(llvm::Serializer& S) const;
1320  static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1321};
1322
1323/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement.
1324/// Example: @synchronized (sem) {
1325///             do-something;
1326///          }
1327///
1328class ObjCAtSynchronizedStmt : public Stmt {
1329private:
1330  enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
1331  Stmt* SubStmts[END_EXPR];
1332  SourceLocation AtSynchronizedLoc;
1333
1334public:
1335  ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
1336                         Stmt *synchBody)
1337  : Stmt(ObjCAtSynchronizedStmtClass) {
1338      SubStmts[SYNC_EXPR] = synchExpr;
1339      SubStmts[SYNC_BODY] = synchBody;
1340      AtSynchronizedLoc = atSynchronizedLoc;
1341    }
1342
1343  const CompoundStmt *getSynchBody() const {
1344    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1345  }
1346  CompoundStmt *getSynchBody() {
1347    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1348  }
1349
1350  const Expr *getSynchExpr() const {
1351    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1352  }
1353  Expr *getSynchExpr() {
1354    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1355  }
1356
1357  virtual SourceRange getSourceRange() const {
1358    return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd());
1359  }
1360
1361  static bool classof(const Stmt *T) {
1362    return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
1363  }
1364  static bool classof(const ObjCAtSynchronizedStmt *) { return true; }
1365
1366  virtual child_iterator child_begin();
1367  virtual child_iterator child_end();
1368
1369  virtual void EmitImpl(llvm::Serializer& S) const;
1370  static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D,
1371                                            ASTContext& C);
1372};
1373
1374/// ObjCAtThrowStmt - This represents objective-c's @throw statement.
1375class ObjCAtThrowStmt : public Stmt {
1376  Stmt *Throw;
1377  SourceLocation AtThrowLoc;
1378public:
1379  ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
1380  : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
1381    AtThrowLoc = atThrowLoc;
1382  }
1383
1384  const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
1385  Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
1386
1387  virtual SourceRange getSourceRange() const {
1388    if (Throw)
1389      return SourceRange(AtThrowLoc, Throw->getLocEnd());
1390    else
1391      return SourceRange(AtThrowLoc);
1392  }
1393
1394  static bool classof(const Stmt *T) {
1395    return T->getStmtClass() == ObjCAtThrowStmtClass;
1396  }
1397  static bool classof(const ObjCAtThrowStmt *) { return true; }
1398
1399  virtual child_iterator child_begin();
1400  virtual child_iterator child_end();
1401
1402  virtual void EmitImpl(llvm::Serializer& S) const;
1403  static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1404};
1405
1406/// CXXCatchStmt - This represents a C++ catch block.
1407class CXXCatchStmt : public Stmt {
1408  SourceLocation CatchLoc;
1409  /// The exception-declaration of the type.
1410  Decl *ExceptionDecl;
1411  /// The handler block.
1412  Stmt *HandlerBlock;
1413
1414public:
1415  CXXCatchStmt(SourceLocation catchLoc, Decl *exDecl, Stmt *handlerBlock)
1416  : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
1417    HandlerBlock(handlerBlock) {}
1418
1419  virtual void Destroy(ASTContext& Ctx);
1420
1421  virtual SourceRange getSourceRange() const {
1422    return SourceRange(CatchLoc, HandlerBlock->getLocEnd());
1423  }
1424
1425  Decl *getExceptionDecl() { return ExceptionDecl; }
1426  QualType getCaughtType();
1427  Stmt *getHandlerBlock() { return HandlerBlock; }
1428
1429  static bool classof(const Stmt *T) {
1430    return T->getStmtClass() == CXXCatchStmtClass;
1431  }
1432  static bool classof(const CXXCatchStmt *) { return true; }
1433
1434  virtual child_iterator child_begin();
1435  virtual child_iterator child_end();
1436
1437  virtual void EmitImpl(llvm::Serializer& S) const;
1438  static CXXCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1439};
1440
1441/// CXXTryStmt - A C++ try block, including all handlers.
1442class CXXTryStmt : public Stmt {
1443  SourceLocation TryLoc;
1444  // First place is the guarded CompoundStatement. Subsequent are the handlers.
1445  // More than three handlers should be rare.
1446  llvm::SmallVector<Stmt*, 4> Stmts;
1447
1448public:
1449  CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
1450             Stmt **handlers, unsigned numHandlers);
1451
1452  virtual SourceRange getSourceRange() const {
1453    return SourceRange(TryLoc, Stmts.back()->getLocEnd());
1454  }
1455
1456  CompoundStmt *getTryBlock() { return llvm::cast<CompoundStmt>(Stmts[0]); }
1457  const CompoundStmt *getTryBlock() const {
1458    return llvm::cast<CompoundStmt>(Stmts[0]);
1459  }
1460
1461  unsigned getNumHandlers() const { return Stmts.size() - 1; }
1462  CXXCatchStmt *getHandler(unsigned i) {
1463    return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
1464  }
1465  const CXXCatchStmt *getHandler(unsigned i) const {
1466    return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
1467  }
1468
1469  static bool classof(const Stmt *T) {
1470    return T->getStmtClass() == CXXTryStmtClass;
1471  }
1472  static bool classof(const CXXTryStmt *) { return true; }
1473
1474  virtual child_iterator child_begin();
1475  virtual child_iterator child_end();
1476
1477  virtual void EmitImpl(llvm::Serializer& S) const;
1478  static CXXTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1479};
1480
1481}  // end namespace clang
1482
1483#endif
1484