Stmt.h revision 0de9d8857b715c2f45c987651f4ce06d73330d93
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  /// \brief Build an empty do-while statement.
720  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
721
722  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
723  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
724  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
725  Stmt *getBody() { return SubExprs[BODY]; }
726  const Stmt *getBody() const { return SubExprs[BODY]; }
727  void setBody(Stmt *S) { SubExprs[BODY] = S; }
728
729  SourceLocation getDoLoc() const { return DoLoc; }
730  void setDoLoc(SourceLocation L) { DoLoc = L; }
731
732  virtual SourceRange getSourceRange() const {
733    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
734  }
735  static bool classof(const Stmt *T) {
736    return T->getStmtClass() == DoStmtClass;
737  }
738  static bool classof(const DoStmt *) { return true; }
739
740  // Iterators
741  virtual child_iterator child_begin();
742  virtual child_iterator child_end();
743
744  virtual void EmitImpl(llvm::Serializer& S) const;
745  static DoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
746};
747
748
749/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
750/// the init/cond/inc parts of the ForStmt will be null if they were not
751/// specified in the source.
752///
753class ForStmt : public Stmt {
754  enum { INIT, COND, INC, BODY, END_EXPR };
755  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
756  SourceLocation ForLoc;
757public:
758  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL)
759    : Stmt(ForStmtClass) {
760    SubExprs[INIT] = Init;
761    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
762    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
763    SubExprs[BODY] = Body;
764    ForLoc = FL;
765  }
766
767  /// \brief Build an empty for statement.
768  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
769
770  Stmt *getInit() { return SubExprs[INIT]; }
771  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
772  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
773  Stmt *getBody() { return SubExprs[BODY]; }
774
775  const Stmt *getInit() const { return SubExprs[INIT]; }
776  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
777  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
778  const Stmt *getBody() const { return SubExprs[BODY]; }
779
780  void setInit(Stmt *S) { SubExprs[INIT] = S; }
781  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
782  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
783  void setBody(Stmt *S) { SubExprs[BODY] = S; }
784
785  SourceLocation getForLoc() const { return ForLoc; }
786  void setForLoc(SourceLocation L) { ForLoc = L; }
787
788  virtual SourceRange getSourceRange() const {
789    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
790  }
791  static bool classof(const Stmt *T) {
792    return T->getStmtClass() == ForStmtClass;
793  }
794  static bool classof(const ForStmt *) { return true; }
795
796  // Iterators
797  virtual child_iterator child_begin();
798  virtual child_iterator child_end();
799
800  virtual void EmitImpl(llvm::Serializer& S) const;
801  static ForStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
802};
803
804/// GotoStmt - This represents a direct goto.
805///
806class GotoStmt : public Stmt {
807  LabelStmt *Label;
808  SourceLocation GotoLoc;
809  SourceLocation LabelLoc;
810public:
811  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
812    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
813
814  LabelStmt *getLabel() const { return Label; }
815
816  virtual SourceRange getSourceRange() const {
817    return SourceRange(GotoLoc, LabelLoc);
818  }
819  static bool classof(const Stmt *T) {
820    return T->getStmtClass() == GotoStmtClass;
821  }
822  static bool classof(const GotoStmt *) { 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 GotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
830};
831
832/// IndirectGotoStmt - This represents an indirect goto.
833///
834class IndirectGotoStmt : public Stmt {
835  Stmt *Target;
836  // FIXME: Add location information (e.g. SourceLocation objects).
837  //        When doing so, update the serialization routines.
838public:
839  IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass),
840                                   Target((Stmt*)target){}
841
842  Expr *getTarget();
843  const Expr *getTarget() const;
844
845  virtual SourceRange getSourceRange() const { return SourceRange(); }
846
847  static bool classof(const Stmt *T) {
848    return T->getStmtClass() == IndirectGotoStmtClass;
849  }
850  static bool classof(const IndirectGotoStmt *) { return true; }
851
852  // Iterators
853  virtual child_iterator child_begin();
854  virtual child_iterator child_end();
855
856  virtual void EmitImpl(llvm::Serializer& S) const;
857  static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
858};
859
860
861/// ContinueStmt - This represents a continue.
862///
863class ContinueStmt : public Stmt {
864  SourceLocation ContinueLoc;
865public:
866  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
867
868  /// \brief Build an empty continue statement.
869  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
870
871  SourceLocation getContinueLoc() const { return ContinueLoc; }
872  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
873
874  virtual SourceRange getSourceRange() const {
875    return SourceRange(ContinueLoc);
876  }
877  static bool classof(const Stmt *T) {
878    return T->getStmtClass() == ContinueStmtClass;
879  }
880  static bool classof(const ContinueStmt *) { return true; }
881
882  // Iterators
883  virtual child_iterator child_begin();
884  virtual child_iterator child_end();
885
886  virtual void EmitImpl(llvm::Serializer& S) const;
887  static ContinueStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
888};
889
890/// BreakStmt - This represents a break.
891///
892class BreakStmt : public Stmt {
893  SourceLocation BreakLoc;
894public:
895  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
896
897  /// \brief Build an empty break statement.
898  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
899
900  SourceLocation getBreakLoc() const { return BreakLoc; }
901  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
902
903  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
904
905  static bool classof(const Stmt *T) {
906    return T->getStmtClass() == BreakStmtClass;
907  }
908  static bool classof(const BreakStmt *) { return true; }
909
910  // Iterators
911  virtual child_iterator child_begin();
912  virtual child_iterator child_end();
913
914  virtual void EmitImpl(llvm::Serializer& S) const;
915  static BreakStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
916};
917
918
919/// ReturnStmt - This represents a return, optionally of an expression:
920///   return;
921///   return 4;
922///
923/// Note that GCC allows return with no argument in a function declared to
924/// return a value, and it allows returning a value in functions declared to
925/// return void.  We explicitly model this in the AST, which means you can't
926/// depend on the return type of the function and the presence of an argument.
927///
928class ReturnStmt : public Stmt {
929  Stmt *RetExpr;
930  SourceLocation RetLoc;
931public:
932  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
933    RetExpr((Stmt*) E), RetLoc(RL) {}
934
935  /// \brief Build an empty return expression.
936  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
937
938  const Expr *getRetValue() const;
939  Expr *getRetValue();
940  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
941
942  SourceLocation getReturnLoc() const { return RetLoc; }
943  void setReturnLoc(SourceLocation L) { RetLoc = L; }
944
945  virtual SourceRange getSourceRange() const;
946
947  static bool classof(const Stmt *T) {
948    return T->getStmtClass() == ReturnStmtClass;
949  }
950  static bool classof(const ReturnStmt *) { return true; }
951
952  // Iterators
953  virtual child_iterator child_begin();
954  virtual child_iterator child_end();
955
956  virtual void EmitImpl(llvm::Serializer& S) const;
957  static ReturnStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
958};
959
960/// AsmStmt - This represents a GNU inline-assembly statement extension.
961///
962class AsmStmt : public Stmt {
963  SourceLocation AsmLoc, RParenLoc;
964  StringLiteral *AsmStr;
965
966  bool IsSimple;
967  bool IsVolatile;
968
969  unsigned NumOutputs;
970  unsigned NumInputs;
971
972  llvm::SmallVector<std::string, 4> Names;
973  llvm::SmallVector<StringLiteral*, 4> Constraints;
974  llvm::SmallVector<Stmt*, 4> Exprs;
975
976  llvm::SmallVector<StringLiteral*, 4> Clobbers;
977public:
978  AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
979          unsigned numoutputs, unsigned numinputs,
980          std::string *names, StringLiteral **constraints,
981          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
982          StringLiteral **clobbers, SourceLocation rparenloc);
983
984  bool isVolatile() const { return IsVolatile; }
985  bool isSimple() const { return IsSimple; }
986
987  //===--- Asm String Analysis ---===//
988
989  const StringLiteral *getAsmString() const { return AsmStr; }
990  StringLiteral *getAsmString() { return AsmStr; }
991
992  /// AsmStringPiece - this is part of a decomposed asm string specification
993  /// (for use with the AnalyzeAsmString function below).  An asm string is
994  /// considered to be a concatenation of these parts.
995  class AsmStringPiece {
996  public:
997    enum Kind {
998      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
999      Operand  // Operand reference, with optional modifier %c4.
1000    };
1001  private:
1002    Kind MyKind;
1003    std::string Str;
1004    unsigned OperandNo;
1005  public:
1006    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1007    AsmStringPiece(unsigned OpNo, char Modifier)
1008      : MyKind(Operand), Str(), OperandNo(OpNo) {
1009      Str += Modifier;
1010    }
1011
1012    bool isString() const { return MyKind == String; }
1013    bool isOperand() const { return MyKind == Operand; }
1014
1015    const std::string &getString() const {
1016      assert(isString());
1017      return Str;
1018    }
1019
1020    unsigned getOperandNo() const {
1021      assert(isOperand());
1022      return OperandNo;
1023    }
1024
1025    /// getModifier - Get the modifier for this operand, if present.  This
1026    /// returns '\0' if there was no modifier.
1027    char getModifier() const {
1028      assert(isOperand());
1029      return Str[0];
1030    }
1031  };
1032
1033  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1034  /// it into pieces.  If the asm string is erroneous, emit errors and return
1035  /// true, otherwise return false.  This handles canonicalization and
1036  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1037  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1038  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1039                            ASTContext &C, unsigned &DiagOffs) const;
1040
1041
1042  //===--- Output operands ---===//
1043
1044  unsigned getNumOutputs() const { return NumOutputs; }
1045
1046  const std::string &getOutputName(unsigned i) const {
1047    return Names[i];
1048  }
1049
1050  /// getOutputConstraint - Return the constraint string for the specified
1051  /// output operand.  All output constraints are known to be non-empty (either
1052  /// '=' or '+').
1053  std::string getOutputConstraint(unsigned i) const;
1054
1055  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1056    return Constraints[i];
1057  }
1058  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1059    return Constraints[i];
1060  }
1061
1062
1063  Expr *getOutputExpr(unsigned i);
1064
1065  const Expr *getOutputExpr(unsigned i) const {
1066    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1067  }
1068
1069  /// isOutputPlusConstraint - Return true if the specified output constraint
1070  /// is a "+" constraint (which is both an input and an output) or false if it
1071  /// is an "=" constraint (just an output).
1072  bool isOutputPlusConstraint(unsigned i) const {
1073    return getOutputConstraint(i)[0] == '+';
1074  }
1075
1076  /// getNumPlusOperands - Return the number of output operands that have a "+"
1077  /// constraint.
1078  unsigned getNumPlusOperands() const;
1079
1080  //===--- Input operands ---===//
1081
1082  unsigned getNumInputs() const { return NumInputs; }
1083
1084  const std::string &getInputName(unsigned i) const {
1085    return Names[i + NumOutputs];
1086  }
1087
1088  /// getInputConstraint - Return the specified input constraint.  Unlike output
1089  /// constraints, these can be empty.
1090  std::string getInputConstraint(unsigned i) const;
1091
1092  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1093    return Constraints[i + NumOutputs];
1094  }
1095  StringLiteral *getInputConstraintLiteral(unsigned i) {
1096    return Constraints[i + NumOutputs];
1097  }
1098
1099
1100  Expr *getInputExpr(unsigned i);
1101
1102  const Expr *getInputExpr(unsigned i) const {
1103    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1104  }
1105
1106  //===--- Other ---===//
1107
1108  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1109  /// translate this into a numeric value needed to reference the same operand.
1110  /// This returns -1 if the operand name is invalid.
1111  int getNamedOperand(const std::string &SymbolicName) const;
1112
1113
1114
1115  unsigned getNumClobbers() const { return Clobbers.size(); }
1116  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1117  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1118
1119  virtual SourceRange getSourceRange() const {
1120    return SourceRange(AsmLoc, RParenLoc);
1121  }
1122
1123  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1124  static bool classof(const AsmStmt *) { return true; }
1125
1126  // Input expr iterators.
1127
1128  typedef ExprIterator inputs_iterator;
1129  typedef ConstExprIterator const_inputs_iterator;
1130
1131  inputs_iterator begin_inputs() {
1132    return &Exprs[0] + NumOutputs;
1133  }
1134
1135  inputs_iterator end_inputs() {
1136    return  &Exprs[0] + NumOutputs + NumInputs;
1137  }
1138
1139  const_inputs_iterator begin_inputs() const {
1140    return &Exprs[0] + NumOutputs;
1141  }
1142
1143  const_inputs_iterator end_inputs() const {
1144    return  &Exprs[0] + NumOutputs + NumInputs;}
1145
1146  // Output expr iterators.
1147
1148  typedef ExprIterator outputs_iterator;
1149  typedef ConstExprIterator const_outputs_iterator;
1150
1151  outputs_iterator begin_outputs() { return &Exprs[0]; }
1152  outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; }
1153
1154  const_outputs_iterator begin_outputs() const { return &Exprs[0]; }
1155  const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; }
1156
1157  // Input name iterator.
1158
1159  const std::string *begin_output_names() const {
1160    return &Names[0];
1161  }
1162
1163  const std::string *end_output_names() const {
1164    return &Names[0] + NumOutputs;
1165  }
1166
1167  // Child iterators
1168
1169  virtual child_iterator child_begin();
1170  virtual child_iterator child_end();
1171
1172  virtual void EmitImpl(llvm::Serializer& S) const;
1173  static AsmStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1174};
1175
1176/// ObjCForCollectionStmt - This represents Objective-c's collection statement;
1177/// represented as 'for (element 'in' collection-expression)' stmt.
1178///
1179class ObjCForCollectionStmt : public Stmt {
1180  enum { ELEM, COLLECTION, BODY, END_EXPR };
1181  Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
1182  SourceLocation ForLoc;
1183  SourceLocation RParenLoc;
1184public:
1185  ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
1186                        SourceLocation FCL, SourceLocation RPL);
1187
1188  Stmt *getElement() { return SubExprs[ELEM]; }
1189  Expr *getCollection() {
1190    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
1191  }
1192  Stmt *getBody() { return SubExprs[BODY]; }
1193
1194  const Stmt *getElement() const { return SubExprs[ELEM]; }
1195  const Expr *getCollection() const {
1196    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
1197  }
1198  const Stmt *getBody() const { return SubExprs[BODY]; }
1199
1200  SourceLocation getRParenLoc() const { return RParenLoc; }
1201
1202  virtual SourceRange getSourceRange() const {
1203    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1204  }
1205  static bool classof(const Stmt *T) {
1206    return T->getStmtClass() == ObjCForCollectionStmtClass;
1207  }
1208  static bool classof(const ObjCForCollectionStmt *) { return true; }
1209
1210  // Iterators
1211  virtual child_iterator child_begin();
1212  virtual child_iterator child_end();
1213
1214  virtual void EmitImpl(llvm::Serializer& S) const;
1215  static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1216};
1217
1218/// ObjCAtCatchStmt - This represents objective-c's @catch statement.
1219class ObjCAtCatchStmt : public Stmt {
1220private:
1221  enum { BODY, NEXT_CATCH, END_EXPR };
1222  ParmVarDecl *ExceptionDecl;
1223  Stmt *SubExprs[END_EXPR];
1224  SourceLocation AtCatchLoc, RParenLoc;
1225
1226  // Used by deserialization.
1227  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc)
1228  : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {}
1229
1230public:
1231  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
1232                  ParmVarDecl *catchVarDecl,
1233                  Stmt *atCatchStmt, Stmt *atCatchList);
1234
1235  const Stmt *getCatchBody() const { return SubExprs[BODY]; }
1236  Stmt *getCatchBody() { return SubExprs[BODY]; }
1237
1238  const ObjCAtCatchStmt *getNextCatchStmt() const {
1239    return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
1240  }
1241  ObjCAtCatchStmt *getNextCatchStmt() {
1242    return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
1243  }
1244
1245  const ParmVarDecl *getCatchParamDecl() const {
1246    return ExceptionDecl;
1247  }
1248  ParmVarDecl *getCatchParamDecl() {
1249    return ExceptionDecl;
1250  }
1251
1252  SourceLocation getRParenLoc() const { return RParenLoc; }
1253
1254  virtual SourceRange getSourceRange() const {
1255    return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd());
1256  }
1257
1258  bool hasEllipsis() const { return getCatchParamDecl() == 0; }
1259
1260  static bool classof(const Stmt *T) {
1261    return T->getStmtClass() == ObjCAtCatchStmtClass;
1262  }
1263  static bool classof(const ObjCAtCatchStmt *) { return true; }
1264
1265  virtual child_iterator child_begin();
1266  virtual child_iterator child_end();
1267
1268  virtual void EmitImpl(llvm::Serializer& S) const;
1269  static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1270};
1271
1272/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement
1273class ObjCAtFinallyStmt : public Stmt {
1274  Stmt *AtFinallyStmt;
1275  SourceLocation AtFinallyLoc;
1276public:
1277  ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
1278  : Stmt(ObjCAtFinallyStmtClass),
1279    AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
1280
1281  const Stmt *getFinallyBody () const { return AtFinallyStmt; }
1282  Stmt *getFinallyBody () { return AtFinallyStmt; }
1283
1284  virtual SourceRange getSourceRange() const {
1285    return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
1286  }
1287
1288  static bool classof(const Stmt *T) {
1289    return T->getStmtClass() == ObjCAtFinallyStmtClass;
1290  }
1291  static bool classof(const ObjCAtFinallyStmt *) { return true; }
1292
1293  virtual child_iterator child_begin();
1294  virtual child_iterator child_end();
1295
1296  virtual void EmitImpl(llvm::Serializer& S) const;
1297  static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1298};
1299
1300/// ObjCAtTryStmt - This represent objective-c's over-all
1301/// @try ... @catch ... @finally statement.
1302class ObjCAtTryStmt : public Stmt {
1303private:
1304  enum { TRY, CATCH, FINALLY, END_EXPR };
1305  Stmt* SubStmts[END_EXPR];
1306
1307  SourceLocation AtTryLoc;
1308public:
1309  ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
1310                Stmt *atCatchStmt,
1311                Stmt *atFinallyStmt)
1312  : Stmt(ObjCAtTryStmtClass) {
1313      SubStmts[TRY] = atTryStmt;
1314      SubStmts[CATCH] = atCatchStmt;
1315      SubStmts[FINALLY] = atFinallyStmt;
1316      AtTryLoc = atTryLoc;
1317    }
1318
1319  const Stmt *getTryBody() const { return SubStmts[TRY]; }
1320  Stmt *getTryBody() { return SubStmts[TRY]; }
1321  const ObjCAtCatchStmt *getCatchStmts() const {
1322    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1323  }
1324  ObjCAtCatchStmt *getCatchStmts() {
1325    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1326  }
1327  const ObjCAtFinallyStmt *getFinallyStmt() const {
1328    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1329  }
1330  ObjCAtFinallyStmt *getFinallyStmt() {
1331    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1332  }
1333  virtual SourceRange getSourceRange() const {
1334    return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd());
1335  }
1336
1337  static bool classof(const Stmt *T) {
1338    return T->getStmtClass() == ObjCAtTryStmtClass;
1339  }
1340  static bool classof(const ObjCAtTryStmt *) { return true; }
1341
1342  virtual child_iterator child_begin();
1343  virtual child_iterator child_end();
1344
1345  virtual void EmitImpl(llvm::Serializer& S) const;
1346  static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1347};
1348
1349/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement.
1350/// Example: @synchronized (sem) {
1351///             do-something;
1352///          }
1353///
1354class ObjCAtSynchronizedStmt : public Stmt {
1355private:
1356  enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
1357  Stmt* SubStmts[END_EXPR];
1358  SourceLocation AtSynchronizedLoc;
1359
1360public:
1361  ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
1362                         Stmt *synchBody)
1363  : Stmt(ObjCAtSynchronizedStmtClass) {
1364      SubStmts[SYNC_EXPR] = synchExpr;
1365      SubStmts[SYNC_BODY] = synchBody;
1366      AtSynchronizedLoc = atSynchronizedLoc;
1367    }
1368
1369  const CompoundStmt *getSynchBody() const {
1370    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1371  }
1372  CompoundStmt *getSynchBody() {
1373    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1374  }
1375
1376  const Expr *getSynchExpr() const {
1377    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1378  }
1379  Expr *getSynchExpr() {
1380    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1381  }
1382
1383  virtual SourceRange getSourceRange() const {
1384    return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd());
1385  }
1386
1387  static bool classof(const Stmt *T) {
1388    return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
1389  }
1390  static bool classof(const ObjCAtSynchronizedStmt *) { return true; }
1391
1392  virtual child_iterator child_begin();
1393  virtual child_iterator child_end();
1394
1395  virtual void EmitImpl(llvm::Serializer& S) const;
1396  static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D,
1397                                            ASTContext& C);
1398};
1399
1400/// ObjCAtThrowStmt - This represents objective-c's @throw statement.
1401class ObjCAtThrowStmt : public Stmt {
1402  Stmt *Throw;
1403  SourceLocation AtThrowLoc;
1404public:
1405  ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
1406  : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
1407    AtThrowLoc = atThrowLoc;
1408  }
1409
1410  const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
1411  Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
1412
1413  virtual SourceRange getSourceRange() const {
1414    if (Throw)
1415      return SourceRange(AtThrowLoc, Throw->getLocEnd());
1416    else
1417      return SourceRange(AtThrowLoc);
1418  }
1419
1420  static bool classof(const Stmt *T) {
1421    return T->getStmtClass() == ObjCAtThrowStmtClass;
1422  }
1423  static bool classof(const ObjCAtThrowStmt *) { return true; }
1424
1425  virtual child_iterator child_begin();
1426  virtual child_iterator child_end();
1427
1428  virtual void EmitImpl(llvm::Serializer& S) const;
1429  static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1430};
1431
1432/// CXXCatchStmt - This represents a C++ catch block.
1433class CXXCatchStmt : public Stmt {
1434  SourceLocation CatchLoc;
1435  /// The exception-declaration of the type.
1436  Decl *ExceptionDecl;
1437  /// The handler block.
1438  Stmt *HandlerBlock;
1439
1440public:
1441  CXXCatchStmt(SourceLocation catchLoc, Decl *exDecl, Stmt *handlerBlock)
1442  : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
1443    HandlerBlock(handlerBlock) {}
1444
1445  virtual void Destroy(ASTContext& Ctx);
1446
1447  virtual SourceRange getSourceRange() const {
1448    return SourceRange(CatchLoc, HandlerBlock->getLocEnd());
1449  }
1450
1451  Decl *getExceptionDecl() { return ExceptionDecl; }
1452  QualType getCaughtType();
1453  Stmt *getHandlerBlock() { return HandlerBlock; }
1454
1455  static bool classof(const Stmt *T) {
1456    return T->getStmtClass() == CXXCatchStmtClass;
1457  }
1458  static bool classof(const CXXCatchStmt *) { return true; }
1459
1460  virtual child_iterator child_begin();
1461  virtual child_iterator child_end();
1462
1463  virtual void EmitImpl(llvm::Serializer& S) const;
1464  static CXXCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1465};
1466
1467/// CXXTryStmt - A C++ try block, including all handlers.
1468class CXXTryStmt : public Stmt {
1469  SourceLocation TryLoc;
1470  // First place is the guarded CompoundStatement. Subsequent are the handlers.
1471  // More than three handlers should be rare.
1472  llvm::SmallVector<Stmt*, 4> Stmts;
1473
1474public:
1475  CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
1476             Stmt **handlers, unsigned numHandlers);
1477
1478  virtual SourceRange getSourceRange() const {
1479    return SourceRange(TryLoc, Stmts.back()->getLocEnd());
1480  }
1481
1482  CompoundStmt *getTryBlock() { return llvm::cast<CompoundStmt>(Stmts[0]); }
1483  const CompoundStmt *getTryBlock() const {
1484    return llvm::cast<CompoundStmt>(Stmts[0]);
1485  }
1486
1487  unsigned getNumHandlers() const { return Stmts.size() - 1; }
1488  CXXCatchStmt *getHandler(unsigned i) {
1489    return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
1490  }
1491  const CXXCatchStmt *getHandler(unsigned i) const {
1492    return llvm::cast<CXXCatchStmt>(Stmts[i + 1]);
1493  }
1494
1495  static bool classof(const Stmt *T) {
1496    return T->getStmtClass() == CXXTryStmtClass;
1497  }
1498  static bool classof(const CXXTryStmt *) { return true; }
1499
1500  virtual child_iterator child_begin();
1501  virtual child_iterator child_end();
1502
1503  virtual void EmitImpl(llvm::Serializer& S) const;
1504  static CXXTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1505};
1506
1507}  // end namespace clang
1508
1509#endif
1510