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