Stmt.h revision d06f6ca61062f85926eb9d409eb3d4f8afcf93c7
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;
690public:
691  DoStmt(Stmt *body, Expr *cond, SourceLocation DL)
692    : Stmt(DoStmtClass), DoLoc(DL) {
693    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
694    SubExprs[BODY] = body;
695    DoLoc = DL;
696  }
697
698  /// \brief Build an empty do-while statement.
699  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
700
701  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
702  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
703  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
704  Stmt *getBody() { return SubExprs[BODY]; }
705  const Stmt *getBody() const { return SubExprs[BODY]; }
706  void setBody(Stmt *S) { SubExprs[BODY] = S; }
707
708  SourceLocation getDoLoc() const { return DoLoc; }
709  void setDoLoc(SourceLocation L) { DoLoc = L; }
710
711  virtual SourceRange getSourceRange() const {
712    return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd());
713  }
714  static bool classof(const Stmt *T) {
715    return T->getStmtClass() == DoStmtClass;
716  }
717  static bool classof(const DoStmt *) { return true; }
718
719  // Iterators
720  virtual child_iterator child_begin();
721  virtual child_iterator child_end();
722};
723
724
725/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
726/// the init/cond/inc parts of the ForStmt will be null if they were not
727/// specified in the source.
728///
729class ForStmt : public Stmt {
730  enum { INIT, COND, INC, BODY, END_EXPR };
731  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
732  SourceLocation ForLoc;
733public:
734  ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL)
735    : Stmt(ForStmtClass) {
736    SubExprs[INIT] = Init;
737    SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
738    SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
739    SubExprs[BODY] = Body;
740    ForLoc = FL;
741  }
742
743  /// \brief Build an empty for statement.
744  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
745
746  Stmt *getInit() { return SubExprs[INIT]; }
747  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
748  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
749  Stmt *getBody() { return SubExprs[BODY]; }
750
751  const Stmt *getInit() const { return SubExprs[INIT]; }
752  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
753  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
754  const Stmt *getBody() const { return SubExprs[BODY]; }
755
756  void setInit(Stmt *S) { SubExprs[INIT] = S; }
757  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
758  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
759  void setBody(Stmt *S) { SubExprs[BODY] = S; }
760
761  SourceLocation getForLoc() const { return ForLoc; }
762  void setForLoc(SourceLocation L) { ForLoc = L; }
763
764  virtual SourceRange getSourceRange() const {
765    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
766  }
767  static bool classof(const Stmt *T) {
768    return T->getStmtClass() == ForStmtClass;
769  }
770  static bool classof(const ForStmt *) { return true; }
771
772  // Iterators
773  virtual child_iterator child_begin();
774  virtual child_iterator child_end();
775};
776
777/// GotoStmt - This represents a direct goto.
778///
779class GotoStmt : public Stmt {
780  LabelStmt *Label;
781  SourceLocation GotoLoc;
782  SourceLocation LabelLoc;
783public:
784  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
785    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
786
787  /// \brief Build an empty goto statement.
788  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
789
790  LabelStmt *getLabel() const { return Label; }
791  void setLabel(LabelStmt *S) { Label = S; }
792
793  SourceLocation getGotoLoc() const { return GotoLoc; }
794  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
795  SourceLocation getLabelLoc() const { return LabelLoc; }
796  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
797
798  virtual SourceRange getSourceRange() const {
799    return SourceRange(GotoLoc, LabelLoc);
800  }
801  static bool classof(const Stmt *T) {
802    return T->getStmtClass() == GotoStmtClass;
803  }
804  static bool classof(const GotoStmt *) { return true; }
805
806  // Iterators
807  virtual child_iterator child_begin();
808  virtual child_iterator child_end();
809};
810
811/// IndirectGotoStmt - This represents an indirect goto.
812///
813class IndirectGotoStmt : public Stmt {
814  SourceLocation GotoLoc;
815  Stmt *Target;
816public:
817  IndirectGotoStmt(SourceLocation gotoLoc, Expr *target)
818    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), Target((Stmt*)target) {}
819
820  /// \brief Build an empty indirect goto statement.
821  explicit IndirectGotoStmt(EmptyShell Empty)
822    : Stmt(IndirectGotoStmtClass, Empty) { }
823
824  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
825  SourceLocation getGotoLoc() const { return GotoLoc; }
826
827  Expr *getTarget();
828  const Expr *getTarget() const;
829  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
830
831  virtual SourceRange getSourceRange() const {
832    return SourceRange(GotoLoc, Target->getLocEnd());
833  }
834
835  static bool classof(const Stmt *T) {
836    return T->getStmtClass() == IndirectGotoStmtClass;
837  }
838  static bool classof(const IndirectGotoStmt *) { return true; }
839
840  // Iterators
841  virtual child_iterator child_begin();
842  virtual child_iterator child_end();
843};
844
845
846/// ContinueStmt - This represents a continue.
847///
848class ContinueStmt : public Stmt {
849  SourceLocation ContinueLoc;
850public:
851  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
852
853  /// \brief Build an empty continue statement.
854  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
855
856  SourceLocation getContinueLoc() const { return ContinueLoc; }
857  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
858
859  virtual SourceRange getSourceRange() const {
860    return SourceRange(ContinueLoc);
861  }
862  static bool classof(const Stmt *T) {
863    return T->getStmtClass() == ContinueStmtClass;
864  }
865  static bool classof(const ContinueStmt *) { return true; }
866
867  // Iterators
868  virtual child_iterator child_begin();
869  virtual child_iterator child_end();
870};
871
872/// BreakStmt - This represents a break.
873///
874class BreakStmt : public Stmt {
875  SourceLocation BreakLoc;
876public:
877  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
878
879  /// \brief Build an empty break statement.
880  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
881
882  SourceLocation getBreakLoc() const { return BreakLoc; }
883  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
884
885  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
886
887  static bool classof(const Stmt *T) {
888    return T->getStmtClass() == BreakStmtClass;
889  }
890  static bool classof(const BreakStmt *) { return true; }
891
892  // Iterators
893  virtual child_iterator child_begin();
894  virtual child_iterator child_end();
895};
896
897
898/// ReturnStmt - This represents a return, optionally of an expression:
899///   return;
900///   return 4;
901///
902/// Note that GCC allows return with no argument in a function declared to
903/// return a value, and it allows returning a value in functions declared to
904/// return void.  We explicitly model this in the AST, which means you can't
905/// depend on the return type of the function and the presence of an argument.
906///
907class ReturnStmt : public Stmt {
908  Stmt *RetExpr;
909  SourceLocation RetLoc;
910public:
911  ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
912    RetExpr((Stmt*) E), RetLoc(RL) {}
913
914  /// \brief Build an empty return expression.
915  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
916
917  const Expr *getRetValue() const;
918  Expr *getRetValue();
919  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
920
921  SourceLocation getReturnLoc() const { return RetLoc; }
922  void setReturnLoc(SourceLocation L) { RetLoc = L; }
923
924  virtual SourceRange getSourceRange() const;
925
926  static bool classof(const Stmt *T) {
927    return T->getStmtClass() == ReturnStmtClass;
928  }
929  static bool classof(const ReturnStmt *) { return true; }
930
931  // Iterators
932  virtual child_iterator child_begin();
933  virtual child_iterator child_end();
934};
935
936/// AsmStmt - This represents a GNU inline-assembly statement extension.
937///
938class AsmStmt : public Stmt {
939  SourceLocation AsmLoc, RParenLoc;
940  StringLiteral *AsmStr;
941
942  bool IsSimple;
943  bool IsVolatile;
944
945  unsigned NumOutputs;
946  unsigned NumInputs;
947
948  llvm::SmallVector<std::string, 4> Names;
949  llvm::SmallVector<StringLiteral*, 4> Constraints;
950  llvm::SmallVector<Stmt*, 4> Exprs;
951
952  llvm::SmallVector<StringLiteral*, 4> Clobbers;
953public:
954  AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
955          unsigned numoutputs, unsigned numinputs,
956          std::string *names, StringLiteral **constraints,
957          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
958          StringLiteral **clobbers, SourceLocation rparenloc);
959
960  /// \brief Build an empty inline-assembly statement.
961  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty) { }
962
963  SourceLocation getAsmLoc() const { return AsmLoc; }
964  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
965  SourceLocation getRParenLoc() const { return RParenLoc; }
966  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
967
968  bool isVolatile() const { return IsVolatile; }
969  void setVolatile(bool V) { IsVolatile = V; }
970  bool isSimple() const { return IsSimple; }
971  void setSimple(bool V) { IsSimple = false; }
972
973  //===--- Asm String Analysis ---===//
974
975  const StringLiteral *getAsmString() const { return AsmStr; }
976  StringLiteral *getAsmString() { return AsmStr; }
977  void setAsmString(StringLiteral *E) { AsmStr = E; }
978
979  /// AsmStringPiece - this is part of a decomposed asm string specification
980  /// (for use with the AnalyzeAsmString function below).  An asm string is
981  /// considered to be a concatenation of these parts.
982  class AsmStringPiece {
983  public:
984    enum Kind {
985      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
986      Operand  // Operand reference, with optional modifier %c4.
987    };
988  private:
989    Kind MyKind;
990    std::string Str;
991    unsigned OperandNo;
992  public:
993    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
994    AsmStringPiece(unsigned OpNo, char Modifier)
995      : MyKind(Operand), Str(), OperandNo(OpNo) {
996      Str += Modifier;
997    }
998
999    bool isString() const { return MyKind == String; }
1000    bool isOperand() const { return MyKind == Operand; }
1001
1002    const std::string &getString() const {
1003      assert(isString());
1004      return Str;
1005    }
1006
1007    unsigned getOperandNo() const {
1008      assert(isOperand());
1009      return OperandNo;
1010    }
1011
1012    /// getModifier - Get the modifier for this operand, if present.  This
1013    /// returns '\0' if there was no modifier.
1014    char getModifier() const {
1015      assert(isOperand());
1016      return Str[0];
1017    }
1018  };
1019
1020  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1021  /// it into pieces.  If the asm string is erroneous, emit errors and return
1022  /// true, otherwise return false.  This handles canonicalization and
1023  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1024  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1025  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1026                            ASTContext &C, unsigned &DiagOffs) const;
1027
1028
1029  //===--- Output operands ---===//
1030
1031  unsigned getNumOutputs() const { return NumOutputs; }
1032
1033  const std::string &getOutputName(unsigned i) const {
1034    return Names[i];
1035  }
1036
1037  /// getOutputConstraint - Return the constraint string for the specified
1038  /// output operand.  All output constraints are known to be non-empty (either
1039  /// '=' or '+').
1040  std::string getOutputConstraint(unsigned i) const;
1041
1042  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1043    return Constraints[i];
1044  }
1045  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1046    return Constraints[i];
1047  }
1048
1049
1050  Expr *getOutputExpr(unsigned i);
1051
1052  const Expr *getOutputExpr(unsigned i) const {
1053    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1054  }
1055
1056  /// isOutputPlusConstraint - Return true if the specified output constraint
1057  /// is a "+" constraint (which is both an input and an output) or false if it
1058  /// is an "=" constraint (just an output).
1059  bool isOutputPlusConstraint(unsigned i) const {
1060    return getOutputConstraint(i)[0] == '+';
1061  }
1062
1063  /// getNumPlusOperands - Return the number of output operands that have a "+"
1064  /// constraint.
1065  unsigned getNumPlusOperands() const;
1066
1067  //===--- Input operands ---===//
1068
1069  unsigned getNumInputs() const { return NumInputs; }
1070
1071  const std::string &getInputName(unsigned i) const {
1072    return Names[i + NumOutputs];
1073  }
1074
1075  /// getInputConstraint - Return the specified input constraint.  Unlike output
1076  /// constraints, these can be empty.
1077  std::string getInputConstraint(unsigned i) const;
1078
1079  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1080    return Constraints[i + NumOutputs];
1081  }
1082  StringLiteral *getInputConstraintLiteral(unsigned i) {
1083    return Constraints[i + NumOutputs];
1084  }
1085
1086
1087  Expr *getInputExpr(unsigned i);
1088
1089  const Expr *getInputExpr(unsigned i) const {
1090    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1091  }
1092
1093  void setOutputsAndInputs(unsigned NumOutputs,
1094                           unsigned NumInputs,
1095                           const std::string *Names,
1096                           StringLiteral **Constraints,
1097                           Stmt **Exprs);
1098
1099  //===--- Other ---===//
1100
1101  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1102  /// translate this into a numeric value needed to reference the same operand.
1103  /// This returns -1 if the operand name is invalid.
1104  int getNamedOperand(const std::string &SymbolicName) const;
1105
1106
1107
1108  unsigned getNumClobbers() const { return Clobbers.size(); }
1109  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1110  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1111  void setClobbers(StringLiteral **Clobbers, unsigned NumClobbers);
1112
1113  virtual SourceRange getSourceRange() const {
1114    return SourceRange(AsmLoc, RParenLoc);
1115  }
1116
1117  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1118  static bool classof(const AsmStmt *) { return true; }
1119
1120  // Input expr iterators.
1121
1122  typedef ExprIterator inputs_iterator;
1123  typedef ConstExprIterator const_inputs_iterator;
1124
1125  inputs_iterator begin_inputs() {
1126    return &Exprs[0] + NumOutputs;
1127  }
1128
1129  inputs_iterator end_inputs() {
1130    return  &Exprs[0] + NumOutputs + NumInputs;
1131  }
1132
1133  const_inputs_iterator begin_inputs() const {
1134    return &Exprs[0] + NumOutputs;
1135  }
1136
1137  const_inputs_iterator end_inputs() const {
1138    return  &Exprs[0] + NumOutputs + NumInputs;}
1139
1140  // Output expr iterators.
1141
1142  typedef ExprIterator outputs_iterator;
1143  typedef ConstExprIterator const_outputs_iterator;
1144
1145  outputs_iterator begin_outputs() { return &Exprs[0]; }
1146  outputs_iterator end_outputs() { return &Exprs[0] + NumOutputs; }
1147
1148  const_outputs_iterator begin_outputs() const { return &Exprs[0]; }
1149  const_outputs_iterator end_outputs() const { return &Exprs[0] + NumOutputs; }
1150
1151  // Input name iterator.
1152
1153  const std::string *begin_output_names() const {
1154    return &Names[0];
1155  }
1156
1157  const std::string *end_output_names() const {
1158    return &Names[0] + NumOutputs;
1159  }
1160
1161  // Child iterators
1162
1163  virtual child_iterator child_begin();
1164  virtual child_iterator child_end();
1165};
1166
1167}  // end namespace clang
1168
1169#endif
1170