1 | // |
2 | // Struct.cpp |
3 | // |
4 | // Library: CppParser |
5 | // Package: SymbolTable |
6 | // Module: Struct |
7 | // |
8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/CppParser/Struct.h" |
16 | #include "Poco/CppParser/Function.h" |
17 | #include "Poco/String.h" |
18 | #include <algorithm> |
19 | #include <sstream> |
20 | #include <cstddef> |
21 | |
22 | |
23 | namespace Poco { |
24 | namespace CppParser { |
25 | |
26 | |
27 | Struct::Struct(const std::string& decl, bool isClass, NameSpace* pNameSpace): |
28 | NameSpace(extractName(decl), pNameSpace), |
29 | _decl(decl), |
30 | _flags(0), |
31 | _isClass(isClass) |
32 | { |
33 | std::size_t pos = decl.find("template" ); |
34 | if (pos != std::string::npos) |
35 | { |
36 | _flags |= FN_TEMPLATE; |
37 | std::size_t templTypeStart = decl.find("<" , pos); |
38 | std::size_t templTypeEnd = decl.find(">" , templTypeStart); |
39 | if (templTypeStart != std::string::npos && templTypeEnd != std::string::npos) |
40 | { |
41 | std::string templParam = decl.substr(templTypeStart+1, templTypeEnd-templTypeStart-1); |
42 | Poco::trimInPlace(templParam); |
43 | if (templParam.empty()) |
44 | _flags |= FN_TEMPLATE_SPECIALIZATION; |
45 | } |
46 | } |
47 | } |
48 | |
49 | |
50 | Struct::~Struct() |
51 | { |
52 | } |
53 | |
54 | |
55 | void Struct::addBase(const std::string& name, Symbol::Access access, bool isVirtual) |
56 | { |
57 | Base base; |
58 | base.name = name; |
59 | base.access = access; |
60 | base.isVirtual = isVirtual; |
61 | base.pClass = 0; |
62 | _bases.push_back(base); |
63 | } |
64 | |
65 | |
66 | Struct::BaseIterator Struct::baseBegin() const |
67 | { |
68 | return _bases.begin(); |
69 | } |
70 | |
71 | |
72 | Struct::BaseIterator Struct::baseEnd() const |
73 | { |
74 | return _bases.end(); |
75 | } |
76 | |
77 | |
78 | void Struct::addDerived(Struct* pClass) |
79 | { |
80 | poco_check_ptr (pClass); |
81 | |
82 | _derived.push_back(pClass); |
83 | } |
84 | |
85 | |
86 | void Struct::fixupBases() |
87 | { |
88 | for (BaseClasses::iterator it = _bases.begin(); it != _bases.end(); ++it) |
89 | { |
90 | it->pClass = dynamic_cast<Struct*>(lookup(it->name)); |
91 | if (it->pClass) |
92 | it->pClass->addDerived(this); |
93 | } |
94 | } |
95 | |
96 | |
97 | Struct::DerivedIterator Struct::derivedBegin() const |
98 | { |
99 | return _derived.begin(); |
100 | } |
101 | |
102 | |
103 | Struct::DerivedIterator Struct::derivedEnd() const |
104 | { |
105 | return _derived.end(); |
106 | } |
107 | |
108 | |
109 | namespace |
110 | { |
111 | struct CPLT |
112 | { |
113 | bool operator() (const Function* pF1, const Function* pF2) const |
114 | { |
115 | int pc1 = pF1->countParameters(); |
116 | int pc2 = pF2->countParameters(); |
117 | return pc1 < pc2; |
118 | } |
119 | }; |
120 | }; |
121 | |
122 | |
123 | void Struct::constructors(Functions& functions) const |
124 | { |
125 | for (NameSpace::Iterator it = begin(); it != end(); ++it) |
126 | { |
127 | Function* pFunc = dynamic_cast<Function*>(it->second); |
128 | if (pFunc && pFunc->isConstructor()) |
129 | functions.push_back(pFunc); |
130 | } |
131 | std::sort(functions.begin(), functions.end(), CPLT()); |
132 | } |
133 | |
134 | |
135 | void Struct::bases(std::set<std::string>& bases) const |
136 | { |
137 | for (BaseIterator it = baseBegin(); it != baseEnd(); ++it) |
138 | { |
139 | if (it->pClass) |
140 | { |
141 | bases.insert(it->pClass->fullName()); |
142 | it->pClass->bases(bases); |
143 | } |
144 | else bases.insert(it->name); |
145 | } |
146 | } |
147 | |
148 | |
149 | Function* Struct::destructor() const |
150 | { |
151 | for (NameSpace::Iterator it = begin(); it != end(); ++it) |
152 | { |
153 | Function* pFunc = dynamic_cast<Function*>(it->second); |
154 | if (pFunc && pFunc->isDestructor()) |
155 | return pFunc; |
156 | } |
157 | return 0; |
158 | } |
159 | |
160 | |
161 | void Struct::methods(Symbol::Access access, Functions& functions) const |
162 | { |
163 | for (NameSpace::Iterator it = begin(); it != end(); ++it) |
164 | { |
165 | Function* pFunc = dynamic_cast<Function*>(it->second); |
166 | if (pFunc && pFunc->getAccess() == access && pFunc->isMethod()) |
167 | functions.push_back(pFunc); |
168 | } |
169 | } |
170 | |
171 | |
172 | void Struct::inheritedMethods(FunctionSet& functions) const |
173 | { |
174 | for (BaseIterator it = baseBegin(); it != baseEnd(); ++it) |
175 | { |
176 | Struct* pBase = it->pClass; |
177 | if (pBase) |
178 | { |
179 | for (NameSpace::Iterator itm = pBase->begin(); itm != pBase->end(); ++itm) |
180 | { |
181 | Function* pFunc = dynamic_cast<Function*>(itm->second); |
182 | if (pFunc && pFunc->getAccess() != Symbol::ACC_PRIVATE && pFunc->isMethod()) |
183 | functions.insert(pFunc); |
184 | } |
185 | pBase->inheritedMethods(functions); |
186 | } |
187 | } |
188 | } |
189 | |
190 | |
191 | void Struct::derived(StructSet& derived) const |
192 | { |
193 | for (DerivedIterator it = derivedBegin(); it != derivedEnd(); ++it) |
194 | { |
195 | derived.insert(*it); |
196 | (*it)->derived(derived); |
197 | } |
198 | } |
199 | |
200 | |
201 | Symbol::Kind Struct::kind() const |
202 | { |
203 | return Symbol::SYM_STRUCT; |
204 | } |
205 | |
206 | |
207 | Function* Struct::findFunction(const std::string& signature) const |
208 | { |
209 | for (NameSpace::Iterator it = begin(); it != end(); ++it) |
210 | { |
211 | Function* pFunc = dynamic_cast<Function*>(it->second); |
212 | if (pFunc && pFunc->signature() == signature) |
213 | return pFunc; |
214 | } |
215 | for (BaseIterator it = baseBegin(); it != baseEnd(); ++it) |
216 | { |
217 | if (it->pClass) |
218 | { |
219 | Function* pFunc = it->pClass->findFunction(signature); |
220 | if (pFunc) return pFunc; |
221 | } |
222 | } |
223 | return 0; |
224 | } |
225 | |
226 | |
227 | bool Struct::hasVirtualDestructor() const |
228 | { |
229 | Function* pFunc = destructor(); |
230 | if (pFunc && (pFunc->flags() & Function::FN_VIRTUAL)) |
231 | return true; |
232 | for (BaseIterator it = baseBegin(); it != baseEnd(); ++it) |
233 | { |
234 | if (it->pClass && it->pClass->hasVirtualDestructor()) |
235 | return true; |
236 | } |
237 | return false; |
238 | } |
239 | |
240 | |
241 | std::string Struct::toString() const |
242 | { |
243 | std::ostringstream ostr; |
244 | ostr << declaration() << "\n{\n" ; |
245 | for (Iterator it = begin(); it != end(); ++it) |
246 | { |
247 | ostr << it->second->fullName() << "\n" ; |
248 | ostr << it->second->toString() << "\n" ; |
249 | } |
250 | ostr << "};\n" ; |
251 | return ostr.str(); |
252 | } |
253 | |
254 | |
255 | } } // namespace Poco::CppParser |
256 | |