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