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