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/AST/DeclGroup.h"
18 #include "clang/AST/PrettyPrinter.h"
19 #include "clang/AST/StmtIterator.h"
20 #include "clang/Basic/IdentifierTable.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "clang/Lex/Token.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/raw_ostream.h"
31 class FoldingSetNodeID;
48 //===--------------------------------------------------------------------===//
49 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
50 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
51 // references to children (to be compatible with StmtIterator).
52 //===--------------------------------------------------------------------===//
60 ExprIterator(Stmt** i) : I(i) {}
61 ExprIterator() : I(0) {}
62 ExprIterator& operator++() { ++I; return *this; }
63 ExprIterator operator-(size_t i) { return I-i; }
64 ExprIterator operator+(size_t i) { return I+i; }
65 Expr* operator[](size_t idx);
66 // FIXME: Verify that this will correctly return a signed distance.
67 signed operator-(const ExprIterator& R) const { return I - R.I; }
68 Expr* operator*() const;
69 Expr* operator->() const;
70 bool operator==(const ExprIterator& R) const { return I == R.I; }
71 bool operator!=(const ExprIterator& R) const { return I != R.I; }
72 bool operator>(const ExprIterator& R) const { return I > R.I; }
73 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
76 class ConstExprIterator {
77 const Stmt * const *I;
79 ConstExprIterator(const Stmt * const *i) : I(i) {}
80 ConstExprIterator() : I(0) {}
81 ConstExprIterator& operator++() { ++I; return *this; }
82 ConstExprIterator operator+(size_t i) const { return I+i; }
83 ConstExprIterator operator-(size_t i) const { return I-i; }
84 const Expr * operator[](size_t idx) const;
85 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
86 const Expr * operator*() const;
87 const Expr * operator->() const;
88 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
89 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
90 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
91 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
94 //===----------------------------------------------------------------------===//
95 // AST classes for statements.
96 //===----------------------------------------------------------------------===//
98 /// Stmt - This represents one statement.
104 #define STMT(CLASS, PARENT) CLASS##Class,
105 #define STMT_RANGE(BASE, FIRST, LAST) \
106 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
107 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
108 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
109 #define ABSTRACT_STMT(STMT)
110 #include "clang/AST/StmtNodes.inc"
113 // Make vanilla 'new' and 'delete' illegal for Stmts.
115 void* operator new(size_t bytes) throw() {
116 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
118 void operator delete(void* data) throw() {
119 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
122 class StmtBitfields {
125 /// \brief The statement class.
128 enum { NumStmtBits = 8 };
130 class CompoundStmtBitfields {
131 friend class CompoundStmt;
132 unsigned : NumStmtBits;
134 unsigned NumStmts : 32 - NumStmtBits;
137 class ExprBitfields {
139 friend class DeclRefExpr; // computeDependence
140 friend class InitListExpr; // ctor
141 friend class DesignatedInitExpr; // ctor
142 friend class BlockDeclRefExpr; // ctor
143 friend class ASTStmtReader; // deserialization
144 friend class CXXNewExpr; // ctor
145 friend class DependentScopeDeclRefExpr; // ctor
146 friend class CXXConstructExpr; // ctor
147 friend class CallExpr; // ctor
148 friend class OffsetOfExpr; // ctor
149 friend class ObjCMessageExpr; // ctor
150 friend class ObjCArrayLiteral; // ctor
151 friend class ObjCDictionaryLiteral; // ctor
152 friend class ShuffleVectorExpr; // ctor
153 friend class ParenListExpr; // ctor
154 friend class CXXUnresolvedConstructExpr; // ctor
155 friend class CXXDependentScopeMemberExpr; // ctor
156 friend class OverloadExpr; // ctor
157 friend class PseudoObjectExpr; // ctor
158 friend class AtomicExpr; // ctor
159 unsigned : NumStmtBits;
161 unsigned ValueKind : 2;
162 unsigned ObjectKind : 2;
163 unsigned TypeDependent : 1;
164 unsigned ValueDependent : 1;
165 unsigned InstantiationDependent : 1;
166 unsigned ContainsUnexpandedParameterPack : 1;
168 enum { NumExprBits = 16 };
170 class CharacterLiteralBitfields {
171 friend class CharacterLiteral;
172 unsigned : NumExprBits;
177 class FloatingLiteralBitfields {
178 friend class FloatingLiteral;
179 unsigned : NumExprBits;
181 unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
182 unsigned IsExact : 1;
185 class UnaryExprOrTypeTraitExprBitfields {
186 friend class UnaryExprOrTypeTraitExpr;
187 unsigned : NumExprBits;
190 unsigned IsType : 1; // true if operand is a type, false if an expression.
193 class DeclRefExprBitfields {
194 friend class DeclRefExpr;
195 friend class ASTStmtReader; // deserialization
196 unsigned : NumExprBits;
198 unsigned HasQualifier : 1;
199 unsigned HasTemplateKWAndArgsInfo : 1;
200 unsigned HasFoundDecl : 1;
201 unsigned HadMultipleCandidates : 1;
202 unsigned RefersToEnclosingLocal : 1;
205 class CastExprBitfields {
206 friend class CastExpr;
207 unsigned : NumExprBits;
210 unsigned BasePathSize : 32 - 6 - NumExprBits;
213 class CallExprBitfields {
214 friend class CallExpr;
215 unsigned : NumExprBits;
217 unsigned NumPreArgs : 1;
220 class ExprWithCleanupsBitfields {
221 friend class ExprWithCleanups;
222 friend class ASTStmtReader; // deserialization
224 unsigned : NumExprBits;
226 unsigned NumObjects : 32 - NumExprBits;
229 class PseudoObjectExprBitfields {
230 friend class PseudoObjectExpr;
231 friend class ASTStmtReader; // deserialization
233 unsigned : NumExprBits;
235 // These don't need to be particularly wide, because they're
236 // strictly limited by the forms of expressions we permit.
237 unsigned NumSubExprs : 8;
238 unsigned ResultIndex : 32 - 8 - NumExprBits;
241 class ObjCIndirectCopyRestoreExprBitfields {
242 friend class ObjCIndirectCopyRestoreExpr;
243 unsigned : NumExprBits;
245 unsigned ShouldCopy : 1;
248 class InitListExprBitfields {
249 friend class InitListExpr;
251 unsigned : NumExprBits;
253 /// Whether this initializer list originally had a GNU array-range
254 /// designator in it. This is a temporary marker used by CodeGen.
255 unsigned HadArrayRangeDesignator : 1;
257 /// Whether this initializer list initializes a std::initializer_list
259 unsigned InitializesStdInitializerList : 1;
262 class TypeTraitExprBitfields {
263 friend class TypeTraitExpr;
264 friend class ASTStmtReader;
265 friend class ASTStmtWriter;
267 unsigned : NumExprBits;
269 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
272 /// \brief If this expression is not value-dependent, this indicates whether
273 /// the trait evaluated true or false.
276 /// \brief The number of arguments to this type trait.
277 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
281 // FIXME: this is wasteful on 64-bit platforms.
284 StmtBitfields StmtBits;
285 CompoundStmtBitfields CompoundStmtBits;
286 ExprBitfields ExprBits;
287 CharacterLiteralBitfields CharacterLiteralBits;
288 FloatingLiteralBitfields FloatingLiteralBits;
289 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
290 DeclRefExprBitfields DeclRefExprBits;
291 CastExprBitfields CastExprBits;
292 CallExprBitfields CallExprBits;
293 ExprWithCleanupsBitfields ExprWithCleanupsBits;
294 PseudoObjectExprBitfields PseudoObjectExprBits;
295 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
296 InitListExprBitfields InitListExprBits;
297 TypeTraitExprBitfields TypeTraitExprBits;
300 friend class ASTStmtReader;
301 friend class ASTStmtWriter;
304 // Only allow allocation of Stmts using the allocator in ASTContext
305 // or by doing a placement new.
306 void* operator new(size_t bytes, ASTContext& C,
307 unsigned alignment = 8) throw();
309 void* operator new(size_t bytes, ASTContext* C,
310 unsigned alignment = 8) throw();
312 void* operator new(size_t bytes, void* mem) throw() {
316 void operator delete(void*, ASTContext&, unsigned) throw() { }
317 void operator delete(void*, ASTContext*, unsigned) throw() { }
318 void operator delete(void*, std::size_t) throw() { }
319 void operator delete(void*, void*) throw() { }
322 /// \brief A placeholder type used to construct an empty shell of a
323 /// type, that will be filled in later (e.g., by some
324 /// de-serialization).
325 struct EmptyShell { };
328 /// \brief Whether statistic collection is enabled.
329 static bool StatisticsEnabled;
332 /// \brief Construct an empty statement.
333 explicit Stmt(StmtClass SC, EmptyShell) {
334 StmtBits.sClass = SC;
335 if (StatisticsEnabled) Stmt::addStmtClass(SC);
340 StmtBits.sClass = SC;
341 if (StatisticsEnabled) Stmt::addStmtClass(SC);
344 StmtClass getStmtClass() const {
345 return static_cast<StmtClass>(StmtBits.sClass);
347 const char *getStmtClassName() const;
349 /// SourceLocation tokens are not useful in isolation - they are low level
350 /// value objects created/interpreted by SourceManager. We assume AST
351 /// clients will have a pointer to the respective SourceManager.
352 SourceRange getSourceRange() const LLVM_READONLY;
353 SourceLocation getLocStart() const LLVM_READONLY;
354 SourceLocation getLocEnd() const LLVM_READONLY;
356 // global temp stats (until we have a per-module visitor)
357 static void addStmtClass(const StmtClass s);
358 static void EnableStatistics();
359 static void PrintStats();
361 /// \brief Dumps the specified AST fragment and all subtrees to
363 LLVM_ATTRIBUTE_USED void dump() const;
364 LLVM_ATTRIBUTE_USED void dump(SourceManager &SM) const;
365 void dump(raw_ostream &OS, SourceManager &SM) const;
367 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
368 /// back to its original source language syntax.
369 void dumpPretty(ASTContext &Context) const;
370 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
371 const PrintingPolicy &Policy,
372 unsigned Indentation = 0) const;
374 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
375 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
376 void viewAST() const;
378 /// Skip past any implicit AST nodes which might surround this
379 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
380 Stmt *IgnoreImplicit();
382 const Stmt *stripLabelLikeStatements() const;
383 Stmt *stripLabelLikeStatements() {
384 return const_cast<Stmt*>(
385 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
388 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
389 /// contain implicit control-flow in the order their subexpressions
390 /// are evaluated. This predicate returns true if this statement has
391 /// such implicit control-flow. Such statements are also specially handled
393 bool hasImplicitControlFlow() const;
395 /// Child Iterators: All subclasses must implement 'children'
396 /// to permit easy iteration over the substatements/subexpessions of an
397 /// AST node. This permits easy iteration over all nodes in the AST.
398 typedef StmtIterator child_iterator;
399 typedef ConstStmtIterator const_child_iterator;
401 typedef StmtRange child_range;
402 typedef ConstStmtRange const_child_range;
404 child_range children();
405 const_child_range children() const {
406 return const_cast<Stmt*>(this)->children();
409 child_iterator child_begin() { return children().first; }
410 child_iterator child_end() { return children().second; }
412 const_child_iterator child_begin() const { return children().first; }
413 const_child_iterator child_end() const { return children().second; }
415 /// \brief Produce a unique representation of the given statement.
417 /// \param ID once the profiling operation is complete, will contain
418 /// the unique representation of the given statement.
420 /// \param Context the AST context in which the statement resides
422 /// \param Canonical whether the profile should be based on the canonical
423 /// representation of this statement (e.g., where non-type template
424 /// parameters are identified by index/level rather than their
425 /// declaration pointers) or the exact representation of the statement as
426 /// written in the source.
427 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
428 bool Canonical) const;
431 /// DeclStmt - Adaptor class for mixing declarations with statements and
432 /// expressions. For example, CompoundStmt mixes statements, expressions
433 /// and declarations (variables, types). Another example is ForStmt, where
434 /// the first statement can be an expression or a declaration.
436 class DeclStmt : public Stmt {
438 SourceLocation StartLoc, EndLoc;
441 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
442 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
443 StartLoc(startLoc), EndLoc(endLoc) {}
445 /// \brief Build an empty declaration statement.
446 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
448 /// isSingleDecl - This method returns true if this DeclStmt refers
449 /// to a single Decl.
450 bool isSingleDecl() const {
451 return DG.isSingleDecl();
454 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
455 Decl *getSingleDecl() { return DG.getSingleDecl(); }
457 const DeclGroupRef getDeclGroup() const { return DG; }
458 DeclGroupRef getDeclGroup() { return DG; }
459 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
461 SourceLocation getStartLoc() const { return StartLoc; }
462 void setStartLoc(SourceLocation L) { StartLoc = L; }
463 SourceLocation getEndLoc() const { return EndLoc; }
464 void setEndLoc(SourceLocation L) { EndLoc = L; }
466 SourceRange getSourceRange() const LLVM_READONLY {
467 return SourceRange(StartLoc, EndLoc);
470 static bool classof(const Stmt *T) {
471 return T->getStmtClass() == DeclStmtClass;
474 // Iterators over subexpressions.
475 child_range children() {
476 return child_range(child_iterator(DG.begin(), DG.end()),
477 child_iterator(DG.end(), DG.end()));
480 typedef DeclGroupRef::iterator decl_iterator;
481 typedef DeclGroupRef::const_iterator const_decl_iterator;
483 decl_iterator decl_begin() { return DG.begin(); }
484 decl_iterator decl_end() { return DG.end(); }
485 const_decl_iterator decl_begin() const { return DG.begin(); }
486 const_decl_iterator decl_end() const { return DG.end(); }
488 typedef std::reverse_iterator<decl_iterator> reverse_decl_iterator;
489 reverse_decl_iterator decl_rbegin() {
490 return reverse_decl_iterator(decl_end());
492 reverse_decl_iterator decl_rend() {
493 return reverse_decl_iterator(decl_begin());
497 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
499 class NullStmt : public Stmt {
500 SourceLocation SemiLoc;
502 /// \brief True if the null statement was preceded by an empty macro, e.g:
507 bool HasLeadingEmptyMacro;
509 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
510 : Stmt(NullStmtClass), SemiLoc(L),
511 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
513 /// \brief Build an empty null statement.
514 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
515 HasLeadingEmptyMacro(false) { }
517 SourceLocation getSemiLoc() const { return SemiLoc; }
518 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
520 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
522 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
524 static bool classof(const Stmt *T) {
525 return T->getStmtClass() == NullStmtClass;
528 child_range children() { return child_range(); }
530 friend class ASTStmtReader;
531 friend class ASTStmtWriter;
534 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
536 class CompoundStmt : public Stmt {
538 SourceLocation LBracLoc, RBracLoc;
540 CompoundStmt(ASTContext &C, Stmt **StmtStart, unsigned NumStmts,
541 SourceLocation LB, SourceLocation RB);
543 // \brief Build an empty compound statment with a location.
544 explicit CompoundStmt(SourceLocation Loc)
545 : Stmt(CompoundStmtClass), Body(0), LBracLoc(Loc), RBracLoc(Loc) {
546 CompoundStmtBits.NumStmts = 0;
549 // \brief Build an empty compound statement.
550 explicit CompoundStmt(EmptyShell Empty)
551 : Stmt(CompoundStmtClass, Empty), Body(0) {
552 CompoundStmtBits.NumStmts = 0;
555 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
557 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
558 unsigned size() const { return CompoundStmtBits.NumStmts; }
560 typedef Stmt** body_iterator;
561 body_iterator body_begin() { return Body; }
562 body_iterator body_end() { return Body + size(); }
563 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
565 void setLastStmt(Stmt *S) {
566 assert(!body_empty() && "setLastStmt");
570 typedef Stmt* const * const_body_iterator;
571 const_body_iterator body_begin() const { return Body; }
572 const_body_iterator body_end() const { return Body + size(); }
573 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
575 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
576 reverse_body_iterator body_rbegin() {
577 return reverse_body_iterator(body_end());
579 reverse_body_iterator body_rend() {
580 return reverse_body_iterator(body_begin());
583 typedef std::reverse_iterator<const_body_iterator>
584 const_reverse_body_iterator;
586 const_reverse_body_iterator body_rbegin() const {
587 return const_reverse_body_iterator(body_end());
590 const_reverse_body_iterator body_rend() const {
591 return const_reverse_body_iterator(body_begin());
594 SourceRange getSourceRange() const LLVM_READONLY {
595 return SourceRange(LBracLoc, RBracLoc);
598 SourceLocation getLBracLoc() const { return LBracLoc; }
599 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
600 SourceLocation getRBracLoc() const { return RBracLoc; }
601 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
603 static bool classof(const Stmt *T) {
604 return T->getStmtClass() == CompoundStmtClass;
608 child_range children() {
609 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
612 const_child_range children() const {
613 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
617 // SwitchCase is the base class for CaseStmt and DefaultStmt,
618 class SwitchCase : public Stmt {
620 // A pointer to the following CaseStmt or DefaultStmt class,
621 // used by SwitchStmt.
622 SwitchCase *NextSwitchCase;
624 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
627 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
629 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
631 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
634 const Stmt *getSubStmt() const {
635 return const_cast<SwitchCase*>(this)->getSubStmt();
638 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
640 static bool classof(const Stmt *T) {
641 return T->getStmtClass() == CaseStmtClass ||
642 T->getStmtClass() == DefaultStmtClass;
646 class CaseStmt : public SwitchCase {
647 enum { LHS, RHS, SUBSTMT, END_EXPR };
648 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
649 // GNU "case 1 ... 4" extension
650 SourceLocation CaseLoc;
651 SourceLocation EllipsisLoc;
652 SourceLocation ColonLoc;
654 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
655 SourceLocation ellipsisLoc, SourceLocation colonLoc)
656 : SwitchCase(CaseStmtClass) {
657 SubExprs[SUBSTMT] = 0;
658 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
659 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
661 EllipsisLoc = ellipsisLoc;
665 /// \brief Build an empty switch case statement.
666 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
668 SourceLocation getCaseLoc() const { return CaseLoc; }
669 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
670 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
671 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
672 SourceLocation getColonLoc() const { return ColonLoc; }
673 void setColonLoc(SourceLocation L) { ColonLoc = L; }
675 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
676 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
677 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
679 const Expr *getLHS() const {
680 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
682 const Expr *getRHS() const {
683 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
685 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
687 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
688 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
689 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
692 SourceRange getSourceRange() const LLVM_READONLY {
693 // Handle deeply nested case statements with iteration instead of recursion.
694 const CaseStmt *CS = this;
695 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
698 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
700 static bool classof(const Stmt *T) {
701 return T->getStmtClass() == CaseStmtClass;
705 child_range children() {
706 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
710 class DefaultStmt : public SwitchCase {
712 SourceLocation DefaultLoc;
713 SourceLocation ColonLoc;
715 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
716 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
719 /// \brief Build an empty default statement.
720 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
722 Stmt *getSubStmt() { return SubStmt; }
723 const Stmt *getSubStmt() const { return SubStmt; }
724 void setSubStmt(Stmt *S) { SubStmt = S; }
726 SourceLocation getDefaultLoc() const { return DefaultLoc; }
727 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
728 SourceLocation getColonLoc() const { return ColonLoc; }
729 void setColonLoc(SourceLocation L) { ColonLoc = L; }
731 SourceRange getSourceRange() const LLVM_READONLY {
732 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
734 static bool classof(const Stmt *T) {
735 return T->getStmtClass() == DefaultStmtClass;
739 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
743 /// LabelStmt - Represents a label, which has a substatement. For example:
746 class LabelStmt : public Stmt {
749 SourceLocation IdentLoc;
751 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
752 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
755 // \brief Build an empty label statement.
756 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
758 SourceLocation getIdentLoc() const { return IdentLoc; }
759 LabelDecl *getDecl() const { return TheDecl; }
760 void setDecl(LabelDecl *D) { TheDecl = D; }
761 const char *getName() const;
762 Stmt *getSubStmt() { return SubStmt; }
763 const Stmt *getSubStmt() const { return SubStmt; }
764 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
765 void setSubStmt(Stmt *SS) { SubStmt = SS; }
767 SourceRange getSourceRange() const LLVM_READONLY {
768 return SourceRange(IdentLoc, SubStmt->getLocEnd());
770 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
772 static bool classof(const Stmt *T) {
773 return T->getStmtClass() == LabelStmtClass;
778 /// \brief Represents an attribute applied to a statement.
780 /// Represents an attribute applied to a statement. For example:
781 /// [[omp::for(...)]] for (...) { ... }
783 class AttributedStmt : public Stmt {
785 SourceLocation AttrLoc;
787 const Attr *Attrs[1];
789 friend class ASTStmtReader;
791 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
792 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
793 NumAttrs(Attrs.size()) {
794 memcpy(this->Attrs, Attrs.data(), Attrs.size() * sizeof(Attr*));
797 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
798 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
799 memset(Attrs, 0, NumAttrs * sizeof(Attr*));
803 static AttributedStmt *Create(ASTContext &C, SourceLocation Loc,
804 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
805 // \brief Build an empty attributed statement.
806 static AttributedStmt *CreateEmpty(ASTContext &C, unsigned NumAttrs);
808 SourceLocation getAttrLoc() const { return AttrLoc; }
809 ArrayRef<const Attr*> getAttrs() const {
810 return ArrayRef<const Attr*>(Attrs, NumAttrs);
812 Stmt *getSubStmt() { return SubStmt; }
813 const Stmt *getSubStmt() const { return SubStmt; }
815 SourceRange getSourceRange() const LLVM_READONLY {
816 return SourceRange(AttrLoc, SubStmt->getLocEnd());
818 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
820 static bool classof(const Stmt *T) {
821 return T->getStmtClass() == AttributedStmtClass;
826 /// IfStmt - This represents an if/then/else.
828 class IfStmt : public Stmt {
829 enum { VAR, COND, THEN, ELSE, END_EXPR };
830 Stmt* SubExprs[END_EXPR];
832 SourceLocation IfLoc;
833 SourceLocation ElseLoc;
836 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
837 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
839 /// \brief Build an empty if/then/else statement
840 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
842 /// \brief Retrieve the variable declared in this "if" statement, if any.
844 /// In the following example, "x" is the condition variable.
846 /// if (int x = foo()) {
847 /// printf("x is %d", x);
850 VarDecl *getConditionVariable() const;
851 void setConditionVariable(ASTContext &C, VarDecl *V);
853 /// If this IfStmt has a condition variable, return the faux DeclStmt
854 /// associated with the creation of that condition variable.
855 const DeclStmt *getConditionVariableDeclStmt() const {
856 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
859 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
860 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
861 const Stmt *getThen() const { return SubExprs[THEN]; }
862 void setThen(Stmt *S) { SubExprs[THEN] = S; }
863 const Stmt *getElse() const { return SubExprs[ELSE]; }
864 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
866 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
867 Stmt *getThen() { return SubExprs[THEN]; }
868 Stmt *getElse() { return SubExprs[ELSE]; }
870 SourceLocation getIfLoc() const { return IfLoc; }
871 void setIfLoc(SourceLocation L) { IfLoc = L; }
872 SourceLocation getElseLoc() const { return ElseLoc; }
873 void setElseLoc(SourceLocation L) { ElseLoc = L; }
875 SourceRange getSourceRange() const LLVM_READONLY {
877 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
879 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
882 // Iterators over subexpressions. The iterators will include iterating
883 // over the initialization expression referenced by the condition variable.
884 child_range children() {
885 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
888 static bool classof(const Stmt *T) {
889 return T->getStmtClass() == IfStmtClass;
893 /// SwitchStmt - This represents a 'switch' stmt.
895 class SwitchStmt : public Stmt {
896 enum { VAR, COND, BODY, END_EXPR };
897 Stmt* SubExprs[END_EXPR];
898 // This points to a linked list of case and default statements.
899 SwitchCase *FirstCase;
900 SourceLocation SwitchLoc;
902 /// If the SwitchStmt is a switch on an enum value, this records whether
903 /// all the enum values were covered by CaseStmts. This value is meant to
904 /// be a hint for possible clients.
905 unsigned AllEnumCasesCovered : 1;
908 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
910 /// \brief Build a empty switch statement.
911 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
913 /// \brief Retrieve the variable declared in this "switch" statement, if any.
915 /// In the following example, "x" is the condition variable.
917 /// switch (int x = foo()) {
922 VarDecl *getConditionVariable() const;
923 void setConditionVariable(ASTContext &C, VarDecl *V);
925 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
926 /// associated with the creation of that condition variable.
927 const DeclStmt *getConditionVariableDeclStmt() const {
928 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
931 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
932 const Stmt *getBody() const { return SubExprs[BODY]; }
933 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
935 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
936 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
937 Stmt *getBody() { return SubExprs[BODY]; }
938 void setBody(Stmt *S) { SubExprs[BODY] = S; }
939 SwitchCase *getSwitchCaseList() { return FirstCase; }
941 /// \brief Set the case list for this switch statement.
943 /// The caller is responsible for incrementing the retain counts on
944 /// all of the SwitchCase statements in this list.
945 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
947 SourceLocation getSwitchLoc() const { return SwitchLoc; }
948 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
950 void setBody(Stmt *S, SourceLocation SL) {
954 void addSwitchCase(SwitchCase *SC) {
955 assert(!SC->getNextSwitchCase()
956 && "case/default already added to a switch");
957 SC->setNextSwitchCase(FirstCase);
961 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
962 /// switch over an enum value then all cases have been explicitly covered.
963 void setAllEnumCasesCovered() {
964 AllEnumCasesCovered = 1;
967 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
968 /// have been explicitly covered.
969 bool isAllEnumCasesCovered() const {
970 return (bool) AllEnumCasesCovered;
973 SourceRange getSourceRange() const LLVM_READONLY {
974 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
977 child_range children() {
978 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
981 static bool classof(const Stmt *T) {
982 return T->getStmtClass() == SwitchStmtClass;
987 /// WhileStmt - This represents a 'while' stmt.
989 class WhileStmt : public Stmt {
990 enum { VAR, COND, BODY, END_EXPR };
991 Stmt* SubExprs[END_EXPR];
992 SourceLocation WhileLoc;
994 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
997 /// \brief Build an empty while statement.
998 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
1000 /// \brief Retrieve the variable declared in this "while" statement, if any.
1002 /// In the following example, "x" is the condition variable.
1004 /// while (int x = random()) {
1008 VarDecl *getConditionVariable() const;
1009 void setConditionVariable(ASTContext &C, VarDecl *V);
1011 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1012 /// associated with the creation of that condition variable.
1013 const DeclStmt *getConditionVariableDeclStmt() const {
1014 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1017 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1018 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1019 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1020 Stmt *getBody() { return SubExprs[BODY]; }
1021 const Stmt *getBody() const { return SubExprs[BODY]; }
1022 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1024 SourceLocation getWhileLoc() const { return WhileLoc; }
1025 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1027 SourceRange getSourceRange() const LLVM_READONLY {
1028 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
1030 static bool classof(const Stmt *T) {
1031 return T->getStmtClass() == WhileStmtClass;
1035 child_range children() {
1036 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1040 /// DoStmt - This represents a 'do/while' stmt.
1042 class DoStmt : public Stmt {
1043 enum { BODY, COND, END_EXPR };
1044 Stmt* SubExprs[END_EXPR];
1045 SourceLocation DoLoc;
1046 SourceLocation WhileLoc;
1047 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1050 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1052 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1053 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1054 SubExprs[BODY] = body;
1057 /// \brief Build an empty do-while statement.
1058 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1060 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1061 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1062 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1063 Stmt *getBody() { return SubExprs[BODY]; }
1064 const Stmt *getBody() const { return SubExprs[BODY]; }
1065 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1067 SourceLocation getDoLoc() const { return DoLoc; }
1068 void setDoLoc(SourceLocation L) { DoLoc = L; }
1069 SourceLocation getWhileLoc() const { return WhileLoc; }
1070 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1072 SourceLocation getRParenLoc() const { return RParenLoc; }
1073 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1075 SourceRange getSourceRange() const LLVM_READONLY {
1076 return SourceRange(DoLoc, RParenLoc);
1078 static bool classof(const Stmt *T) {
1079 return T->getStmtClass() == DoStmtClass;
1083 child_range children() {
1084 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1089 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1090 /// the init/cond/inc parts of the ForStmt will be null if they were not
1091 /// specified in the source.
1093 class ForStmt : public Stmt {
1094 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1095 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1096 SourceLocation ForLoc;
1097 SourceLocation LParenLoc, RParenLoc;
1100 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1101 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1103 /// \brief Build an empty for statement.
1104 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1106 Stmt *getInit() { return SubExprs[INIT]; }
1108 /// \brief Retrieve the variable declared in this "for" statement, if any.
1110 /// In the following example, "y" is the condition variable.
1112 /// for (int x = random(); int y = mangle(x); ++x) {
1116 VarDecl *getConditionVariable() const;
1117 void setConditionVariable(ASTContext &C, VarDecl *V);
1119 /// If this ForStmt has a condition variable, return the faux DeclStmt
1120 /// associated with the creation of that condition variable.
1121 const DeclStmt *getConditionVariableDeclStmt() const {
1122 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1125 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1126 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1127 Stmt *getBody() { return SubExprs[BODY]; }
1129 const Stmt *getInit() const { return SubExprs[INIT]; }
1130 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1131 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1132 const Stmt *getBody() const { return SubExprs[BODY]; }
1134 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1135 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1136 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1137 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1139 SourceLocation getForLoc() const { return ForLoc; }
1140 void setForLoc(SourceLocation L) { ForLoc = L; }
1141 SourceLocation getLParenLoc() const { return LParenLoc; }
1142 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1143 SourceLocation getRParenLoc() const { return RParenLoc; }
1144 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1146 SourceRange getSourceRange() const LLVM_READONLY {
1147 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1149 static bool classof(const Stmt *T) {
1150 return T->getStmtClass() == ForStmtClass;
1154 child_range children() {
1155 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1159 /// GotoStmt - This represents a direct goto.
1161 class GotoStmt : public Stmt {
1163 SourceLocation GotoLoc;
1164 SourceLocation LabelLoc;
1166 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1167 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1169 /// \brief Build an empty goto statement.
1170 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1172 LabelDecl *getLabel() const { return Label; }
1173 void setLabel(LabelDecl *D) { Label = D; }
1175 SourceLocation getGotoLoc() const { return GotoLoc; }
1176 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1177 SourceLocation getLabelLoc() const { return LabelLoc; }
1178 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1180 SourceRange getSourceRange() const LLVM_READONLY {
1181 return SourceRange(GotoLoc, LabelLoc);
1183 static bool classof(const Stmt *T) {
1184 return T->getStmtClass() == GotoStmtClass;
1188 child_range children() { return child_range(); }
1191 /// IndirectGotoStmt - This represents an indirect goto.
1193 class IndirectGotoStmt : public Stmt {
1194 SourceLocation GotoLoc;
1195 SourceLocation StarLoc;
1198 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1200 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1201 Target((Stmt*)target) {}
1203 /// \brief Build an empty indirect goto statement.
1204 explicit IndirectGotoStmt(EmptyShell Empty)
1205 : Stmt(IndirectGotoStmtClass, Empty) { }
1207 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1208 SourceLocation getGotoLoc() const { return GotoLoc; }
1209 void setStarLoc(SourceLocation L) { StarLoc = L; }
1210 SourceLocation getStarLoc() const { return StarLoc; }
1212 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1213 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1214 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1216 /// getConstantTarget - Returns the fixed target of this indirect
1217 /// goto, if one exists.
1218 LabelDecl *getConstantTarget();
1219 const LabelDecl *getConstantTarget() const {
1220 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1223 SourceRange getSourceRange() const LLVM_READONLY {
1224 return SourceRange(GotoLoc, Target->getLocEnd());
1227 static bool classof(const Stmt *T) {
1228 return T->getStmtClass() == IndirectGotoStmtClass;
1232 child_range children() { return child_range(&Target, &Target+1); }
1236 /// ContinueStmt - This represents a continue.
1238 class ContinueStmt : public Stmt {
1239 SourceLocation ContinueLoc;
1241 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1243 /// \brief Build an empty continue statement.
1244 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1246 SourceLocation getContinueLoc() const { return ContinueLoc; }
1247 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1249 SourceRange getSourceRange() const LLVM_READONLY {
1250 return SourceRange(ContinueLoc);
1253 static bool classof(const Stmt *T) {
1254 return T->getStmtClass() == ContinueStmtClass;
1258 child_range children() { return child_range(); }
1261 /// BreakStmt - This represents a break.
1263 class BreakStmt : public Stmt {
1264 SourceLocation BreakLoc;
1266 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1268 /// \brief Build an empty break statement.
1269 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1271 SourceLocation getBreakLoc() const { return BreakLoc; }
1272 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1274 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
1276 static bool classof(const Stmt *T) {
1277 return T->getStmtClass() == BreakStmtClass;
1281 child_range children() { return child_range(); }
1285 /// ReturnStmt - This represents a return, optionally of an expression:
1289 /// Note that GCC allows return with no argument in a function declared to
1290 /// return a value, and it allows returning a value in functions declared to
1291 /// return void. We explicitly model this in the AST, which means you can't
1292 /// depend on the return type of the function and the presence of an argument.
1294 class ReturnStmt : public Stmt {
1296 SourceLocation RetLoc;
1297 const VarDecl *NRVOCandidate;
1300 ReturnStmt(SourceLocation RL)
1301 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1303 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1304 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1305 NRVOCandidate(NRVOCandidate) {}
1307 /// \brief Build an empty return expression.
1308 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1310 const Expr *getRetValue() const;
1311 Expr *getRetValue();
1312 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1314 SourceLocation getReturnLoc() const { return RetLoc; }
1315 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1317 /// \brief Retrieve the variable that might be used for the named return
1318 /// value optimization.
1320 /// The optimization itself can only be performed if the variable is
1321 /// also marked as an NRVO object.
1322 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1323 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1325 SourceRange getSourceRange() const LLVM_READONLY;
1327 static bool classof(const Stmt *T) {
1328 return T->getStmtClass() == ReturnStmtClass;
1332 child_range children() {
1333 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1334 return child_range();
1338 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1340 class AsmStmt : public Stmt {
1342 SourceLocation AsmLoc;
1343 /// \brief True if the assembly statement does not have any input or output
1347 /// \brief If true, treat this inline assembly as having side effects.
1348 /// This assembly statement should not be optimized, deleted or moved.
1351 unsigned NumOutputs;
1353 unsigned NumClobbers;
1355 IdentifierInfo **Names;
1358 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1359 unsigned numoutputs, unsigned numinputs, unsigned numclobbers) :
1360 Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1361 NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) { }
1364 /// \brief Build an empty inline-assembly statement.
1365 explicit AsmStmt(StmtClass SC, EmptyShell Empty) :
1366 Stmt(SC, Empty), Names(0), Exprs(0) { }
1368 SourceLocation getAsmLoc() const { return AsmLoc; }
1369 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1371 bool isSimple() const { return IsSimple; }
1372 void setSimple(bool V) { IsSimple = V; }
1374 bool isVolatile() const { return IsVolatile; }
1375 void setVolatile(bool V) { IsVolatile = V; }
1377 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
1379 //===--- Asm String Analysis ---===//
1381 /// Assemble final IR asm string.
1382 std::string generateAsmString(ASTContext &C) const;
1384 //===--- Output operands ---===//
1386 unsigned getNumOutputs() const { return NumOutputs; }
1388 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1392 StringRef getOutputName(unsigned i) const {
1393 if (IdentifierInfo *II = getOutputIdentifier(i))
1394 return II->getName();
1399 /// getOutputConstraint - Return the constraint string for the specified
1400 /// output operand. All output constraints are known to be non-empty (either
1402 StringRef getOutputConstraint(unsigned i) const;
1404 /// isOutputPlusConstraint - Return true if the specified output constraint
1405 /// is a "+" constraint (which is both an input and an output) or false if it
1406 /// is an "=" constraint (just an output).
1407 bool isOutputPlusConstraint(unsigned i) const {
1408 return getOutputConstraint(i)[0] == '+';
1411 const Expr *getOutputExpr(unsigned i) const;
1413 /// getNumPlusOperands - Return the number of output operands that have a "+"
1415 unsigned getNumPlusOperands() const;
1417 //===--- Input operands ---===//
1419 unsigned getNumInputs() const { return NumInputs; }
1421 IdentifierInfo *getInputIdentifier(unsigned i) const {
1422 return Names[i + NumOutputs];
1425 StringRef getInputName(unsigned i) const {
1426 if (IdentifierInfo *II = getInputIdentifier(i))
1427 return II->getName();
1432 /// getInputConstraint - Return the specified input constraint. Unlike output
1433 /// constraints, these can be empty.
1434 StringRef getInputConstraint(unsigned i) const;
1436 const Expr *getInputExpr(unsigned i) const;
1438 //===--- Other ---===//
1440 unsigned getNumClobbers() const { return NumClobbers; }
1441 StringRef getClobber(unsigned i) const;
1443 static bool classof(const Stmt *T) {
1444 return T->getStmtClass() == GCCAsmStmtClass ||
1445 T->getStmtClass() == MSAsmStmtClass;
1448 // Input expr iterators.
1450 typedef ExprIterator inputs_iterator;
1451 typedef ConstExprIterator const_inputs_iterator;
1453 inputs_iterator begin_inputs() {
1454 return &Exprs[0] + NumOutputs;
1457 inputs_iterator end_inputs() {
1458 return &Exprs[0] + NumOutputs + NumInputs;
1461 const_inputs_iterator begin_inputs() const {
1462 return &Exprs[0] + NumOutputs;
1465 const_inputs_iterator end_inputs() const {
1466 return &Exprs[0] + NumOutputs + NumInputs;
1469 // Output expr iterators.
1471 typedef ExprIterator outputs_iterator;
1472 typedef ConstExprIterator const_outputs_iterator;
1474 outputs_iterator begin_outputs() {
1477 outputs_iterator end_outputs() {
1478 return &Exprs[0] + NumOutputs;
1481 const_outputs_iterator begin_outputs() const {
1484 const_outputs_iterator end_outputs() const {
1485 return &Exprs[0] + NumOutputs;
1488 child_range children() {
1489 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1493 /// This represents a GCC inline-assembly statement extension.
1495 class GCCAsmStmt : public AsmStmt {
1496 SourceLocation RParenLoc;
1497 StringLiteral *AsmStr;
1499 // FIXME: If we wanted to, we could allocate all of these in one big array.
1500 StringLiteral **Constraints;
1501 StringLiteral **Clobbers;
1504 GCCAsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
1505 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1506 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1507 StringLiteral *asmstr, unsigned numclobbers,
1508 StringLiteral **clobbers, SourceLocation rparenloc);
1510 /// \brief Build an empty inline-assembly statement.
1511 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty),
1512 Constraints(0), Clobbers(0) { }
1514 SourceLocation getRParenLoc() const { return RParenLoc; }
1515 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1517 //===--- Asm String Analysis ---===//
1519 const StringLiteral *getAsmString() const { return AsmStr; }
1520 StringLiteral *getAsmString() { return AsmStr; }
1521 void setAsmString(StringLiteral *E) { AsmStr = E; }
1523 /// AsmStringPiece - this is part of a decomposed asm string specification
1524 /// (for use with the AnalyzeAsmString function below). An asm string is
1525 /// considered to be a concatenation of these parts.
1526 class AsmStringPiece {
1529 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1530 Operand // Operand reference, with optional modifier %c4.
1537 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1538 AsmStringPiece(unsigned OpNo, char Modifier)
1539 : MyKind(Operand), Str(), OperandNo(OpNo) {
1543 bool isString() const { return MyKind == String; }
1544 bool isOperand() const { return MyKind == Operand; }
1546 const std::string &getString() const {
1551 unsigned getOperandNo() const {
1552 assert(isOperand());
1556 /// getModifier - Get the modifier for this operand, if present. This
1557 /// returns '\0' if there was no modifier.
1558 char getModifier() const {
1559 assert(isOperand());
1564 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1565 /// it into pieces. If the asm string is erroneous, emit errors and return
1566 /// true, otherwise return false. This handles canonicalization and
1567 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1568 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1569 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1570 ASTContext &C, unsigned &DiagOffs) const;
1572 /// Assemble final IR asm string.
1573 std::string generateAsmString(ASTContext &C) const;
1575 //===--- Output operands ---===//
1577 StringRef getOutputConstraint(unsigned i) const;
1579 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1580 return Constraints[i];
1582 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1583 return Constraints[i];
1586 Expr *getOutputExpr(unsigned i);
1588 const Expr *getOutputExpr(unsigned i) const {
1589 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1592 //===--- Input operands ---===//
1594 StringRef getInputConstraint(unsigned i) const;
1596 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1597 return Constraints[i + NumOutputs];
1599 StringLiteral *getInputConstraintLiteral(unsigned i) {
1600 return Constraints[i + NumOutputs];
1603 Expr *getInputExpr(unsigned i);
1604 void setInputExpr(unsigned i, Expr *E);
1606 const Expr *getInputExpr(unsigned i) const {
1607 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1610 void setOutputsAndInputsAndClobbers(ASTContext &C,
1611 IdentifierInfo **Names,
1612 StringLiteral **Constraints,
1614 unsigned NumOutputs,
1616 StringLiteral **Clobbers,
1617 unsigned NumClobbers);
1619 //===--- Other ---===//
1621 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1622 /// translate this into a numeric value needed to reference the same operand.
1623 /// This returns -1 if the operand name is invalid.
1624 int getNamedOperand(StringRef SymbolicName) const;
1626 StringRef getClobber(unsigned i) const;
1627 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1628 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1632 SourceRange getSourceRange() const LLVM_READONLY {
1633 return SourceRange(AsmLoc, RParenLoc);
1636 static bool classof(const Stmt *T) {
1637 return T->getStmtClass() == GCCAsmStmtClass;
1641 /// This represents a Microsoft inline-assembly statement extension.
1643 class MSAsmStmt : public AsmStmt {
1644 SourceLocation AsmLoc, LBraceLoc, EndLoc;
1647 unsigned NumAsmToks;
1650 StringRef *Constraints;
1651 StringRef *Clobbers;
1654 MSAsmStmt(ASTContext &C, SourceLocation asmloc, SourceLocation lbraceloc,
1655 bool issimple, bool isvolatile, ArrayRef<Token> asmtoks,
1656 unsigned numoutputs, unsigned numinputs,
1657 ArrayRef<IdentifierInfo*> names, ArrayRef<StringRef> constraints,
1658 ArrayRef<Expr*> exprs, StringRef asmstr,
1659 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1661 /// \brief Build an empty MS-style inline-assembly statement.
1662 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty),
1663 NumAsmToks(0), AsmToks(0), Constraints(0), Clobbers(0) { }
1665 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1666 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1667 SourceLocation getEndLoc() const { return EndLoc; }
1668 void setEndLoc(SourceLocation L) { EndLoc = L; }
1670 bool hasBraces() const { return LBraceLoc.isValid(); }
1672 unsigned getNumAsmToks() { return NumAsmToks; }
1673 Token *getAsmToks() { return AsmToks; }
1675 //===--- Asm String Analysis ---===//
1677 const std::string *getAsmString() const { return &AsmStr; }
1678 std::string *getAsmString() { return &AsmStr; }
1679 void setAsmString(StringRef &E) { AsmStr = E.str(); }
1681 /// Assemble final IR asm string.
1682 std::string generateAsmString(ASTContext &C) const;
1684 //===--- Output operands ---===//
1686 StringRef getOutputConstraint(unsigned i) const {
1687 return Constraints[i];
1690 Expr *getOutputExpr(unsigned i);
1692 const Expr *getOutputExpr(unsigned i) const {
1693 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1696 //===--- Input operands ---===//
1698 StringRef getInputConstraint(unsigned i) const {
1699 return Constraints[i + NumOutputs];
1702 Expr *getInputExpr(unsigned i);
1703 void setInputExpr(unsigned i, Expr *E);
1705 const Expr *getInputExpr(unsigned i) const {
1706 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1709 //===--- Other ---===//
1711 StringRef getClobber(unsigned i) const { return Clobbers[i]; }
1713 SourceRange getSourceRange() const LLVM_READONLY {
1714 return SourceRange(AsmLoc, EndLoc);
1716 static bool classof(const Stmt *T) {
1717 return T->getStmtClass() == MSAsmStmtClass;
1720 child_range children() {
1721 return child_range(&Exprs[0], &Exprs[0]);
1725 class SEHExceptStmt : public Stmt {
1729 enum { FILTER_EXPR, BLOCK };
1731 SEHExceptStmt(SourceLocation Loc,
1735 friend class ASTReader;
1736 friend class ASTStmtReader;
1737 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1740 static SEHExceptStmt* Create(ASTContext &C,
1741 SourceLocation ExceptLoc,
1744 SourceRange getSourceRange() const LLVM_READONLY {
1745 return SourceRange(getExceptLoc(), getEndLoc());
1748 SourceLocation getExceptLoc() const { return Loc; }
1749 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1751 Expr *getFilterExpr() const {
1752 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1755 CompoundStmt *getBlock() const {
1756 return llvm::cast<CompoundStmt>(Children[BLOCK]);
1759 child_range children() {
1760 return child_range(Children,Children+2);
1763 static bool classof(const Stmt *T) {
1764 return T->getStmtClass() == SEHExceptStmtClass;
1769 class SEHFinallyStmt : public Stmt {
1773 SEHFinallyStmt(SourceLocation Loc,
1776 friend class ASTReader;
1777 friend class ASTStmtReader;
1778 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1781 static SEHFinallyStmt* Create(ASTContext &C,
1782 SourceLocation FinallyLoc,
1785 SourceRange getSourceRange() const LLVM_READONLY {
1786 return SourceRange(getFinallyLoc(), getEndLoc());
1789 SourceLocation getFinallyLoc() const { return Loc; }
1790 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1792 CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1794 child_range children() {
1795 return child_range(&Block,&Block+1);
1798 static bool classof(const Stmt *T) {
1799 return T->getStmtClass() == SEHFinallyStmtClass;
1804 class SEHTryStmt : public Stmt {
1806 SourceLocation TryLoc;
1809 enum { TRY = 0, HANDLER = 1 };
1811 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1812 SourceLocation TryLoc,
1816 friend class ASTReader;
1817 friend class ASTStmtReader;
1818 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1821 static SEHTryStmt* Create(ASTContext &C,
1823 SourceLocation TryLoc,
1827 SourceRange getSourceRange() const LLVM_READONLY {
1828 return SourceRange(getTryLoc(), getEndLoc());
1831 SourceLocation getTryLoc() const { return TryLoc; }
1832 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1834 bool getIsCXXTry() const { return IsCXXTry; }
1836 CompoundStmt* getTryBlock() const {
1837 return llvm::cast<CompoundStmt>(Children[TRY]);
1840 Stmt *getHandler() const { return Children[HANDLER]; }
1842 /// Returns 0 if not defined
1843 SEHExceptStmt *getExceptHandler() const;
1844 SEHFinallyStmt *getFinallyHandler() const;
1846 child_range children() {
1847 return child_range(Children,Children+2);
1850 static bool classof(const Stmt *T) {
1851 return T->getStmtClass() == SEHTryStmtClass;
1855 } // end namespace clang