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