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