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 "llvm/ADT/SmallVector.h"
24 #include "clang/AST/ASTContext.h"
26 using llvm::dyn_cast_or_null;
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 {
74 ConstExprIterator(Stmt* const* i) : I(i) {}
75 ConstExprIterator() : I(0) {}
76 ConstExprIterator& operator++() { ++I; return *this; }
77 ConstExprIterator operator+(size_t i) { return I+i; }
78 ConstExprIterator operator-(size_t i) { 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 FIRST_STMT(CLASS) firstStmtConstant = CLASS##Class,
101 #define LAST_STMT(CLASS) lastStmtConstant = CLASS##Class,
102 #define FIRST_EXPR(CLASS) firstExprConstant = CLASS##Class,
103 #define LAST_EXPR(CLASS) lastExprConstant = CLASS##Class
104 #include "clang/AST/StmtNodes.def"
107 /// \brief The statement class.
108 const unsigned sClass : 8;
110 /// \brief The reference count for this statement.
111 unsigned RefCount : 24;
113 // Make vanilla 'new' and 'delete' illegal for Stmts.
115 void* operator new(size_t bytes) throw() {
116 assert(0 && "Stmts cannot be allocated with regular 'new'.");
119 void operator delete(void* data) throw() {
120 assert(0 && "Stmts cannot be released with regular 'delete'.");
124 // Only allow allocation of Stmts using the allocator in ASTContext
125 // or by doing a placement new.
126 void* operator new(size_t bytes, ASTContext& C,
127 unsigned alignment = 16) throw() {
128 return ::operator new(bytes, C, alignment);
131 void* operator new(size_t bytes, ASTContext* C,
132 unsigned alignment = 16) throw() {
133 return ::operator new(bytes, *C, alignment);
136 void* operator new(size_t bytes, void* mem) throw() {
140 void operator delete(void*, ASTContext&, unsigned) throw() { }
141 void operator delete(void*, ASTContext*, unsigned) throw() { }
142 void operator delete(void*, std::size_t) throw() { }
143 void operator delete(void*, void*) throw() { }
146 /// \brief A placeholder type used to construct an empty shell of a
147 /// type, that will be filled in later (e.g., by some
148 /// de-serialization).
149 struct EmptyShell { };
152 /// DestroyChildren - Invoked by destructors of subclasses of Stmt to
153 /// recursively release child AST nodes.
154 void DestroyChildren(ASTContext& Ctx);
156 /// \brief Construct an empty statement.
157 explicit Stmt(StmtClass SC, EmptyShell) : sClass(SC), RefCount(1) {
158 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
161 /// \brief Virtual method that performs the actual destruction of
164 /// Subclasses should override this method (not Destroy()) to
165 /// provide class-specific destruction.
166 virtual void DoDestroy(ASTContext &Ctx);
169 Stmt(StmtClass SC) : sClass(SC), RefCount(1) {
170 if (Stmt::CollectingStats()) Stmt::addStmtClass(SC);
175 /// \brief True if this statement's refcount is in a valid state.
176 /// Should be used only in assertions.
177 bool isRetained() const {
178 return (RefCount >= 1);
182 /// \brief Destroy the current statement and its children.
183 void Destroy(ASTContext &Ctx) {
184 assert(RefCount >= 1);
189 /// \brief Increases the reference count for this statement.
191 /// Invoke the Retain() operation when this statement or expression
192 /// is being shared by another owner.
194 assert(RefCount >= 1);
199 StmtClass getStmtClass() const {
200 assert(RefCount >= 1 && "Referencing already-destroyed statement!");
201 return (StmtClass)sClass;
203 const char *getStmtClassName() const;
205 /// SourceLocation tokens are not useful in isolation - they are low level
206 /// value objects created/interpreted by SourceManager. We assume AST
207 /// clients will have a pointer to the respective SourceManager.
208 virtual SourceRange getSourceRange() const = 0;
209 SourceLocation getLocStart() const { return getSourceRange().getBegin(); }
210 SourceLocation getLocEnd() const { return getSourceRange().getEnd(); }
212 // global temp stats (until we have a per-module visitor)
213 static void addStmtClass(const StmtClass s);
214 static bool CollectingStats(bool Enable = false);
215 static void PrintStats();
217 /// dump - This does a local dump of the specified AST fragment. It dumps the
218 /// specified node and a few nodes underneath it, but not the whole subtree.
219 /// This is useful in a debugger.
221 void dump(SourceManager &SM) const;
223 /// dumpAll - This does a dump of the specified AST fragment and all subtrees.
224 void dumpAll() const;
225 void dumpAll(SourceManager &SM) const;
227 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
228 /// back to its original source language syntax.
229 void dumpPretty(ASTContext& Context) const;
230 void printPretty(llvm::raw_ostream &OS, PrinterHelper *Helper,
231 const PrintingPolicy &Policy,
232 unsigned Indentation = 0) const {
233 printPretty(OS, *(ASTContext*)0, Helper, Policy, Indentation);
235 void printPretty(llvm::raw_ostream &OS, ASTContext &Context,
236 PrinterHelper *Helper,
237 const PrintingPolicy &Policy,
238 unsigned Indentation = 0) const;
240 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
241 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
242 void viewAST() const;
244 // Implement isa<T> support.
245 static bool classof(const Stmt *) { return true; }
247 /// hasImplicitControlFlow - Some statements (e.g. short circuited operations)
248 /// contain implicit control-flow in the order their subexpressions
249 /// are evaluated. This predicate returns true if this statement has
250 /// such implicit control-flow. Such statements are also specially handled
252 bool hasImplicitControlFlow() const;
254 /// Child Iterators: All subclasses must implement child_begin and child_end
255 /// to permit easy iteration over the substatements/subexpessions of an
256 /// AST node. This permits easy iteration over all nodes in the AST.
257 typedef StmtIterator child_iterator;
258 typedef ConstStmtIterator const_child_iterator;
260 virtual child_iterator child_begin() = 0;
261 virtual child_iterator child_end() = 0;
263 const_child_iterator child_begin() const {
264 return const_child_iterator(const_cast<Stmt*>(this)->child_begin());
267 const_child_iterator child_end() const {
268 return const_child_iterator(const_cast<Stmt*>(this)->child_end());
271 /// \brief Produce a unique representation of the given statement.
273 /// \brief ID once the profiling operation is complete, will contain
274 /// the unique representation of the given statement.
276 /// \brief Context the AST context in which the statement resides
278 /// \brief Canonical whether the profile should be based on the canonical
279 /// representation of this statement (e.g., where non-type template
280 /// parameters are identified by index/level rather than their
281 /// declaration pointers) or the exact representation of the statement as
282 /// written in the source.
283 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context,
287 /// DeclStmt - Adaptor class for mixing declarations with statements and
288 /// expressions. For example, CompoundStmt mixes statements, expressions
289 /// and declarations (variables, types). Another example is ForStmt, where
290 /// the first statement can be an expression or a declaration.
292 class DeclStmt : public Stmt {
294 SourceLocation StartLoc, EndLoc;
297 DeclStmt(DeclGroupRef dg, SourceLocation startLoc,
298 SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
299 StartLoc(startLoc), EndLoc(endLoc) {}
301 /// \brief Build an empty declaration statement.
302 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { }
304 /// isSingleDecl - This method returns true if this DeclStmt refers
305 /// to a single Decl.
306 bool isSingleDecl() const {
307 return DG.isSingleDecl();
310 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
311 Decl *getSingleDecl() { return DG.getSingleDecl(); }
313 const DeclGroupRef getDeclGroup() const { return DG; }
314 DeclGroupRef getDeclGroup() { return DG; }
315 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
317 SourceLocation getStartLoc() const { return StartLoc; }
318 void setStartLoc(SourceLocation L) { StartLoc = L; }
319 SourceLocation getEndLoc() const { return EndLoc; }
320 void setEndLoc(SourceLocation L) { EndLoc = L; }
322 SourceRange getSourceRange() const {
323 return SourceRange(StartLoc, EndLoc);
326 static bool classof(const Stmt *T) {
327 return T->getStmtClass() == DeclStmtClass;
329 static bool classof(const DeclStmt *) { return true; }
331 // Iterators over subexpressions.
332 virtual child_iterator child_begin();
333 virtual child_iterator child_end();
335 typedef DeclGroupRef::iterator decl_iterator;
336 typedef DeclGroupRef::const_iterator const_decl_iterator;
338 decl_iterator decl_begin() { return DG.begin(); }
339 decl_iterator decl_end() { return DG.end(); }
340 const_decl_iterator decl_begin() const { return DG.begin(); }
341 const_decl_iterator decl_end() const { return DG.end(); }
344 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
346 class NullStmt : public Stmt {
347 SourceLocation SemiLoc;
349 NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {}
351 /// \brief Build an empty null statement.
352 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) { }
354 SourceLocation getSemiLoc() const { return SemiLoc; }
355 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
357 virtual SourceRange getSourceRange() const { return SourceRange(SemiLoc); }
359 static bool classof(const Stmt *T) {
360 return T->getStmtClass() == NullStmtClass;
362 static bool classof(const NullStmt *) { return true; }
365 virtual child_iterator child_begin();
366 virtual child_iterator child_end();
369 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
371 class CompoundStmt : public Stmt {
374 SourceLocation LBracLoc, RBracLoc;
376 CompoundStmt(ASTContext& C, Stmt **StmtStart, unsigned numStmts,
377 SourceLocation LB, SourceLocation RB)
378 : Stmt(CompoundStmtClass), NumStmts(numStmts), LBracLoc(LB), RBracLoc(RB) {
384 Body = new (C) Stmt*[NumStmts];
385 memcpy(Body, StmtStart, numStmts * sizeof(*Body));
388 // \brief Build an empty compound statement.
389 explicit CompoundStmt(EmptyShell Empty)
390 : Stmt(CompoundStmtClass, Empty), Body(0), NumStmts(0) { }
392 void setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts);
394 bool body_empty() const { return NumStmts == 0; }
395 unsigned size() const { return NumStmts; }
397 typedef Stmt** body_iterator;
398 body_iterator body_begin() { return Body; }
399 body_iterator body_end() { return Body + NumStmts; }
400 Stmt *body_back() { return NumStmts ? Body[NumStmts-1] : 0; }
402 typedef Stmt* const * const_body_iterator;
403 const_body_iterator body_begin() const { return Body; }
404 const_body_iterator body_end() const { return Body + NumStmts; }
405 const Stmt *body_back() const { return NumStmts ? Body[NumStmts-1] : 0; }
407 typedef std::reverse_iterator<body_iterator> reverse_body_iterator;
408 reverse_body_iterator body_rbegin() {
409 return reverse_body_iterator(body_end());
411 reverse_body_iterator body_rend() {
412 return reverse_body_iterator(body_begin());
415 typedef std::reverse_iterator<const_body_iterator>
416 const_reverse_body_iterator;
418 const_reverse_body_iterator body_rbegin() const {
419 return const_reverse_body_iterator(body_end());
422 const_reverse_body_iterator body_rend() const {
423 return const_reverse_body_iterator(body_begin());
426 virtual SourceRange getSourceRange() const {
427 return SourceRange(LBracLoc, RBracLoc);
430 SourceLocation getLBracLoc() const { return LBracLoc; }
431 void setLBracLoc(SourceLocation L) { LBracLoc = L; }
432 SourceLocation getRBracLoc() const { return RBracLoc; }
433 void setRBracLoc(SourceLocation L) { RBracLoc = L; }
435 static bool classof(const Stmt *T) {
436 return T->getStmtClass() == CompoundStmtClass;
438 static bool classof(const CompoundStmt *) { return true; }
441 virtual child_iterator child_begin();
442 virtual child_iterator child_end();
445 // SwitchCase is the base class for CaseStmt and DefaultStmt,
446 class SwitchCase : public Stmt {
448 // A pointer to the following CaseStmt or DefaultStmt class,
449 // used by SwitchStmt.
450 SwitchCase *NextSwitchCase;
452 SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {}
455 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
457 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
459 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
461 Stmt *getSubStmt() { return v_getSubStmt(); }
463 virtual SourceRange getSourceRange() const { return SourceRange(); }
465 static bool classof(const Stmt *T) {
466 return T->getStmtClass() == CaseStmtClass ||
467 T->getStmtClass() == DefaultStmtClass;
469 static bool classof(const SwitchCase *) { return true; }
471 virtual Stmt* v_getSubStmt() = 0;
474 class CaseStmt : public SwitchCase {
475 enum { SUBSTMT, LHS, RHS, END_EXPR };
476 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
477 // GNU "case 1 ... 4" extension
478 SourceLocation CaseLoc;
479 SourceLocation EllipsisLoc;
480 SourceLocation ColonLoc;
482 virtual Stmt* v_getSubStmt() { return getSubStmt(); }
484 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
485 SourceLocation ellipsisLoc, SourceLocation colonLoc)
486 : SwitchCase(CaseStmtClass) {
487 SubExprs[SUBSTMT] = 0;
488 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
489 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
491 EllipsisLoc = ellipsisLoc;
495 /// \brief Build an empty switch case statement.
496 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { }
498 SourceLocation getCaseLoc() const { return CaseLoc; }
499 void setCaseLoc(SourceLocation L) { CaseLoc = L; }
500 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
501 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
502 SourceLocation getColonLoc() const { return ColonLoc; }
503 void setColonLoc(SourceLocation L) { ColonLoc = L; }
505 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
506 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
507 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
509 const Expr *getLHS() const {
510 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
512 const Expr *getRHS() const {
513 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
515 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
517 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
518 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
519 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
522 virtual SourceRange getSourceRange() const {
523 // Handle deeply nested case statements with iteration instead of recursion.
524 const CaseStmt *CS = this;
525 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
528 return SourceRange(CaseLoc, CS->getSubStmt()->getLocEnd());
530 static bool classof(const Stmt *T) {
531 return T->getStmtClass() == CaseStmtClass;
533 static bool classof(const CaseStmt *) { return true; }
536 virtual child_iterator child_begin();
537 virtual child_iterator child_end();
540 class DefaultStmt : public SwitchCase {
542 SourceLocation DefaultLoc;
543 SourceLocation ColonLoc;
544 virtual Stmt* v_getSubStmt() { return getSubStmt(); }
546 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
547 SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL),
550 /// \brief Build an empty default statement.
551 explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { }
553 Stmt *getSubStmt() { return SubStmt; }
554 const Stmt *getSubStmt() const { return SubStmt; }
555 void setSubStmt(Stmt *S) { SubStmt = S; }
557 SourceLocation getDefaultLoc() const { return DefaultLoc; }
558 void setDefaultLoc(SourceLocation L) { DefaultLoc = L; }
559 SourceLocation getColonLoc() const { return ColonLoc; }
560 void setColonLoc(SourceLocation L) { ColonLoc = L; }
562 virtual SourceRange getSourceRange() const {
563 return SourceRange(DefaultLoc, SubStmt->getLocEnd());
565 static bool classof(const Stmt *T) {
566 return T->getStmtClass() == DefaultStmtClass;
568 static bool classof(const DefaultStmt *) { return true; }
571 virtual child_iterator child_begin();
572 virtual child_iterator child_end();
575 class LabelStmt : public Stmt {
576 IdentifierInfo *Label;
578 SourceLocation IdentLoc;
580 LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt)
581 : Stmt(LabelStmtClass), Label(label),
582 SubStmt(substmt), IdentLoc(IL) {}
584 // \brief Build an empty label statement.
585 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) { }
587 SourceLocation getIdentLoc() const { return IdentLoc; }
588 IdentifierInfo *getID() const { return Label; }
589 void setID(IdentifierInfo *II) { Label = II; }
590 const char *getName() const;
591 Stmt *getSubStmt() { return SubStmt; }
592 const Stmt *getSubStmt() const { return SubStmt; }
593 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
594 void setSubStmt(Stmt *SS) { SubStmt = SS; }
596 virtual SourceRange getSourceRange() const {
597 return SourceRange(IdentLoc, SubStmt->getLocEnd());
599 static bool classof(const Stmt *T) {
600 return T->getStmtClass() == LabelStmtClass;
602 static bool classof(const LabelStmt *) { return true; }
605 virtual child_iterator child_begin();
606 virtual child_iterator child_end();
610 /// IfStmt - This represents an if/then/else.
612 class IfStmt : public Stmt {
613 enum { COND, THEN, ELSE, END_EXPR };
614 Stmt* SubExprs[END_EXPR];
616 /// \brief If non-NULL, the declaration in the "if" statement.
619 SourceLocation IfLoc;
620 SourceLocation ElseLoc;
623 IfStmt(SourceLocation IL, VarDecl *Var, Expr *cond, Stmt *then,
624 SourceLocation EL = SourceLocation(), Stmt *elsev = 0)
625 : Stmt(IfStmtClass), Var(Var), IfLoc(IL), ElseLoc(EL) {
626 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
627 SubExprs[THEN] = then;
628 SubExprs[ELSE] = elsev;
631 /// \brief Build an empty if/then/else statement
632 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) { }
634 /// \brief Retrieve the variable declared in this "if" statement, if any.
636 /// In the following example, "x" is the condition variable.
638 /// if (int x = foo()) {
639 /// printf("x is %d", x);
642 VarDecl *getConditionVariable() const { return Var; }
643 void setConditionVariable(VarDecl *V) { Var = V; }
645 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
646 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
647 const Stmt *getThen() const { return SubExprs[THEN]; }
648 void setThen(Stmt *S) { SubExprs[THEN] = S; }
649 const Stmt *getElse() const { return SubExprs[ELSE]; }
650 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
652 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
653 Stmt *getThen() { return SubExprs[THEN]; }
654 Stmt *getElse() { return SubExprs[ELSE]; }
656 SourceLocation getIfLoc() const { return IfLoc; }
657 void setIfLoc(SourceLocation L) { IfLoc = L; }
658 SourceLocation getElseLoc() const { return ElseLoc; }
659 void setElseLoc(SourceLocation L) { ElseLoc = L; }
661 virtual SourceRange getSourceRange() const {
663 return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd());
665 return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd());
668 static bool classof(const Stmt *T) {
669 return T->getStmtClass() == IfStmtClass;
671 static bool classof(const IfStmt *) { return true; }
674 virtual child_iterator child_begin();
675 virtual child_iterator child_end();
678 /// SwitchStmt - This represents a 'switch' stmt.
680 class SwitchStmt : public Stmt {
681 enum { COND, BODY, END_EXPR };
682 Stmt* SubExprs[END_EXPR];
684 // This points to a linked list of case and default statements.
685 SwitchCase *FirstCase;
686 SourceLocation SwitchLoc;
689 virtual void DoDestroy(ASTContext &Ctx);
692 SwitchStmt(VarDecl *Var, Expr *cond)
693 : Stmt(SwitchStmtClass), Var(Var), FirstCase(0)
695 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
696 SubExprs[BODY] = NULL;
699 /// \brief Build a empty switch statement.
700 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) { }
702 /// \brief Retrieve the variable declared in this "switch" statement, if any.
704 /// In the following example, "x" is the condition variable.
706 /// switch (int x = foo()) {
711 VarDecl *getConditionVariable() const { return Var; }
712 void setConditionVariable(VarDecl *V) { Var = V; }
714 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
715 const Stmt *getBody() const { return SubExprs[BODY]; }
716 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
718 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
719 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
720 Stmt *getBody() { return SubExprs[BODY]; }
721 void setBody(Stmt *S) { SubExprs[BODY] = S; }
722 SwitchCase *getSwitchCaseList() { return FirstCase; }
724 /// \brief Set the case list for this switch statement.
726 /// The caller is responsible for incrementing the retain counts on
727 /// all of the SwitchCase statements in this list.
728 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
730 SourceLocation getSwitchLoc() const { return SwitchLoc; }
731 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
733 void setBody(Stmt *S, SourceLocation SL) {
737 void addSwitchCase(SwitchCase *SC) {
738 assert(!SC->getNextSwitchCase() && "case/default already added to a switch");
740 SC->setNextSwitchCase(FirstCase);
743 virtual SourceRange getSourceRange() const {
744 return SourceRange(SwitchLoc, SubExprs[BODY]->getLocEnd());
746 static bool classof(const Stmt *T) {
747 return T->getStmtClass() == SwitchStmtClass;
749 static bool classof(const SwitchStmt *) { return true; }
752 virtual child_iterator child_begin();
753 virtual child_iterator child_end();
757 /// WhileStmt - This represents a 'while' stmt.
759 class WhileStmt : public Stmt {
760 enum { COND, BODY, END_EXPR };
762 Stmt* SubExprs[END_EXPR];
763 SourceLocation WhileLoc;
765 WhileStmt(VarDecl *Var, Expr *cond, Stmt *body, SourceLocation WL)
766 : Stmt(WhileStmtClass), Var(Var)
768 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
769 SubExprs[BODY] = body;
773 /// \brief Build an empty while statement.
774 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) { }
776 /// \brief Retrieve the variable declared in this "while" statement, if any.
778 /// In the following example, "x" is the condition variable.
780 /// while (int x = random()) {
784 VarDecl *getConditionVariable() const { return Var; }
785 void setConditionVariable(VarDecl *V) { Var = V; }
787 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
788 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
789 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
790 Stmt *getBody() { return SubExprs[BODY]; }
791 const Stmt *getBody() const { return SubExprs[BODY]; }
792 void setBody(Stmt *S) { SubExprs[BODY] = S; }
794 SourceLocation getWhileLoc() const { return WhileLoc; }
795 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
797 virtual SourceRange getSourceRange() const {
798 return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd());
800 static bool classof(const Stmt *T) {
801 return T->getStmtClass() == WhileStmtClass;
803 static bool classof(const WhileStmt *) { return true; }
806 virtual child_iterator child_begin();
807 virtual child_iterator child_end();
810 /// DoStmt - This represents a 'do/while' stmt.
812 class DoStmt : public Stmt {
813 enum { COND, BODY, END_EXPR };
814 Stmt* SubExprs[END_EXPR];
815 SourceLocation DoLoc;
816 SourceLocation WhileLoc;
817 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
820 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
822 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
823 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
824 SubExprs[BODY] = body;
827 /// \brief Build an empty do-while statement.
828 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
830 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
831 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
832 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
833 Stmt *getBody() { return SubExprs[BODY]; }
834 const Stmt *getBody() const { return SubExprs[BODY]; }
835 void setBody(Stmt *S) { SubExprs[BODY] = S; }
837 SourceLocation getDoLoc() const { return DoLoc; }
838 void setDoLoc(SourceLocation L) { DoLoc = L; }
839 SourceLocation getWhileLoc() const { return WhileLoc; }
840 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
842 SourceLocation getRParenLoc() const { return RParenLoc; }
843 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
845 virtual SourceRange getSourceRange() const {
846 return SourceRange(DoLoc, RParenLoc);
848 static bool classof(const Stmt *T) {
849 return T->getStmtClass() == DoStmtClass;
851 static bool classof(const DoStmt *) { return true; }
854 virtual child_iterator child_begin();
855 virtual child_iterator child_end();
859 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
860 /// the init/cond/inc parts of the ForStmt will be null if they were not
861 /// specified in the source.
863 class ForStmt : public Stmt {
864 enum { INIT, COND, INC, BODY, END_EXPR };
865 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
867 SourceLocation ForLoc;
868 SourceLocation LParenLoc, RParenLoc;
871 ForStmt(Stmt *Init, Expr *Cond, VarDecl *CondVar, Expr *Inc, Stmt *Body,
872 SourceLocation FL, SourceLocation LP, SourceLocation RP)
873 : Stmt(ForStmtClass), CondVar(CondVar), ForLoc(FL), LParenLoc(LP),
876 SubExprs[INIT] = Init;
877 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
878 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
879 SubExprs[BODY] = Body;
882 /// \brief Build an empty for statement.
883 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
885 Stmt *getInit() { return SubExprs[INIT]; }
887 /// \brief Retrieve the variable declared in this "for" statement, if any.
889 /// In the following example, "y" is the condition variable.
891 /// for (int x = random(); int y = mangle(x); ++x) {
895 VarDecl *getConditionVariable() const { return CondVar; }
896 void setConditionVariable(VarDecl *V) { CondVar = V; }
898 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
899 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
900 Stmt *getBody() { return SubExprs[BODY]; }
902 const Stmt *getInit() const { return SubExprs[INIT]; }
903 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
904 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
905 const Stmt *getBody() const { return SubExprs[BODY]; }
907 void setInit(Stmt *S) { SubExprs[INIT] = S; }
908 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
909 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
910 void setBody(Stmt *S) { SubExprs[BODY] = S; }
912 SourceLocation getForLoc() const { return ForLoc; }
913 void setForLoc(SourceLocation L) { ForLoc = L; }
914 SourceLocation getLParenLoc() const { return LParenLoc; }
915 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
916 SourceLocation getRParenLoc() const { return RParenLoc; }
917 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
919 virtual SourceRange getSourceRange() const {
920 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
922 static bool classof(const Stmt *T) {
923 return T->getStmtClass() == ForStmtClass;
925 static bool classof(const ForStmt *) { return true; }
928 virtual child_iterator child_begin();
929 virtual child_iterator child_end();
932 /// GotoStmt - This represents a direct goto.
934 class GotoStmt : public Stmt {
936 SourceLocation GotoLoc;
937 SourceLocation LabelLoc;
939 GotoStmt(LabelStmt *label, SourceLocation GL, SourceLocation LL)
940 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
942 /// \brief Build an empty goto statement.
943 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) { }
945 LabelStmt *getLabel() const { return Label; }
946 void setLabel(LabelStmt *S) { Label = S; }
948 SourceLocation getGotoLoc() const { return GotoLoc; }
949 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
950 SourceLocation getLabelLoc() const { return LabelLoc; }
951 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
953 virtual SourceRange getSourceRange() const {
954 return SourceRange(GotoLoc, LabelLoc);
956 static bool classof(const Stmt *T) {
957 return T->getStmtClass() == GotoStmtClass;
959 static bool classof(const GotoStmt *) { return true; }
962 virtual child_iterator child_begin();
963 virtual child_iterator child_end();
966 /// IndirectGotoStmt - This represents an indirect goto.
968 class IndirectGotoStmt : public Stmt {
969 SourceLocation GotoLoc;
970 SourceLocation StarLoc;
973 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
975 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
976 Target((Stmt*)target) {}
978 /// \brief Build an empty indirect goto statement.
979 explicit IndirectGotoStmt(EmptyShell Empty)
980 : Stmt(IndirectGotoStmtClass, Empty) { }
982 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
983 SourceLocation getGotoLoc() const { return GotoLoc; }
984 void setStarLoc(SourceLocation L) { StarLoc = L; }
985 SourceLocation getStarLoc() const { return StarLoc; }
988 const Expr *getTarget() const;
989 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
991 virtual SourceRange getSourceRange() const {
992 return SourceRange(GotoLoc, Target->getLocEnd());
995 static bool classof(const Stmt *T) {
996 return T->getStmtClass() == IndirectGotoStmtClass;
998 static bool classof(const IndirectGotoStmt *) { return true; }
1001 virtual child_iterator child_begin();
1002 virtual child_iterator child_end();
1006 /// ContinueStmt - This represents a continue.
1008 class ContinueStmt : public Stmt {
1009 SourceLocation ContinueLoc;
1011 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1013 /// \brief Build an empty continue statement.
1014 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) { }
1016 SourceLocation getContinueLoc() const { return ContinueLoc; }
1017 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1019 virtual SourceRange getSourceRange() const {
1020 return SourceRange(ContinueLoc);
1023 static bool classof(const Stmt *T) {
1024 return T->getStmtClass() == ContinueStmtClass;
1026 static bool classof(const ContinueStmt *) { return true; }
1029 virtual child_iterator child_begin();
1030 virtual child_iterator child_end();
1033 /// BreakStmt - This represents a break.
1035 class BreakStmt : public Stmt {
1036 SourceLocation BreakLoc;
1038 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {}
1040 /// \brief Build an empty break statement.
1041 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) { }
1043 SourceLocation getBreakLoc() const { return BreakLoc; }
1044 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1046 virtual SourceRange getSourceRange() const { return SourceRange(BreakLoc); }
1048 static bool classof(const Stmt *T) {
1049 return T->getStmtClass() == BreakStmtClass;
1051 static bool classof(const BreakStmt *) { return true; }
1054 virtual child_iterator child_begin();
1055 virtual child_iterator child_end();
1059 /// ReturnStmt - This represents a return, optionally of an expression:
1063 /// Note that GCC allows return with no argument in a function declared to
1064 /// return a value, and it allows returning a value in functions declared to
1065 /// return void. We explicitly model this in the AST, which means you can't
1066 /// depend on the return type of the function and the presence of an argument.
1068 class ReturnStmt : public Stmt {
1070 SourceLocation RetLoc;
1072 ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass),
1073 RetExpr((Stmt*) E), RetLoc(RL) {}
1075 /// \brief Build an empty return expression.
1076 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { }
1078 const Expr *getRetValue() const;
1079 Expr *getRetValue();
1080 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1082 SourceLocation getReturnLoc() const { return RetLoc; }
1083 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1085 virtual SourceRange getSourceRange() const;
1087 static bool classof(const Stmt *T) {
1088 return T->getStmtClass() == ReturnStmtClass;
1090 static bool classof(const ReturnStmt *) { return true; }
1093 virtual child_iterator child_begin();
1094 virtual child_iterator child_end();
1097 /// AsmStmt - This represents a GNU inline-assembly statement extension.
1099 class AsmStmt : public Stmt {
1100 SourceLocation AsmLoc, RParenLoc;
1101 StringLiteral *AsmStr;
1106 unsigned NumOutputs;
1109 llvm::SmallVector<std::string, 4> Names;
1110 llvm::SmallVector<StringLiteral*, 4> Constraints;
1111 llvm::SmallVector<Stmt*, 4> Exprs;
1113 llvm::SmallVector<StringLiteral*, 4> Clobbers;
1115 AsmStmt(SourceLocation asmloc, bool issimple, bool isvolatile,
1116 unsigned numoutputs, unsigned numinputs,
1117 std::string *names, StringLiteral **constraints,
1118 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
1119 StringLiteral **clobbers, SourceLocation rparenloc);
1121 /// \brief Build an empty inline-assembly statement.
1122 explicit AsmStmt(EmptyShell Empty) : Stmt(AsmStmtClass, Empty) { }
1124 SourceLocation getAsmLoc() const { return AsmLoc; }
1125 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1126 SourceLocation getRParenLoc() const { return RParenLoc; }
1127 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1129 bool isVolatile() const { return IsVolatile; }
1130 void setVolatile(bool V) { IsVolatile = V; }
1131 bool isSimple() const { return IsSimple; }
1132 void setSimple(bool V) { IsSimple = false; }
1134 //===--- Asm String Analysis ---===//
1136 const StringLiteral *getAsmString() const { return AsmStr; }
1137 StringLiteral *getAsmString() { return AsmStr; }
1138 void setAsmString(StringLiteral *E) { AsmStr = E; }
1140 /// AsmStringPiece - this is part of a decomposed asm string specification
1141 /// (for use with the AnalyzeAsmString function below). An asm string is
1142 /// considered to be a concatenation of these parts.
1143 class AsmStringPiece {
1146 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1147 Operand // Operand reference, with optional modifier %c4.
1154 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1155 AsmStringPiece(unsigned OpNo, char Modifier)
1156 : MyKind(Operand), Str(), OperandNo(OpNo) {
1160 bool isString() const { return MyKind == String; }
1161 bool isOperand() const { return MyKind == Operand; }
1163 const std::string &getString() const {
1168 unsigned getOperandNo() const {
1169 assert(isOperand());
1173 /// getModifier - Get the modifier for this operand, if present. This
1174 /// returns '\0' if there was no modifier.
1175 char getModifier() const {
1176 assert(isOperand());
1181 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1182 /// it into pieces. If the asm string is erroneous, emit errors and return
1183 /// true, otherwise return false. This handles canonicalization and
1184 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1185 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1186 unsigned AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece> &Pieces,
1187 ASTContext &C, unsigned &DiagOffs) const;
1190 //===--- Output operands ---===//
1192 unsigned getNumOutputs() const { return NumOutputs; }
1194 const std::string &getOutputName(unsigned i) const {
1198 /// getOutputConstraint - Return the constraint string for the specified
1199 /// output operand. All output constraints are known to be non-empty (either
1201 std::string getOutputConstraint(unsigned i) const;
1203 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1204 return Constraints[i];
1206 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1207 return Constraints[i];
1211 Expr *getOutputExpr(unsigned i);
1213 const Expr *getOutputExpr(unsigned i) const {
1214 return const_cast<AsmStmt*>(this)->getOutputExpr(i);
1217 /// isOutputPlusConstraint - Return true if the specified output constraint
1218 /// is a "+" constraint (which is both an input and an output) or false if it
1219 /// is an "=" constraint (just an output).
1220 bool isOutputPlusConstraint(unsigned i) const {
1221 return getOutputConstraint(i)[0] == '+';
1224 /// getNumPlusOperands - Return the number of output operands that have a "+"
1226 unsigned getNumPlusOperands() const;
1228 //===--- Input operands ---===//
1230 unsigned getNumInputs() const { return NumInputs; }
1232 const std::string &getInputName(unsigned i) const {
1233 return Names[i + NumOutputs];
1236 /// getInputConstraint - Return the specified input constraint. Unlike output
1237 /// constraints, these can be empty.
1238 std::string getInputConstraint(unsigned i) const;
1240 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1241 return Constraints[i + NumOutputs];
1243 StringLiteral *getInputConstraintLiteral(unsigned i) {
1244 return Constraints[i + NumOutputs];
1248 Expr *getInputExpr(unsigned i);
1250 const Expr *getInputExpr(unsigned i) const {
1251 return const_cast<AsmStmt*>(this)->getInputExpr(i);
1254 void setOutputsAndInputs(unsigned NumOutputs,
1256 const std::string *Names,
1257 StringLiteral **Constraints,
1260 //===--- Other ---===//
1262 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1263 /// translate this into a numeric value needed to reference the same operand.
1264 /// This returns -1 if the operand name is invalid.
1265 int getNamedOperand(const std::string &SymbolicName) const;
1269 unsigned getNumClobbers() const { return Clobbers.size(); }
1270 StringLiteral *getClobber(unsigned i) { return Clobbers[i]; }
1271 const StringLiteral *getClobber(unsigned i) const { return Clobbers[i]; }
1272 void setClobbers(StringLiteral **Clobbers, unsigned NumClobbers);
1274 virtual SourceRange getSourceRange() const {
1275 return SourceRange(AsmLoc, RParenLoc);
1278 static bool classof(const Stmt *T) {return T->getStmtClass() == AsmStmtClass;}
1279 static bool classof(const AsmStmt *) { return true; }
1281 // Input expr iterators.
1283 typedef ExprIterator inputs_iterator;
1284 typedef ConstExprIterator const_inputs_iterator;
1286 inputs_iterator begin_inputs() {
1287 return Exprs.data() + NumOutputs;
1290 inputs_iterator end_inputs() {
1291 return Exprs.data() + NumOutputs + NumInputs;
1294 const_inputs_iterator begin_inputs() const {
1295 return Exprs.data() + NumOutputs;
1298 const_inputs_iterator end_inputs() const {
1299 return Exprs.data() + NumOutputs + NumInputs;
1302 // Output expr iterators.
1304 typedef ExprIterator outputs_iterator;
1305 typedef ConstExprIterator const_outputs_iterator;
1307 outputs_iterator begin_outputs() {
1308 return Exprs.data();
1310 outputs_iterator end_outputs() {
1311 return Exprs.data() + NumOutputs;
1314 const_outputs_iterator begin_outputs() const {
1315 return Exprs.data();
1317 const_outputs_iterator end_outputs() const {
1318 return Exprs.data() + NumOutputs;
1321 // Input name iterator.
1323 const std::string *begin_output_names() const {
1327 const std::string *end_output_names() const {
1328 return &Names[0] + NumOutputs;
1333 virtual child_iterator child_begin();
1334 virtual child_iterator child_end();
1337 } // end namespace clang