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