1 /* Type Analyzer for GNU C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* This file is the type analyzer for GNU C++. To debug it, define SPEW_DEBUG
25 when compiling parse.c and spew.c. */
38 /* This takes a token stream that hasn't decided much about types and
39 tries to figure out as much as it can, with excessive lookahead and
42 /* fifo of tokens recognized and available to parser. */
44 /* The values for YYCHAR will fit in a short. */
50 static int do_aggr PARAMS ((void));
51 static void scan_tokens PARAMS ((unsigned int));
54 static int num_tokens PARAMS ((void));
55 static struct token *nth_token PARAMS ((int));
56 static void add_token PARAMS ((struct token *));
57 static void consume_token PARAMS ((void));
58 static int debug_yychar PARAMS ((int));
62 /* the declaration found for the last IDENTIFIER token read in.
63 yylex must look this up to detect typedefs, which get token type TYPENAME,
64 so it is left around in case the identifier is not a typedef but is
65 used in a context which makes it a reference to a variable. */
66 extern tree lastiddecl; /* let our brains leak out here too */
67 extern int yychar; /* the lookahead symbol */
68 extern YYSTYPE yylval; /* the semantic value of the */
69 /* lookahead symbol */
70 extern int end_of_file;
72 struct obstack token_obstack;
77 static unsigned int yylex_ctr = 0;
78 static int debug_yychar ();
81 /* Initialize token_obstack. Called once, from init_parse. */
86 gcc_obstack_init (&token_obstack);
90 /* Use functions for debugging... */
92 /* Return the number of tokens available on the fifo. */
97 return (obstack_object_size (&token_obstack) / sizeof (struct token))
101 /* Fetch the token N down the line from the head of the fifo. */
107 /* could just have this do slurp_ implicitly, but this way is easier
109 my_friendly_assert (n < num_tokens (), 298);
110 return ((struct token*)obstack_base (&token_obstack)) + n + first_token;
113 /* Add a token to the token fifo. */
119 obstack_grow (&token_obstack, t, sizeof (struct token));
122 /* Consume the next token out of the fifo. */
127 if (num_tokens () == 1)
129 obstack_free (&token_obstack, obstack_base (&token_obstack));
137 /* ...otherwise use macros. */
139 #define num_tokens() \
140 ((obstack_object_size (&token_obstack) / sizeof (struct token)) - first_token)
142 #define nth_token(N) \
143 (((struct token*)obstack_base (&token_obstack))+(N)+first_token)
145 #define add_token(T) obstack_grow (&token_obstack, (T), sizeof (struct token))
147 #define consume_token() \
148 (num_tokens () == 1 \
149 ? (obstack_free (&token_obstack, obstack_base (&token_obstack)), \
154 /* Pull in enough tokens from real_yylex that the queue is N long beyond
155 the current token. */
164 /* We cannot read past certain tokens, so make sure we don't. */
171 /* Never read past these characters: they might separate
172 the current input stream from one we save away later. */
173 if (tmp->yychar == '{' || tmp->yychar == ':' || tmp->yychar == ';')
177 while (num_tokens () <= n)
179 obstack_blank (&token_obstack, sizeof (struct token));
180 tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
181 tmp->yychar = real_yylex ();
182 tmp->end_of_file = end_of_file;
183 tmp->yylval = yylval;
185 if (tmp->yychar == '{'
186 || tmp->yychar == ':'
187 || tmp->yychar == ';')
190 while (num_tokens () <= n)
192 obstack_blank (&token_obstack, sizeof (struct token));
193 tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
195 tmp->end_of_file = 0;
202 /* Value is 1 (or 2) if we should try to make the next identifier look like
203 a typename (when it may be a local variable or a class variable).
204 Value is 0 if we treat this name in a default fashion. */
205 extern int looking_for_typename;
206 int looking_for_template;
207 extern int do_snarf_defarg;
216 return nth_token (0)->yychar;
222 struct token tmp_token;
223 tree trrr = NULL_TREE;
224 int old_looking_for_typename = 0;
231 fprintf (stderr, "\t\t## %d ##", yylex_ctr);
237 my_friendly_assert (num_tokens () == 0, 2837);
238 tmp_token.yychar = DEFARG;
239 tmp_token.yylval.ttype = snarf_defarg ();
240 tmp_token.end_of_file = 0;
242 add_token (&tmp_token);
245 /* if we've got tokens, send them */
246 else if (num_tokens ())
247 tmp_token= *nth_token (0);
250 /* if not, grab the next one and think about it */
251 tmp_token.yychar = real_yylex ();
252 tmp_token.yylval = yylval;
253 tmp_token.end_of_file = end_of_file;
254 add_token (&tmp_token);
257 /* many tokens just need to be returned. At first glance, all we
258 have to do is send them back up, but some of them are needed to
259 figure out local context. */
260 switch (tmp_token.yychar)
263 /* This is a lexical no-op. */
267 debug_yychar (tmp_token.yychar);
273 if (nth_token (1)->yychar == ')')
276 tmp_token.yychar = LEFT_RIGHT;
283 if (nth_token (1)->yychar == SCOPE)
285 /* Don't interfere with the setting from an 'aggr' prefix. */
286 old_looking_for_typename = looking_for_typename;
287 looking_for_typename = 1;
289 else if (nth_token (1)->yychar == '<')
290 looking_for_template = 1;
292 trrr = lookup_name (tmp_token.yylval.ttype, -2);
296 tmp_token.yychar = identifier_type (trrr);
297 switch (tmp_token.yychar)
305 /* If this got special lookup, remember it. In these
306 cases, we know it can't be a declarator-id. */
307 if (got_scope || got_object)
308 tmp_token.yylval.ttype = trrr;
317 my_friendly_abort (101);
321 lastiddecl = NULL_TREE;
322 got_scope = NULL_TREE;
323 /* and fall through to... */
324 case IDENTIFIER_DEFN:
330 /* If we see a SCOPE next, restore the old value.
331 Otherwise, we got what we want. */
332 looking_for_typename = old_looking_for_typename;
333 looking_for_template = 0;
337 if (tmp_token.yylval.ttype == ridpointers[RID_EXTERN])
340 if (nth_token (1)->yychar == STRING)
342 tmp_token.yychar = EXTERN_LANG_STRING;
343 tmp_token.yylval.ttype = get_identifier
344 (TREE_STRING_POINTER (nth_token (1)->yylval.ttype));
348 /* If export, warn that it's unimplemented and go on. */
349 else if (tmp_token.yylval.ttype == ridpointers[RID_EXPORT])
351 warning ("keyword 'export' not implemented and will be ignored");
355 /* do_aggr needs to check if the previous token was `friend',
356 so just increment first_token instead of calling consume_token. */
361 /* do_aggr needs to check if the previous token was `new',
362 so just increment first_token instead of calling consume_token. */
367 /* If this provides a type for us, then revert lexical
368 state to standard state. */
369 looking_for_typename = 0;
374 *nth_token (0) = tmp_token;
376 /* fall through to output... */
378 /* Set this again, in case we are rescanning. */
379 looking_for_typename = 2;
380 /* fall through... */
385 /* class member lookup only applies to the first token after the object
386 expression, except for explicit destructor calls. */
387 if (tmp_token.yychar != '~')
388 got_object = NULL_TREE;
390 /* Clear looking_for_typename if we got 'enum { ... };'. */
391 if (tmp_token.yychar == '{' || tmp_token.yychar == ':'
392 || tmp_token.yychar == ';')
393 looking_for_typename = 0;
395 yylval = tmp_token.yylval;
396 yychar = tmp_token.yychar;
397 end_of_file = tmp_token.end_of_file;
400 debug_yychar (yychar);
406 /* token[0] == AGGR (struct/union/enum)
407 Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
408 If token[2] == '{' or ':' then it's TYPENAME_DEFN.
409 It's also a definition if it's a forward declaration (as in 'struct Foo;')
410 which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW. */
418 yc1 = nth_token (1)->yychar;
419 if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
421 yc2 = nth_token (2)->yychar;
424 /* It's a forward declaration iff we were not preceded by
425 'friend' or `new'. */
428 if (nth_token (-1)->yychar == SCSPEC
429 && nth_token (-1)->yylval.ttype == ridpointers[(int) RID_FRIEND])
431 if (nth_token (-1)->yychar == NEW)
435 else if (yc2 != '{' && yc2 != ':')
441 nth_token (1)->yychar = TYPENAME_DEFN;
444 nth_token (1)->yychar = PTYPENAME_DEFN;
447 nth_token (1)->yychar = IDENTIFIER_DEFN;
450 my_friendly_abort (102);
456 /* debug_yychar takes a yychar (token number) value and prints its name. */
463 extern char *debug_yytranslate ();
468 fprintf (stderr, "<%d: %c >\n", yy, yy);
471 fprintf (stderr, "<%d:%s>\n", yy, debug_yytranslate (yy));