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