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