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