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