Stmt.h revision f4b88a45902af1802a1cb42ba48b1c474474f228
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 "clang/Basic/LLVM.h"
18#include "clang/Basic/SourceLocation.h"
19#include "clang/AST/PrettyPrinter.h"
20#include "clang/AST/StmtIterator.h"
21#include "clang/AST/DeclGroup.h"
22#include "clang/AST/ASTContext.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/raw_ostream.h"
26#include <string>
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    llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
112  }
113  void operator delete(void* data) throw() {
114    llvm_unreachable("Stmts cannot be released with regular 'delete'.");
115  }
116
117  class StmtBitfields {
118    friend class Stmt;
119
120    /// \brief The statement class.
121    unsigned sClass : 8;
122  };
123  enum { NumStmtBits = 8 };
124
125  class CompoundStmtBitfields {
126    friend class CompoundStmt;
127    unsigned : NumStmtBits;
128
129    unsigned NumStmts : 32 - NumStmtBits;
130  };
131
132  class ExprBitfields {
133    friend class Expr;
134    friend class DeclRefExpr; // computeDependence
135    friend class InitListExpr; // ctor
136    friend class DesignatedInitExpr; // ctor
137    friend class BlockDeclRefExpr; // ctor
138    friend class ASTStmtReader; // deserialization
139    friend class CXXNewExpr; // ctor
140    friend class DependentScopeDeclRefExpr; // ctor
141    friend class CXXConstructExpr; // ctor
142    friend class CallExpr; // ctor
143    friend class OffsetOfExpr; // ctor
144    friend class ObjCMessageExpr; // ctor
145    friend class ObjCArrayLiteral; // ctor
146    friend class ObjCDictionaryLiteral; // ctor
147    friend class ShuffleVectorExpr; // ctor
148    friend class ParenListExpr; // ctor
149    friend class CXXUnresolvedConstructExpr; // ctor
150    friend class CXXDependentScopeMemberExpr; // ctor
151    friend class OverloadExpr; // ctor
152    friend class PseudoObjectExpr; // ctor
153    friend class AtomicExpr; // ctor
154    unsigned : NumStmtBits;
155
156    unsigned ValueKind : 2;
157    unsigned ObjectKind : 2;
158    unsigned TypeDependent : 1;
159    unsigned ValueDependent : 1;
160    unsigned InstantiationDependent : 1;
161    unsigned ContainsUnexpandedParameterPack : 1;
162  };
163  enum { NumExprBits = 16 };
164
165  class CharacterLiteralBitfields {
166    friend class CharacterLiteral;
167    unsigned : NumExprBits;
168
169    unsigned Kind : 2;
170  };
171
172  class FloatingLiteralBitfields {
173    friend class FloatingLiteral;
174    unsigned : NumExprBits;
175
176    unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
177    unsigned IsExact : 1;
178  };
179
180  class UnaryExprOrTypeTraitExprBitfields {
181    friend class UnaryExprOrTypeTraitExpr;
182    unsigned : NumExprBits;
183
184    unsigned Kind : 2;
185    unsigned IsType : 1; // true if operand is a type, false if an expression.
186  };
187
188  class DeclRefExprBitfields {
189    friend class DeclRefExpr;
190    friend class ASTStmtReader; // deserialization
191    unsigned : NumExprBits;
192
193    unsigned HasQualifier : 1;
194    unsigned HasTemplateKWAndArgsInfo : 1;
195    unsigned HasFoundDecl : 1;
196    unsigned HadMultipleCandidates : 1;
197    unsigned RefersToEnclosingLocal : 1;
198  };
199
200  class CastExprBitfields {
201    friend class CastExpr;
202    unsigned : NumExprBits;
203
204    unsigned Kind : 6;
205    unsigned BasePathSize : 32 - 6 - NumExprBits;
206  };
207
208  class CallExprBitfields {
209    friend class CallExpr;
210    unsigned : NumExprBits;
211
212    unsigned NumPreArgs : 1;
213  };
214
215  class ExprWithCleanupsBitfields {
216    friend class ExprWithCleanups;
217    friend class ASTStmtReader; // deserialization
218
219    unsigned : NumExprBits;
220
221    unsigned NumObjects : 32 - NumExprBits;
222  };
223
224  class PseudoObjectExprBitfields {
225    friend class PseudoObjectExpr;
226    friend class ASTStmtReader; // deserialization
227
228    unsigned : NumExprBits;
229
230    // These don't need to be particularly wide, because they're
231    // strictly limited by the forms of expressions we permit.
232    unsigned NumSubExprs : 8;
233    unsigned ResultIndex : 32 - 8 - NumExprBits;
234  };
235
236  class ObjCIndirectCopyRestoreExprBitfields {
237    friend class ObjCIndirectCopyRestoreExpr;
238    unsigned : NumExprBits;
239
240    unsigned ShouldCopy : 1;
241  };
242
243  class InitListExprBitfields {
244    friend class InitListExpr;
245
246    unsigned : NumExprBits;
247
248    /// Whether this initializer list originally had a GNU array-range
249    /// designator in it. This is a temporary marker used by CodeGen.
250    unsigned HadArrayRangeDesignator : 1;
251
252    /// Whether this initializer list initializes a std::initializer_list
253    /// object.
254    unsigned InitializesStdInitializerList : 1;
255  };
256
257  class TypeTraitExprBitfields {
258    friend class TypeTraitExpr;
259    friend class ASTStmtReader;
260    friend class ASTStmtWriter;
261
262    unsigned : NumExprBits;
263
264    /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
265    unsigned Kind : 8;
266
267    /// \brief If this expression is not value-dependent, this indicates whether
268    /// the trait evaluated true or false.
269    unsigned Value : 1;
270
271    /// \brief The number of arguments to this type trait.
272    unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
273  };
274
275  union {
276    // FIXME: this is wasteful on 64-bit platforms.
277    void *Aligner;
278
279    StmtBitfields StmtBits;
280    CompoundStmtBitfields CompoundStmtBits;
281    ExprBitfields ExprBits;
282    CharacterLiteralBitfields CharacterLiteralBits;
283    FloatingLiteralBitfields FloatingLiteralBits;
284    UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
285    DeclRefExprBitfields DeclRefExprBits;
286    CastExprBitfields CastExprBits;
287    CallExprBitfields CallExprBits;
288    ExprWithCleanupsBitfields ExprWithCleanupsBits;
289    PseudoObjectExprBitfields PseudoObjectExprBits;
290    ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
291    InitListExprBitfields InitListExprBits;
292    TypeTraitExprBitfields TypeTraitExprBits;
293  };
294
295  friend class ASTStmtReader;
296  friend class ASTStmtWriter;
297
298public:
299  // Only allow allocation of Stmts using the allocator in ASTContext
300  // or by doing a placement new.
301  void* operator new(size_t bytes, ASTContext& C,
302                     unsigned alignment = 8) throw() {
303    return ::operator new(bytes, C, alignment);
304  }
305
306  void* operator new(size_t bytes, ASTContext* C,
307                     unsigned alignment = 8) throw() {
308    return ::operator new(bytes, *C, alignment);
309  }
310
311  void* operator new(size_t bytes, void* mem) throw() {
312    return mem;
313  }
314
315  void operator delete(void*, ASTContext&, unsigned) throw() { }
316  void operator delete(void*, ASTContext*, unsigned) throw() { }
317  void operator delete(void*, std::size_t) throw() { }
318  void operator delete(void*, void*) throw() { }
319
320public:
321  /// \brief A placeholder type used to construct an empty shell of a
322  /// type, that will be filled in later (e.g., by some
323  /// de-serialization).
324  struct EmptyShell { };
325
326private:
327  /// \brief Whether statistic collection is enabled.
328  static bool StatisticsEnabled;
329
330protected:
331  /// \brief Construct an empty statement.
332  explicit Stmt(StmtClass SC, EmptyShell) {
333    StmtBits.sClass = SC;
334    if (StatisticsEnabled) Stmt::addStmtClass(SC);
335  }
336
337public:
338  Stmt(StmtClass SC) {
339    StmtBits.sClass = SC;
340    if (StatisticsEnabled) Stmt::addStmtClass(SC);
341  }
342
343  StmtClass getStmtClass() const {
344    return static_cast<StmtClass>(StmtBits.sClass);
345  }
346  const char *getStmtClassName() const;
347
348  /// SourceLocation tokens are not useful in isolation - they are low level
349  /// value objects created/interpreted by SourceManager. We assume AST
350  /// clients will have a pointer to the respective SourceManager.
351  SourceRange getSourceRange() const LLVM_READONLY;
352  SourceLocation getLocStart() const LLVM_READONLY;
353  SourceLocation getLocEnd() const LLVM_READONLY;
354
355  // global temp stats (until we have a per-module visitor)
356  static void addStmtClass(const StmtClass s);
357  static void EnableStatistics();
358  static void PrintStats();
359
360  /// dump - This does a local dump of the specified AST fragment.  It dumps the
361  /// specified node and a few nodes underneath it, but not the whole subtree.
362  /// This is useful in a debugger.
363  LLVM_ATTRIBUTE_USED void dump() const;
364  LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
365  void dump(raw_ostream &OS, SourceManager &SM) const;
366
367  /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
368  void dumpAll() const;
369  void dumpAll(SourceManager &SM) const;
370
371  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
372  /// back to its original source language syntax.
373  void dumpPretty(ASTContext& Context) const;
374  void printPretty(raw_ostream &OS, PrinterHelper *Helper,
375                   const PrintingPolicy &Policy,
376                   unsigned Indentation = 0) const {
377    printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
378  }
379  void printPretty(raw_ostream &OS, ASTContext &Context,
380                   PrinterHelper *Helper,
381                   const PrintingPolicy &Policy,
382                   unsigned Indentation = 0) const;
383
384  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
385  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
386  void viewAST() const;
387
388  /// Skip past any implicit AST nodes which might surround this
389  /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
390  Stmt *IgnoreImplicit();
391
392  const Stmt *stripLabelLikeStatements() const;
393  Stmt *stripLabelLikeStatements() {
394    return const_cast<Stmt*>(
395      const_cast<const Stmt*>(this)->stripLabelLikeStatements());
396  }
397
398  // Implement isa<T> support.
399  static bool classof(const Stmt *) { return true; }
400
401  /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
402  ///  contain implicit control-flow in the order their subexpressions
403  ///  are evaluated.  This predicate returns true if this statement has
404  ///  such implicit control-flow.  Such statements are also specially handled
405  ///  within CFGs.
406  bool hasImplicitControlFlow() const;
407
408  /// Child Iterators: All subclasses must implement 'children'
409  /// to permit easy iteration over the substatements/subexpessions of an
410  /// AST node.  This permits easy iteration over all nodes in the AST.
411  typedef StmtIterator       child_iterator;
412  typedef ConstStmtIterator  const_child_iterator;
413
414  typedef StmtRange          child_range;
415  typedef ConstStmtRange     const_child_range;
416
417  child_range children();
418  const_child_range children() const {
419    return const_cast<Stmt*>(this)->children();
420  }
421
422  child_iterator child_begin() { return children().first; }
423  child_iterator child_end() { return children().second; }
424
425  const_child_iterator child_begin() const { return children().first; }
426  const_child_iterator child_end() const { return children().second; }
427
428  /// \brief Produce a unique representation of the given statement.
429  ///
430  /// \brief ID once the profiling operation is complete, will contain
431  /// the unique representation of the given statement.
432  ///
433  /// \brief Context the AST context in which the statement resides
434  ///
435  /// \brief Canonical whether the profile should be based on the canonical
436  /// representation of this statement (e.g., where non-type template
437  /// parameters are identified by index/level rather than their
438  /// declaration pointers) or the exact representation of the statement as
439  /// written in the source.
440  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
441               bool Canonical) const;
442};
443
444/// DeclStmt - Adaptor class for mixing declarations with statements and
445/// expressions. For example, CompoundStmt mixes statements, expressions
446/// and declarations (variables, types). Another example is ForStmt, where
447/// the first statement can be an expression or a declaration.
448///
449class DeclStmt : public Stmt {
450  DeclGroupRef DG;
451  SourceLocation StartLoc, EndLoc;
452
453public:
454  DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
455           SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
456                                    StartLoc(startLoc), EndLoc(endLoc) {}
457
458  /// \brief Build an empty declaration statement.
459  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
460
461  /// isSingleDecl - This method returns true if this DeclStmt refers
462  /// to a single Decl.
463  bool isSingleDecl() const {
464    return DG.isSingleDecl();
465  }
466
467  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
468  Decl *getSingleDecl() { return DG.getSingleDecl(); }
469
470  const DeclGroupRef getDeclGroup() const { return DG; }
471  DeclGroupRef getDeclGroup() { return DG; }
472  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
473
474  SourceLocation getStartLoc() const { return StartLoc; }
475  void setStartLoc(SourceLocation L) { StartLoc = L; }
476  SourceLocation getEndLoc() const { return EndLoc; }
477  void setEndLoc(SourceLocation L) { EndLoc = L; }
478
479  SourceRange getSourceRange() const LLVM_READONLY {
480    return SourceRange(StartLoc, EndLoc);
481  }
482
483  static bool classof(const Stmt *T) {
484    return T->getStmtClass() == DeclStmtClass;
485  }
486  static bool classof(const DeclStmt *) { return true; }
487
488  // Iterators over subexpressions.
489  child_range children() {
490    return child_range(child_iterator(DG.begin(), DG.end()),
491                       child_iterator(DG.end(), DG.end()));
492  }
493
494  typedef DeclGroupRef::iterator decl_iterator;
495  typedef DeclGroupRef::const_iterator const_decl_iterator;
496
497  decl_iterator decl_begin() { return DG.begin(); }
498  decl_iterator decl_end() { return DG.end(); }
499  const_decl_iterator decl_begin() const { return DG.begin(); }
500  const_decl_iterator decl_end() const { return DG.end(); }
501};
502
503/// NullStmt - This is the null statement ";": C99 6.8.3p3.
504///
505class NullStmt : public Stmt {
506  SourceLocation SemiLoc;
507
508  /// \brief True if the null statement was preceded by an empty macro, e.g:
509  /// @code
510  ///   #define CALL(x)
511  ///   CALL(0);
512  /// @endcode
513  bool HasLeadingEmptyMacro;
514public:
515  NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
516    : Stmt(NullStmtClass), SemiLoc(L),
517      HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
518
519  /// \brief Build an empty null statement.
520  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
521      HasLeadingEmptyMacro(false) { }
522
523  SourceLocation getSemiLoc() const { return SemiLoc; }
524  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
525
526  bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
527
528  SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
529
530  static bool classof(const Stmt *T) {
531    return T->getStmtClass() == NullStmtClass;
532  }
533  static bool classof(const NullStmt *) { return true; }
534
535  child_range children() { return child_range(); }
536
537  friend class ASTStmtReader;
538  friend class ASTStmtWriter;
539};
540
541/// CompoundStmt - This represents a group of statements like { stmt stmt }.
542///
543class CompoundStmt : public Stmt {
544  Stmt** Body;
545  SourceLocation LBracLoc, RBracLoc;
546public:
547  CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned NumStmts,
548               SourceLocation LB, SourceLocation RB)
549  : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
550    CompoundStmtBits.NumStmts = NumStmts;
551    assert(CompoundStmtBits.NumStmts == NumStmts &&
552           "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
553
554    if (NumStmts == 0) {
555      Body = 0;
556      return;
557    }
558
559    Body = new (C) Stmt*[NumStmts];
560    memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
561  }
562
563  // \brief Build an empty compound statement.
564  explicit CompoundStmt(EmptyShell Empty)
565    : Stmt(CompoundStmtClass, Empty), Body(0) {
566    CompoundStmtBits.NumStmts = 0;
567  }
568
569  void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
570
571  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
572  unsigned size() const { return CompoundStmtBits.NumStmts; }
573
574  typedef Stmt** body_iterator;
575  body_iterator body_begin() { return Body; }
576  body_iterator body_end() { return Body + size(); }
577  Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
578
579  void setLastStmt(Stmt *S) {
580    assert(!body_empty() && "setLastStmt");
581    Body[size()-1] = S;
582  }
583
584  typedef Stmt* const * const_body_iterator;
585  const_body_iterator body_begin() const { return Body; }
586  const_body_iterator body_end() const { return Body + size(); }
587  const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
588
589  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
590  reverse_body_iterator body_rbegin() {
591    return reverse_body_iterator(body_end());
592  }
593  reverse_body_iterator body_rend() {
594    return reverse_body_iterator(body_begin());
595  }
596
597  typedef std::reverse_iterator<const_body_iterator>
598          const_reverse_body_iterator;
599
600  const_reverse_body_iterator body_rbegin() const {
601    return const_reverse_body_iterator(body_end());
602  }
603
604  const_reverse_body_iterator body_rend() const {
605    return const_reverse_body_iterator(body_begin());
606  }
607
608  SourceRange getSourceRange() const LLVM_READONLY {
609    return SourceRange(LBracLoc, RBracLoc);
610  }
611
612  SourceLocation getLBracLoc() const { return LBracLoc; }
613  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
614  SourceLocation getRBracLoc() const { return RBracLoc; }
615  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
616
617  static bool classof(const Stmt *T) {
618    return T->getStmtClass() == CompoundStmtClass;
619  }
620  static bool classof(const CompoundStmt *) { return true; }
621
622  // Iterators
623  child_range children() {
624    return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
625  }
626
627  const_child_range children() const {
628    return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
629  }
630};
631
632// SwitchCase is the base class for CaseStmt and DefaultStmt,
633class SwitchCase : public Stmt {
634protected:
635  // A pointer to the following CaseStmt or DefaultStmt class,
636  // used by SwitchStmt.
637  SwitchCase *NextSwitchCase;
638
639  SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
640
641public:
642  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
643
644  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
645
646  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
647
648  Stmt *getSubStmt();
649  const Stmt *getSubStmt() const {
650    return const_cast<SwitchCase*>(this)->getSubStmt();
651  }
652
653  SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
654
655  static bool classof(const Stmt *T) {
656    return T->getStmtClass() == CaseStmtClass ||
657           T->getStmtClass() == DefaultStmtClass;
658  }
659  static bool classof(const SwitchCase *) { return true; }
660};
661
662class CaseStmt : public SwitchCase {
663  enum { LHS, RHS, SUBSTMT, END_EXPR };
664  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
665                             // GNU "case 1 ... 4" extension
666  SourceLocation CaseLoc;
667  SourceLocation EllipsisLoc;
668  SourceLocation ColonLoc;
669public:
670  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
671           SourceLocation ellipsisLoc, SourceLocation colonLoc)
672    : SwitchCase(CaseStmtClass) {
673    SubExprs[SUBSTMT] = 0;
674    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
675    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
676    CaseLoc = caseLoc;
677    EllipsisLoc = ellipsisLoc;
678    ColonLoc = colonLoc;
679  }
680
681  /// \brief Build an empty switch case statement.
682  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
683
684  SourceLocation getCaseLoc() const { return CaseLoc; }
685  void setCaseLoc(SourceLocation L) { CaseLoc = L; }
686  SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
687  void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
688  SourceLocation getColonLoc() const { return ColonLoc; }
689  void setColonLoc(SourceLocation L) { ColonLoc = L; }
690
691  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
692  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
693  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
694
695  const Expr *getLHS() const {
696    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
697  }
698  const Expr *getRHS() const {
699    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
700  }
701  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
702
703  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
704  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
705  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
706
707
708  SourceRange getSourceRange() const LLVM_READONLY {
709    // Handle deeply nested case statements with iteration instead of recursion.
710    const CaseStmt *CS = this;
711    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
712      CS = CS2;
713
714    return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
715  }
716  static bool classof(const Stmt *T) {
717    return T->getStmtClass() == CaseStmtClass;
718  }
719  static bool classof(const CaseStmt *) { return true; }
720
721  // Iterators
722  child_range children() {
723    return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
724  }
725};
726
727class DefaultStmt : public SwitchCase {
728  Stmt* SubStmt;
729  SourceLocation DefaultLoc;
730  SourceLocation ColonLoc;
731public:
732  DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
733    SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
734    ColonLoc(CL) {}
735
736  /// \brief Build an empty default statement.
737  explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
738
739  Stmt *getSubStmt() { return SubStmt; }
740  const Stmt *getSubStmt() const { return SubStmt; }
741  void setSubStmt(Stmt *S) { SubStmt = S; }
742
743  SourceLocation getDefaultLoc() const { return DefaultLoc; }
744  void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
745  SourceLocation getColonLoc() const { return ColonLoc; }
746  void setColonLoc(SourceLocation L) { ColonLoc = L; }
747
748  SourceRange getSourceRange() const LLVM_READONLY {
749    return SourceRange(DefaultLoc, SubStmt->getLocEnd());
750  }
751  static bool classof(const Stmt *T) {
752    return T->getStmtClass() == DefaultStmtClass;
753  }
754  static bool classof(const DefaultStmt *) { return true; }
755
756  // Iterators
757  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
758};
759
760
761/// LabelStmt - Represents a label, which has a substatement.  For example:
762///    foo: return;
763///
764class LabelStmt : public Stmt {
765  LabelDecl *TheDecl;
766  Stmt *SubStmt;
767  SourceLocation IdentLoc;
768public:
769  LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
770    : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
771  }
772
773  // \brief Build an empty label statement.
774  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
775
776  SourceLocation getIdentLoc() const { return IdentLoc; }
777  LabelDecl *getDecl() const { return TheDecl; }
778  void setDecl(LabelDecl *D) { TheDecl = D; }
779  const char *getName() const;
780  Stmt *getSubStmt() { return SubStmt; }
781  const Stmt *getSubStmt() const { return SubStmt; }
782  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
783  void setSubStmt(Stmt *SS) { SubStmt = SS; }
784
785  SourceRange getSourceRange() const LLVM_READONLY {
786    return SourceRange(IdentLoc, SubStmt->getLocEnd());
787  }
788  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
789
790  static bool classof(const Stmt *T) {
791    return T->getStmtClass() == LabelStmtClass;
792  }
793  static bool classof(const LabelStmt *) { return true; }
794};
795
796
797/// IfStmt - This represents an if/then/else.
798///
799class IfStmt : public Stmt {
800  enum { VAR, COND, THEN, ELSE, END_EXPR };
801  Stmt* SubExprs[END_EXPR];
802
803  SourceLocation IfLoc;
804  SourceLocation ElseLoc;
805
806public:
807  IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
808         Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
809
810  /// \brief Build an empty if/then/else statement
811  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
812
813  /// \brief Retrieve the variable declared in this "if" statement, if any.
814  ///
815  /// In the following example, "x" is the condition variable.
816  /// \code
817  /// if (int x = foo()) {
818  ///   printf("x is %d", x);
819  /// }
820  /// \endcode
821  VarDecl *getConditionVariable() const;
822  void setConditionVariable(ASTContext &C, VarDecl *V);
823
824  /// If this IfStmt has a condition variable, return the faux DeclStmt
825  /// associated with the creation of that condition variable.
826  const DeclStmt *getConditionVariableDeclStmt() const {
827    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
828  }
829
830  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
831  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
832  const Stmt *getThen() const { return SubExprs[THEN]; }
833  void setThen(Stmt *S) { SubExprs[THEN] = S; }
834  const Stmt *getElse() const { return SubExprs[ELSE]; }
835  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
836
837  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
838  Stmt *getThen() { return SubExprs[THEN]; }
839  Stmt *getElse() { return SubExprs[ELSE]; }
840
841  SourceLocation getIfLoc() const { return IfLoc; }
842  void setIfLoc(SourceLocation L) { IfLoc = L; }
843  SourceLocation getElseLoc() const { return ElseLoc; }
844  void setElseLoc(SourceLocation L) { ElseLoc = L; }
845
846  SourceRange getSourceRange() const LLVM_READONLY {
847    if (SubExprs[ELSE])
848      return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
849    else
850      return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
851  }
852
853  // Iterators over subexpressions.  The iterators will include iterating
854  // over the initialization expression referenced by the condition variable.
855  child_range children() {
856    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
857  }
858
859  static bool classof(const Stmt *T) {
860    return T->getStmtClass() == IfStmtClass;
861  }
862  static bool classof(const IfStmt *) { return true; }
863};
864
865/// SwitchStmt - This represents a 'switch' stmt.
866///
867class SwitchStmt : public Stmt {
868  enum { VAR, COND, BODY, END_EXPR };
869  Stmt* SubExprs[END_EXPR];
870  // This points to a linked list of case and default statements.
871  SwitchCase *FirstCase;
872  SourceLocation SwitchLoc;
873
874  /// If the SwitchStmt is a switch on an enum value, this records whether
875  /// all the enum values were covered by CaseStmts.  This value is meant to
876  /// be a hint for possible clients.
877  unsigned AllEnumCasesCovered : 1;
878
879public:
880  SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
881
882  /// \brief Build a empty switch statement.
883  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
884
885  /// \brief Retrieve the variable declared in this "switch" statement, if any.
886  ///
887  /// In the following example, "x" is the condition variable.
888  /// \code
889  /// switch (int x = foo()) {
890  ///   case 0: break;
891  ///   // ...
892  /// }
893  /// \endcode
894  VarDecl *getConditionVariable() const;
895  void setConditionVariable(ASTContext &C, VarDecl *V);
896
897  /// If this SwitchStmt has a condition variable, return the faux DeclStmt
898  /// associated with the creation of that condition variable.
899  const DeclStmt *getConditionVariableDeclStmt() const {
900    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
901  }
902
903  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
904  const Stmt *getBody() const { return SubExprs[BODY]; }
905  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
906
907  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
908  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
909  Stmt *getBody() { return SubExprs[BODY]; }
910  void setBody(Stmt *S) { SubExprs[BODY] = S; }
911  SwitchCase *getSwitchCaseList() { return FirstCase; }
912
913  /// \brief Set the case list for this switch statement.
914  ///
915  /// The caller is responsible for incrementing the retain counts on
916  /// all of the SwitchCase statements in this list.
917  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
918
919  SourceLocation getSwitchLoc() const { return SwitchLoc; }
920  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
921
922  void setBody(Stmt *S, SourceLocation SL) {
923    SubExprs[BODY] = S;
924    SwitchLoc = SL;
925  }
926  void addSwitchCase(SwitchCase *SC) {
927    assert(!SC->getNextSwitchCase()
928           && "case/default already added to a switch");
929    SC->setNextSwitchCase(FirstCase);
930    FirstCase = SC;
931  }
932
933  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
934  /// switch over an enum value then all cases have been explicitly covered.
935  void setAllEnumCasesCovered() {
936    AllEnumCasesCovered = 1;
937  }
938
939  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
940  /// have been explicitly covered.
941  bool isAllEnumCasesCovered() const {
942    return (bool) AllEnumCasesCovered;
943  }
944
945  SourceRange getSourceRange() const LLVM_READONLY {
946    return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
947  }
948  // Iterators
949  child_range children() {
950    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
951  }
952
953  static bool classof(const Stmt *T) {
954    return T->getStmtClass() == SwitchStmtClass;
955  }
956  static bool classof(const SwitchStmt *) { return true; }
957};
958
959
960/// WhileStmt - This represents a 'while' stmt.
961///
962class WhileStmt : public Stmt {
963  enum { VAR, COND, BODY, END_EXPR };
964  Stmt* SubExprs[END_EXPR];
965  SourceLocation WhileLoc;
966public:
967  WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
968            SourceLocation WL);
969
970  /// \brief Build an empty while statement.
971  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
972
973  /// \brief Retrieve the variable declared in this "while" statement, if any.
974  ///
975  /// In the following example, "x" is the condition variable.
976  /// \code
977  /// while (int x = random()) {
978  ///   // ...
979  /// }
980  /// \endcode
981  VarDecl *getConditionVariable() const;
982  void setConditionVariable(ASTContext &C, VarDecl *V);
983
984  /// If this WhileStmt has a condition variable, return the faux DeclStmt
985  /// associated with the creation of that condition variable.
986  const DeclStmt *getConditionVariableDeclStmt() const {
987    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
988  }
989
990  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
991  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
992  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
993  Stmt *getBody() { return SubExprs[BODY]; }
994  const Stmt *getBody() const { return SubExprs[BODY]; }
995  void setBody(Stmt *S) { SubExprs[BODY] = S; }
996
997  SourceLocation getWhileLoc() const { return WhileLoc; }
998  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
999
1000  SourceRange getSourceRange() const LLVM_READONLY {
1001    return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
1002  }
1003  static bool classof(const Stmt *T) {
1004    return T->getStmtClass() == WhileStmtClass;
1005  }
1006  static bool classof(const WhileStmt *) { return true; }
1007
1008  // Iterators
1009  child_range children() {
1010    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1011  }
1012};
1013
1014/// DoStmt - This represents a 'do/while' stmt.
1015///
1016class DoStmt : public Stmt {
1017  enum { BODY, COND, END_EXPR };
1018  Stmt* SubExprs[END_EXPR];
1019  SourceLocation DoLoc;
1020  SourceLocation WhileLoc;
1021  SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
1022
1023public:
1024  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1025         SourceLocation RP)
1026    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1027    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1028    SubExprs[BODY] = body;
1029  }
1030
1031  /// \brief Build an empty do-while statement.
1032  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1033
1034  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1035  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1036  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1037  Stmt *getBody() { return SubExprs[BODY]; }
1038  const Stmt *getBody() const { return SubExprs[BODY]; }
1039  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1040
1041  SourceLocation getDoLoc() const { return DoLoc; }
1042  void setDoLoc(SourceLocation L) { DoLoc = L; }
1043  SourceLocation getWhileLoc() const { return WhileLoc; }
1044  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1045
1046  SourceLocation getRParenLoc() const { return RParenLoc; }
1047  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1048
1049  SourceRange getSourceRange() const LLVM_READONLY {
1050    return SourceRange(DoLoc, RParenLoc);
1051  }
1052  static bool classof(const Stmt *T) {
1053    return T->getStmtClass() == DoStmtClass;
1054  }
1055  static bool classof(const DoStmt *) { return true; }
1056
1057  // Iterators
1058  child_range children() {
1059    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1060  }
1061};
1062
1063
1064/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
1065/// the init/cond/inc parts of the ForStmt will be null if they were not
1066/// specified in the source.
1067///
1068class ForStmt : public Stmt {
1069  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1070  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1071  SourceLocation ForLoc;
1072  SourceLocation LParenLoc, RParenLoc;
1073
1074public:
1075  ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1076          Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1077
1078  /// \brief Build an empty for statement.
1079  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1080
1081  Stmt *getInit() { return SubExprs[INIT]; }
1082
1083  /// \brief Retrieve the variable declared in this "for" statement, if any.
1084  ///
1085  /// In the following example, "y" is the condition variable.
1086  /// \code
1087  /// for (int x = random(); int y = mangle(x); ++x) {
1088  ///   // ...
1089  /// }
1090  /// \endcode
1091  VarDecl *getConditionVariable() const;
1092  void setConditionVariable(ASTContext &C, VarDecl *V);
1093
1094  /// If this ForStmt has a condition variable, return the faux DeclStmt
1095  /// associated with the creation of that condition variable.
1096  const DeclStmt *getConditionVariableDeclStmt() const {
1097    return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1098  }
1099
1100  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1101  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1102  Stmt *getBody() { return SubExprs[BODY]; }
1103
1104  const Stmt *getInit() const { return SubExprs[INIT]; }
1105  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1106  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1107  const Stmt *getBody() const { return SubExprs[BODY]; }
1108
1109  void setInit(Stmt *S) { SubExprs[INIT] = S; }
1110  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1111  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1112  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1113
1114  SourceLocation getForLoc() const { return ForLoc; }
1115  void setForLoc(SourceLocation L) { ForLoc = L; }
1116  SourceLocation getLParenLoc() const { return LParenLoc; }
1117  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1118  SourceLocation getRParenLoc() const { return RParenLoc; }
1119  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1120
1121  SourceRange getSourceRange() const LLVM_READONLY {
1122    return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1123  }
1124  static bool classof(const Stmt *T) {
1125    return T->getStmtClass() == ForStmtClass;
1126  }
1127  static bool classof(const ForStmt *) { return true; }
1128
1129  // Iterators
1130  child_range children() {
1131    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1132  }
1133};
1134
1135/// GotoStmt - This represents a direct goto.
1136///
1137class GotoStmt : public Stmt {
1138  LabelDecl *Label;
1139  SourceLocation GotoLoc;
1140  SourceLocation LabelLoc;
1141public:
1142  GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1143    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1144
1145  /// \brief Build an empty goto statement.
1146  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1147
1148  LabelDecl *getLabel() const { return Label; }
1149  void setLabel(LabelDecl *D) { Label = D; }
1150
1151  SourceLocation getGotoLoc() const { return GotoLoc; }
1152  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1153  SourceLocation getLabelLoc() const { return LabelLoc; }
1154  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1155
1156  SourceRange getSourceRange() const LLVM_READONLY {
1157    return SourceRange(GotoLoc, LabelLoc);
1158  }
1159  static bool classof(const Stmt *T) {
1160    return T->getStmtClass() == GotoStmtClass;
1161  }
1162  static bool classof(const GotoStmt *) { return true; }
1163
1164  // Iterators
1165  child_range children() { return child_range(); }
1166};
1167
1168/// IndirectGotoStmt - This represents an indirect goto.
1169///
1170class IndirectGotoStmt : public Stmt {
1171  SourceLocation GotoLoc;
1172  SourceLocation StarLoc;
1173  Stmt *Target;
1174public:
1175  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1176                   Expr *target)
1177    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1178      Target((Stmt*)target) {}
1179
1180  /// \brief Build an empty indirect goto statement.
1181  explicit IndirectGotoStmt(EmptyShell Empty)
1182    : Stmt(IndirectGotoStmtClass, Empty) { }
1183
1184  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1185  SourceLocation getGotoLoc() const { return GotoLoc; }
1186  void setStarLoc(SourceLocation L) { StarLoc = L; }
1187  SourceLocation getStarLoc() const { return StarLoc; }
1188
1189  Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1190  const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1191  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1192
1193  /// getConstantTarget - Returns the fixed target of this indirect
1194  /// goto, if one exists.
1195  LabelDecl *getConstantTarget();
1196  const LabelDecl *getConstantTarget() const {
1197    return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1198  }
1199
1200  SourceRange getSourceRange() const LLVM_READONLY {
1201    return SourceRange(GotoLoc, Target->getLocEnd());
1202  }
1203
1204  static bool classof(const Stmt *T) {
1205    return T->getStmtClass() == IndirectGotoStmtClass;
1206  }
1207  static bool classof(const IndirectGotoStmt *) { return true; }
1208
1209  // Iterators
1210  child_range children() { return child_range(&Target, &Target+1); }
1211};
1212
1213
1214/// ContinueStmt - This represents a continue.
1215///
1216class ContinueStmt : public Stmt {
1217  SourceLocation ContinueLoc;
1218public:
1219  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1220
1221  /// \brief Build an empty continue statement.
1222  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1223
1224  SourceLocation getContinueLoc() const { return ContinueLoc; }
1225  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1226
1227  SourceRange getSourceRange() const LLVM_READONLY {
1228    return SourceRange(ContinueLoc);
1229  }
1230
1231  static bool classof(const Stmt *T) {
1232    return T->getStmtClass() == ContinueStmtClass;
1233  }
1234  static bool classof(const ContinueStmt *) { return true; }
1235
1236  // Iterators
1237  child_range children() { return child_range(); }
1238};
1239
1240/// BreakStmt - This represents a break.
1241///
1242class BreakStmt : public Stmt {
1243  SourceLocation BreakLoc;
1244public:
1245  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1246
1247  /// \brief Build an empty break statement.
1248  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1249
1250  SourceLocation getBreakLoc() const { return BreakLoc; }
1251  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1252
1253  SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
1254
1255  static bool classof(const Stmt *T) {
1256    return T->getStmtClass() == BreakStmtClass;
1257  }
1258  static bool classof(const BreakStmt *) { return true; }
1259
1260  // Iterators
1261  child_range children() { return child_range(); }
1262};
1263
1264
1265/// ReturnStmt - This represents a return, optionally of an expression:
1266///   return;
1267///   return 4;
1268///
1269/// Note that GCC allows return with no argument in a function declared to
1270/// return a value, and it allows returning a value in functions declared to
1271/// return void.  We explicitly model this in the AST, which means you can't
1272/// depend on the return type of the function and the presence of an argument.
1273///
1274class ReturnStmt : public Stmt {
1275  Stmt *RetExpr;
1276  SourceLocation RetLoc;
1277  const VarDecl *NRVOCandidate;
1278
1279public:
1280  ReturnStmt(SourceLocation RL)
1281    : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1282
1283  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1284    : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1285      NRVOCandidate(NRVOCandidate) {}
1286
1287  /// \brief Build an empty return expression.
1288  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1289
1290  const Expr *getRetValue() const;
1291  Expr *getRetValue();
1292  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1293
1294  SourceLocation getReturnLoc() const { return RetLoc; }
1295  void setReturnLoc(SourceLocation L) { RetLoc = L; }
1296
1297  /// \brief Retrieve the variable that might be used for the named return
1298  /// value optimization.
1299  ///
1300  /// The optimization itself can only be performed if the variable is
1301  /// also marked as an NRVO object.
1302  const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1303  void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1304
1305  SourceRange getSourceRange() const LLVM_READONLY;
1306
1307  static bool classof(const Stmt *T) {
1308    return T->getStmtClass() == ReturnStmtClass;
1309  }
1310  static bool classof(const ReturnStmt *) { return true; }
1311
1312  // Iterators
1313  child_range children() {
1314    if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1315    return child_range();
1316  }
1317};
1318
1319/// AsmStmt - This represents a GNU inline-assembly statement extension.
1320///
1321class AsmStmt : public Stmt {
1322  SourceLocation AsmLoc, RParenLoc;
1323  StringLiteral *AsmStr;
1324
1325  bool IsSimple;
1326  bool IsVolatile;
1327  bool MSAsm;
1328
1329  unsigned NumOutputs;
1330  unsigned NumInputs;
1331  unsigned NumClobbers;
1332
1333  // FIXME: If we wanted to, we could allocate all of these in one big array.
1334  IdentifierInfo **Names;
1335  StringLiteral **Constraints;
1336  Stmt **Exprs;
1337  StringLiteral **Clobbers;
1338
1339public:
1340  AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1341          bool msasm, unsigned numoutputs, unsigned numinputs,
1342          IdentifierInfo **names, StringLiteral **constraints,
1343          Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1344          StringLiteral **clobbers, SourceLocation rparenloc);
1345
1346  /// \brief Build an empty inline-assembly statement.
1347  explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1348    Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1349
1350  SourceLocation getAsmLoc() const { return AsmLoc; }
1351  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1352  SourceLocation getRParenLoc() const { return RParenLoc; }
1353  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1354
1355  bool isVolatile() const { return IsVolatile; }
1356  void setVolatile(bool V) { IsVolatile = V; }
1357  bool isSimple() const { return IsSimple; }
1358  void setSimple(bool V) { IsSimple = V; }
1359  bool isMSAsm() const { return MSAsm; }
1360  void setMSAsm(bool V) { MSAsm = V; }
1361
1362  //===--- Asm String Analysis ---===//
1363
1364  const StringLiteral *getAsmString() const { return AsmStr; }
1365  StringLiteral *getAsmString() { return AsmStr; }
1366  void setAsmString(StringLiteral *E) { AsmStr = E; }
1367
1368  /// AsmStringPiece - this is part of a decomposed asm string specification
1369  /// (for use with the AnalyzeAsmString function below).  An asm string is
1370  /// considered to be a concatenation of these parts.
1371  class AsmStringPiece {
1372  public:
1373    enum Kind {
1374      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1375      Operand  // Operand reference, with optional modifier %c4.
1376    };
1377  private:
1378    Kind MyKind;
1379    std::string Str;
1380    unsigned OperandNo;
1381  public:
1382    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1383    AsmStringPiece(unsigned OpNo, char Modifier)
1384      : MyKind(Operand), Str(), OperandNo(OpNo) {
1385      Str += Modifier;
1386    }
1387
1388    bool isString() const { return MyKind == String; }
1389    bool isOperand() const { return MyKind == Operand; }
1390
1391    const std::string &getString() const {
1392      assert(isString());
1393      return Str;
1394    }
1395
1396    unsigned getOperandNo() const {
1397      assert(isOperand());
1398      return OperandNo;
1399    }
1400
1401    /// getModifier - Get the modifier for this operand, if present.  This
1402    /// returns '\0' if there was no modifier.
1403    char getModifier() const {
1404      assert(isOperand());
1405      return Str[0];
1406    }
1407  };
1408
1409  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1410  /// it into pieces.  If the asm string is erroneous, emit errors and return
1411  /// true, otherwise return false.  This handles canonicalization and
1412  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1413  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1414  unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1415                            ASTContext &C, unsigned &DiagOffs) const;
1416
1417
1418  //===--- Output operands ---===//
1419
1420  unsigned getNumOutputs() const { return NumOutputs; }
1421
1422  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1423    return Names[i];
1424  }
1425
1426  StringRef getOutputName(unsigned i) const {
1427    if (IdentifierInfo *II = getOutputIdentifier(i))
1428      return II->getName();
1429
1430    return StringRef();
1431  }
1432
1433  /// getOutputConstraint - Return the constraint string for the specified
1434  /// output operand.  All output constraints are known to be non-empty (either
1435  /// '=' or '+').
1436  StringRef getOutputConstraint(unsigned i) const;
1437
1438  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1439    return Constraints[i];
1440  }
1441  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1442    return Constraints[i];
1443  }
1444
1445  Expr *getOutputExpr(unsigned i);
1446
1447  const Expr *getOutputExpr(unsigned i) const {
1448    return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1449  }
1450
1451  /// isOutputPlusConstraint - Return true if the specified output constraint
1452  /// is a "+" constraint (which is both an input and an output) or false if it
1453  /// is an "=" constraint (just an output).
1454  bool isOutputPlusConstraint(unsigned i) const {
1455    return getOutputConstraint(i)[0] == '+';
1456  }
1457
1458  /// getNumPlusOperands - Return the number of output operands that have a "+"
1459  /// constraint.
1460  unsigned getNumPlusOperands() const;
1461
1462  //===--- Input operands ---===//
1463
1464  unsigned getNumInputs() const { return NumInputs; }
1465
1466  IdentifierInfo *getInputIdentifier(unsigned i) const {
1467    return Names[i + NumOutputs];
1468  }
1469
1470  StringRef getInputName(unsigned i) const {
1471    if (IdentifierInfo *II = getInputIdentifier(i))
1472      return II->getName();
1473
1474    return StringRef();
1475  }
1476
1477  /// getInputConstraint - Return the specified input constraint.  Unlike output
1478  /// constraints, these can be empty.
1479  StringRef getInputConstraint(unsigned i) const;
1480
1481  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1482    return Constraints[i + NumOutputs];
1483  }
1484  StringLiteral *getInputConstraintLiteral(unsigned i) {
1485    return Constraints[i + NumOutputs];
1486  }
1487
1488  Expr *getInputExpr(unsigned i);
1489  void setInputExpr(unsigned i, Expr *E);
1490
1491  const Expr *getInputExpr(unsigned i) const {
1492    return const_cast<AsmStmt*>(this)->getInputExpr(i);
1493  }
1494
1495  void setOutputsAndInputsAndClobbers(ASTContext &C,
1496                                      IdentifierInfo **Names,
1497                                      StringLiteral **Constraints,
1498                                      Stmt **Exprs,
1499                                      unsigned NumOutputs,
1500                                      unsigned NumInputs,
1501                                      StringLiteral **Clobbers,
1502                                      unsigned NumClobbers);
1503
1504  //===--- Other ---===//
1505
1506  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1507  /// translate this into a numeric value needed to reference the same operand.
1508  /// This returns -1 if the operand name is invalid.
1509  int getNamedOperand(StringRef SymbolicName) const;
1510
1511  unsigned getNumClobbers() const { return NumClobbers; }
1512  StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1513  const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1514
1515  SourceRange getSourceRange() const LLVM_READONLY {
1516    return SourceRange(AsmLoc, RParenLoc);
1517  }
1518
1519  static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1520  static bool classof(const AsmStmt *) { return true; }
1521
1522  // Input expr iterators.
1523
1524  typedef ExprIterator inputs_iterator;
1525  typedef ConstExprIterator const_inputs_iterator;
1526
1527  inputs_iterator begin_inputs() {
1528    return &Exprs[0] + NumOutputs;
1529  }
1530
1531  inputs_iterator end_inputs() {
1532    return &Exprs[0] + NumOutputs + NumInputs;
1533  }
1534
1535  const_inputs_iterator begin_inputs() const {
1536    return &Exprs[0] + NumOutputs;
1537  }
1538
1539  const_inputs_iterator end_inputs() const {
1540    return &Exprs[0] + NumOutputs + NumInputs;
1541  }
1542
1543  // Output expr iterators.
1544
1545  typedef ExprIterator outputs_iterator;
1546  typedef ConstExprIterator const_outputs_iterator;
1547
1548  outputs_iterator begin_outputs() {
1549    return &Exprs[0];
1550  }
1551  outputs_iterator end_outputs() {
1552    return &Exprs[0] + NumOutputs;
1553  }
1554
1555  const_outputs_iterator begin_outputs() const {
1556    return &Exprs[0];
1557  }
1558  const_outputs_iterator end_outputs() const {
1559    return &Exprs[0] + NumOutputs;
1560  }
1561
1562  child_range children() {
1563    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1564  }
1565};
1566
1567class SEHExceptStmt : public Stmt {
1568  SourceLocation  Loc;
1569  Stmt           *Children[2];
1570
1571  enum { FILTER_EXPR, BLOCK };
1572
1573  SEHExceptStmt(SourceLocation Loc,
1574                Expr *FilterExpr,
1575                Stmt *Block);
1576
1577  friend class ASTReader;
1578  friend class ASTStmtReader;
1579  explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1580
1581public:
1582  static SEHExceptStmt* Create(ASTContext &C,
1583                               SourceLocation ExceptLoc,
1584                               Expr *FilterExpr,
1585                               Stmt *Block);
1586  SourceRange getSourceRange() const LLVM_READONLY {
1587    return SourceRange(getExceptLoc(), getEndLoc());
1588  }
1589
1590  SourceLocation getExceptLoc() const { return Loc; }
1591  SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1592
1593  Expr *getFilterExpr() const {
1594    return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1595  }
1596
1597  CompoundStmt *getBlock() const {
1598    return llvm::cast<CompoundStmt>(Children[BLOCK]);
1599  }
1600
1601  child_range children() {
1602    return child_range(Children,Children+2);
1603  }
1604
1605  static bool classof(const Stmt *T) {
1606    return T->getStmtClass() == SEHExceptStmtClass;
1607  }
1608
1609  static bool classof(SEHExceptStmt *) { return true; }
1610
1611};
1612
1613class SEHFinallyStmt : public Stmt {
1614  SourceLocation  Loc;
1615  Stmt           *Block;
1616
1617  SEHFinallyStmt(SourceLocation Loc,
1618                 Stmt *Block);
1619
1620  friend class ASTReader;
1621  friend class ASTStmtReader;
1622  explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1623
1624public:
1625  static SEHFinallyStmt* Create(ASTContext &C,
1626                                SourceLocation FinallyLoc,
1627                                Stmt *Block);
1628
1629  SourceRange getSourceRange() const LLVM_READONLY {
1630    return SourceRange(getFinallyLoc(), getEndLoc());
1631  }
1632
1633  SourceLocation getFinallyLoc() const { return Loc; }
1634  SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1635
1636  CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1637
1638  child_range children() {
1639    return child_range(&Block,&Block+1);
1640  }
1641
1642  static bool classof(const Stmt *T) {
1643    return T->getStmtClass() == SEHFinallyStmtClass;
1644  }
1645
1646  static bool classof(SEHFinallyStmt *) { return true; }
1647
1648};
1649
1650class SEHTryStmt : public Stmt {
1651  bool            IsCXXTry;
1652  SourceLocation  TryLoc;
1653  Stmt           *Children[2];
1654
1655  enum { TRY = 0, HANDLER = 1 };
1656
1657  SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1658             SourceLocation TryLoc,
1659             Stmt *TryBlock,
1660             Stmt *Handler);
1661
1662  friend class ASTReader;
1663  friend class ASTStmtReader;
1664  explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1665
1666public:
1667  static SEHTryStmt* Create(ASTContext &C,
1668                            bool isCXXTry,
1669                            SourceLocation TryLoc,
1670                            Stmt *TryBlock,
1671                            Stmt *Handler);
1672
1673  SourceRange getSourceRange() const LLVM_READONLY {
1674    return SourceRange(getTryLoc(), getEndLoc());
1675  }
1676
1677  SourceLocation getTryLoc() const { return TryLoc; }
1678  SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1679
1680  bool getIsCXXTry() const { return IsCXXTry; }
1681
1682  CompoundStmt* getTryBlock() const {
1683    return llvm::cast<CompoundStmt>(Children[TRY]);
1684  }
1685
1686  Stmt *getHandler() const { return Children[HANDLER]; }
1687
1688  /// Returns 0 if not defined
1689  SEHExceptStmt  *getExceptHandler() const;
1690  SEHFinallyStmt *getFinallyHandler() const;
1691
1692  child_range children() {
1693    return child_range(Children,Children+2);
1694  }
1695
1696  static bool classof(const Stmt *T) {
1697    return T->getStmtClass() == SEHTryStmtClass;
1698  }
1699
1700  static bool classof(SEHTryStmt *) { return true; }
1701};
1702
1703}  // end namespace clang
1704
1705#endif
1706