Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 1 | //===--- StmtIterator.cpp - Iterators for Statements ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines internal methods for StmtIterator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/StmtIterator.h" |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 20 | static inline VariableArrayType* FindVA(Type* t) { |
| 21 | while (ArrayType* vt = dyn_cast<ArrayType>(t)) { |
| 22 | if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) |
| 23 | if (vat->getSizeExpr()) |
| 24 | return vat; |
| 25 | |
| 26 | t = vt->getElementType().getTypePtr(); |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 29 | return NULL; |
| 30 | } |
| 31 | |
| 32 | void StmtIteratorBase::NextVA() { |
| 33 | assert (getVAPtr()); |
Ted Kremenek | 103a1b4 | 2007-10-29 21:23:58 +0000 | [diff] [blame^] | 34 | assert (decl); |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 35 | |
| 36 | VariableArrayType* p = getVAPtr(); |
| 37 | p = FindVA(p->getElementType().getTypePtr()); |
| 38 | setVAPtr(p); |
| 39 | |
| 40 | if (!p) { |
| 41 | VarDecl* VD = cast<VarDecl>(decl); |
| 42 | |
| 43 | if (!VD->Init) |
| 44 | NextDecl(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void StmtIteratorBase::NextDecl(bool ImmediateAdvance) { |
| 49 | assert (inDeclMode()); |
| 50 | assert (getVAPtr() == NULL); |
| 51 | assert (decl); |
| 52 | |
| 53 | if (ImmediateAdvance) { |
| 54 | decl = decl->getNextDeclarator(); |
| 55 | |
| 56 | if (!decl) { |
| 57 | RawVAPtr = 0; |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | for ( ; decl ; decl = decl->getNextDeclarator()) { |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 63 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) { |
| 64 | if (VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) { |
| 65 | setVAPtr(VAPtr); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (VD->getInit()) |
| 70 | return; |
| 71 | } |
| 72 | else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(decl)) |
| 73 | if (ECD->getInitExpr()) |
| 74 | return; |
| 75 | } |
Ted Kremenek | 103a1b4 | 2007-10-29 21:23:58 +0000 | [diff] [blame^] | 76 | |
| 77 | if (!decl) { |
| 78 | RawVAPtr = 0; |
| 79 | return; |
| 80 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | StmtIteratorBase::StmtIteratorBase(ScopedDecl* d) |
| 84 | : decl(d), RawVAPtr(DeclMode) { |
| 85 | assert (decl); |
| 86 | NextDecl(false); |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 87 | } |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame] | 89 | Stmt*& StmtIteratorBase::GetDeclExpr() const { |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 90 | if (VariableArrayType* VAPtr = getVAPtr()) { |
| 91 | assert (VAPtr->SizeExpr); |
| 92 | return reinterpret_cast<Stmt*&>(VAPtr->SizeExpr); |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame] | 93 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 94 | |
| 95 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) { |
| 96 | assert (VD->Init); |
| 97 | return reinterpret_cast<Stmt*&>(VD->Init); |
| 98 | } |
| 99 | |
| 100 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl); |
| 101 | return reinterpret_cast<Stmt*&>(ECD->Init); |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 102 | } |