Stmt.h revision ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298
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 Whether the null statement was preceded by an empty macro, e.g:
387  /// @code
388  ///   #define CALL(x)
389  ///   CALL(0);
390  /// @endcode
391  bool LeadingEmptyMacro;
392public:
393  NullStmt(SourceLocation L, bool LeadingEmptyMacro = false)
394    : Stmt(NullStmtClass), SemiLoc(L), LeadingEmptyMacro(LeadingEmptyMacro) {}
395
396  /// \brief Build an empty null statement.
397  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
398
399  SourceLocation getSemiLoc() const { return SemiLoc; }
400  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
401
402  bool hasLeadingEmptyMacro() const { return LeadingEmptyMacro; }
403
404  SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
405
406  static bool classof(const Stmt *T) {
407    return T->getStmtClass() == NullStmtClass;
408  }
409  static bool classof(const NullStmt *) { return true; }
410
411  child_range children() { return child_range(); }
412
413  friend class ASTStmtReader;
414  friend class ASTStmtWriter;
415};
416
417/// CompoundStmt - This represents a group of statements like { stmt stmt }.
418///
419class CompoundStmt : public Stmt {
420  Stmt** Body;
421  SourceLocation LBracLoc, RBracLoc;
422public:
423  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
424               SourceLocation LB, SourceLocation RB)
425  : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
426    CompoundStmtBits.NumStmts = NumStmts;
427
428    if (NumStmts == 0) {
429      Body = 0;
430      return;
431    }
432
433    Body = new (C) Stmt*[NumStmts];
434    memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
435  }
436
437  // \brief Build an empty compound statement.
438  explicit CompoundStmt(EmptyShell Empty)
439    : Stmt(CompoundStmtClass, Empty), Body(0) {
440    CompoundStmtBits.NumStmts = 0;
441  }
442
443  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
444
445  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
446  unsigned size() const { return CompoundStmtBits.NumStmts; }
447
448  typedef Stmt** body_iterator;
449  body_iterator body_begin() { return Body; }
450  body_iterator body_end() { return Body + size(); }
451  Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
452
453  void setLastStmt(Stmt *S) {
454    assert(!body_empty() && "setLastStmt");
455    Body[size()-1] = S;
456  }
457
458  typedef Stmt* const * const_body_iterator;
459  const_body_iterator body_begin() const { return Body; }
460  const_body_iterator body_end() const { return Body + size(); }
461  const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
462
463  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
464  reverse_body_iterator body_rbegin() {
465    return reverse_body_iterator(body_end());
466  }
467  reverse_body_iterator body_rend() {
468    return reverse_body_iterator(body_begin());
469  }
470
471  typedef std::reverse_iterator<const_body_iterator>
472          const_reverse_body_iterator;
473
474  const_reverse_body_iterator body_rbegin() const {
475    return const_reverse_body_iterator(body_end());
476  }
477
478  const_reverse_body_iterator body_rend() const {
479    return const_reverse_body_iterator(body_begin());
480  }
481
482  SourceRange getSourceRange() const {
483    return SourceRange(LBracLoc, RBracLoc);
484  }
485
486  SourceLocation getLBracLoc() const { return LBracLoc; }
487  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
488  SourceLocation getRBracLoc() const { return RBracLoc; }
489  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
490
491  static bool classof(const Stmt *T) {
492    return T->getStmtClass() == CompoundStmtClass;
493  }
494  static bool classof(const CompoundStmt *) { return true; }
495
496  // Iterators
497  child_range children() {
498    return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
499  }
500};
501
502// SwitchCase is the base class for CaseStmt and DefaultStmt,
503class SwitchCase : public Stmt {
504protected:
505  // A pointer to the following CaseStmt or DefaultStmt class,
506  // used by SwitchStmt.
507  SwitchCase *NextSwitchCase;
508
509  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
510
511public:
512  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
513
514  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
515
516  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
517
518  Stmt *getSubStmt();
519
520  SourceRange getSourceRange() const { return SourceRange(); }
521
522  static bool classof(const Stmt *T) {
523    return T->getStmtClass() == CaseStmtClass ||
524           T->getStmtClass() == DefaultStmtClass;
525  }
526  static bool classof(const SwitchCase *) { return true; }
527};
528
529class CaseStmt : public SwitchCase {
530  enum { SUBSTMT, LHS, RHS, END_EXPR };
531  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
532                             // GNU "case 1 ... 4" extension
533  SourceLocation CaseLoc;
534  SourceLocation EllipsisLoc;
535  SourceLocation ColonLoc;
536public:
537  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
538           SourceLocation ellipsisLoc, SourceLocation colonLoc)
539    : SwitchCase(CaseStmtClass) {
540    SubExprs[SUBSTMT] = 0;
541    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
542    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
543    CaseLoc = caseLoc;
544    EllipsisLoc = ellipsisLoc;
545    ColonLoc = colonLoc;
546  }
547
548  /// \brief Build an empty switch case statement.
549  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
550
551  SourceLocation getCaseLoc() const { return CaseLoc; }
552  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
553  SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
554  void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
555  SourceLocation getColonLoc() const { return ColonLoc; }
556  void setColonLoc(SourceLocation L) { ColonLoc = L; }
557
558  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
559  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
560  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
561
562  const Expr *getLHS() const {
563    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
564  }
565  const Expr *getRHS() const {
566    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
567  }
568  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
569
570  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
571  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
572  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
573
574
575  SourceRange getSourceRange() const {
576    // Handle deeply nested case statements with iteration instead of recursion.
577    const CaseStmt *CS = this;
578    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
579      CS = CS2;
580
581    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
582  }
583  static bool classof(const Stmt *T) {
584    return T->getStmtClass() == CaseStmtClass;
585  }
586  static bool classof(const CaseStmt *) { return true; }
587
588  // Iterators
589  child_range children() {
590    return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
591  }
592};
593
594class DefaultStmt : public SwitchCase {
595  Stmt* SubStmt;
596  SourceLocation DefaultLoc;
597  SourceLocation ColonLoc;
598public:
599  DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
600    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
601    ColonLoc(CL) {}
602
603  /// \brief Build an empty default statement.
604  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
605
606  Stmt *getSubStmt() { return SubStmt; }
607  const Stmt *getSubStmt() const { return SubStmt; }
608  void setSubStmt(Stmt *S) { SubStmt = S; }
609
610  SourceLocation getDefaultLoc() const { return DefaultLoc; }
611  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
612  SourceLocation getColonLoc() const { return ColonLoc; }
613  void setColonLoc(SourceLocation L) { ColonLoc = L; }
614
615  SourceRange getSourceRange() const {
616    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
617  }
618  static bool classof(const Stmt *T) {
619    return T->getStmtClass() == DefaultStmtClass;
620  }
621  static bool classof(const DefaultStmt *) { return true; }
622
623  // Iterators
624  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
625};
626
627
628/// LabelStmt - Represents a label, which has a substatement.  For example:
629///    foo: return;
630///
631class LabelStmt : public Stmt {
632  LabelDecl *TheDecl;
633  Stmt *SubStmt;
634  SourceLocation IdentLoc;
635public:
636  LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
637    : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
638  }
639
640  // \brief Build an empty label statement.
641  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
642
643  SourceLocation getIdentLoc() const { return IdentLoc; }
644  LabelDecl *getDecl() const { return TheDecl; }
645  void setDecl(LabelDecl *D) { TheDecl = D; }
646  const char *getName() const;
647  Stmt *getSubStmt() { return SubStmt; }
648  const Stmt *getSubStmt() const { return SubStmt; }
649  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
650  void setSubStmt(Stmt *SS) { SubStmt = SS; }
651
652  SourceRange getSourceRange() const {
653    return SourceRange(IdentLoc, SubStmt->getLocEnd());
654  }
655  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
656
657  static bool classof(const Stmt *T) {
658    return T->getStmtClass() == LabelStmtClass;
659  }
660  static bool classof(const LabelStmt *) { return true; }
661};
662
663
664/// IfStmt - This represents an if/then/else.
665///
666class IfStmt : public Stmt {
667  enum { VAR, COND, THEN, ELSE, END_EXPR };
668  Stmt* SubExprs[END_EXPR];
669
670  SourceLocation IfLoc;
671  SourceLocation ElseLoc;
672
673public:
674  IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
675         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
676
677  /// \brief Build an empty if/then/else statement
678  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
679
680  /// \brief Retrieve the variable declared in this "if" statement, if any.
681  ///
682  /// In the following example, "x" is the condition variable.
683  /// \code
684  /// if (int x = foo()) {
685  ///   printf("x is %d", x);
686  /// }
687  /// \endcode
688  VarDecl *getConditionVariable() const;
689  void setConditionVariable(ASTContext &C, VarDecl *V);
690
691  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
692  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
693  const Stmt *getThen() const { return SubExprs[THEN]; }
694  void setThen(Stmt *S) { SubExprs[THEN] = S; }
695  const Stmt *getElse() const { return SubExprs[ELSE]; }
696  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
697
698  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
699  Stmt *getThen() { return SubExprs[THEN]; }
700  Stmt *getElse() { return SubExprs[ELSE]; }
701
702  SourceLocation getIfLoc() const { return IfLoc; }
703  void setIfLoc(SourceLocation L) { IfLoc = L; }
704  SourceLocation getElseLoc() const { return ElseLoc; }
705  void setElseLoc(SourceLocation L) { ElseLoc = L; }
706
707  SourceRange getSourceRange() const {
708    if (SubExprs[ELSE])
709      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
710    else
711      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
712  }
713
714  // Iterators over subexpressions.  The iterators will include iterating
715  // over the initialization expression referenced by the condition variable.
716  child_range children() {
717    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
718  }
719
720  static bool classof(const Stmt *T) {
721    return T->getStmtClass() == IfStmtClass;
722  }
723  static bool classof(const IfStmt *) { return true; }
724};
725
726/// SwitchStmt - This represents a 'switch' stmt.
727///
728class SwitchStmt : public Stmt {
729  enum { VAR, COND, BODY, END_EXPR };
730  Stmt* SubExprs[END_EXPR];
731  // This points to a linked list of case and default statements.
732  SwitchCase *FirstCase;
733  SourceLocation SwitchLoc;
734
735  /// If the SwitchStmt is a switch on an enum value, this records whether
736  /// all the enum values were covered by CaseStmts.  This value is meant to
737  /// be a hint for possible clients.
738  unsigned AllEnumCasesCovered : 1;
739
740public:
741  SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
742
743  /// \brief Build a empty switch statement.
744  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
745
746  /// \brief Retrieve the variable declared in this "switch" statement, if any.
747  ///
748  /// In the following example, "x" is the condition variable.
749  /// \code
750  /// switch (int x = foo()) {
751  ///   case 0: break;
752  ///   // ...
753  /// }
754  /// \endcode
755  VarDecl *getConditionVariable() const;
756  void setConditionVariable(ASTContext &C, VarDecl *V);
757
758  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
759  const Stmt *getBody() const { return SubExprs[BODY]; }
760  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
761
762  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
763  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
764  Stmt *getBody() { return SubExprs[BODY]; }
765  void setBody(Stmt *S) { SubExprs[BODY] = S; }
766  SwitchCase *getSwitchCaseList() { return FirstCase; }
767
768  /// \brief Set the case list for this switch statement.
769  ///
770  /// The caller is responsible for incrementing the retain counts on
771  /// all of the SwitchCase statements in this list.
772  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
773
774  SourceLocation getSwitchLoc() const { return SwitchLoc; }
775  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
776
777  void setBody(Stmt *S, SourceLocation SL) {
778    SubExprs[BODY] = S;
779    SwitchLoc = SL;
780  }
781  void addSwitchCase(SwitchCase *SC) {
782    assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
783    SC->setNextSwitchCase(FirstCase);
784    FirstCase = SC;
785  }
786
787  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
788  /// switch over an enum value then all cases have been explicitly covered.
789  void setAllEnumCasesCovered() {
790    AllEnumCasesCovered = 1;
791  }
792
793  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
794  /// have been explicitly covered.
795  bool isAllEnumCasesCovered() const {
796    return (bool) AllEnumCasesCovered;
797  }
798
799  SourceRange getSourceRange() const {
800    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
801  }
802  // Iterators
803  child_range children() {
804    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
805  }
806
807  static bool classof(const Stmt *T) {
808    return T->getStmtClass() == SwitchStmtClass;
809  }
810  static bool classof(const SwitchStmt *) { return true; }
811};
812
813
814/// WhileStmt - This represents a 'while' stmt.
815///
816class WhileStmt : public Stmt {
817  enum { VAR, COND, BODY, END_EXPR };
818  Stmt* SubExprs[END_EXPR];
819  SourceLocation WhileLoc;
820public:
821  WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
822            SourceLocation WL);
823
824  /// \brief Build an empty while statement.
825  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
826
827  /// \brief Retrieve the variable declared in this "while" statement, if any.
828  ///
829  /// In the following example, "x" is the condition variable.
830  /// \code
831  /// while (int x = random()) {
832  ///   // ...
833  /// }
834  /// \endcode
835  VarDecl *getConditionVariable() const;
836  void setConditionVariable(ASTContext &C, VarDecl *V);
837
838  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
839  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
840  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
841  Stmt *getBody() { return SubExprs[BODY]; }
842  const Stmt *getBody() const { return SubExprs[BODY]; }
843  void setBody(Stmt *S) { SubExprs[BODY] = S; }
844
845  SourceLocation getWhileLoc() const { return WhileLoc; }
846  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
847
848  SourceRange getSourceRange() const {
849    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
850  }
851  static bool classof(const Stmt *T) {
852    return T->getStmtClass() == WhileStmtClass;
853  }
854  static bool classof(const WhileStmt *) { return true; }
855
856  // Iterators
857  child_range children() {
858    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
859  }
860};
861
862/// DoStmt - This represents a 'do/while' stmt.
863///
864class DoStmt : public Stmt {
865  enum { BODY, COND, END_EXPR };
866  Stmt* SubExprs[END_EXPR];
867  SourceLocation DoLoc;
868  SourceLocation WhileLoc;
869  SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
870
871public:
872  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
873         SourceLocation RP)
874    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
875    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
876    SubExprs[BODY] = body;
877  }
878
879  /// \brief Build an empty do-while statement.
880  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
881
882  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
883  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
884  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
885  Stmt *getBody() { return SubExprs[BODY]; }
886  const Stmt *getBody() const { return SubExprs[BODY]; }
887  void setBody(Stmt *S) { SubExprs[BODY] = S; }
888
889  SourceLocation getDoLoc() const { return DoLoc; }
890  void setDoLoc(SourceLocation L) { DoLoc = L; }
891  SourceLocation getWhileLoc() const { return WhileLoc; }
892  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
893
894  SourceLocation getRParenLoc() const { return RParenLoc; }
895  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
896
897  SourceRange getSourceRange() const {
898    return SourceRange(DoLoc, RParenLoc);
899  }
900  static bool classof(const Stmt *T) {
901    return T->getStmtClass() == DoStmtClass;
902  }
903  static bool classof(const DoStmt *) { return true; }
904
905  // Iterators
906  child_range children() {
907    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
908  }
909};
910
911
912/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
913/// the init/cond/inc parts of the ForStmt will be null if they were not
914/// specified in the source.
915///
916class ForStmt : public Stmt {
917  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
918  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
919  SourceLocation ForLoc;
920  SourceLocation LParenLoc, RParenLoc;
921
922public:
923  ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
924          Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
925
926  /// \brief Build an empty for statement.
927  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
928
929  Stmt *getInit() { return SubExprs[INIT]; }
930
931  /// \brief Retrieve the variable declared in this "for" statement, if any.
932  ///
933  /// In the following example, "y" is the condition variable.
934  /// \code
935  /// for (int x = random(); int y = mangle(x); ++x) {
936  ///   // ...
937  /// }
938  /// \endcode
939  VarDecl *getConditionVariable() const;
940  void setConditionVariable(ASTContext &C, VarDecl *V);
941
942  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
943  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
944  Stmt *getBody() { return SubExprs[BODY]; }
945
946  const Stmt *getInit() const { return SubExprs[INIT]; }
947  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
948  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
949  const Stmt *getBody() const { return SubExprs[BODY]; }
950
951  void setInit(Stmt *S) { SubExprs[INIT] = S; }
952  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
953  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
954  void setBody(Stmt *S) { SubExprs[BODY] = S; }
955
956  SourceLocation getForLoc() const { return ForLoc; }
957  void setForLoc(SourceLocation L) { ForLoc = L; }
958  SourceLocation getLParenLoc() const { return LParenLoc; }
959  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
960  SourceLocation getRParenLoc() const { return RParenLoc; }
961  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
962
963  SourceRange getSourceRange() const {
964    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
965  }
966  static bool classof(const Stmt *T) {
967    return T->getStmtClass() == ForStmtClass;
968  }
969  static bool classof(const ForStmt *) { return true; }
970
971  // Iterators
972  child_range children() {
973    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
974  }
975};
976
977/// GotoStmt - This represents a direct goto.
978///
979class GotoStmt : public Stmt {
980  LabelDecl *Label;
981  SourceLocation GotoLoc;
982  SourceLocation LabelLoc;
983public:
984  GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
985    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
986
987  /// \brief Build an empty goto statement.
988  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
989
990  LabelDecl *getLabel() const { return Label; }
991  void setLabel(LabelDecl *D) { Label = D; }
992
993  SourceLocation getGotoLoc() const { return GotoLoc; }
994  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
995  SourceLocation getLabelLoc() const { return LabelLoc; }
996  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
997
998  SourceRange getSourceRange() const {
999    return SourceRange(GotoLoc, LabelLoc);
1000  }
1001  static bool classof(const Stmt *T) {
1002    return T->getStmtClass() == GotoStmtClass;
1003  }
1004  static bool classof(const GotoStmt *) { return true; }
1005
1006  // Iterators
1007  child_range children() { return child_range(); }
1008};
1009
1010/// IndirectGotoStmt - This represents an indirect goto.
1011///
1012class IndirectGotoStmt : public Stmt {
1013  SourceLocation GotoLoc;
1014  SourceLocation StarLoc;
1015  Stmt *Target;
1016public:
1017  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1018                   Expr *target)
1019    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1020      Target((Stmt*)target) {}
1021
1022  /// \brief Build an empty indirect goto statement.
1023  explicit IndirectGotoStmt(EmptyShell Empty)
1024    : Stmt(IndirectGotoStmtClass, Empty) { }
1025
1026  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1027  SourceLocation getGotoLoc() const { return GotoLoc; }
1028  void setStarLoc(SourceLocation L) { StarLoc = L; }
1029  SourceLocation getStarLoc() const { return StarLoc; }
1030
1031  Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1032  const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1033  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1034
1035  /// getConstantTarget - Returns the fixed target of this indirect
1036  /// goto, if one exists.
1037  LabelDecl *getConstantTarget();
1038  const LabelDecl *getConstantTarget() const {
1039    return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1040  }
1041
1042  SourceRange getSourceRange() const {
1043    return SourceRange(GotoLoc, Target->getLocEnd());
1044  }
1045
1046  static bool classof(const Stmt *T) {
1047    return T->getStmtClass() == IndirectGotoStmtClass;
1048  }
1049  static bool classof(const IndirectGotoStmt *) { return true; }
1050
1051  // Iterators
1052  child_range children() { return child_range(&Target, &Target+1); }
1053};
1054
1055
1056/// ContinueStmt - This represents a continue.
1057///
1058class ContinueStmt : public Stmt {
1059  SourceLocation ContinueLoc;
1060public:
1061  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1062
1063  /// \brief Build an empty continue statement.
1064  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1065
1066  SourceLocation getContinueLoc() const { return ContinueLoc; }
1067  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1068
1069  SourceRange getSourceRange() const {
1070    return SourceRange(ContinueLoc);
1071  }
1072
1073  static bool classof(const Stmt *T) {
1074    return T->getStmtClass() == ContinueStmtClass;
1075  }
1076  static bool classof(const ContinueStmt *) { return true; }
1077
1078  // Iterators
1079  child_range children() { return child_range(); }
1080};
1081
1082/// BreakStmt - This represents a break.
1083///
1084class BreakStmt : public Stmt {
1085  SourceLocation BreakLoc;
1086public:
1087  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1088
1089  /// \brief Build an empty break statement.
1090  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1091
1092  SourceLocation getBreakLoc() const { return BreakLoc; }
1093  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1094
1095  SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
1096
1097  static bool classof(const Stmt *T) {
1098    return T->getStmtClass() == BreakStmtClass;
1099  }
1100  static bool classof(const BreakStmt *) { return true; }
1101
1102  // Iterators
1103  child_range children() { return child_range(); }
1104};
1105
1106
1107/// ReturnStmt - This represents a return, optionally of an expression:
1108///   return;
1109///   return 4;
1110///
1111/// Note that GCC allows return with no argument in a function declared to
1112/// return a value, and it allows returning a value in functions declared to
1113/// return void.  We explicitly model this in the AST, which means you can't
1114/// depend on the return type of the function and the presence of an argument.
1115///
1116class ReturnStmt : public Stmt {
1117  Stmt *RetExpr;
1118  SourceLocation RetLoc;
1119  const VarDecl *NRVOCandidate;
1120
1121public:
1122  ReturnStmt(SourceLocation RL)
1123    : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1124
1125  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1126    : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1127      NRVOCandidate(NRVOCandidate) {}
1128
1129  /// \brief Build an empty return expression.
1130  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1131
1132  const Expr *getRetValue() const;
1133  Expr *getRetValue();
1134  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1135
1136  SourceLocation getReturnLoc() const { return RetLoc; }
1137  void setReturnLoc(SourceLocation L) { RetLoc = L; }
1138
1139  /// \brief Retrieve the variable that might be used for the named return
1140  /// value optimization.
1141  ///
1142  /// The optimization itself can only be performed if the variable is
1143  /// also marked as an NRVO object.
1144  const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1145  void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1146
1147  SourceRange getSourceRange() const;
1148
1149  static bool classof(const Stmt *T) {
1150    return T->getStmtClass() == ReturnStmtClass;
1151  }
1152  static bool classof(const ReturnStmt *) { return true; }
1153
1154  // Iterators
1155  child_range children() {
1156    if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1157    return child_range();
1158  }
1159};
1160
1161/// AsmStmt - This represents a GNU inline-assembly statement extension.
1162///
1163class AsmStmt : public Stmt {
1164  SourceLocation AsmLoc, RParenLoc;
1165  StringLiteral *AsmStr;
1166
1167  bool IsSimple;
1168  bool IsVolatile;
1169  bool MSAsm;
1170
1171  unsigned NumOutputs;
1172  unsigned NumInputs;
1173  unsigned NumClobbers;
1174
1175  // FIXME: If we wanted to, we could allocate all of these in one big array.
1176  IdentifierInfo **Names;
1177  StringLiteral **Constraints;
1178  Stmt **Exprs;
1179  StringLiteral **Clobbers;
1180
1181public:
1182  AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1183          bool msasm, unsigned numoutputs, unsigned numinputs,
1184          IdentifierInfo **names, StringLiteral **constraints,
1185          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1186          StringLiteral **clobbers, SourceLocation rparenloc);
1187
1188  /// \brief Build an empty inline-assembly statement.
1189  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1190    Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1191
1192  SourceLocation getAsmLoc() const { return AsmLoc; }
1193  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1194  SourceLocation getRParenLoc() const { return RParenLoc; }
1195  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1196
1197  bool isVolatile() const { return IsVolatile; }
1198  void setVolatile(bool V) { IsVolatile = V; }
1199  bool isSimple() const { return IsSimple; }
1200  void setSimple(bool V) { IsSimple = V; }
1201  bool isMSAsm() const { return MSAsm; }
1202  void setMSAsm(bool V) { MSAsm = V; }
1203
1204  //===--- Asm String Analysis ---===//
1205
1206  const StringLiteral *getAsmString() const { return AsmStr; }
1207  StringLiteral *getAsmString() { return AsmStr; }
1208  void setAsmString(StringLiteral *E) { AsmStr = E; }
1209
1210  /// AsmStringPiece - this is part of a decomposed asm string specification
1211  /// (for use with the AnalyzeAsmString function below).  An asm string is
1212  /// considered to be a concatenation of these parts.
1213  class AsmStringPiece {
1214  public:
1215    enum Kind {
1216      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1217      Operand  // Operand reference, with optional modifier %c4.
1218    };
1219  private:
1220    Kind MyKind;
1221    std::string Str;
1222    unsigned OperandNo;
1223  public:
1224    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1225    AsmStringPiece(unsigned OpNo, char Modifier)
1226      : MyKind(Operand), Str(), OperandNo(OpNo) {
1227      Str += Modifier;
1228    }
1229
1230    bool isString() const { return MyKind == String; }
1231    bool isOperand() const { return MyKind == Operand; }
1232
1233    const std::string &getString() const {
1234      assert(isString());
1235      return Str;
1236    }
1237
1238    unsigned getOperandNo() const {
1239      assert(isOperand());
1240      return OperandNo;
1241    }
1242
1243    /// getModifier - Get the modifier for this operand, if present.  This
1244    /// returns '\0' if there was no modifier.
1245    char getModifier() const {
1246      assert(isOperand());
1247      return Str[0];
1248    }
1249  };
1250
1251  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1252  /// it into pieces.  If the asm string is erroneous, emit errors and return
1253  /// true, otherwise return false.  This handles canonicalization and
1254  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1255  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1256  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1257                            ASTContext &C, unsigned &DiagOffs) const;
1258
1259
1260  //===--- Output operands ---===//
1261
1262  unsigned getNumOutputs() const { return NumOutputs; }
1263
1264  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1265    return Names[i];
1266  }
1267
1268  llvm::StringRef getOutputName(unsigned i) const {
1269    if (IdentifierInfo *II = getOutputIdentifier(i))
1270      return II->getName();
1271
1272    return llvm::StringRef();
1273  }
1274
1275  /// getOutputConstraint - Return the constraint string for the specified
1276  /// output operand.  All output constraints are known to be non-empty (either
1277  /// '=' or '+').
1278  llvm::StringRef getOutputConstraint(unsigned i) const;
1279
1280  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1281    return Constraints[i];
1282  }
1283  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1284    return Constraints[i];
1285  }
1286
1287  Expr *getOutputExpr(unsigned i);
1288
1289  const Expr *getOutputExpr(unsigned i) const {
1290    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1291  }
1292
1293  /// isOutputPlusConstraint - Return true if the specified output constraint
1294  /// is a "+" constraint (which is both an input and an output) or false if it
1295  /// is an "=" constraint (just an output).
1296  bool isOutputPlusConstraint(unsigned i) const {
1297    return getOutputConstraint(i)[0] == '+';
1298  }
1299
1300  /// getNumPlusOperands - Return the number of output operands that have a "+"
1301  /// constraint.
1302  unsigned getNumPlusOperands() const;
1303
1304  //===--- Input operands ---===//
1305
1306  unsigned getNumInputs() const { return NumInputs; }
1307
1308  IdentifierInfo *getInputIdentifier(unsigned i) const {
1309    return Names[i + NumOutputs];
1310  }
1311
1312  llvm::StringRef getInputName(unsigned i) const {
1313    if (IdentifierInfo *II = getInputIdentifier(i))
1314      return II->getName();
1315
1316    return llvm::StringRef();
1317  }
1318
1319  /// getInputConstraint - Return the specified input constraint.  Unlike output
1320  /// constraints, these can be empty.
1321  llvm::StringRef getInputConstraint(unsigned i) const;
1322
1323  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1324    return Constraints[i + NumOutputs];
1325  }
1326  StringLiteral *getInputConstraintLiteral(unsigned i) {
1327    return Constraints[i + NumOutputs];
1328  }
1329
1330  Expr *getInputExpr(unsigned i);
1331
1332  const Expr *getInputExpr(unsigned i) const {
1333    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1334  }
1335
1336  void setOutputsAndInputsAndClobbers(ASTContext &C,
1337                                      IdentifierInfo **Names,
1338                                      StringLiteral **Constraints,
1339                                      Stmt **Exprs,
1340                                      unsigned NumOutputs,
1341                                      unsigned NumInputs,
1342                                      StringLiteral **Clobbers,
1343                                      unsigned NumClobbers);
1344
1345  //===--- Other ---===//
1346
1347  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1348  /// translate this into a numeric value needed to reference the same operand.
1349  /// This returns -1 if the operand name is invalid.
1350  int getNamedOperand(llvm::StringRef SymbolicName) const;
1351
1352  unsigned getNumClobbers() const { return NumClobbers; }
1353  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1354  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1355
1356  SourceRange getSourceRange() const {
1357    return SourceRange(AsmLoc, RParenLoc);
1358  }
1359
1360  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1361  static bool classof(const AsmStmt *) { return true; }
1362
1363  // Input expr iterators.
1364
1365  typedef ExprIterator inputs_iterator;
1366  typedef ConstExprIterator const_inputs_iterator;
1367
1368  inputs_iterator begin_inputs() {
1369    return &Exprs[0] + NumOutputs;
1370  }
1371
1372  inputs_iterator end_inputs() {
1373    return &Exprs[0] + NumOutputs + NumInputs;
1374  }
1375
1376  const_inputs_iterator begin_inputs() const {
1377    return &Exprs[0] + NumOutputs;
1378  }
1379
1380  const_inputs_iterator end_inputs() const {
1381    return &Exprs[0] + NumOutputs + NumInputs;
1382  }
1383
1384  // Output expr iterators.
1385
1386  typedef ExprIterator outputs_iterator;
1387  typedef ConstExprIterator const_outputs_iterator;
1388
1389  outputs_iterator begin_outputs() {
1390    return &Exprs[0];
1391  }
1392  outputs_iterator end_outputs() {
1393    return &Exprs[0] + NumOutputs;
1394  }
1395
1396  const_outputs_iterator begin_outputs() const {
1397    return &Exprs[0];
1398  }
1399  const_outputs_iterator end_outputs() const {
1400    return &Exprs[0] + NumOutputs;
1401  }
1402
1403  child_range children() {
1404    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1405  }
1406};
1407
1408}  // end namespace clang
1409
1410#endif
1411