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