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