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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 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()); |
| 34 | |
| 35 | VariableArrayType* p = getVAPtr(); |
| 36 | p = FindVA(p->getElementType().getTypePtr()); |
| 37 | setVAPtr(p); |
| 38 | |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 39 | if (!p && decl) { |
Ted Kremenek | 771fe16 | 2007-10-29 21:38:03 +0000 | [diff] [blame] | 40 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) |
| 41 | if (VD->Init) |
| 42 | return; |
| 43 | |
| 44 | NextDecl(); |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 45 | } |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 46 | else { |
| 47 | RawVAPtr = 0; |
| 48 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void StmtIteratorBase::NextDecl(bool ImmediateAdvance) { |
Ted Kremenek | b51d2b2 | 2007-12-14 23:40:56 +0000 | [diff] [blame] | 52 | assert (inDecl()); |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 53 | assert (getVAPtr() == NULL); |
| 54 | assert (decl); |
| 55 | |
| 56 | if (ImmediateAdvance) { |
| 57 | decl = decl->getNextDeclarator(); |
| 58 | |
| 59 | if (!decl) { |
| 60 | RawVAPtr = 0; |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | for ( ; decl ; decl = decl->getNextDeclarator()) { |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 66 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) { |
| 67 | if (VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) { |
| 68 | setVAPtr(VAPtr); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if (VD->getInit()) |
| 73 | return; |
| 74 | } |
Ted Kremenek | 771fe16 | 2007-10-29 21:38:03 +0000 | [diff] [blame] | 75 | else if (TypedefDecl* TD = dyn_cast<TypedefDecl>(decl)) { |
| 76 | if (VariableArrayType* VAPtr = |
| 77 | FindVA(TD->getUnderlyingType().getTypePtr())) { |
| 78 | setVAPtr(VAPtr); |
| 79 | return; |
| 80 | } |
| 81 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 82 | else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(decl)) |
| 83 | if (ECD->getInitExpr()) |
| 84 | return; |
| 85 | } |
Ted Kremenek | 103a1b4 | 2007-10-29 21:23:58 +0000 | [diff] [blame] | 86 | |
| 87 | if (!decl) { |
| 88 | RawVAPtr = 0; |
| 89 | return; |
| 90 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | StmtIteratorBase::StmtIteratorBase(ScopedDecl* d) |
| 94 | : decl(d), RawVAPtr(DeclMode) { |
| 95 | assert (decl); |
| 96 | NextDecl(false); |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 97 | } |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 99 | StmtIteratorBase::StmtIteratorBase(VariableArrayType* t) |
Ted Kremenek | b51d2b2 | 2007-12-14 23:40:56 +0000 | [diff] [blame] | 100 | : decl(NULL), RawVAPtr(SizeOfTypeVAMode) { |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 101 | RawVAPtr |= reinterpret_cast<uintptr_t>(t); |
| 102 | } |
| 103 | |
| 104 | |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame] | 105 | Stmt*& StmtIteratorBase::GetDeclExpr() const { |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 106 | if (VariableArrayType* VAPtr = getVAPtr()) { |
| 107 | assert (VAPtr->SizeExpr); |
| 108 | return reinterpret_cast<Stmt*&>(VAPtr->SizeExpr); |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame] | 109 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 110 | |
| 111 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) { |
| 112 | assert (VD->Init); |
| 113 | return reinterpret_cast<Stmt*&>(VD->Init); |
| 114 | } |
| 115 | |
| 116 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl); |
| 117 | return reinterpret_cast<Stmt*&>(ECD->Init); |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 118 | } |