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