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