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