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