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