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