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