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 "llvm/Support/Casting.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/AST/PrettyPrinter.h"
21 #include "clang/AST/StmtIterator.h"
22 #include "clang/AST/DeclGroup.h"
23 #include "clang/AST/FullExpr.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "clang/AST/ASTContext.h"
27 using llvm::dyn_cast_or_null;
30 class FoldingSetNodeID;
44 //===----------------------------------------------------------------------===//
45 // ExprIterator - Iterators for iterating over Stmt* arrays that contain
46 // only Expr*. This is needed because AST nodes use Stmt* arrays to store
47 // references to children (to be compatible with StmtIterator).
48 //===----------------------------------------------------------------------===//
56 ExprIterator(Stmt** i) : I(i) {}
57 ExprIterator() : I(0) {}
58 ExprIterator& operator++() { ++I; return *this; }
59 ExprIterator operator-(size_t i) { return I-i; }
60 ExprIterator operator+(size_t i) { return I+i; }
61 Expr* operator[](size_t idx);
62 // FIXME: Verify that this will correctly return a signed distance.
63 signed operator-(const ExprIterator& R) const { return I - R.I; }
64 Expr* operator*() const;
65 Expr* operator->() const;
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; }
69 bool operator>=(const ExprIterator& R) const { return I >= R.I; }
72 class ConstExprIterator {
75 ConstExprIterator(Stmt* const* i) : I(i) {}
76 ConstExprIterator() : I(0) {}
77 ConstExprIterator& operator++() { ++I; return *this; }
78 ConstExprIterator operator+(size_t i) { return I+i; }
79 ConstExprIterator operator-(size_t i) { return I-i; }
80 const Expr * operator[](size_t idx) const;
81 signed operator-(const ConstExprIterator& R) const { return I - R.I; }
82 const Expr * operator*() const;
83 const Expr * operator->() const;
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; }
87 bool operator>=(const ConstExprIterator& R) const { return I >= R.I; }
90 //===----------------------------------------------------------------------===//
91 // AST classes for statements.
92 //===----------------------------------------------------------------------===//
94 /// Stmt - This represents one statement.
100 #define STMT(CLASS, PARENT) CLASS##Class,
101 #define FIRST_STMT(CLASS) firstStmtConstant = CLASS##Class,
102 #define LAST_STMT(CLASS) lastStmtConstant = CLASS##Class,
103 #define FIRST_EXPR(CLASS) firstExprConstant = CLASS##Class,
104 #define LAST_EXPR(CLASS) lastExprConstant = CLASS##Class
105 #define ABSTRACT_EXPR(CLASS, PARENT)
106 #include "clang/AST/StmtNodes.def"
109 /// \brief The statement class.
110 const unsigned sClass : 8;
112 /// \brief The reference count for this statement.
113 unsigned RefCount : 24;
115 // Make vanilla 'new' and 'delete' illegal for Stmts.
117 void* operator new(size_t bytes) throw() {
118 assert(0 && "Stmts cannot be allocated with regular 'new'.");
121 void operator delete(void* data) throw() {
122 assert(0 && "Stmts cannot be released with regular 'delete'.");
126 // Only allow allocation of Stmts using the allocator in ASTContext
127 // or by doing a placement new.
128 void* operator new(size_t bytes, ASTContext& C,
129 unsigned alignment = 8) throw() {
130 return ::operator new(bytes, C, alignment);
133 void* operator new(size_t bytes, ASTContext* C,
134 unsigned alignment = 8) throw() {
135 return ::operator new(bytes, *C, alignment);
138 void* operator new(size_t bytes, void* mem) throw() {
142 void operator delete(void*, ASTContext&, unsigned) throw() { }
143 void operator delete(void*, ASTContext*, unsigned) throw() { }
144 void operator delete(void*, std::size_t) throw() { }
145 void operator delete(void*, void*) throw() { }
148 /// \brief A placeholder type used to construct an empty shell of a
149 /// type, that will be filled in later (e.g., by some
150 /// de-serialization).
151 struct EmptyShell { };
154 /// DestroyChildren - Invoked by destructors of subclasses of Stmt to
155 /// recursively release child AST nodes.
156 void DestroyChildren(ASTContext& Ctx);
158 /// \brief Construct an empty statement.
159 explicit Stmt(StmtClass SC, EmptyShell) : sClass(SC), RefCount(1) {
160 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
163 /// \brief Virtual method that performs the actual destruction of
166 /// Subclasses should override this method (not Destroy()) to
167 /// provide class-specific destruction.
168 virtual void DoDestroy(ASTContext &Ctx);
171 Stmt(StmtClass SC) : sClass(SC), RefCount(1) {
172 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
177 /// \brief True if this statement's refcount is in a valid state.
178 /// Should be used only in assertions.
179 bool isRetained() const {
180 return (RefCount >= 1);
184 /// \brief Destroy the current statement and its children.
185 void Destroy(ASTContext &Ctx) {
186 assert(RefCount >= 1);
191 /// \brief Increases the reference count for this statement.
193 /// Invoke the Retain() operation when this statement or expression
194 /// is being shared by another owner.
196 assert(RefCount >= 1);
201 StmtClass getStmtClass() const {
202 assert(RefCount >= 1 && "Referencing already-destroyed statement!");
203 return (StmtClass)sClass;
205 const char *getStmtClassName() const;
207 /// SourceLocation tokens are not useful in isolation - they are low level
208 /// value objects created/interpreted by SourceManager. We assume AST
209 /// clients will have a pointer to the respective SourceManager.
210 virtual SourceRange getSourceRange() const = 0;
211 SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
212 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
214 // global temp stats (until we have a per-module visitor)
215 static void addStmtClass(const StmtClass s);
216 static bool CollectingStats(bool Enable = false);
217 static void PrintStats();
219 /// dump - This does a local dump of the specified AST fragment. It dumps the
220 /// specified node and a few nodes underneath it, but not the whole subtree.
221 /// This is useful in a debugger.
223 void dump(SourceManager &SM) const;
225 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
226 void dumpAll() const;
227 void dumpAll(SourceManager &SM) const;
229 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
230 /// back to its original source language syntax.
231 void dumpPretty(ASTContext& Context) const;
232 void printPretty(llvm::raw_ostream &OS, PrinterHelper *Helper,
233 const PrintingPolicy &Policy,
234 unsigned Indentation = 0) const {
235 printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
237 void printPretty(llvm::raw_ostream &OS, ASTContext &Context,
238 PrinterHelper *Helper,
239 const PrintingPolicy &Policy,
240 unsigned Indentation = 0) const;
242 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
243 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
244 void viewAST() const;
246 // Implement isa<T> support.
247 static bool classof(const Stmt *) { return true; }
249 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
250 /// contain implicit control-flow in the order their subexpressions
251 /// are evaluated. This predicate returns true if this statement has
252 /// such implicit control-flow. Such statements are also specially handled
254 bool hasImplicitControlFlow() const;
256 /// Child Iterators: All subclasses must implement child_begin and child_end
257 /// to permit easy iteration over the substatements/subexpessions of an
258 /// AST node. This permits easy iteration over all nodes in the AST.
259 typedef StmtIterator child_iterator;
260 typedef ConstStmtIterator const_child_iterator;
262 virtual child_iterator child_begin() = 0;
263 virtual child_iterator child_end() = 0;
265 const_child_iterator child_begin() const {
266 return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
269 const_child_iterator child_end() const {
270 return const_child_iterator(const_cast<Stmt*>(this)->child_end());
273 /// \brief Produce a unique representation of the given statement.
275 /// \brief ID once the profiling operation is complete, will contain
276 /// the unique representation of the given statement.
278 /// \brief Context the AST context in which the statement resides
280 /// \brief Canonical whether the profile should be based on the canonical
281 /// representation of this statement (e.g., where non-type template
282 /// parameters are identified by index/level rather than their
283 /// declaration pointers) or the exact representation of the statement as
284 /// written in the source.
285 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
289 /// DeclStmt - Adaptor class for mixing declarations with statements and
290 /// expressions. For example, CompoundStmt mixes statements, expressions
291 /// and declarations (variables, types). Another example is ForStmt, where
292 /// the first statement can be an expression or a declaration.
294 class DeclStmt : public Stmt {
296 SourceLocation StartLoc, EndLoc;
299 virtual void DoDestroy(ASTContext &Ctx);
302 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
303 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
304 StartLoc(startLoc), EndLoc(endLoc) {}
306 /// \brief Build an empty declaration statement.
307 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
309 /// isSingleDecl - This method returns true if this DeclStmt refers
310 /// to a single Decl.
311 bool isSingleDecl() const {
312 return DG.isSingleDecl();
315 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
316 Decl *getSingleDecl() { return DG.getSingleDecl(); }
318 const DeclGroupRef getDeclGroup() const { return DG; }
319 DeclGroupRef getDeclGroup() { return DG; }
320 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
322 SourceLocation getStartLoc() const { return StartLoc; }
323 void setStartLoc(SourceLocation L) { StartLoc = L; }
324 SourceLocation getEndLoc() const { return EndLoc; }
325 void setEndLoc(SourceLocation L) { EndLoc = L; }
327 SourceRange getSourceRange() const {
328 return SourceRange(StartLoc, EndLoc);
331 static bool classof(const Stmt *T) {
332 return T->getStmtClass() == DeclStmtClass;
334 static bool classof(const DeclStmt *) { return true; }
336 // Iterators over subexpressions.
337 virtual child_iterator child_begin();
338 virtual child_iterator child_end();
340 typedef DeclGroupRef::iterator decl_iterator;
341 typedef DeclGroupRef::const_iterator const_decl_iterator;
343 decl_iterator decl_begin() { return DG.begin(); }
344 decl_iterator decl_end() { return DG.end(); }
345 const_decl_iterator decl_begin() const { return DG.begin(); }
346 const_decl_iterator decl_end() const { return DG.end(); }
349 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
351 class NullStmt : public Stmt {
352 SourceLocation SemiLoc;
354 NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
356 /// \brief Build an empty null statement.
357 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
359 SourceLocation getSemiLoc() const { return SemiLoc; }
360 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
362 virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
364 static bool classof(const Stmt *T) {
365 return T->getStmtClass() == NullStmtClass;
367 static bool classof(const NullStmt *) { return true; }
370 virtual child_iterator child_begin();
371 virtual child_iterator child_end();
374 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
376 class CompoundStmt : public Stmt {
379 SourceLocation LBracLoc, RBracLoc;
381 CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts,
382 SourceLocation LB, SourceLocation RB)
383 : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) {
389 Body = new (C) Stmt*[NumStmts];
390 memcpy(Body, StmtStart, numStmts * sizeof(*Body));
393 // \brief Build an empty compound statement.
394 explicit CompoundStmt(EmptyShell Empty)
395 : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { }
397 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
399 bool body_empty() const { return NumStmts == 0; }
400 unsigned size() const { return NumStmts; }
402 typedef Stmt** body_iterator;
403 body_iterator body_begin() { return Body; }
404 body_iterator body_end() { return Body + NumStmts; }
405 Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; }
407 typedef Stmt* const * const_body_iterator;
408 const_body_iterator body_begin() const { return Body; }
409 const_body_iterator body_end() const { return Body + NumStmts; }
410 const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; }
412 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
413 reverse_body_iterator body_rbegin() {
414 return reverse_body_iterator(body_end());
416 reverse_body_iterator body_rend() {
417 return reverse_body_iterator(body_begin());
420 typedef std::reverse_iterator<const_body_iterator>
421 const_reverse_body_iterator;
423 const_reverse_body_iterator body_rbegin() const {
424 return const_reverse_body_iterator(body_end());
427 const_reverse_body_iterator body_rend() const {
428 return const_reverse_body_iterator(body_begin());
431 virtual SourceRange getSourceRange() const {
432 return SourceRange(LBracLoc, RBracLoc);
435 SourceLocation getLBracLoc() const { return LBracLoc; }
436 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
437 SourceLocation getRBracLoc() const { return RBracLoc; }
438 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
440 static bool classof(const Stmt *T) {
441 return T->getStmtClass() == CompoundStmtClass;
443 static bool classof(const CompoundStmt *) { return true; }
446 virtual child_iterator child_begin();
447 virtual child_iterator child_end();
450 // SwitchCase is the base class for CaseStmt and DefaultStmt,
451 class SwitchCase : public Stmt {
453 // A pointer to the following CaseStmt or DefaultStmt class,
454 // used by SwitchStmt.
455 SwitchCase *NextSwitchCase;
457 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
460 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
462 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
464 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
466 Stmt *getSubStmt() { return v_getSubStmt(); }
468 virtual SourceRange getSourceRange() const { return SourceRange(); }
470 static bool classof(const Stmt *T) {
471 return T->getStmtClass() == CaseStmtClass ||
472 T->getStmtClass() == DefaultStmtClass;
474 static bool classof(const SwitchCase *) { return true; }
476 virtual Stmt* v_getSubStmt() = 0;
479 class CaseStmt : public SwitchCase {
480 enum { SUBSTMT, LHS, RHS, END_EXPR };
481 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
482 // GNU "case 1 ... 4" extension
483 SourceLocation CaseLoc;
484 SourceLocation EllipsisLoc;
485 SourceLocation ColonLoc;
487 virtual Stmt* v_getSubStmt() { return getSubStmt(); }
489 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
490 SourceLocation ellipsisLoc, SourceLocation colonLoc)
491 : SwitchCase(CaseStmtClass) {
492 SubExprs[SUBSTMT] = 0;
493 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
494 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
496 EllipsisLoc = ellipsisLoc;
500 /// \brief Build an empty switch case statement.
501 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
503 SourceLocation getCaseLoc() const { return CaseLoc; }
504 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
505 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
506 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
507 SourceLocation getColonLoc() const { return ColonLoc; }
508 void setColonLoc(SourceLocation L) { ColonLoc = L; }
510 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
511 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
512 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
514 const Expr *getLHS() const {
515 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
517 const Expr *getRHS() const {
518 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
520 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
522 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
523 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
524 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
527 virtual SourceRange getSourceRange() const {
528 // Handle deeply nested case statements with iteration instead of recursion.
529 const CaseStmt *CS = this;
530 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
533 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
535 static bool classof(const Stmt *T) {
536 return T->getStmtClass() == CaseStmtClass;
538 static bool classof(const CaseStmt *) { return true; }
541 virtual child_iterator child_begin();
542 virtual child_iterator child_end();
545 class DefaultStmt : public SwitchCase {
547 SourceLocation DefaultLoc;
548 SourceLocation ColonLoc;
549 virtual Stmt* v_getSubStmt() { return getSubStmt(); }
551 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
552 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
555 /// \brief Build an empty default statement.
556 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
558 Stmt *getSubStmt() { return SubStmt; }
559 const Stmt *getSubStmt() const { return SubStmt; }
560 void setSubStmt(Stmt *S) { SubStmt = S; }
562 SourceLocation getDefaultLoc() const { return DefaultLoc; }
563 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
564 SourceLocation getColonLoc() const { return ColonLoc; }
565 void setColonLoc(SourceLocation L) { ColonLoc = L; }
567 virtual SourceRange getSourceRange() const {
568 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
570 static bool classof(const Stmt *T) {
571 return T->getStmtClass() == DefaultStmtClass;
573 static bool classof(const DefaultStmt *) { return true; }
576 virtual child_iterator child_begin();
577 virtual child_iterator child_end();
580 class LabelStmt : public Stmt {
581 IdentifierInfo *Label;
583 SourceLocation IdentLoc;
585 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
586 : Stmt(LabelStmtClass), Label(label),
587 SubStmt(substmt), IdentLoc(IL) {}
589 // \brief Build an empty label statement.
590 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
592 SourceLocation getIdentLoc() const { return IdentLoc; }
593 IdentifierInfo *getID() const { return Label; }
594 void setID(IdentifierInfo *II) { Label = II; }
595 const char *getName() const;
596 Stmt *getSubStmt() { return SubStmt; }
597 const Stmt *getSubStmt() const { return SubStmt; }
598 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
599 void setSubStmt(Stmt *SS) { SubStmt = SS; }
601 virtual SourceRange getSourceRange() const {
602 return SourceRange(IdentLoc, SubStmt->getLocEnd());
604 static bool classof(const Stmt *T) {
605 return T->getStmtClass() == LabelStmtClass;
607 static bool classof(const LabelStmt *) { return true; }
610 virtual child_iterator child_begin();
611 virtual child_iterator child_end();
615 /// IfStmt - This represents an if/then/else.
617 class IfStmt : public Stmt {
618 enum { COND, THEN, ELSE, END_EXPR };
619 Stmt* SubExprs[END_EXPR];
621 /// \brief If non-NULL, the declaration in the "if" statement.
624 SourceLocation IfLoc;
625 SourceLocation ElseLoc;
628 IfStmt(SourceLocation IL, VarDecl *var, Expr *cond, Stmt *then,
629 SourceLocation EL = SourceLocation(), Stmt *elsev = 0)
630 : Stmt(IfStmtClass), Var(var), IfLoc(IL), ElseLoc(EL) {
631 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
632 SubExprs[THEN] = then;
633 SubExprs[ELSE] = elsev;
636 /// \brief Build an empty if/then/else statement
637 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
639 /// \brief Retrieve the variable declared in this "if" statement, if any.
641 /// In the following example, "x" is the condition variable.
643 /// if (int x = foo()) {
644 /// printf("x is %d", x);
647 VarDecl *getConditionVariable() const { return Var; }
648 void setConditionVariable(VarDecl *V) { Var = V; }
650 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
651 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
652 const Stmt *getThen() const { return SubExprs[THEN]; }
653 void setThen(Stmt *S) { SubExprs[THEN] = S; }
654 const Stmt *getElse() const { return SubExprs[ELSE]; }
655 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
657 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
658 Stmt *getThen() { return SubExprs[THEN]; }
659 Stmt *getElse() { return SubExprs[ELSE]; }
661 SourceLocation getIfLoc() const { return IfLoc; }
662 void setIfLoc(SourceLocation L) { IfLoc = L; }
663 SourceLocation getElseLoc() const { return ElseLoc; }
664 void setElseLoc(SourceLocation L) { ElseLoc = L; }
666 virtual SourceRange getSourceRange() const {
668 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
670 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
673 static bool classof(const Stmt *T) {
674 return T->getStmtClass() == IfStmtClass;
676 static bool classof(const IfStmt *) { return true; }
678 // Iterators over subexpressions. The iterators will include iterating
679 // over the initialization expression referenced by the condition variable.
680 virtual child_iterator child_begin();
681 virtual child_iterator child_end();
684 virtual void DoDestroy(ASTContext &Ctx);
687 /// SwitchStmt - This represents a 'switch' stmt.
689 class SwitchStmt : public Stmt {
690 enum { COND, BODY, END_EXPR };
691 Stmt* SubExprs[END_EXPR];
693 // This points to a linked list of case and default statements.
694 SwitchCase *FirstCase;
695 SourceLocation SwitchLoc;
698 virtual void DoDestroy(ASTContext &Ctx);
701 SwitchStmt(VarDecl *Var, Expr *cond)
702 : Stmt(SwitchStmtClass), Var(Var), FirstCase(0)
704 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
705 SubExprs[BODY] = NULL;
708 /// \brief Build a empty switch statement.
709 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
711 /// \brief Retrieve the variable declared in this "switch" statement, if any.
713 /// In the following example, "x" is the condition variable.
715 /// switch (int x = foo()) {
720 VarDecl *getConditionVariable() const { return Var; }
721 void setConditionVariable(VarDecl *V) { Var = V; }
723 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
724 const Stmt *getBody() const { return SubExprs[BODY]; }
725 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
727 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
728 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
729 Stmt *getBody() { return SubExprs[BODY]; }
730 void setBody(Stmt *S) { SubExprs[BODY] = S; }
731 SwitchCase *getSwitchCaseList() { return FirstCase; }
733 /// \brief Set the case list for this switch statement.
735 /// The caller is responsible for incrementing the retain counts on
736 /// all of the SwitchCase statements in this list.
737 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
739 SourceLocation getSwitchLoc() const { return SwitchLoc; }
740 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
742 void setBody(Stmt *S, SourceLocation SL) {
746 void addSwitchCase(SwitchCase *SC) {
747 assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
749 SC->setNextSwitchCase(FirstCase);
752 virtual SourceRange getSourceRange() const {
753 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
755 static bool classof(const Stmt *T) {
756 return T->getStmtClass() == SwitchStmtClass;
758 static bool classof(const SwitchStmt *) { return true; }
761 virtual child_iterator child_begin();
762 virtual child_iterator child_end();
766 /// WhileStmt - This represents a 'while' stmt.
768 class WhileStmt : public Stmt {
769 enum { COND, BODY, END_EXPR };
771 Stmt* SubExprs[END_EXPR];
772 SourceLocation WhileLoc;
774 WhileStmt(VarDecl *Var, Expr *cond, Stmt *body, SourceLocation WL)
775 : Stmt(WhileStmtClass), Var(Var)
777 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
778 SubExprs[BODY] = body;
782 /// \brief Build an empty while statement.
783 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
785 /// \brief Retrieve the variable declared in this "while" statement, if any.
787 /// In the following example, "x" is the condition variable.
789 /// while (int x = random()) {
793 VarDecl *getConditionVariable() const { return Var; }
794 void setConditionVariable(VarDecl *V) { Var = V; }
796 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
797 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
798 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
799 Stmt *getBody() { return SubExprs[BODY]; }
800 const Stmt *getBody() const { return SubExprs[BODY]; }
801 void setBody(Stmt *S) { SubExprs[BODY] = S; }
803 SourceLocation getWhileLoc() const { return WhileLoc; }
804 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
806 virtual SourceRange getSourceRange() const {
807 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
809 static bool classof(const Stmt *T) {
810 return T->getStmtClass() == WhileStmtClass;
812 static bool classof(const WhileStmt *) { return true; }
815 virtual child_iterator child_begin();
816 virtual child_iterator child_end();
819 virtual void DoDestroy(ASTContext &Ctx);
822 /// DoStmt - This represents a 'do/while' stmt.
824 class DoStmt : public Stmt {
825 enum { COND, BODY, END_EXPR };
826 Stmt* SubExprs[END_EXPR];
827 SourceLocation DoLoc;
828 SourceLocation WhileLoc;
829 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
832 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
834 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
835 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
836 SubExprs[BODY] = body;
839 /// \brief Build an empty do-while statement.
840 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
842 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
843 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
844 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
845 Stmt *getBody() { return SubExprs[BODY]; }
846 const Stmt *getBody() const { return SubExprs[BODY]; }
847 void setBody(Stmt *S) { SubExprs[BODY] = S; }
849 SourceLocation getDoLoc() const { return DoLoc; }
850 void setDoLoc(SourceLocation L) { DoLoc = L; }
851 SourceLocation getWhileLoc() const { return WhileLoc; }
852 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
854 SourceLocation getRParenLoc() const { return RParenLoc; }
855 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
857 virtual SourceRange getSourceRange() const {
858 return SourceRange(DoLoc, RParenLoc);
860 static bool classof(const Stmt *T) {
861 return T->getStmtClass() == DoStmtClass;
863 static bool classof(const DoStmt *) { return true; }
866 virtual child_iterator child_begin();
867 virtual child_iterator child_end();
871 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
872 /// the init/cond/inc parts of the ForStmt will be null if they were not
873 /// specified in the source.
875 class ForStmt : public Stmt {
876 enum { INIT, COND, INC, BODY, END_EXPR };
877 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
879 SourceLocation ForLoc;
880 SourceLocation LParenLoc, RParenLoc;
883 ForStmt(Stmt *Init, Expr *Cond, VarDecl *condVar, Expr *Inc, Stmt *Body,
884 SourceLocation FL, SourceLocation LP, SourceLocation RP)
885 : Stmt(ForStmtClass), CondVar(condVar), ForLoc(FL), LParenLoc(LP),
888 SubExprs[INIT] = Init;
889 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
890 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
891 SubExprs[BODY] = Body;
894 /// \brief Build an empty for statement.
895 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
897 Stmt *getInit() { return SubExprs[INIT]; }
899 /// \brief Retrieve the variable declared in this "for" statement, if any.
901 /// In the following example, "y" is the condition variable.
903 /// for (int x = random(); int y = mangle(x); ++x) {
907 VarDecl *getConditionVariable() const { return CondVar; }
908 void setConditionVariable(VarDecl *V) { CondVar = V; }
910 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
911 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
912 Stmt *getBody() { return SubExprs[BODY]; }
914 const Stmt *getInit() const { return SubExprs[INIT]; }
915 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
916 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
917 const Stmt *getBody() const { return SubExprs[BODY]; }
919 void setInit(Stmt *S) { SubExprs[INIT] = S; }
920 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
921 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
922 void setBody(Stmt *S) { SubExprs[BODY] = S; }
924 SourceLocation getForLoc() const { return ForLoc; }
925 void setForLoc(SourceLocation L) { ForLoc = L; }
926 SourceLocation getLParenLoc() const { return LParenLoc; }
927 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
928 SourceLocation getRParenLoc() const { return RParenLoc; }
929 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
931 virtual SourceRange getSourceRange() const {
932 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
934 static bool classof(const Stmt *T) {
935 return T->getStmtClass() == ForStmtClass;
937 static bool classof(const ForStmt *) { return true; }
940 virtual child_iterator child_begin();
941 virtual child_iterator child_end();
944 virtual void DoDestroy(ASTContext &Ctx);
947 /// GotoStmt - This represents a direct goto.
949 class GotoStmt : public Stmt {
951 SourceLocation GotoLoc;
952 SourceLocation LabelLoc;
954 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
955 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
957 /// \brief Build an empty goto statement.
958 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
960 LabelStmt *getLabel() const { return Label; }
961 void setLabel(LabelStmt *S) { Label = S; }
963 SourceLocation getGotoLoc() const { return GotoLoc; }
964 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
965 SourceLocation getLabelLoc() const { return LabelLoc; }
966 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
968 virtual SourceRange getSourceRange() const {
969 return SourceRange(GotoLoc, LabelLoc);
971 static bool classof(const Stmt *T) {
972 return T->getStmtClass() == GotoStmtClass;
974 static bool classof(const GotoStmt *) { return true; }
977 virtual child_iterator child_begin();
978 virtual child_iterator child_end();
981 /// IndirectGotoStmt - This represents an indirect goto.
983 class IndirectGotoStmt : public Stmt {
984 SourceLocation GotoLoc;
985 SourceLocation StarLoc;
988 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
990 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
991 Target((Stmt*)target) {}
993 /// \brief Build an empty indirect goto statement.
994 explicit IndirectGotoStmt(EmptyShell Empty)
995 : Stmt(IndirectGotoStmtClass, Empty) { }
997 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
998 SourceLocation getGotoLoc() const { return GotoLoc; }
999 void setStarLoc(SourceLocation L) { StarLoc = L; }
1000 SourceLocation getStarLoc() const { return StarLoc; }
1003 const Expr *getTarget() const;
1004 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1006 virtual SourceRange getSourceRange() const {
1007 return SourceRange(GotoLoc, Target->getLocEnd());
1010 static bool classof(const Stmt *T) {
1011 return T->getStmtClass() == IndirectGotoStmtClass;
1013 static bool classof(const IndirectGotoStmt *) { return true; }
1016 virtual child_iterator child_begin();
1017 virtual child_iterator child_end();
1021 /// ContinueStmt - This represents a continue.
1023 class ContinueStmt : public Stmt {
1024 SourceLocation ContinueLoc;
1026 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1028 /// \brief Build an empty continue statement.
1029 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1031 SourceLocation getContinueLoc() const { return ContinueLoc; }
1032 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1034 virtual SourceRange getSourceRange() const {
1035 return SourceRange(ContinueLoc);
1038 static bool classof(const Stmt *T) {
1039 return T->getStmtClass() == ContinueStmtClass;
1041 static bool classof(const ContinueStmt *) { return true; }
1044 virtual child_iterator child_begin();
1045 virtual child_iterator child_end();
1048 /// BreakStmt - This represents a break.
1050 class BreakStmt : public Stmt {
1051 SourceLocation BreakLoc;
1053 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1055 /// \brief Build an empty break statement.
1056 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1058 SourceLocation getBreakLoc() const { return BreakLoc; }
1059 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1061 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
1063 static bool classof(const Stmt *T) {
1064 return T->getStmtClass() == BreakStmtClass;
1066 static bool classof(const BreakStmt *) { return true; }
1069 virtual child_iterator child_begin();
1070 virtual child_iterator child_end();
1074 /// ReturnStmt - This represents a return, optionally of an expression:
1078 /// Note that GCC allows return with no argument in a function declared to
1079 /// return a value, and it allows returning a value in functions declared to
1080 /// return void. We explicitly model this in the AST, which means you can't
1081 /// depend on the return type of the function and the presence of an argument.
1083 class ReturnStmt : public Stmt {
1085 SourceLocation RetLoc;
1087 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
1088 RetExpr((Stmt*) E), RetLoc(RL) {}
1090 /// \brief Build an empty return expression.
1091 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1093 const Expr *getRetValue() const;
1094 Expr *getRetValue();
1095 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1097 SourceLocation getReturnLoc() const { return RetLoc; }
1098 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1100 virtual SourceRange getSourceRange() const;
1102 static bool classof(const Stmt *T) {
1103 return T->getStmtClass() == ReturnStmtClass;
1105 static bool classof(const ReturnStmt *) { return true; }
1108 virtual child_iterator child_begin();
1109 virtual child_iterator child_end();
1112 /// AsmStmt - This represents a GNU inline-assembly statement extension.
1114 class AsmStmt : public Stmt {
1115 SourceLocation AsmLoc, RParenLoc;
1116 StringLiteral *AsmStr;
1122 unsigned NumOutputs;
1124 unsigned NumClobbers;
1126 // FIXME: If we wanted to, we could allocate all of these in one big array.
1127 IdentifierInfo **Names;
1128 StringLiteral **Constraints;
1130 StringLiteral **Clobbers;
1133 virtual void DoDestroy(ASTContext &Ctx);
1136 AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple, bool isvolatile,
1137 bool msasm, unsigned numoutputs, unsigned numinputs,
1138 IdentifierInfo **names, StringLiteral **constraints,
1139 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1140 StringLiteral **clobbers, SourceLocation rparenloc);
1142 /// \brief Build an empty inline-assembly statement.
1143 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty),
1144 Names(0), Constraints(0), Exprs(0), Clobbers(0) { }
1146 SourceLocation getAsmLoc() const { return AsmLoc; }
1147 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1148 SourceLocation getRParenLoc() const { return RParenLoc; }
1149 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1151 bool isVolatile() const { return IsVolatile; }
1152 void setVolatile(bool V) { IsVolatile = V; }
1153 bool isSimple() const { return IsSimple; }
1154 void setSimple(bool V) { IsSimple = V; }
1155 bool isMSAsm() const { return MSAsm; }
1156 void setMSAsm(bool V) { MSAsm = V; }
1158 //===--- Asm String Analysis ---===//
1160 const StringLiteral *getAsmString() const { return AsmStr; }
1161 StringLiteral *getAsmString() { return AsmStr; }
1162 void setAsmString(StringLiteral *E) { AsmStr = E; }
1164 /// AsmStringPiece - this is part of a decomposed asm string specification
1165 /// (for use with the AnalyzeAsmString function below). An asm string is
1166 /// considered to be a concatenation of these parts.
1167 class AsmStringPiece {
1170 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1171 Operand // Operand reference, with optional modifier %c4.
1178 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1179 AsmStringPiece(unsigned OpNo, char Modifier)
1180 : MyKind(Operand), Str(), OperandNo(OpNo) {
1184 bool isString() const { return MyKind == String; }
1185 bool isOperand() const { return MyKind == Operand; }
1187 const std::string &getString() const {
1192 unsigned getOperandNo() const {
1193 assert(isOperand());
1197 /// getModifier - Get the modifier for this operand, if present. This
1198 /// returns '\0' if there was no modifier.
1199 char getModifier() const {
1200 assert(isOperand());
1205 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1206 /// it into pieces. If the asm string is erroneous, emit errors and return
1207 /// true, otherwise return false. This handles canonicalization and
1208 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1209 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1210 unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1211 ASTContext &C, unsigned &DiagOffs) const;
1214 //===--- Output operands ---===//
1216 unsigned getNumOutputs() const { return NumOutputs; }
1218 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1222 llvm::StringRef getOutputName(unsigned i) const {
1223 if (IdentifierInfo *II = getOutputIdentifier(i))
1224 return II->getName();
1226 return llvm::StringRef();
1229 /// getOutputConstraint - Return the constraint string for the specified
1230 /// output operand. All output constraints are known to be non-empty (either
1232 llvm::StringRef getOutputConstraint(unsigned i) const;
1234 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1235 return Constraints[i];
1237 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1238 return Constraints[i];
1241 Expr *getOutputExpr(unsigned i);
1243 const Expr *getOutputExpr(unsigned i) const {
1244 return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1247 /// isOutputPlusConstraint - Return true if the specified output constraint
1248 /// is a "+" constraint (which is both an input and an output) or false if it
1249 /// is an "=" constraint (just an output).
1250 bool isOutputPlusConstraint(unsigned i) const {
1251 return getOutputConstraint(i)[0] == '+';
1254 /// getNumPlusOperands - Return the number of output operands that have a "+"
1256 unsigned getNumPlusOperands() const;
1258 //===--- Input operands ---===//
1260 unsigned getNumInputs() const { return NumInputs; }
1262 IdentifierInfo *getInputIdentifier(unsigned i) const {
1263 return Names[i + NumOutputs];
1266 llvm::StringRef getInputName(unsigned i) const {
1267 if (IdentifierInfo *II = getInputIdentifier(i))
1268 return II->getName();
1270 return llvm::StringRef();
1273 /// getInputConstraint - Return the specified input constraint. Unlike output
1274 /// constraints, these can be empty.
1275 llvm::StringRef getInputConstraint(unsigned i) const;
1277 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1278 return Constraints[i + NumOutputs];
1280 StringLiteral *getInputConstraintLiteral(unsigned i) {
1281 return Constraints[i + NumOutputs];
1284 Expr *getInputExpr(unsigned i);
1286 const Expr *getInputExpr(unsigned i) const {
1287 return const_cast<AsmStmt*>(this)->getInputExpr(i);
1290 void setOutputsAndInputsAndClobbers(ASTContext &C,
1291 IdentifierInfo **Names,
1292 StringLiteral **Constraints,
1294 unsigned NumOutputs,
1296 StringLiteral **Clobbers,
1297 unsigned NumClobbers);
1299 //===--- Other ---===//
1301 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1302 /// translate this into a numeric value needed to reference the same operand.
1303 /// This returns -1 if the operand name is invalid.
1304 int getNamedOperand(llvm::StringRef SymbolicName) const;
1306 unsigned getNumClobbers() const { return NumClobbers; }
1307 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1308 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1310 virtual SourceRange getSourceRange() const {
1311 return SourceRange(AsmLoc, RParenLoc);
1314 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1315 static bool classof(const AsmStmt *) { return true; }
1317 // Input expr iterators.
1319 typedef ExprIterator inputs_iterator;
1320 typedef ConstExprIterator const_inputs_iterator;
1322 inputs_iterator begin_inputs() {
1323 return &Exprs[0] + NumOutputs;
1326 inputs_iterator end_inputs() {
1327 return &Exprs[0] + NumOutputs + NumInputs;
1330 const_inputs_iterator begin_inputs() const {
1331 return &Exprs[0] + NumOutputs;
1334 const_inputs_iterator end_inputs() const {
1335 return &Exprs[0] + NumOutputs + NumInputs;
1338 // Output expr iterators.
1340 typedef ExprIterator outputs_iterator;
1341 typedef ConstExprIterator const_outputs_iterator;
1343 outputs_iterator begin_outputs() {
1346 outputs_iterator end_outputs() {
1347 return &Exprs[0] + NumOutputs;
1350 const_outputs_iterator begin_outputs() const {
1353 const_outputs_iterator end_outputs() const {
1354 return &Exprs[0] + NumOutputs;
1359 virtual child_iterator child_begin();
1360 virtual child_iterator child_end();
1363 } // end namespace clang