1 //===--- Stmt.h - Classes for representing statements -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Stmt interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMT_H
15 #define LLVM_CLANG_AST_STMT_H
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/Attr.h"
23 #include "clang/Lex/Token.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/raw_ostream.h"
30 class FoldingSetNodeID;
46 //===--------------------------------------------------------------------===//
47 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
48 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
49 // references to children (to be compatible with StmtIterator).
50 //===--------------------------------------------------------------------===//
58 ExprIterator(Stmt** i) : I(i) {}
59 ExprIterator() : I(0) {}
60 ExprIterator& operator++() { ++I; return *this; }
61 ExprIterator operator-(size_t i) { return I-i; }
62 ExprIterator operator+(size_t i) { return I+i; }
63 Expr* operator[](size_t idx);
64 // FIXME: Verify that this will correctly return a signed distance.
65 signed operator-(const ExprIterator& R) const { return I - R.I; }
66 Expr* operator*() const;
67 Expr* operator->() const;
68 bool operator==(const ExprIterator& R) const { return I == R.I; }
69 bool operator!=(const ExprIterator& R) const { return I != R.I; }
70 bool operator>(const ExprIterator& R) const { return I > R.I; }
71 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
74 class ConstExprIterator {
75 const Stmt * const *I;
77 ConstExprIterator(const Stmt * const *i) : I(i) {}
78 ConstExprIterator() : I(0) {}
79 ConstExprIterator& operator++() { ++I; return *this; }
80 ConstExprIterator operator+(size_t i) const { return I+i; }
81 ConstExprIterator operator-(size_t i) const { return I-i; }
82 const Expr * operator[](size_t idx) const;
83 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
84 const Expr * operator*() const;
85 const Expr * operator->() const;
86 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
87 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
88 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
89 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
92 //===----------------------------------------------------------------------===//
93 // AST classes for statements.
94 //===----------------------------------------------------------------------===//
96 /// Stmt - This represents one statement.
102 #define STMT(CLASS, PARENT) CLASS##Class,
103 #define STMT_RANGE(BASE, FIRST, LAST) \
104 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
105 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
106 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
107 #define ABSTRACT_STMT(STMT)
108 #include "clang/AST/StmtNodes.inc"
111 // Make vanilla 'new' and 'delete' illegal for Stmts.
113 void* operator new(size_t bytes) throw() {
114 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
116 void operator delete(void* data) throw() {
117 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
120 class StmtBitfields {
123 /// \brief The statement class.
126 enum { NumStmtBits = 8 };
128 class CompoundStmtBitfields {
129 friend class CompoundStmt;
130 unsigned : NumStmtBits;
132 unsigned NumStmts : 32 - NumStmtBits;
135 class ExprBitfields {
137 friend class DeclRefExpr; // computeDependence
138 friend class InitListExpr; // ctor
139 friend class DesignatedInitExpr; // ctor
140 friend class BlockDeclRefExpr; // ctor
141 friend class ASTStmtReader; // deserialization
142 friend class CXXNewExpr; // ctor
143 friend class DependentScopeDeclRefExpr; // ctor
144 friend class CXXConstructExpr; // ctor
145 friend class CallExpr; // ctor
146 friend class OffsetOfExpr; // ctor
147 friend class ObjCMessageExpr; // ctor
148 friend class ObjCArrayLiteral; // ctor
149 friend class ObjCDictionaryLiteral; // ctor
150 friend class ShuffleVectorExpr; // ctor
151 friend class ParenListExpr; // ctor
152 friend class CXXUnresolvedConstructExpr; // ctor
153 friend class CXXDependentScopeMemberExpr; // ctor
154 friend class OverloadExpr; // ctor
155 friend class PseudoObjectExpr; // ctor
156 friend class AtomicExpr; // ctor
157 unsigned : NumStmtBits;
159 unsigned ValueKind : 2;
160 unsigned ObjectKind : 2;
161 unsigned TypeDependent : 1;
162 unsigned ValueDependent : 1;
163 unsigned InstantiationDependent : 1;
164 unsigned ContainsUnexpandedParameterPack : 1;
166 enum { NumExprBits = 16 };
168 class CharacterLiteralBitfields {
169 friend class CharacterLiteral;
170 unsigned : NumExprBits;
175 class FloatingLiteralBitfields {
176 friend class FloatingLiteral;
177 unsigned : NumExprBits;
179 unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
180 unsigned IsExact : 1;
183 class UnaryExprOrTypeTraitExprBitfields {
184 friend class UnaryExprOrTypeTraitExpr;
185 unsigned : NumExprBits;
188 unsigned IsType : 1; // true if operand is a type, false if an expression.
191 class DeclRefExprBitfields {
192 friend class DeclRefExpr;
193 friend class ASTStmtReader; // deserialization
194 unsigned : NumExprBits;
196 unsigned HasQualifier : 1;
197 unsigned HasTemplateKWAndArgsInfo : 1;
198 unsigned HasFoundDecl : 1;
199 unsigned HadMultipleCandidates : 1;
200 unsigned RefersToEnclosingLocal : 1;
203 class CastExprBitfields {
204 friend class CastExpr;
205 unsigned : NumExprBits;
208 unsigned BasePathSize : 32 - 6 - NumExprBits;
211 class CallExprBitfields {
212 friend class CallExpr;
213 unsigned : NumExprBits;
215 unsigned NumPreArgs : 1;
218 class ExprWithCleanupsBitfields {
219 friend class ExprWithCleanups;
220 friend class ASTStmtReader; // deserialization
222 unsigned : NumExprBits;
224 unsigned NumObjects : 32 - NumExprBits;
227 class PseudoObjectExprBitfields {
228 friend class PseudoObjectExpr;
229 friend class ASTStmtReader; // deserialization
231 unsigned : NumExprBits;
233 // These don't need to be particularly wide, because they're
234 // strictly limited by the forms of expressions we permit.
235 unsigned NumSubExprs : 8;
236 unsigned ResultIndex : 32 - 8 - NumExprBits;
239 class ObjCIndirectCopyRestoreExprBitfields {
240 friend class ObjCIndirectCopyRestoreExpr;
241 unsigned : NumExprBits;
243 unsigned ShouldCopy : 1;
246 class InitListExprBitfields {
247 friend class InitListExpr;
249 unsigned : NumExprBits;
251 /// Whether this initializer list originally had a GNU array-range
252 /// designator in it. This is a temporary marker used by CodeGen.
253 unsigned HadArrayRangeDesignator : 1;
255 /// Whether this initializer list initializes a std::initializer_list
257 unsigned InitializesStdInitializerList : 1;
260 class TypeTraitExprBitfields {
261 friend class TypeTraitExpr;
262 friend class ASTStmtReader;
263 friend class ASTStmtWriter;
265 unsigned : NumExprBits;
267 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
270 /// \brief If this expression is not value-dependent, this indicates whether
271 /// the trait evaluated true or false.
274 /// \brief The number of arguments to this type trait.
275 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
279 // FIXME: this is wasteful on 64-bit platforms.
282 StmtBitfields StmtBits;
283 CompoundStmtBitfields CompoundStmtBits;
284 ExprBitfields ExprBits;
285 CharacterLiteralBitfields CharacterLiteralBits;
286 FloatingLiteralBitfields FloatingLiteralBits;
287 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
288 DeclRefExprBitfields DeclRefExprBits;
289 CastExprBitfields CastExprBits;
290 CallExprBitfields CallExprBits;
291 ExprWithCleanupsBitfields ExprWithCleanupsBits;
292 PseudoObjectExprBitfields PseudoObjectExprBits;
293 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
294 InitListExprBitfields InitListExprBits;
295 TypeTraitExprBitfields TypeTraitExprBits;
298 friend class ASTStmtReader;
299 friend class ASTStmtWriter;
302 // Only allow allocation of Stmts using the allocator in ASTContext
303 // or by doing a placement new.
304 void* operator new(size_t bytes, ASTContext& C,
305 unsigned alignment = 8) throw() {
306 return ::operator new(bytes, C, alignment);
309 void* operator new(size_t bytes, ASTContext* C,
310 unsigned alignment = 8) throw() {
311 return ::operator new(bytes, *C, alignment);
314 void* operator new(size_t bytes, void* mem) throw() {
318 void operator delete(void*, ASTContext&, unsigned) throw() { }
319 void operator delete(void*, ASTContext*, unsigned) throw() { }
320 void operator delete(void*, std::size_t) throw() { }
321 void operator delete(void*, void*) throw() { }
324 /// \brief A placeholder type used to construct an empty shell of a
325 /// type, that will be filled in later (e.g., by some
326 /// de-serialization).
327 struct EmptyShell { };
330 /// \brief Whether statistic collection is enabled.
331 static bool StatisticsEnabled;
334 /// \brief Construct an empty statement.
335 explicit Stmt(StmtClass SC, EmptyShell) {
336 StmtBits.sClass = SC;
337 if (StatisticsEnabled) Stmt::addStmtClass(SC);
342 StmtBits.sClass = SC;
343 if (StatisticsEnabled) Stmt::addStmtClass(SC);
346 StmtClass getStmtClass() const {
347 return static_cast<StmtClass>(StmtBits.sClass);
349 const char *getStmtClassName() const;
351 /// SourceLocation tokens are not useful in isolation - they are low level
352 /// value objects created/interpreted by SourceManager. We assume AST
353 /// clients will have a pointer to the respective SourceManager.
354 SourceRange getSourceRange() const LLVM_READONLY;
355 SourceLocation getLocStart() const LLVM_READONLY;
356 SourceLocation getLocEnd() const LLVM_READONLY;
358 // global temp stats (until we have a per-module visitor)
359 static void addStmtClass(const StmtClass s);
360 static void EnableStatistics();
361 static void PrintStats();
363 /// dump - This does a local dump of the specified AST fragment. It dumps the
364 /// specified node and a few nodes underneath it, but not the whole subtree.
365 /// This is useful in a debugger.
366 LLVM_ATTRIBUTE_USED void dump() const;
367 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
368 void dump(raw_ostream &OS, SourceManager &SM) const;
370 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
371 void dumpAll() const;
372 void dumpAll(SourceManager &SM) const;
374 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
375 /// back to its original source language syntax.
376 void dumpPretty(ASTContext &Context) const;
377 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
378 const PrintingPolicy &Policy,
379 unsigned Indentation = 0) const;
381 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
382 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
383 void viewAST() const;
385 /// Skip past any implicit AST nodes which might surround this
386 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
387 Stmt *IgnoreImplicit();
389 const Stmt *stripLabelLikeStatements() const;
390 Stmt *stripLabelLikeStatements() {
391 return const_cast<Stmt*>(
392 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
395 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
396 /// contain implicit control-flow in the order their subexpressions
397 /// are evaluated. This predicate returns true if this statement has
398 /// such implicit control-flow. Such statements are also specially handled
400 bool hasImplicitControlFlow() const;
402 /// Child Iterators: All subclasses must implement 'children'
403 /// to permit easy iteration over the substatements/subexpessions of an
404 /// AST node. This permits easy iteration over all nodes in the AST.
405 typedef StmtIterator child_iterator;
406 typedef ConstStmtIterator const_child_iterator;
408 typedef StmtRange child_range;
409 typedef ConstStmtRange const_child_range;
411 child_range children();
412 const_child_range children() const {
413 return const_cast<Stmt*>(this)->children();
416 child_iterator child_begin() { return children().first; }
417 child_iterator child_end() { return children().second; }
419 const_child_iterator child_begin() const { return children().first; }
420 const_child_iterator child_end() const { return children().second; }
422 /// \brief Produce a unique representation of the given statement.
424 /// \param ID once the profiling operation is complete, will contain
425 /// the unique representation of the given statement.
427 /// \param Context the AST context in which the statement resides
429 /// \param Canonical whether the profile should be based on the canonical
430 /// representation of this statement (e.g., where non-type template
431 /// parameters are identified by index/level rather than their
432 /// declaration pointers) or the exact representation of the statement as
433 /// written in the source.
434 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
435 bool Canonical) const;
438 /// DeclStmt - Adaptor class for mixing declarations with statements and
439 /// expressions. For example, CompoundStmt mixes statements, expressions
440 /// and declarations (variables, types). Another example is ForStmt, where
441 /// the first statement can be an expression or a declaration.
443 class DeclStmt : public Stmt {
445 SourceLocation StartLoc, EndLoc;
448 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
449 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
450 StartLoc(startLoc), EndLoc(endLoc) {}
452 /// \brief Build an empty declaration statement.
453 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
455 /// isSingleDecl - This method returns true if this DeclStmt refers
456 /// to a single Decl.
457 bool isSingleDecl() const {
458 return DG.isSingleDecl();
461 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
462 Decl *getSingleDecl() { return DG.getSingleDecl(); }
464 const DeclGroupRef getDeclGroup() const { return DG; }
465 DeclGroupRef getDeclGroup() { return DG; }
466 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
468 SourceLocation getStartLoc() const { return StartLoc; }
469 void setStartLoc(SourceLocation L) { StartLoc = L; }
470 SourceLocation getEndLoc() const { return EndLoc; }
471 void setEndLoc(SourceLocation L) { EndLoc = L; }
473 SourceRange getSourceRange() const LLVM_READONLY {
474 return SourceRange(StartLoc, EndLoc);
477 static bool classof(const Stmt *T) {
478 return T->getStmtClass() == DeclStmtClass;
481 // Iterators over subexpressions.
482 child_range children() {
483 return child_range(child_iterator(DG.begin(), DG.end()),
484 child_iterator(DG.end(), DG.end()));
487 typedef DeclGroupRef::iterator decl_iterator;
488 typedef DeclGroupRef::const_iterator const_decl_iterator;
490 decl_iterator decl_begin() { return DG.begin(); }
491 decl_iterator decl_end() { return DG.end(); }
492 const_decl_iterator decl_begin() const { return DG.begin(); }
493 const_decl_iterator decl_end() const { return DG.end(); }
495 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
496 reverse_decl_iterator decl_rbegin() {
497 return reverse_decl_iterator(decl_end());
499 reverse_decl_iterator decl_rend() {
500 return reverse_decl_iterator(decl_begin());
504 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
506 class NullStmt : public Stmt {
507 SourceLocation SemiLoc;
509 /// \brief True if the null statement was preceded by an empty macro, e.g:
514 bool HasLeadingEmptyMacro;
516 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
517 : Stmt(NullStmtClass), SemiLoc(L),
518 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
520 /// \brief Build an empty null statement.
521 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
522 HasLeadingEmptyMacro(false) { }
524 SourceLocation getSemiLoc() const { return SemiLoc; }
525 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
527 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
529 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
531 static bool classof(const Stmt *T) {
532 return T->getStmtClass() == NullStmtClass;
535 child_range children() { return child_range(); }
537 friend class ASTStmtReader;
538 friend class ASTStmtWriter;
541 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
543 class CompoundStmt : public Stmt {
545 SourceLocation LBracLoc, RBracLoc;
547 CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
548 SourceLocation LB, SourceLocation RB);
550 // \brief Build an empty compound statment with a location.
551 explicit CompoundStmt(SourceLocation Loc)
552 : Stmt(CompoundStmtClass), Body(0), LBracLoc(Loc), RBracLoc(Loc) {
553 CompoundStmtBits.NumStmts = 0;
556 // \brief Build an empty compound statement.
557 explicit CompoundStmt(EmptyShell Empty)
558 : Stmt(CompoundStmtClass, Empty), Body(0) {
559 CompoundStmtBits.NumStmts = 0;
562 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
564 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
565 unsigned size() const { return CompoundStmtBits.NumStmts; }
567 typedef Stmt** body_iterator;
568 body_iterator body_begin() { return Body; }
569 body_iterator body_end() { return Body + size(); }
570 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
572 void setLastStmt(Stmt *S) {
573 assert(!body_empty() && "setLastStmt");
577 typedef Stmt* const * const_body_iterator;
578 const_body_iterator body_begin() const { return Body; }
579 const_body_iterator body_end() const { return Body + size(); }
580 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
582 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
583 reverse_body_iterator body_rbegin() {
584 return reverse_body_iterator(body_end());
586 reverse_body_iterator body_rend() {
587 return reverse_body_iterator(body_begin());
590 typedef std::reverse_iterator<const_body_iterator>
591 const_reverse_body_iterator;
593 const_reverse_body_iterator body_rbegin() const {
594 return const_reverse_body_iterator(body_end());
597 const_reverse_body_iterator body_rend() const {
598 return const_reverse_body_iterator(body_begin());
601 SourceRange getSourceRange() const LLVM_READONLY {
602 return SourceRange(LBracLoc, RBracLoc);
605 SourceLocation getLBracLoc() const { return LBracLoc; }
606 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
607 SourceLocation getRBracLoc() const { return RBracLoc; }
608 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
610 static bool classof(const Stmt *T) {
611 return T->getStmtClass() == CompoundStmtClass;
615 child_range children() {
616 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
619 const_child_range children() const {
620 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
624 // SwitchCase is the base class for CaseStmt and DefaultStmt,
625 class SwitchCase : public Stmt {
627 // A pointer to the following CaseStmt or DefaultStmt class,
628 // used by SwitchStmt.
629 SwitchCase *NextSwitchCase;
631 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
634 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
636 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
638 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
641 const Stmt *getSubStmt() const {
642 return const_cast<SwitchCase*>(this)->getSubStmt();
645 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
647 static bool classof(const Stmt *T) {
648 return T->getStmtClass() == CaseStmtClass ||
649 T->getStmtClass() == DefaultStmtClass;
653 class CaseStmt : public SwitchCase {
654 enum { LHS, RHS, SUBSTMT, END_EXPR };
655 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
656 // GNU "case 1 ... 4" extension
657 SourceLocation CaseLoc;
658 SourceLocation EllipsisLoc;
659 SourceLocation ColonLoc;
661 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
662 SourceLocation ellipsisLoc, SourceLocation colonLoc)
663 : SwitchCase(CaseStmtClass) {
664 SubExprs[SUBSTMT] = 0;
665 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
666 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
668 EllipsisLoc = ellipsisLoc;
672 /// \brief Build an empty switch case statement.
673 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
675 SourceLocation getCaseLoc() const { return CaseLoc; }
676 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
677 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
678 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
679 SourceLocation getColonLoc() const { return ColonLoc; }
680 void setColonLoc(SourceLocation L) { ColonLoc = L; }
682 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
683 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
684 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
686 const Expr *getLHS() const {
687 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
689 const Expr *getRHS() const {
690 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
692 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
694 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
695 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
696 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
699 SourceRange getSourceRange() const LLVM_READONLY {
700 // Handle deeply nested case statements with iteration instead of recursion.
701 const CaseStmt *CS = this;
702 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
705 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
707 static bool classof(const Stmt *T) {
708 return T->getStmtClass() == CaseStmtClass;
712 child_range children() {
713 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
717 class DefaultStmt : public SwitchCase {
719 SourceLocation DefaultLoc;
720 SourceLocation ColonLoc;
722 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
723 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
726 /// \brief Build an empty default statement.
727 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
729 Stmt *getSubStmt() { return SubStmt; }
730 const Stmt *getSubStmt() const { return SubStmt; }
731 void setSubStmt(Stmt *S) { SubStmt = S; }
733 SourceLocation getDefaultLoc() const { return DefaultLoc; }
734 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
735 SourceLocation getColonLoc() const { return ColonLoc; }
736 void setColonLoc(SourceLocation L) { ColonLoc = L; }
738 SourceRange getSourceRange() const LLVM_READONLY {
739 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
741 static bool classof(const Stmt *T) {
742 return T->getStmtClass() == DefaultStmtClass;
746 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
750 /// LabelStmt - Represents a label, which has a substatement. For example:
753 class LabelStmt : public Stmt {
756 SourceLocation IdentLoc;
758 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
759 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
762 // \brief Build an empty label statement.
763 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
765 SourceLocation getIdentLoc() const { return IdentLoc; }
766 LabelDecl *getDecl() const { return TheDecl; }
767 void setDecl(LabelDecl *D) { TheDecl = D; }
768 const char *getName() const;
769 Stmt *getSubStmt() { return SubStmt; }
770 const Stmt *getSubStmt() const { return SubStmt; }
771 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
772 void setSubStmt(Stmt *SS) { SubStmt = SS; }
774 SourceRange getSourceRange() const LLVM_READONLY {
775 return SourceRange(IdentLoc, SubStmt->getLocEnd());
777 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
779 static bool classof(const Stmt *T) {
780 return T->getStmtClass() == LabelStmtClass;
785 /// \brief Represents an attribute applied to a statement.
787 /// Represents an attribute applied to a statement. For example:
788 /// [[omp::for(...)]] for (...) { ... }
790 class AttributedStmt : public Stmt {
792 SourceLocation AttrLoc;
794 const Attr *Attrs[1];
796 friend class ASTStmtReader;
798 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
799 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
800 NumAttrs(Attrs.size()) {
801 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*));
804 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
805 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
806 memset(Attrs, 0, NumAttrs * sizeof(Attr*));
810 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc,
811 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
812 // \brief Build an empty attributed statement.
813 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs);
815 SourceLocation getAttrLoc() const { return AttrLoc; }
816 ArrayRef<const Attr*> getAttrs() const {
817 return ArrayRef<const Attr*>(Attrs, NumAttrs);
819 Stmt *getSubStmt() { return SubStmt; }
820 const Stmt *getSubStmt() const { return SubStmt; }
822 SourceRange getSourceRange() const LLVM_READONLY {
823 return SourceRange(AttrLoc, SubStmt->getLocEnd());
825 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
827 static bool classof(const Stmt *T) {
828 return T->getStmtClass() == AttributedStmtClass;
833 /// IfStmt - This represents an if/then/else.
835 class IfStmt : public Stmt {
836 enum { VAR, COND, THEN, ELSE, END_EXPR };
837 Stmt* SubExprs[END_EXPR];
839 SourceLocation IfLoc;
840 SourceLocation ElseLoc;
843 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
844 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
846 /// \brief Build an empty if/then/else statement
847 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
849 /// \brief Retrieve the variable declared in this "if" statement, if any.
851 /// In the following example, "x" is the condition variable.
853 /// if (int x = foo()) {
854 /// printf("x is %d", x);
857 VarDecl *getConditionVariable() const;
858 void setConditionVariable(ASTContext &C, VarDecl *V);
860 /// If this IfStmt has a condition variable, return the faux DeclStmt
861 /// associated with the creation of that condition variable.
862 const DeclStmt *getConditionVariableDeclStmt() const {
863 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
866 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
867 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
868 const Stmt *getThen() const { return SubExprs[THEN]; }
869 void setThen(Stmt *S) { SubExprs[THEN] = S; }
870 const Stmt *getElse() const { return SubExprs[ELSE]; }
871 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
873 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
874 Stmt *getThen() { return SubExprs[THEN]; }
875 Stmt *getElse() { return SubExprs[ELSE]; }
877 SourceLocation getIfLoc() const { return IfLoc; }
878 void setIfLoc(SourceLocation L) { IfLoc = L; }
879 SourceLocation getElseLoc() const { return ElseLoc; }
880 void setElseLoc(SourceLocation L) { ElseLoc = L; }
882 SourceRange getSourceRange() const LLVM_READONLY {
884 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
886 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
889 // Iterators over subexpressions. The iterators will include iterating
890 // over the initialization expression referenced by the condition variable.
891 child_range children() {
892 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
895 static bool classof(const Stmt *T) {
896 return T->getStmtClass() == IfStmtClass;
900 /// SwitchStmt - This represents a 'switch' stmt.
902 class SwitchStmt : public Stmt {
903 enum { VAR, COND, BODY, END_EXPR };
904 Stmt* SubExprs[END_EXPR];
905 // This points to a linked list of case and default statements.
906 SwitchCase *FirstCase;
907 SourceLocation SwitchLoc;
909 /// If the SwitchStmt is a switch on an enum value, this records whether
910 /// all the enum values were covered by CaseStmts. This value is meant to
911 /// be a hint for possible clients.
912 unsigned AllEnumCasesCovered : 1;
915 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
917 /// \brief Build a empty switch statement.
918 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
920 /// \brief Retrieve the variable declared in this "switch" statement, if any.
922 /// In the following example, "x" is the condition variable.
924 /// switch (int x = foo()) {
929 VarDecl *getConditionVariable() const;
930 void setConditionVariable(ASTContext &C, VarDecl *V);
932 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
933 /// associated with the creation of that condition variable.
934 const DeclStmt *getConditionVariableDeclStmt() const {
935 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
938 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
939 const Stmt *getBody() const { return SubExprs[BODY]; }
940 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
942 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
943 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
944 Stmt *getBody() { return SubExprs[BODY]; }
945 void setBody(Stmt *S) { SubExprs[BODY] = S; }
946 SwitchCase *getSwitchCaseList() { return FirstCase; }
948 /// \brief Set the case list for this switch statement.
950 /// The caller is responsible for incrementing the retain counts on
951 /// all of the SwitchCase statements in this list.
952 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
954 SourceLocation getSwitchLoc() const { return SwitchLoc; }
955 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
957 void setBody(Stmt *S, SourceLocation SL) {
961 void addSwitchCase(SwitchCase *SC) {
962 assert(!SC->getNextSwitchCase()
963 && "case/default already added to a switch");
964 SC->setNextSwitchCase(FirstCase);
968 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
969 /// switch over an enum value then all cases have been explicitly covered.
970 void setAllEnumCasesCovered() {
971 AllEnumCasesCovered = 1;
974 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
975 /// have been explicitly covered.
976 bool isAllEnumCasesCovered() const {
977 return (bool) AllEnumCasesCovered;
980 SourceRange getSourceRange() const LLVM_READONLY {
981 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
984 child_range children() {
985 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
988 static bool classof(const Stmt *T) {
989 return T->getStmtClass() == SwitchStmtClass;
994 /// WhileStmt - This represents a 'while' stmt.
996 class WhileStmt : public Stmt {
997 enum { VAR, COND, BODY, END_EXPR };
998 Stmt* SubExprs[END_EXPR];
999 SourceLocation WhileLoc;
1001 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1004 /// \brief Build an empty while statement.
1005 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1007 /// \brief Retrieve the variable declared in this "while" statement, if any.
1009 /// In the following example, "x" is the condition variable.
1011 /// while (int x = random()) {
1015 VarDecl *getConditionVariable() const;
1016 void setConditionVariable(ASTContext &C, VarDecl *V);
1018 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1019 /// associated with the creation of that condition variable.
1020 const DeclStmt *getConditionVariableDeclStmt() const {
1021 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1024 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1025 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1026 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1027 Stmt *getBody() { return SubExprs[BODY]; }
1028 const Stmt *getBody() const { return SubExprs[BODY]; }
1029 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1031 SourceLocation getWhileLoc() const { return WhileLoc; }
1032 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1034 SourceRange getSourceRange() const LLVM_READONLY {
1035 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
1037 static bool classof(const Stmt *T) {
1038 return T->getStmtClass() == WhileStmtClass;
1042 child_range children() {
1043 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1047 /// DoStmt - This represents a 'do/while' stmt.
1049 class DoStmt : public Stmt {
1050 enum { BODY, COND, END_EXPR };
1051 Stmt* SubExprs[END_EXPR];
1052 SourceLocation DoLoc;
1053 SourceLocation WhileLoc;
1054 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1057 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1059 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1060 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1061 SubExprs[BODY] = body;
1064 /// \brief Build an empty do-while statement.
1065 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1067 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1068 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1069 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1070 Stmt *getBody() { return SubExprs[BODY]; }
1071 const Stmt *getBody() const { return SubExprs[BODY]; }
1072 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1074 SourceLocation getDoLoc() const { return DoLoc; }
1075 void setDoLoc(SourceLocation L) { DoLoc = L; }
1076 SourceLocation getWhileLoc() const { return WhileLoc; }
1077 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1079 SourceLocation getRParenLoc() const { return RParenLoc; }
1080 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1082 SourceRange getSourceRange() const LLVM_READONLY {
1083 return SourceRange(DoLoc, RParenLoc);
1085 static bool classof(const Stmt *T) {
1086 return T->getStmtClass() == DoStmtClass;
1090 child_range children() {
1091 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1096 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1097 /// the init/cond/inc parts of the ForStmt will be null if they were not
1098 /// specified in the source.
1100 class ForStmt : public Stmt {
1101 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1102 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1103 SourceLocation ForLoc;
1104 SourceLocation LParenLoc, RParenLoc;
1107 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1108 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1110 /// \brief Build an empty for statement.
1111 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1113 Stmt *getInit() { return SubExprs[INIT]; }
1115 /// \brief Retrieve the variable declared in this "for" statement, if any.
1117 /// In the following example, "y" is the condition variable.
1119 /// for (int x = random(); int y = mangle(x); ++x) {
1123 VarDecl *getConditionVariable() const;
1124 void setConditionVariable(ASTContext &C, VarDecl *V);
1126 /// If this ForStmt has a condition variable, return the faux DeclStmt
1127 /// associated with the creation of that condition variable.
1128 const DeclStmt *getConditionVariableDeclStmt() const {
1129 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1132 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1133 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1134 Stmt *getBody() { return SubExprs[BODY]; }
1136 const Stmt *getInit() const { return SubExprs[INIT]; }
1137 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1138 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1139 const Stmt *getBody() const { return SubExprs[BODY]; }
1141 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1142 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1143 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1144 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1146 SourceLocation getForLoc() const { return ForLoc; }
1147 void setForLoc(SourceLocation L) { ForLoc = L; }
1148 SourceLocation getLParenLoc() const { return LParenLoc; }
1149 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1150 SourceLocation getRParenLoc() const { return RParenLoc; }
1151 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1153 SourceRange getSourceRange() const LLVM_READONLY {
1154 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1156 static bool classof(const Stmt *T) {
1157 return T->getStmtClass() == ForStmtClass;
1161 child_range children() {
1162 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1166 /// GotoStmt - This represents a direct goto.
1168 class GotoStmt : public Stmt {
1170 SourceLocation GotoLoc;
1171 SourceLocation LabelLoc;
1173 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1174 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1176 /// \brief Build an empty goto statement.
1177 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1179 LabelDecl *getLabel() const { return Label; }
1180 void setLabel(LabelDecl *D) { Label = D; }
1182 SourceLocation getGotoLoc() const { return GotoLoc; }
1183 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1184 SourceLocation getLabelLoc() const { return LabelLoc; }
1185 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1187 SourceRange getSourceRange() const LLVM_READONLY {
1188 return SourceRange(GotoLoc, LabelLoc);
1190 static bool classof(const Stmt *T) {
1191 return T->getStmtClass() == GotoStmtClass;
1195 child_range children() { return child_range(); }
1198 /// IndirectGotoStmt - This represents an indirect goto.
1200 class IndirectGotoStmt : public Stmt {
1201 SourceLocation GotoLoc;
1202 SourceLocation StarLoc;
1205 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1207 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1208 Target((Stmt*)target) {}
1210 /// \brief Build an empty indirect goto statement.
1211 explicit IndirectGotoStmt(EmptyShell Empty)
1212 : Stmt(IndirectGotoStmtClass, Empty) { }
1214 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1215 SourceLocation getGotoLoc() const { return GotoLoc; }
1216 void setStarLoc(SourceLocation L) { StarLoc = L; }
1217 SourceLocation getStarLoc() const { return StarLoc; }
1219 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1220 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1221 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1223 /// getConstantTarget - Returns the fixed target of this indirect
1224 /// goto, if one exists.
1225 LabelDecl *getConstantTarget();
1226 const LabelDecl *getConstantTarget() const {
1227 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1230 SourceRange getSourceRange() const LLVM_READONLY {
1231 return SourceRange(GotoLoc, Target->getLocEnd());
1234 static bool classof(const Stmt *T) {
1235 return T->getStmtClass() == IndirectGotoStmtClass;
1239 child_range children() { return child_range(&Target, &Target+1); }
1243 /// ContinueStmt - This represents a continue.
1245 class ContinueStmt : public Stmt {
1246 SourceLocation ContinueLoc;
1248 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1250 /// \brief Build an empty continue statement.
1251 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1253 SourceLocation getContinueLoc() const { return ContinueLoc; }
1254 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1256 SourceRange getSourceRange() const LLVM_READONLY {
1257 return SourceRange(ContinueLoc);
1260 static bool classof(const Stmt *T) {
1261 return T->getStmtClass() == ContinueStmtClass;
1265 child_range children() { return child_range(); }
1268 /// BreakStmt - This represents a break.
1270 class BreakStmt : public Stmt {
1271 SourceLocation BreakLoc;
1273 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1275 /// \brief Build an empty break statement.
1276 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1278 SourceLocation getBreakLoc() const { return BreakLoc; }
1279 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1281 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
1283 static bool classof(const Stmt *T) {
1284 return T->getStmtClass() == BreakStmtClass;
1288 child_range children() { return child_range(); }
1292 /// ReturnStmt - This represents a return, optionally of an expression:
1296 /// Note that GCC allows return with no argument in a function declared to
1297 /// return a value, and it allows returning a value in functions declared to
1298 /// return void. We explicitly model this in the AST, which means you can't
1299 /// depend on the return type of the function and the presence of an argument.
1301 class ReturnStmt : public Stmt {
1303 SourceLocation RetLoc;
1304 const VarDecl *NRVOCandidate;
1307 ReturnStmt(SourceLocation RL)
1308 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1310 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1311 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1312 NRVOCandidate(NRVOCandidate) {}
1314 /// \brief Build an empty return expression.
1315 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1317 const Expr *getRetValue() const;
1318 Expr *getRetValue();
1319 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1321 SourceLocation getReturnLoc() const { return RetLoc; }
1322 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1324 /// \brief Retrieve the variable that might be used for the named return
1325 /// value optimization.
1327 /// The optimization itself can only be performed if the variable is
1328 /// also marked as an NRVO object.
1329 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1330 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1332 SourceRange getSourceRange() const LLVM_READONLY;
1334 static bool classof(const Stmt *T) {
1335 return T->getStmtClass() == ReturnStmtClass;
1339 child_range children() {
1340 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1341 return child_range();
1345 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1347 class AsmStmt : public Stmt {
1349 SourceLocation AsmLoc;
1350 /// \brief True if the assembly statement does not have any input or output
1354 /// \brief If true, treat this inline assembly as having side effects.
1355 /// This assembly statement should not be optimized, deleted or moved.
1358 unsigned NumOutputs;
1360 unsigned NumClobbers;
1362 IdentifierInfo **Names;
1365 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1366 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1367 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1368 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1371 /// \brief Build an empty inline-assembly statement.
1372 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1373 Stmt(SC, Empty), Names(0), Exprs(0) { }
1375 SourceLocation getAsmLoc() const { return AsmLoc; }
1376 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1378 bool isSimple() const { return IsSimple; }
1379 void setSimple(bool V) { IsSimple = V; }
1381 bool isVolatile() const { return IsVolatile; }
1382 void setVolatile(bool V) { IsVolatile = V; }
1384 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
1386 //===--- Asm String Analysis ---===//
1388 /// Assemble final IR asm string.
1389 std::string generateAsmString(ASTContext &C) const;
1391 //===--- Output operands ---===//
1393 unsigned getNumOutputs() const { return NumOutputs; }
1395 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1399 StringRef getOutputName(unsigned i) const {
1400 if (IdentifierInfo *II = getOutputIdentifier(i))
1401 return II->getName();
1406 /// getOutputConstraint - Return the constraint string for the specified
1407 /// output operand. All output constraints are known to be non-empty (either
1409 StringRef getOutputConstraint(unsigned i) const;
1411 /// isOutputPlusConstraint - Return true if the specified output constraint
1412 /// is a "+" constraint (which is both an input and an output) or false if it
1413 /// is an "=" constraint (just an output).
1414 bool isOutputPlusConstraint(unsigned i) const {
1415 return getOutputConstraint(i)[0] == '+';
1418 const Expr *getOutputExpr(unsigned i) const;
1420 /// getNumPlusOperands - Return the number of output operands that have a "+"
1422 unsigned getNumPlusOperands() const;
1424 //===--- Input operands ---===//
1426 unsigned getNumInputs() const { return NumInputs; }
1428 IdentifierInfo *getInputIdentifier(unsigned i) const {
1429 return Names[i + NumOutputs];
1432 StringRef getInputName(unsigned i) const {
1433 if (IdentifierInfo *II = getInputIdentifier(i))
1434 return II->getName();
1439 /// getInputConstraint - Return the specified input constraint. Unlike output
1440 /// constraints, these can be empty.
1441 StringRef getInputConstraint(unsigned i) const;
1443 const Expr *getInputExpr(unsigned i) const;
1445 //===--- Other ---===//
1447 unsigned getNumClobbers() const { return NumClobbers; }
1448 StringRef getClobber(unsigned i) const;
1450 static bool classof(const Stmt *T) {
1451 return T->getStmtClass() == GCCAsmStmtClass ||
1452 T->getStmtClass() == MSAsmStmtClass;
1455 // Input expr iterators.
1457 typedef ExprIterator inputs_iterator;
1458 typedef ConstExprIterator const_inputs_iterator;
1460 inputs_iterator begin_inputs() {
1461 return &Exprs[0] + NumOutputs;
1464 inputs_iterator end_inputs() {
1465 return &Exprs[0] + NumOutputs + NumInputs;
1468 const_inputs_iterator begin_inputs() const {
1469 return &Exprs[0] + NumOutputs;
1472 const_inputs_iterator end_inputs() const {
1473 return &Exprs[0] + NumOutputs + NumInputs;
1476 // Output expr iterators.
1478 typedef ExprIterator outputs_iterator;
1479 typedef ConstExprIterator const_outputs_iterator;
1481 outputs_iterator begin_outputs() {
1484 outputs_iterator end_outputs() {
1485 return &Exprs[0] + NumOutputs;
1488 const_outputs_iterator begin_outputs() const {
1491 const_outputs_iterator end_outputs() const {
1492 return &Exprs[0] + NumOutputs;
1495 child_range children() {
1496 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1500 /// This represents a GCC inline-assembly statement extension.
1502 class GCCAsmStmt : public AsmStmt {
1503 SourceLocation RParenLoc;
1504 StringLiteral *AsmStr;
1506 // FIXME: If we wanted to, we could allocate all of these in one big array.
1507 StringLiteral **Constraints;
1508 StringLiteral **Clobbers;
1511 GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
1512 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1513 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1514 StringLiteral *asmstr, unsigned numclobbers,
1515 StringLiteral **clobbers, SourceLocation rparenloc);
1517 /// \brief Build an empty inline-assembly statement.
1518 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1519 Constraints(0), Clobbers(0) { }
1521 SourceLocation getRParenLoc() const { return RParenLoc; }
1522 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1524 //===--- Asm String Analysis ---===//
1526 const StringLiteral *getAsmString() const { return AsmStr; }
1527 StringLiteral *getAsmString() { return AsmStr; }
1528 void setAsmString(StringLiteral *E) { AsmStr = E; }
1530 /// AsmStringPiece - this is part of a decomposed asm string specification
1531 /// (for use with the AnalyzeAsmString function below). An asm string is
1532 /// considered to be a concatenation of these parts.
1533 class AsmStringPiece {
1536 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1537 Operand // Operand reference, with optional modifier %c4.
1544 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1545 AsmStringPiece(unsigned OpNo, char Modifier)
1546 : MyKind(Operand), Str(), OperandNo(OpNo) {
1550 bool isString() const { return MyKind == String; }
1551 bool isOperand() const { return MyKind == Operand; }
1553 const std::string &getString() const {
1558 unsigned getOperandNo() const {
1559 assert(isOperand());
1563 /// getModifier - Get the modifier for this operand, if present. This
1564 /// returns '\0' if there was no modifier.
1565 char getModifier() const {
1566 assert(isOperand());
1571 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1572 /// it into pieces. If the asm string is erroneous, emit errors and return
1573 /// true, otherwise return false. This handles canonicalization and
1574 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1575 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1576 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1577 ASTContext &C, unsigned &DiagOffs) const;
1579 /// Assemble final IR asm string.
1580 std::string generateAsmString(ASTContext &C) const;
1582 //===--- Output operands ---===//
1584 StringRef getOutputConstraint(unsigned i) const;
1586 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1587 return Constraints[i];
1589 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1590 return Constraints[i];
1593 Expr *getOutputExpr(unsigned i);
1595 const Expr *getOutputExpr(unsigned i) const {
1596 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1599 //===--- Input operands ---===//
1601 StringRef getInputConstraint(unsigned i) const;
1603 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1604 return Constraints[i + NumOutputs];
1606 StringLiteral *getInputConstraintLiteral(unsigned i) {
1607 return Constraints[i + NumOutputs];
1610 Expr *getInputExpr(unsigned i);
1611 void setInputExpr(unsigned i, Expr *E);
1613 const Expr *getInputExpr(unsigned i) const {
1614 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1617 void setOutputsAndInputsAndClobbers(ASTContext &C,
1618 IdentifierInfo **Names,
1619 StringLiteral **Constraints,
1621 unsigned NumOutputs,
1623 StringLiteral **Clobbers,
1624 unsigned NumClobbers);
1626 //===--- Other ---===//
1628 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1629 /// translate this into a numeric value needed to reference the same operand.
1630 /// This returns -1 if the operand name is invalid.
1631 int getNamedOperand(StringRef SymbolicName) const;
1633 StringRef getClobber(unsigned i) const;
1634 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1635 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1639 SourceRange getSourceRange() const LLVM_READONLY {
1640 return SourceRange(AsmLoc, RParenLoc);
1643 static bool classof(const Stmt *T) {
1644 return T->getStmtClass() == GCCAsmStmtClass;
1648 /// This represents a Microsoft inline-assembly statement extension.
1650 class MSAsmStmt : public AsmStmt {
1651 SourceLocation AsmLoc, LBraceLoc, EndLoc;
1654 unsigned NumAsmToks;
1657 StringRef *Constraints;
1658 StringRef *Clobbers;
1661 MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1662 bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1663 unsigned numoutputs, unsigned numinputs,
1664 ArrayRef<IdentifierInfo*> names, ArrayRef<StringRef> constraints,
1665 ArrayRef<Expr*> exprs, StringRef asmstr,
1666 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1668 /// \brief Build an empty MS-style inline-assembly statement.
1669 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1670 NumAsmToks(0), AsmToks(0), Constraints(0), Clobbers(0) { }
1672 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1673 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1674 SourceLocation getEndLoc() const { return EndLoc; }
1675 void setEndLoc(SourceLocation L) { EndLoc = L; }
1677 bool hasBraces() const { return LBraceLoc.isValid(); }
1679 unsigned getNumAsmToks() { return NumAsmToks; }
1680 Token *getAsmToks() { return AsmToks; }
1682 //===--- Asm String Analysis ---===//
1684 const std::string *getAsmString() const { return &AsmStr; }
1685 std::string *getAsmString() { return &AsmStr; }
1686 void setAsmString(StringRef &E) { AsmStr = E.str(); }
1688 /// Assemble final IR asm string.
1689 std::string generateAsmString(ASTContext &C) const;
1691 //===--- Output operands ---===//
1693 StringRef getOutputConstraint(unsigned i) const {
1694 return Constraints[i];
1697 Expr *getOutputExpr(unsigned i);
1699 const Expr *getOutputExpr(unsigned i) const {
1700 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1703 //===--- Input operands ---===//
1705 StringRef getInputConstraint(unsigned i) const {
1706 return Constraints[i + NumOutputs];
1709 Expr *getInputExpr(unsigned i);
1710 void setInputExpr(unsigned i, Expr *E);
1712 const Expr *getInputExpr(unsigned i) const {
1713 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1716 //===--- Other ---===//
1718 StringRef getClobber(unsigned i) const { return Clobbers[i]; }
1720 SourceRange getSourceRange() const LLVM_READONLY {
1721 return SourceRange(AsmLoc, EndLoc);
1723 static bool classof(const Stmt *T) {
1724 return T->getStmtClass() == MSAsmStmtClass;
1727 child_range children() {
1728 return child_range(&Exprs[0], &Exprs[0]);
1732 class SEHExceptStmt : public Stmt {
1736 enum { FILTER_EXPR, BLOCK };
1738 SEHExceptStmt(SourceLocation Loc,
1742 friend class ASTReader;
1743 friend class ASTStmtReader;
1744 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1747 static SEHExceptStmt* Create(ASTContext &C,
1748 SourceLocation ExceptLoc,
1751 SourceRange getSourceRange() const LLVM_READONLY {
1752 return SourceRange(getExceptLoc(), getEndLoc());
1755 SourceLocation getExceptLoc() const { return Loc; }
1756 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1758 Expr *getFilterExpr() const {
1759 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1762 CompoundStmt *getBlock() const {
1763 return llvm::cast<CompoundStmt>(Children[BLOCK]);
1766 child_range children() {
1767 return child_range(Children,Children+2);
1770 static bool classof(const Stmt *T) {
1771 return T->getStmtClass() == SEHExceptStmtClass;
1776 class SEHFinallyStmt : public Stmt {
1780 SEHFinallyStmt(SourceLocation Loc,
1783 friend class ASTReader;
1784 friend class ASTStmtReader;
1785 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1788 static SEHFinallyStmt* Create(ASTContext &C,
1789 SourceLocation FinallyLoc,
1792 SourceRange getSourceRange() const LLVM_READONLY {
1793 return SourceRange(getFinallyLoc(), getEndLoc());
1796 SourceLocation getFinallyLoc() const { return Loc; }
1797 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1799 CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1801 child_range children() {
1802 return child_range(&Block,&Block+1);
1805 static bool classof(const Stmt *T) {
1806 return T->getStmtClass() == SEHFinallyStmtClass;
1811 class SEHTryStmt : public Stmt {
1813 SourceLocation TryLoc;
1816 enum { TRY = 0, HANDLER = 1 };
1818 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1819 SourceLocation TryLoc,
1823 friend class ASTReader;
1824 friend class ASTStmtReader;
1825 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1828 static SEHTryStmt* Create(ASTContext &C,
1830 SourceLocation TryLoc,
1834 SourceRange getSourceRange() const LLVM_READONLY {
1835 return SourceRange(getTryLoc(), getEndLoc());
1838 SourceLocation getTryLoc() const { return TryLoc; }
1839 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1841 bool getIsCXXTry() const { return IsCXXTry; }
1843 CompoundStmt* getTryBlock() const {
1844 return llvm::cast<CompoundStmt>(Children[TRY]);
1847 Stmt *getHandler() const { return Children[HANDLER]; }
1849 /// Returns 0 if not defined
1850 SEHExceptStmt *getExceptHandler() const;
1851 SEHFinallyStmt *getFinallyHandler() const;
1853 child_range children() {
1854 return child_range(Children,Children+2);
1857 static bool classof(const Stmt *T) {
1858 return T->getStmtClass() == SEHTryStmtClass;
1862 } // end namespace clang