forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppFunctionType.cxx
More file actions
444 lines (395 loc) · 11.5 KB
/
cppFunctionType.cxx
File metadata and controls
444 lines (395 loc) · 11.5 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file cppFunctionType.cxx
* @author drose
* @date 1999-10-21
*/
#include "cppFunctionType.h"
#include "cppParameterList.h"
#include "cppSimpleType.h"
#include "cppInstance.h"
using std::ostream;
using std::ostringstream;
using std::string;
/**
*
*/
CPPFunctionType::
CPPFunctionType(CPPType *return_type, CPPParameterList *parameters,
int flags) :
CPPType(CPPFile()),
_return_type(return_type),
_parameters(parameters),
_flags(flags)
{
_class_owner = nullptr;
// If the parameter list contains just the token "void", it means no
// parameters.
if (_parameters != nullptr &&
_parameters->_parameters.size() == 1 &&
_parameters->_parameters.front()->_type->as_simple_type() != nullptr &&
_parameters->_parameters.front()->_type->as_simple_type()->_type ==
CPPSimpleType::T_void &&
_parameters->_parameters.front()->_ident == nullptr) {
_parameters->_parameters.clear();
}
}
/**
*
*/
CPPFunctionType::
CPPFunctionType(const CPPFunctionType ©) :
CPPType(copy),
_return_type(copy._return_type),
_parameters(copy._parameters),
_flags(copy._flags),
_class_owner(copy._class_owner)
{
}
/**
*
*/
void CPPFunctionType::
operator = (const CPPFunctionType ©) {
CPPType::operator = (copy);
_return_type = copy._return_type;
_parameters = copy._parameters;
_flags = copy._flags;
_class_owner = copy._class_owner;
}
/**
* Returns true if the function accepts the given number of parameters.
*/
bool CPPFunctionType::
accepts_num_parameters(int num_parameters) {
assert(num_parameters >= 0);
if (_parameters == nullptr) {
return (num_parameters == 0);
}
size_t actual_num_parameters = _parameters->_parameters.size();
// If we passed too many parameters, it must have an ellipsis.
if ((size_t)num_parameters > actual_num_parameters) {
return _parameters->_includes_ellipsis;
}
// Make sure all superfluous parameters have a default value.
for (size_t i = (size_t)num_parameters; i < actual_num_parameters; ++i) {
CPPInstance *param = _parameters->_parameters[i];
if (param->_initializer == nullptr) {
return false;
}
}
return true;
}
/**
* Returns true if this declaration is an actual, factual declaration, or
* false if some part of the declaration depends on a template parameter which
* has not yet been instantiated.
*/
bool CPPFunctionType::
is_fully_specified() const {
return CPPType::is_fully_specified() &&
_return_type->is_fully_specified() &&
_parameters->is_fully_specified();
}
/**
*
*/
CPPDeclaration *CPPFunctionType::
substitute_decl(CPPDeclaration::SubstDecl &subst,
CPPScope *current_scope, CPPScope *global_scope) {
SubstDecl::const_iterator si = subst.find(this);
if (si != subst.end()) {
return (*si).second;
}
CPPFunctionType *rep = new CPPFunctionType(*this);
if (_return_type != nullptr) {
rep->_return_type =
_return_type->substitute_decl(subst, current_scope, global_scope)
->as_type();
}
if (_parameters != nullptr) {
rep->_parameters =
_parameters->substitute_decl(subst, current_scope, global_scope);
}
if (rep->_return_type == _return_type &&
rep->_parameters == _parameters) {
delete rep;
rep = this;
}
rep = CPPType::new_type(rep)->as_function_type();
subst.insert(SubstDecl::value_type(this, rep));
return rep;
}
/**
* If this CPPType object is a forward reference or other nonspecified
* reference to a type that might now be known a real type, returns the real
* type. Otherwise returns the type itself.
*/
CPPType *CPPFunctionType::
resolve_type(CPPScope *current_scope, CPPScope *global_scope) {
CPPType *rtype = _return_type->resolve_type(current_scope, global_scope);
CPPParameterList *params;
if (_parameters == nullptr) {
params = nullptr;
} else {
params = _parameters->resolve_type(current_scope, global_scope);
}
if (rtype != _return_type || params != _parameters) {
CPPFunctionType *rep = new CPPFunctionType(*this);
rep->_return_type = rtype;
rep->_parameters = params;
return CPPType::new_type(rep);
}
return this;
}
/**
* Returns true if the type, or any nested type within the type, is a
* CPPTBDType and thus isn't fully determined right now. In this case,
* calling resolve_type() may or may not resolve the type.
*/
bool CPPFunctionType::
is_tbd() const {
if (_return_type->is_tbd()) {
return true;
}
return _parameters == nullptr || _parameters->is_tbd();
}
/**
* Returns true if the type is considered a Plain Old Data (POD) type.
*/
bool CPPFunctionType::
is_trivial() const {
return false;
}
/**
*
*/
void CPPFunctionType::
output(ostream &out, int indent_level, CPPScope *scope, bool complete) const {
output(out, indent_level, scope, complete, -1);
}
/**
* The additional parameter allows us to specify the number of parameters we
* wish to show the default values for. If num_default_parameters is >= 0, it
* indicates the number of default parameter values to show on output.
* Otherwise, all parameter values are shown.
*/
void CPPFunctionType::
output(ostream &out, int indent_level, CPPScope *scope, bool complete,
int num_default_parameters) const {
if (_flags & F_trailing_return_type) {
// It was declared using trailing return type, so let's format it that
// way.
out << "auto(";
_parameters->output(out, scope, true, num_default_parameters);
out << ")";
if (_flags & F_const_method) {
out << " const";
}
if (_flags & F_noexcept) {
out << " noexcept";
}
if (_flags & F_final) {
out << " final";
}
if (_flags & F_override) {
out << " override";
}
out << " -> ";
_return_type->output(out, indent_level, scope, false);
} else {
_return_type->output(out, indent_level, scope, complete);
out << "(";
_parameters->output(out, scope, true, num_default_parameters);
out << ")";
if (_flags & F_const_method) {
out << " const";
}
if (_flags & F_noexcept) {
out << " noexcept";
}
if (_flags & F_final) {
out << " final";
}
if (_flags & F_override) {
out << " override";
}
}
}
/**
* Formats a C++-looking line that defines an instance of the given type, with
* the indicated name. In most cases this will be "type name", but some types
* have special exceptions.
*/
void CPPFunctionType::
output_instance(ostream &out, int indent_level, CPPScope *scope,
bool complete, const string &prename,
const string &name) const {
output_instance(out, indent_level, scope, complete, prename, name, -1);
}
/**
* The additional parameter allows us to specify the number of parameters we
* wish to show the default values for. If num_default_parameters is >= 0, it
* indicates the number of default parameter values to show on output.
* Otherwise, all parameter values are shown.
*/
void CPPFunctionType::
output_instance(ostream &out, int indent_level, CPPScope *scope,
bool complete, const string &prename,
const string &name, int num_default_parameters) const {
ostringstream parm_string;
parm_string << "(";
_parameters->output(parm_string, scope, true, num_default_parameters);
parm_string << ")";
string str = parm_string.str();
if (_flags & (F_constructor | F_destructor)) {
// No return type for constructors and destructors.
out << prename << name << str;
} else if (_flags & F_trailing_return_type) {
// It was declared using trailing return type, so let's format it that
// way.
out << "auto ";
if (prename.empty()) {
out << name;
} else {
out << "(" << prename << name << ")";
}
out << str;
} else if (_flags & F_operator_typecast) {
out << "operator ";
_return_type->output_instance(out, indent_level, scope, complete, "", prename + str);
} else {
if (prename.empty()) {
_return_type->output_instance(out, indent_level, scope, complete,
"", prename + name + str);
} else {
_return_type->output_instance(out, indent_level, scope, complete,
"", "(" + prename + name + ")" + str);
}
}
if (_flags & F_const_method) {
out << " const";
}
if (_flags & F_volatile_method) {
out << " volatile";
}
if (_flags & F_noexcept) {
out << " noexcept";
}
if (_flags & F_final) {
out << " final";
}
if (_flags & F_override) {
out << " override";
}
if (_flags & F_trailing_return_type) {
out << " -> ";
_return_type->output(out, indent_level, scope, false);
}
}
/**
* Returns the number of parameters in the list that may take default values.
*/
int CPPFunctionType::
get_num_default_parameters() const {
// The trick is just to count, beginning from the end and working towards
// the front, the number of parameters that have some initializer.
if (_parameters == nullptr) {
return 0;
}
const CPPParameterList::Parameters ¶ms = _parameters->_parameters;
CPPParameterList::Parameters::const_reverse_iterator pi;
int count = 0;
for (pi = params.rbegin();
pi != params.rend() && (*pi)->_initializer != nullptr;
++pi) {
count++;
}
return count;
}
/**
*
*/
CPPDeclaration::SubType CPPFunctionType::
get_subtype() const {
return ST_function;
}
/**
*
*/
CPPFunctionType *CPPFunctionType::
as_function_type() {
return this;
}
/**
* This is similar to is_equal(), except it is more forgiving: it considers
* the functions to be equivalent only if the return type and the types of all
* parameters match.
*
* Note that this isn't symmetric to account for covariant return types.
*/
bool CPPFunctionType::
match_virtual_override(const CPPFunctionType &other) const {
if (!_return_type->is_equivalent(*other._return_type) &&
!_return_type->is_convertible_to(other._return_type)) {
return false;
}
if (((_flags ^ other._flags) & ~(F_override | F_final)) != 0) {
return false;
}
if (!_parameters->is_equivalent(*other._parameters)) {
return false;
}
return true;
}
/**
* Called by CPPDeclaration() to determine whether this type is equivalent to
* another type of the same type.
*/
bool CPPFunctionType::
is_equal(const CPPDeclaration *other) const {
const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
assert(ot != nullptr);
if (_return_type != ot->_return_type) {
return false;
}
if (_flags != ot->_flags) {
return false;
}
if (_parameters == ot->_parameters) {
return true;
}
if (_parameters == nullptr || ot->_parameters == nullptr ||
*_parameters != *ot->_parameters) {
return false;
}
return true;
}
/**
* Called by CPPDeclaration() to determine whether this type should be ordered
* before another type of the same type, in an arbitrary but fixed ordering.
*/
bool CPPFunctionType::
is_less(const CPPDeclaration *other) const {
const CPPFunctionType *ot = ((CPPDeclaration *)other)->as_function_type();
assert(ot != nullptr);
if (_return_type != ot->_return_type) {
return _return_type < ot->_return_type;
}
if (_flags != ot->_flags) {
return _flags < ot->_flags;
}
if (_parameters == ot->_parameters) {
return 0;
}
if (_parameters == nullptr || ot->_parameters == nullptr) {
return _parameters < ot->_parameters;
}
return *_parameters < *ot->_parameters;
}