1 /* Type Analyzer for GNU C++.
2 Copyright (C) 1987, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This file is the type analyzer for GNU C++. To debug it, define SPEW_DEBUG
24 when compiling parse.c and spew.c. */
36 /* This takes a token stream that hasn't decided much about types and
37 tries to figure out as much as it can, with excessive lookahead and
40 /* fifo of tokens recognized and available to parser. */
42 /* The values for YYCHAR will fit in a short. */
48 static int do_aggr ();
51 /* the declaration found for the last IDENTIFIER token read in.
52 yylex must look this up to detect typedefs, which get token type TYPENAME,
53 so it is left around in case the identifier is not a typedef but is
54 used in a context which makes it a reference to a variable. */
55 extern tree lastiddecl; /* let our brains leak out here too */
56 extern int yychar; /* the lookahead symbol */
57 extern YYSTYPE yylval; /* the semantic value of the */
58 /* lookahead symbol */
59 extern int end_of_file;
61 struct obstack token_obstack;
66 static unsigned int yylex_ctr = 0;
67 static int debug_yychar ();
70 /* Initialize token_obstack. Called once, from init_lex. */
74 gcc_obstack_init(&token_obstack);
78 /* Use functions for debugging... */
80 /* Return the number of tokens available on the fifo. */
84 return (obstack_object_size(&token_obstack)/sizeof(struct token))
88 /* Fetch the token N down the line from the head of the fifo. */
93 /* could just have this do slurp_ implicitly, but this way is easier
95 my_friendly_assert (n < num_tokens(), 298);
96 return ((struct token*)obstack_base(&token_obstack))+n+first_token;
99 /* Add a token to the token fifo. */
104 obstack_grow(&token_obstack,t,sizeof (struct token));
107 /* Consume the next token out of the fifo. */
111 if (num_tokens() == 1)
113 obstack_free(&token_obstack, obstack_base (&token_obstack));
121 /* ...otherwise use macros. */
123 #define num_tokens() \
124 ((obstack_object_size(&token_obstack)/sizeof(struct token)) - first_token)
126 #define nth_token(N) \
127 (((struct token*)obstack_base(&token_obstack))+(N)+first_token)
129 #define add_token(T) obstack_grow(&token_obstack, (T), sizeof (struct token))
131 #define consume_token() \
133 ? (obstack_free (&token_obstack, obstack_base (&token_obstack)), \
138 /* Pull in enough tokens from real_yylex that the queue is N long beyond
139 the current token. */
148 /* We cannot read past certain tokens, so make sure we don't. */
155 /* Never read past these characters: they might separate
156 the current input stream from one we save away later. */
157 if (tmp->yychar == '{' || tmp->yychar == ':' || tmp->yychar == ';')
161 while (num_tokens() <= n)
163 obstack_blank(&token_obstack,sizeof (struct token));
164 tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
165 tmp->yychar = real_yylex();
166 tmp->end_of_file = end_of_file;
167 tmp->yylval = yylval;
169 if (tmp->yychar == '{'
170 || tmp->yychar == ':'
171 || tmp->yychar == ';')
174 while (num_tokens () <= n)
176 obstack_blank(&token_obstack,sizeof (struct token));
177 tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
179 tmp->end_of_file = 0;
186 probe_obstack (h, obj, nlevels)
189 unsigned int nlevels;
191 register struct _obstack_chunk* lp; /* below addr of any objects in this chunk */
192 register struct _obstack_chunk* plp; /* point to previous chunk if any */
195 /* We use >= rather than > since the object cannot be exactly at
196 the beginning of the chunk but might be an empty object exactly
197 at the end of an adjacent chunk. */
198 for (; nlevels != 0 && lp != 0 && ((tree)lp >= obj || (tree)lp->limit < obj);
204 return nlevels != 0 && lp != 0;
208 /* Value is 1 (or 2) if we should try to make the next identifier look like
209 a typename (when it may be a local variable or a class variable).
210 Value is 0 if we treat this name in a default fashion. */
211 extern int looking_for_typename;
212 int looking_for_template;
214 extern struct obstack *current_obstack, *saveable_obstack;
222 return nth_token (0)->yychar;
228 struct token tmp_token;
236 fprintf(stderr, "\t\t## %d ##",yylex_ctr);
240 /* if we've got tokens, send them */
243 tmp_token= *nth_token(0);
245 /* TMP_TOKEN.YYLVAL.TTYPE may have been allocated on the wrong obstack.
246 If we don't find it in CURRENT_OBSTACK's current or immediately
247 previous chunk, assume it was and copy it to the current obstack. */
248 if ((tmp_token.yychar == CONSTANT
249 || tmp_token.yychar == STRING)
250 && ! TREE_PERMANENT (tmp_token.yylval.ttype)
251 && ! probe_obstack (current_obstack, tmp_token.yylval.ttype, 2)
252 && ! probe_obstack (saveable_obstack, tmp_token.yylval.ttype, 2))
253 tmp_token.yylval.ttype = copy_node (tmp_token.yylval.ttype);
257 /* if not, grab the next one and think about it */
258 tmp_token.yychar = real_yylex ();
259 tmp_token.yylval = yylval;
260 tmp_token.end_of_file = end_of_file;
261 add_token(&tmp_token);
264 /* many tokens just need to be returned. At first glance, all we
265 * have to do is send them back up, but some of them are needed to
266 * figure out local context. */
267 switch(tmp_token.yychar)
270 /* This is a lexical no-op. */
274 debug_yychar (tmp_token.yychar);
280 if (nth_token (1)->yychar == SCOPE)
281 /* Don't interfere with the setting from an 'aggr' prefix. */
282 looking_for_typename++;
283 else if (nth_token (1)->yychar == '<')
284 looking_for_template = 1;
286 trrr = lookup_name (tmp_token.yylval.ttype, -2);
290 tmp_token.yychar = identifier_type (trrr);
291 switch (tmp_token.yychar)
294 lastiddecl = identifier_typedecl_value (tmp_token.yylval.ttype);
295 if (lastiddecl != trrr)
299 tmp_token.yylval.ttype = trrr;
306 lastiddecl = NULL_TREE;
311 tmp_token.yylval.ttype = trrr;
314 my_friendly_abort (101);
319 got_scope = NULL_TREE;
320 /* and fall through to... */
321 case IDENTIFIER_DEFN:
327 if (looking_for_typename > 0)
328 looking_for_typename--;
329 looking_for_template = 0;
334 /* do_aggr needs to check if the previous token was RID_NEW,
335 so just increment first_token instead of calling consume_token. */
344 *nth_token(0) = tmp_token;
346 /* fall through to output... */
348 /* Set this again, in case we are rescanning. */
349 looking_for_typename = 1;
350 /* fall through... */
355 got_object = NULL_TREE;
356 yylval = tmp_token.yylval;
357 yychar = tmp_token.yychar;
358 end_of_file = tmp_token.end_of_file;
361 debug_yychar(yychar);
366 /* token[0] == AGGR (struct/union/enum)
367 * Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
368 * If token[2] == '{' or ':' then it's TYPENAME_DEFN.
369 * It's also a definition if it's a forward declaration (as in 'struct Foo;')
370 * which we can tell if token[2] == ';' *and* token[-1] != FRIEND or NEW.
378 yc1 = nth_token (1)->yychar;
379 if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
381 yc2 = nth_token (2)->yychar;
384 /* It's a forward declaration iff we were not preceded by
385 'friend' or `new'. */
388 if (nth_token (-1)->yychar == SCSPEC
389 && nth_token (-1)->yylval.ttype == ridpointers[(int) RID_FRIEND])
391 if (nth_token (-1)->yychar == NEW)
395 else if (yc2 != '{' && yc2 != ':')
401 nth_token (1)->yychar = TYPENAME_DEFN;
404 nth_token (1)->yychar = PTYPENAME_DEFN;
407 nth_token (1)->yychar = IDENTIFIER_DEFN;
410 my_friendly_abort (102);
416 /* debug_yychar takes a yychar (token number) value and prints its name. */
422 extern char *debug_yytranslate ();
427 fprintf (stderr, "<%d: %c >\n", yy, yy);
430 fprintf (stderr, "<%d:%s>\n", yy, debug_yytranslate (yy));