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(nullptr) {}
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(nullptr) {}
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  void dump() const;
375  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  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(const 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  typedef llvm::iterator_range<decl_iterator> decl_range;
489  typedef llvm::iterator_range<const_decl_iterator> decl_const_range;
490
491  decl_range decls() { return decl_range(decl_begin(), decl_end()); }
492  decl_const_range decls() const {
493    return decl_const_range(decl_begin(), decl_end());
494  }
495  decl_iterator decl_begin() { return DG.begin(); }
496  decl_iterator decl_end() { return DG.end(); }
497  const_decl_iterator decl_begin() const { return DG.begin(); }
498  const_decl_iterator decl_end() const { return DG.end(); }
499
500  typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
501  reverse_decl_iterator decl_rbegin() {
502    return reverse_decl_iterator(decl_end());
503  }
504  reverse_decl_iterator decl_rend() {
505    return reverse_decl_iterator(decl_begin());
506  }
507};
508
509/// NullStmt - This is the null statement ";": C99 6.8.3p3.
510///
511class NullStmt : public Stmt {
512  SourceLocation SemiLoc;
513
514  /// \brief True if the null statement was preceded by an empty macro, e.g:
515  /// @code
516  ///   #define CALL(x)
517  ///   CALL(0);
518  /// @endcode
519  bool HasLeadingEmptyMacro;
520public:
521  NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
522    : Stmt(NullStmtClass), SemiLoc(L),
523      HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
524
525  /// \brief Build an empty null statement.
526  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
527      HasLeadingEmptyMacro(false) { }
528
529  SourceLocation getSemiLoc() const { return SemiLoc; }
530  void setSemiLoc(SourceLocation L) { SemiLoc = L; }
531
532  bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
533
534  SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
535  SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
536
537  static bool classof(const Stmt *T) {
538    return T->getStmtClass() == NullStmtClass;
539  }
540
541  child_range children() { return child_range(); }
542
543  friend class ASTStmtReader;
544  friend class ASTStmtWriter;
545};
546
547/// CompoundStmt - This represents a group of statements like { stmt stmt }.
548///
549class CompoundStmt : public Stmt {
550  Stmt** Body;
551  SourceLocation LBracLoc, RBracLoc;
552public:
553  CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
554               SourceLocation LB, SourceLocation RB);
555
556  // \brief Build an empty compound statement with a location.
557  explicit CompoundStmt(SourceLocation Loc)
558    : Stmt(CompoundStmtClass), Body(nullptr), LBracLoc(Loc), RBracLoc(Loc) {
559    CompoundStmtBits.NumStmts = 0;
560  }
561
562  // \brief Build an empty compound statement.
563  explicit CompoundStmt(EmptyShell Empty)
564    : Stmt(CompoundStmtClass, Empty), Body(nullptr) {
565    CompoundStmtBits.NumStmts = 0;
566  }
567
568  void setStmts(const ASTContext &C, Stmt **Stmts, unsigned NumStmts);
569
570  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
571  unsigned size() const { return CompoundStmtBits.NumStmts; }
572
573  typedef Stmt** body_iterator;
574  typedef llvm::iterator_range<body_iterator> body_range;
575
576  body_range body() { return body_range(body_begin(), body_end()); }
577  body_iterator body_begin() { return Body; }
578  body_iterator body_end() { return Body + size(); }
579  Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
580
581  void setLastStmt(Stmt *S) {
582    assert(!body_empty() && "setLastStmt");
583    Body[size()-1] = S;
584  }
585
586  typedef Stmt* const * const_body_iterator;
587  typedef llvm::iterator_range<const_body_iterator> body_const_range;
588
589  body_const_range body() const {
590    return body_const_range(body_begin(), body_end());
591  }
592  const_body_iterator body_begin() const { return Body; }
593  const_body_iterator body_end() const { return Body + size(); }
594  const Stmt *body_back() const {
595    return !body_empty() ? Body[size() - 1] : nullptr;
596  }
597
598  typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
599  reverse_body_iterator body_rbegin() {
600    return reverse_body_iterator(body_end());
601  }
602  reverse_body_iterator body_rend() {
603    return reverse_body_iterator(body_begin());
604  }
605
606  typedef std::reverse_iterator<const_body_iterator>
607          const_reverse_body_iterator;
608
609  const_reverse_body_iterator body_rbegin() const {
610    return const_reverse_body_iterator(body_end());
611  }
612
613  const_reverse_body_iterator body_rend() const {
614    return const_reverse_body_iterator(body_begin());
615  }
616
617  SourceLocation getLocStart() const LLVM_READONLY { return LBracLoc; }
618  SourceLocation getLocEnd() const LLVM_READONLY { return RBracLoc; }
619
620  SourceLocation getLBracLoc() const { return LBracLoc; }
621  void setLBracLoc(SourceLocation L) { LBracLoc = L; }
622  SourceLocation getRBracLoc() const { return RBracLoc; }
623  void setRBracLoc(SourceLocation L) { RBracLoc = L; }
624
625  static bool classof(const Stmt *T) {
626    return T->getStmtClass() == CompoundStmtClass;
627  }
628
629  // Iterators
630  child_range children() {
631    return child_range(Body, Body + CompoundStmtBits.NumStmts);
632  }
633
634  const_child_range children() const {
635    return child_range(Body, Body + CompoundStmtBits.NumStmts);
636  }
637};
638
639// SwitchCase is the base class for CaseStmt and DefaultStmt,
640class SwitchCase : public Stmt {
641protected:
642  // A pointer to the following CaseStmt or DefaultStmt class,
643  // used by SwitchStmt.
644  SwitchCase *NextSwitchCase;
645  SourceLocation KeywordLoc;
646  SourceLocation ColonLoc;
647
648  SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
649    : Stmt(SC), NextSwitchCase(nullptr), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {
650  }
651
652  SwitchCase(StmtClass SC, EmptyShell)
653    : Stmt(SC), NextSwitchCase(nullptr) {}
654
655public:
656  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
657
658  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
659
660  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
661
662  SourceLocation getKeywordLoc() const { return KeywordLoc; }
663  void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
664  SourceLocation getColonLoc() const { return ColonLoc; }
665  void setColonLoc(SourceLocation L) { ColonLoc = L; }
666
667  Stmt *getSubStmt();
668  const Stmt *getSubStmt() const {
669    return const_cast<SwitchCase*>(this)->getSubStmt();
670  }
671
672  SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
673  SourceLocation getLocEnd() const LLVM_READONLY;
674
675  static bool classof(const Stmt *T) {
676    return T->getStmtClass() == CaseStmtClass ||
677           T->getStmtClass() == DefaultStmtClass;
678  }
679};
680
681class CaseStmt : public SwitchCase {
682  enum { LHS, RHS, SUBSTMT, END_EXPR };
683  Stmt* SubExprs[END_EXPR];  // The expression for the RHS is Non-null for
684                             // GNU "case 1 ... 4" extension
685  SourceLocation EllipsisLoc;
686public:
687  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
688           SourceLocation ellipsisLoc, SourceLocation colonLoc)
689    : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
690    SubExprs[SUBSTMT] = nullptr;
691    SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
692    SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
693    EllipsisLoc = ellipsisLoc;
694  }
695
696  /// \brief Build an empty switch case statement.
697  explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { }
698
699  SourceLocation getCaseLoc() const { return KeywordLoc; }
700  void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
701  SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
702  void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
703  SourceLocation getColonLoc() const { return ColonLoc; }
704  void setColonLoc(SourceLocation L) { ColonLoc = L; }
705
706  Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
707  Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
708  Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
709
710  const Expr *getLHS() const {
711    return reinterpret_cast<const Expr*>(SubExprs[LHS]);
712  }
713  const Expr *getRHS() const {
714    return reinterpret_cast<const Expr*>(SubExprs[RHS]);
715  }
716  const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
717
718  void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
719  void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
720  void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
721
722  SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
723  SourceLocation getLocEnd() const LLVM_READONLY {
724    // Handle deeply nested case statements with iteration instead of recursion.
725    const CaseStmt *CS = this;
726    while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
727      CS = CS2;
728
729    return CS->getSubStmt()->getLocEnd();
730  }
731
732  static bool classof(const Stmt *T) {
733    return T->getStmtClass() == CaseStmtClass;
734  }
735
736  // Iterators
737  child_range children() {
738    return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
739  }
740};
741
742class DefaultStmt : public SwitchCase {
743  Stmt* SubStmt;
744public:
745  DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
746    SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
747
748  /// \brief Build an empty default statement.
749  explicit DefaultStmt(EmptyShell Empty)
750    : SwitchCase(DefaultStmtClass, Empty) { }
751
752  Stmt *getSubStmt() { return SubStmt; }
753  const Stmt *getSubStmt() const { return SubStmt; }
754  void setSubStmt(Stmt *S) { SubStmt = S; }
755
756  SourceLocation getDefaultLoc() const { return KeywordLoc; }
757  void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
758  SourceLocation getColonLoc() const { return ColonLoc; }
759  void setColonLoc(SourceLocation L) { ColonLoc = L; }
760
761  SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
762  SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
763
764  static bool classof(const Stmt *T) {
765    return T->getStmtClass() == DefaultStmtClass;
766  }
767
768  // Iterators
769  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
770};
771
772inline SourceLocation SwitchCase::getLocEnd() const {
773  if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
774    return CS->getLocEnd();
775  return cast<DefaultStmt>(this)->getLocEnd();
776}
777
778/// LabelStmt - Represents a label, which has a substatement.  For example:
779///    foo: return;
780///
781class LabelStmt : public Stmt {
782  LabelDecl *TheDecl;
783  Stmt *SubStmt;
784  SourceLocation IdentLoc;
785public:
786  LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
787    : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
788  }
789
790  // \brief Build an empty label statement.
791  explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
792
793  SourceLocation getIdentLoc() const { return IdentLoc; }
794  LabelDecl *getDecl() const { return TheDecl; }
795  void setDecl(LabelDecl *D) { TheDecl = D; }
796  const char *getName() const;
797  Stmt *getSubStmt() { return SubStmt; }
798  const Stmt *getSubStmt() const { return SubStmt; }
799  void setIdentLoc(SourceLocation L) { IdentLoc = L; }
800  void setSubStmt(Stmt *SS) { SubStmt = SS; }
801
802  SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
803  SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
804
805  child_range children() { return child_range(&SubStmt, &SubStmt+1); }
806
807  static bool classof(const Stmt *T) {
808    return T->getStmtClass() == LabelStmtClass;
809  }
810};
811
812
813/// \brief Represents an attribute applied to a statement.
814///
815/// Represents an attribute applied to a statement. For example:
816///   [[omp::for(...)]] for (...) { ... }
817///
818class AttributedStmt : public Stmt {
819  Stmt *SubStmt;
820  SourceLocation AttrLoc;
821  unsigned NumAttrs;
822
823  friend class ASTStmtReader;
824
825  AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
826    : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
827      NumAttrs(Attrs.size()) {
828    memcpy(getAttrArrayPtr(), Attrs.data(), Attrs.size() * sizeof(Attr *));
829  }
830
831  explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
832    : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
833    memset(getAttrArrayPtr(), 0, NumAttrs * sizeof(Attr *));
834  }
835
836  Attr *const *getAttrArrayPtr() const {
837    return reinterpret_cast<Attr *const *>(this + 1);
838  }
839  Attr **getAttrArrayPtr() { return reinterpret_cast<Attr **>(this + 1); }
840
841public:
842  static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
843                                ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
844  // \brief Build an empty attributed statement.
845  static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
846
847  SourceLocation getAttrLoc() const { return AttrLoc; }
848  ArrayRef<const Attr*> getAttrs() const {
849    return ArrayRef<const Attr*>(getAttrArrayPtr(), NumAttrs);
850  }
851  Stmt *getSubStmt() { return SubStmt; }
852  const Stmt *getSubStmt() const { return SubStmt; }
853
854  SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
855  SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
856
857  child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
858
859  static bool classof(const Stmt *T) {
860    return T->getStmtClass() == AttributedStmtClass;
861  }
862};
863
864
865/// IfStmt - This represents an if/then/else.
866///
867class IfStmt : public Stmt {
868  enum { VAR, COND, THEN, ELSE, END_EXPR };
869  Stmt* SubExprs[END_EXPR];
870
871  SourceLocation IfLoc;
872  SourceLocation ElseLoc;
873
874public:
875  IfStmt(const ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
876         Stmt *then, SourceLocation EL = SourceLocation(),
877         Stmt *elsev = nullptr);
878
879  /// \brief Build an empty if/then/else statement
880  explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
881
882  /// \brief Retrieve the variable declared in this "if" statement, if any.
883  ///
884  /// In the following example, "x" is the condition variable.
885  /// \code
886  /// if (int x = foo()) {
887  ///   printf("x is %d", x);
888  /// }
889  /// \endcode
890  VarDecl *getConditionVariable() const;
891  void setConditionVariable(const ASTContext &C, VarDecl *V);
892
893  /// If this IfStmt has a condition variable, return the faux DeclStmt
894  /// associated with the creation of that condition variable.
895  const DeclStmt *getConditionVariableDeclStmt() const {
896    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
897  }
898
899  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
900  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
901  const Stmt *getThen() const { return SubExprs[THEN]; }
902  void setThen(Stmt *S) { SubExprs[THEN] = S; }
903  const Stmt *getElse() const { return SubExprs[ELSE]; }
904  void setElse(Stmt *S) { SubExprs[ELSE] = S; }
905
906  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
907  Stmt *getThen() { return SubExprs[THEN]; }
908  Stmt *getElse() { return SubExprs[ELSE]; }
909
910  SourceLocation getIfLoc() const { return IfLoc; }
911  void setIfLoc(SourceLocation L) { IfLoc = L; }
912  SourceLocation getElseLoc() const { return ElseLoc; }
913  void setElseLoc(SourceLocation L) { ElseLoc = L; }
914
915  SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
916  SourceLocation getLocEnd() const LLVM_READONLY {
917    if (SubExprs[ELSE])
918      return SubExprs[ELSE]->getLocEnd();
919    else
920      return SubExprs[THEN]->getLocEnd();
921  }
922
923  // Iterators over subexpressions.  The iterators will include iterating
924  // over the initialization expression referenced by the condition variable.
925  child_range children() {
926    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
927  }
928
929  static bool classof(const Stmt *T) {
930    return T->getStmtClass() == IfStmtClass;
931  }
932};
933
934/// SwitchStmt - This represents a 'switch' stmt.
935///
936class SwitchStmt : public Stmt {
937  enum { VAR, COND, BODY, END_EXPR };
938  Stmt* SubExprs[END_EXPR];
939  // This points to a linked list of case and default statements.
940  SwitchCase *FirstCase;
941  SourceLocation SwitchLoc;
942
943  /// If the SwitchStmt is a switch on an enum value, this records whether
944  /// all the enum values were covered by CaseStmts.  This value is meant to
945  /// be a hint for possible clients.
946  unsigned AllEnumCasesCovered : 1;
947
948public:
949  SwitchStmt(const ASTContext &C, VarDecl *Var, Expr *cond);
950
951  /// \brief Build a empty switch statement.
952  explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
953
954  /// \brief Retrieve the variable declared in this "switch" statement, if any.
955  ///
956  /// In the following example, "x" is the condition variable.
957  /// \code
958  /// switch (int x = foo()) {
959  ///   case 0: break;
960  ///   // ...
961  /// }
962  /// \endcode
963  VarDecl *getConditionVariable() const;
964  void setConditionVariable(const ASTContext &C, VarDecl *V);
965
966  /// If this SwitchStmt has a condition variable, return the faux DeclStmt
967  /// associated with the creation of that condition variable.
968  const DeclStmt *getConditionVariableDeclStmt() const {
969    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
970  }
971
972  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
973  const Stmt *getBody() const { return SubExprs[BODY]; }
974  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
975
976  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
977  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
978  Stmt *getBody() { return SubExprs[BODY]; }
979  void setBody(Stmt *S) { SubExprs[BODY] = S; }
980  SwitchCase *getSwitchCaseList() { return FirstCase; }
981
982  /// \brief Set the case list for this switch statement.
983  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
984
985  SourceLocation getSwitchLoc() const { return SwitchLoc; }
986  void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
987
988  void setBody(Stmt *S, SourceLocation SL) {
989    SubExprs[BODY] = S;
990    SwitchLoc = SL;
991  }
992  void addSwitchCase(SwitchCase *SC) {
993    assert(!SC->getNextSwitchCase()
994           && "case/default already added to a switch");
995    SC->setNextSwitchCase(FirstCase);
996    FirstCase = SC;
997  }
998
999  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1000  /// switch over an enum value then all cases have been explicitly covered.
1001  void setAllEnumCasesCovered() {
1002    AllEnumCasesCovered = 1;
1003  }
1004
1005  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1006  /// have been explicitly covered.
1007  bool isAllEnumCasesCovered() const {
1008    return (bool) AllEnumCasesCovered;
1009  }
1010
1011  SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1012  SourceLocation getLocEnd() const LLVM_READONLY {
1013    return SubExprs[BODY]->getLocEnd();
1014  }
1015
1016  // Iterators
1017  child_range children() {
1018    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1019  }
1020
1021  static bool classof(const Stmt *T) {
1022    return T->getStmtClass() == SwitchStmtClass;
1023  }
1024};
1025
1026
1027/// WhileStmt - This represents a 'while' stmt.
1028///
1029class WhileStmt : public Stmt {
1030  enum { VAR, COND, BODY, END_EXPR };
1031  Stmt* SubExprs[END_EXPR];
1032  SourceLocation WhileLoc;
1033public:
1034  WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1035            SourceLocation WL);
1036
1037  /// \brief Build an empty while statement.
1038  explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1039
1040  /// \brief Retrieve the variable declared in this "while" statement, if any.
1041  ///
1042  /// In the following example, "x" is the condition variable.
1043  /// \code
1044  /// while (int x = random()) {
1045  ///   // ...
1046  /// }
1047  /// \endcode
1048  VarDecl *getConditionVariable() const;
1049  void setConditionVariable(const ASTContext &C, VarDecl *V);
1050
1051  /// If this WhileStmt has a condition variable, return the faux DeclStmt
1052  /// associated with the creation of that condition variable.
1053  const DeclStmt *getConditionVariableDeclStmt() const {
1054    return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1055  }
1056
1057  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1058  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1059  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1060  Stmt *getBody() { return SubExprs[BODY]; }
1061  const Stmt *getBody() const { return SubExprs[BODY]; }
1062  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1063
1064  SourceLocation getWhileLoc() const { return WhileLoc; }
1065  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1066
1067  SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1068  SourceLocation getLocEnd() const LLVM_READONLY {
1069    return SubExprs[BODY]->getLocEnd();
1070  }
1071
1072  static bool classof(const Stmt *T) {
1073    return T->getStmtClass() == WhileStmtClass;
1074  }
1075
1076  // Iterators
1077  child_range children() {
1078    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1079  }
1080};
1081
1082/// DoStmt - This represents a 'do/while' stmt.
1083///
1084class DoStmt : public Stmt {
1085  enum { BODY, COND, END_EXPR };
1086  Stmt* SubExprs[END_EXPR];
1087  SourceLocation DoLoc;
1088  SourceLocation WhileLoc;
1089  SourceLocation RParenLoc;  // Location of final ')' in do stmt condition.
1090
1091public:
1092  DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1093         SourceLocation RP)
1094    : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1095    SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1096    SubExprs[BODY] = body;
1097  }
1098
1099  /// \brief Build an empty do-while statement.
1100  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1101
1102  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1103  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1104  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1105  Stmt *getBody() { return SubExprs[BODY]; }
1106  const Stmt *getBody() const { return SubExprs[BODY]; }
1107  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1108
1109  SourceLocation getDoLoc() const { return DoLoc; }
1110  void setDoLoc(SourceLocation L) { DoLoc = L; }
1111  SourceLocation getWhileLoc() const { return WhileLoc; }
1112  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1113
1114  SourceLocation getRParenLoc() const { return RParenLoc; }
1115  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1116
1117  SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1118  SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1119
1120  static bool classof(const Stmt *T) {
1121    return T->getStmtClass() == DoStmtClass;
1122  }
1123
1124  // Iterators
1125  child_range children() {
1126    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1127  }
1128};
1129
1130
1131/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
1132/// the init/cond/inc parts of the ForStmt will be null if they were not
1133/// specified in the source.
1134///
1135class ForStmt : public Stmt {
1136  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1137  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1138  SourceLocation ForLoc;
1139  SourceLocation LParenLoc, RParenLoc;
1140
1141public:
1142  ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1143          Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1144          SourceLocation RP);
1145
1146  /// \brief Build an empty for statement.
1147  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1148
1149  Stmt *getInit() { return SubExprs[INIT]; }
1150
1151  /// \brief Retrieve the variable declared in this "for" statement, if any.
1152  ///
1153  /// In the following example, "y" is the condition variable.
1154  /// \code
1155  /// for (int x = random(); int y = mangle(x); ++x) {
1156  ///   // ...
1157  /// }
1158  /// \endcode
1159  VarDecl *getConditionVariable() const;
1160  void setConditionVariable(const ASTContext &C, VarDecl *V);
1161
1162  /// If this ForStmt has a condition variable, return the faux DeclStmt
1163  /// associated with the creation of that condition variable.
1164  const DeclStmt *getConditionVariableDeclStmt() const {
1165    return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1166  }
1167
1168  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1169  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1170  Stmt *getBody() { return SubExprs[BODY]; }
1171
1172  const Stmt *getInit() const { return SubExprs[INIT]; }
1173  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1174  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1175  const Stmt *getBody() const { return SubExprs[BODY]; }
1176
1177  void setInit(Stmt *S) { SubExprs[INIT] = S; }
1178  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1179  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1180  void setBody(Stmt *S) { SubExprs[BODY] = S; }
1181
1182  SourceLocation getForLoc() const { return ForLoc; }
1183  void setForLoc(SourceLocation L) { ForLoc = L; }
1184  SourceLocation getLParenLoc() const { return LParenLoc; }
1185  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1186  SourceLocation getRParenLoc() const { return RParenLoc; }
1187  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1188
1189  SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1190  SourceLocation getLocEnd() const LLVM_READONLY {
1191    return SubExprs[BODY]->getLocEnd();
1192  }
1193
1194  static bool classof(const Stmt *T) {
1195    return T->getStmtClass() == ForStmtClass;
1196  }
1197
1198  // Iterators
1199  child_range children() {
1200    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1201  }
1202};
1203
1204/// GotoStmt - This represents a direct goto.
1205///
1206class GotoStmt : public Stmt {
1207  LabelDecl *Label;
1208  SourceLocation GotoLoc;
1209  SourceLocation LabelLoc;
1210public:
1211  GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1212    : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1213
1214  /// \brief Build an empty goto statement.
1215  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1216
1217  LabelDecl *getLabel() const { return Label; }
1218  void setLabel(LabelDecl *D) { Label = D; }
1219
1220  SourceLocation getGotoLoc() const { return GotoLoc; }
1221  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1222  SourceLocation getLabelLoc() const { return LabelLoc; }
1223  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1224
1225  SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1226  SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1227
1228  static bool classof(const Stmt *T) {
1229    return T->getStmtClass() == GotoStmtClass;
1230  }
1231
1232  // Iterators
1233  child_range children() { return child_range(); }
1234};
1235
1236/// IndirectGotoStmt - This represents an indirect goto.
1237///
1238class IndirectGotoStmt : public Stmt {
1239  SourceLocation GotoLoc;
1240  SourceLocation StarLoc;
1241  Stmt *Target;
1242public:
1243  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1244                   Expr *target)
1245    : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1246      Target((Stmt*)target) {}
1247
1248  /// \brief Build an empty indirect goto statement.
1249  explicit IndirectGotoStmt(EmptyShell Empty)
1250    : Stmt(IndirectGotoStmtClass, Empty) { }
1251
1252  void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1253  SourceLocation getGotoLoc() const { return GotoLoc; }
1254  void setStarLoc(SourceLocation L) { StarLoc = L; }
1255  SourceLocation getStarLoc() const { return StarLoc; }
1256
1257  Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1258  const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1259  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1260
1261  /// getConstantTarget - Returns the fixed target of this indirect
1262  /// goto, if one exists.
1263  LabelDecl *getConstantTarget();
1264  const LabelDecl *getConstantTarget() const {
1265    return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1266  }
1267
1268  SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1269  SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1270
1271  static bool classof(const Stmt *T) {
1272    return T->getStmtClass() == IndirectGotoStmtClass;
1273  }
1274
1275  // Iterators
1276  child_range children() { return child_range(&Target, &Target+1); }
1277};
1278
1279
1280/// ContinueStmt - This represents a continue.
1281///
1282class ContinueStmt : public Stmt {
1283  SourceLocation ContinueLoc;
1284public:
1285  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1286
1287  /// \brief Build an empty continue statement.
1288  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1289
1290  SourceLocation getContinueLoc() const { return ContinueLoc; }
1291  void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1292
1293  SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1294  SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1295
1296  static bool classof(const Stmt *T) {
1297    return T->getStmtClass() == ContinueStmtClass;
1298  }
1299
1300  // Iterators
1301  child_range children() { return child_range(); }
1302};
1303
1304/// BreakStmt - This represents a break.
1305///
1306class BreakStmt : public Stmt {
1307  SourceLocation BreakLoc;
1308public:
1309  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1310
1311  /// \brief Build an empty break statement.
1312  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1313
1314  SourceLocation getBreakLoc() const { return BreakLoc; }
1315  void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1316
1317  SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1318  SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1319
1320  static bool classof(const Stmt *T) {
1321    return T->getStmtClass() == BreakStmtClass;
1322  }
1323
1324  // Iterators
1325  child_range children() { return child_range(); }
1326};
1327
1328
1329/// ReturnStmt - This represents a return, optionally of an expression:
1330///   return;
1331///   return 4;
1332///
1333/// Note that GCC allows return with no argument in a function declared to
1334/// return a value, and it allows returning a value in functions declared to
1335/// return void.  We explicitly model this in the AST, which means you can't
1336/// depend on the return type of the function and the presence of an argument.
1337///
1338class ReturnStmt : public Stmt {
1339  Stmt *RetExpr;
1340  SourceLocation RetLoc;
1341  const VarDecl *NRVOCandidate;
1342
1343public:
1344  ReturnStmt(SourceLocation RL)
1345    : Stmt(ReturnStmtClass), RetExpr(nullptr), RetLoc(RL),
1346      NRVOCandidate(nullptr) {}
1347
1348  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1349    : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1350      NRVOCandidate(NRVOCandidate) {}
1351
1352  /// \brief Build an empty return expression.
1353  explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1354
1355  const Expr *getRetValue() const;
1356  Expr *getRetValue();
1357  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1358
1359  SourceLocation getReturnLoc() const { return RetLoc; }
1360  void setReturnLoc(SourceLocation L) { RetLoc = L; }
1361
1362  /// \brief Retrieve the variable that might be used for the named return
1363  /// value optimization.
1364  ///
1365  /// The optimization itself can only be performed if the variable is
1366  /// also marked as an NRVO object.
1367  const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1368  void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1369
1370  SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1371  SourceLocation getLocEnd() const LLVM_READONLY {
1372    return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1373  }
1374
1375  static bool classof(const Stmt *T) {
1376    return T->getStmtClass() == ReturnStmtClass;
1377  }
1378
1379  // Iterators
1380  child_range children() {
1381    if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1382    return child_range();
1383  }
1384};
1385
1386/// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1387///
1388class AsmStmt : public Stmt {
1389protected:
1390  SourceLocation AsmLoc;
1391  /// \brief True if the assembly statement does not have any input or output
1392  /// operands.
1393  bool IsSimple;
1394
1395  /// \brief If true, treat this inline assembly as having side effects.
1396  /// This assembly statement should not be optimized, deleted or moved.
1397  bool IsVolatile;
1398
1399  unsigned NumOutputs;
1400  unsigned NumInputs;
1401  unsigned NumClobbers;
1402
1403  Stmt **Exprs;
1404
1405  AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1406          unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1407    Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1408    NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1409
1410  friend class ASTStmtReader;
1411
1412public:
1413  /// \brief Build an empty inline-assembly statement.
1414  explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1415    Stmt(SC, Empty), Exprs(nullptr) { }
1416
1417  SourceLocation getAsmLoc() const { return AsmLoc; }
1418  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1419
1420  bool isSimple() const { return IsSimple; }
1421  void setSimple(bool V) { IsSimple = V; }
1422
1423  bool isVolatile() const { return IsVolatile; }
1424  void setVolatile(bool V) { IsVolatile = V; }
1425
1426  SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1427  SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1428
1429  //===--- Asm String Analysis ---===//
1430
1431  /// Assemble final IR asm string.
1432  std::string generateAsmString(const ASTContext &C) const;
1433
1434  //===--- Output operands ---===//
1435
1436  unsigned getNumOutputs() const { return NumOutputs; }
1437
1438  /// getOutputConstraint - Return the constraint string for the specified
1439  /// output operand.  All output constraints are known to be non-empty (either
1440  /// '=' or '+').
1441  StringRef getOutputConstraint(unsigned i) const;
1442
1443  /// isOutputPlusConstraint - Return true if the specified output constraint
1444  /// is a "+" constraint (which is both an input and an output) or false if it
1445  /// is an "=" constraint (just an output).
1446  bool isOutputPlusConstraint(unsigned i) const {
1447    return getOutputConstraint(i)[0] == '+';
1448  }
1449
1450  const Expr *getOutputExpr(unsigned i) const;
1451
1452  /// getNumPlusOperands - Return the number of output operands that have a "+"
1453  /// constraint.
1454  unsigned getNumPlusOperands() const;
1455
1456  //===--- Input operands ---===//
1457
1458  unsigned getNumInputs() const { return NumInputs; }
1459
1460  /// getInputConstraint - Return the specified input constraint.  Unlike output
1461  /// constraints, these can be empty.
1462  StringRef getInputConstraint(unsigned i) const;
1463
1464  const Expr *getInputExpr(unsigned i) const;
1465
1466  //===--- Other ---===//
1467
1468  unsigned getNumClobbers() const { return NumClobbers; }
1469  StringRef getClobber(unsigned i) const;
1470
1471  static bool classof(const Stmt *T) {
1472    return T->getStmtClass() == GCCAsmStmtClass ||
1473      T->getStmtClass() == MSAsmStmtClass;
1474  }
1475
1476  // Input expr iterators.
1477
1478  typedef ExprIterator inputs_iterator;
1479  typedef ConstExprIterator const_inputs_iterator;
1480  typedef llvm::iterator_range<inputs_iterator> inputs_range;
1481  typedef llvm::iterator_range<const_inputs_iterator> inputs_const_range;
1482
1483  inputs_iterator begin_inputs() {
1484    return &Exprs[0] + NumOutputs;
1485  }
1486
1487  inputs_iterator end_inputs() {
1488    return &Exprs[0] + NumOutputs + NumInputs;
1489  }
1490
1491  inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1492
1493  const_inputs_iterator begin_inputs() const {
1494    return &Exprs[0] + NumOutputs;
1495  }
1496
1497  const_inputs_iterator end_inputs() const {
1498    return &Exprs[0] + NumOutputs + NumInputs;
1499  }
1500
1501  inputs_const_range inputs() const {
1502    return inputs_const_range(begin_inputs(), end_inputs());
1503  }
1504
1505  // Output expr iterators.
1506
1507  typedef ExprIterator outputs_iterator;
1508  typedef ConstExprIterator const_outputs_iterator;
1509  typedef llvm::iterator_range<outputs_iterator> outputs_range;
1510  typedef llvm::iterator_range<const_outputs_iterator> outputs_const_range;
1511
1512  outputs_iterator begin_outputs() {
1513    return &Exprs[0];
1514  }
1515  outputs_iterator end_outputs() {
1516    return &Exprs[0] + NumOutputs;
1517  }
1518  outputs_range outputs() {
1519    return outputs_range(begin_outputs(), end_outputs());
1520  }
1521
1522  const_outputs_iterator begin_outputs() const {
1523    return &Exprs[0];
1524  }
1525  const_outputs_iterator end_outputs() const {
1526    return &Exprs[0] + NumOutputs;
1527  }
1528  outputs_const_range outputs() const {
1529    return outputs_const_range(begin_outputs(), end_outputs());
1530  }
1531
1532  child_range children() {
1533    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1534  }
1535};
1536
1537/// This represents a GCC inline-assembly statement extension.
1538///
1539class GCCAsmStmt : public AsmStmt {
1540  SourceLocation RParenLoc;
1541  StringLiteral *AsmStr;
1542
1543  // FIXME: If we wanted to, we could allocate all of these in one big array.
1544  StringLiteral **Constraints;
1545  StringLiteral **Clobbers;
1546  IdentifierInfo **Names;
1547
1548  friend class ASTStmtReader;
1549
1550public:
1551  GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1552             bool isvolatile, unsigned numoutputs, unsigned numinputs,
1553             IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1554             StringLiteral *asmstr, unsigned numclobbers,
1555             StringLiteral **clobbers, SourceLocation rparenloc);
1556
1557  /// \brief Build an empty inline-assembly statement.
1558  explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1559    Constraints(nullptr), Clobbers(nullptr), Names(nullptr) { }
1560
1561  SourceLocation getRParenLoc() const { return RParenLoc; }
1562  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1563
1564  //===--- Asm String Analysis ---===//
1565
1566  const StringLiteral *getAsmString() const { return AsmStr; }
1567  StringLiteral *getAsmString() { return AsmStr; }
1568  void setAsmString(StringLiteral *E) { AsmStr = E; }
1569
1570  /// AsmStringPiece - this is part of a decomposed asm string specification
1571  /// (for use with the AnalyzeAsmString function below).  An asm string is
1572  /// considered to be a concatenation of these parts.
1573  class AsmStringPiece {
1574  public:
1575    enum Kind {
1576      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1577      Operand  // Operand reference, with optional modifier %c4.
1578    };
1579  private:
1580    Kind MyKind;
1581    std::string Str;
1582    unsigned OperandNo;
1583  public:
1584    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1585    AsmStringPiece(unsigned OpNo, char Modifier)
1586      : MyKind(Operand), Str(), OperandNo(OpNo) {
1587      Str += Modifier;
1588    }
1589
1590    bool isString() const { return MyKind == String; }
1591    bool isOperand() const { return MyKind == Operand; }
1592
1593    const std::string &getString() const {
1594      assert(isString());
1595      return Str;
1596    }
1597
1598    unsigned getOperandNo() const {
1599      assert(isOperand());
1600      return OperandNo;
1601    }
1602
1603    /// getModifier - Get the modifier for this operand, if present.  This
1604    /// returns '\0' if there was no modifier.
1605    char getModifier() const {
1606      assert(isOperand());
1607      return Str[0];
1608    }
1609  };
1610
1611  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1612  /// it into pieces.  If the asm string is erroneous, emit errors and return
1613  /// true, otherwise return false.  This handles canonicalization and
1614  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1615  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1616  unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1617                            const ASTContext &C, unsigned &DiagOffs) const;
1618
1619  /// Assemble final IR asm string.
1620  std::string generateAsmString(const ASTContext &C) const;
1621
1622  //===--- Output operands ---===//
1623
1624  IdentifierInfo *getOutputIdentifier(unsigned i) const {
1625    return Names[i];
1626  }
1627
1628  StringRef getOutputName(unsigned i) const {
1629    if (IdentifierInfo *II = getOutputIdentifier(i))
1630      return II->getName();
1631
1632    return StringRef();
1633  }
1634
1635  StringRef getOutputConstraint(unsigned i) const;
1636
1637  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1638    return Constraints[i];
1639  }
1640  StringLiteral *getOutputConstraintLiteral(unsigned i) {
1641    return Constraints[i];
1642  }
1643
1644  Expr *getOutputExpr(unsigned i);
1645
1646  const Expr *getOutputExpr(unsigned i) const {
1647    return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1648  }
1649
1650  //===--- Input operands ---===//
1651
1652  IdentifierInfo *getInputIdentifier(unsigned i) const {
1653    return Names[i + NumOutputs];
1654  }
1655
1656  StringRef getInputName(unsigned i) const {
1657    if (IdentifierInfo *II = getInputIdentifier(i))
1658      return II->getName();
1659
1660    return StringRef();
1661  }
1662
1663  StringRef getInputConstraint(unsigned i) const;
1664
1665  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1666    return Constraints[i + NumOutputs];
1667  }
1668  StringLiteral *getInputConstraintLiteral(unsigned i) {
1669    return Constraints[i + NumOutputs];
1670  }
1671
1672  Expr *getInputExpr(unsigned i);
1673  void setInputExpr(unsigned i, Expr *E);
1674
1675  const Expr *getInputExpr(unsigned i) const {
1676    return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1677  }
1678
1679private:
1680  void setOutputsAndInputsAndClobbers(const ASTContext &C,
1681                                      IdentifierInfo **Names,
1682                                      StringLiteral **Constraints,
1683                                      Stmt **Exprs,
1684                                      unsigned NumOutputs,
1685                                      unsigned NumInputs,
1686                                      StringLiteral **Clobbers,
1687                                      unsigned NumClobbers);
1688public:
1689
1690  //===--- Other ---===//
1691
1692  /// getNamedOperand - Given a symbolic operand reference like %[foo],
1693  /// translate this into a numeric value needed to reference the same operand.
1694  /// This returns -1 if the operand name is invalid.
1695  int getNamedOperand(StringRef SymbolicName) const;
1696
1697  StringRef getClobber(unsigned i) const;
1698  StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1699  const StringLiteral *getClobberStringLiteral(unsigned i) const {
1700    return Clobbers[i];
1701  }
1702
1703  SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1704  SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1705
1706  static bool classof(const Stmt *T) {
1707    return T->getStmtClass() == GCCAsmStmtClass;
1708  }
1709};
1710
1711/// This represents a Microsoft inline-assembly statement extension.
1712///
1713class MSAsmStmt : public AsmStmt {
1714  SourceLocation LBraceLoc, EndLoc;
1715  StringRef AsmStr;
1716
1717  unsigned NumAsmToks;
1718
1719  Token *AsmToks;
1720  StringRef *Constraints;
1721  StringRef *Clobbers;
1722
1723  friend class ASTStmtReader;
1724
1725public:
1726  MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1727            SourceLocation lbraceloc, bool issimple, bool isvolatile,
1728            ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1729            ArrayRef<StringRef> constraints,
1730            ArrayRef<Expr*> exprs, StringRef asmstr,
1731            ArrayRef<StringRef> clobbers, SourceLocation endloc);
1732
1733  /// \brief Build an empty MS-style inline-assembly statement.
1734  explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1735    NumAsmToks(0), AsmToks(nullptr), Constraints(nullptr), Clobbers(nullptr) { }
1736
1737  SourceLocation getLBraceLoc() const { return LBraceLoc; }
1738  void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1739  SourceLocation getEndLoc() const { return EndLoc; }
1740  void setEndLoc(SourceLocation L) { EndLoc = L; }
1741
1742  bool hasBraces() const { return LBraceLoc.isValid(); }
1743
1744  unsigned getNumAsmToks() { return NumAsmToks; }
1745  Token *getAsmToks() { return AsmToks; }
1746
1747  //===--- Asm String Analysis ---===//
1748  StringRef getAsmString() const { return AsmStr; }
1749
1750  /// Assemble final IR asm string.
1751  std::string generateAsmString(const ASTContext &C) const;
1752
1753  //===--- Output operands ---===//
1754
1755  StringRef getOutputConstraint(unsigned i) const {
1756    assert(i < NumOutputs);
1757    return Constraints[i];
1758  }
1759
1760  Expr *getOutputExpr(unsigned i);
1761
1762  const Expr *getOutputExpr(unsigned i) const {
1763    return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1764  }
1765
1766  //===--- Input operands ---===//
1767
1768  StringRef getInputConstraint(unsigned i) const {
1769    assert(i < NumInputs);
1770    return Constraints[i + NumOutputs];
1771  }
1772
1773  Expr *getInputExpr(unsigned i);
1774  void setInputExpr(unsigned i, Expr *E);
1775
1776  const Expr *getInputExpr(unsigned i) const {
1777    return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1778  }
1779
1780  //===--- Other ---===//
1781
1782  ArrayRef<StringRef> getAllConstraints() const {
1783    return ArrayRef<StringRef>(Constraints, NumInputs + NumOutputs);
1784  }
1785  ArrayRef<StringRef> getClobbers() const {
1786    return ArrayRef<StringRef>(Clobbers, NumClobbers);
1787  }
1788  ArrayRef<Expr*> getAllExprs() const {
1789    return ArrayRef<Expr*>(reinterpret_cast<Expr**>(Exprs),
1790                           NumInputs + NumOutputs);
1791  }
1792
1793  StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1794
1795private:
1796  void initialize(const ASTContext &C, StringRef AsmString,
1797                  ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1798                  ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1799public:
1800
1801  SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1802  SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1803
1804  static bool classof(const Stmt *T) {
1805    return T->getStmtClass() == MSAsmStmtClass;
1806  }
1807
1808  child_range children() {
1809    return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1810  }
1811};
1812
1813class SEHExceptStmt : public Stmt {
1814  SourceLocation  Loc;
1815  Stmt           *Children[2];
1816
1817  enum { FILTER_EXPR, BLOCK };
1818
1819  SEHExceptStmt(SourceLocation Loc,
1820                Expr *FilterExpr,
1821                Stmt *Block);
1822
1823  friend class ASTReader;
1824  friend class ASTStmtReader;
1825  explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1826
1827public:
1828  static SEHExceptStmt* Create(const ASTContext &C,
1829                               SourceLocation ExceptLoc,
1830                               Expr *FilterExpr,
1831                               Stmt *Block);
1832
1833  SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1834  SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1835
1836  SourceLocation getExceptLoc() const { return Loc; }
1837  SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1838
1839  Expr *getFilterExpr() const {
1840    return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1841  }
1842
1843  CompoundStmt *getBlock() const {
1844    return cast<CompoundStmt>(Children[BLOCK]);
1845  }
1846
1847  child_range children() {
1848    return child_range(Children,Children+2);
1849  }
1850
1851  static bool classof(const Stmt *T) {
1852    return T->getStmtClass() == SEHExceptStmtClass;
1853  }
1854
1855};
1856
1857class SEHFinallyStmt : public Stmt {
1858  SourceLocation  Loc;
1859  Stmt           *Block;
1860
1861  SEHFinallyStmt(SourceLocation Loc,
1862                 Stmt *Block);
1863
1864  friend class ASTReader;
1865  friend class ASTStmtReader;
1866  explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1867
1868public:
1869  static SEHFinallyStmt* Create(const ASTContext &C,
1870                                SourceLocation FinallyLoc,
1871                                Stmt *Block);
1872
1873  SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1874  SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1875
1876  SourceLocation getFinallyLoc() const { return Loc; }
1877  SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1878
1879  CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1880
1881  child_range children() {
1882    return child_range(&Block,&Block+1);
1883  }
1884
1885  static bool classof(const Stmt *T) {
1886    return T->getStmtClass() == SEHFinallyStmtClass;
1887  }
1888
1889};
1890
1891class SEHTryStmt : public Stmt {
1892  bool            IsCXXTry;
1893  SourceLocation  TryLoc;
1894  Stmt           *Children[2];
1895
1896  enum { TRY = 0, HANDLER = 1 };
1897
1898  SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1899             SourceLocation TryLoc,
1900             Stmt *TryBlock,
1901             Stmt *Handler);
1902
1903  friend class ASTReader;
1904  friend class ASTStmtReader;
1905  explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1906
1907public:
1908  static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
1909                            SourceLocation TryLoc, Stmt *TryBlock,
1910                            Stmt *Handler);
1911
1912  SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1913  SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1914
1915  SourceLocation getTryLoc() const { return TryLoc; }
1916  SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1917
1918  bool getIsCXXTry() const { return IsCXXTry; }
1919
1920  CompoundStmt* getTryBlock() const {
1921    return cast<CompoundStmt>(Children[TRY]);
1922  }
1923
1924  Stmt *getHandler() const { return Children[HANDLER]; }
1925
1926  /// Returns 0 if not defined
1927  SEHExceptStmt  *getExceptHandler() const;
1928  SEHFinallyStmt *getFinallyHandler() const;
1929
1930  child_range children() {
1931    return child_range(Children,Children+2);
1932  }
1933
1934  static bool classof(const Stmt *T) {
1935    return T->getStmtClass() == SEHTryStmtClass;
1936  }
1937};
1938
1939/// Represents a __leave statement.
1940///
1941class SEHLeaveStmt : public Stmt {
1942  SourceLocation LeaveLoc;
1943public:
1944  explicit SEHLeaveStmt(SourceLocation LL)
1945      : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
1946
1947  /// \brief Build an empty __leave statement.
1948  explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) { }
1949
1950  SourceLocation getLeaveLoc() const { return LeaveLoc; }
1951  void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
1952
1953  SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
1954  SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
1955
1956  static bool classof(const Stmt *T) {
1957    return T->getStmtClass() == SEHLeaveStmtClass;
1958  }
1959
1960  // Iterators
1961  child_range children() { return child_range(); }
1962};
1963
1964/// \brief This captures a statement into a function. For example, the following
1965/// pragma annotated compound statement can be represented as a CapturedStmt,
1966/// and this compound statement is the body of an anonymous outlined function.
1967/// @code
1968/// #pragma omp parallel
1969/// {
1970///   compute();
1971/// }
1972/// @endcode
1973class CapturedStmt : public Stmt {
1974public:
1975  /// \brief The different capture forms: by 'this' or by reference, etc.
1976  enum VariableCaptureKind {
1977    VCK_This,
1978    VCK_ByRef
1979  };
1980
1981  /// \brief Describes the capture of either a variable or 'this'.
1982  class Capture {
1983    llvm::PointerIntPair<VarDecl *, 1, VariableCaptureKind> VarAndKind;
1984    SourceLocation Loc;
1985
1986  public:
1987    /// \brief Create a new capture.
1988    ///
1989    /// \param Loc The source location associated with this capture.
1990    ///
1991    /// \param Kind The kind of capture (this, ByRef, ...).
1992    ///
1993    /// \param Var The variable being captured, or null if capturing this.
1994    ///
1995    Capture(SourceLocation Loc, VariableCaptureKind Kind,
1996            VarDecl *Var = nullptr)
1997      : VarAndKind(Var, Kind), Loc(Loc) {
1998      switch (Kind) {
1999      case VCK_This:
2000        assert(!Var && "'this' capture cannot have a variable!");
2001        break;
2002      case VCK_ByRef:
2003        assert(Var && "capturing by reference must have a variable!");
2004        break;
2005      }
2006    }
2007
2008    /// \brief Determine the kind of capture.
2009    VariableCaptureKind getCaptureKind() const { return VarAndKind.getInt(); }
2010
2011    /// \brief Retrieve the source location at which the variable or 'this' was
2012    /// first used.
2013    SourceLocation getLocation() const { return Loc; }
2014
2015    /// \brief Determine whether this capture handles the C++ 'this' pointer.
2016    bool capturesThis() const { return getCaptureKind() == VCK_This; }
2017
2018    /// \brief Determine whether this capture handles a variable.
2019    bool capturesVariable() const { return getCaptureKind() != VCK_This; }
2020
2021    /// \brief Retrieve the declaration of the variable being captured.
2022    ///
2023    /// This operation is only valid if this capture does not capture 'this'.
2024    VarDecl *getCapturedVar() const {
2025      assert(!capturesThis() && "No variable available for 'this' capture");
2026      return VarAndKind.getPointer();
2027    }
2028    friend class ASTStmtReader;
2029  };
2030
2031private:
2032  /// \brief The number of variable captured, including 'this'.
2033  unsigned NumCaptures;
2034
2035  /// \brief The pointer part is the implicit the outlined function and the
2036  /// int part is the captured region kind, 'CR_Default' etc.
2037  llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind;
2038
2039  /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
2040  RecordDecl *TheRecordDecl;
2041
2042  /// \brief Construct a captured statement.
2043  CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2044               ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2045
2046  /// \brief Construct an empty captured statement.
2047  CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2048
2049  Stmt **getStoredStmts() const {
2050    return reinterpret_cast<Stmt **>(const_cast<CapturedStmt *>(this) + 1);
2051  }
2052
2053  Capture *getStoredCaptures() const;
2054
2055  void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2056
2057public:
2058  static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2059                              CapturedRegionKind Kind,
2060                              ArrayRef<Capture> Captures,
2061                              ArrayRef<Expr *> CaptureInits,
2062                              CapturedDecl *CD, RecordDecl *RD);
2063
2064  static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2065                                          unsigned NumCaptures);
2066
2067  /// \brief Retrieve the statement being captured.
2068  Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2069  const Stmt *getCapturedStmt() const {
2070    return const_cast<CapturedStmt *>(this)->getCapturedStmt();
2071  }
2072
2073  /// \brief Retrieve the outlined function declaration.
2074  CapturedDecl *getCapturedDecl() { return CapDeclAndKind.getPointer(); }
2075  const CapturedDecl *getCapturedDecl() const {
2076    return const_cast<CapturedStmt *>(this)->getCapturedDecl();
2077  }
2078
2079  /// \brief Set the outlined function declaration.
2080  void setCapturedDecl(CapturedDecl *D) {
2081    assert(D && "null CapturedDecl");
2082    CapDeclAndKind.setPointer(D);
2083  }
2084
2085  /// \brief Retrieve the captured region kind.
2086  CapturedRegionKind getCapturedRegionKind() const {
2087    return CapDeclAndKind.getInt();
2088  }
2089
2090  /// \brief Set the captured region kind.
2091  void setCapturedRegionKind(CapturedRegionKind Kind) {
2092    CapDeclAndKind.setInt(Kind);
2093  }
2094
2095  /// \brief Retrieve the record declaration for captured variables.
2096  const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2097
2098  /// \brief Set the record declaration for captured variables.
2099  void setCapturedRecordDecl(RecordDecl *D) {
2100    assert(D && "null RecordDecl");
2101    TheRecordDecl = D;
2102  }
2103
2104  /// \brief True if this variable has been captured.
2105  bool capturesVariable(const VarDecl *Var) const;
2106
2107  /// \brief An iterator that walks over the captures.
2108  typedef Capture *capture_iterator;
2109  typedef const Capture *const_capture_iterator;
2110  typedef llvm::iterator_range<capture_iterator> capture_range;
2111  typedef llvm::iterator_range<const_capture_iterator> capture_const_range;
2112
2113  capture_range captures() {
2114    return capture_range(capture_begin(), capture_end());
2115  }
2116  capture_const_range captures() const {
2117    return capture_const_range(capture_begin(), capture_end());
2118  }
2119
2120  /// \brief Retrieve an iterator pointing to the first capture.
2121  capture_iterator capture_begin() { return getStoredCaptures(); }
2122  const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2123
2124  /// \brief Retrieve an iterator pointing past the end of the sequence of
2125  /// captures.
2126  capture_iterator capture_end() const {
2127    return getStoredCaptures() + NumCaptures;
2128  }
2129
2130  /// \brief Retrieve the number of captures, including 'this'.
2131  unsigned capture_size() const { return NumCaptures; }
2132
2133  /// \brief Iterator that walks over the capture initialization arguments.
2134  typedef Expr **capture_init_iterator;
2135  typedef llvm::iterator_range<capture_init_iterator> capture_init_range;
2136
2137  capture_init_range capture_inits() const {
2138    return capture_init_range(capture_init_begin(), capture_init_end());
2139  }
2140
2141  /// \brief Retrieve the first initialization argument.
2142  capture_init_iterator capture_init_begin() const {
2143    return reinterpret_cast<Expr **>(getStoredStmts());
2144  }
2145
2146  /// \brief Retrieve the iterator pointing one past the last initialization
2147  /// argument.
2148  capture_init_iterator capture_init_end() const {
2149    return capture_init_begin() + NumCaptures;
2150  }
2151
2152  SourceLocation getLocStart() const LLVM_READONLY {
2153    return getCapturedStmt()->getLocStart();
2154  }
2155  SourceLocation getLocEnd() const LLVM_READONLY {
2156    return getCapturedStmt()->getLocEnd();
2157  }
2158  SourceRange getSourceRange() const LLVM_READONLY {
2159    return getCapturedStmt()->getSourceRange();
2160  }
2161
2162  static bool classof(const Stmt *T) {
2163    return T->getStmtClass() == CapturedStmtClass;
2164  }
2165
2166  child_range children();
2167
2168  friend class ASTStmtReader;
2169};
2170
2171}  // end namespace clang
2172
2173#endif
2174