Stmt.h revision cb66cff8fdf641f57f85dedb515a5f3240e3a9bb
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/PrettyPrinter.h"
21#include "clang/AST/StmtIterator.h"
22#include "clang/AST/DeclGroup.h"
23#include "llvm/ADT/SmallVector.h"
24#include "clang/AST/ASTContext.h"
25#include <string>
26using llvm::dyn_cast_or_null;
27
28namespace llvm {
29  class FoldingSetNodeID;
30}
31
32namespace clang {
33  class ASTContext;
34  class Expr;
35  class Decl;
36  class ParmVarDecl;
37  class QualType;
38  class IdentifierInfo;
39  class SourceManager;
40  class StringLiteral;
41  class SwitchStmt;
42
43  //===----------------------------------------------------------------------===//
44  // ExprIterator - Iterators for iterating over Stmt* arrays that contain
45  //  only Expr*.  This is needed because AST nodes use Stmt* arrays to store
46  //  references to children (to be compatible with StmtIterator).
47  //===----------------------------------------------------------------------===//
48
49  class Stmt;
50  class Expr;
51
52  class ExprIterator {
53    Stmt** I;
54  public:
55    ExprIterator(Stmt** i) : I(i) {}
56    ExprIterator() : I(0) {}
57    ExprIterator& operator++() { ++I; return *this; }
58    ExprIterator operator-(size_t i) { return I-i; }
59    ExprIterator operator+(size_t i) { return I+i; }
60    Expr* operator[](size_t idx);
61    // FIXME: Verify that this will correctly return a signed distance.
62    signed operator-(const ExprIterator& R) const { return I - R.I; }
63    Expr* operator*() const;
64    Expr* operator->() const;
65    bool operator==(const ExprIterator& R) const { return I == R.I; }
66    bool operator!=(const ExprIterator& R) const { return I != R.I; }
67    bool operator>(const ExprIterator& R) const { return I > R.I; }
68    bool operator>=(const ExprIterator& R) const { return I >= R.I; }
69  };
70
71  class ConstExprIterator {
72    const Stmt * const *I;
73  public:
74    ConstExprIterator(const Stmt * const *i) : I(i) {}
75    ConstExprIterator() : I(0) {}
76    ConstExprIterator& operator++() { ++I; return *this; }
77    ConstExprIterator operator+(size_t i) const { return I+i; }
78    ConstExprIterator operator-(size_t i) const { return I-i; }
79    const Expr * operator[](size_t idx) const;
80    signed operator-(const ConstExprIterator& R) const { return I - R.I; }
81    const Expr * operator*() const;
82    const Expr * operator->() const;
83    bool operator==(const ConstExprIterator& R) const { return I == R.I; }
84    bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
85    bool operator>(const ConstExprIterator& R) const { return I > R.I; }
86    bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
87  };
88
89//===----------------------------------------------------------------------===//
90// AST classes for statements.
91//===----------------------------------------------------------------------===//
92
93/// Stmt - This represents one statement.
94///
95class Stmt {
96public:
97  enum StmtClass {
98    NoStmtClass = 0,
99#define STMT(CLASS, PARENT) CLASS##Class,
100#define STMT_RANGE(BASE, FIRST, LAST) \
101        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
102#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
103        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
104#define ABSTRACT_STMT(STMT)
105#include "clang/AST/StmtNodes.inc"
106  };
107
108  // Make vanilla 'new' and 'delete' illegal for Stmts.
109protected:
110  void* operator new(size_t bytes) throw() {
111    assert(0 && "Stmts cannot be allocated with regular 'new'.");
112    return 0;
113  }
114  void operator delete(void* data) throw() {
115    assert(0 && "Stmts cannot be released with regular 'delete'.");
116  }
117
118  class StmtBitfields {
119    friend class Stmt;
120
121    /// \brief The statement class.
122    unsigned sClass : 8;
123  };
124  enum { NumStmtBits = 8 };
125
126  class CompoundStmtBitfields {
127    friend class CompoundStmt;
128    unsigned : NumStmtBits;
129
130    unsigned NumStmts : 32 - NumStmtBits;
131  };
132
133  class ExprBitfields {
134    friend class Expr;
135    friend class DeclRefExpr; // computeDependence
136    friend class InitListExpr; // ctor
137    friend class DesignatedInitExpr; // ctor
138    friend class BlockDeclRefExpr; // ctor
139    friend class ASTStmtReader; // deserialization
140    friend class CXXNewExpr; // ctor
141    friend class DependentScopeDeclRefExpr; // ctor
142    friend class CXXConstructExpr; // ctor
143    friend class CallExpr; // ctor
144    friend class OffsetOfExpr; // ctor
145    friend class ObjCMessageExpr; // ctor
146    friend class ShuffleVectorExpr; // ctor
147    friend class ParenListExpr; // ctor
148    friend class CXXUnresolvedConstructExpr; // ctor
149    friend class CXXDependentScopeMemberExpr; // ctor
150    friend class OverloadExpr; // ctor
151    unsigned : NumStmtBits;
152
153    unsigned ValueKind : 2;
154    unsigned ObjectKind : 2;
155    unsigned TypeDependent : 1;
156    unsigned ValueDependent : 1;
157    unsigned ContainsUnexpandedParameterPack : 1;
158  };
159  enum { NumExprBits = 15 };
160
161  class DeclRefExprBitfields {
162    friend class DeclRefExpr;
163    friend class ASTStmtReader; // deserialization
164    unsigned : NumExprBits;
165
166    unsigned HasQualifier : 1;
167    unsigned HasExplicitTemplateArgs : 1;
168  };
169
170  class CastExprBitfields {
171    friend class CastExpr;
172    unsigned : NumExprBits;
173
174    unsigned Kind : 6;
175    unsigned BasePathSize : 32 - 6 - NumExprBits;
176  };
177
178  class CallExprBitfields {
179    friend class CallExpr;
180    unsigned : NumExprBits;
181
182    unsigned NumPreArgs : 1;
183  };
184
185  union {
186    // FIXME: this is wasteful on 64-bit platforms.
187    void *Aligner;
188
189    StmtBitfields StmtBits;
190    CompoundStmtBitfields CompoundStmtBits;
191    ExprBitfields ExprBits;
192    DeclRefExprBitfields DeclRefExprBits;
193    CastExprBitfields CastExprBits;
194    CallExprBitfields CallExprBits;
195  };
196
197  friend class ASTStmtReader;
198
199public:
200  // Only allow allocation of Stmts using the allocator in ASTContext
201  // or by doing a placement new.
202  void* operator new(size_t bytes, ASTContext& C,
203                     unsigned alignment = 8) throw() {
204    return ::operator new(bytes, C, alignment);
205  }
206
207  void* operator new(size_t bytes, ASTContext* C,
208                     unsigned alignment = 8) throw() {
209    return ::operator new(bytes, *C, alignment);
210  }
211
212  void* operator new(size_t bytes, void* mem) throw() {
213    return mem;
214  }
215
216  void operator delete(void*, ASTContext&, unsigned) throw() { }
217  void operator delete(void*, ASTContext*, unsigned) throw() { }
218  void operator delete(void*, std::size_t) throw() { }
219  void operator delete(void*, void*) throw() { }
220
221public:
222  /// \brief A placeholder type used to construct an empty shell of a
223  /// type, that will be filled in later (e.g., by some
224  /// de-serialization).
225  struct EmptyShell { };
226
227protected:
228  /// \brief Construct an empty statement.
229  explicit Stmt(StmtClass SC, EmptyShell) {
230    StmtBits.sClass = SC;
231    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
232  }
233
234public:
235  Stmt(StmtClass SC) {
236    StmtBits.sClass = SC;
237    if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
238  }
239
240  StmtClass getStmtClass() const {
241    return static_cast<StmtClass>(StmtBits.sClass);
242  }
243  const char *getStmtClassName() const;
244
245  /// SourceLocation tokens are not useful in isolation - they are low level
246  /// value objects created/interpreted by SourceManager. We assume AST
247  /// clients will have a pointer to the respective SourceManager.
248  SourceRange getSourceRange() const;
249
250  SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
251  SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
252
253  // global temp stats (until we have a per-module visitor)
254  static void addStmtClass(const StmtClass s);
255  static bool CollectingStats(bool Enable = false);
256  static void PrintStats();
257
258  /// dump - This does a local dump of the specified AST fragment.  It dumps the
259  /// specified node and a few nodes underneath it, but not the whole subtree.
260  /// This is useful in a debugger.
261  void dump() const;
262  void dump(SourceManager &SM) const;
263  void dump(llvm::raw_ostream &OS, SourceManager &SM) const;
264
265  /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
266  void dumpAll() const;
267  void dumpAll(SourceManager &SM) const;
268
269  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
270  /// back to its original source language syntax.
271  void dumpPretty(ASTContext& Context) const;
272  void printPretty(llvm::raw_ostream &OS, PrinterHelper *Helper,
273                   const PrintingPolicy &Policy,
274                   unsigned Indentation = 0) const {
275    printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
276  }
277  void printPretty(llvm::raw_ostream &OS, ASTContext &Context,
278                   PrinterHelper *Helper,
279                   const PrintingPolicy &Policy,
280                   unsigned Indentation = 0) const;
281
282  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
283  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
284  void viewAST() const;
285
286  // Implement isa<T> support.
287  static bool classof(const Stmt *) { return true; }
288
289  /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
290  ///  contain implicit control-flow in the order their subexpressions
291  ///  are evaluated.  This predicate returns true if this statement has
292  ///  such implicit control-flow.  Such statements are also specially handled
293  ///  within CFGs.
294  bool hasImplicitControlFlow() const;
295
296  /// Child Iterators: All subclasses must implement 'children'
297  /// to permit easy iteration over the substatements/subexpessions of an
298  /// AST node.  This permits easy iteration over all nodes in the AST.
299  typedef StmtIterator       child_iterator;
300  typedef ConstStmtIterator  const_child_iterator;
301
302  typedef StmtRange          child_range;
303  typedef ConstStmtRange     const_child_range;
304
305  child_range children();
306  const_child_range children() const {
307    return const_cast<Stmt*>(this)->children();
308  }
309
310  child_iterator child_begin() { return children().first; }
311  child_iterator child_end() { return children().second; }
312
313  const_child_iterator child_begin() const { return children().first; }
314  const_child_iterator child_end() const { return children().second; }
315
316  /// \brief Produce a unique representation of the given statement.
317  ///
318  /// \brief ID once the profiling operation is complete, will contain
319  /// the unique representation of the given statement.
320  ///
321  /// \brief Context the AST context in which the statement resides
322  ///
323  /// \brief Canonical whether the profile should be based on the canonical
324  /// representation of this statement (e.g., where non-type template
325  /// parameters are identified by index/level rather than their
326  /// declaration pointers) or the exact representation of the statement as
327  /// written in the source.
328  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
329               bool Canonical);
330};
331
332/// DeclStmt - Adaptor class for mixing declarations with statements and
333/// expressions. For example, CompoundStmt mixes statements, expressions
334/// and declarations (variables, types). Another example is ForStmt, where
335/// the first statement can be an expression or a declaration.
336///
337class DeclStmt : public Stmt {
338  DeclGroupRef DG;
339  SourceLocation StartLoc, EndLoc;
340
341public:
342  DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
343           SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
344                                    StartLoc(startLoc), EndLoc(endLoc) {}
345
346  /// \brief Build an empty declaration statement.
347  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
348
349  /// isSingleDecl - This method returns true if this DeclStmt refers
350  /// to a single Decl.
351  bool isSingleDecl() const {
352    return DG.isSingleDecl();
353  }
354
355  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
356  Decl *getSingleDecl() { return DG.getSingleDecl(); }
357
358  const DeclGroupRef getDeclGroup() const { return DG; }
359  DeclGroupRef getDeclGroup() { return DG; }
360  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
361
362  SourceLocation getStartLoc() const { return StartLoc; }
363  void setStartLoc(SourceLocation L) { StartLoc = L; }
364  SourceLocation getEndLoc() const { return EndLoc; }
365  void setEndLoc(SourceLocation L) { EndLoc = L; }
366
367  SourceRange getSourceRange() const {
368    return SourceRange(StartLoc, EndLoc);
369  }
370
371  static bool classof(const Stmt *T) {
372    return T->getStmtClass() == DeclStmtClass;
373  }
374  static bool classof(const DeclStmt *) { return true; }
375
376  // Iterators over subexpressions.
377  child_range children() {
378    return child_range(child_iterator(DG.begin(), DG.end()),
379                       child_iterator(DG.end(), DG.end()));
380  }
381
382  typedef DeclGroupRef::iterator decl_iterator;
383  typedef DeclGroupRef::const_iterator const_decl_iterator;
384
385  decl_iterator decl_begin() { return DG.begin(); }
386  decl_iterator decl_end() { return DG.end(); }
387  const_decl_iterator decl_begin() const { return DG.begin(); }
388  const_decl_iterator decl_end() const { return DG.end(); }
389};
390
391/// NullStmt - This is the null statement ";": C99 6.8.3p3.
392///
393class NullStmt : public Stmt {
394  SourceLocation SemiLoc;
395
396  /// \brief If the null statement was preceded by an empty macro this is
397  /// its instantiation source location, e.g:
398  /// @code
399  ///   #define CALL(x)
400  ///   CALL(0);
401  /// @endcode
402  SourceLocation LeadingEmptyMacro;
403public:
404  NullStmt(SourceLocation L, SourceLocation LeadingEmptyMacro =SourceLocation())
405    : Stmt(NullStmtClass), SemiLoc(L), LeadingEmptyMacro(LeadingEmptyMacro) {}
406
407  /// \brief Build an empty null statement.
408  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
409
410  SourceLocation getSemiLoc() const { return SemiLoc; }
411  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
412
413  bool hasLeadingEmptyMacro() const { return LeadingEmptyMacro.isValid(); }
414  SourceLocation getLeadingEmptyMacroLoc() const { return LeadingEmptyMacro; }
415
416  SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
417
418  static bool classof(const Stmt *T) {
419    return T->getStmtClass() == NullStmtClass;
420  }
421  static bool classof(const NullStmt *) { return true; }
422
423  child_range children() { return child_range(); }
424
425  friend class ASTStmtReader;
426  friend class ASTStmtWriter;
427};
428
429/// CompoundStmt - This represents a group of statements like { stmt stmt }.
430///
431class CompoundStmt : public Stmt {
432  Stmt** Body;
433  SourceLocation LBracLoc, RBracLoc;
434public:
435  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
436               SourceLocation LB, SourceLocation RB)
437  : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
438    CompoundStmtBits.NumStmts = NumStmts;
439    assert(CompoundStmtBits.NumStmts == NumStmts &&
440           "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
441
442    if (NumStmts == 0) {
443      Body = 0;
444      return;
445    }
446
447    Body = new (C) Stmt*[NumStmts];
448    memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
449  }
450
451  // \brief Build an empty compound statement.
452  explicit CompoundStmt(EmptyShell Empty)
453    : Stmt(CompoundStmtClass, Empty), Body(0) {
454    CompoundStmtBits.NumStmts = 0;
455  }
456
457  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
458
459  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
460  unsigned size() const { return CompoundStmtBits.NumStmts; }
461
462  typedef Stmt** body_iterator;
463  body_iterator body_begin() { return Body; }
464  body_iterator body_end() { return Body + size(); }
465  Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
466
467  void setLastStmt(Stmt *S) {
468    assert(!body_empty() && "setLastStmt");
469    Body[size()-1] = S;
470  }
471
472  typedef Stmt* const * const_body_iterator;
473  const_body_iterator body_begin() const { return Body; }
474  const_body_iterator body_end() const { return Body + size(); }
475  const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
476
477  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
478  reverse_body_iterator body_rbegin() {
479    return reverse_body_iterator(body_end());
480  }
481  reverse_body_iterator body_rend() {
482    return reverse_body_iterator(body_begin());
483  }
484
485  typedef std::reverse_iterator<const_body_iterator>
486          const_reverse_body_iterator;
487
488  const_reverse_body_iterator body_rbegin() const {
489    return const_reverse_body_iterator(body_end());
490  }
491
492  const_reverse_body_iterator body_rend() const {
493    return const_reverse_body_iterator(body_begin());
494  }
495
496  SourceRange getSourceRange() const {
497    return SourceRange(LBracLoc, RBracLoc);
498  }
499
500  SourceLocation getLBracLoc() const { return LBracLoc; }
501  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
502  SourceLocation getRBracLoc() const { return RBracLoc; }
503  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
504
505  static bool classof(const Stmt *T) {
506    return T->getStmtClass() == CompoundStmtClass;
507  }
508  static bool classof(const CompoundStmt *) { return true; }
509
510  // Iterators
511  child_range children() {
512    return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
513  }
514};
515
516// SwitchCase is the base class for CaseStmt and DefaultStmt,
517class SwitchCase : public Stmt {
518protected:
519  // A pointer to the following CaseStmt or DefaultStmt class,
520  // used by SwitchStmt.
521  SwitchCase *NextSwitchCase;
522
523  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
524
525public:
526  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
527
528  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
529
530  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
531
532  Stmt *getSubStmt();
533  const Stmt *getSubStmt() const {
534    return const_cast<SwitchCase*>(this)->getSubStmt();
535  }
536
537  SourceRange getSourceRange() const { return SourceRange(); }
538
539  static bool classof(const Stmt *T) {
540    return T->getStmtClass() == CaseStmtClass ||
541           T->getStmtClass() == DefaultStmtClass;
542  }
543  static bool classof(const SwitchCase *) { return true; }
544};
545
546class CaseStmt : public SwitchCase {
547  enum { LHS, RHS, SUBSTMT, END_EXPR };
548  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
549                             // GNU "case 1 ... 4" extension
550  SourceLocation CaseLoc;
551  SourceLocation EllipsisLoc;
552  SourceLocation ColonLoc;
553public:
554  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
555           SourceLocation ellipsisLoc, SourceLocation colonLoc)
556    : SwitchCase(CaseStmtClass) {
557    SubExprs[SUBSTMT] = 0;
558    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
559    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
560    CaseLoc = caseLoc;
561    EllipsisLoc = ellipsisLoc;
562    ColonLoc = colonLoc;
563  }
564
565  /// \brief Build an empty switch case statement.
566  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
567
568  SourceLocation getCaseLoc() const { return CaseLoc; }
569  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
570  SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
571  void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
572  SourceLocation getColonLoc() const { return ColonLoc; }
573  void setColonLoc(SourceLocation L) { ColonLoc = L; }
574
575  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
576  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
577  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
578
579  const Expr *getLHS() const {
580    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
581  }
582  const Expr *getRHS() const {
583    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
584  }
585  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
586
587  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
588  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
589  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
590
591
592  SourceRange getSourceRange() const {
593    // Handle deeply nested case statements with iteration instead of recursion.
594    const CaseStmt *CS = this;
595    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
596      CS = CS2;
597
598    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
599  }
600  static bool classof(const Stmt *T) {
601    return T->getStmtClass() == CaseStmtClass;
602  }
603  static bool classof(const CaseStmt *) { return true; }
604
605  // Iterators
606  child_range children() {
607    return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
608  }
609};
610
611class DefaultStmt : public SwitchCase {
612  Stmt* SubStmt;
613  SourceLocation DefaultLoc;
614  SourceLocation ColonLoc;
615public:
616  DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
617    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
618    ColonLoc(CL) {}
619
620  /// \brief Build an empty default statement.
621  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
622
623  Stmt *getSubStmt() { return SubStmt; }
624  const Stmt *getSubStmt() const { return SubStmt; }
625  void setSubStmt(Stmt *S) { SubStmt = S; }
626
627  SourceLocation getDefaultLoc() const { return DefaultLoc; }
628  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
629  SourceLocation getColonLoc() const { return ColonLoc; }
630  void setColonLoc(SourceLocation L) { ColonLoc = L; }
631
632  SourceRange getSourceRange() const {
633    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
634  }
635  static bool classof(const Stmt *T) {
636    return T->getStmtClass() == DefaultStmtClass;
637  }
638  static bool classof(const DefaultStmt *) { return true; }
639
640  // Iterators
641  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
642};
643
644
645/// LabelStmt - Represents a label, which has a substatement.  For example:
646///    foo: return;
647///
648class LabelStmt : public Stmt {
649  LabelDecl *TheDecl;
650  Stmt *SubStmt;
651  SourceLocation IdentLoc;
652public:
653  LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
654    : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
655  }
656
657  // \brief Build an empty label statement.
658  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
659
660  SourceLocation getIdentLoc() const { return IdentLoc; }
661  LabelDecl *getDecl() const { return TheDecl; }
662  void setDecl(LabelDecl *D) { TheDecl = D; }
663  const char *getName() const;
664  Stmt *getSubStmt() { return SubStmt; }
665  const Stmt *getSubStmt() const { return SubStmt; }
666  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
667  void setSubStmt(Stmt *SS) { SubStmt = SS; }
668
669  SourceRange getSourceRange() const {
670    return SourceRange(IdentLoc, SubStmt->getLocEnd());
671  }
672  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
673
674  static bool classof(const Stmt *T) {
675    return T->getStmtClass() == LabelStmtClass;
676  }
677  static bool classof(const LabelStmt *) { return true; }
678};
679
680
681/// IfStmt - This represents an if/then/else.
682///
683class IfStmt : public Stmt {
684  enum { VAR, COND, THEN, ELSE, END_EXPR };
685  Stmt* SubExprs[END_EXPR];
686
687  SourceLocation IfLoc;
688  SourceLocation ElseLoc;
689
690public:
691  IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
692         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
693
694  /// \brief Build an empty if/then/else statement
695  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
696
697  /// \brief Retrieve the variable declared in this "if" statement, if any.
698  ///
699  /// In the following example, "x" is the condition variable.
700  /// \code
701  /// if (int x = foo()) {
702  ///   printf("x is %d", x);
703  /// }
704  /// \endcode
705  VarDecl *getConditionVariable() const;
706  void setConditionVariable(ASTContext &C, VarDecl *V);
707
708  /// If this IfStmt has a condition variable, return the faux DeclStmt
709  /// associated with the creation of that condition variable.
710  const DeclStmt *getConditionVariableDeclStmt() const {
711    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
712  }
713
714  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
715  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
716  const Stmt *getThen() const { return SubExprs[THEN]; }
717  void setThen(Stmt *S) { SubExprs[THEN] = S; }
718  const Stmt *getElse() const { return SubExprs[ELSE]; }
719  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
720
721  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
722  Stmt *getThen() { return SubExprs[THEN]; }
723  Stmt *getElse() { return SubExprs[ELSE]; }
724
725  SourceLocation getIfLoc() const { return IfLoc; }
726  void setIfLoc(SourceLocation L) { IfLoc = L; }
727  SourceLocation getElseLoc() const { return ElseLoc; }
728  void setElseLoc(SourceLocation L) { ElseLoc = L; }
729
730  SourceRange getSourceRange() const {
731    if (SubExprs[ELSE])
732      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
733    else
734      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
735  }
736
737  // Iterators over subexpressions.  The iterators will include iterating
738  // over the initialization expression referenced by the condition variable.
739  child_range children() {
740    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
741  }
742
743  static bool classof(const Stmt *T) {
744    return T->getStmtClass() == IfStmtClass;
745  }
746  static bool classof(const IfStmt *) { return true; }
747};
748
749/// SwitchStmt - This represents a 'switch' stmt.
750///
751class SwitchStmt : public Stmt {
752  enum { VAR, COND, BODY, END_EXPR };
753  Stmt* SubExprs[END_EXPR];
754  // This points to a linked list of case and default statements.
755  SwitchCase *FirstCase;
756  SourceLocation SwitchLoc;
757
758  /// If the SwitchStmt is a switch on an enum value, this records whether
759  /// all the enum values were covered by CaseStmts.  This value is meant to
760  /// be a hint for possible clients.
761  unsigned AllEnumCasesCovered : 1;
762
763public:
764  SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
765
766  /// \brief Build a empty switch statement.
767  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
768
769  /// \brief Retrieve the variable declared in this "switch" statement, if any.
770  ///
771  /// In the following example, "x" is the condition variable.
772  /// \code
773  /// switch (int x = foo()) {
774  ///   case 0: break;
775  ///   // ...
776  /// }
777  /// \endcode
778  VarDecl *getConditionVariable() const;
779  void setConditionVariable(ASTContext &C, VarDecl *V);
780
781  /// If this SwitchStmt has a condition variable, return the faux DeclStmt
782  /// associated with the creation of that condition variable.
783  const DeclStmt *getConditionVariableDeclStmt() const {
784    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
785  }
786
787  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
788  const Stmt *getBody() const { return SubExprs[BODY]; }
789  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
790
791  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
792  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
793  Stmt *getBody() { return SubExprs[BODY]; }
794  void setBody(Stmt *S) { SubExprs[BODY] = S; }
795  SwitchCase *getSwitchCaseList() { return FirstCase; }
796
797  /// \brief Set the case list for this switch statement.
798  ///
799  /// The caller is responsible for incrementing the retain counts on
800  /// all of the SwitchCase statements in this list.
801  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
802
803  SourceLocation getSwitchLoc() const { return SwitchLoc; }
804  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
805
806  void setBody(Stmt *S, SourceLocation SL) {
807    SubExprs[BODY] = S;
808    SwitchLoc = SL;
809  }
810  void addSwitchCase(SwitchCase *SC) {
811    assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
812    SC->setNextSwitchCase(FirstCase);
813    FirstCase = SC;
814  }
815
816  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
817  /// switch over an enum value then all cases have been explicitly covered.
818  void setAllEnumCasesCovered() {
819    AllEnumCasesCovered = 1;
820  }
821
822  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
823  /// have been explicitly covered.
824  bool isAllEnumCasesCovered() const {
825    return (bool) AllEnumCasesCovered;
826  }
827
828  SourceRange getSourceRange() const {
829    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
830  }
831  // Iterators
832  child_range children() {
833    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
834  }
835
836  static bool classof(const Stmt *T) {
837    return T->getStmtClass() == SwitchStmtClass;
838  }
839  static bool classof(const SwitchStmt *) { return true; }
840};
841
842
843/// WhileStmt - This represents a 'while' stmt.
844///
845class WhileStmt : public Stmt {
846  enum { VAR, COND, BODY, END_EXPR };
847  Stmt* SubExprs[END_EXPR];
848  SourceLocation WhileLoc;
849public:
850  WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
851            SourceLocation WL);
852
853  /// \brief Build an empty while statement.
854  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
855
856  /// \brief Retrieve the variable declared in this "while" statement, if any.
857  ///
858  /// In the following example, "x" is the condition variable.
859  /// \code
860  /// while (int x = random()) {
861  ///   // ...
862  /// }
863  /// \endcode
864  VarDecl *getConditionVariable() const;
865  void setConditionVariable(ASTContext &C, VarDecl *V);
866
867  /// If this WhileStmt has a condition variable, return the faux DeclStmt
868  /// associated with the creation of that condition variable.
869  const DeclStmt *getConditionVariableDeclStmt() const {
870    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
871  }
872
873  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
874  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
875  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
876  Stmt *getBody() { return SubExprs[BODY]; }
877  const Stmt *getBody() const { return SubExprs[BODY]; }
878  void setBody(Stmt *S) { SubExprs[BODY] = S; }
879
880  SourceLocation getWhileLoc() const { return WhileLoc; }
881  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
882
883  SourceRange getSourceRange() const {
884    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
885  }
886  static bool classof(const Stmt *T) {
887    return T->getStmtClass() == WhileStmtClass;
888  }
889  static bool classof(const WhileStmt *) { return true; }
890
891  // Iterators
892  child_range children() {
893    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
894  }
895};
896
897/// DoStmt - This represents a 'do/while' stmt.
898///
899class DoStmt : public Stmt {
900  enum { BODY, COND, END_EXPR };
901  Stmt* SubExprs[END_EXPR];
902  SourceLocation DoLoc;
903  SourceLocation WhileLoc;
904  SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
905
906public:
907  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
908         SourceLocation RP)
909    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
910    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
911    SubExprs[BODY] = body;
912  }
913
914  /// \brief Build an empty do-while statement.
915  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
916
917  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
918  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
919  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
920  Stmt *getBody() { return SubExprs[BODY]; }
921  const Stmt *getBody() const { return SubExprs[BODY]; }
922  void setBody(Stmt *S) { SubExprs[BODY] = S; }
923
924  SourceLocation getDoLoc() const { return DoLoc; }
925  void setDoLoc(SourceLocation L) { DoLoc = L; }
926  SourceLocation getWhileLoc() const { return WhileLoc; }
927  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
928
929  SourceLocation getRParenLoc() const { return RParenLoc; }
930  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
931
932  SourceRange getSourceRange() const {
933    return SourceRange(DoLoc, RParenLoc);
934  }
935  static bool classof(const Stmt *T) {
936    return T->getStmtClass() == DoStmtClass;
937  }
938  static bool classof(const DoStmt *) { return true; }
939
940  // Iterators
941  child_range children() {
942    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
943  }
944};
945
946
947/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
948/// the init/cond/inc parts of the ForStmt will be null if they were not
949/// specified in the source.
950///
951class ForStmt : public Stmt {
952  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
953  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
954  SourceLocation ForLoc;
955  SourceLocation LParenLoc, RParenLoc;
956
957public:
958  ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
959          Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
960
961  /// \brief Build an empty for statement.
962  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
963
964  Stmt *getInit() { return SubExprs[INIT]; }
965
966  /// \brief Retrieve the variable declared in this "for" statement, if any.
967  ///
968  /// In the following example, "y" is the condition variable.
969  /// \code
970  /// for (int x = random(); int y = mangle(x); ++x) {
971  ///   // ...
972  /// }
973  /// \endcode
974  VarDecl *getConditionVariable() const;
975  void setConditionVariable(ASTContext &C, VarDecl *V);
976
977  /// If this ForStmt has a condition variable, return the faux DeclStmt
978  /// associated with the creation of that condition variable.
979  const DeclStmt *getConditionVariableDeclStmt() const {
980    return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
981  }
982
983  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
984  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
985  Stmt *getBody() { return SubExprs[BODY]; }
986
987  const Stmt *getInit() const { return SubExprs[INIT]; }
988  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
989  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
990  const Stmt *getBody() const { return SubExprs[BODY]; }
991
992  void setInit(Stmt *S) { SubExprs[INIT] = S; }
993  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
994  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
995  void setBody(Stmt *S) { SubExprs[BODY] = S; }
996
997  SourceLocation getForLoc() const { return ForLoc; }
998  void setForLoc(SourceLocation L) { ForLoc = L; }
999  SourceLocation getLParenLoc() const { return LParenLoc; }
1000  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1001  SourceLocation getRParenLoc() const { return RParenLoc; }
1002  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1003
1004  SourceRange getSourceRange() const {
1005    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1006  }
1007  static bool classof(const Stmt *T) {
1008    return T->getStmtClass() == ForStmtClass;
1009  }
1010  static bool classof(const ForStmt *) { return true; }
1011
1012  // Iterators
1013  child_range children() {
1014    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1015  }
1016};
1017
1018/// GotoStmt - This represents a direct goto.
1019///
1020class GotoStmt : public Stmt {
1021  LabelDecl *Label;
1022  SourceLocation GotoLoc;
1023  SourceLocation LabelLoc;
1024public:
1025  GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1026    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1027
1028  /// \brief Build an empty goto statement.
1029  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1030
1031  LabelDecl *getLabel() const { return Label; }
1032  void setLabel(LabelDecl *D) { Label = D; }
1033
1034  SourceLocation getGotoLoc() const { return GotoLoc; }
1035  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1036  SourceLocation getLabelLoc() const { return LabelLoc; }
1037  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1038
1039  SourceRange getSourceRange() const {
1040    return SourceRange(GotoLoc, LabelLoc);
1041  }
1042  static bool classof(const Stmt *T) {
1043    return T->getStmtClass() == GotoStmtClass;
1044  }
1045  static bool classof(const GotoStmt *) { return true; }
1046
1047  // Iterators
1048  child_range children() { return child_range(); }
1049};
1050
1051/// IndirectGotoStmt - This represents an indirect goto.
1052///
1053class IndirectGotoStmt : public Stmt {
1054  SourceLocation GotoLoc;
1055  SourceLocation StarLoc;
1056  Stmt *Target;
1057public:
1058  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1059                   Expr *target)
1060    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1061      Target((Stmt*)target) {}
1062
1063  /// \brief Build an empty indirect goto statement.
1064  explicit IndirectGotoStmt(EmptyShell Empty)
1065    : Stmt(IndirectGotoStmtClass, Empty) { }
1066
1067  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1068  SourceLocation getGotoLoc() const { return GotoLoc; }
1069  void setStarLoc(SourceLocation L) { StarLoc = L; }
1070  SourceLocation getStarLoc() const { return StarLoc; }
1071
1072  Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1073  const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1074  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1075
1076  /// getConstantTarget - Returns the fixed target of this indirect
1077  /// goto, if one exists.
1078  LabelDecl *getConstantTarget();
1079  const LabelDecl *getConstantTarget() const {
1080    return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1081  }
1082
1083  SourceRange getSourceRange() const {
1084    return SourceRange(GotoLoc, Target->getLocEnd());
1085  }
1086
1087  static bool classof(const Stmt *T) {
1088    return T->getStmtClass() == IndirectGotoStmtClass;
1089  }
1090  static bool classof(const IndirectGotoStmt *) { return true; }
1091
1092  // Iterators
1093  child_range children() { return child_range(&Target, &Target+1); }
1094};
1095
1096
1097/// ContinueStmt - This represents a continue.
1098///
1099class ContinueStmt : public Stmt {
1100  SourceLocation ContinueLoc;
1101public:
1102  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1103
1104  /// \brief Build an empty continue statement.
1105  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1106
1107  SourceLocation getContinueLoc() const { return ContinueLoc; }
1108  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1109
1110  SourceRange getSourceRange() const {
1111    return SourceRange(ContinueLoc);
1112  }
1113
1114  static bool classof(const Stmt *T) {
1115    return T->getStmtClass() == ContinueStmtClass;
1116  }
1117  static bool classof(const ContinueStmt *) { return true; }
1118
1119  // Iterators
1120  child_range children() { return child_range(); }
1121};
1122
1123/// BreakStmt - This represents a break.
1124///
1125class BreakStmt : public Stmt {
1126  SourceLocation BreakLoc;
1127public:
1128  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1129
1130  /// \brief Build an empty break statement.
1131  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1132
1133  SourceLocation getBreakLoc() const { return BreakLoc; }
1134  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1135
1136  SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
1137
1138  static bool classof(const Stmt *T) {
1139    return T->getStmtClass() == BreakStmtClass;
1140  }
1141  static bool classof(const BreakStmt *) { return true; }
1142
1143  // Iterators
1144  child_range children() { return child_range(); }
1145};
1146
1147
1148/// ReturnStmt - This represents a return, optionally of an expression:
1149///   return;
1150///   return 4;
1151///
1152/// Note that GCC allows return with no argument in a function declared to
1153/// return a value, and it allows returning a value in functions declared to
1154/// return void.  We explicitly model this in the AST, which means you can't
1155/// depend on the return type of the function and the presence of an argument.
1156///
1157class ReturnStmt : public Stmt {
1158  Stmt *RetExpr;
1159  SourceLocation RetLoc;
1160  const VarDecl *NRVOCandidate;
1161
1162public:
1163  ReturnStmt(SourceLocation RL)
1164    : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1165
1166  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1167    : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1168      NRVOCandidate(NRVOCandidate) {}
1169
1170  /// \brief Build an empty return expression.
1171  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1172
1173  const Expr *getRetValue() const;
1174  Expr *getRetValue();
1175  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1176
1177  SourceLocation getReturnLoc() const { return RetLoc; }
1178  void setReturnLoc(SourceLocation L) { RetLoc = L; }
1179
1180  /// \brief Retrieve the variable that might be used for the named return
1181  /// value optimization.
1182  ///
1183  /// The optimization itself can only be performed if the variable is
1184  /// also marked as an NRVO object.
1185  const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1186  void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1187
1188  SourceRange getSourceRange() const;
1189
1190  static bool classof(const Stmt *T) {
1191    return T->getStmtClass() == ReturnStmtClass;
1192  }
1193  static bool classof(const ReturnStmt *) { return true; }
1194
1195  // Iterators
1196  child_range children() {
1197    if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1198    return child_range();
1199  }
1200};
1201
1202/// AsmStmt - This represents a GNU inline-assembly statement extension.
1203///
1204class AsmStmt : public Stmt {
1205  SourceLocation AsmLoc, RParenLoc;
1206  StringLiteral *AsmStr;
1207
1208  bool IsSimple;
1209  bool IsVolatile;
1210  bool MSAsm;
1211
1212  unsigned NumOutputs;
1213  unsigned NumInputs;
1214  unsigned NumClobbers;
1215
1216  // FIXME: If we wanted to, we could allocate all of these in one big array.
1217  IdentifierInfo **Names;
1218  StringLiteral **Constraints;
1219  Stmt **Exprs;
1220  StringLiteral **Clobbers;
1221
1222public:
1223  AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1224          bool msasm, unsigned numoutputs, unsigned numinputs,
1225          IdentifierInfo **names, StringLiteral **constraints,
1226          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1227          StringLiteral **clobbers, SourceLocation rparenloc);
1228
1229  /// \brief Build an empty inline-assembly statement.
1230  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1231    Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1232
1233  SourceLocation getAsmLoc() const { return AsmLoc; }
1234  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1235  SourceLocation getRParenLoc() const { return RParenLoc; }
1236  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1237
1238  bool isVolatile() const { return IsVolatile; }
1239  void setVolatile(bool V) { IsVolatile = V; }
1240  bool isSimple() const { return IsSimple; }
1241  void setSimple(bool V) { IsSimple = V; }
1242  bool isMSAsm() const { return MSAsm; }
1243  void setMSAsm(bool V) { MSAsm = V; }
1244
1245  //===--- Asm String Analysis ---===//
1246
1247  const StringLiteral *getAsmString() const { return AsmStr; }
1248  StringLiteral *getAsmString() { return AsmStr; }
1249  void setAsmString(StringLiteral *E) { AsmStr = E; }
1250
1251  /// AsmStringPiece - this is part of a decomposed asm string specification
1252  /// (for use with the AnalyzeAsmString function below).  An asm string is
1253  /// considered to be a concatenation of these parts.
1254  class AsmStringPiece {
1255  public:
1256    enum Kind {
1257      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1258      Operand  // Operand reference, with optional modifier %c4.
1259    };
1260  private:
1261    Kind MyKind;
1262    std::string Str;
1263    unsigned OperandNo;
1264  public:
1265    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1266    AsmStringPiece(unsigned OpNo, char Modifier)
1267      : MyKind(Operand), Str(), OperandNo(OpNo) {
1268      Str += Modifier;
1269    }
1270
1271    bool isString() const { return MyKind == String; }
1272    bool isOperand() const { return MyKind == Operand; }
1273
1274    const std::string &getString() const {
1275      assert(isString());
1276      return Str;
1277    }
1278
1279    unsigned getOperandNo() const {
1280      assert(isOperand());
1281      return OperandNo;
1282    }
1283
1284    /// getModifier - Get the modifier for this operand, if present.  This
1285    /// returns '\0' if there was no modifier.
1286    char getModifier() const {
1287      assert(isOperand());
1288      return Str[0];
1289    }
1290  };
1291
1292  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1293  /// it into pieces.  If the asm string is erroneous, emit errors and return
1294  /// true, otherwise return false.  This handles canonicalization and
1295  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1296  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1297  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1298                            ASTContext &C, unsigned &DiagOffs) const;
1299
1300
1301  //===--- Output operands ---===//
1302
1303  unsigned getNumOutputs() const { return NumOutputs; }
1304
1305  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1306    return Names[i];
1307  }
1308
1309  llvm::StringRef getOutputName(unsigned i) const {
1310    if (IdentifierInfo *II = getOutputIdentifier(i))
1311      return II->getName();
1312
1313    return llvm::StringRef();
1314  }
1315
1316  /// getOutputConstraint - Return the constraint string for the specified
1317  /// output operand.  All output constraints are known to be non-empty (either
1318  /// '=' or '+').
1319  llvm::StringRef getOutputConstraint(unsigned i) const;
1320
1321  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1322    return Constraints[i];
1323  }
1324  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1325    return Constraints[i];
1326  }
1327
1328  Expr *getOutputExpr(unsigned i);
1329
1330  const Expr *getOutputExpr(unsigned i) const {
1331    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1332  }
1333
1334  /// isOutputPlusConstraint - Return true if the specified output constraint
1335  /// is a "+" constraint (which is both an input and an output) or false if it
1336  /// is an "=" constraint (just an output).
1337  bool isOutputPlusConstraint(unsigned i) const {
1338    return getOutputConstraint(i)[0] == '+';
1339  }
1340
1341  /// getNumPlusOperands - Return the number of output operands that have a "+"
1342  /// constraint.
1343  unsigned getNumPlusOperands() const;
1344
1345  //===--- Input operands ---===//
1346
1347  unsigned getNumInputs() const { return NumInputs; }
1348
1349  IdentifierInfo *getInputIdentifier(unsigned i) const {
1350    return Names[i + NumOutputs];
1351  }
1352
1353  llvm::StringRef getInputName(unsigned i) const {
1354    if (IdentifierInfo *II = getInputIdentifier(i))
1355      return II->getName();
1356
1357    return llvm::StringRef();
1358  }
1359
1360  /// getInputConstraint - Return the specified input constraint.  Unlike output
1361  /// constraints, these can be empty.
1362  llvm::StringRef getInputConstraint(unsigned i) const;
1363
1364  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1365    return Constraints[i + NumOutputs];
1366  }
1367  StringLiteral *getInputConstraintLiteral(unsigned i) {
1368    return Constraints[i + NumOutputs];
1369  }
1370
1371  Expr *getInputExpr(unsigned i);
1372  void setInputExpr(unsigned i, Expr *E);
1373
1374  const Expr *getInputExpr(unsigned i) const {
1375    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1376  }
1377
1378  void setOutputsAndInputsAndClobbers(ASTContext &C,
1379                                      IdentifierInfo **Names,
1380                                      StringLiteral **Constraints,
1381                                      Stmt **Exprs,
1382                                      unsigned NumOutputs,
1383                                      unsigned NumInputs,
1384                                      StringLiteral **Clobbers,
1385                                      unsigned NumClobbers);
1386
1387  //===--- Other ---===//
1388
1389  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1390  /// translate this into a numeric value needed to reference the same operand.
1391  /// This returns -1 if the operand name is invalid.
1392  int getNamedOperand(llvm::StringRef SymbolicName) const;
1393
1394  unsigned getNumClobbers() const { return NumClobbers; }
1395  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1396  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1397
1398  SourceRange getSourceRange() const {
1399    return SourceRange(AsmLoc, RParenLoc);
1400  }
1401
1402  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1403  static bool classof(const AsmStmt *) { return true; }
1404
1405  // Input expr iterators.
1406
1407  typedef ExprIterator inputs_iterator;
1408  typedef ConstExprIterator const_inputs_iterator;
1409
1410  inputs_iterator begin_inputs() {
1411    return &Exprs[0] + NumOutputs;
1412  }
1413
1414  inputs_iterator end_inputs() {
1415    return &Exprs[0] + NumOutputs + NumInputs;
1416  }
1417
1418  const_inputs_iterator begin_inputs() const {
1419    return &Exprs[0] + NumOutputs;
1420  }
1421
1422  const_inputs_iterator end_inputs() const {
1423    return &Exprs[0] + NumOutputs + NumInputs;
1424  }
1425
1426  // Output expr iterators.
1427
1428  typedef ExprIterator outputs_iterator;
1429  typedef ConstExprIterator const_outputs_iterator;
1430
1431  outputs_iterator begin_outputs() {
1432    return &Exprs[0];
1433  }
1434  outputs_iterator end_outputs() {
1435    return &Exprs[0] + NumOutputs;
1436  }
1437
1438  const_outputs_iterator begin_outputs() const {
1439    return &Exprs[0];
1440  }
1441  const_outputs_iterator end_outputs() const {
1442    return &Exprs[0] + NumOutputs;
1443  }
1444
1445  child_range children() {
1446    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1447  }
1448};
1449
1450class SEHExceptStmt : public Stmt {
1451  SourceLocation  Loc;
1452  Stmt           *Children[2];
1453
1454  enum { FILTER_EXPR, BLOCK };
1455
1456  SEHExceptStmt(SourceLocation Loc,
1457                Expr *FilterExpr,
1458                Stmt *Block);
1459
1460public:
1461  static SEHExceptStmt* Create(ASTContext &C,
1462                               SourceLocation ExceptLoc,
1463                               Expr *FilterExpr,
1464                               Stmt *Block);
1465  SourceRange getSourceRange() const {
1466    return SourceRange(getExceptLoc(), getEndLoc());
1467  }
1468
1469  SourceLocation getExceptLoc() const { return Loc; }
1470  SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1471
1472  Expr *getFilterExpr() const { return reinterpret_cast<Expr*>(Children[FILTER_EXPR]); }
1473  CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Children[BLOCK]); }
1474
1475  child_range children() {
1476    return child_range(Children,Children+2);
1477  }
1478
1479  static bool classof(const Stmt *T) {
1480    return T->getStmtClass() == SEHExceptStmtClass;
1481  }
1482
1483  static bool classof(SEHExceptStmt *) { return true; }
1484
1485};
1486
1487class SEHFinallyStmt : public Stmt {
1488  SourceLocation  Loc;
1489  Stmt           *Block;
1490
1491  SEHFinallyStmt(SourceLocation Loc,
1492                 Stmt *Block);
1493
1494public:
1495  static SEHFinallyStmt* Create(ASTContext &C,
1496                                SourceLocation FinallyLoc,
1497                                Stmt *Block);
1498
1499  SourceRange getSourceRange() const {
1500    return SourceRange(getFinallyLoc(), getEndLoc());
1501  }
1502
1503  SourceLocation getFinallyLoc() const { return Loc; }
1504  SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1505
1506  CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1507
1508  child_range children() {
1509    return child_range(&Block,&Block+1);
1510  }
1511
1512  static bool classof(const Stmt *T) {
1513    return T->getStmtClass() == SEHFinallyStmtClass;
1514  }
1515
1516  static bool classof(SEHFinallyStmt *) { return true; }
1517
1518};
1519
1520class SEHTryStmt : public Stmt {
1521  bool            IsCXXTry;
1522  SourceLocation  TryLoc;
1523  Stmt           *Children[2];
1524
1525  enum { TRY = 0, HANDLER = 1 };
1526
1527  SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1528             SourceLocation TryLoc,
1529             Stmt *TryBlock,
1530             Stmt *Handler);
1531
1532public:
1533  static SEHTryStmt* Create(ASTContext &C,
1534                            bool isCXXTry,
1535                            SourceLocation TryLoc,
1536                            Stmt *TryBlock,
1537                            Stmt *Handler);
1538
1539  SourceRange getSourceRange() const {
1540    return SourceRange(getTryLoc(), getEndLoc());
1541  }
1542
1543  SourceLocation getTryLoc() const { return TryLoc; }
1544  SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1545
1546  bool getIsCXXTry() const { return IsCXXTry; }
1547  CompoundStmt* getTryBlock() const { return llvm::cast<CompoundStmt>(Children[TRY]); }
1548  Stmt *getHandler() const { return Children[HANDLER]; }
1549
1550  /// Returns 0 if not defined
1551  SEHExceptStmt  *getExceptHandler() const;
1552  SEHFinallyStmt *getFinallyHandler() const;
1553
1554  child_range children() {
1555    return child_range(Children,Children+2);
1556  }
1557
1558  static bool classof(const Stmt *T) {
1559    return T->getStmtClass() == SEHTryStmtClass;
1560  }
1561
1562  static bool classof(SEHTryStmt *) { return true; }
1563
1564};
1565
1566}  // end namespace clang
1567
1568#endif
1569