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