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
|
/* Type Analyzer for GNU C++.
Copyright (C) 1987, 89, 92-97, 98, 99, 2000 Free Software Foundation, Inc.
Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* This file is the type analyzer for GNU C++. To debug it, define SPEW_DEBUG
when compiling parse.c and spew.c. */
#include "config.h"
#include "system.h"
#include "input.h"
#include "tree.h"
#include "lex.h"
#include "cp-tree.h"
#include "parse.h"
#include "flags.h"
#include "obstack.h"
#include "toplev.h"
/* This takes a token stream that hasn't decided much about types and
tries to figure out as much as it can, with excessive lookahead and
backtracking. */
/* fifo of tokens recognized and available to parser. */
struct token {
/* The values for YYCHAR will fit in a short. */
short yychar;
short end_of_file;
YYSTYPE yylval;
};
static int do_aggr PARAMS ((void));
static void scan_tokens PARAMS ((unsigned int));
#ifdef SPEW_DEBUG
static int num_tokens PARAMS ((void));
static struct token *nth_token PARAMS ((int));
static void add_token PARAMS ((struct token *));
static void consume_token PARAMS ((void));
static int debug_yychar PARAMS ((int));
#endif
/* From lex.c: */
/* the declaration found for the last IDENTIFIER token read in.
yylex must look this up to detect typedefs, which get token type TYPENAME,
so it is left around in case the identifier is not a typedef but is
used in a context which makes it a reference to a variable. */
extern tree lastiddecl; /* let our brains leak out here too */
extern int yychar; /* the lookahead symbol */
extern YYSTYPE yylval; /* the semantic value of the */
/* lookahead symbol */
extern int end_of_file;
struct obstack token_obstack;
int first_token;
#ifdef SPEW_DEBUG
int spew_debug = 0;
static unsigned int yylex_ctr = 0;
static int debug_yychar ();
#endif
/* Initialize token_obstack. Called once, from init_parse. */
void
init_spew ()
{
gcc_obstack_init (&token_obstack);
}
#ifdef SPEW_DEBUG
/* Use functions for debugging... */
/* Return the number of tokens available on the fifo. */
static int
num_tokens ()
{
return (obstack_object_size (&token_obstack) / sizeof (struct token))
- first_token;
}
/* Fetch the token N down the line from the head of the fifo. */
static struct token*
nth_token (n)
int n;
{
/* could just have this do slurp_ implicitly, but this way is easier
to debug... */
my_friendly_assert (n < num_tokens (), 298);
return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
}
/* Add a token to the token fifo. */
static void
add_token (t)
struct token* t;
{
obstack_grow (&token_obstack, t, sizeof (struct token));
}
/* Consume the next token out of the fifo. */
static void
consume_token ()
{
if (num_tokens () == 1)
{
obstack_free (&token_obstack, obstack_base (&token_obstack));
first_token = 0;
}
else
first_token++;
}
#else
/* ...otherwise use macros. */
#define num_tokens() \
((obstack_object_size (&token_obstack) / sizeof (struct token)) - first_token)
#define nth_token(N) \
(((struct token*)obstack_base (&token_obstack))+(N)+first_token)
#define add_token(T) obstack_grow (&token_obstack, (T), sizeof (struct token))
#define consume_token() \
(num_tokens () == 1 \
? (obstack_free (&token_obstack, obstack_base (&token_obstack)), \
(first_token = 0)) \
: first_token++)
#endif
/* Pull in enough tokens from real_yylex that the queue is N long beyond
the current token. */
static void
scan_tokens (n)
unsigned int n;
{
unsigned int i;
struct token *tmp;
/* We cannot read past certain tokens, so make sure we don't. */
i = num_tokens ();
if (i > n)
return;
while (i-- > 0)
{
tmp = nth_token (i);
/* Never read past these characters: they might separate
the current input stream from one we save away later. */
if (tmp->yychar == '{' || tmp->yychar == ':' || tmp->yychar == ';')
goto pad_tokens;
}
while (num_tokens () <= n)
{
obstack_blank (&token_obstack, sizeof (struct token));
tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
tmp->yychar = real_yylex ();
tmp->end_of_file = end_of_file;
tmp->yylval = yylval;
end_of_file = 0;
if (tmp->yychar == '{'
|| tmp->yychar == ':'
|| tmp->yychar == ';')
{
pad_tokens:
while (num_tokens () <= n)
{
obstack_blank (&token_obstack, sizeof (struct token));
tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
tmp->yychar = EMPTY;
tmp->end_of_file = 0;
}
}
}
}
/* from lex.c: */
/* Value is 1 (or 2) if we should try to make the next identifier look like
a typename (when it may be a local variable or a class variable).
Value is 0 if we treat this name in a default fashion. */
extern int looking_for_typename;
int looking_for_template;
extern int do_snarf_defarg;
tree got_scope;
tree got_object;
int
peekyylex ()
{
scan_tokens (0);
return nth_token (0)->yychar;
}
int
yylex ()
{
struct token tmp_token;
tree trrr = NULL_TREE;
int old_looking_for_typename = 0;
retry:
#ifdef SPEW_DEBUG
if (spew_debug)
{
yylex_ctr ++;
fprintf (stderr, "\t\t## %d ##", yylex_ctr);
}
#endif
if (do_snarf_defarg)
{
my_friendly_assert (num_tokens () == 0, 2837);
tmp_token.yychar = DEFARG;
tmp_token.yylval.ttype = snarf_defarg ();
tmp_token.end_of_file = 0;
do_snarf_defarg = 0;
add_token (&tmp_token);
}
/* if we've got tokens, send them */
else if (num_tokens ())
tmp_token= *nth_token (0);
else
{
/* if not, grab the next one and think about it */
tmp_token.yychar = real_yylex ();
tmp_token.yylval = yylval;
tmp_token.end_of_file = end_of_file;
add_token (&tmp_token);
}
/* many tokens just need to be returned. At first glance, all we
have to do is send them back up, but some of them are needed to
figure out local context. */
switch (tmp_token.yychar)
{
case EMPTY:
/* This is a lexical no-op. */
consume_token ();
#ifdef SPEW_DEBUG
if (spew_debug)
debug_yychar (tmp_token.yychar);
#endif
goto retry;
case '(':
scan_tokens (1);
if (nth_token (1)->yychar == ')')
{
consume_token ();
tmp_token.yychar = LEFT_RIGHT;
}
consume_token ();
break;
case IDENTIFIER:
scan_tokens (1);
if (nth_token (1)->yychar == SCOPE)
{
/* Don't interfere with the setting from an 'aggr' prefix. */
old_looking_for_typename = looking_for_typename;
looking_for_typename = 1;
}
else if (nth_token (1)->yychar == '<')
looking_for_template = 1;
trrr = lookup_name (tmp_token.yylval.ttype, -2);
if (trrr)
{
tmp_token.yychar = identifier_type (trrr);
switch (tmp_token.yychar)
{
case TYPENAME:
case SELFNAME:
case NSNAME:
case PTYPENAME:
lastiddecl = trrr;
/* If this got special lookup, remember it. In these cases,
we don't have to worry about being a declarator-id. */
if (got_scope || got_object)
tmp_token.yylval.ttype = trrr;
break;
case PFUNCNAME:
case IDENTIFIER:
lastiddecl = trrr;
break;
default:
my_friendly_abort (101);
}
}
else
lastiddecl = NULL_TREE;
got_scope = NULL_TREE;
/* and fall through to... */
case IDENTIFIER_DEFN:
case TYPENAME:
case TYPENAME_DEFN:
case PTYPENAME:
case PTYPENAME_DEFN:
consume_token ();
/* If we see a SCOPE next, restore the old value.
Otherwise, we got what we want. */
looking_for_typename = old_looking_for_typename;
looking_for_template = 0;
break;
case SCSPEC:
if (tmp_token.yylval.ttype == ridpointers[RID_EXTERN])
{
scan_tokens (1);
if (nth_token (1)->yychar == STRING)
{
tmp_token.yychar = EXTERN_LANG_STRING;
tmp_token.yylval.ttype = get_identifier
(TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
consume_token ();
}
}
/* If export, warn that it's unimplemented and go on. */
else if (tmp_token.yylval.ttype == ridpointers[RID_EXPORT])
{
warning ("keyword 'export' not implemented and will be ignored");
consume_token ();
goto retry;
}
/* do_aggr needs to check if the previous token was `friend',
so just increment first_token instead of calling consume_token. */
++first_token;
break;
case NEW:
/* do_aggr needs to check if the previous token was `new',
so just increment first_token instead of calling consume_token. */
++first_token;
break;
case TYPESPEC:
/* If this provides a type for us, then revert lexical
state to standard state. */
looking_for_typename = 0;
consume_token ();
break;
case AGGR:
*nth_token (0) = tmp_token;
do_aggr ();
/* fall through to output... */
case ENUM:
/* Set this again, in case we are rescanning. */
looking_for_typename = 2;
/* fall through... */
default:
consume_token ();
}
/* class member lookup only applies to the first token after the object
expression, except for explicit destructor calls. */
if (tmp_token.yychar != '~')
got_object = NULL_TREE;
/* Clear looking_for_typename if we got 'enum { ... };'. */
if (tmp_token.yychar == '{' || tmp_token.yychar == ':'
|| tmp_token.yychar == ';')
looking_for_typename = 0;
yylval = tmp_token.yylval;
yychar = tmp_token.yychar;
end_of_file = tmp_token.end_of_file;
#ifdef SPEW_DEBUG
if (spew_debug)
debug_yychar (yychar);
#endif
return yychar;
}
/* token[0] == AGGR (struct/union/enum)
Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
If token[2] == '{' or ':' then it's TYPENAME_DEFN.
It's also a definition if it's a forward declaration (as in 'struct Foo;')
which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
static int
do_aggr ()
{
int yc1, yc2;
scan_tokens (2);
yc1 = nth_token (1)->yychar;
if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
return 0;
yc2 = nth_token (2)->yychar;
if (yc2 == ';')
{
/* It's a forward declaration iff we were not preceded by
'friend' or `new'. */
if (first_token > 0)
{
if (nth_token (-1)->yychar == SCSPEC
&& nth_token (-1)->yylval.ttype == ridpointers[(int) RID_FRIEND])
return 0;
if (nth_token (-1)->yychar == NEW)
return 0;
}
}
else if (yc2 != '{' && yc2 != ':')
return 0;
switch (yc1)
{
case TYPENAME:
nth_token (1)->yychar = TYPENAME_DEFN;
break;
case PTYPENAME:
nth_token (1)->yychar = PTYPENAME_DEFN;
break;
case IDENTIFIER:
nth_token (1)->yychar = IDENTIFIER_DEFN;
break;
default:
my_friendly_abort (102);
}
return 0;
}
#ifdef SPEW_DEBUG
/* debug_yychar takes a yychar (token number) value and prints its name. */
static int
debug_yychar (yy)
int yy;
{
/* In parse.y: */
extern char *debug_yytranslate ();
int i;
if (yy<256) {
fprintf (stderr, "<%d: %c >\n", yy, yy);
return 0;
}
fprintf (stderr, "<%d:%s>\n", yy, debug_yytranslate (yy));
return 1;
}
#endif
|