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