-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_function.h
More file actions
357 lines (303 loc) · 7.93 KB
/
Copy pathcpp_function.h
File metadata and controls
357 lines (303 loc) · 7.93 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef D6B4396A_776A_46E6_97BD_3EA2B04625F9
#define D6B4396A_776A_46E6_97BD_3EA2B04625F9
#include "cppast/cpp_compound.h"
#include "cppast/cpp_entity.h"
#include "cppast/cpp_expression.h"
#include "cppast/cpp_template_param.h"
#include "cppast/cpp_var_decl.h"
#include "cppast/cpp_var_type.h"
#include <functional>
#include <list>
namespace cppast {
class CppExpression;
class CppBlob;
class CppFuncLike
{
public:
const std::vector<std::string>& throwSpec() const
{
return throwSpec_;
}
void throwSpec(std::vector<std::string> throwSpecArg)
{
throwSpec_ = std::move(throwSpecArg);
}
const CppCompound* defn() const
{
return defn_.get();
}
void defn(std::unique_ptr<CppCompound> defnArg)
{
defn_ = std::move(defnArg);
}
private:
std::unique_ptr<CppCompound> defn_; // If it is nullptr then this object is just for declaration.
std::vector<std::string> throwSpec_;
};
/**
* @brief Base class of constructor, destructor, and functions.
*/
class CppFunctionCommon : public CppFuncLike, public CppTemplatableEntity
{
public:
const std::string& name() const
{
return name_;
}
std::uint32_t attr() const
{
return attr_;
}
void addAttr(std::uint32_t attrArg)
{
attr_ |= attrArg;
}
bool hasAttr(std::uint32_t attrArg) const
{
return ((attr_ & attrArg) == attrArg);
}
const std::string& decor1() const
{
return decor1_;
}
void decor1(std::string decorArg)
{
decor1_ = std::move(decorArg);
}
const std::string& decor2() const
{
return decor2_;
}
void decor2(std::string decorArg)
{
decor2_ = std::move(decorArg);
}
protected:
CppFunctionCommon(std::string name, std::uint32_t attr)
: name_(std::move(name))
, attr_(attr)
{
}
private:
std::string name_;
std::uint32_t attr_; // e.g.: const, static, virtual, inline, constexpr, etc.
std::string decor1_; // e.g. __declspec(dllexport)
std::string decor2_; // e.g. __stdcall
};
class CppFuncOrCtorCommon : public CppFunctionCommon
{
public:
bool hasParams() const
{
return !params_.empty();
}
bool visitParams(const std::function<bool(const CppEntity& param)>& callback) const
{
for (const auto& param : params_)
{
if (!callback(*param))
{
return false;
}
}
return true;
}
void visitAllParams(const std::function<void(const CppEntity& param)>& callback) const
{
visitParams([&callback](auto& param) {
callback(param);
return true;
});
}
protected:
CppFuncOrCtorCommon(std::string name, std::vector<std::unique_ptr<CppEntity>> params, std::uint32_t attr)
: CppFunctionCommon(std::move(name), attr)
, params_(std::move(params))
{
}
protected:
// FIXME: Not all CppEntity objects can be function parameters. Chose a more suitable type.
std::vector<std::unique_ptr<CppEntity>> params_;
};
class CppVarType;
class CppFunctionOrFuncPtrCommon : public CppFuncOrCtorCommon
{
public:
const CppVarType* returnType() const
{
return retType_.get();
}
protected:
CppFunctionOrFuncPtrCommon(std::string name,
std::unique_ptr<CppVarType> retType,
std::vector<std::unique_ptr<CppEntity>> params,
std::uint32_t attr)
: CppFuncOrCtorCommon(std::move(name), std::move(params), attr)
, retType_(std::move(retType))
{
}
private:
std::unique_ptr<CppVarType> retType_;
};
class CppFunction : public CppEntity, public CppFunctionOrFuncPtrCommon
{
public:
static constexpr auto EntityType()
{
return CppEntityType::FUNCTION;
}
public:
CppFunction(std::string name,
std::unique_ptr<CppVarType> retType,
std::vector<std::unique_ptr<CppEntity>> params,
std::uint32_t attr)
: CppEntity(EntityType())
, CppFunctionOrFuncPtrCommon(std::move(name), std::move(retType), std::move(params), attr)
{
}
};
class CppLambda : public CppEntity
{
public:
static constexpr auto EntityType()
{
return CppEntityType::LAMBDA;
}
public:
CppLambda(std::unique_ptr<CppExpression> captures,
std::vector<std::unique_ptr<CppEntity>> params,
std::unique_ptr<CppCompound> defn,
std::unique_ptr<CppVarType> retType = nullptr);
public:
const CppExpression* captures() const
{
return captures_.get();
}
const std::vector<std::unique_ptr<CppEntity>>& params() const
{
return params_;
}
const CppVarType* returnType() const
{
return retType_.get();
}
const CppCompound* defn() const
{
return defn_.get();
}
private:
std::unique_ptr<CppExpression> captures_;
std::vector<std::unique_ptr<CppEntity>> params_;
std::unique_ptr<CppVarType> retType_;
std::unique_ptr<CppCompound> defn_;
};
/**
* Function pointer type definition using typedef, e.g. 'typedef void (*funcPtr)(int);'
* It has all the attributes of a function object and so it is simply derived from CppFunction.
*/
class CppFunctionPointer : public CppEntity, public CppFunctionOrFuncPtrCommon
{
public:
static constexpr auto EntityType()
{
return CppEntityType::FUNCTION_PTR;
}
public:
CppFunctionPointer(std::string name,
std::unique_ptr<CppVarType> retType,
std::vector<std::unique_ptr<CppEntity>> params,
std::uint32_t attr,
std::string ownerName = std::string())
: CppEntity(EntityType())
, CppFunctionOrFuncPtrCommon(std::move(name), std::move(retType), std::move(params), attr)
, ownerName_(std::move(ownerName))
{
}
public:
const std::string& ownerName() const
{
return ownerName_;
}
private:
std::string ownerName_;
};
/**
* Class data member initialization as part of
* class constructor.
*/
struct CppMemberInit
{
std::string memberName;
CppConstructorCallInfo memberInitInfo;
};
/**
* Entire member initialization list.
*/
using CppMemberInits = std::list<CppMemberInit>;
class CppConstructor : public CppEntity, public CppFuncOrCtorCommon
{
public:
static constexpr auto EntityType()
{
return CppEntityType::CONSTRUCTOR;
}
CppConstructor(std::string name,
std::vector<std::unique_ptr<CppEntity>> params,
CppMemberInits memInitList,
std::uint32_t attr);
~CppConstructor() override;
bool hasMemberInitList() const
{
return memInits_.has_value();
}
void memberInits(CppMemberInits memInits)
{
memInits_ = std::move(memInits);
}
const CppMemberInits& memberInits() const
{
return memInits_.value();
}
private:
std::optional<CppMemberInits> memInits_;
};
class CppDestructor : public CppEntity, public CppFunctionCommon
{
public:
static constexpr auto EntityType()
{
return CppEntityType::DESTRUCTOR;
}
public:
CppDestructor(std::string name, std::uint32_t attr)
: CppEntity(EntityType())
, CppFunctionCommon(name, attr)
{
}
};
class CppTypeConverter : public CppEntity, public CppFunctionCommon
{
public:
static constexpr auto EntityType()
{
return CppEntityType::TYPE_CONVERTER;
}
public:
CppTypeConverter(CppVarType* type, std::string name)
: CppEntity(EntityType())
, CppFunctionCommon(std::move(name), 0)
, targetType_(type)
{
}
public:
const CppVarType* targetType() const
{
return targetType_.get();
}
private:
std::unique_ptr<CppVarType> targetType_;
};
} // namespace cppast
#endif /* D6B4396A_776A_46E6_97BD_3EA2B04625F9 */