Stmt.h revision be4b3332c2d17c03af6d015d9e8e2cc524806e6e
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/// ObjCForCollectionStmt - This represents Objective-c's collection statement;
582/// represented as 'for (element 'in' collection-expression)' stmt.
583///
584class ObjCForCollectionStmt : public Stmt {
585  enum { ELEM, COLLECTION, BODY, END_EXPR };
586  Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
587  SourceLocation ForLoc;
588  SourceLocation RParenLoc;
589public:
590  ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
591                        SourceLocation FCL, SourceLocation RPL);
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  SourceLocation getRParenLoc() const { return RParenLoc; }
606
607  virtual SourceRange getSourceRange() const {
608    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
609  }
610  static bool classof(const Stmt *T) {
611    return T->getStmtClass() == ObjCForCollectionStmtClass;
612  }
613  static bool classof(const ObjCForCollectionStmt *) { return true; }
614
615  // Iterators
616  virtual child_iterator child_begin();
617  virtual child_iterator child_end();
618
619  virtual void EmitImpl(llvm::Serializer& S) const;
620  static ObjCForCollectionStmt* CreateImpl(llvm::Deserializer& D);
621};
622
623/// GotoStmt - This represents a direct goto.
624///
625class GotoStmt : public Stmt {
626  LabelStmt *Label;
627  SourceLocation GotoLoc;
628  SourceLocation LabelLoc;
629public:
630  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
631    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
632
633  LabelStmt *getLabel() const { return Label; }
634
635  virtual SourceRange getSourceRange() const {
636    return SourceRange(GotoLoc, LabelLoc);
637  }
638  static bool classof(const Stmt *T) {
639    return T->getStmtClass() == GotoStmtClass;
640  }
641  static bool classof(const GotoStmt *) { return true; }
642
643  // Iterators
644  virtual child_iterator child_begin();
645  virtual child_iterator child_end();
646
647  virtual void EmitImpl(llvm::Serializer& S) const;
648  static GotoStmt* CreateImpl(llvm::Deserializer& D);
649};
650
651/// IndirectGotoStmt - This represents an indirect goto.
652///
653class IndirectGotoStmt : public Stmt {
654  Expr *Target;
655  // FIXME: Add location information (e.g. SourceLocation objects).
656  //        When doing so, update the serialization routines.
657public:
658  IndirectGotoStmt(Expr *target) : Stmt(IndirectGotoStmtClass), Target(target){}
659
660  Expr *getTarget() { return Target; }
661  const Expr *getTarget() const { return Target; }
662
663  virtual SourceRange getSourceRange() const { return SourceRange(); }
664
665  static bool classof(const Stmt *T) {
666    return T->getStmtClass() == IndirectGotoStmtClass;
667  }
668  static bool classof(const IndirectGotoStmt *) { return true; }
669
670  // Iterators
671  virtual child_iterator child_begin();
672  virtual child_iterator child_end();
673
674  virtual void EmitImpl(llvm::Serializer& S) const;
675  static IndirectGotoStmt* CreateImpl(llvm::Deserializer& D);
676};
677
678
679/// ContinueStmt - This represents a continue.
680///
681class ContinueStmt : public Stmt {
682  SourceLocation ContinueLoc;
683public:
684  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
685
686  virtual SourceRange getSourceRange() const {
687    return SourceRange(ContinueLoc);
688  }
689  static bool classof(const Stmt *T) {
690    return T->getStmtClass() == ContinueStmtClass;
691  }
692  static bool classof(const ContinueStmt *) { return true; }
693
694  // Iterators
695  virtual child_iterator child_begin();
696  virtual child_iterator child_end();
697
698  virtual void EmitImpl(llvm::Serializer& S) const;
699  static ContinueStmt* CreateImpl(llvm::Deserializer& D);
700};
701
702/// BreakStmt - This represents a break.
703///
704class BreakStmt : public Stmt {
705  SourceLocation BreakLoc;
706public:
707  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
708
709  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
710
711  static bool classof(const Stmt *T) {
712    return T->getStmtClass() == BreakStmtClass;
713  }
714  static bool classof(const BreakStmt *) { return true; }
715
716  // Iterators
717  virtual child_iterator child_begin();
718  virtual child_iterator child_end();
719
720  virtual void EmitImpl(llvm::Serializer& S) const;
721  static BreakStmt* CreateImpl(llvm::Deserializer& D);
722};
723
724
725/// ReturnStmt - This represents a return, optionally of an expression.
726///
727class ReturnStmt : public Stmt {
728  Expr *RetExpr;
729  SourceLocation RetLoc;
730public:
731  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
732    RetExpr(E), RetLoc(RL) {}
733
734  const Expr *getRetValue() const { return RetExpr; }
735  Expr *getRetValue() { return RetExpr; }
736
737  virtual SourceRange getSourceRange() const;
738
739  static bool classof(const Stmt *T) {
740    return T->getStmtClass() == ReturnStmtClass;
741  }
742  static bool classof(const ReturnStmt *) { return true; }
743
744  // Iterators
745  virtual child_iterator child_begin();
746  virtual child_iterator child_end();
747
748  virtual void EmitImpl(llvm::Serializer& S) const;
749  static ReturnStmt* CreateImpl(llvm::Deserializer& D);
750};
751
752/// AsmStmt - This represents a GNU inline-assembly statement extension.
753///
754class AsmStmt : public Stmt {
755  SourceLocation AsmLoc, RParenLoc;
756  StringLiteral *AsmStr;
757
758  bool IsVolatile;
759
760  unsigned NumOutputs;
761  unsigned NumInputs;
762
763  llvm::SmallVector<std::string, 4> Names;
764  llvm::SmallVector<StringLiteral*, 4> Constraints;
765  llvm::SmallVector<Expr*, 4> Exprs;
766
767  llvm::SmallVector<StringLiteral*, 4> Clobbers;
768public:
769  AsmStmt(SourceLocation asmloc,  bool isvolatile, unsigned numoutputs,
770          unsigned numinputs, std::string *names, StringLiteral **constraints,
771          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
772          StringLiteral **clobbers, SourceLocation rparenloc);
773
774  bool isVolatile() const { return IsVolatile; }
775
776  unsigned getNumOutputs() const { return NumOutputs; }
777  const std::string &getOutputName(unsigned i) const
778    { return Names[i]; }
779  const StringLiteral *getOutputConstraint(unsigned i) const
780  { return Constraints[i]; }
781  StringLiteral *getOutputConstraint(unsigned i)
782    { return Constraints[i]; }
783  const Expr *getOutputExpr(unsigned i) const { return Exprs[i]; }
784  Expr *getOutputExpr(unsigned i) { return Exprs[i]; }
785
786  unsigned getNumInputs() const { return NumInputs; }
787  const std::string &getInputName(unsigned i) const
788    { return Names[i + NumOutputs]; }
789  StringLiteral *getInputConstraint(unsigned i)
790    { return Constraints[i + NumOutputs]; }
791  const StringLiteral *getInputConstraint(unsigned i) const
792    { return Constraints[i + NumOutputs]; }
793  Expr *getInputExpr(unsigned i) { return Exprs[i + NumOutputs]; }
794  const Expr *getInputExpr(unsigned i) const { return Exprs[i + NumOutputs]; }
795
796  const StringLiteral *getAsmString() const { return AsmStr; }
797  StringLiteral *getAsmString() { return AsmStr; }
798
799  unsigned getNumClobbers() const { return Clobbers.size(); }
800  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
801  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
802
803  virtual SourceRange getSourceRange() const {
804    return SourceRange(AsmLoc, RParenLoc);
805  }
806
807  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
808  static bool classof(const AsmStmt *) { return true; }
809
810  virtual child_iterator child_begin();
811  virtual child_iterator child_end();
812
813  virtual void EmitImpl(llvm::Serializer& S) const;
814  static AsmStmt* CreateImpl(llvm::Deserializer& D);
815};
816
817/// ObjCAtCatchStmt - This represents objective-c's @catch statement.
818class ObjCAtCatchStmt : public Stmt {
819private:
820  enum { SELECTOR, BODY, NEXT_CATCH, END_EXPR };
821  Stmt *SubExprs[END_EXPR];
822  SourceLocation AtCatchLoc, RParenLoc;
823
824  // Used by deserialization.
825  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc)
826  : Stmt(ObjCAtCatchStmtClass), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) {}
827
828public:
829  ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
830                  Stmt *catchVarStmtDecl, Stmt *atCatchStmt, Stmt *atCatchList);
831
832  const Stmt *getCatchBody() const { return SubExprs[BODY]; }
833  Stmt *getCatchBody() { return SubExprs[BODY]; }
834
835  const ObjCAtCatchStmt *getNextCatchStmt() const {
836    return static_cast<const ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
837  }
838  ObjCAtCatchStmt *getNextCatchStmt() {
839    return static_cast<ObjCAtCatchStmt*>(SubExprs[NEXT_CATCH]);
840  }
841
842  const Stmt *getCatchParamStmt() const { return SubExprs[SELECTOR]; }
843  Stmt *getCatchParamStmt() { return SubExprs[SELECTOR]; }
844
845  SourceLocation getRParenLoc() const { return RParenLoc; }
846
847  virtual SourceRange getSourceRange() const {
848    return SourceRange(AtCatchLoc, SubExprs[BODY]->getLocEnd());
849  }
850
851  bool hasEllipsis() const { return getCatchParamStmt() == 0; }
852
853  static bool classof(const Stmt *T) {
854    return T->getStmtClass() == ObjCAtCatchStmtClass;
855  }
856  static bool classof(const ObjCAtCatchStmt *) { return true; }
857
858  virtual child_iterator child_begin();
859  virtual child_iterator child_end();
860
861  virtual void EmitImpl(llvm::Serializer& S) const;
862  static ObjCAtCatchStmt* CreateImpl(llvm::Deserializer& D);
863};
864
865/// ObjCAtFinallyStmt - This represent objective-c's @finally Statement
866class ObjCAtFinallyStmt : public Stmt {
867  Stmt *AtFinallyStmt;
868  SourceLocation AtFinallyLoc;
869public:
870  ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
871  : Stmt(ObjCAtFinallyStmtClass),
872    AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
873
874  const Stmt *getFinallyBody () const { return AtFinallyStmt; }
875  Stmt *getFinallyBody () { return AtFinallyStmt; }
876
877  virtual SourceRange getSourceRange() const {
878    return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
879  }
880
881  static bool classof(const Stmt *T) {
882    return T->getStmtClass() == ObjCAtFinallyStmtClass;
883  }
884  static bool classof(const ObjCAtFinallyStmt *) { return true; }
885
886  virtual child_iterator child_begin();
887  virtual child_iterator child_end();
888
889  virtual void EmitImpl(llvm::Serializer& S) const;
890  static ObjCAtFinallyStmt* CreateImpl(llvm::Deserializer& D);
891};
892
893/// ObjCAtTryStmt - This represent objective-c's over-all
894/// @try ... @catch ... @finally statement.
895class ObjCAtTryStmt : public Stmt {
896private:
897  enum { TRY, CATCH, FINALLY, END_EXPR };
898  Stmt* SubStmts[END_EXPR];
899
900  SourceLocation AtTryLoc;
901public:
902  ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
903                Stmt *atCatchStmt,
904                Stmt *atFinallyStmt)
905  : Stmt(ObjCAtTryStmtClass) {
906      SubStmts[TRY] = atTryStmt;
907      SubStmts[CATCH] = atCatchStmt;
908      SubStmts[FINALLY] = atFinallyStmt;
909      AtTryLoc = atTryLoc;
910    }
911
912  const Stmt *getTryBody() const { return SubStmts[TRY]; }
913  Stmt *getTryBody() { return SubStmts[TRY]; }
914  const ObjCAtCatchStmt *getCatchStmts() const {
915    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
916  }
917  ObjCAtCatchStmt *getCatchStmts() {
918    return dyn_cast_or_null<ObjCAtCatchStmt>(SubStmts[CATCH]);
919  }
920  const ObjCAtFinallyStmt *getFinallyStmt() const {
921    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
922  }
923  ObjCAtFinallyStmt *getFinallyStmt() {
924    return dyn_cast_or_null<ObjCAtFinallyStmt>(SubStmts[FINALLY]);
925  }
926  virtual SourceRange getSourceRange() const {
927    return SourceRange(AtTryLoc, SubStmts[TRY]->getLocEnd());
928  }
929
930  static bool classof(const Stmt *T) {
931    return T->getStmtClass() == ObjCAtTryStmtClass;
932  }
933  static bool classof(const ObjCAtTryStmt *) { return true; }
934
935  virtual child_iterator child_begin();
936  virtual child_iterator child_end();
937
938  virtual void EmitImpl(llvm::Serializer& S) const;
939  static ObjCAtTryStmt* CreateImpl(llvm::Deserializer& D);
940};
941
942/// ObjCAtSynchronizedStmt - This is for objective-c's @synchronized statement.
943/// Example: @synchronized (sem) {
944///             do-something;
945///          }
946///
947class ObjCAtSynchronizedStmt : public Stmt {
948private:
949  enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
950  Stmt* SubStmts[END_EXPR];
951  SourceLocation AtSynchronizedLoc;
952
953public:
954  ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
955                         Stmt *synchBody)
956  : Stmt(ObjCAtSynchronizedStmtClass) {
957      SubStmts[SYNC_EXPR] = synchExpr;
958      SubStmts[SYNC_BODY] = synchBody;
959      AtSynchronizedLoc = atSynchronizedLoc;
960    }
961
962  const CompoundStmt *getSynchBody() const {
963    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
964  }
965  CompoundStmt *getSynchBody() {
966    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
967  }
968
969  const Expr *getSynchExpr() const {
970    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
971  }
972  Expr *getSynchExpr() {
973    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
974  }
975
976  virtual SourceRange getSourceRange() const {
977    return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd());
978  }
979
980  static bool classof(const Stmt *T) {
981    return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
982  }
983  static bool classof(const ObjCAtSynchronizedStmt *) { return true; }
984
985  virtual child_iterator child_begin();
986  virtual child_iterator child_end();
987
988  virtual void EmitImpl(llvm::Serializer& S) const;
989  static ObjCAtSynchronizedStmt* CreateImpl(llvm::Deserializer& D);
990};
991
992/// ObjCAtThrowStmt - This represents objective-c's @throw statement.
993class ObjCAtThrowStmt : public Stmt {
994  Stmt *Throw;
995  SourceLocation AtThrowLoc;
996public:
997  ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
998  : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
999    AtThrowLoc = atThrowLoc;
1000  }
1001
1002  Expr *const getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
1003
1004  virtual SourceRange getSourceRange() const {
1005    if (Throw)
1006      return SourceRange(AtThrowLoc, Throw->getLocEnd());
1007    else
1008      return SourceRange(AtThrowLoc);
1009  }
1010
1011  static bool classof(const Stmt *T) {
1012    return T->getStmtClass() == ObjCAtThrowStmtClass;
1013  }
1014  static bool classof(const ObjCAtThrowStmt *) { return true; }
1015
1016  virtual child_iterator child_begin();
1017  virtual child_iterator child_end();
1018
1019  virtual void EmitImpl(llvm::Serializer& S) const;
1020  static ObjCAtThrowStmt* CreateImpl(llvm::Deserializer& D);
1021};
1022
1023}  // end namespace clang
1024
1025#endif
1026