forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppType.cxx
More file actions
377 lines (340 loc) · 13.5 KB
/
cppType.cxx
File metadata and controls
377 lines (340 loc) · 13.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
// Filename: cppType.cxx
// Created by: drose (19Oct99)
//
////////////////////////////////////////////////////////////////////
//
// 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."
//
////////////////////////////////////////////////////////////////////
#include "cppType.h"
#include "cppTypedef.h"
#include <algorithm>
CPPType::Types CPPType::_types;
CPPType::PreferredNames CPPType::_preferred_names;
CPPType::AltNames CPPType::_alt_names;
bool CPPTypeCompare::
operator () (CPPType *a, CPPType *b) const {
return (*a) < (*b);
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::Constructor
// Access: Public
// Description:
////////////////////////////////////////////////////////////////////
CPPType::
CPPType(const CPPFile &file) :
CPPDeclaration(file)
{
_declaration = (CPPTypeDeclaration *)NULL;
// This is set true by interrogate when the "forcetype" keyword is
// used.
_forcetype = false;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::resolve_type
// Access: Public, Virtual
// Description: 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 *CPPType::
resolve_type(CPPScope *, CPPScope *) {
return this;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::is_tbd
// Access: Public, Virtual
// Description: 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 CPPType::
is_tbd() const {
return false;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::is_parameter_expr
// Access: Public, Virtual
// Description: Returns true if the type is a special parameter
// expression type.
//
// This sort of type is created to handle instance
// declarations that initially look like function
// prototypes.
////////////////////////////////////////////////////////////////////
bool CPPType::
is_parameter_expr() const {
return false;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::has_typedef_name
// Access: Public
// Description: Returns true if the type has even been typedef'ed and
// therefore has a simple name available to stand for
// it. Extension types are all implicitly typedef'ed on
// declaration.
////////////////////////////////////////////////////////////////////
bool CPPType::
has_typedef_name() const {
return !_typedefs.empty();
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_typedef_name
// Access: Public
// Description: Returns a string that can be used to name the type,
// if has_typedef_name() returned true. This will be
// the first typedef name applied to the type.
////////////////////////////////////////////////////////////////////
string CPPType::
get_typedef_name(CPPScope *scope) const {
if (_typedefs.empty()) {
return string();
} else {
return _typedefs.front()->get_local_name(scope);
}
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_simple_name
// Access: Public, Virtual
// Description: Returns a fundametal one-word name for the type.
// This name will not include any scoping operators or
// template parameters, so it may not be a compilable
// reference to the type.
////////////////////////////////////////////////////////////////////
string CPPType::
get_simple_name() const {
return get_local_name();
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_local_name
// Access: Public, Virtual
// Description: Returns the compilable, correct name for this type
// within the indicated scope. If the scope is NULL,
// within the scope the type is declared in.
////////////////////////////////////////////////////////////////////
string CPPType::
get_local_name(CPPScope *scope) const {
ostringstream ostrm;
output(ostrm, 0, scope, false);
return ostrm.str();
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_fully_scoped_name
// Access: Public, Virtual
// Description: Returns the compilable, correct name for the type,
// with completely explicit scoping.
////////////////////////////////////////////////////////////////////
string CPPType::
get_fully_scoped_name() const {
return get_local_name();
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_preferred_name
// Access: Public, Virtual
// Description: Returns the best name to use for the type from a
// programmer's point of view. This will typically be a
// typedef name if one is available, or the full C++
// name if it is not. The typedef may or may not be
// visible within the current scope, so this type name
// may not be compilable.
////////////////////////////////////////////////////////////////////
string CPPType::
get_preferred_name() const {
string preferred_name = get_preferred_name_for(this);
if (!preferred_name.empty()) {
return preferred_name;
}
return get_local_name();
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_num_alt_names
// Access: Public
// Description: Returns the number of "alternate" names for this
// type. The alternate names are alternate typedef
// names. This list might be empty, or it might be
// long. One of these names may or may not be the same
// as the "preferred" name.
////////////////////////////////////////////////////////////////////
int CPPType::
get_num_alt_names() const {
// We do a lookup based on the type's name, instead of its pointer,
// so we can resolve different expansions of the same type.
string tname = this->get_fully_scoped_name();
if (!tname.empty()) {
AltNames::const_iterator ai;
ai = _alt_names.find(tname);
if (ai != _alt_names.end()) {
const Names &names = (*ai).second;
return names.size();
}
}
return 0;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_alt_name
// Access: Public
// Description: Returns the nth "alternate" name for this
// type. See get_num_alt_names().
////////////////////////////////////////////////////////////////////
string CPPType::
get_alt_name(int n) const {
// We do a lookup based on the type's name, instead of its pointer,
// so we can resolve different expansions of the same type.
string tname = this->get_fully_scoped_name();
if (!tname.empty()) {
AltNames::const_iterator ai;
ai = _alt_names.find(tname);
if (ai != _alt_names.end()) {
const Names &names = (*ai).second;
if (n >= 0 && n < (int)names.size()) {
return names[n];
}
}
}
return string();
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::is_incomplete
// Access: Public, Virtual
// Description: Returns true if the type has not yet been fully
// specified, false if it has.
////////////////////////////////////////////////////////////////////
bool CPPType::
is_incomplete() const {
return false;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::is_equivalent
// Access: Public, Virtual
// Description: This is a little more forgiving than is_equal(): it
// returns true if the types appear to be referring to
// the same thing, even if they may have different
// pointers or somewhat different definitions. It's
// useful for parameter matching, etc.
////////////////////////////////////////////////////////////////////
bool CPPType::
is_equivalent(const CPPType &other) const {
if (get_subtype() != other.get_subtype()) {
return false;
}
return is_equal(&other);
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::output_instance
// Access: Public, Virtual
// Description: 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 CPPType::
output_instance(ostream &out, const string &name, CPPScope *scope) const {
output_instance(out, 0, scope, false, "", name);
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::output_instance
// Access: Public, Virtual
// Description: 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 CPPType::
output_instance(ostream &out, int indent_level, CPPScope *scope,
bool complete, const string &prename,
const string &name) const {
output(out, indent_level, scope, complete);
out << " " << prename << name;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::as_type
// Access: Public, Virtual
// Description:
////////////////////////////////////////////////////////////////////
CPPType *CPPType::
as_type() {
return this;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::new_type
// Access: Public, Static
// Description: This should be called whenever a new CPPType object
// is created. It will uniquify the type pointers by
// checking to see if some equivalent CPPType object has
// previously been created; if it has, it returns the
// old object and deletes the new one. Otherwise, it
// stores the new one and returns it.
////////////////////////////////////////////////////////////////////
CPPType *CPPType::
new_type(CPPType *type) {
pair<Types::iterator, bool> result = _types.insert(type);
if (result.second) {
// The insertion has taken place; thus, this is the first time
// this type has been declared.
assert(*result.first == type);
return type;
}
// The insertion has not taken place; thus, there was previously
// another equivalent type declared.
if (*result.first != type) {
// *** Something wrong here. Deleting this should always be safe;
// however, it's not. Thus, someone failed to call new_type() on
// a type pointer before saving it somewhere. Fix me soon. ****
delete type;
}
return *result.first;
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::record_alt_name_for
// Access: Public, Static
// Description: Records a global typedef name associated with the
// indicated Type. This will be an "alt" name, and it
// may also become the "preferred" name.
////////////////////////////////////////////////////////////////////
void CPPType::
record_alt_name_for(const CPPType *type, const string &name) {
if (!name.empty()) {
string tname = type->get_fully_scoped_name();
if (!tname.empty()) {
if (tname.find('<') != string::npos) {
// If the name contains a funny character like a template
// name, then we implicitly take the first typedef as the
// preferred name.
_preferred_names.insert(PreferredNames::value_type(tname, name));
}
Names &names = _alt_names[tname];
if (find(names.begin(), names.end(), name) == names.end()) {
// It's not already recorded as an alt name, so record it now.
names.push_back(name);
}
}
}
}
////////////////////////////////////////////////////////////////////
// Function: CPPType::get_preferred_name_for
// Access: Public, Static
// Description: Returns the previously-stored "preferred" name
// associated with the type, if any, or empty string if
// no name is associated.
////////////////////////////////////////////////////////////////////
string CPPType::
get_preferred_name_for(const CPPType *type) {
// We do a lookup based on the type's name, instead of its pointer,
// so we can resolve different expansions of the same type.
string tname = type->get_fully_scoped_name();
if (!tname.empty()) {
PreferredNames::const_iterator pi;
pi = _preferred_names.find(tname);
if (pi != _preferred_names.end()) {
return (*pi).second;
}
}
return string();
}