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