1 //===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
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 implements the Stmt class and statement subclasses.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/Stmt.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/AST/ExprObjC.h"
17 #include "clang/AST/StmtCXX.h"
18 #include "clang/AST/StmtObjC.h"
19 #include "clang/AST/Type.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/ASTDiagnostic.h"
22 #include "clang/Basic/TargetInfo.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
26 static struct StmtClassNameTable {
30 } StmtClassInfo[Stmt::lastStmtConstant+1];
32 static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
33 static bool Initialized = false;
35 return StmtClassInfo[E];
37 // Intialize the table on the first use.
39 #define ABSTRACT_STMT(STMT)
40 #define STMT(CLASS, PARENT) \
41 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
42 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
43 #include "clang/AST/StmtNodes.inc"
45 return StmtClassInfo[E];
48 const char *Stmt::getStmtClassName() const {
49 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
52 void Stmt::PrintStats() {
53 // Ensure the table is primed.
54 getStmtInfoTableEntry(Stmt::NullStmtClass);
57 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
58 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
59 if (StmtClassInfo[i].Name == 0) continue;
60 sum += StmtClassInfo[i].Counter;
62 llvm::errs() << " " << sum << " stmts/exprs total.\n";
64 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
65 if (StmtClassInfo[i].Name == 0) continue;
66 if (StmtClassInfo[i].Counter == 0) continue;
67 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
68 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
69 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
71 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
74 llvm::errs() << "Total bytes = " << sum << "\n";
77 void Stmt::addStmtClass(StmtClass s) {
78 ++getStmtInfoTableEntry(s).Counter;
81 bool Stmt::StatisticsEnabled = false;
82 void Stmt::EnableStatistics() {
83 StatisticsEnabled = true;
86 Stmt *Stmt::IgnoreImplicit() {
89 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
90 s = ewc->getSubExpr();
92 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
93 s = ice->getSubExpr();
98 /// \brief Strip off all label-like statements.
100 /// This will strip off label statements, case statements, and default
101 /// statements recursively.
102 const Stmt *Stmt::stripLabelLikeStatements() const {
103 const Stmt *S = this;
105 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
106 S = LS->getSubStmt();
107 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
108 S = SC->getSubStmt();
118 // These silly little functions have to be static inline to suppress
119 // unused warnings, and they have to be defined to suppress other
121 static inline good is_good(good) { return good(); }
123 typedef Stmt::child_range children_t();
124 template <class T> good implements_children(children_t T::*) {
127 static inline bad implements_children(children_t Stmt::*) {
131 typedef SourceRange getSourceRange_t() const;
132 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
135 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
139 #define ASSERT_IMPLEMENTS_children(type) \
140 (void) sizeof(is_good(implements_children(&type::children)))
141 #define ASSERT_IMPLEMENTS_getSourceRange(type) \
142 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
145 /// Check whether the various Stmt classes implement their member
147 static inline void check_implementations() {
148 #define ABSTRACT_STMT(type)
149 #define STMT(type, base) \
150 ASSERT_IMPLEMENTS_children(type); \
151 ASSERT_IMPLEMENTS_getSourceRange(type);
152 #include "clang/AST/StmtNodes.inc"
155 Stmt::child_range Stmt::children() {
156 switch (getStmtClass()) {
157 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
158 #define ABSTRACT_STMT(type)
159 #define STMT(type, base) \
160 case Stmt::type##Class: \
161 return static_cast<type*>(this)->children();
162 #include "clang/AST/StmtNodes.inc"
164 llvm_unreachable("unknown statement kind!");
167 SourceRange Stmt::getSourceRange() const {
168 switch (getStmtClass()) {
169 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
170 #define ABSTRACT_STMT(type)
171 #define STMT(type, base) \
172 case Stmt::type##Class: \
173 return static_cast<const type*>(this)->getSourceRange();
174 #include "clang/AST/StmtNodes.inc"
176 llvm_unreachable("unknown statement kind!");
179 // Amusing macro metaprogramming hack: check whether a class provides
180 // a more specific implementation of getLocStart() and getLocEnd().
182 // See also Expr.cpp:getExprLoc().
184 /// This implementation is used when a class provides a custom
185 /// implementation of getLocStart.
186 template <class S, class T>
187 SourceLocation getLocStartImpl(const Stmt *stmt,
188 SourceLocation (T::*v)() const) {
189 return static_cast<const S*>(stmt)->getLocStart();
192 /// This implementation is used when a class doesn't provide a custom
193 /// implementation of getLocStart. Overload resolution should pick it over
194 /// the implementation above because it's more specialized according to
195 /// function template partial ordering.
197 SourceLocation getLocStartImpl(const Stmt *stmt,
198 SourceLocation (Stmt::*v)() const) {
199 return static_cast<const S*>(stmt)->getSourceRange().getBegin();
202 /// This implementation is used when a class provides a custom
203 /// implementation of getLocEnd.
204 template <class S, class T>
205 SourceLocation getLocEndImpl(const Stmt *stmt,
206 SourceLocation (T::*v)() const) {
207 return static_cast<const S*>(stmt)->getLocEnd();
210 /// This implementation is used when a class doesn't provide a custom
211 /// implementation of getLocEnd. Overload resolution should pick it over
212 /// the implementation above because it's more specialized according to
213 /// function template partial ordering.
215 SourceLocation getLocEndImpl(const Stmt *stmt,
216 SourceLocation (Stmt::*v)() const) {
217 return static_cast<const S*>(stmt)->getSourceRange().getEnd();
221 SourceLocation Stmt::getLocStart() const {
222 switch (getStmtClass()) {
223 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
224 #define ABSTRACT_STMT(type)
225 #define STMT(type, base) \
226 case Stmt::type##Class: \
227 return getLocStartImpl<type>(this, &type::getLocStart);
228 #include "clang/AST/StmtNodes.inc"
230 llvm_unreachable("unknown statement kind");
233 SourceLocation Stmt::getLocEnd() const {
234 switch (getStmtClass()) {
235 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
236 #define ABSTRACT_STMT(type)
237 #define STMT(type, base) \
238 case Stmt::type##Class: \
239 return getLocEndImpl<type>(this, &type::getLocEnd);
240 #include "clang/AST/StmtNodes.inc"
242 llvm_unreachable("unknown statement kind");
245 void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
248 this->CompoundStmtBits.NumStmts = NumStmts;
250 Body = new (C) Stmt*[NumStmts];
251 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
254 const char *LabelStmt::getName() const {
255 return getDecl()->getIdentifier()->getNameStart();
258 // This is defined here to avoid polluting Stmt.h with importing Expr.h
259 SourceRange ReturnStmt::getSourceRange() const {
261 return SourceRange(RetLoc, RetExpr->getLocEnd());
263 return SourceRange(RetLoc);
266 bool Stmt::hasImplicitControlFlow() const {
267 switch (StmtBits.sClass) {
272 case ConditionalOperatorClass:
273 case ChooseExprClass:
278 case Stmt::BinaryOperatorClass: {
279 const BinaryOperator* B = cast<BinaryOperator>(this);
280 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
288 Expr *AsmStmt::getOutputExpr(unsigned i) {
289 return cast<Expr>(Exprs[i]);
292 /// getOutputConstraint - Return the constraint string for the specified
293 /// output operand. All output constraints are known to be non-empty (either
295 StringRef AsmStmt::getOutputConstraint(unsigned i) const {
296 return getOutputConstraintLiteral(i)->getString();
299 /// getNumPlusOperands - Return the number of output operands that have a "+"
301 unsigned AsmStmt::getNumPlusOperands() const {
303 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
304 if (isOutputPlusConstraint(i))
309 Expr *AsmStmt::getInputExpr(unsigned i) {
310 return cast<Expr>(Exprs[i + NumOutputs]);
312 void AsmStmt::setInputExpr(unsigned i, Expr *E) {
313 Exprs[i + NumOutputs] = E;
317 /// getInputConstraint - Return the specified input constraint. Unlike output
318 /// constraints, these can be empty.
319 StringRef AsmStmt::getInputConstraint(unsigned i) const {
320 return getInputConstraintLiteral(i)->getString();
324 void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
325 IdentifierInfo **Names,
326 StringLiteral **Constraints,
330 StringLiteral **Clobbers,
331 unsigned NumClobbers) {
332 this->NumOutputs = NumOutputs;
333 this->NumInputs = NumInputs;
334 this->NumClobbers = NumClobbers;
336 unsigned NumExprs = NumOutputs + NumInputs;
338 C.Deallocate(this->Names);
339 this->Names = new (C) IdentifierInfo*[NumExprs];
340 std::copy(Names, Names + NumExprs, this->Names);
342 C.Deallocate(this->Exprs);
343 this->Exprs = new (C) Stmt*[NumExprs];
344 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
346 C.Deallocate(this->Constraints);
347 this->Constraints = new (C) StringLiteral*[NumExprs];
348 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
350 C.Deallocate(this->Clobbers);
351 this->Clobbers = new (C) StringLiteral*[NumClobbers];
352 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
355 /// getNamedOperand - Given a symbolic operand reference like %[foo],
356 /// translate this into a numeric value needed to reference the same operand.
357 /// This returns -1 if the operand name is invalid.
358 int AsmStmt::getNamedOperand(StringRef SymbolicName) const {
359 unsigned NumPlusOperands = 0;
361 // Check if this is an output operand.
362 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
363 if (getOutputName(i) == SymbolicName)
367 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
368 if (getInputName(i) == SymbolicName)
369 return getNumOutputs() + NumPlusOperands + i;
375 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
376 /// it into pieces. If the asm string is erroneous, emit errors and return
377 /// true, otherwise return false.
378 unsigned AsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
379 ASTContext &C, unsigned &DiagOffs) const {
380 StringRef Str = getAsmString()->getString();
381 const char *StrStart = Str.begin();
382 const char *StrEnd = Str.end();
383 const char *CurPtr = StrStart;
385 // "Simple" inline asms have no constraints or operands, just convert the asm
386 // string to escape $'s.
389 for (; CurPtr != StrEnd; ++CurPtr) {
399 Pieces.push_back(AsmStringPiece(Result));
403 // CurStringPiece - The current string that we are building up as we scan the
405 std::string CurStringPiece;
407 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
410 // Done with the string?
411 if (CurPtr == StrEnd) {
412 if (!CurStringPiece.empty())
413 Pieces.push_back(AsmStringPiece(CurStringPiece));
417 char CurChar = *CurPtr++;
419 case '$': CurStringPiece += "$$"; continue;
420 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
421 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
422 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
426 CurStringPiece += CurChar;
430 // Escaped "%" character in asm string.
431 if (CurPtr == StrEnd) {
432 // % at end of string is invalid (no escape).
433 DiagOffs = CurPtr-StrStart-1;
434 return diag::err_asm_invalid_escape;
437 char EscapedChar = *CurPtr++;
438 if (EscapedChar == '%') { // %% -> %
439 // Escaped percentage sign.
440 CurStringPiece += '%';
444 if (EscapedChar == '=') { // %= -> Generate an unique ID.
445 CurStringPiece += "${:uid}";
449 // Otherwise, we have an operand. If we have accumulated a string so far,
450 // add it to the Pieces list.
451 if (!CurStringPiece.empty()) {
452 Pieces.push_back(AsmStringPiece(CurStringPiece));
453 CurStringPiece.clear();
456 // Handle %x4 and %x[foo] by capturing x as the modifier character.
457 char Modifier = '\0';
458 if (isalpha(EscapedChar)) {
459 if (CurPtr == StrEnd) { // Premature end.
460 DiagOffs = CurPtr-StrStart-1;
461 return diag::err_asm_invalid_escape;
463 Modifier = EscapedChar;
464 EscapedChar = *CurPtr++;
467 if (isdigit(EscapedChar)) {
468 // %n - Assembler operand n
472 while (CurPtr != StrEnd && isdigit(*CurPtr))
473 N = N*10 + ((*CurPtr++)-'0');
475 unsigned NumOperands =
476 getNumOutputs() + getNumPlusOperands() + getNumInputs();
477 if (N >= NumOperands) {
478 DiagOffs = CurPtr-StrStart-1;
479 return diag::err_asm_invalid_operand_number;
482 Pieces.push_back(AsmStringPiece(N, Modifier));
486 // Handle %[foo], a symbolic operand reference.
487 if (EscapedChar == '[') {
488 DiagOffs = CurPtr-StrStart-1;
491 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
493 return diag::err_asm_unterminated_symbolic_operand_name;
494 if (NameEnd == CurPtr)
495 return diag::err_asm_empty_symbolic_operand_name;
497 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
499 int N = getNamedOperand(SymbolicName);
501 // Verify that an operand with that name exists.
502 DiagOffs = CurPtr-StrStart;
503 return diag::err_asm_unknown_symbolic_operand_name;
505 Pieces.push_back(AsmStringPiece(N, Modifier));
511 DiagOffs = CurPtr-StrStart-1;
512 return diag::err_asm_invalid_escape;
516 QualType CXXCatchStmt::getCaughtType() const {
518 return ExceptionDecl->getType();
522 //===----------------------------------------------------------------------===//
524 //===----------------------------------------------------------------------===//
526 AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
527 bool isvolatile, bool msasm,
528 unsigned numoutputs, unsigned numinputs,
529 IdentifierInfo **names, StringLiteral **constraints,
530 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
531 StringLiteral **clobbers, SourceLocation rparenloc)
532 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
533 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
534 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
536 unsigned NumExprs = NumOutputs +NumInputs;
538 Names = new (C) IdentifierInfo*[NumExprs];
539 std::copy(names, names + NumExprs, Names);
541 Exprs = new (C) Stmt*[NumExprs];
542 std::copy(exprs, exprs + NumExprs, Exprs);
544 Constraints = new (C) StringLiteral*[NumExprs];
545 std::copy(constraints, constraints + NumExprs, Constraints);
547 Clobbers = new (C) StringLiteral*[NumClobbers];
548 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
551 ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
552 Stmt *Body, SourceLocation FCL,
554 : Stmt(ObjCForCollectionStmtClass) {
555 SubExprs[ELEM] = Elem;
556 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
557 SubExprs[BODY] = Body;
562 ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
563 Stmt **CatchStmts, unsigned NumCatchStmts,
565 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
566 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
568 Stmt **Stmts = getStmts();
569 Stmts[0] = atTryStmt;
570 for (unsigned I = 0; I != NumCatchStmts; ++I)
571 Stmts[I + 1] = CatchStmts[I];
574 Stmts[NumCatchStmts + 1] = atFinallyStmt;
577 ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
578 SourceLocation atTryLoc,
581 unsigned NumCatchStmts,
582 Stmt *atFinallyStmt) {
583 unsigned Size = sizeof(ObjCAtTryStmt) +
584 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
585 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
586 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
590 ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
591 unsigned NumCatchStmts,
593 unsigned Size = sizeof(ObjCAtTryStmt) +
594 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
595 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
596 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
599 SourceRange ObjCAtTryStmt::getSourceRange() const {
600 SourceLocation EndLoc;
602 EndLoc = getFinallyStmt()->getLocEnd();
603 else if (NumCatchStmts)
604 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
606 EndLoc = getTryBody()->getLocEnd();
608 return SourceRange(AtTryLoc, EndLoc);
611 CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
612 Stmt *tryBlock, Stmt **handlers,
613 unsigned numHandlers) {
614 std::size_t Size = sizeof(CXXTryStmt);
615 Size += ((numHandlers + 1) * sizeof(Stmt));
617 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
618 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
621 CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
622 unsigned numHandlers) {
623 std::size_t Size = sizeof(CXXTryStmt);
624 Size += ((numHandlers + 1) * sizeof(Stmt));
626 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
627 return new (Mem) CXXTryStmt(Empty, numHandlers);
630 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
631 Stmt **handlers, unsigned numHandlers)
632 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
633 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
635 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
638 CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
639 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
640 Stmt *Body, SourceLocation FL,
641 SourceLocation CL, SourceLocation RPL)
642 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
643 SubExprs[RANGE] = Range;
644 SubExprs[BEGINEND] = BeginEndStmt;
645 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
646 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
647 SubExprs[LOOPVAR] = LoopVar;
648 SubExprs[BODY] = Body;
651 Expr *CXXForRangeStmt::getRangeInit() {
652 DeclStmt *RangeStmt = getRangeStmt();
653 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
654 assert(RangeDecl &&& "for-range should have a single var decl");
655 return RangeDecl->getInit();
658 const Expr *CXXForRangeStmt::getRangeInit() const {
659 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
662 VarDecl *CXXForRangeStmt::getLoopVariable() {
663 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
664 assert(LV && "No loop variable in CXXForRangeStmt");
665 return cast<VarDecl>(LV);
668 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
669 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
672 IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
673 Stmt *then, SourceLocation EL, Stmt *elsev)
674 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
676 setConditionVariable(C, var);
677 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
678 SubExprs[THEN] = then;
679 SubExprs[ELSE] = elsev;
682 VarDecl *IfStmt::getConditionVariable() const {
686 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
687 return cast<VarDecl>(DS->getSingleDecl());
690 void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
696 SourceRange VarRange = V->getSourceRange();
697 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
701 ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
702 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
704 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
706 SubExprs[INIT] = Init;
707 setConditionVariable(C, condVar);
708 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
709 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
710 SubExprs[BODY] = Body;
713 VarDecl *ForStmt::getConditionVariable() const {
714 if (!SubExprs[CONDVAR])
717 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
718 return cast<VarDecl>(DS->getSingleDecl());
721 void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
723 SubExprs[CONDVAR] = 0;
727 SourceRange VarRange = V->getSourceRange();
728 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
732 SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
733 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
735 setConditionVariable(C, Var);
736 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
737 SubExprs[BODY] = NULL;
740 VarDecl *SwitchStmt::getConditionVariable() const {
744 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
745 return cast<VarDecl>(DS->getSingleDecl());
748 void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
754 SourceRange VarRange = V->getSourceRange();
755 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
759 Stmt *SwitchCase::getSubStmt() {
760 if (isa<CaseStmt>(this))
761 return cast<CaseStmt>(this)->getSubStmt();
762 return cast<DefaultStmt>(this)->getSubStmt();
765 WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
767 : Stmt(WhileStmtClass) {
768 setConditionVariable(C, Var);
769 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
770 SubExprs[BODY] = body;
774 VarDecl *WhileStmt::getConditionVariable() const {
778 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
779 return cast<VarDecl>(DS->getSingleDecl());
782 void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
788 SourceRange VarRange = V->getSourceRange();
789 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
794 LabelDecl *IndirectGotoStmt::getConstantTarget() {
795 if (AddrLabelExpr *E =
796 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
797 return E->getLabel();
802 const Expr* ReturnStmt::getRetValue() const {
803 return cast_or_null<Expr>(RetExpr);
805 Expr* ReturnStmt::getRetValue() {
806 return cast_or_null<Expr>(RetExpr);
809 SEHTryStmt::SEHTryStmt(bool IsCXXTry,
810 SourceLocation TryLoc,
813 : Stmt(SEHTryStmtClass),
817 Children[TRY] = TryBlock;
818 Children[HANDLER] = Handler;
821 SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
823 SourceLocation TryLoc,
826 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
829 SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
830 return dyn_cast<SEHExceptStmt>(getHandler());
833 SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
834 return dyn_cast<SEHFinallyStmt>(getHandler());
837 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
840 : Stmt(SEHExceptStmtClass),
843 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
844 Children[BLOCK] = Block;
847 SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
851 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
854 SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
856 : Stmt(SEHFinallyStmtClass),
861 SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
864 return new(C)SEHFinallyStmt(Loc,Block);