Stmt.h revision a25b6a4b43e8b9611f7506e5fe1b448833b10a46
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
655  /// \brief True if we have code like:
656  /// @code
657  ///   #define CALL(x)
658  ///   if (condition)
659  ///     CALL(0);
660  /// @endcode
661  bool MacroExpandedInThenStmt;
662
663public:
664  IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
665         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0,
666         bool macroExpandedInThenStmt = false);
667
668  /// \brief Build an empty if/then/else statement
669  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
670
671  /// \brief Retrieve the variable declared in this "if" statement, if any.
672  ///
673  /// In the following example, "x" is the condition variable.
674  /// \code
675  /// if (int x = foo()) {
676  ///   printf("x is %d", x);
677  /// }
678  /// \endcode
679  VarDecl *getConditionVariable() const;
680  void setConditionVariable(ASTContext &C, VarDecl *V);
681
682  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
683  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
684  const Stmt *getThen() const { return SubExprs[THEN]; }
685  void setThen(Stmt *S) { SubExprs[THEN] = S; }
686  const Stmt *getElse() const { return SubExprs[ELSE]; }
687  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
688
689  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
690  Stmt *getThen() { return SubExprs[THEN]; }
691  Stmt *getElse() { return SubExprs[ELSE]; }
692
693  SourceLocation getIfLoc() const { return IfLoc; }
694  void setIfLoc(SourceLocation L) { IfLoc = L; }
695  SourceLocation getElseLoc() const { return ElseLoc; }
696  void setElseLoc(SourceLocation L) { ElseLoc = L; }
697
698  bool hasMacroExpandedInThenStmt() const { return MacroExpandedInThenStmt; }
699
700  virtual SourceRange getSourceRange() const {
701    if (SubExprs[ELSE])
702      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
703    else
704      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
705  }
706
707  static bool classof(const Stmt *T) {
708    return T->getStmtClass() == IfStmtClass;
709  }
710  static bool classof(const IfStmt *) { return true; }
711
712  // Iterators over subexpressions.  The iterators will include iterating
713  // over the initialization expression referenced by the condition variable.
714  virtual child_iterator child_begin();
715  virtual child_iterator child_end();
716
717  friend class ASTStmtReader;
718  friend class ASTStmtWriter;
719};
720
721/// SwitchStmt - This represents a 'switch' stmt.
722///
723class SwitchStmt : public Stmt {
724  enum { VAR, COND, BODY, END_EXPR };
725  Stmt* SubExprs[END_EXPR];
726  // This points to a linked list of case and default statements.
727  SwitchCase *FirstCase;
728  SourceLocation SwitchLoc;
729
730  /// If the SwitchStmt is a switch on an enum value, this records whether
731  /// all the enum values were covered by CaseStmts.  This value is meant to
732  /// be a hint for possible clients.
733  unsigned AllEnumCasesCovered : 1;
734
735public:
736  SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
737
738  /// \brief Build a empty switch statement.
739  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
740
741  /// \brief Retrieve the variable declared in this "switch" statement, if any.
742  ///
743  /// In the following example, "x" is the condition variable.
744  /// \code
745  /// switch (int x = foo()) {
746  ///   case 0: break;
747  ///   // ...
748  /// }
749  /// \endcode
750  VarDecl *getConditionVariable() const;
751  void setConditionVariable(ASTContext &C, VarDecl *V);
752
753  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
754  const Stmt *getBody() const { return SubExprs[BODY]; }
755  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
756
757  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
758  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
759  Stmt *getBody() { return SubExprs[BODY]; }
760  void setBody(Stmt *S) { SubExprs[BODY] = S; }
761  SwitchCase *getSwitchCaseList() { return FirstCase; }
762
763  /// \brief Set the case list for this switch statement.
764  ///
765  /// The caller is responsible for incrementing the retain counts on
766  /// all of the SwitchCase statements in this list.
767  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
768
769  SourceLocation getSwitchLoc() const { return SwitchLoc; }
770  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
771
772  void setBody(Stmt *S, SourceLocation SL) {
773    SubExprs[BODY] = S;
774    SwitchLoc = SL;
775  }
776  void addSwitchCase(SwitchCase *SC) {
777    assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
778    SC->setNextSwitchCase(FirstCase);
779    FirstCase = SC;
780  }
781
782  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
783  /// switch over an enum value then all cases have been explicitly covered.
784  void setAllEnumCasesCovered() {
785    AllEnumCasesCovered = 1;
786  }
787
788  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
789  /// have been explicitly covered.
790  bool isAllEnumCasesCovered() const {
791    return (bool) AllEnumCasesCovered;
792  }
793
794  virtual SourceRange getSourceRange() const {
795    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
796  }
797  static bool classof(const Stmt *T) {
798    return T->getStmtClass() == SwitchStmtClass;
799  }
800  static bool classof(const SwitchStmt *) { return true; }
801
802  // Iterators
803  virtual child_iterator child_begin();
804  virtual child_iterator child_end();
805};
806
807
808/// WhileStmt - This represents a 'while' stmt.
809///
810class WhileStmt : public Stmt {
811  enum { VAR, COND, BODY, END_EXPR };
812  Stmt* SubExprs[END_EXPR];
813  SourceLocation WhileLoc;
814public:
815  WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
816            SourceLocation WL);
817
818  /// \brief Build an empty while statement.
819  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
820
821  /// \brief Retrieve the variable declared in this "while" statement, if any.
822  ///
823  /// In the following example, "x" is the condition variable.
824  /// \code
825  /// while (int x = random()) {
826  ///   // ...
827  /// }
828  /// \endcode
829  VarDecl *getConditionVariable() const;
830  void setConditionVariable(ASTContext &C, VarDecl *V);
831
832  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
833  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
834  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
835  Stmt *getBody() { return SubExprs[BODY]; }
836  const Stmt *getBody() const { return SubExprs[BODY]; }
837  void setBody(Stmt *S) { SubExprs[BODY] = S; }
838
839  SourceLocation getWhileLoc() const { return WhileLoc; }
840  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
841
842  virtual SourceRange getSourceRange() const {
843    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
844  }
845  static bool classof(const Stmt *T) {
846    return T->getStmtClass() == WhileStmtClass;
847  }
848  static bool classof(const WhileStmt *) { return true; }
849
850  // Iterators
851  virtual child_iterator child_begin();
852  virtual child_iterator child_end();
853};
854
855/// DoStmt - This represents a 'do/while' stmt.
856///
857class DoStmt : public Stmt {
858  enum { COND, BODY, END_EXPR };
859  Stmt* SubExprs[END_EXPR];
860  SourceLocation DoLoc;
861  SourceLocation WhileLoc;
862  SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
863
864public:
865  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
866         SourceLocation RP)
867    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
868    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
869    SubExprs[BODY] = body;
870  }
871
872  /// \brief Build an empty do-while statement.
873  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
874
875  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
876  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
877  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
878  Stmt *getBody() { return SubExprs[BODY]; }
879  const Stmt *getBody() const { return SubExprs[BODY]; }
880  void setBody(Stmt *S) { SubExprs[BODY] = S; }
881
882  SourceLocation getDoLoc() const { return DoLoc; }
883  void setDoLoc(SourceLocation L) { DoLoc = L; }
884  SourceLocation getWhileLoc() const { return WhileLoc; }
885  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
886
887  SourceLocation getRParenLoc() const { return RParenLoc; }
888  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
889
890  virtual SourceRange getSourceRange() const {
891    return SourceRange(DoLoc, RParenLoc);
892  }
893  static bool classof(const Stmt *T) {
894    return T->getStmtClass() == DoStmtClass;
895  }
896  static bool classof(const DoStmt *) { return true; }
897
898  // Iterators
899  virtual child_iterator child_begin();
900  virtual child_iterator child_end();
901};
902
903
904/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
905/// the init/cond/inc parts of the ForStmt will be null if they were not
906/// specified in the source.
907///
908class ForStmt : public Stmt {
909  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
910  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
911  SourceLocation ForLoc;
912  SourceLocation LParenLoc, RParenLoc;
913
914public:
915  ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
916          Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
917
918  /// \brief Build an empty for statement.
919  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
920
921  Stmt *getInit() { return SubExprs[INIT]; }
922
923  /// \brief Retrieve the variable declared in this "for" statement, if any.
924  ///
925  /// In the following example, "y" is the condition variable.
926  /// \code
927  /// for (int x = random(); int y = mangle(x); ++x) {
928  ///   // ...
929  /// }
930  /// \endcode
931  VarDecl *getConditionVariable() const;
932  void setConditionVariable(ASTContext &C, VarDecl *V);
933
934  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
935  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
936  Stmt *getBody() { return SubExprs[BODY]; }
937
938  const Stmt *getInit() const { return SubExprs[INIT]; }
939  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
940  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
941  const Stmt *getBody() const { return SubExprs[BODY]; }
942
943  void setInit(Stmt *S) { SubExprs[INIT] = S; }
944  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
945  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
946  void setBody(Stmt *S) { SubExprs[BODY] = S; }
947
948  SourceLocation getForLoc() const { return ForLoc; }
949  void setForLoc(SourceLocation L) { ForLoc = L; }
950  SourceLocation getLParenLoc() const { return LParenLoc; }
951  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
952  SourceLocation getRParenLoc() const { return RParenLoc; }
953  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
954
955  virtual SourceRange getSourceRange() const {
956    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
957  }
958  static bool classof(const Stmt *T) {
959    return T->getStmtClass() == ForStmtClass;
960  }
961  static bool classof(const ForStmt *) { return true; }
962
963  // Iterators
964  virtual child_iterator child_begin();
965  virtual child_iterator child_end();
966};
967
968/// GotoStmt - This represents a direct goto.
969///
970class GotoStmt : public Stmt {
971  LabelStmt *Label;
972  SourceLocation GotoLoc;
973  SourceLocation LabelLoc;
974public:
975  GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
976    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
977
978  /// \brief Build an empty goto statement.
979  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
980
981  LabelStmt *getLabel() const { return Label; }
982  void setLabel(LabelStmt *S) { Label = S; }
983
984  SourceLocation getGotoLoc() const { return GotoLoc; }
985  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
986  SourceLocation getLabelLoc() const { return LabelLoc; }
987  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
988
989  virtual SourceRange getSourceRange() const {
990    return SourceRange(GotoLoc, LabelLoc);
991  }
992  static bool classof(const Stmt *T) {
993    return T->getStmtClass() == GotoStmtClass;
994  }
995  static bool classof(const GotoStmt *) { return true; }
996
997  // Iterators
998  virtual child_iterator child_begin();
999  virtual child_iterator child_end();
1000};
1001
1002/// IndirectGotoStmt - This represents an indirect goto.
1003///
1004class IndirectGotoStmt : public Stmt {
1005  SourceLocation GotoLoc;
1006  SourceLocation StarLoc;
1007  Stmt *Target;
1008public:
1009  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1010                   Expr *target)
1011    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1012      Target((Stmt*)target) {}
1013
1014  /// \brief Build an empty indirect goto statement.
1015  explicit IndirectGotoStmt(EmptyShell Empty)
1016    : Stmt(IndirectGotoStmtClass, Empty) { }
1017
1018  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1019  SourceLocation getGotoLoc() const { return GotoLoc; }
1020  void setStarLoc(SourceLocation L) { StarLoc = L; }
1021  SourceLocation getStarLoc() const { return StarLoc; }
1022
1023  Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1024  const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1025  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1026
1027  /// getConstantTarget - Returns the fixed target of this indirect
1028  /// goto, if one exists.
1029  LabelStmt *getConstantTarget();
1030  const LabelStmt *getConstantTarget() const {
1031    return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1032  }
1033
1034  virtual SourceRange getSourceRange() const {
1035    return SourceRange(GotoLoc, Target->getLocEnd());
1036  }
1037
1038  static bool classof(const Stmt *T) {
1039    return T->getStmtClass() == IndirectGotoStmtClass;
1040  }
1041  static bool classof(const IndirectGotoStmt *) { return true; }
1042
1043  // Iterators
1044  virtual child_iterator child_begin();
1045  virtual child_iterator child_end();
1046};
1047
1048
1049/// ContinueStmt - This represents a continue.
1050///
1051class ContinueStmt : public Stmt {
1052  SourceLocation ContinueLoc;
1053public:
1054  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1055
1056  /// \brief Build an empty continue statement.
1057  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1058
1059  SourceLocation getContinueLoc() const { return ContinueLoc; }
1060  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1061
1062  virtual SourceRange getSourceRange() const {
1063    return SourceRange(ContinueLoc);
1064  }
1065
1066  static bool classof(const Stmt *T) {
1067    return T->getStmtClass() == ContinueStmtClass;
1068  }
1069  static bool classof(const ContinueStmt *) { return true; }
1070
1071  // Iterators
1072  virtual child_iterator child_begin();
1073  virtual child_iterator child_end();
1074};
1075
1076/// BreakStmt - This represents a break.
1077///
1078class BreakStmt : public Stmt {
1079  SourceLocation BreakLoc;
1080public:
1081  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1082
1083  /// \brief Build an empty break statement.
1084  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1085
1086  SourceLocation getBreakLoc() const { return BreakLoc; }
1087  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1088
1089  virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
1090
1091  static bool classof(const Stmt *T) {
1092    return T->getStmtClass() == BreakStmtClass;
1093  }
1094  static bool classof(const BreakStmt *) { return true; }
1095
1096  // Iterators
1097  virtual child_iterator child_begin();
1098  virtual child_iterator child_end();
1099};
1100
1101
1102/// ReturnStmt - This represents a return, optionally of an expression:
1103///   return;
1104///   return 4;
1105///
1106/// Note that GCC allows return with no argument in a function declared to
1107/// return a value, and it allows returning a value in functions declared to
1108/// return void.  We explicitly model this in the AST, which means you can't
1109/// depend on the return type of the function and the presence of an argument.
1110///
1111class ReturnStmt : public Stmt {
1112  Stmt *RetExpr;
1113  SourceLocation RetLoc;
1114  const VarDecl *NRVOCandidate;
1115
1116public:
1117  ReturnStmt(SourceLocation RL)
1118    : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1119
1120  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1121    : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1122      NRVOCandidate(NRVOCandidate) {}
1123
1124  /// \brief Build an empty return expression.
1125  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1126
1127  const Expr *getRetValue() const;
1128  Expr *getRetValue();
1129  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1130
1131  SourceLocation getReturnLoc() const { return RetLoc; }
1132  void setReturnLoc(SourceLocation L) { RetLoc = L; }
1133
1134  /// \brief Retrieve the variable that might be used for the named return
1135  /// value optimization.
1136  ///
1137  /// The optimization itself can only be performed if the variable is
1138  /// also marked as an NRVO object.
1139  const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1140  void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1141
1142  virtual SourceRange getSourceRange() const;
1143
1144  static bool classof(const Stmt *T) {
1145    return T->getStmtClass() == ReturnStmtClass;
1146  }
1147  static bool classof(const ReturnStmt *) { return true; }
1148
1149  // Iterators
1150  virtual child_iterator child_begin();
1151  virtual child_iterator child_end();
1152};
1153
1154/// AsmStmt - This represents a GNU inline-assembly statement extension.
1155///
1156class AsmStmt : public Stmt {
1157  SourceLocation AsmLoc, RParenLoc;
1158  StringLiteral *AsmStr;
1159
1160  bool IsSimple;
1161  bool IsVolatile;
1162  bool MSAsm;
1163
1164  unsigned NumOutputs;
1165  unsigned NumInputs;
1166  unsigned NumClobbers;
1167
1168  // FIXME: If we wanted to, we could allocate all of these in one big array.
1169  IdentifierInfo **Names;
1170  StringLiteral **Constraints;
1171  Stmt **Exprs;
1172  StringLiteral **Clobbers;
1173
1174public:
1175  AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1176          bool msasm, unsigned numoutputs, unsigned numinputs,
1177          IdentifierInfo **names, StringLiteral **constraints,
1178          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1179          StringLiteral **clobbers, SourceLocation rparenloc);
1180
1181  /// \brief Build an empty inline-assembly statement.
1182  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1183    Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1184
1185  SourceLocation getAsmLoc() const { return AsmLoc; }
1186  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1187  SourceLocation getRParenLoc() const { return RParenLoc; }
1188  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1189
1190  bool isVolatile() const { return IsVolatile; }
1191  void setVolatile(bool V) { IsVolatile = V; }
1192  bool isSimple() const { return IsSimple; }
1193  void setSimple(bool V) { IsSimple = V; }
1194  bool isMSAsm() const { return MSAsm; }
1195  void setMSAsm(bool V) { MSAsm = V; }
1196
1197  //===--- Asm String Analysis ---===//
1198
1199  const StringLiteral *getAsmString() const { return AsmStr; }
1200  StringLiteral *getAsmString() { return AsmStr; }
1201  void setAsmString(StringLiteral *E) { AsmStr = E; }
1202
1203  /// AsmStringPiece - this is part of a decomposed asm string specification
1204  /// (for use with the AnalyzeAsmString function below).  An asm string is
1205  /// considered to be a concatenation of these parts.
1206  class AsmStringPiece {
1207  public:
1208    enum Kind {
1209      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1210      Operand  // Operand reference, with optional modifier %c4.
1211    };
1212  private:
1213    Kind MyKind;
1214    std::string Str;
1215    unsigned OperandNo;
1216  public:
1217    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1218    AsmStringPiece(unsigned OpNo, char Modifier)
1219      : MyKind(Operand), Str(), OperandNo(OpNo) {
1220      Str += Modifier;
1221    }
1222
1223    bool isString() const { return MyKind == String; }
1224    bool isOperand() const { return MyKind == Operand; }
1225
1226    const std::string &getString() const {
1227      assert(isString());
1228      return Str;
1229    }
1230
1231    unsigned getOperandNo() const {
1232      assert(isOperand());
1233      return OperandNo;
1234    }
1235
1236    /// getModifier - Get the modifier for this operand, if present.  This
1237    /// returns '\0' if there was no modifier.
1238    char getModifier() const {
1239      assert(isOperand());
1240      return Str[0];
1241    }
1242  };
1243
1244  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1245  /// it into pieces.  If the asm string is erroneous, emit errors and return
1246  /// true, otherwise return false.  This handles canonicalization and
1247  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1248  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1249  unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1250                            ASTContext &C, unsigned &DiagOffs) const;
1251
1252
1253  //===--- Output operands ---===//
1254
1255  unsigned getNumOutputs() const { return NumOutputs; }
1256
1257  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1258    return Names[i];
1259  }
1260
1261  llvm::StringRef getOutputName(unsigned i) const {
1262    if (IdentifierInfo *II = getOutputIdentifier(i))
1263      return II->getName();
1264
1265    return llvm::StringRef();
1266  }
1267
1268  /// getOutputConstraint - Return the constraint string for the specified
1269  /// output operand.  All output constraints are known to be non-empty (either
1270  /// '=' or '+').
1271  llvm::StringRef getOutputConstraint(unsigned i) const;
1272
1273  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1274    return Constraints[i];
1275  }
1276  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1277    return Constraints[i];
1278  }
1279
1280  Expr *getOutputExpr(unsigned i);
1281
1282  const Expr *getOutputExpr(unsigned i) const {
1283    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1284  }
1285
1286  /// isOutputPlusConstraint - Return true if the specified output constraint
1287  /// is a "+" constraint (which is both an input and an output) or false if it
1288  /// is an "=" constraint (just an output).
1289  bool isOutputPlusConstraint(unsigned i) const {
1290    return getOutputConstraint(i)[0] == '+';
1291  }
1292
1293  /// getNumPlusOperands - Return the number of output operands that have a "+"
1294  /// constraint.
1295  unsigned getNumPlusOperands() const;
1296
1297  //===--- Input operands ---===//
1298
1299  unsigned getNumInputs() const { return NumInputs; }
1300
1301  IdentifierInfo *getInputIdentifier(unsigned i) const {
1302    return Names[i + NumOutputs];
1303  }
1304
1305  llvm::StringRef getInputName(unsigned i) const {
1306    if (IdentifierInfo *II = getInputIdentifier(i))
1307      return II->getName();
1308
1309    return llvm::StringRef();
1310  }
1311
1312  /// getInputConstraint - Return the specified input constraint.  Unlike output
1313  /// constraints, these can be empty.
1314  llvm::StringRef getInputConstraint(unsigned i) const;
1315
1316  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1317    return Constraints[i + NumOutputs];
1318  }
1319  StringLiteral *getInputConstraintLiteral(unsigned i) {
1320    return Constraints[i + NumOutputs];
1321  }
1322
1323  Expr *getInputExpr(unsigned i);
1324
1325  const Expr *getInputExpr(unsigned i) const {
1326    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1327  }
1328
1329  void setOutputsAndInputsAndClobbers(ASTContext &C,
1330                                      IdentifierInfo **Names,
1331                                      StringLiteral **Constraints,
1332                                      Stmt **Exprs,
1333                                      unsigned NumOutputs,
1334                                      unsigned NumInputs,
1335                                      StringLiteral **Clobbers,
1336                                      unsigned NumClobbers);
1337
1338  //===--- Other ---===//
1339
1340  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1341  /// translate this into a numeric value needed to reference the same operand.
1342  /// This returns -1 if the operand name is invalid.
1343  int getNamedOperand(llvm::StringRef SymbolicName) const;
1344
1345  unsigned getNumClobbers() const { return NumClobbers; }
1346  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1347  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1348
1349  virtual SourceRange getSourceRange() const {
1350    return SourceRange(AsmLoc, RParenLoc);
1351  }
1352
1353  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1354  static bool classof(const AsmStmt *) { return true; }
1355
1356  // Input expr iterators.
1357
1358  typedef ExprIterator inputs_iterator;
1359  typedef ConstExprIterator const_inputs_iterator;
1360
1361  inputs_iterator begin_inputs() {
1362    return &Exprs[0] + NumOutputs;
1363  }
1364
1365  inputs_iterator end_inputs() {
1366    return &Exprs[0] + NumOutputs + NumInputs;
1367  }
1368
1369  const_inputs_iterator begin_inputs() const {
1370    return &Exprs[0] + NumOutputs;
1371  }
1372
1373  const_inputs_iterator end_inputs() const {
1374    return &Exprs[0] + NumOutputs + NumInputs;
1375  }
1376
1377  // Output expr iterators.
1378
1379  typedef ExprIterator outputs_iterator;
1380  typedef ConstExprIterator const_outputs_iterator;
1381
1382  outputs_iterator begin_outputs() {
1383    return &Exprs[0];
1384  }
1385  outputs_iterator end_outputs() {
1386    return &Exprs[0] + NumOutputs;
1387  }
1388
1389  const_outputs_iterator begin_outputs() const {
1390    return &Exprs[0];
1391  }
1392  const_outputs_iterator end_outputs() const {
1393    return &Exprs[0] + NumOutputs;
1394  }
1395
1396  // Child iterators
1397
1398  virtual child_iterator child_begin();
1399  virtual child_iterator child_end();
1400};
1401
1402}  // end namespace clang
1403
1404#endif
1405