forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppManifest.cxx
More file actions
434 lines (374 loc) · 9.25 KB
/
cppManifest.cxx
File metadata and controls
434 lines (374 loc) · 9.25 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
/**
* 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 cppManifest.cxx
* @author drose
* @date 1999-10-22
*/
#include "cppManifest.h"
#include "cppExpression.h"
#include <ctype.h>
using std::string;
/**
*
*/
CPPManifest::ExpansionNode::
ExpansionNode(int parm_number, bool stringify, bool paste) :
_parm_number(parm_number), _stringify(stringify), _paste(paste)
{
}
/**
*
*/
CPPManifest::ExpansionNode::
ExpansionNode(const string &str, bool paste) :
_parm_number(-1), _stringify(false), _paste(paste), _str(str)
{
}
/**
* Creates a manifest from a preprocessor definition.
*/
CPPManifest::
CPPManifest(const string &args, const cppyyltype &loc) :
_variadic_param(-1),
_loc(loc),
_expr(nullptr),
_vis(V_public)
{
assert(!args.empty());
assert(!isspace(args[0]));
// First, identify the manifest name.
size_t p = 0;
while (p < args.size() && !isspace(args[p]) && args[p] != '(') {
p++;
}
_name = args.substr(0, p);
vector_string parameter_names;
if (args[p] == '(') {
// Hmm, parameters.
_has_parameters = true;
parse_parameters(args, p, parameter_names);
_num_parameters = parameter_names.size();
p++;
} else {
_has_parameters = false;
_num_parameters = 0;
}
// Now identify the expansion. Skip whitespace.
while (p < args.size() && isspace(args[p])) {
p++;
}
save_expansion(args.substr(p), parameter_names);
}
/**
* Creates a custom manifest definition, for example as specified from a
* command-line -D option.
*/
CPPManifest::
CPPManifest(const string ¯o, const string &definition) :
_variadic_param(-1),
_expr(nullptr),
_vis(V_public)
{
_loc.first_line = 0;
_loc.first_column = 0;
_loc.last_line = 0;
_loc.last_column = 0;
assert(!macro.empty());
assert(!isspace(macro[0]));
// First, identify the manifest name.
size_t p = 0;
while (p < macro.size() && !isspace(macro[p]) && macro[p] != '(') {
p++;
}
_name = macro.substr(0, p);
vector_string parameter_names;
if (macro[p] == '(') {
// Hmm, parameters.
_has_parameters = true;
parse_parameters(macro, p, parameter_names);
_num_parameters = parameter_names.size();
p++;
} else {
_has_parameters = false;
_num_parameters = 0;
}
save_expansion(definition, parameter_names);
}
/**
*
*/
CPPManifest::
~CPPManifest() {
delete _expr;
}
/**
* This implements the stringification operator, #.
*/
string CPPManifest::
stringify(const string &source) {
string result("\"");
enum {
S_escaped = 0x01,
S_single_quoted = 0x02,
S_double_quoted = 0x04,
S_quoted = 0x06,
};
int state = 0;
string::const_iterator it;
for (it = source.begin(); it != source.end(); ++it) {
char c = *it;
if ((state & S_escaped) == 0) {
switch (c) {
case '\\':
if (state & S_quoted) {
state |= S_escaped;
result += '\\';
}
break;
case '\'':
state ^= S_single_quoted;
break;
case '"':
state ^= S_double_quoted;
result += '\\';
break;
}
} else {
if (c == '\\' || c == '"') {
result += '\\';
}
state &= ~S_escaped;
}
result += c;
}
result += '"';
return result;
}
/**
*
*/
string CPPManifest::
expand(const vector_string &args) const {
string result;
Expansion::const_iterator ei;
for (ei = _expansion.begin(); ei != _expansion.end(); ++ei) {
if ((*ei)._parm_number >= 0) {
int i = (*ei)._parm_number;
string subst;
if (i < (int)args.size()) {
subst = args[i];
if (i == _variadic_param) {
for (++i; i < (int)args.size(); ++i) {
subst += ", " + args[i];
}
}
if ((*ei)._stringify) {
subst = stringify(subst);
}
} else if (i == _variadic_param && (*ei)._paste) {
// Special case GCC behavior: if __VA_ARGS__ is pasted to a comma and
// no arguments are passed, the comma is removed. MSVC does this
// automatically. Not sure if we should allow MSVC behavior as well.
if (!result.empty() && *result.rbegin() == ',') {
result.resize(result.size() - 1);
}
}
if (!subst.empty()) {
if (result.empty() || (*ei)._paste) {
result += subst;
} else {
result += ' ';
result += subst;
}
}
}
if (!(*ei)._str.empty()) {
if (result.empty() || (*ei)._paste) {
result += (*ei)._str;
} else {
result += ' ';
result += (*ei)._str;
}
}
}
return result;
}
/**
* Returns the type of the manifest, if it is known, or NULL if the type
* cannot be determined.
*/
CPPType *CPPManifest::
determine_type() const {
if (_expr != nullptr) {
return _expr->determine_type();
}
return nullptr;
}
/**
*
*/
void CPPManifest::
output(std::ostream &out) const {
out << _name;
if (_has_parameters) {
out << "(";
if (_num_parameters > 0) {
if (_variadic_param == 0) {
out << "...";
} else {
out << "$1";
}
for (int i = 1; i < _num_parameters; ++i) {
if (_variadic_param == i) {
out << ", ...";
} else {
out << ", $" << i + 1;
}
}
}
out << ")";
}
Expansion::const_iterator ei;
for (ei = _expansion.begin(); ei != _expansion.end(); ++ei) {
if ((*ei)._paste) {
out << " ## ";
} else {
out << " ";
}
if ((*ei)._parm_number >= 0) {
if ((*ei)._stringify) {
out << "#";
}
if ((*ei)._parm_number == _variadic_param) {
out << "__VA_ARGS__";
} else {
out << "$" << (*ei)._parm_number + 1;
}
}
if (!(*ei)._str.empty()) {
out << (*ei)._str;
}
}
}
/**
*
*/
void CPPManifest::
parse_parameters(const string &args, size_t &p,
vector_string ¶meter_names) {
assert(p < args.size());
assert(args[p] == '(');
p++;
while (p < args.size() && isspace(args[p])) {
p++;
}
while (p < args.size() && args[p] != ')') {
// Here's the beginning of a parm.
size_t q = p;
while (p < args.size() && !isspace(args[p]) &&
args[p] != ')' && args[p] != ',') {
p++;
}
// Check if it's a variadic parameter by checking if it ends with "...".
// This picks up both C99-style variadic macros and GCC-style variadic
// macros.
if (p - q >= 3 && args.compare(p - 3, 3, "...") == 0) {
_variadic_param = parameter_names.size();
parameter_names.push_back(args.substr(q, p - q - 3));
} else {
parameter_names.push_back(args.substr(q, p - q));
}
// Skip whitespace after the parameter name.
while (p < args.size() && isspace(args[p])) {
p++;
}
if (p < args.size() && args[p] == ',') {
p++;
// Skip whitespace after a comma.
while (p < args.size() && isspace(args[p])) {
p++;
}
}
}
}
/**
*
*/
void CPPManifest::
save_expansion(const string &exp, const vector_string ¶meter_names) {
// Walk through the expansion string. For each substring that is an
// identifier, check it against parameter_names.
size_t p = 0;
size_t last = 0;
bool stringify = false;
bool paste = false;
while (p < exp.size()) {
if (isalpha(exp[p]) || exp[p] == '_') {
// Here's the start of an identifier. Find the end of it.
size_t q = p;
p++;
while (p < exp.size() && (isalnum(exp[p]) || exp[p] == '_')) {
p++;
}
string ident = exp.substr(q, p - q);
// Is this identifier one of our parameters?
int pnum = -1;
if (ident == "__VA_ARGS__") {
// C99-style variadics, ie. #define macro(...) __VA_ARGS__
pnum = _variadic_param;
} else {
for (int i = 0; pnum == -1 && i < (int)parameter_names.size(); ++i) {
const string &pname = parameter_names[i];
if (pname == ident) {
pnum = i;
}
}
}
if (pnum != -1) {
// Yep!
if (last != q) {
_expansion.push_back(ExpansionNode(exp.substr(last, q - last), paste));
paste = false;
}
_expansion.push_back(ExpansionNode(pnum, stringify, paste));
stringify = false;
paste = false;
last = p;
}
} else if (exp[p] == '#') {
// This may be a stringification operator.
if (last != p) {
_expansion.push_back(ExpansionNode(exp.substr(last, p - last), paste));
paste = false;
}
++p;
if (p < exp.size() && exp[p] == '#') {
// Woah, this is a token-pasting operator.
paste = true;
++p;
} else {
// Mark that the next argument should be stringified.
stringify = true;
}
last = p;
} else if (isspace(exp[p])) {
if (last != p) {
_expansion.push_back(ExpansionNode(exp.substr(last, p - last), paste));
paste = false;
}
++p;
last = p;
} else {
++p;
}
}
if (last != p) {
_expansion.push_back(ExpansionNode(exp.substr(last, p - last), paste));
}
}