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