Stmt.h revision 335a2d4122e41343fe11a775889b8bec5b14be60
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  virtual SourceRange getSourceRange() const {
291    return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd());
292  }
293  static bool classof(const Stmt *T) {
294    return T->getStmtClass() == CaseStmtClass;
295  }
296  static bool classof(const CaseStmt *) { 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 CaseStmt* CreateImpl(llvm::Deserializer& D);
304};
305
306class DefaultStmt : public SwitchCase {
307  Stmt* SubStmt;
308  SourceLocation DefaultLoc;
309public:
310  DefaultStmt(SourceLocation DL, Stmt *substmt) :
311    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {}
312
313  Stmt *getSubStmt() { return SubStmt; }
314  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
315  const Stmt *getSubStmt() const { return SubStmt; }
316
317  SourceLocation getDefaultLoc() const { return DefaultLoc; }
318
319  virtual SourceRange getSourceRange() const {
320    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
321  }
322  static bool classof(const Stmt *T) {
323    return T->getStmtClass() == DefaultStmtClass;
324  }
325  static bool classof(const DefaultStmt *) { return true; }
326
327  // Iterators
328  virtual child_iterator child_begin();
329  virtual child_iterator child_end();
330
331  virtual void EmitImpl(llvm::Serializer& S) const;
332  static DefaultStmt* CreateImpl(llvm::Deserializer& D);
333};
334
335class LabelStmt : public Stmt {
336  IdentifierInfo *Label;
337  Stmt *SubStmt;
338  SourceLocation IdentLoc;
339public:
340  LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
341    : Stmt(LabelStmtClass), Label(label),
342      SubStmt(substmt), IdentLoc(IL) {}
343
344  SourceLocation getIdentLoc() const { return IdentLoc; }
345  IdentifierInfo *getID() const { return Label; }
346  const char *getName() const;
347  Stmt *getSubStmt() { return SubStmt; }
348  const Stmt *getSubStmt() const { return SubStmt; }
349
350  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
351  void setSubStmt(Stmt *SS) { SubStmt = SS; }
352
353  virtual SourceRange getSourceRange() const {
354    return SourceRange(IdentLoc, SubStmt->getLocEnd());
355  }
356  static bool classof(const Stmt *T) {
357    return T->getStmtClass() == LabelStmtClass;
358  }
359  static bool classof(const LabelStmt *) { return true; }
360
361  // Iterators
362  virtual child_iterator child_begin();
363  virtual child_iterator child_end();
364
365  virtual void EmitImpl(llvm::Serializer& S) const;
366  static LabelStmt* CreateImpl(llvm::Deserializer& D);
367};
368
369
370/// IfStmt - This represents an if/then/else.
371///
372class IfStmt : public Stmt {
373  enum { COND, THEN, ELSE, END_EXPR };
374  Stmt* SubExprs[END_EXPR];
375  SourceLocation IfLoc;
376public:
377  IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0)
378    : Stmt(IfStmtClass)  {
379    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
380    SubExprs[THEN] = then;
381    SubExprs[ELSE] = elsev;
382    IfLoc = IL;
383  }
384
385  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
386  const Stmt *getThen() const { return SubExprs[THEN]; }
387  const Stmt *getElse() const { return SubExprs[ELSE]; }
388
389  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
390  Stmt *getThen() { return SubExprs[THEN]; }
391  Stmt *getElse() { return SubExprs[ELSE]; }
392
393  virtual SourceRange getSourceRange() const {
394    if (SubExprs[ELSE])
395      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
396    else
397      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
398  }
399
400  static bool classof(const Stmt *T) {
401    return T->getStmtClass() == IfStmtClass;
402  }
403  static bool classof(const IfStmt *) { return true; }
404
405  // Iterators
406  virtual child_iterator child_begin();
407  virtual child_iterator child_end();
408
409  virtual void EmitImpl(llvm::Serializer& S) const;
410  static IfStmt* CreateImpl(llvm::Deserializer& D);
411};
412
413/// SwitchStmt - This represents a 'switch' stmt.
414///
415class SwitchStmt : public Stmt {
416  enum { COND, BODY, END_EXPR };
417  Stmt* SubExprs[END_EXPR];
418  // This points to a linked list of case and default statements.
419  SwitchCase *FirstCase;
420  SourceLocation SwitchLoc;
421public:
422  SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) {
423      SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
424      SubExprs[BODY] = NULL;
425    }
426
427  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
428  const Stmt *getBody() const { return SubExprs[BODY]; }
429  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
430
431  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
432  Stmt *getBody() { return SubExprs[BODY]; }
433  SwitchCase *getSwitchCaseList() { return FirstCase; }
434
435  void setBody(Stmt *S, SourceLocation SL) {
436    SubExprs[BODY] = S;
437    SwitchLoc = SL;
438  }
439  void addSwitchCase(SwitchCase *SC) {
440    if (FirstCase)
441      SC->setNextSwitchCase(FirstCase);
442
443    FirstCase = SC;
444  }
445  virtual SourceRange getSourceRange() const {
446    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
447  }
448  static bool classof(const Stmt *T) {
449    return T->getStmtClass() == SwitchStmtClass;
450  }
451  static bool classof(const SwitchStmt *) { return true; }
452
453  // Iterators
454  virtual child_iterator child_begin();
455  virtual child_iterator child_end();
456
457  virtual void EmitImpl(llvm::Serializer& S) const;
458  static SwitchStmt* CreateImpl(llvm::Deserializer& D);
459};
460
461
462/// WhileStmt - This represents a 'while' stmt.
463///
464class WhileStmt : public Stmt {
465  enum { COND, BODY, END_EXPR };
466  Stmt* SubExprs[END_EXPR];
467  SourceLocation WhileLoc;
468public:
469  WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) {
470    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
471    SubExprs[BODY] = body;
472    WhileLoc = WL;
473  }
474
475  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
476  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
477  Stmt *getBody() { return SubExprs[BODY]; }
478  const Stmt *getBody() const { return SubExprs[BODY]; }
479
480  virtual SourceRange getSourceRange() const {
481    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
482  }
483  static bool classof(const Stmt *T) {
484    return T->getStmtClass() == WhileStmtClass;
485  }
486  static bool classof(const WhileStmt *) { return true; }
487
488  // Iterators
489  virtual child_iterator child_begin();
490  virtual child_iterator child_end();
491
492  virtual void EmitImpl(llvm::Serializer& S) const;
493  static WhileStmt* CreateImpl(llvm::Deserializer& D);
494};
495
496/// DoStmt - This represents a 'do/while' stmt.
497///
498class DoStmt : public Stmt {
499  enum { COND, BODY, END_EXPR };
500  Stmt* SubExprs[END_EXPR];
501  SourceLocation DoLoc;
502public:
503  DoStmt(Stmt *body, Expr *cond, SourceLocation DL)
504    : Stmt(DoStmtClass), DoLoc(DL) {
505    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
506    SubExprs[BODY] = body;
507    DoLoc = DL;
508  }
509
510  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
511  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
512  Stmt *getBody() { return SubExprs[BODY]; }
513  const Stmt *getBody() const { return SubExprs[BODY]; }
514
515  virtual SourceRange getSourceRange() const {
516    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
517  }
518  static bool classof(const Stmt *T) {
519    return T->getStmtClass() == DoStmtClass;
520  }
521  static bool classof(const DoStmt *) { return true; }
522
523  // Iterators
524  virtual child_iterator child_begin();
525  virtual child_iterator child_end();
526
527  virtual void EmitImpl(llvm::Serializer& S) const;
528  static DoStmt* CreateImpl(llvm::Deserializer& D);
529};
530
531
532/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
533/// the init/cond/inc parts of the ForStmt will be null if they were not
534/// specified in the source.
535///
536class ForStmt : public Stmt {
537  enum { INIT, COND, INC, BODY, END_EXPR };
538  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
539  SourceLocation ForLoc;
540public:
541  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL)
542    : Stmt(ForStmtClass) {
543    SubExprs[INIT] = Init;
544    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
545    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
546    SubExprs[BODY] = Body;
547    ForLoc = FL;
548  }
549
550  Stmt *getInit() { return SubExprs[INIT]; }
551  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
552  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
553  Stmt *getBody() { return SubExprs[BODY]; }
554
555  const Stmt *getInit() const { return SubExprs[INIT]; }
556  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
557  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
558  const Stmt *getBody() const { return SubExprs[BODY]; }
559
560  virtual SourceRange getSourceRange() const {
561    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
562  }
563  static bool classof(const Stmt *T) {
564    return T->getStmtClass() == ForStmtClass;
565  }
566  static bool classof(const ForStmt *) { return true; }
567
568  // Iterators
569  virtual child_iterator child_begin();
570  virtual child_iterator child_end();
571
572  virtual void EmitImpl(llvm::Serializer& S) const;
573  static ForStmt* CreateImpl(llvm::Deserializer& D);
574};
575
576/// ObjcForCollectionStmt - This represents Objective-c's collection statement;
577/// represented as 'for (element 'in' collection-expression)' stmt.
578///
579class ObjcForCollectionStmt : public Stmt {
580  enum { ELEM, COLLECTION, BODY, END_EXPR };
581  Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
582  SourceLocation ForLoc;
583public:
584  ObjcForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
585                        SourceLocation FCL)
586  : Stmt(ObjcForCollectionStmtClass) {
587    SubExprs[ELEM] = Elem;
588    SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
589    SubExprs[BODY] = Body;
590    ForLoc = FCL;
591  }
592
593  Stmt *getElement() { return SubExprs[ELEM]; }
594  Expr *getCollection() {
595    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
596  }
597  Stmt *getBody() { return SubExprs[BODY]; }
598
599  const Stmt *getElement() const { return SubExprs[ELEM]; }
600  const Expr *getCollection() const {
601    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
602  }
603  const Stmt *getBody() const { return SubExprs[BODY]; }
604
605  virtual SourceRange getSourceRange() const {
606    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
607  }
608  static bool classof(const Stmt *T) {
609    return T->getStmtClass() == ObjcForCollectionStmtClass;
610  }
611  static bool classof(const ObjcForCollectionStmt *) { return true; }
612
613  // Iterators
614  virtual child_iterator child_begin();
615  virtual child_iterator child_end();
616
617  virtual void EmitImpl(llvm::Serializer& S) const;
618  static ObjcForCollectionStmt* CreateImpl(llvm::Deserializer& D);
619};
620
621/// GotoStmt - This represents a direct goto.
622///
623class GotoStmt : public Stmt {
624  LabelStmt *Label;
625  SourceLocation GotoLoc;
626  SourceLocation LabelLoc;
627public:
628  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
629    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
630
631  LabelStmt *getLabel() const { return Label; }
632
633  virtual SourceRange getSourceRange() const {
634    return SourceRange(GotoLoc, LabelLoc);
635  }
636  static bool classof(const Stmt *T) {
637    return T->getStmtClass() == GotoStmtClass;
638  }
639  static bool classof(const GotoStmt *) { return true; }
640
641  // Iterators
642  virtual child_iterator child_begin();
643  virtual child_iterator child_end();
644
645  virtual void EmitImpl(llvm::Serializer& S) const;
646  static GotoStmt* CreateImpl(llvm::Deserializer& D);
647};
648
649/// IndirectGotoStmt - This represents an indirect goto.
650///
651class IndirectGotoStmt : public Stmt {
652  Expr *Target;
653  // FIXME: Add location information (e.g. SourceLocation objects).
654  //        When doing so, update the serialization routines.
655public:
656  IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass), Target(target){}
657
658  Expr *getTarget() { return Target; }
659  const Expr *getTarget() const { return Target; }
660
661  virtual SourceRange getSourceRange() const { return SourceRange(); }
662
663  static bool classof(const Stmt *T) {
664    return T->getStmtClass() == IndirectGotoStmtClass;
665  }
666  static bool classof(const IndirectGotoStmt *) { return true; }
667
668  // Iterators
669  virtual child_iterator child_begin();
670  virtual child_iterator child_end();
671
672  virtual void EmitImpl(llvm::Serializer& S) const;
673  static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D);
674};
675
676
677/// ContinueStmt - This represents a continue.
678///
679class ContinueStmt : public Stmt {
680  SourceLocation ContinueLoc;
681public:
682  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
683
684  virtual SourceRange getSourceRange() const {
685    return SourceRange(ContinueLoc);
686  }
687  static bool classof(const Stmt *T) {
688    return T->getStmtClass() == ContinueStmtClass;
689  }
690  static bool classof(const ContinueStmt *) { return true; }
691
692  // Iterators
693  virtual child_iterator child_begin();
694  virtual child_iterator child_end();
695
696  virtual void EmitImpl(llvm::Serializer& S) const;
697  static ContinueStmt* CreateImpl(llvm::Deserializer& D);
698};
699
700/// BreakStmt - This represents a break.
701///
702class BreakStmt : public Stmt {
703  SourceLocation BreakLoc;
704public:
705  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
706
707  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
708
709  static bool classof(const Stmt *T) {
710    return T->getStmtClass() == BreakStmtClass;
711  }
712  static bool classof(const BreakStmt *) { return true; }
713
714  // Iterators
715  virtual child_iterator child_begin();
716  virtual child_iterator child_end();
717
718  virtual void EmitImpl(llvm::Serializer& S) const;
719  static BreakStmt* CreateImpl(llvm::Deserializer& D);
720};
721
722
723/// ReturnStmt - This represents a return, optionally of an expression.
724///
725class ReturnStmt : public Stmt {
726  Expr *RetExpr;
727  SourceLocation RetLoc;
728public:
729  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
730    RetExpr(E), RetLoc(RL) {}
731
732  const Expr *getRetValue() const { return RetExpr; }
733  Expr *getRetValue() { return RetExpr; }
734
735  virtual SourceRange getSourceRange() const;
736
737  static bool classof(const Stmt *T) {
738    return T->getStmtClass() == ReturnStmtClass;
739  }
740  static bool classof(const ReturnStmt *) { return true; }
741
742  // Iterators
743  virtual child_iterator child_begin();
744  virtual child_iterator child_end();
745
746  virtual void EmitImpl(llvm::Serializer& S) const;
747  static ReturnStmt* CreateImpl(llvm::Deserializer& D);
748};
749
750/// AsmStmt - This represents a GNU inline-assembly statement extension.
751///
752class AsmStmt : public Stmt {
753  SourceLocation AsmLoc, RParenLoc;
754  StringLiteral *AsmStr;
755
756  bool IsVolatile;
757
758  unsigned NumOutputs;
759  unsigned NumInputs;
760
761  llvm::SmallVector<std::string, 4> Names;
762  llvm::SmallVector<StringLiteral*, 4> Constraints;
763  llvm::SmallVector<Expr*, 4> Exprs;
764
765  llvm::SmallVector<StringLiteral*, 4> Clobbers;
766public:
767  AsmStmt(SourceLocation asmloc,
768          bool isvolatile,
769          unsigned numoutputs,
770          unsigned numinputs,
771          std::string *names,
772          StringLiteral **constraints,
773          Expr **exprs,
774          StringLiteral *asmstr,
775          unsigned numclobbers,
776          StringLiteral **clobbers,
777          SourceLocation rparenloc);
778
779  bool isVolatile() const { return IsVolatile; }
780
781  unsigned getNumOutputs() const { return NumOutputs; }
782  const std::string &getOutputName(unsigned i) const
783    { return Names[i]; }
784  const StringLiteral *getOutputConstraint(unsigned i) const
785  { return Constraints[i]; }
786  StringLiteral *getOutputConstraint(unsigned i)
787    { return Constraints[i]; }
788  const Expr *getOutputExpr(unsigned i) const { return Exprs[i]; }
789  Expr *getOutputExpr(unsigned i) { return Exprs[i]; }
790
791  unsigned getNumInputs() const { return NumInputs; }
792  const std::string &getInputName(unsigned i) const
793    { return Names[i + NumOutputs]; }
794  StringLiteral *getInputConstraint(unsigned i)
795    { return Constraints[i + NumOutputs]; }
796  const StringLiteral *getInputConstraint(unsigned i) const
797    { return Constraints[i + NumOutputs]; }
798  Expr *getInputExpr(unsigned i) { return Exprs[i + NumOutputs]; }
799  const Expr *getInputExpr(unsigned i) const { return Exprs[i + NumOutputs]; }
800
801  const StringLiteral *getAsmString() const { return AsmStr; }
802  StringLiteral *getAsmString() { return AsmStr; }
803
804  unsigned getNumClobbers() const { return Clobbers.size(); }
805  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
806  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
807
808  virtual SourceRange getSourceRange() const {
809    return SourceRange(AsmLoc, RParenLoc);
810  }
811
812  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
813  static bool classof(const AsmStmt *) { return true; }
814
815  virtual child_iterator child_begin();
816  virtual child_iterator child_end();
817
818  virtual void EmitImpl(llvm::Serializer& S) const;
819  static AsmStmt* CreateImpl(llvm::Deserializer& D);
820};
821
822/// ObjcAtCatchStmt - This represents objective-c's @catch statement.
823class ObjcAtCatchStmt : public Stmt {
824private:
825  // Points to next @catch statement, or null
826  ObjcAtCatchStmt *NextAtCatchStmt;
827  enum { SELECTOR, BODY, END_EXPR };
828  Stmt *SubExprs[END_EXPR];
829  SourceLocation AtCatchLoc, RParenLoc;
830
831  // Used by deserialization.
832  ObjcAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc)
833  : Stmt(ObjcAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {}
834
835public:
836  ObjcAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
837                  Stmt *catchVarStmtDecl, Stmt *atCatchStmt, Stmt *atCatchList)
838  : Stmt(ObjcAtCatchStmtClass) {
839      SubExprs[SELECTOR] = catchVarStmtDecl;
840      SubExprs[BODY] = atCatchStmt;
841      if (!atCatchList)
842        NextAtCatchStmt = NULL;
843      else {
844        ObjcAtCatchStmt *AtCatchList =
845          static_cast<ObjcAtCatchStmt*>(atCatchList);
846        while (AtCatchList->NextAtCatchStmt)
847          AtCatchList = AtCatchList->NextAtCatchStmt;
848        AtCatchList->NextAtCatchStmt = this;
849      }
850      AtCatchLoc = atCatchLoc;
851      RParenLoc = rparenloc;
852    }
853
854  const Stmt *getCatchBody() const { return SubExprs[BODY]; }
855  Stmt *getCatchBody() { return SubExprs[BODY]; }
856  const ObjcAtCatchStmt *getNextCatchStmt() const { return NextAtCatchStmt; }
857  ObjcAtCatchStmt *getNextCatchStmt() { return NextAtCatchStmt; }
858  const Stmt *getCatchParamStmt() const { return SubExprs[SELECTOR]; }
859  Stmt *getCatchParamStmt() { return SubExprs[SELECTOR]; }
860
861  SourceLocation getRParenLoc() const { return RParenLoc; }
862
863  virtual SourceRange getSourceRange() const {
864    return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd());
865  }
866
867  static bool classof(const Stmt *T) {
868    return T->getStmtClass() == ObjcAtCatchStmtClass;
869  }
870  static bool classof(const ObjcAtCatchStmt *) { return true; }
871
872  virtual child_iterator child_begin();
873  virtual child_iterator child_end();
874
875  virtual void EmitImpl(llvm::Serializer& S) const;
876  static ObjcAtCatchStmt* CreateImpl(llvm::Deserializer& D);
877};
878
879/// ObjcAtFinallyStmt - This represent objective-c's @finally Statement
880class ObjcAtFinallyStmt : public Stmt {
881  Stmt *AtFinallyStmt;
882  SourceLocation AtFinallyLoc;
883public:
884  ObjcAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
885  : Stmt(ObjcAtFinallyStmtClass),
886    AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
887
888  const Stmt *getFinallyBody () const { return AtFinallyStmt; }
889  Stmt *getFinallyBody () { return AtFinallyStmt; }
890
891  virtual SourceRange getSourceRange() const {
892    return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
893  }
894
895  static bool classof(const Stmt *T) {
896    return T->getStmtClass() == ObjcAtFinallyStmtClass;
897  }
898  static bool classof(const ObjcAtFinallyStmt *) { return true; }
899
900  virtual child_iterator child_begin();
901  virtual child_iterator child_end();
902
903  virtual void EmitImpl(llvm::Serializer& S) const;
904  static ObjcAtFinallyStmt* CreateImpl(llvm::Deserializer& D);
905};
906
907/// ObjcAtTryStmt - This represent objective-c's over-all
908/// @try ... @catch ... @finally statement.
909class ObjcAtTryStmt : public Stmt {
910private:
911  enum { TRY, CATCH, FINALLY, END_EXPR };
912  Stmt* SubStmts[END_EXPR];
913
914  SourceLocation AtTryLoc;
915public:
916  ObjcAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
917                Stmt *atCatchStmt,
918                Stmt *atFinallyStmt)
919  : Stmt(ObjcAtTryStmtClass) {
920      SubStmts[TRY] = atTryStmt;
921      SubStmts[CATCH] = atCatchStmt;
922      SubStmts[FINALLY] = atFinallyStmt;
923      AtTryLoc = atTryLoc;
924    }
925
926  const Stmt *getTryBody() const { return SubStmts[TRY]; }
927  Stmt *getTryBody() { return SubStmts[TRY]; }
928  const ObjcAtCatchStmt *getCatchStmts() const {
929    return dyn_cast_or_null<ObjcAtCatchStmt>(SubStmts[CATCH]);
930  }
931  ObjcAtCatchStmt *getCatchStmts() {
932    return dyn_cast_or_null<ObjcAtCatchStmt>(SubStmts[CATCH]);
933  }
934  const ObjcAtFinallyStmt *getFinallyStmt() const {
935    return dyn_cast_or_null<ObjcAtFinallyStmt>(SubStmts[FINALLY]);
936  }
937  ObjcAtFinallyStmt *getFinallyStmt() {
938    return dyn_cast_or_null<ObjcAtFinallyStmt>(SubStmts[FINALLY]);
939  }
940  virtual SourceRange getSourceRange() const {
941    return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd());
942  }
943
944  static bool classof(const Stmt *T) {
945    return T->getStmtClass() == ObjcAtTryStmtClass;
946  }
947  static bool classof(const ObjcAtTryStmt *) { return true; }
948
949  virtual child_iterator child_begin();
950  virtual child_iterator child_end();
951
952  virtual void EmitImpl(llvm::Serializer& S) const;
953  static ObjcAtTryStmt* CreateImpl(llvm::Deserializer& D);
954};
955
956/// ObjcAtThrowStmt - This represents objective-c's @throw statement.
957class ObjcAtThrowStmt : public Stmt {
958  Stmt *Throw;
959  SourceLocation AtThrowLoc;
960public:
961  ObjcAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
962  : Stmt(ObjcAtThrowStmtClass), Throw(throwExpr) {
963    AtThrowLoc = atThrowLoc;
964  }
965
966  Expr *const getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
967
968  virtual SourceRange getSourceRange() const {
969    return SourceRange(AtThrowLoc, Throw->getLocEnd());
970  }
971
972  static bool classof(const Stmt *T) {
973    return T->getStmtClass() == ObjcAtThrowStmtClass;
974  }
975  static bool classof(const ObjcAtThrowStmt *) { return true; }
976
977  virtual child_iterator child_begin();
978  virtual child_iterator child_end();
979
980  virtual void EmitImpl(llvm::Serializer& S) const;
981  static ObjcAtThrowStmt* CreateImpl(llvm::Deserializer& D);
982};
983
984}  // end namespace clang
985
986#endif
987