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 | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 15 | #include "clang/AST/Decl.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 19 | static inline VariableArrayType* FindVA(Type* t) { |
| 20 | while (ArrayType* vt = dyn_cast<ArrayType>(t)) { |
| 21 | if (VariableArrayType* vat = dyn_cast<VariableArrayType>(vt)) |
| 22 | if (vat->getSizeExpr()) |
| 23 | return vat; |
| 24 | |
| 25 | t = vt->getElementType().getTypePtr(); |
Ted Kremenek | c325e7f | 2007-10-18 18:19:31 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | void StmtIteratorBase::NextVA() { |
| 32 | assert (getVAPtr()); |
| 33 | |
| 34 | VariableArrayType* p = getVAPtr(); |
| 35 | p = FindVA(p->getElementType().getTypePtr()); |
| 36 | setVAPtr(p); |
| 37 | |
Ted Kremenek | 8a213de | 2008-10-07 23:35:42 +0000 | [diff] [blame^] | 38 | if (p) |
| 39 | return; |
| 40 | |
| 41 | if (inDecl()) { |
| 42 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) |
| 43 | if (VD->Init) |
| 44 | return; |
| 45 | |
| 46 | NextDecl(); |
| 47 | } |
| 48 | else if (inDeclGroup()) { |
| 49 | if (VarDecl* VD = dyn_cast<VarDecl>(*DGI)) |
| 50 | if (VD->Init) |
| 51 | return; |
| 52 | |
| 53 | NextDecl(); |
| 54 | } |
| 55 | else { |
| 56 | assert (inSizeOfTypeVA()); |
Eli Friedman | b0c0554 | 2008-05-21 05:06:46 +0000 | [diff] [blame] | 57 | assert(!decl); |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 58 | RawVAPtr = 0; |
Eli Friedman | b0c0554 | 2008-05-21 05:06:46 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void StmtIteratorBase::NextDecl(bool ImmediateAdvance) { |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 63 | assert (getVAPtr() == NULL); |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 65 | if (inDecl()) { |
| 66 | assert (decl); |
| 67 | |
| 68 | if (ImmediateAdvance) |
| 69 | decl = decl->getNextDeclarator(); |
| 70 | |
| 71 | for ( ; decl ; decl = decl->getNextDeclarator()) |
| 72 | if (HandleDecl(decl)) |
| 73 | return; |
| 74 | } |
| 75 | else { |
| 76 | assert (inDeclGroup()); |
| 77 | |
| 78 | if (ImmediateAdvance) |
| 79 | ++DGI; |
| 80 | |
| 81 | for ( ; DGI != DGE; ++DGI) |
| 82 | if (HandleDecl(*DGI)) |
| 83 | return; |
| 84 | } |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 86 | RawVAPtr = 0; |
| 87 | } |
| 88 | |
| 89 | bool StmtIteratorBase::HandleDecl(Decl* D) { |
| 90 | |
| 91 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) { |
| 92 | if (VariableArrayType* VAPtr = FindVA(VD->getType().getTypePtr())) { |
| 93 | setVAPtr(VAPtr); |
| 94 | return true; |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 95 | } |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 96 | |
| 97 | if (VD->getInit()) |
| 98 | return true; |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 99 | } |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 100 | else if (TypedefDecl* TD = dyn_cast<TypedefDecl>(D)) { |
| 101 | if (VariableArrayType* VAPtr = |
| 102 | FindVA(TD->getUnderlyingType().getTypePtr())) { |
| 103 | setVAPtr(VAPtr); |
| 104 | return true; |
| 105 | } |
Ted Kremenek | 103a1b4 | 2007-10-29 21:23:58 +0000 | [diff] [blame] | 106 | } |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 107 | else if (EnumConstantDecl* ECD = dyn_cast<EnumConstantDecl>(D)) { |
| 108 | if (ECD->getInitExpr()) |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return false; |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | StmtIteratorBase::StmtIteratorBase(ScopedDecl* d) |
| 116 | : decl(d), RawVAPtr(DeclMode) { |
| 117 | assert (decl); |
| 118 | NextDecl(false); |
Ted Kremenek | c7c326a | 2007-10-18 16:25:40 +0000 | [diff] [blame] | 119 | } |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 121 | StmtIteratorBase::StmtIteratorBase(Decl** dgi, Decl** dge) |
| 122 | : DGI(dgi), RawVAPtr(DeclGroupMode), DGE(dge) { |
| 123 | NextDecl(false); |
| 124 | } |
| 125 | |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 126 | StmtIteratorBase::StmtIteratorBase(VariableArrayType* t) |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 127 | : decl(0), RawVAPtr(SizeOfTypeVAMode) { |
Ted Kremenek | 699e9fb | 2007-12-14 22:52:23 +0000 | [diff] [blame] | 128 | RawVAPtr |= reinterpret_cast<uintptr_t>(t); |
| 129 | } |
| 130 | |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame] | 131 | Stmt*& StmtIteratorBase::GetDeclExpr() const { |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 132 | |
| 133 | if (inDeclGroup()) { |
| 134 | VarDecl* VD = cast<VarDecl>(*DGI); |
| 135 | return VD->Init; |
| 136 | } |
| 137 | |
| 138 | assert (inDecl() || inSizeOfTypeVA()); |
| 139 | |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 140 | if (VariableArrayType* VAPtr = getVAPtr()) { |
| 141 | assert (VAPtr->SizeExpr); |
Ted Kremenek | b306404 | 2008-05-30 16:14:41 +0000 | [diff] [blame] | 142 | return VAPtr->SizeExpr; |
Ted Kremenek | b990f18 | 2007-10-25 22:24:19 +0000 | [diff] [blame] | 143 | } |
Ted Kremenek | 3e6d120 | 2008-10-07 23:04:14 +0000 | [diff] [blame] | 144 | |
| 145 | assert (inDecl()); |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 146 | |
| 147 | if (VarDecl* VD = dyn_cast<VarDecl>(decl)) { |
| 148 | assert (VD->Init); |
Ted Kremenek | b306404 | 2008-05-30 16:14:41 +0000 | [diff] [blame] | 149 | return VD->Init; |
Ted Kremenek | 92866e2 | 2007-10-29 20:50:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl); |
Ted Kremenek | b306404 | 2008-05-30 16:14:41 +0000 | [diff] [blame] | 153 | return ECD->Init; |
Ted Kremenek | 9caf8b1 | 2007-10-18 00:24:38 +0000 | [diff] [blame] | 154 | } |