Stmt.h revision 5831c6a1efc47e6a19d82fe3dd25b5b8fef6979d
1//===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the Stmt interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_STMT_H
15#define LLVM_CLANG_AST_STMT_H
16
17#include "llvm/Support/Casting.h"
18#include "llvm/Support/raw_ostream.h"
19#include "clang/Basic/SourceLocation.h"
20#include "clang/AST/StmtIterator.h"
21#include "clang/AST/DeclGroup.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/iterator.h"
24#include "clang/AST/ASTContext.h"
25#include <string>
26using llvm::dyn_cast_or_null;
27
28namespace clang {
29  class ASTContext;
30  class Expr;
31  class Decl;
32  class ParmVarDecl;
33  class QualType;
34  class IdentifierInfo;
35  class SourceManager;
36  class StringLiteral;
37  class SwitchStmt;
38  class PrinterHelper;
39
40  //===----------------------------------------------------------------------===//
41  // ExprIterator - Iterators for iterating over Stmt* arrays that contain
42  //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
43  //  references to children (to be compatible with StmtIterator).
44  //===----------------------------------------------------------------------===//
45
46  class Stmt;
47  class Expr;
48
49  class ExprIterator {
50    Stmt** I;
51  public:
52    ExprIterator(Stmt** i) : I(i) {}
53    ExprIterator() : I(0) {}
54    ExprIterator& operator++() { ++I; return *this; }
55    ExprIterator operator-(size_t i) { return I-i; }
56    ExprIterator operator+(size_t i) { return I+i; }
57    Expr* operator[](size_t idx);
58    // FIXME: Verify that this will correctly return a signed distance.
59    signed operator-(const ExprIterator& R) const { return I - R.I; }
60    Expr* operator*() const;
61    Expr* operator->() const;
62    bool operator==(const ExprIterator& R) const { return I == R.I; }
63    bool operator!=(const ExprIterator& R) const { return I != R.I; }
64    bool operator>(const ExprIterator& R) const { return I > R.I; }
65    bool operator>=(const ExprIterator& R) const { return I >= R.I; }
66  };
67
68  class ConstExprIterator {
69    Stmt* const * I;
70  public:
71    ConstExprIterator(Stmt* const* i) : I(i) {}
72    ConstExprIterator() : I(0) {}
73    ConstExprIterator& operator++() { ++I; return *this; }
74    ConstExprIterator operator+(size_t i) { return I+i; }
75    ConstExprIterator operator-(size_t i) { return I-i; }
76    const Expr * operator[](size_t idx) const;
77    signed operator-(const ConstExprIterator& R) const { return I - R.I; }
78    const Expr * operator*() const;
79    const Expr * operator->() const;
80    bool operator==(const ConstExprIterator& R) const { return I == R.I; }
81    bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
82    bool operator>(const ConstExprIterator& R) const { return I > R.I; }
83    bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
84  };
85
86//===----------------------------------------------------------------------===//
87// AST classes for statements.
88//===----------------------------------------------------------------------===//
89
90/// Stmt - This represents one statement.
91///
92class Stmt {
93public:
94  enum StmtClass {
95    NoStmtClass = 0,
96#define STMT(CLASS, PARENT) CLASS##Class,
97#define FIRST_STMT(CLASS) firstStmtConstant = CLASS##Class,
98#define LAST_STMT(CLASS) lastStmtConstant = CLASS##Class,
99#define FIRST_EXPR(CLASS) firstExprConstant = CLASS##Class,
100#define LAST_EXPR(CLASS) lastExprConstant = CLASS##Class
101#include "clang/AST/StmtNodes.def"
102};
103private:
104  const StmtClass sClass;
105
106  // Make vanilla 'new' and 'delete' illegal for Stmts.
107protected:
108  void* operator new(size_t bytes) throw() {
109    assert(0 && "Stmts cannot be allocated with regular 'new'.");
110    return 0;
111  }
112  void operator delete(void* data) throw() {
113    assert(0 && "Stmts cannot be released with regular 'delete'.");
114  }
115
116public:
117  // Only allow allocation of Stmts using the allocator in ASTContext
118  // or by doing a placement new.
119  void* operator new(size_t bytes, ASTContext& C,
120                     unsigned alignment = 16) throw() {
121    return ::operator new(bytes, C, alignment);
122  }
123
124  void* operator new(size_t bytes, ASTContext* C,
125                     unsigned alignment = 16) throw() {
126    return ::operator new(bytes, *C, alignment);
127  }
128
129  void* operator new(size_t bytes, void* mem) throw() {
130    return mem;
131  }
132
133  void operator delete(void*, ASTContext&, unsigned) throw() { }
134  void operator delete(void*, ASTContext*, unsigned) throw() { }
135  void operator delete(void*, std::size_t) throw() { }
136  void operator delete(void*, void*) throw() { }
137
138public:
139  /// \brief A placeholder type used to construct an empty shell of a
140  /// type, that will be filled in later (e.g., by some
141  /// de-serialization).
142  struct EmptyShell { };
143
144protected:
145  /// DestroyChildren - Invoked by destructors of subclasses of Stmt to
146  ///  recursively release child AST nodes.
147  void DestroyChildren(ASTContext& Ctx);
148
149  /// \brief Construct an empty statement.
150  explicit Stmt(StmtClass SC, EmptyShell) : sClass(SC) {
151    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
152  }
153
154public:
155  Stmt(StmtClass SC) : sClass(SC) {
156    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
157  }
158  virtual ~Stmt() {}
159
160  virtual void Destroy(ASTContext &Ctx);
161
162  StmtClass getStmtClass() const { return sClass; }
163  const char *getStmtClassName() const;
164
165  /// SourceLocation tokens are not useful in isolation - they are low level
166  /// value objects created/interpreted by SourceManager. We assume AST
167  /// clients will have a pointer to the respective SourceManager.
168  virtual SourceRange getSourceRange() const = 0;
169  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
170  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
171
172  // global temp stats (until we have a per-module visitor)
173  static void addStmtClass(const StmtClass s);
174  static bool CollectingStats(bool enable=false);
175  static void PrintStats();
176
177  /// dump - This does a local dump of the specified AST fragment.  It dumps the
178  /// specified node and a few nodes underneath it, but not the whole subtree.
179  /// This is useful in a debugger.
180  void dump() const;
181  void dump(SourceManager &SM) const;
182
183  /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
184  void dumpAll() const;
185  void dumpAll(SourceManager &SM) const;
186
187  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
188  /// back to its original source language syntax.
189  void dumpPretty() const;
190  void printPretty(llvm::raw_ostream &OS, PrinterHelper* = NULL, unsigned = 0,
191                   bool NoIndent=false) const;
192
193  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
194  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
195  void viewAST() const;
196
197  // Implement isa<T> support.
198  static bool classof(const Stmt *) { return true; }
199
200  /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
201  ///  contain implicit control-flow in the order their subexpressions
202  ///  are evaluated.  This predicate returns true if this statement has
203  ///  such implicit control-flow.  Such statements are also specially handled
204  ///  within CFGs.
205  bool hasImplicitControlFlow() const;
206
207  /// Child Iterators: All subclasses must implement child_begin and child_end
208  ///  to permit easy iteration over the substatements/subexpessions of an
209  ///  AST node.  This permits easy iteration over all nodes in the AST.
210  typedef StmtIterator       child_iterator;
211  typedef ConstStmtIterator  const_child_iterator;
212
213  virtual child_iterator child_begin() = 0;
214  virtual child_iterator child_end()   = 0;
215
216  const_child_iterator child_begin() const {
217    return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
218  }
219
220  const_child_iterator child_end() const {
221    return const_child_iterator(const_cast<Stmt*>(this)->child_end());
222  }
223};
224
225/// DeclStmt - Adaptor class for mixing declarations with statements and
226/// expressions. For example, CompoundStmt mixes statements, expressions
227/// and declarations (variables, types). Another example is ForStmt, where
228/// the first statement can be an expression or a declaration.
229///
230class DeclStmt : public Stmt {
231  DeclGroupRef DG;
232  SourceLocation StartLoc, EndLoc;
233public:
234  DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
235           SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
236                                    StartLoc(startLoc), EndLoc(endLoc) {}
237
238  /// \brief Build an empty declaration statement.
239  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
240
241  virtual void Destroy(ASTContext& Ctx);
242
243  /// isSingleDecl - This method returns true if this DeclStmt refers
244  /// to a single Decl.
245  bool isSingleDecl() const {
246    return DG.isSingleDecl();
247  }
248
249  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
250  Decl *getSingleDecl() { return DG.getSingleDecl(); }
251
252  const DeclGroupRef getDeclGroup() const { return DG; }
253  DeclGroupRef getDeclGroup() { return DG; }
254  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
255
256  SourceLocation getStartLoc() const { return StartLoc; }
257  void setStartLoc(SourceLocation L) { StartLoc = L; }
258  SourceLocation getEndLoc() const { return EndLoc; }
259  void setEndLoc(SourceLocation L) { EndLoc = L; }
260
261  SourceRange getSourceRange() const {
262    return SourceRange(StartLoc, EndLoc);
263  }
264
265  static bool classof(const Stmt *T) {
266    return T->getStmtClass() == DeclStmtClass;
267  }
268  static bool classof(const DeclStmt *) { return true; }
269
270  // Iterators over subexpressions.
271  virtual child_iterator child_begin();
272  virtual child_iterator child_end();
273
274  typedef DeclGroupRef::iterator decl_iterator;
275  typedef DeclGroupRef::const_iterator const_decl_iterator;
276
277  decl_iterator decl_begin() { return DG.begin(); }
278  decl_iterator decl_end() { return DG.end(); }
279  const_decl_iterator decl_begin() const { return DG.begin(); }
280  const_decl_iterator decl_end() const { return DG.end(); }
281};
282
283/// NullStmt - This is the null statement ";": C99 6.8.3p3.
284///
285class NullStmt : public Stmt {
286  SourceLocation SemiLoc;
287public:
288  NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
289
290  /// \brief Build an empty null statement.
291  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
292
293  NullStmt* Clone(ASTContext &C) const;
294
295  SourceLocation getSemiLoc() const { return SemiLoc; }
296  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
297
298  virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
299
300  static bool classof(const Stmt *T) {
301    return T->getStmtClass() == NullStmtClass;
302  }
303  static bool classof(const NullStmt *) { return true; }
304
305  // Iterators
306  virtual child_iterator child_begin();
307  virtual child_iterator child_end();
308};
309
310/// CompoundStmt - This represents a group of statements like { stmt stmt }.
311///
312class CompoundStmt : public Stmt {
313  Stmt** Body;
314  unsigned NumStmts;
315  SourceLocation LBracLoc, RBracLoc;
316public:
317  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts,
318                             SourceLocation LB, SourceLocation RB)
319  : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) {
320    if (NumStmts == 0) {
321      Body = 0;
322      return;
323    }
324
325    Body = new (C) Stmt*[NumStmts];
326    memcpy(Body, StmtStart, numStmts * sizeof(*Body));
327  }
328
329  // \brief Build an empty compound statement.
330  explicit CompoundStmt(EmptyShell Empty)
331    : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { }
332
333  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
334
335  bool body_empty() const { return NumStmts == 0; }
336  unsigned size() const { return NumStmts; }
337
338  typedef Stmt** body_iterator;
339  body_iterator body_begin() { return Body; }
340  body_iterator body_end() { return Body + NumStmts; }
341  Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; }
342
343  typedef Stmt* const * const_body_iterator;
344  const_body_iterator body_begin() const { return Body; }
345  const_body_iterator body_end() const { return Body + NumStmts; }
346  const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; }
347
348  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
349  reverse_body_iterator body_rbegin() {
350    return reverse_body_iterator(body_end());
351  }
352  reverse_body_iterator body_rend() {
353    return reverse_body_iterator(body_begin());
354  }
355
356  typedef std::reverse_iterator<const_body_iterator>
357          const_reverse_body_iterator;
358
359  const_reverse_body_iterator body_rbegin() const {
360    return const_reverse_body_iterator(body_end());
361  }
362
363  const_reverse_body_iterator body_rend() const {
364    return const_reverse_body_iterator(body_begin());
365  }
366
367  virtual SourceRange getSourceRange() const {
368    return SourceRange(LBracLoc, RBracLoc);
369  }
370
371  SourceLocation getLBracLoc() const { return LBracLoc; }
372  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
373  SourceLocation getRBracLoc() const { return RBracLoc; }
374  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
375
376  static bool classof(const Stmt *T) {
377    return T->getStmtClass() == CompoundStmtClass;
378  }
379  static bool classof(const CompoundStmt *) { return true; }
380
381  // Iterators
382  virtual child_iterator child_begin();
383  virtual child_iterator child_end();
384};
385
386// SwitchCase is the base class for CaseStmt and DefaultStmt,
387class SwitchCase : public Stmt {
388protected:
389  // A pointer to the following CaseStmt or DefaultStmt class,
390  // used by SwitchStmt.
391  SwitchCase *NextSwitchCase;
392
393  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
394
395public:
396  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
397
398  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
399
400  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
401
402  Stmt *getSubStmt() { return v_getSubStmt(); }
403
404  virtual SourceRange getSourceRange() const { return SourceRange(); }
405
406  static bool classof(const Stmt *T) {
407    return T->getStmtClass() == CaseStmtClass ||
408    T->getStmtClass() == DefaultStmtClass;
409  }
410  static bool classof(const SwitchCase *) { return true; }
411protected:
412  virtual Stmt* v_getSubStmt() = 0;
413};
414
415class CaseStmt : public SwitchCase {
416  enum { SUBSTMT, LHS, RHS, END_EXPR };
417  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
418                             // GNU "case 1 ... 4" extension
419  SourceLocation CaseLoc;
420  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
421public:
422  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc)
423    : SwitchCase(CaseStmtClass) {
424    SubExprs[SUBSTMT] = 0;
425    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
426    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
427    CaseLoc = caseLoc;
428  }
429
430  /// \brief Build an empty switch case statement.
431  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
432
433  SourceLocation getCaseLoc() const { return CaseLoc; }
434  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
435
436  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
437  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
438  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
439
440  const Expr *getLHS() const {
441    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
442  }
443  const Expr *getRHS() const {
444    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
445  }
446  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
447
448  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
449  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
450  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
451
452
453  virtual SourceRange getSourceRange() const {
454    // Handle deeply nested case statements with iteration instead of recursion.
455    const CaseStmt *CS = this;
456    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
457      CS = CS2;
458
459    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
460  }
461  static bool classof(const Stmt *T) {
462    return T->getStmtClass() == CaseStmtClass;
463  }
464  static bool classof(const CaseStmt *) { return true; }
465
466  // Iterators
467  virtual child_iterator child_begin();
468  virtual child_iterator child_end();
469};
470
471class DefaultStmt : public SwitchCase {
472  Stmt* SubStmt;
473  SourceLocation DefaultLoc;
474  virtual Stmt* v_getSubStmt() { return getSubStmt(); }
475public:
476  DefaultStmt(SourceLocation DL, Stmt *substmt) :
477    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {}
478
479  /// \brief Build an empty default statement.
480  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
481
482  Stmt *getSubStmt() { return SubStmt; }
483  const Stmt *getSubStmt() const { return SubStmt; }
484  void setSubStmt(Stmt *S) { SubStmt = S; }
485
486  SourceLocation getDefaultLoc() const { return DefaultLoc; }
487  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
488
489  virtual SourceRange getSourceRange() const {
490    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
491  }
492  static bool classof(const Stmt *T) {
493    return T->getStmtClass() == DefaultStmtClass;
494  }
495  static bool classof(const DefaultStmt *) { return true; }
496
497  // Iterators
498  virtual child_iterator child_begin();
499  virtual child_iterator child_end();
500};
501
502class LabelStmt : public Stmt {
503  IdentifierInfo *Label;
504  Stmt *SubStmt;
505  SourceLocation IdentLoc;
506public:
507  LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
508    : Stmt(LabelStmtClass), Label(label),
509      SubStmt(substmt), IdentLoc(IL) {}
510
511  // \brief Build an empty label statement.
512  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
513
514  SourceLocation getIdentLoc() const { return IdentLoc; }
515  IdentifierInfo *getID() const { return Label; }
516  void setID(IdentifierInfo *II) { Label = II; }
517  const char *getName() const;
518  Stmt *getSubStmt() { return SubStmt; }
519  const Stmt *getSubStmt() const { return SubStmt; }
520  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
521  void setSubStmt(Stmt *SS) { SubStmt = SS; }
522
523  virtual SourceRange getSourceRange() const {
524    return SourceRange(IdentLoc, SubStmt->getLocEnd());
525  }
526  static bool classof(const Stmt *T) {
527    return T->getStmtClass() == LabelStmtClass;
528  }
529  static bool classof(const LabelStmt *) { return true; }
530
531  // Iterators
532  virtual child_iterator child_begin();
533  virtual child_iterator child_end();
534};
535
536
537/// IfStmt - This represents an if/then/else.
538///
539class IfStmt : public Stmt {
540  enum { COND, THEN, ELSE, END_EXPR };
541  Stmt* SubExprs[END_EXPR];
542  SourceLocation IfLoc;
543  SourceLocation ElseLoc;
544public:
545  IfStmt(SourceLocation IL, Expr *cond, Stmt *then,
546         SourceLocation EL = SourceLocation(), Stmt *elsev = 0)
547    : Stmt(IfStmtClass)  {
548    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
549    SubExprs[THEN] = then;
550    SubExprs[ELSE] = elsev;
551    IfLoc = IL;
552    ElseLoc = EL;
553  }
554
555  /// \brief Build an empty if/then/else statement
556  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
557
558  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
559  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
560  const Stmt *getThen() const { return SubExprs[THEN]; }
561  void setThen(Stmt *S) { SubExprs[THEN] = S; }
562  const Stmt *getElse() const { return SubExprs[ELSE]; }
563  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
564
565  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
566  Stmt *getThen() { return SubExprs[THEN]; }
567  Stmt *getElse() { return SubExprs[ELSE]; }
568
569  SourceLocation getIfLoc() const { return IfLoc; }
570  void setIfLoc(SourceLocation L) { IfLoc = L; }
571  SourceLocation getElseLoc() const { return ElseLoc; }
572  void setElseLoc(SourceLocation L) { ElseLoc = L; }
573
574  virtual SourceRange getSourceRange() const {
575    if (SubExprs[ELSE])
576      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
577    else
578      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
579  }
580
581  static bool classof(const Stmt *T) {
582    return T->getStmtClass() == IfStmtClass;
583  }
584  static bool classof(const IfStmt *) { return true; }
585
586  // Iterators
587  virtual child_iterator child_begin();
588  virtual child_iterator child_end();
589};
590
591/// SwitchStmt - This represents a 'switch' stmt.
592///
593class SwitchStmt : public Stmt {
594  enum { COND, BODY, END_EXPR };
595  Stmt* SubExprs[END_EXPR];
596  // This points to a linked list of case and default statements.
597  SwitchCase *FirstCase;
598  SourceLocation SwitchLoc;
599public:
600  SwitchStmt(Expr *cond) : Stmt(SwitchStmtClass), FirstCase(0) {
601      SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
602      SubExprs[BODY] = NULL;
603    }
604
605  /// \brief Build a empty switch statement.
606  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
607
608  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
609  const Stmt *getBody() const { return SubExprs[BODY]; }
610  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
611
612  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
613  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
614  Stmt *getBody() { return SubExprs[BODY]; }
615  void setBody(Stmt *S) { SubExprs[BODY] = S; }
616  SwitchCase *getSwitchCaseList() { return FirstCase; }
617  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
618
619  SourceLocation getSwitchLoc() const { return SwitchLoc; }
620  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
621
622  void setBody(Stmt *S, SourceLocation SL) {
623    SubExprs[BODY] = S;
624    SwitchLoc = SL;
625  }
626  void addSwitchCase(SwitchCase *SC) {
627    assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
628    SC->setNextSwitchCase(FirstCase);
629    FirstCase = SC;
630  }
631  virtual SourceRange getSourceRange() const {
632    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
633  }
634  static bool classof(const Stmt *T) {
635    return T->getStmtClass() == SwitchStmtClass;
636  }
637  static bool classof(const SwitchStmt *) { return true; }
638
639  // Iterators
640  virtual child_iterator child_begin();
641  virtual child_iterator child_end();
642};
643
644
645/// WhileStmt - This represents a 'while' stmt.
646///
647class WhileStmt : public Stmt {
648  enum { COND, BODY, END_EXPR };
649  Stmt* SubExprs[END_EXPR];
650  SourceLocation WhileLoc;
651public:
652  WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) {
653    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
654    SubExprs[BODY] = body;
655    WhileLoc = WL;
656  }
657
658  /// \brief Build an empty while statement.
659  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
660
661  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
662  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
663  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
664  Stmt *getBody() { return SubExprs[BODY]; }
665  const Stmt *getBody() const { return SubExprs[BODY]; }
666  void setBody(Stmt *S) { SubExprs[BODY] = S; }
667
668  SourceLocation getWhileLoc() const { return WhileLoc; }
669  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
670
671  virtual SourceRange getSourceRange() const {
672    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
673  }
674  static bool classof(const Stmt *T) {
675    return T->getStmtClass() == WhileStmtClass;
676  }
677  static bool classof(const WhileStmt *) { return true; }
678
679  // Iterators
680  virtual child_iterator child_begin();
681  virtual child_iterator child_end();
682};
683
684/// DoStmt - This represents a 'do/while' stmt.
685///
686class DoStmt : public Stmt {
687  enum { COND, BODY, END_EXPR };
688  Stmt* SubExprs[END_EXPR];
689  SourceLocation DoLoc;
690  SourceLocation WhileLoc;
691
692public:
693  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL)
694    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL) {
695    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
696    SubExprs[BODY] = body;
697    DoLoc = DL;
698    WhileLoc = WL;
699  }
700
701  /// \brief Build an empty do-while statement.
702  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
703
704  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
705  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
706  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
707  Stmt *getBody() { return SubExprs[BODY]; }
708  const Stmt *getBody() const { return SubExprs[BODY]; }
709  void setBody(Stmt *S) { SubExprs[BODY] = S; }
710
711  SourceLocation getDoLoc() const { return DoLoc; }
712  void setDoLoc(SourceLocation L) { DoLoc = L; }
713  SourceLocation getWhileLoc() const { return WhileLoc; }
714  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
715
716  virtual SourceRange getSourceRange() const {
717    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
718  }
719  static bool classof(const Stmt *T) {
720    return T->getStmtClass() == DoStmtClass;
721  }
722  static bool classof(const DoStmt *) { return true; }
723
724  // Iterators
725  virtual child_iterator child_begin();
726  virtual child_iterator child_end();
727};
728
729
730/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
731/// the init/cond/inc parts of the ForStmt will be null if they were not
732/// specified in the source.
733///
734class ForStmt : public Stmt {
735  enum { INIT, COND, INC, BODY, END_EXPR };
736  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
737  SourceLocation ForLoc;
738  SourceLocation LParenLoc, RParenLoc;
739
740public:
741  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL,
742          SourceLocation LP, SourceLocation RP)
743    : Stmt(ForStmtClass) {
744    SubExprs[INIT] = Init;
745    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
746    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
747    SubExprs[BODY] = Body;
748    ForLoc = FL;
749    LParenLoc = LP;
750    RParenLoc = RP;
751  }
752
753  /// \brief Build an empty for statement.
754  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
755
756  Stmt *getInit() { return SubExprs[INIT]; }
757  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
758  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
759  Stmt *getBody() { return SubExprs[BODY]; }
760
761  const Stmt *getInit() const { return SubExprs[INIT]; }
762  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
763  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
764  const Stmt *getBody() const { return SubExprs[BODY]; }
765
766  void setInit(Stmt *S) { SubExprs[INIT] = S; }
767  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
768  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
769  void setBody(Stmt *S) { SubExprs[BODY] = S; }
770
771  SourceLocation getForLoc() const { return ForLoc; }
772  void setForLoc(SourceLocation L) { ForLoc = L; }
773  SourceLocation getLParenLoc() const { return LParenLoc; }
774  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
775  SourceLocation getRParenLoc() const { return RParenLoc; }
776  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
777
778  virtual SourceRange getSourceRange() const {
779    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
780  }
781  static bool classof(const Stmt *T) {
782    return T->getStmtClass() == ForStmtClass;
783  }
784  static bool classof(const ForStmt *) { return true; }
785
786  // Iterators
787  virtual child_iterator child_begin();
788  virtual child_iterator child_end();
789};
790
791/// GotoStmt - This represents a direct goto.
792///
793class GotoStmt : public Stmt {
794  LabelStmt *Label;
795  SourceLocation GotoLoc;
796  SourceLocation LabelLoc;
797public:
798  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
799    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
800
801  /// \brief Build an empty goto statement.
802  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
803
804  LabelStmt *getLabel() const { return Label; }
805  void setLabel(LabelStmt *S) { Label = S; }
806
807  SourceLocation getGotoLoc() const { return GotoLoc; }
808  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
809  SourceLocation getLabelLoc() const { return LabelLoc; }
810  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
811
812  virtual SourceRange getSourceRange() const {
813    return SourceRange(GotoLoc, LabelLoc);
814  }
815  static bool classof(const Stmt *T) {
816    return T->getStmtClass() == GotoStmtClass;
817  }
818  static bool classof(const GotoStmt *) { return true; }
819
820  // Iterators
821  virtual child_iterator child_begin();
822  virtual child_iterator child_end();
823};
824
825/// IndirectGotoStmt - This represents an indirect goto.
826///
827class IndirectGotoStmt : public Stmt {
828  SourceLocation GotoLoc;
829  Stmt *Target;
830public:
831  IndirectGotoStmt(SourceLocation gotoLoc, Expr *target)
832    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), Target((Stmt*)target) {}
833
834  /// \brief Build an empty indirect goto statement.
835  explicit IndirectGotoStmt(EmptyShell Empty)
836    : Stmt(IndirectGotoStmtClass, Empty) { }
837
838  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
839  SourceLocation getGotoLoc() const { return GotoLoc; }
840
841  Expr *getTarget();
842  const Expr *getTarget() const;
843  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
844
845  virtual SourceRange getSourceRange() const {
846    return SourceRange(GotoLoc, Target->getLocEnd());
847  }
848
849  static bool classof(const Stmt *T) {
850    return T->getStmtClass() == IndirectGotoStmtClass;
851  }
852  static bool classof(const IndirectGotoStmt *) { return true; }
853
854  // Iterators
855  virtual child_iterator child_begin();
856  virtual child_iterator child_end();
857};
858
859
860/// ContinueStmt - This represents a continue.
861///
862class ContinueStmt : public Stmt {
863  SourceLocation ContinueLoc;
864public:
865  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
866
867  /// \brief Build an empty continue statement.
868  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
869
870  SourceLocation getContinueLoc() const { return ContinueLoc; }
871  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
872
873  virtual SourceRange getSourceRange() const {
874    return SourceRange(ContinueLoc);
875  }
876  static bool classof(const Stmt *T) {
877    return T->getStmtClass() == ContinueStmtClass;
878  }
879  static bool classof(const ContinueStmt *) { return true; }
880
881  // Iterators
882  virtual child_iterator child_begin();
883  virtual child_iterator child_end();
884};
885
886/// BreakStmt - This represents a break.
887///
888class BreakStmt : public Stmt {
889  SourceLocation BreakLoc;
890public:
891  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
892
893  /// \brief Build an empty break statement.
894  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
895
896  SourceLocation getBreakLoc() const { return BreakLoc; }
897  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
898
899  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
900
901  static bool classof(const Stmt *T) {
902    return T->getStmtClass() == BreakStmtClass;
903  }
904  static bool classof(const BreakStmt *) { return true; }
905
906  // Iterators
907  virtual child_iterator child_begin();
908  virtual child_iterator child_end();
909};
910
911
912/// ReturnStmt - This represents a return, optionally of an expression:
913///   return;
914///   return 4;
915///
916/// Note that GCC allows return with no argument in a function declared to
917/// return a value, and it allows returning a value in functions declared to
918/// return void.  We explicitly model this in the AST, which means you can't
919/// depend on the return type of the function and the presence of an argument.
920///
921class ReturnStmt : public Stmt {
922  Stmt *RetExpr;
923  SourceLocation RetLoc;
924public:
925  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
926    RetExpr((Stmt*) E), RetLoc(RL) {}
927
928  /// \brief Build an empty return expression.
929  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
930
931  const Expr *getRetValue() const;
932  Expr *getRetValue();
933  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
934
935  SourceLocation getReturnLoc() const { return RetLoc; }
936  void setReturnLoc(SourceLocation L) { RetLoc = L; }
937
938  virtual SourceRange getSourceRange() const;
939
940  static bool classof(const Stmt *T) {
941    return T->getStmtClass() == ReturnStmtClass;
942  }
943  static bool classof(const ReturnStmt *) { return true; }
944
945  // Iterators
946  virtual child_iterator child_begin();
947  virtual child_iterator child_end();
948};
949
950/// AsmStmt - This represents a GNU inline-assembly statement extension.
951///
952class AsmStmt : public Stmt {
953  SourceLocation AsmLoc, RParenLoc;
954  StringLiteral *AsmStr;
955
956  bool IsSimple;
957  bool IsVolatile;
958
959  unsigned NumOutputs;
960  unsigned NumInputs;
961
962  llvm::SmallVector<std::string, 4> Names;
963  llvm::SmallVector<StringLiteral*, 4> Constraints;
964  llvm::SmallVector<Stmt*, 4> Exprs;
965
966  llvm::SmallVector<StringLiteral*, 4> Clobbers;
967public:
968  AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
969          unsigned numoutputs, unsigned numinputs,
970          std::string *names, StringLiteral **constraints,
971          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
972          StringLiteral **clobbers, SourceLocation rparenloc);
973
974  /// \brief Build an empty inline-assembly statement.
975  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty) { }
976
977  SourceLocation getAsmLoc() const { return AsmLoc; }
978  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
979  SourceLocation getRParenLoc() const { return RParenLoc; }
980  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
981
982  bool isVolatile() const { return IsVolatile; }
983  void setVolatile(bool V) { IsVolatile = V; }
984  bool isSimple() const { return IsSimple; }
985  void setSimple(bool V) { IsSimple = false; }
986
987  //===--- Asm String Analysis ---===//
988
989  const StringLiteral *getAsmString() const { return AsmStr; }
990  StringLiteral *getAsmString() { return AsmStr; }
991  void setAsmString(StringLiteral *E) { AsmStr = E; }
992
993  /// AsmStringPiece - this is part of a decomposed asm string specification
994  /// (for use with the AnalyzeAsmString function below).  An asm string is
995  /// considered to be a concatenation of these parts.
996  class AsmStringPiece {
997  public:
998    enum Kind {
999      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1000      Operand  // Operand reference, with optional modifier %c4.
1001    };
1002  private:
1003    Kind MyKind;
1004    std::string Str;
1005    unsigned OperandNo;
1006  public:
1007    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1008    AsmStringPiece(unsigned OpNo, char Modifier)
1009      : MyKind(Operand), Str(), OperandNo(OpNo) {
1010      Str += Modifier;
1011    }
1012
1013    bool isString() const { return MyKind == String; }
1014    bool isOperand() const { return MyKind == Operand; }
1015
1016    const std::string &getString() const {
1017      assert(isString());
1018      return Str;
1019    }
1020
1021    unsigned getOperandNo() const {
1022      assert(isOperand());
1023      return OperandNo;
1024    }
1025
1026    /// getModifier - Get the modifier for this operand, if present.  This
1027    /// returns '\0' if there was no modifier.
1028    char getModifier() const {
1029      assert(isOperand());
1030      return Str[0];
1031    }
1032  };
1033
1034  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1035  /// it into pieces.  If the asm string is erroneous, emit errors and return
1036  /// true, otherwise return false.  This handles canonicalization and
1037  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1038  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1039  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1040                            ASTContext &C, unsigned &DiagOffs) const;
1041
1042
1043  //===--- Output operands ---===//
1044
1045  unsigned getNumOutputs() const { return NumOutputs; }
1046
1047  const std::string &getOutputName(unsigned i) const {
1048    return Names[i];
1049  }
1050
1051  /// getOutputConstraint - Return the constraint string for the specified
1052  /// output operand.  All output constraints are known to be non-empty (either
1053  /// '=' or '+').
1054  std::string getOutputConstraint(unsigned i) const;
1055
1056  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1057    return Constraints[i];
1058  }
1059  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1060    return Constraints[i];
1061  }
1062
1063
1064  Expr *getOutputExpr(unsigned i);
1065
1066  const Expr *getOutputExpr(unsigned i) const {
1067    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1068  }
1069
1070  /// isOutputPlusConstraint - Return true if the specified output constraint
1071  /// is a "+" constraint (which is both an input and an output) or false if it
1072  /// is an "=" constraint (just an output).
1073  bool isOutputPlusConstraint(unsigned i) const {
1074    return getOutputConstraint(i)[0] == '+';
1075  }
1076
1077  /// getNumPlusOperands - Return the number of output operands that have a "+"
1078  /// constraint.
1079  unsigned getNumPlusOperands() const;
1080
1081  //===--- Input operands ---===//
1082
1083  unsigned getNumInputs() const { return NumInputs; }
1084
1085  const std::string &getInputName(unsigned i) const {
1086    return Names[i + NumOutputs];
1087  }
1088
1089  /// getInputConstraint - Return the specified input constraint.  Unlike output
1090  /// constraints, these can be empty.
1091  std::string getInputConstraint(unsigned i) const;
1092
1093  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1094    return Constraints[i + NumOutputs];
1095  }
1096  StringLiteral *getInputConstraintLiteral(unsigned i) {
1097    return Constraints[i + NumOutputs];
1098  }
1099
1100
1101  Expr *getInputExpr(unsigned i);
1102
1103  const Expr *getInputExpr(unsigned i) const {
1104    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1105  }
1106
1107  void setOutputsAndInputs(unsigned NumOutputs,
1108                           unsigned NumInputs,
1109                           const std::string *Names,
1110                           StringLiteral **Constraints,
1111                           Stmt **Exprs);
1112
1113  //===--- Other ---===//
1114
1115  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1116  /// translate this into a numeric value needed to reference the same operand.
1117  /// This returns -1 if the operand name is invalid.
1118  int getNamedOperand(const std::string &SymbolicName) const;
1119
1120
1121
1122  unsigned getNumClobbers() const { return Clobbers.size(); }
1123  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1124  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1125  void setClobbers(StringLiteral **Clobbers, unsigned NumClobbers);
1126
1127  virtual SourceRange getSourceRange() const {
1128    return SourceRange(AsmLoc, RParenLoc);
1129  }
1130
1131  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1132  static bool classof(const AsmStmt *) { return true; }
1133
1134  // Input expr iterators.
1135
1136  typedef ExprIterator inputs_iterator;
1137  typedef ConstExprIterator const_inputs_iterator;
1138
1139  inputs_iterator begin_inputs() {
1140    return &Exprs[0] + NumOutputs;
1141  }
1142
1143  inputs_iterator end_inputs() {
1144    return  &Exprs[0] + NumOutputs + NumInputs;
1145  }
1146
1147  const_inputs_iterator begin_inputs() const {
1148    return &Exprs[0] + NumOutputs;
1149  }
1150
1151  const_inputs_iterator end_inputs() const {
1152    return  &Exprs[0] + NumOutputs + NumInputs;}
1153
1154  // Output expr iterators.
1155
1156  typedef ExprIterator outputs_iterator;
1157  typedef ConstExprIterator const_outputs_iterator;
1158
1159  outputs_iterator begin_outputs() { return &Exprs[0]; }
1160  outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; }
1161
1162  const_outputs_iterator begin_outputs() const { return &Exprs[0]; }
1163  const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; }
1164
1165  // Input name iterator.
1166
1167  const std::string *begin_output_names() const {
1168    return &Names[0];
1169  }
1170
1171  const std::string *end_output_names() const {
1172    return &Names[0] + NumOutputs;
1173  }
1174
1175  // Child iterators
1176
1177  virtual child_iterator child_begin();
1178  virtual child_iterator child_end();
1179};
1180
1181}  // end namespace clang
1182
1183#endif
1184