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/ASTContext.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/raw_ostream.h"
29 class FoldingSetNodeID;
43 //===--------------------------------------------------------------------===//
44 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
45 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
46 // references to children (to be compatible with StmtIterator).
47 //===--------------------------------------------------------------------===//
55 ExprIterator(Stmt** i) : I(i) {}
56 ExprIterator() : I(0) {}
57 ExprIterator& operator++() { ++I; return *this; }
58 ExprIterator operator-(size_t i) { return I-i; }
59 ExprIterator operator+(size_t i) { return I+i; }
60 Expr* operator[](size_t idx);
61 // FIXME: Verify that this will correctly return a signed distance.
62 signed operator-(const ExprIterator& R) const { return I - R.I; }
63 Expr* operator*() const;
64 Expr* operator->() const;
65 bool operator==(const ExprIterator& R) const { return I == R.I; }
66 bool operator!=(const ExprIterator& R) const { return I != R.I; }
67 bool operator>(const ExprIterator& R) const { return I > R.I; }
68 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
71 class ConstExprIterator {
72 const Stmt * const *I;
74 ConstExprIterator(const Stmt * const *i) : I(i) {}
75 ConstExprIterator() : I(0) {}
76 ConstExprIterator& operator++() { ++I; return *this; }
77 ConstExprIterator operator+(size_t i) const { return I+i; }
78 ConstExprIterator operator-(size_t i) const { return I-i; }
79 const Expr * operator[](size_t idx) const;
80 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
81 const Expr * operator*() const;
82 const Expr * operator->() const;
83 bool operator==(const ConstExprIterator& R) const { return I == R.I; }
84 bool operator!=(const ConstExprIterator& R) const { return I != R.I; }
85 bool operator>(const ConstExprIterator& R) const { return I > R.I; }
86 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
89 //===----------------------------------------------------------------------===//
90 // AST classes for statements.
91 //===----------------------------------------------------------------------===//
93 /// Stmt - This represents one statement.
99 #define STMT(CLASS, PARENT) CLASS##Class,
100 #define STMT_RANGE(BASE, FIRST, LAST) \
101 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
102 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
103 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
104 #define ABSTRACT_STMT(STMT)
105 #include "clang/AST/StmtNodes.inc"
108 // Make vanilla 'new' and 'delete' illegal for Stmts.
110 void* operator new(size_t bytes) throw() {
111 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
113 void operator delete(void* data) throw() {
114 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
117 class StmtBitfields {
120 /// \brief The statement class.
123 enum { NumStmtBits = 8 };
125 class CompoundStmtBitfields {
126 friend class CompoundStmt;
127 unsigned : NumStmtBits;
129 unsigned NumStmts : 32 - NumStmtBits;
132 class ExprBitfields {
134 friend class DeclRefExpr; // computeDependence
135 friend class InitListExpr; // ctor
136 friend class DesignatedInitExpr; // ctor
137 friend class BlockDeclRefExpr; // ctor
138 friend class ASTStmtReader; // deserialization
139 friend class CXXNewExpr; // ctor
140 friend class DependentScopeDeclRefExpr; // ctor
141 friend class CXXConstructExpr; // ctor
142 friend class CallExpr; // ctor
143 friend class OffsetOfExpr; // ctor
144 friend class ObjCMessageExpr; // ctor
145 friend class ObjCArrayLiteral; // ctor
146 friend class ObjCDictionaryLiteral; // ctor
147 friend class ShuffleVectorExpr; // ctor
148 friend class ParenListExpr; // ctor
149 friend class CXXUnresolvedConstructExpr; // ctor
150 friend class CXXDependentScopeMemberExpr; // ctor
151 friend class OverloadExpr; // ctor
152 friend class PseudoObjectExpr; // ctor
153 friend class AtomicExpr; // ctor
154 unsigned : NumStmtBits;
156 unsigned ValueKind : 2;
157 unsigned ObjectKind : 2;
158 unsigned TypeDependent : 1;
159 unsigned ValueDependent : 1;
160 unsigned InstantiationDependent : 1;
161 unsigned ContainsUnexpandedParameterPack : 1;
163 enum { NumExprBits = 16 };
165 class CharacterLiteralBitfields {
166 friend class CharacterLiteral;
167 unsigned : NumExprBits;
172 class FloatingLiteralBitfields {
173 friend class FloatingLiteral;
174 unsigned : NumExprBits;
176 unsigned IsIEEE : 1; // Distinguishes between PPC128 and IEEE128.
177 unsigned IsExact : 1;
180 class UnaryExprOrTypeTraitExprBitfields {
181 friend class UnaryExprOrTypeTraitExpr;
182 unsigned : NumExprBits;
185 unsigned IsType : 1; // true if operand is a type, false if an expression.
188 class DeclRefExprBitfields {
189 friend class DeclRefExpr;
190 friend class ASTStmtReader; // deserialization
191 unsigned : NumExprBits;
193 unsigned HasQualifier : 1;
194 unsigned HasTemplateKWAndArgsInfo : 1;
195 unsigned HasFoundDecl : 1;
196 unsigned HadMultipleCandidates : 1;
197 unsigned RefersToEnclosingLocal : 1;
200 class CastExprBitfields {
201 friend class CastExpr;
202 unsigned : NumExprBits;
205 unsigned BasePathSize : 32 - 6 - NumExprBits;
208 class CallExprBitfields {
209 friend class CallExpr;
210 unsigned : NumExprBits;
212 unsigned NumPreArgs : 1;
215 class ExprWithCleanupsBitfields {
216 friend class ExprWithCleanups;
217 friend class ASTStmtReader; // deserialization
219 unsigned : NumExprBits;
221 unsigned NumObjects : 32 - NumExprBits;
224 class PseudoObjectExprBitfields {
225 friend class PseudoObjectExpr;
226 friend class ASTStmtReader; // deserialization
228 unsigned : NumExprBits;
230 // These don't need to be particularly wide, because they're
231 // strictly limited by the forms of expressions we permit.
232 unsigned NumSubExprs : 8;
233 unsigned ResultIndex : 32 - 8 - NumExprBits;
236 class ObjCIndirectCopyRestoreExprBitfields {
237 friend class ObjCIndirectCopyRestoreExpr;
238 unsigned : NumExprBits;
240 unsigned ShouldCopy : 1;
243 class InitListExprBitfields {
244 friend class InitListExpr;
246 unsigned : NumExprBits;
248 /// Whether this initializer list originally had a GNU array-range
249 /// designator in it. This is a temporary marker used by CodeGen.
250 unsigned HadArrayRangeDesignator : 1;
252 /// Whether this initializer list initializes a std::initializer_list
254 unsigned InitializesStdInitializerList : 1;
257 class TypeTraitExprBitfields {
258 friend class TypeTraitExpr;
259 friend class ASTStmtReader;
260 friend class ASTStmtWriter;
262 unsigned : NumExprBits;
264 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
267 /// \brief If this expression is not value-dependent, this indicates whether
268 /// the trait evaluated true or false.
271 /// \brief The number of arguments to this type trait.
272 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
276 // FIXME: this is wasteful on 64-bit platforms.
279 StmtBitfields StmtBits;
280 CompoundStmtBitfields CompoundStmtBits;
281 ExprBitfields ExprBits;
282 CharacterLiteralBitfields CharacterLiteralBits;
283 FloatingLiteralBitfields FloatingLiteralBits;
284 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
285 DeclRefExprBitfields DeclRefExprBits;
286 CastExprBitfields CastExprBits;
287 CallExprBitfields CallExprBits;
288 ExprWithCleanupsBitfields ExprWithCleanupsBits;
289 PseudoObjectExprBitfields PseudoObjectExprBits;
290 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
291 InitListExprBitfields InitListExprBits;
292 TypeTraitExprBitfields TypeTraitExprBits;
295 friend class ASTStmtReader;
296 friend class ASTStmtWriter;
299 // Only allow allocation of Stmts using the allocator in ASTContext
300 // or by doing a placement new.
301 void* operator new(size_t bytes, ASTContext& C,
302 unsigned alignment = 8) throw() {
303 return ::operator new(bytes, C, alignment);
306 void* operator new(size_t bytes, ASTContext* C,
307 unsigned alignment = 8) throw() {
308 return ::operator new(bytes, *C, alignment);
311 void* operator new(size_t bytes, void* mem) throw() {
315 void operator delete(void*, ASTContext&, unsigned) throw() { }
316 void operator delete(void*, ASTContext*, unsigned) throw() { }
317 void operator delete(void*, std::size_t) throw() { }
318 void operator delete(void*, void*) throw() { }
321 /// \brief A placeholder type used to construct an empty shell of a
322 /// type, that will be filled in later (e.g., by some
323 /// de-serialization).
324 struct EmptyShell { };
327 /// \brief Whether statistic collection is enabled.
328 static bool StatisticsEnabled;
331 /// \brief Construct an empty statement.
332 explicit Stmt(StmtClass SC, EmptyShell) {
333 StmtBits.sClass = SC;
334 if (StatisticsEnabled) Stmt::addStmtClass(SC);
339 StmtBits.sClass = SC;
340 if (StatisticsEnabled) Stmt::addStmtClass(SC);
343 StmtClass getStmtClass() const {
344 return static_cast<StmtClass>(StmtBits.sClass);
346 const char *getStmtClassName() const;
348 /// SourceLocation tokens are not useful in isolation - they are low level
349 /// value objects created/interpreted by SourceManager. We assume AST
350 /// clients will have a pointer to the respective SourceManager.
351 SourceRange getSourceRange() const LLVM_READONLY;
352 SourceLocation getLocStart() const LLVM_READONLY;
353 SourceLocation getLocEnd() const LLVM_READONLY;
355 // global temp stats (until we have a per-module visitor)
356 static void addStmtClass(const StmtClass s);
357 static void EnableStatistics();
358 static void PrintStats();
360 /// dump - This does a local dump of the specified AST fragment. It dumps the
361 /// specified node and a few nodes underneath it, but not the whole subtree.
362 /// This is useful in a debugger.
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 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
368 void dumpAll() const;
369 void dumpAll(SourceManager &SM) const;
371 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
372 /// back to its original source language syntax.
373 void dumpPretty(ASTContext& Context) const;
374 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
375 const PrintingPolicy &Policy,
376 unsigned Indentation = 0) const {
377 printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
379 void printPretty(raw_ostream &OS, ASTContext &Context,
380 PrinterHelper *Helper,
381 const PrintingPolicy &Policy,
382 unsigned Indentation = 0) const;
384 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
385 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
386 void viewAST() const;
388 /// Skip past any implicit AST nodes which might surround this
389 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
390 Stmt *IgnoreImplicit();
392 const Stmt *stripLabelLikeStatements() const;
393 Stmt *stripLabelLikeStatements() {
394 return const_cast<Stmt*>(
395 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
398 // Implement isa<T> support.
399 static bool classof(const Stmt *) { return true; }
401 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
402 /// contain implicit control-flow in the order their subexpressions
403 /// are evaluated. This predicate returns true if this statement has
404 /// such implicit control-flow. Such statements are also specially handled
406 bool hasImplicitControlFlow() const;
408 /// Child Iterators: All subclasses must implement 'children'
409 /// to permit easy iteration over the substatements/subexpessions of an
410 /// AST node. This permits easy iteration over all nodes in the AST.
411 typedef StmtIterator child_iterator;
412 typedef ConstStmtIterator const_child_iterator;
414 typedef StmtRange child_range;
415 typedef ConstStmtRange const_child_range;
417 child_range children();
418 const_child_range children() const {
419 return const_cast<Stmt*>(this)->children();
422 child_iterator child_begin() { return children().first; }
423 child_iterator child_end() { return children().second; }
425 const_child_iterator child_begin() const { return children().first; }
426 const_child_iterator child_end() const { return children().second; }
428 /// \brief Produce a unique representation of the given statement.
430 /// \brief ID once the profiling operation is complete, will contain
431 /// the unique representation of the given statement.
433 /// \brief Context the AST context in which the statement resides
435 /// \brief Canonical whether the profile should be based on the canonical
436 /// representation of this statement (e.g., where non-type template
437 /// parameters are identified by index/level rather than their
438 /// declaration pointers) or the exact representation of the statement as
439 /// written in the source.
440 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
441 bool Canonical) const;
444 /// DeclStmt - Adaptor class for mixing declarations with statements and
445 /// expressions. For example, CompoundStmt mixes statements, expressions
446 /// and declarations (variables, types). Another example is ForStmt, where
447 /// the first statement can be an expression or a declaration.
449 class DeclStmt : public Stmt {
451 SourceLocation StartLoc, EndLoc;
454 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
455 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
456 StartLoc(startLoc), EndLoc(endLoc) {}
458 /// \brief Build an empty declaration statement.
459 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
461 /// isSingleDecl - This method returns true if this DeclStmt refers
462 /// to a single Decl.
463 bool isSingleDecl() const {
464 return DG.isSingleDecl();
467 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
468 Decl *getSingleDecl() { return DG.getSingleDecl(); }
470 const DeclGroupRef getDeclGroup() const { return DG; }
471 DeclGroupRef getDeclGroup() { return DG; }
472 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
474 SourceLocation getStartLoc() const { return StartLoc; }
475 void setStartLoc(SourceLocation L) { StartLoc = L; }
476 SourceLocation getEndLoc() const { return EndLoc; }
477 void setEndLoc(SourceLocation L) { EndLoc = L; }
479 SourceRange getSourceRange() const LLVM_READONLY {
480 return SourceRange(StartLoc, EndLoc);
483 static bool classof(const Stmt *T) {
484 return T->getStmtClass() == DeclStmtClass;
486 static bool classof(const DeclStmt *) { return true; }
488 // Iterators over subexpressions.
489 child_range children() {
490 return child_range(child_iterator(DG.begin(), DG.end()),
491 child_iterator(DG.end(), DG.end()));
494 typedef DeclGroupRef::iterator decl_iterator;
495 typedef DeclGroupRef::const_iterator const_decl_iterator;
497 decl_iterator decl_begin() { return DG.begin(); }
498 decl_iterator decl_end() { return DG.end(); }
499 const_decl_iterator decl_begin() const { return DG.begin(); }
500 const_decl_iterator decl_end() const { return DG.end(); }
503 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
505 class NullStmt : public Stmt {
506 SourceLocation SemiLoc;
508 /// \brief True if the null statement was preceded by an empty macro, e.g:
513 bool HasLeadingEmptyMacro;
515 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
516 : Stmt(NullStmtClass), SemiLoc(L),
517 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
519 /// \brief Build an empty null statement.
520 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty),
521 HasLeadingEmptyMacro(false) { }
523 SourceLocation getSemiLoc() const { return SemiLoc; }
524 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
526 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
528 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(SemiLoc); }
530 static bool classof(const Stmt *T) {
531 return T->getStmtClass() == NullStmtClass;
533 static bool classof(const NullStmt *) { return true; }
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)
549 : Stmt(CompoundStmtClass), LBracLoc(LB), RBracLoc(RB) {
550 CompoundStmtBits.NumStmts = NumStmts;
551 assert(CompoundStmtBits.NumStmts == NumStmts &&
552 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
559 Body = new (C) Stmt*[NumStmts];
560 memcpy(Body, StmtStart, NumStmts * sizeof(*Body));
563 // \brief Build an empty compound statement.
564 explicit CompoundStmt(EmptyShell Empty)
565 : Stmt(CompoundStmtClass, Empty), Body(0) {
566 CompoundStmtBits.NumStmts = 0;
569 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
571 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
572 unsigned size() const { return CompoundStmtBits.NumStmts; }
574 typedef Stmt** body_iterator;
575 body_iterator body_begin() { return Body; }
576 body_iterator body_end() { return Body + size(); }
577 Stmt *body_back() { return !body_empty() ? Body[size()-1] : 0; }
579 void setLastStmt(Stmt *S) {
580 assert(!body_empty() && "setLastStmt");
584 typedef Stmt* const * const_body_iterator;
585 const_body_iterator body_begin() const { return Body; }
586 const_body_iterator body_end() const { return Body + size(); }
587 const Stmt *body_back() const { return !body_empty() ? Body[size()-1] : 0; }
589 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
590 reverse_body_iterator body_rbegin() {
591 return reverse_body_iterator(body_end());
593 reverse_body_iterator body_rend() {
594 return reverse_body_iterator(body_begin());
597 typedef std::reverse_iterator<const_body_iterator>
598 const_reverse_body_iterator;
600 const_reverse_body_iterator body_rbegin() const {
601 return const_reverse_body_iterator(body_end());
604 const_reverse_body_iterator body_rend() const {
605 return const_reverse_body_iterator(body_begin());
608 SourceRange getSourceRange() const LLVM_READONLY {
609 return SourceRange(LBracLoc, RBracLoc);
612 SourceLocation getLBracLoc() const { return LBracLoc; }
613 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
614 SourceLocation getRBracLoc() const { return RBracLoc; }
615 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
617 static bool classof(const Stmt *T) {
618 return T->getStmtClass() == CompoundStmtClass;
620 static bool classof(const CompoundStmt *) { return true; }
623 child_range children() {
624 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
627 const_child_range children() const {
628 return child_range(&Body[0], &Body[0]+CompoundStmtBits.NumStmts);
632 // SwitchCase is the base class for CaseStmt and DefaultStmt,
633 class SwitchCase : public Stmt {
635 // A pointer to the following CaseStmt or DefaultStmt class,
636 // used by SwitchStmt.
637 SwitchCase *NextSwitchCase;
639 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
642 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
644 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
646 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
649 const Stmt *getSubStmt() const {
650 return const_cast<SwitchCase*>(this)->getSubStmt();
653 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(); }
655 static bool classof(const Stmt *T) {
656 return T->getStmtClass() == CaseStmtClass ||
657 T->getStmtClass() == DefaultStmtClass;
659 static bool classof(const SwitchCase *) { return true; }
662 class CaseStmt : public SwitchCase {
663 enum { LHS, RHS, SUBSTMT, END_EXPR };
664 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
665 // GNU "case 1 ... 4" extension
666 SourceLocation CaseLoc;
667 SourceLocation EllipsisLoc;
668 SourceLocation ColonLoc;
670 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
671 SourceLocation ellipsisLoc, SourceLocation colonLoc)
672 : SwitchCase(CaseStmtClass) {
673 SubExprs[SUBSTMT] = 0;
674 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
675 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
677 EllipsisLoc = ellipsisLoc;
681 /// \brief Build an empty switch case statement.
682 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
684 SourceLocation getCaseLoc() const { return CaseLoc; }
685 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
686 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
687 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
688 SourceLocation getColonLoc() const { return ColonLoc; }
689 void setColonLoc(SourceLocation L) { ColonLoc = L; }
691 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
692 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
693 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
695 const Expr *getLHS() const {
696 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
698 const Expr *getRHS() const {
699 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
701 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
703 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
704 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
705 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
708 SourceRange getSourceRange() const LLVM_READONLY {
709 // Handle deeply nested case statements with iteration instead of recursion.
710 const CaseStmt *CS = this;
711 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
714 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
716 static bool classof(const Stmt *T) {
717 return T->getStmtClass() == CaseStmtClass;
719 static bool classof(const CaseStmt *) { return true; }
722 child_range children() {
723 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
727 class DefaultStmt : public SwitchCase {
729 SourceLocation DefaultLoc;
730 SourceLocation ColonLoc;
732 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
733 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
736 /// \brief Build an empty default statement.
737 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
739 Stmt *getSubStmt() { return SubStmt; }
740 const Stmt *getSubStmt() const { return SubStmt; }
741 void setSubStmt(Stmt *S) { SubStmt = S; }
743 SourceLocation getDefaultLoc() const { return DefaultLoc; }
744 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
745 SourceLocation getColonLoc() const { return ColonLoc; }
746 void setColonLoc(SourceLocation L) { ColonLoc = L; }
748 SourceRange getSourceRange() const LLVM_READONLY {
749 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
751 static bool classof(const Stmt *T) {
752 return T->getStmtClass() == DefaultStmtClass;
754 static bool classof(const DefaultStmt *) { return true; }
757 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
761 /// LabelStmt - Represents a label, which has a substatement. For example:
764 class LabelStmt : public Stmt {
767 SourceLocation IdentLoc;
769 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
770 : Stmt(LabelStmtClass), TheDecl(D), SubStmt(substmt), IdentLoc(IL) {
773 // \brief Build an empty label statement.
774 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
776 SourceLocation getIdentLoc() const { return IdentLoc; }
777 LabelDecl *getDecl() const { return TheDecl; }
778 void setDecl(LabelDecl *D) { TheDecl = D; }
779 const char *getName() const;
780 Stmt *getSubStmt() { return SubStmt; }
781 const Stmt *getSubStmt() const { return SubStmt; }
782 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
783 void setSubStmt(Stmt *SS) { SubStmt = SS; }
785 SourceRange getSourceRange() const LLVM_READONLY {
786 return SourceRange(IdentLoc, SubStmt->getLocEnd());
788 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
790 static bool classof(const Stmt *T) {
791 return T->getStmtClass() == LabelStmtClass;
793 static bool classof(const LabelStmt *) { return true; }
797 /// IfStmt - This represents an if/then/else.
799 class IfStmt : public Stmt {
800 enum { VAR, COND, THEN, ELSE, END_EXPR };
801 Stmt* SubExprs[END_EXPR];
803 SourceLocation IfLoc;
804 SourceLocation ElseLoc;
807 IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
808 Stmt *then, SourceLocation EL = SourceLocation(), Stmt *elsev = 0);
810 /// \brief Build an empty if/then/else statement
811 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
813 /// \brief Retrieve the variable declared in this "if" statement, if any.
815 /// In the following example, "x" is the condition variable.
817 /// if (int x = foo()) {
818 /// printf("x is %d", x);
821 VarDecl *getConditionVariable() const;
822 void setConditionVariable(ASTContext &C, VarDecl *V);
824 /// If this IfStmt has a condition variable, return the faux DeclStmt
825 /// associated with the creation of that condition variable.
826 const DeclStmt *getConditionVariableDeclStmt() const {
827 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
830 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
831 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
832 const Stmt *getThen() const { return SubExprs[THEN]; }
833 void setThen(Stmt *S) { SubExprs[THEN] = S; }
834 const Stmt *getElse() const { return SubExprs[ELSE]; }
835 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
837 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
838 Stmt *getThen() { return SubExprs[THEN]; }
839 Stmt *getElse() { return SubExprs[ELSE]; }
841 SourceLocation getIfLoc() const { return IfLoc; }
842 void setIfLoc(SourceLocation L) { IfLoc = L; }
843 SourceLocation getElseLoc() const { return ElseLoc; }
844 void setElseLoc(SourceLocation L) { ElseLoc = L; }
846 SourceRange getSourceRange() const LLVM_READONLY {
848 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
850 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
853 // Iterators over subexpressions. The iterators will include iterating
854 // over the initialization expression referenced by the condition variable.
855 child_range children() {
856 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
859 static bool classof(const Stmt *T) {
860 return T->getStmtClass() == IfStmtClass;
862 static bool classof(const IfStmt *) { return true; }
865 /// SwitchStmt - This represents a 'switch' stmt.
867 class SwitchStmt : public Stmt {
868 enum { VAR, COND, BODY, END_EXPR };
869 Stmt* SubExprs[END_EXPR];
870 // This points to a linked list of case and default statements.
871 SwitchCase *FirstCase;
872 SourceLocation SwitchLoc;
874 /// If the SwitchStmt is a switch on an enum value, this records whether
875 /// all the enum values were covered by CaseStmts. This value is meant to
876 /// be a hint for possible clients.
877 unsigned AllEnumCasesCovered : 1;
880 SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond);
882 /// \brief Build a empty switch statement.
883 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
885 /// \brief Retrieve the variable declared in this "switch" statement, if any.
887 /// In the following example, "x" is the condition variable.
889 /// switch (int x = foo()) {
894 VarDecl *getConditionVariable() const;
895 void setConditionVariable(ASTContext &C, VarDecl *V);
897 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
898 /// associated with the creation of that condition variable.
899 const DeclStmt *getConditionVariableDeclStmt() const {
900 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
903 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
904 const Stmt *getBody() const { return SubExprs[BODY]; }
905 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
907 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
908 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
909 Stmt *getBody() { return SubExprs[BODY]; }
910 void setBody(Stmt *S) { SubExprs[BODY] = S; }
911 SwitchCase *getSwitchCaseList() { return FirstCase; }
913 /// \brief Set the case list for this switch statement.
915 /// The caller is responsible for incrementing the retain counts on
916 /// all of the SwitchCase statements in this list.
917 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
919 SourceLocation getSwitchLoc() const { return SwitchLoc; }
920 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
922 void setBody(Stmt *S, SourceLocation SL) {
926 void addSwitchCase(SwitchCase *SC) {
927 assert(!SC->getNextSwitchCase()
928 && "case/default already added to a switch");
929 SC->setNextSwitchCase(FirstCase);
933 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
934 /// switch over an enum value then all cases have been explicitly covered.
935 void setAllEnumCasesCovered() {
936 AllEnumCasesCovered = 1;
939 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
940 /// have been explicitly covered.
941 bool isAllEnumCasesCovered() const {
942 return (bool) AllEnumCasesCovered;
945 SourceRange getSourceRange() const LLVM_READONLY {
946 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
949 child_range children() {
950 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
953 static bool classof(const Stmt *T) {
954 return T->getStmtClass() == SwitchStmtClass;
956 static bool classof(const SwitchStmt *) { return true; }
960 /// WhileStmt - This represents a 'while' stmt.
962 class WhileStmt : public Stmt {
963 enum { VAR, COND, BODY, END_EXPR };
964 Stmt* SubExprs[END_EXPR];
965 SourceLocation WhileLoc;
967 WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
970 /// \brief Build an empty while statement.
971 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
973 /// \brief Retrieve the variable declared in this "while" statement, if any.
975 /// In the following example, "x" is the condition variable.
977 /// while (int x = random()) {
981 VarDecl *getConditionVariable() const;
982 void setConditionVariable(ASTContext &C, VarDecl *V);
984 /// If this WhileStmt has a condition variable, return the faux DeclStmt
985 /// associated with the creation of that condition variable.
986 const DeclStmt *getConditionVariableDeclStmt() const {
987 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
990 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
991 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
992 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
993 Stmt *getBody() { return SubExprs[BODY]; }
994 const Stmt *getBody() const { return SubExprs[BODY]; }
995 void setBody(Stmt *S) { SubExprs[BODY] = S; }
997 SourceLocation getWhileLoc() const { return WhileLoc; }
998 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1000 SourceRange getSourceRange() const LLVM_READONLY {
1001 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
1003 static bool classof(const Stmt *T) {
1004 return T->getStmtClass() == WhileStmtClass;
1006 static bool classof(const WhileStmt *) { return true; }
1009 child_range children() {
1010 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1014 /// DoStmt - This represents a 'do/while' stmt.
1016 class DoStmt : public Stmt {
1017 enum { BODY, COND, END_EXPR };
1018 Stmt* SubExprs[END_EXPR];
1019 SourceLocation DoLoc;
1020 SourceLocation WhileLoc;
1021 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1024 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1026 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1027 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1028 SubExprs[BODY] = body;
1031 /// \brief Build an empty do-while statement.
1032 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
1034 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1035 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1036 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1037 Stmt *getBody() { return SubExprs[BODY]; }
1038 const Stmt *getBody() const { return SubExprs[BODY]; }
1039 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1041 SourceLocation getDoLoc() const { return DoLoc; }
1042 void setDoLoc(SourceLocation L) { DoLoc = L; }
1043 SourceLocation getWhileLoc() const { return WhileLoc; }
1044 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1046 SourceLocation getRParenLoc() const { return RParenLoc; }
1047 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1049 SourceRange getSourceRange() const LLVM_READONLY {
1050 return SourceRange(DoLoc, RParenLoc);
1052 static bool classof(const Stmt *T) {
1053 return T->getStmtClass() == DoStmtClass;
1055 static bool classof(const DoStmt *) { return true; }
1058 child_range children() {
1059 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1064 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1065 /// the init/cond/inc parts of the ForStmt will be null if they were not
1066 /// specified in the source.
1068 class ForStmt : public Stmt {
1069 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1070 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1071 SourceLocation ForLoc;
1072 SourceLocation LParenLoc, RParenLoc;
1075 ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc,
1076 Stmt *Body, SourceLocation FL, SourceLocation LP, SourceLocation RP);
1078 /// \brief Build an empty for statement.
1079 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
1081 Stmt *getInit() { return SubExprs[INIT]; }
1083 /// \brief Retrieve the variable declared in this "for" statement, if any.
1085 /// In the following example, "y" is the condition variable.
1087 /// for (int x = random(); int y = mangle(x); ++x) {
1091 VarDecl *getConditionVariable() const;
1092 void setConditionVariable(ASTContext &C, VarDecl *V);
1094 /// If this ForStmt has a condition variable, return the faux DeclStmt
1095 /// associated with the creation of that condition variable.
1096 const DeclStmt *getConditionVariableDeclStmt() const {
1097 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1100 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1101 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1102 Stmt *getBody() { return SubExprs[BODY]; }
1104 const Stmt *getInit() const { return SubExprs[INIT]; }
1105 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1106 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1107 const Stmt *getBody() const { return SubExprs[BODY]; }
1109 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1110 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1111 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1112 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1114 SourceLocation getForLoc() const { return ForLoc; }
1115 void setForLoc(SourceLocation L) { ForLoc = L; }
1116 SourceLocation getLParenLoc() const { return LParenLoc; }
1117 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1118 SourceLocation getRParenLoc() const { return RParenLoc; }
1119 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1121 SourceRange getSourceRange() const LLVM_READONLY {
1122 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
1124 static bool classof(const Stmt *T) {
1125 return T->getStmtClass() == ForStmtClass;
1127 static bool classof(const ForStmt *) { return true; }
1130 child_range children() {
1131 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1135 /// GotoStmt - This represents a direct goto.
1137 class GotoStmt : public Stmt {
1139 SourceLocation GotoLoc;
1140 SourceLocation LabelLoc;
1142 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1143 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1145 /// \brief Build an empty goto statement.
1146 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
1148 LabelDecl *getLabel() const { return Label; }
1149 void setLabel(LabelDecl *D) { Label = D; }
1151 SourceLocation getGotoLoc() const { return GotoLoc; }
1152 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1153 SourceLocation getLabelLoc() const { return LabelLoc; }
1154 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1156 SourceRange getSourceRange() const LLVM_READONLY {
1157 return SourceRange(GotoLoc, LabelLoc);
1159 static bool classof(const Stmt *T) {
1160 return T->getStmtClass() == GotoStmtClass;
1162 static bool classof(const GotoStmt *) { return true; }
1165 child_range children() { return child_range(); }
1168 /// IndirectGotoStmt - This represents an indirect goto.
1170 class IndirectGotoStmt : public Stmt {
1171 SourceLocation GotoLoc;
1172 SourceLocation StarLoc;
1175 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1177 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1178 Target((Stmt*)target) {}
1180 /// \brief Build an empty indirect goto statement.
1181 explicit IndirectGotoStmt(EmptyShell Empty)
1182 : Stmt(IndirectGotoStmtClass, Empty) { }
1184 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1185 SourceLocation getGotoLoc() const { return GotoLoc; }
1186 void setStarLoc(SourceLocation L) { StarLoc = L; }
1187 SourceLocation getStarLoc() const { return StarLoc; }
1189 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1190 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1191 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1193 /// getConstantTarget - Returns the fixed target of this indirect
1194 /// goto, if one exists.
1195 LabelDecl *getConstantTarget();
1196 const LabelDecl *getConstantTarget() const {
1197 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1200 SourceRange getSourceRange() const LLVM_READONLY {
1201 return SourceRange(GotoLoc, Target->getLocEnd());
1204 static bool classof(const Stmt *T) {
1205 return T->getStmtClass() == IndirectGotoStmtClass;
1207 static bool classof(const IndirectGotoStmt *) { return true; }
1210 child_range children() { return child_range(&Target, &Target+1); }
1214 /// ContinueStmt - This represents a continue.
1216 class ContinueStmt : public Stmt {
1217 SourceLocation ContinueLoc;
1219 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1221 /// \brief Build an empty continue statement.
1222 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1224 SourceLocation getContinueLoc() const { return ContinueLoc; }
1225 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1227 SourceRange getSourceRange() const LLVM_READONLY {
1228 return SourceRange(ContinueLoc);
1231 static bool classof(const Stmt *T) {
1232 return T->getStmtClass() == ContinueStmtClass;
1234 static bool classof(const ContinueStmt *) { return true; }
1237 child_range children() { return child_range(); }
1240 /// BreakStmt - This represents a break.
1242 class BreakStmt : public Stmt {
1243 SourceLocation BreakLoc;
1245 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1247 /// \brief Build an empty break statement.
1248 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1250 SourceLocation getBreakLoc() const { return BreakLoc; }
1251 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1253 SourceRange getSourceRange() const LLVM_READONLY { return SourceRange(BreakLoc); }
1255 static bool classof(const Stmt *T) {
1256 return T->getStmtClass() == BreakStmtClass;
1258 static bool classof(const BreakStmt *) { return true; }
1261 child_range children() { return child_range(); }
1265 /// ReturnStmt - This represents a return, optionally of an expression:
1269 /// Note that GCC allows return with no argument in a function declared to
1270 /// return a value, and it allows returning a value in functions declared to
1271 /// return void. We explicitly model this in the AST, which means you can't
1272 /// depend on the return type of the function and the presence of an argument.
1274 class ReturnStmt : public Stmt {
1276 SourceLocation RetLoc;
1277 const VarDecl *NRVOCandidate;
1280 ReturnStmt(SourceLocation RL)
1281 : Stmt(ReturnStmtClass), RetExpr(0), RetLoc(RL), NRVOCandidate(0) { }
1283 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1284 : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL),
1285 NRVOCandidate(NRVOCandidate) {}
1287 /// \brief Build an empty return expression.
1288 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1290 const Expr *getRetValue() const;
1291 Expr *getRetValue();
1292 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1294 SourceLocation getReturnLoc() const { return RetLoc; }
1295 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1297 /// \brief Retrieve the variable that might be used for the named return
1298 /// value optimization.
1300 /// The optimization itself can only be performed if the variable is
1301 /// also marked as an NRVO object.
1302 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1303 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1305 SourceRange getSourceRange() const LLVM_READONLY;
1307 static bool classof(const Stmt *T) {
1308 return T->getStmtClass() == ReturnStmtClass;
1310 static bool classof(const ReturnStmt *) { return true; }
1313 child_range children() {
1314 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1315 return child_range();
1319 /// AsmStmt - This represents a GNU inline-assembly statement extension.
1321 class AsmStmt : public Stmt {
1322 SourceLocation AsmLoc, RParenLoc;
1323 StringLiteral *AsmStr;
1329 unsigned NumOutputs;
1331 unsigned NumClobbers;
1333 // FIXME: If we wanted to, we could allocate all of these in one big array.
1334 IdentifierInfo **Names;
1335 StringLiteral **Constraints;
1337 StringLiteral **Clobbers;
1340 AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1341 bool msasm, unsigned numoutputs, unsigned numinputs,
1342 IdentifierInfo **names, StringLiteral **constraints,
1343 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1344 StringLiteral **clobbers, SourceLocation rparenloc);
1346 /// \brief Build an empty inline-assembly statement.
1347 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1348 Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1350 SourceLocation getAsmLoc() const { return AsmLoc; }
1351 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1352 SourceLocation getRParenLoc() const { return RParenLoc; }
1353 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1355 bool isVolatile() const { return IsVolatile; }
1356 void setVolatile(bool V) { IsVolatile = V; }
1357 bool isSimple() const { return IsSimple; }
1358 void setSimple(bool V) { IsSimple = V; }
1359 bool isMSAsm() const { return MSAsm; }
1360 void setMSAsm(bool V) { MSAsm = V; }
1362 //===--- Asm String Analysis ---===//
1364 const StringLiteral *getAsmString() const { return AsmStr; }
1365 StringLiteral *getAsmString() { return AsmStr; }
1366 void setAsmString(StringLiteral *E) { AsmStr = E; }
1368 /// AsmStringPiece - this is part of a decomposed asm string specification
1369 /// (for use with the AnalyzeAsmString function below). An asm string is
1370 /// considered to be a concatenation of these parts.
1371 class AsmStringPiece {
1374 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1375 Operand // Operand reference, with optional modifier %c4.
1382 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1383 AsmStringPiece(unsigned OpNo, char Modifier)
1384 : MyKind(Operand), Str(), OperandNo(OpNo) {
1388 bool isString() const { return MyKind == String; }
1389 bool isOperand() const { return MyKind == Operand; }
1391 const std::string &getString() const {
1396 unsigned getOperandNo() const {
1397 assert(isOperand());
1401 /// getModifier - Get the modifier for this operand, if present. This
1402 /// returns '\0' if there was no modifier.
1403 char getModifier() const {
1404 assert(isOperand());
1409 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1410 /// it into pieces. If the asm string is erroneous, emit errors and return
1411 /// true, otherwise return false. This handles canonicalization and
1412 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1413 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1414 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1415 ASTContext &C, unsigned &DiagOffs) const;
1418 //===--- Output operands ---===//
1420 unsigned getNumOutputs() const { return NumOutputs; }
1422 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1426 StringRef getOutputName(unsigned i) const {
1427 if (IdentifierInfo *II = getOutputIdentifier(i))
1428 return II->getName();
1433 /// getOutputConstraint - Return the constraint string for the specified
1434 /// output operand. All output constraints are known to be non-empty (either
1436 StringRef getOutputConstraint(unsigned i) const;
1438 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1439 return Constraints[i];
1441 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1442 return Constraints[i];
1445 Expr *getOutputExpr(unsigned i);
1447 const Expr *getOutputExpr(unsigned i) const {
1448 return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1451 /// isOutputPlusConstraint - Return true if the specified output constraint
1452 /// is a "+" constraint (which is both an input and an output) or false if it
1453 /// is an "=" constraint (just an output).
1454 bool isOutputPlusConstraint(unsigned i) const {
1455 return getOutputConstraint(i)[0] == '+';
1458 /// getNumPlusOperands - Return the number of output operands that have a "+"
1460 unsigned getNumPlusOperands() const;
1462 //===--- Input operands ---===//
1464 unsigned getNumInputs() const { return NumInputs; }
1466 IdentifierInfo *getInputIdentifier(unsigned i) const {
1467 return Names[i + NumOutputs];
1470 StringRef getInputName(unsigned i) const {
1471 if (IdentifierInfo *II = getInputIdentifier(i))
1472 return II->getName();
1477 /// getInputConstraint - Return the specified input constraint. Unlike output
1478 /// constraints, these can be empty.
1479 StringRef getInputConstraint(unsigned i) const;
1481 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1482 return Constraints[i + NumOutputs];
1484 StringLiteral *getInputConstraintLiteral(unsigned i) {
1485 return Constraints[i + NumOutputs];
1488 Expr *getInputExpr(unsigned i);
1489 void setInputExpr(unsigned i, Expr *E);
1491 const Expr *getInputExpr(unsigned i) const {
1492 return const_cast<AsmStmt*>(this)->getInputExpr(i);
1495 void setOutputsAndInputsAndClobbers(ASTContext &C,
1496 IdentifierInfo **Names,
1497 StringLiteral **Constraints,
1499 unsigned NumOutputs,
1501 StringLiteral **Clobbers,
1502 unsigned NumClobbers);
1504 //===--- Other ---===//
1506 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1507 /// translate this into a numeric value needed to reference the same operand.
1508 /// This returns -1 if the operand name is invalid.
1509 int getNamedOperand(StringRef SymbolicName) const;
1511 unsigned getNumClobbers() const { return NumClobbers; }
1512 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1513 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1515 SourceRange getSourceRange() const LLVM_READONLY {
1516 return SourceRange(AsmLoc, RParenLoc);
1519 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1520 static bool classof(const AsmStmt *) { return true; }
1522 // Input expr iterators.
1524 typedef ExprIterator inputs_iterator;
1525 typedef ConstExprIterator const_inputs_iterator;
1527 inputs_iterator begin_inputs() {
1528 return &Exprs[0] + NumOutputs;
1531 inputs_iterator end_inputs() {
1532 return &Exprs[0] + NumOutputs + NumInputs;
1535 const_inputs_iterator begin_inputs() const {
1536 return &Exprs[0] + NumOutputs;
1539 const_inputs_iterator end_inputs() const {
1540 return &Exprs[0] + NumOutputs + NumInputs;
1543 // Output expr iterators.
1545 typedef ExprIterator outputs_iterator;
1546 typedef ConstExprIterator const_outputs_iterator;
1548 outputs_iterator begin_outputs() {
1551 outputs_iterator end_outputs() {
1552 return &Exprs[0] + NumOutputs;
1555 const_outputs_iterator begin_outputs() const {
1558 const_outputs_iterator end_outputs() const {
1559 return &Exprs[0] + NumOutputs;
1562 child_range children() {
1563 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1567 class SEHExceptStmt : public Stmt {
1571 enum { FILTER_EXPR, BLOCK };
1573 SEHExceptStmt(SourceLocation Loc,
1577 friend class ASTReader;
1578 friend class ASTStmtReader;
1579 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) { }
1582 static SEHExceptStmt* Create(ASTContext &C,
1583 SourceLocation ExceptLoc,
1586 SourceRange getSourceRange() const LLVM_READONLY {
1587 return SourceRange(getExceptLoc(), getEndLoc());
1590 SourceLocation getExceptLoc() const { return Loc; }
1591 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1593 Expr *getFilterExpr() const {
1594 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1597 CompoundStmt *getBlock() const {
1598 return llvm::cast<CompoundStmt>(Children[BLOCK]);
1601 child_range children() {
1602 return child_range(Children,Children+2);
1605 static bool classof(const Stmt *T) {
1606 return T->getStmtClass() == SEHExceptStmtClass;
1609 static bool classof(SEHExceptStmt *) { return true; }
1613 class SEHFinallyStmt : public Stmt {
1617 SEHFinallyStmt(SourceLocation Loc,
1620 friend class ASTReader;
1621 friend class ASTStmtReader;
1622 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) { }
1625 static SEHFinallyStmt* Create(ASTContext &C,
1626 SourceLocation FinallyLoc,
1629 SourceRange getSourceRange() const LLVM_READONLY {
1630 return SourceRange(getFinallyLoc(), getEndLoc());
1633 SourceLocation getFinallyLoc() const { return Loc; }
1634 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1636 CompoundStmt *getBlock() const { return llvm::cast<CompoundStmt>(Block); }
1638 child_range children() {
1639 return child_range(&Block,&Block+1);
1642 static bool classof(const Stmt *T) {
1643 return T->getStmtClass() == SEHFinallyStmtClass;
1646 static bool classof(SEHFinallyStmt *) { return true; }
1650 class SEHTryStmt : public Stmt {
1652 SourceLocation TryLoc;
1655 enum { TRY = 0, HANDLER = 1 };
1657 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1658 SourceLocation TryLoc,
1662 friend class ASTReader;
1663 friend class ASTStmtReader;
1664 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) { }
1667 static SEHTryStmt* Create(ASTContext &C,
1669 SourceLocation TryLoc,
1673 SourceRange getSourceRange() const LLVM_READONLY {
1674 return SourceRange(getTryLoc(), getEndLoc());
1677 SourceLocation getTryLoc() const { return TryLoc; }
1678 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1680 bool getIsCXXTry() const { return IsCXXTry; }
1682 CompoundStmt* getTryBlock() const {
1683 return llvm::cast<CompoundStmt>(Children[TRY]);
1686 Stmt *getHandler() const { return Children[HANDLER]; }
1688 /// Returns 0 if not defined
1689 SEHExceptStmt *getExceptHandler() const;
1690 SEHFinallyStmt *getFinallyHandler() const;
1692 child_range children() {
1693 return child_range(Children,Children+2);
1696 static bool classof(const Stmt *T) {
1697 return T->getStmtClass() == SEHTryStmtClass;
1700 static bool classof(SEHTryStmt *) { return true; }
1703 } // end namespace clang