Stmt.h revision 65aa3b9fda7b2114f708014e3d42176abc0e39ab
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 "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/iterator.h"
23#include "llvm/Bitcode/SerializationFwd.h"
24#include <string>
25
26using llvm::dyn_cast_or_null;
27
28namespace clang {
29  class ASTContext;
30  class Expr;
31  class Decl;
32  class ScopedDecl;
33  class IdentifierInfo;
34  class SourceManager;
35  class StringLiteral;
36  class SwitchStmt;
37  class PrinterHelper;
38
39/// Stmt - This represents one statement.
40///
41class Stmt {
42public:
43  enum StmtClass {
44#define STMT(N, CLASS, PARENT) CLASS##Class = N,
45#define FIRST_STMT(N) firstStmtConstant = N,
46#define LAST_STMT(N) lastStmtConstant = N,
47#define FIRST_EXPR(N) firstExprConstant = N,
48#define LAST_EXPR(N) lastExprConstant = N
49#include "clang/AST/StmtNodes.def"
50};
51private:
52  const StmtClass sClass;
53
54protected:
55  /// DestroyChildren - Invoked by destructors of subclasses of Stmt to
56  ///  recursively release child AST nodes.
57  void DestroyChildren(ASTContext& Ctx);
58
59public:
60  Stmt(StmtClass SC) : sClass(SC) {
61    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
62  }
63  virtual ~Stmt() {}
64
65  virtual void Destroy(ASTContext& Ctx);
66
67  StmtClass getStmtClass() const { return sClass; }
68  const char *getStmtClassName() const;
69
70  /// SourceLocation tokens are not useful in isolation - they are low level
71  /// value objects created/interpreted by SourceManager. We assume AST
72  /// clients will have a pointer to the respective SourceManager.
73  virtual SourceRange getSourceRange() const = 0;
74  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
75  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
76
77  // global temp stats (until we have a per-module visitor)
78  static void addStmtClass(const StmtClass s);
79  static bool CollectingStats(bool enable=false);
80  static void PrintStats();
81
82  /// dump - This does a local dump of the specified AST fragment.  It dumps the
83  /// specified node and a few nodes underneath it, but not the whole subtree.
84  /// This is useful in a debugger.
85  void dump() const;
86  void dump(SourceManager &SM) const;
87
88  /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
89  void dumpAll() const;
90  void dumpAll(SourceManager &SM) const;
91
92  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
93  /// back to its original source language syntax.
94  void dumpPretty() const;
95  void printPretty(llvm::raw_ostream &OS, PrinterHelper* = NULL) const;
96
97  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
98  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
99  void viewAST() const;
100
101  // Implement isa<T> support.
102  static bool classof(const Stmt *) { return true; }
103
104  /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
105  ///  contain implicit control-flow in the order their subexpressions
106  ///  are evaluated.  This predicate returns true if this statement has
107  ///  such implicit control-flow.  Such statements are also specially handled
108  ///  within CFGs.
109  bool hasImplicitControlFlow() const;
110
111  /// Child Iterators: All subclasses must implement child_begin and child_end
112  ///  to permit easy iteration over the substatements/subexpessions of an
113  ///  AST node.  This permits easy iteration over all nodes in the AST.
114  typedef StmtIterator       child_iterator;
115  typedef ConstStmtIterator  const_child_iterator;
116
117  virtual child_iterator child_begin() = 0;
118  virtual child_iterator child_end()   = 0;
119
120  const_child_iterator child_begin() const {
121    return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
122  }
123
124  const_child_iterator child_end() const {
125    return const_child_iterator(const_cast<Stmt*>(this)->child_end());
126  }
127
128  void Emit(llvm::Serializer& S) const;
129  static Stmt* Create(llvm::Deserializer& D, ASTContext& C);
130
131  virtual void EmitImpl(llvm::Serializer& S) const {
132    // This method will eventually be a pure-virtual function.
133    assert (false && "Not implemented.");
134  }
135};
136
137/// DeclStmt - Adaptor class for mixing declarations with statements and
138/// expressions. For example, CompoundStmt mixes statements, expressions
139/// and declarations (variables, types). Another example is ForStmt, where
140/// the first statement can be an expression or a declaration.
141///
142class DeclStmt : public Stmt {
143protected:
144  ScopedDecl *TheDecl;
145  SourceLocation StartLoc, EndLoc;
146public:
147  DeclStmt(ScopedDecl *D, SourceLocation startLoc, SourceLocation endLoc)
148    : Stmt(DeclStmtClass), TheDecl(D), StartLoc(startLoc), EndLoc(endLoc) {}
149
150  virtual void Destroy(ASTContext& Ctx);
151
152  // hasSolitaryDecl - This method returns true if this DeclStmt refers
153  // to a single Decl.
154  bool hasSolitaryDecl() const;
155
156  const ScopedDecl *getDecl() const { return TheDecl; }
157  ScopedDecl *getDecl() { return TheDecl; }
158
159  const ScopedDecl* getSolitaryDecl() const {
160    assert (hasSolitaryDecl() &&
161            "Caller assumes this DeclStmt points to one Decl*");
162    return TheDecl;
163  }
164
165  ScopedDecl* getSolitaryDecl() {
166    assert (hasSolitaryDecl() &&
167            "Caller assumes this DeclStmt points to one Decl*");
168    return TheDecl;
169  }
170
171  SourceLocation getStartLoc() const { return StartLoc; }
172  SourceLocation getEndLoc() const { return EndLoc; }
173  virtual SourceRange getSourceRange() const {
174    return SourceRange(StartLoc, EndLoc);
175  }
176
177  static bool classof(const Stmt *T) {
178    return T->getStmtClass() == DeclStmtClass;
179  }
180  static bool classof(const DeclStmt *) { return true; }
181
182  // Iterators over subexpressions.
183  virtual child_iterator child_begin();
184  virtual child_iterator child_end();
185
186  // Iterators over the decls.
187  class decl_iterator {
188    ScopedDecl* D;
189  public:
190    decl_iterator(ScopedDecl *d) : D(d) {}
191    bool operator==(const decl_iterator& I) const { return D == I.D; }
192    bool operator!=(const decl_iterator& I) const { return D != I.D; }
193    ScopedDecl* operator*() const { return D; }
194    decl_iterator& operator++();
195  };
196
197  virtual decl_iterator decl_begin() { return TheDecl; }
198  virtual decl_iterator decl_end() { return 0; }
199
200  class const_decl_iterator {
201    decl_iterator Impl;
202  public:
203    const_decl_iterator(const ScopedDecl *d)
204      : Impl(const_cast<ScopedDecl*>(d)) {}
205
206    bool operator==(const const_decl_iterator& I) const {
207      return Impl == I.Impl;
208    }
209    bool operator!=(const const_decl_iterator& I) const {
210      return Impl != I.Impl;
211    }
212    const ScopedDecl* operator*() const {
213      return *Impl;
214    }
215    const_decl_iterator& operator++() {
216      ++Impl; return *this;
217    }
218  };
219
220  const_decl_iterator decl_begin() const { return TheDecl; }
221  const_decl_iterator decl_end() const { return 0; }
222
223  // Serialization.
224  virtual void EmitImpl(llvm::Serializer& S) const;
225  static DeclStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
226};
227
228/// NullStmt - This is the null statement ";": C99 6.8.3p3.
229///
230class NullStmt : public Stmt {
231  SourceLocation SemiLoc;
232public:
233  NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
234
235  SourceLocation getSemiLoc() const { return SemiLoc; }
236
237  virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
238
239  static bool classof(const Stmt *T) {
240    return T->getStmtClass() == NullStmtClass;
241  }
242  static bool classof(const NullStmt *) { return true; }
243
244  // Iterators
245  virtual child_iterator child_begin();
246  virtual child_iterator child_end();
247
248  virtual void EmitImpl(llvm::Serializer& S) const;
249  static NullStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
250};
251
252/// CompoundStmt - This represents a group of statements like { stmt stmt }.
253///
254class CompoundStmt : public Stmt {
255  llvm::SmallVector<Stmt*, 16> Body;
256  SourceLocation LBracLoc, RBracLoc;
257public:
258  CompoundStmt(Stmt **StmtStart, unsigned NumStmts,
259               SourceLocation LB, SourceLocation RB)
260    : Stmt(CompoundStmtClass), Body(StmtStart, StmtStart+NumStmts),
261      LBracLoc(LB), RBracLoc(RB) {}
262
263  bool body_empty() const { return Body.empty(); }
264
265  typedef llvm::SmallVector<Stmt*, 16>::iterator body_iterator;
266  body_iterator body_begin() { return Body.begin(); }
267  body_iterator body_end() { return Body.end(); }
268  Stmt *body_back() { return Body.back(); }
269
270  typedef llvm::SmallVector<Stmt*, 16>::const_iterator const_body_iterator;
271  const_body_iterator body_begin() const { return Body.begin(); }
272  const_body_iterator body_end() const { return Body.end(); }
273  const Stmt *body_back() const { return Body.back(); }
274
275  typedef llvm::SmallVector<Stmt*, 16>::reverse_iterator reverse_body_iterator;
276  reverse_body_iterator body_rbegin() { return Body.rbegin(); }
277  reverse_body_iterator body_rend() { return Body.rend(); }
278
279  typedef llvm::SmallVector<Stmt*, 16>::const_reverse_iterator
280    const_reverse_body_iterator;
281  const_reverse_body_iterator body_rbegin() const { return Body.rbegin(); }
282  const_reverse_body_iterator body_rend() const { return Body.rend(); }
283
284  void push_back(Stmt *S) { Body.push_back(S); }
285
286  virtual SourceRange getSourceRange() const {
287    return SourceRange(LBracLoc, RBracLoc);
288  }
289
290  SourceLocation getLBracLoc() const { return LBracLoc; }
291  SourceLocation getRBracLoc() const { return RBracLoc; }
292
293  static bool classof(const Stmt *T) {
294    return T->getStmtClass() == CompoundStmtClass;
295  }
296  static bool classof(const CompoundStmt *) { return true; }
297
298  // Iterators
299  virtual child_iterator child_begin();
300  virtual child_iterator child_end();
301
302  virtual void EmitImpl(llvm::Serializer& S) const;
303  static CompoundStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
304};
305
306// SwitchCase is the base class for CaseStmt and DefaultStmt,
307class SwitchCase : public Stmt {
308protected:
309  // A pointer to the following CaseStmt or DefaultStmt class,
310  // used by SwitchStmt.
311  SwitchCase *NextSwitchCase;
312
313  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
314
315public:
316  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
317
318  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
319
320  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
321
322  virtual Stmt* v_getSubStmt() = 0;
323  Stmt *getSubStmt() { return v_getSubStmt(); }
324
325  virtual SourceRange getSourceRange() const { return SourceRange(); }
326
327  static bool classof(const Stmt *T) {
328    return T->getStmtClass() == CaseStmtClass ||
329    T->getStmtClass() == DefaultStmtClass;
330  }
331  static bool classof(const SwitchCase *) { return true; }
332};
333
334class CaseStmt : public SwitchCase {
335  enum { SUBSTMT, LHS, RHS, END_EXPR };
336  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
337                             // GNU "case 1 ... 4" extension
338  SourceLocation CaseLoc;
339public:
340  CaseStmt(Expr *lhs, Expr *rhs, Stmt *substmt, SourceLocation caseLoc)
341    : SwitchCase(CaseStmtClass) {
342    SubExprs[SUBSTMT] = substmt;
343    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
344    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
345    CaseLoc = caseLoc;
346  }
347
348  SourceLocation getCaseLoc() const { return CaseLoc; }
349
350  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
351  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
352  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
353  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
354  const Expr *getLHS() const {
355    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
356  }
357  const Expr *getRHS() const {
358    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
359  }
360  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
361
362  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
363  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
364  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
365
366
367  virtual SourceRange getSourceRange() const {
368    return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd());
369  }
370  static bool classof(const Stmt *T) {
371    return T->getStmtClass() == CaseStmtClass;
372  }
373  static bool classof(const CaseStmt *) { return true; }
374
375  // Iterators
376  virtual child_iterator child_begin();
377  virtual child_iterator child_end();
378
379  virtual void EmitImpl(llvm::Serializer& S) const;
380  static CaseStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
381};
382
383class DefaultStmt : public SwitchCase {
384  Stmt* SubStmt;
385  SourceLocation DefaultLoc;
386public:
387  DefaultStmt(SourceLocation DL, Stmt *substmt) :
388    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {}
389
390  Stmt *getSubStmt() { return SubStmt; }
391  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
392  const Stmt *getSubStmt() const { return SubStmt; }
393
394  SourceLocation getDefaultLoc() const { return DefaultLoc; }
395
396  virtual SourceRange getSourceRange() const {
397    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
398  }
399  static bool classof(const Stmt *T) {
400    return T->getStmtClass() == DefaultStmtClass;
401  }
402  static bool classof(const DefaultStmt *) { return true; }
403
404  // Iterators
405  virtual child_iterator child_begin();
406  virtual child_iterator child_end();
407
408  virtual void EmitImpl(llvm::Serializer& S) const;
409  static DefaultStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
410};
411
412class LabelStmt : public Stmt {
413  IdentifierInfo *Label;
414  Stmt *SubStmt;
415  SourceLocation IdentLoc;
416public:
417  LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
418    : Stmt(LabelStmtClass), Label(label),
419      SubStmt(substmt), IdentLoc(IL) {}
420
421  SourceLocation getIdentLoc() const { return IdentLoc; }
422  IdentifierInfo *getID() const { return Label; }
423  const char *getName() const;
424  Stmt *getSubStmt() { return SubStmt; }
425  const Stmt *getSubStmt() const { return SubStmt; }
426
427  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
428  void setSubStmt(Stmt *SS) { SubStmt = SS; }
429
430  virtual SourceRange getSourceRange() const {
431    return SourceRange(IdentLoc, SubStmt->getLocEnd());
432  }
433  static bool classof(const Stmt *T) {
434    return T->getStmtClass() == LabelStmtClass;
435  }
436  static bool classof(const LabelStmt *) { return true; }
437
438  // Iterators
439  virtual child_iterator child_begin();
440  virtual child_iterator child_end();
441
442  virtual void EmitImpl(llvm::Serializer& S) const;
443  static LabelStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
444};
445
446
447/// IfStmt - This represents an if/then/else.
448///
449class IfStmt : public Stmt {
450  enum { COND, THEN, ELSE, END_EXPR };
451  Stmt* SubExprs[END_EXPR];
452  SourceLocation IfLoc;
453public:
454  IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0)
455    : Stmt(IfStmtClass)  {
456    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
457    SubExprs[THEN] = then;
458    SubExprs[ELSE] = elsev;
459    IfLoc = IL;
460  }
461
462  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
463  const Stmt *getThen() const { return SubExprs[THEN]; }
464  const Stmt *getElse() const { return SubExprs[ELSE]; }
465
466  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
467  Stmt *getThen() { return SubExprs[THEN]; }
468  Stmt *getElse() { return SubExprs[ELSE]; }
469
470  virtual SourceRange getSourceRange() const {
471    if (SubExprs[ELSE])
472      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
473    else
474      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
475  }
476
477  static bool classof(const Stmt *T) {
478    return T->getStmtClass() == IfStmtClass;
479  }
480  static bool classof(const IfStmt *) { return true; }
481
482  // Iterators
483  virtual child_iterator child_begin();
484  virtual child_iterator child_end();
485
486  virtual void EmitImpl(llvm::Serializer& S) const;
487  static IfStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
488};
489
490/// SwitchStmt - This represents a 'switch' stmt.
491///
492class SwitchStmt : public Stmt {
493  enum { COND, BODY, END_EXPR };
494  Stmt* SubExprs[END_EXPR];
495  // This points to a linked list of case and default statements.
496  SwitchCase *FirstCase;
497  SourceLocation SwitchLoc;
498public:
499  SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) {
500      SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
501      SubExprs[BODY] = NULL;
502    }
503
504  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
505  const Stmt *getBody() const { return SubExprs[BODY]; }
506  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
507
508  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
509  Stmt *getBody() { return SubExprs[BODY]; }
510  SwitchCase *getSwitchCaseList() { return FirstCase; }
511
512  void setBody(Stmt *S, SourceLocation SL) {
513    SubExprs[BODY] = S;
514    SwitchLoc = SL;
515  }
516  void addSwitchCase(SwitchCase *SC) {
517    if (FirstCase)
518      SC->setNextSwitchCase(FirstCase);
519
520    FirstCase = SC;
521  }
522  virtual SourceRange getSourceRange() const {
523    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
524  }
525  static bool classof(const Stmt *T) {
526    return T->getStmtClass() == SwitchStmtClass;
527  }
528  static bool classof(const SwitchStmt *) { return true; }
529
530  // Iterators
531  virtual child_iterator child_begin();
532  virtual child_iterator child_end();
533
534  virtual void EmitImpl(llvm::Serializer& S) const;
535  static SwitchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
536};
537
538
539/// WhileStmt - This represents a 'while' stmt.
540///
541class WhileStmt : public Stmt {
542  enum { COND, BODY, END_EXPR };
543  Stmt* SubExprs[END_EXPR];
544  SourceLocation WhileLoc;
545public:
546  WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) {
547    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
548    SubExprs[BODY] = body;
549    WhileLoc = WL;
550  }
551
552  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
553  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
554  Stmt *getBody() { return SubExprs[BODY]; }
555  const Stmt *getBody() const { return SubExprs[BODY]; }
556
557  virtual SourceRange getSourceRange() const {
558    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
559  }
560  static bool classof(const Stmt *T) {
561    return T->getStmtClass() == WhileStmtClass;
562  }
563  static bool classof(const WhileStmt *) { return true; }
564
565  // Iterators
566  virtual child_iterator child_begin();
567  virtual child_iterator child_end();
568
569  virtual void EmitImpl(llvm::Serializer& S) const;
570  static WhileStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
571};
572
573/// DoStmt - This represents a 'do/while' stmt.
574///
575class DoStmt : public Stmt {
576  enum { COND, BODY, END_EXPR };
577  Stmt* SubExprs[END_EXPR];
578  SourceLocation DoLoc;
579public:
580  DoStmt(Stmt *body, Expr *cond, SourceLocation DL)
581    : Stmt(DoStmtClass), DoLoc(DL) {
582    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
583    SubExprs[BODY] = body;
584    DoLoc = DL;
585  }
586
587  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
588  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
589  Stmt *getBody() { return SubExprs[BODY]; }
590  const Stmt *getBody() const { return SubExprs[BODY]; }
591
592  virtual SourceRange getSourceRange() const {
593    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
594  }
595  static bool classof(const Stmt *T) {
596    return T->getStmtClass() == DoStmtClass;
597  }
598  static bool classof(const DoStmt *) { return true; }
599
600  // Iterators
601  virtual child_iterator child_begin();
602  virtual child_iterator child_end();
603
604  virtual void EmitImpl(llvm::Serializer& S) const;
605  static DoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
606};
607
608
609/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
610/// the init/cond/inc parts of the ForStmt will be null if they were not
611/// specified in the source.
612///
613class ForStmt : public Stmt {
614  enum { INIT, COND, INC, BODY, END_EXPR };
615  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
616  SourceLocation ForLoc;
617public:
618  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL)
619    : Stmt(ForStmtClass) {
620    SubExprs[INIT] = Init;
621    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
622    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
623    SubExprs[BODY] = Body;
624    ForLoc = FL;
625  }
626
627  Stmt *getInit() { return SubExprs[INIT]; }
628  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
629  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
630  Stmt *getBody() { return SubExprs[BODY]; }
631
632  const Stmt *getInit() const { return SubExprs[INIT]; }
633  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
634  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
635  const Stmt *getBody() const { return SubExprs[BODY]; }
636
637  virtual SourceRange getSourceRange() const {
638    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
639  }
640  static bool classof(const Stmt *T) {
641    return T->getStmtClass() == ForStmtClass;
642  }
643  static bool classof(const ForStmt *) { return true; }
644
645  // Iterators
646  virtual child_iterator child_begin();
647  virtual child_iterator child_end();
648
649  virtual void EmitImpl(llvm::Serializer& S) const;
650  static ForStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
651};
652
653/// GotoStmt - This represents a direct goto.
654///
655class GotoStmt : public Stmt {
656  LabelStmt *Label;
657  SourceLocation GotoLoc;
658  SourceLocation LabelLoc;
659public:
660  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
661    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
662
663  LabelStmt *getLabel() const { return Label; }
664
665  virtual SourceRange getSourceRange() const {
666    return SourceRange(GotoLoc, LabelLoc);
667  }
668  static bool classof(const Stmt *T) {
669    return T->getStmtClass() == GotoStmtClass;
670  }
671  static bool classof(const GotoStmt *) { return true; }
672
673  // Iterators
674  virtual child_iterator child_begin();
675  virtual child_iterator child_end();
676
677  virtual void EmitImpl(llvm::Serializer& S) const;
678  static GotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
679};
680
681/// IndirectGotoStmt - This represents an indirect goto.
682///
683class IndirectGotoStmt : public Stmt {
684  Stmt *Target;
685  // FIXME: Add location information (e.g. SourceLocation objects).
686  //        When doing so, update the serialization routines.
687public:
688  IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass),
689                                   Target((Stmt*)target){}
690
691  Expr *getTarget();
692  const Expr *getTarget() const;
693
694  virtual SourceRange getSourceRange() const { return SourceRange(); }
695
696  static bool classof(const Stmt *T) {
697    return T->getStmtClass() == IndirectGotoStmtClass;
698  }
699  static bool classof(const IndirectGotoStmt *) { return true; }
700
701  // Iterators
702  virtual child_iterator child_begin();
703  virtual child_iterator child_end();
704
705  virtual void EmitImpl(llvm::Serializer& S) const;
706  static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
707};
708
709
710/// ContinueStmt - This represents a continue.
711///
712class ContinueStmt : public Stmt {
713  SourceLocation ContinueLoc;
714public:
715  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
716
717  virtual SourceRange getSourceRange() const {
718    return SourceRange(ContinueLoc);
719  }
720  static bool classof(const Stmt *T) {
721    return T->getStmtClass() == ContinueStmtClass;
722  }
723  static bool classof(const ContinueStmt *) { return true; }
724
725  // Iterators
726  virtual child_iterator child_begin();
727  virtual child_iterator child_end();
728
729  virtual void EmitImpl(llvm::Serializer& S) const;
730  static ContinueStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
731};
732
733/// BreakStmt - This represents a break.
734///
735class BreakStmt : public Stmt {
736  SourceLocation BreakLoc;
737public:
738  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
739
740  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
741
742  static bool classof(const Stmt *T) {
743    return T->getStmtClass() == BreakStmtClass;
744  }
745  static bool classof(const BreakStmt *) { return true; }
746
747  // Iterators
748  virtual child_iterator child_begin();
749  virtual child_iterator child_end();
750
751  virtual void EmitImpl(llvm::Serializer& S) const;
752  static BreakStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
753};
754
755
756/// ReturnStmt - This represents a return, optionally of an expression:
757///   return;
758///   return 4;
759///
760/// Note that GCC allows return with no argument in a function declared to
761/// return a value, and it allows returning a value in functions declared to
762/// return void.  We explicitly model this in the AST, which means you can't
763/// depend on the return type of the function and the presence of an argument.
764///
765class ReturnStmt : public Stmt {
766  Stmt *RetExpr;
767  SourceLocation RetLoc;
768public:
769  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
770    RetExpr((Stmt*) E), RetLoc(RL) {}
771
772  const Expr *getRetValue() const;
773  Expr *getRetValue();
774
775  virtual SourceRange getSourceRange() const;
776
777  static bool classof(const Stmt *T) {
778    return T->getStmtClass() == ReturnStmtClass;
779  }
780  static bool classof(const ReturnStmt *) { return true; }
781
782  // Iterators
783  virtual child_iterator child_begin();
784  virtual child_iterator child_end();
785
786  virtual void EmitImpl(llvm::Serializer& S) const;
787  static ReturnStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
788};
789
790/// AsmStmt - This represents a GNU inline-assembly statement extension.
791///
792class AsmStmt : public Stmt {
793  SourceLocation AsmLoc, RParenLoc;
794  StringLiteral *AsmStr;
795
796  bool IsSimple;
797  bool IsVolatile;
798
799  unsigned NumOutputs;
800  unsigned NumInputs;
801
802  llvm::SmallVector<std::string, 4> Names;
803  llvm::SmallVector<StringLiteral*, 4> Constraints;
804  llvm::SmallVector<Expr*, 4> Exprs;
805
806  llvm::SmallVector<StringLiteral*, 4> Clobbers;
807public:
808  AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
809          unsigned numoutputs, unsigned numinputs,
810          std::string *names, StringLiteral **constraints,
811          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
812          StringLiteral **clobbers, SourceLocation rparenloc);
813
814  bool isVolatile() const { return IsVolatile; }
815  bool isSimple() const { return IsSimple; }
816
817  unsigned getNumOutputs() const { return NumOutputs; }
818  const std::string &getOutputName(unsigned i) const
819    { return Names[i]; }
820  const StringLiteral *getOutputConstraint(unsigned i) const
821  { return Constraints[i]; }
822  StringLiteral *getOutputConstraint(unsigned i)
823    { return Constraints[i]; }
824  const Expr *getOutputExpr(unsigned i) const { return Exprs[i]; }
825  Expr *getOutputExpr(unsigned i) { return Exprs[i]; }
826
827  unsigned getNumInputs() const { return NumInputs; }
828  const std::string &getInputName(unsigned i) const
829    { return Names[i + NumOutputs]; }
830  StringLiteral *getInputConstraint(unsigned i)
831    { return Constraints[i + NumOutputs]; }
832  const StringLiteral *getInputConstraint(unsigned i) const
833    { return Constraints[i + NumOutputs]; }
834  Expr *getInputExpr(unsigned i) { return Exprs[i + NumOutputs]; }
835  const Expr *getInputExpr(unsigned i) const { return Exprs[i + NumOutputs]; }
836
837  const StringLiteral *getAsmString() const { return AsmStr; }
838  StringLiteral *getAsmString() { return AsmStr; }
839
840  unsigned getNumClobbers() const { return Clobbers.size(); }
841  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
842  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
843
844  virtual SourceRange getSourceRange() const {
845    return SourceRange(AsmLoc, RParenLoc);
846  }
847
848  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
849  static bool classof(const AsmStmt *) { return true; }
850
851  // Input expr iterators.
852
853  typedef Expr* const * inputs_iterator;
854  typedef const Expr* const* const_inputs_iterator;
855
856  inputs_iterator begin_inputs() { return &Exprs[0] + NumOutputs; }
857  inputs_iterator end_inputs() { return begin_inputs() + NumInputs; }
858
859  const_inputs_iterator begin_inputs() const { return &Exprs[0] + NumOutputs; }
860  const_inputs_iterator end_inputs() const { return begin_inputs() + NumInputs;}
861
862  // Output expr iterators.
863
864  typedef Expr* const * outputs_iterator;
865  typedef const Expr* const* const_outputs_iterator;
866
867  outputs_iterator begin_outputs() { return &Exprs[0]; }
868  outputs_iterator end_outputs() { return begin_outputs() + NumOutputs; }
869
870  const_outputs_iterator begin_outputs() const { return &Exprs[0]; }
871  const_outputs_iterator end_outputs() const {
872    return begin_outputs() + NumOutputs;
873  }
874
875  // Child iterators
876
877  virtual child_iterator child_begin();
878  virtual child_iterator child_end();
879
880  virtual void EmitImpl(llvm::Serializer& S) const;
881  static AsmStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
882};
883
884/// ObjCForCollectionStmt - This represents Objective-c's collection statement;
885/// represented as 'for (element 'in' collection-expression)' stmt.
886///
887class ObjCForCollectionStmt : public Stmt {
888  enum { ELEM, COLLECTION, BODY, END_EXPR };
889  Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
890  SourceLocation ForLoc;
891  SourceLocation RParenLoc;
892public:
893  ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
894                        SourceLocation FCL, SourceLocation RPL);
895
896  Stmt *getElement() { return SubExprs[ELEM]; }
897  Expr *getCollection() {
898    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
899  }
900  Stmt *getBody() { return SubExprs[BODY]; }
901
902  const Stmt *getElement() const { return SubExprs[ELEM]; }
903  const Expr *getCollection() const {
904    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
905  }
906  const Stmt *getBody() const { return SubExprs[BODY]; }
907
908  SourceLocation getRParenLoc() const { return RParenLoc; }
909
910  virtual SourceRange getSourceRange() const {
911    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
912  }
913  static bool classof(const Stmt *T) {
914    return T->getStmtClass() == ObjCForCollectionStmtClass;
915  }
916  static bool classof(const ObjCForCollectionStmt *) { return true; }
917
918  // Iterators
919  virtual child_iterator child_begin();
920  virtual child_iterator child_end();
921
922  virtual void EmitImpl(llvm::Serializer& S) const;
923  static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
924};
925
926/// ObjCAtCatchStmt - This represents objective-c's @catch statement.
927class ObjCAtCatchStmt : public Stmt {
928private:
929  enum { SELECTOR, BODY, NEXT_CATCH, END_EXPR };
930  Stmt *SubExprs[END_EXPR];
931  SourceLocation AtCatchLoc, RParenLoc;
932
933  // Used by deserialization.
934  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc)
935  : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {}
936
937public:
938  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
939                  Stmt *catchVarStmtDecl, Stmt *atCatchStmt, Stmt *atCatchList);
940
941  const Stmt *getCatchBody() const { return SubExprs[BODY]; }
942  Stmt *getCatchBody() { return SubExprs[BODY]; }
943
944  const ObjCAtCatchStmt *getNextCatchStmt() const {
945    return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
946  }
947  ObjCAtCatchStmt *getNextCatchStmt() {
948    return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
949  }
950
951  const Stmt *getCatchParamStmt() const { return SubExprs[SELECTOR]; }
952  Stmt *getCatchParamStmt() { return SubExprs[SELECTOR]; }
953
954  SourceLocation getRParenLoc() const { return RParenLoc; }
955
956  virtual SourceRange getSourceRange() const {
957    return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd());
958  }
959
960  bool hasEllipsis() const { return getCatchParamStmt() == 0; }
961
962  static bool classof(const Stmt *T) {
963    return T->getStmtClass() == ObjCAtCatchStmtClass;
964  }
965  static bool classof(const ObjCAtCatchStmt *) { return true; }
966
967  virtual child_iterator child_begin();
968  virtual child_iterator child_end();
969
970  virtual void EmitImpl(llvm::Serializer& S) const;
971  static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
972};
973
974/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement
975class ObjCAtFinallyStmt : public Stmt {
976  Stmt *AtFinallyStmt;
977  SourceLocation AtFinallyLoc;
978public:
979  ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
980  : Stmt(ObjCAtFinallyStmtClass),
981    AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
982
983  const Stmt *getFinallyBody () const { return AtFinallyStmt; }
984  Stmt *getFinallyBody () { return AtFinallyStmt; }
985
986  virtual SourceRange getSourceRange() const {
987    return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
988  }
989
990  static bool classof(const Stmt *T) {
991    return T->getStmtClass() == ObjCAtFinallyStmtClass;
992  }
993  static bool classof(const ObjCAtFinallyStmt *) { return true; }
994
995  virtual child_iterator child_begin();
996  virtual child_iterator child_end();
997
998  virtual void EmitImpl(llvm::Serializer& S) const;
999  static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1000};
1001
1002/// ObjCAtTryStmt - This represent objective-c's over-all
1003/// @try ... @catch ... @finally statement.
1004class ObjCAtTryStmt : public Stmt {
1005private:
1006  enum { TRY, CATCH, FINALLY, END_EXPR };
1007  Stmt* SubStmts[END_EXPR];
1008
1009  SourceLocation AtTryLoc;
1010public:
1011  ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
1012                Stmt *atCatchStmt,
1013                Stmt *atFinallyStmt)
1014  : Stmt(ObjCAtTryStmtClass) {
1015      SubStmts[TRY] = atTryStmt;
1016      SubStmts[CATCH] = atCatchStmt;
1017      SubStmts[FINALLY] = atFinallyStmt;
1018      AtTryLoc = atTryLoc;
1019    }
1020
1021  const Stmt *getTryBody() const { return SubStmts[TRY]; }
1022  Stmt *getTryBody() { return SubStmts[TRY]; }
1023  const ObjCAtCatchStmt *getCatchStmts() const {
1024    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1025  }
1026  ObjCAtCatchStmt *getCatchStmts() {
1027    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
1028  }
1029  const ObjCAtFinallyStmt *getFinallyStmt() const {
1030    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1031  }
1032  ObjCAtFinallyStmt *getFinallyStmt() {
1033    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
1034  }
1035  virtual SourceRange getSourceRange() const {
1036    return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd());
1037  }
1038
1039  static bool classof(const Stmt *T) {
1040    return T->getStmtClass() == ObjCAtTryStmtClass;
1041  }
1042  static bool classof(const ObjCAtTryStmt *) { return true; }
1043
1044  virtual child_iterator child_begin();
1045  virtual child_iterator child_end();
1046
1047  virtual void EmitImpl(llvm::Serializer& S) const;
1048  static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1049};
1050
1051/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement.
1052/// Example: @synchronized (sem) {
1053///             do-something;
1054///          }
1055///
1056class ObjCAtSynchronizedStmt : public Stmt {
1057private:
1058  enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
1059  Stmt* SubStmts[END_EXPR];
1060  SourceLocation AtSynchronizedLoc;
1061
1062public:
1063  ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
1064                         Stmt *synchBody)
1065  : Stmt(ObjCAtSynchronizedStmtClass) {
1066      SubStmts[SYNC_EXPR] = synchExpr;
1067      SubStmts[SYNC_BODY] = synchBody;
1068      AtSynchronizedLoc = atSynchronizedLoc;
1069    }
1070
1071  const CompoundStmt *getSynchBody() const {
1072    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1073  }
1074  CompoundStmt *getSynchBody() {
1075    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
1076  }
1077
1078  const Expr *getSynchExpr() const {
1079    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1080  }
1081  Expr *getSynchExpr() {
1082    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
1083  }
1084
1085  virtual SourceRange getSourceRange() const {
1086    return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd());
1087  }
1088
1089  static bool classof(const Stmt *T) {
1090    return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
1091  }
1092  static bool classof(const ObjCAtSynchronizedStmt *) { return true; }
1093
1094  virtual child_iterator child_begin();
1095  virtual child_iterator child_end();
1096
1097  virtual void EmitImpl(llvm::Serializer& S) const;
1098  static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D,
1099                                            ASTContext& C);
1100};
1101
1102/// ObjCAtThrowStmt - This represents objective-c's @throw statement.
1103class ObjCAtThrowStmt : public Stmt {
1104  Stmt *Throw;
1105  SourceLocation AtThrowLoc;
1106public:
1107  ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
1108  : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
1109    AtThrowLoc = atThrowLoc;
1110  }
1111
1112  const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
1113  Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
1114
1115  virtual SourceRange getSourceRange() const {
1116    if (Throw)
1117      return SourceRange(AtThrowLoc, Throw->getLocEnd());
1118    else
1119      return SourceRange(AtThrowLoc);
1120  }
1121
1122  static bool classof(const Stmt *T) {
1123    return T->getStmtClass() == ObjCAtThrowStmtClass;
1124  }
1125  static bool classof(const ObjCAtThrowStmt *) { return true; }
1126
1127  virtual child_iterator child_begin();
1128  virtual child_iterator child_end();
1129
1130  virtual void EmitImpl(llvm::Serializer& S) const;
1131  static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D, ASTContext& C);
1132};
1133
1134}  // end namespace clang
1135
1136#endif
1137