forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppIdentifier.cxx
More file actions
604 lines (532 loc) · 14.7 KB
/
cppIdentifier.cxx
File metadata and controls
604 lines (532 loc) · 14.7 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/**
* 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 cppIdentifier.cxx
* @author drose
* @date 1999-10-26
*/
#include "cppIdentifier.h"
#include "cppScope.h"
#include "cppTemplateScope.h"
#include "cppPreprocessor.h"
#include "cppTemplateParameterList.h"
#include "cppTBDType.h"
#include "cppStructType.h"
using std::string;
/**
*
*/
CPPIdentifier::
CPPIdentifier(const string &name, const CPPFile &file) {
_names.push_back(CPPNameComponent(name));
_native_scope = nullptr;
_loc.first_line = 0;
_loc.first_column = 0;
_loc.last_line = 0;
_loc.last_column = 0;
_loc.file = file;
}
/**
*
*/
CPPIdentifier::
CPPIdentifier(const CPPNameComponent &name, const CPPFile &file) {
_names.push_back(name);
_native_scope = nullptr;
_loc.first_line = 0;
_loc.first_column = 0;
_loc.last_line = 0;
_loc.last_column = 0;
_loc.file = file;
}
/**
*
*/
CPPIdentifier::
CPPIdentifier(const string &name, const cppyyltype &loc) : _loc(loc) {
_names.push_back(CPPNameComponent(name));
_native_scope = nullptr;
}
/**
*
*/
CPPIdentifier::
CPPIdentifier(const CPPNameComponent &name, const cppyyltype &loc) : _loc(loc) {
_names.push_back(name);
_native_scope = nullptr;
}
/**
*
*/
void CPPIdentifier::
add_name(const string &name) {
_names.push_back(CPPNameComponent(name));
}
/**
*
*/
void CPPIdentifier::
add_name(const CPPNameComponent &name) {
_names.push_back(name);
}
/**
*
*/
bool CPPIdentifier::
operator == (const CPPIdentifier &other) const {
if (_names.size() != other._names.size()) {
return false;
}
for (int i = 0; i < (int)_names.size(); ++i) {
if (_names[i] != other._names[i]) {
return false;
}
}
return true;
}
/**
*
*/
bool CPPIdentifier::
operator != (const CPPIdentifier &other) const {
return !(*this == other);
}
/**
*
*/
bool CPPIdentifier::
operator < (const CPPIdentifier &other) const {
if (_names.size() != other._names.size()) {
return _names.size() < other._names.size();
}
for (int i = 0; i < (int)_names.size(); ++i) {
if (_names[i] != other._names[i]) {
return _names[i] < other._names[i];
}
}
return false;
}
/**
*
*/
bool CPPIdentifier::
is_scoped() const {
return _names.size() > 1;
}
/**
*
*/
string CPPIdentifier::
get_simple_name() const {
return _names.back().get_name();
}
/**
*
*/
string CPPIdentifier::
get_local_name(CPPScope *scope) const {
assert(!_names.empty());
string result;
if (scope == nullptr || (_native_scope == nullptr && _names.size() == 1)) {
result = _names.back().get_name_with_templ(scope);
} else if (_names.front().empty()) {
result = get_fully_scoped_name();
} else {
// Determine the scope of everything up until but not including the last
// name.
CPPScope *my_scope = get_scope(scope, nullptr);
// Strip off template scopes, since they don't add anything particularly
// meaningful to the local name.
while (my_scope != nullptr && my_scope->as_template_scope() != nullptr) {
my_scope = my_scope->get_parent_scope();
}
if (my_scope == nullptr) {
result = get_fully_scoped_name();
} else if (my_scope == scope) {
return _names.back().get_name_with_templ(scope);
} else {
result = my_scope->get_local_name(scope);
if (!result.empty()) {
result += "::";
}
result += _names.back().get_name_with_templ(scope);
}
}
return result;
}
/**
*
*/
string CPPIdentifier::
get_fully_scoped_name() const {
assert(!_names.empty());
Names::const_iterator ni = _names.begin();
string name = (*ni).get_name_with_templ();
++ni;
while (ni != _names.end()) {
name += "::" + (*ni).get_name_with_templ();
++ni;
}
return name;
}
/**
* 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 CPPIdentifier::
is_fully_specified() const {
Names::const_iterator ni;
for (ni = _names.begin(); ni != _names.end(); ++ni) {
if ((*ni).has_templ() && !(*ni).get_templ()->is_fully_specified()) {
return false;
}
}
return true;
}
/**
* Returns true if the identifier includes a template parameter list that
* includes some not-yet-defined type.
*/
bool CPPIdentifier::
is_tbd() const {
Names::const_iterator ni;
for (ni = _names.begin(); ni != _names.end(); ++ni) {
if ((*ni).is_tbd()) {
return true;
}
}
return false;
}
/**
*
*/
CPPScope *CPPIdentifier::
get_scope(CPPScope *current_scope, CPPScope *global_scope,
CPPPreprocessor *error_sink) const {
assert(!_names.empty());
CPPScope *scope = _native_scope;
if (scope == nullptr) {
scope = current_scope;
}
int i = 0;
if (_names[i].empty()) {
// This identifier starts with a ::, thus it begins at the global scope.
scope = global_scope;
i++;
}
while (i + 1 < (int)_names.size() && scope != nullptr) {
// Check for an explicitly specialized scope first.
CPPScope *next_scope = scope->find_scope(_names[i].get_name_with_templ(), global_scope);
if (next_scope == nullptr) {
next_scope = scope->find_scope(_names[i].get_name(), global_scope);
if (next_scope == nullptr) {
if (error_sink != nullptr) {
error_sink->error("Symbol " + _names[i].get_name() +
" is not a known scope in " +
scope->get_fully_scoped_name(),
_loc);
}
return nullptr;
}
if (_names[i].has_templ()) {
next_scope = next_scope->instantiate(_names[i].get_templ(),
current_scope, global_scope);
}
}
scope = next_scope;
i++;
}
return scope;
}
/**
*
*/
CPPScope *CPPIdentifier::
get_scope(CPPScope *current_scope, CPPScope *global_scope,
CPPDeclaration::SubstDecl &subst,
CPPPreprocessor *error_sink) const {
assert(!_names.empty());
CPPScope *scope = _native_scope;
if (scope == nullptr) {
scope = current_scope;
}
int i = 0;
if (_names[i].empty()) {
// This identifier starts with a ::, thus it begins at the global scope.
scope = global_scope;
i++;
}
while (i + 1 < (int)_names.size() && scope != nullptr) {
CPPScope *next_scope = scope->find_scope(_names[i].get_name(), subst,
global_scope);
if (next_scope == nullptr) {
if (error_sink != nullptr) {
error_sink->error("Symbol " + _names[i].get_name() +
" is not a known scope in " +
scope->get_fully_scoped_name(),
_loc);
}
return nullptr;
}
if (_names[i].has_templ()) {
next_scope = next_scope->instantiate(_names[i].get_templ(),
current_scope, global_scope);
}
scope = next_scope;
i++;
}
return scope;
}
/**
* Looks up the identifier in the current and/or global scopes, and returns a
* CPPType pointer if it seems to refer to a type, or NULL if it does not. If
* force_instantiate is true, the type will be instantiated as fully as
* possible right now, even if it means instantiating it into an identical
* template type. Otherwise, the instantiation may be delayed for
* optimization reasons, and a CPPTBDType placeholder may be returned instead.
*/
CPPType *CPPIdentifier::
find_type(CPPScope *current_scope, CPPScope *global_scope,
bool force_instantiate,
CPPPreprocessor *error_sink) const {
CPPScope *scope = get_scope(current_scope, global_scope, error_sink);
if (scope == nullptr) {
return nullptr;
}
CPPType *type = nullptr;
if (!_names.back().has_templ()) {
type = scope->find_type(get_simple_name());
} else {
CPPDeclaration *decl = find_symbol(current_scope, global_scope, error_sink);
type = decl->as_type();
/*
if (type != NULL) {
if (!type->is_incomplete() || force_instantiate) {
type = type->instantiate(_names.back().get_templ(),
current_scope, global_scope,
error_sink)->as_type();
// If we ended up with another template, instantiate later.
if (type->is_template() && !force_instantiate) {
type = CPPType::new_type(new CPPTBDType((CPPIdentifier *)this));
}
} else {
// Otherwise, we'll have to instantiate the type later.
type = CPPType::new_type(new CPPTBDType((CPPIdentifier *)this));
}
// type->_file.replace_nearer(_file);
}
*/
}
return type;
}
/**
* This flavor of find_type() will instantiate any scope names in the
* identifier. It's useful for fully defining a type while instantiating a
* class.
*/
CPPType *CPPIdentifier::
find_type(CPPScope *current_scope, CPPScope *global_scope,
CPPDeclaration::SubstDecl &subst,
CPPPreprocessor *error_sink) const {
CPPScope *scope = get_scope(current_scope, global_scope, subst, error_sink);
if (scope == nullptr) {
return nullptr;
}
CPPType *type = scope->find_type(get_simple_name(), subst, global_scope);
if (type != nullptr && _names.back().has_templ()) {
// This is a template type.
if (is_fully_specified()) {
// If our identifier fully specifies the instantiation, then apply it.
CPPDeclaration *decl =
type->instantiate(_names.back().get_templ(),
current_scope, global_scope,
error_sink);
assert(decl != nullptr);
CPPType *new_type = decl->as_type();
assert(new_type != nullptr);
if (new_type == type) {
type = CPPType::new_type(new CPPTBDType((CPPIdentifier *)this));
} else {
type = new_type;
}
} else {
// Otherwise, we'll have to instantiate the type later.
type = CPPType::new_type(new CPPTBDType((CPPIdentifier *)this));
}
// type->_file.replace_nearer(_file);
}
return type;
}
/**
*
*/
CPPDeclaration *CPPIdentifier::
find_symbol(CPPScope *current_scope, CPPScope *global_scope,
CPPPreprocessor *error_sink) const {
CPPScope *scope = get_scope(current_scope, global_scope, error_sink);
if (scope == nullptr) {
return nullptr;
}
CPPDeclaration *sym;
if (!_names.back().has_templ()) {
if (_names.size() > 1 && scope->get_simple_name() == get_simple_name()) {
/**
*/
sym = scope->get_struct_type()->get_constructor();
} else {
sym = scope->find_symbol(get_simple_name());
}
} else {
sym = scope->find_template(get_simple_name());
if (sym != nullptr) {
CPPType *type = sym->as_type();
if (type != nullptr && type->is_incomplete()) {
// We can't instantiate an incomplete type.
sym = CPPType::new_type(new CPPTBDType((CPPIdentifier *)this));
} else {
// Instantiate the symbol.
sym = sym->instantiate(_names.back().get_templ(), current_scope,
global_scope, error_sink);
}
}
}
return sym;
}
/**
*
*/
CPPDeclaration *CPPIdentifier::
find_symbol(CPPScope *current_scope, CPPScope *global_scope,
CPPDeclaration::SubstDecl &subst,
CPPPreprocessor *error_sink) const {
CPPScope *scope = get_scope(current_scope, global_scope, subst, error_sink);
if (scope == nullptr) {
return nullptr;
}
CPPDeclaration *sym;
if (!_names.back().has_templ()) {
if (_names.size() > 1 && scope->get_simple_name() == get_simple_name()) {
/**
*/
sym = scope->get_struct_type()->get_constructor();
} else {
sym = scope->find_symbol(get_simple_name());
}
} else {
sym = scope->find_template(get_simple_name());
if (sym != nullptr) {
CPPType *type = sym->as_type();
if (type != nullptr && type->is_incomplete()) {
// We can't instantiate an incomplete type.
sym = CPPType::new_type(new CPPTBDType((CPPIdentifier *)this));
} else {
// Instantiate the symbol.
sym = sym->instantiate(_names.back().get_templ(), current_scope,
global_scope, error_sink);
}
}
}
return sym;
}
/**
*
*/
CPPDeclaration *CPPIdentifier::
find_template(CPPScope *current_scope, CPPScope *global_scope,
CPPPreprocessor *error_sink) const {
CPPScope *scope = get_scope(current_scope, global_scope, error_sink);
if (scope == nullptr) {
return nullptr;
}
return scope->find_template(get_simple_name());
}
/**
*
*/
CPPScope *CPPIdentifier::
find_scope(CPPScope *current_scope, CPPScope *global_scope,
CPPPreprocessor *error_sink) const {
CPPScope *scope = get_scope(current_scope, global_scope, error_sink);
if (scope == nullptr) {
return nullptr;
}
return scope->find_scope(get_simple_name(), global_scope);
}
/**
*
*/
CPPIdentifier *CPPIdentifier::
substitute_decl(CPPDeclaration::SubstDecl &subst,
CPPScope *current_scope, CPPScope *global_scope) {
CPPIdentifier *rep = new CPPIdentifier(*this);
bool anything_changed = false;
for (int i = 0; i < (int)rep->_names.size(); ++i) {
if (_names[i].has_templ()) {
rep->_names[i].set_templ
(_names[i].get_templ()->substitute_decl(subst, current_scope, global_scope));
if (rep->_names[i].get_templ() != _names[i].get_templ()) {
anything_changed = true;
}
}
}
if (!anything_changed) {
delete rep;
rep = this;
}
return rep;
}
/**
*
*/
void CPPIdentifier::
output(std::ostream &out, CPPScope *scope) const {
if (scope == nullptr) {
output_fully_scoped_name(out);
} else {
output_local_name(out, scope);
}
}
/**
*
*/
void CPPIdentifier::
output_local_name(std::ostream &out, CPPScope *scope) const {
assert(!_names.empty());
if (scope == nullptr || (_native_scope == nullptr && _names.size() == 1)) {
out << _names.back();
} else if (_names.front().empty()) {
output_fully_scoped_name(out);
} else {
// Determine the scope of everything up until but not including the last
// name.
CPPScope *my_scope = get_scope(scope, nullptr);
if (my_scope == nullptr) {
output_fully_scoped_name(out);
} else {
out << my_scope->get_local_name(scope) << "::" << _names.back();
}
}
}
/**
*
*/
void CPPIdentifier::
output_fully_scoped_name(std::ostream &out) const {
if (_native_scope != nullptr) {
_native_scope->output(out, nullptr);
out << "::";
}
Names::const_iterator ni = _names.begin();
out << (*ni);
++ni;
while (ni != _names.end()) {
out << "::" << (*ni);
++ni;
}
}