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