1 /* -*- indented-text -*- */
2 /* Process source files and output type information.
3 Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #define malloc xmalloc
27 #define realloc xrealloc
30 #include "gengtype-yacc.h"
32 #define YY_INPUT(BUF,RESULT,SIZE) ((RESULT) = macro_input (BUF,SIZE))
34 static unsigned macro_input (char *buffer, unsigned);
35 static const char *push_macro_expansion (const char *, unsigned,
36 const char *, unsigned);
37 static char *mangle_macro_name (const char *, unsigned,
38 const char *, unsigned);
39 static void update_lineno (const char *l, size_t len);
41 struct fileloc lexer_line;
42 int lexer_toplevel_done;
45 update_lineno (const char *l, size_t len)
54 ID [[:alpha:]_][[:alnum:]_]*
56 IWORD short|long|(un)?signed|char|int|HOST_WIDE_INT|HOST_WIDEST_INT|bool|size_t|BOOL_BITFIELD
57 ITYPE {IWORD}({WS}{IWORD})*
59 %x in_struct in_struct_comment in_comment in_yacc_escape
60 %option warn noyywrap nounput nodefault perf-report
61 %option 8bit never-interactive
64 [^[:alnum:]_]typedef{WS}(struct|union){WS}{ID}{WS}?[*[:space:]]{WS}?{ID}{WS}?";" {
73 tagstart = yytext + strlen (" typedef ");
74 while (ISSPACE (*tagstart))
76 union_p = tagstart[0] == 'u';
77 tagstart += strlen ("union ");
78 while (ISSPACE (*tagstart))
80 for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
82 for (namestart = tagstart + taglen;
83 ! ISIDNUM (*namestart);
85 if (*namestart == '*')
87 for (namelen = 1; ISIDNUM (namestart[namelen]); namelen++)
89 t = find_structure ((const char *) xmemdup (tagstart, taglen, taglen+1),
92 t = create_pointer (t);
93 namestart = (char *) xmemdup (namestart, namelen, namelen+1);
94 #ifdef USE_MAPPED_LOCATION
95 /* temporary kludge - gentype doesn't handle cpp conditionals */
96 if (strcmp (namestart, "location_t") != 0
97 && strcmp (namestart, "expanded_location") != 0)
99 do_typedef (namestart, t, &lexer_line);
100 update_lineno (yytext, yyleng);
103 [^[:alnum:]_]typedef{WS}{ITYPE}{WS}{ID}{WS}?";" {
111 for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
113 for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
115 namestart -= namelen - 1;
116 for (typestart = yytext + strlen (" typedef ");
120 for (typelen = namestart - typestart;
121 ISSPACE (typestart[typelen-1]);
125 t = create_scalar_type (typestart, typelen);
126 do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
128 update_lineno (yytext, yyleng);
131 [^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}PARAMS {
136 for (namestart = yytext + yyleng - 7; ISSPACE (*namestart); namestart--)
138 for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
140 namestart -= namelen - 1;
142 t = create_scalar_type ("function type", sizeof ("function type")-1);
143 do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
145 update_lineno (yytext, yyleng);
148 [^[:alnum:]_]typedef{WS}{ID}{WS}{ID}{WS}"(" {
153 for (namestart = yytext + yyleng - 2; ISSPACE (*namestart); namestart--)
155 for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++)
157 namestart -= namelen - 1;
159 t = create_scalar_type ("function type", sizeof ("function type")-1);
160 do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
162 update_lineno (yytext, yyleng);
165 [^[:alnum:]_]typedef{WS}{ID}{WS}?"*"?{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?PARAMS {
170 for (namestart = yytext + yyleng - 7; !ISIDNUM (*namestart); namestart--)
172 for (namelen = 1; ISIDNUM (namestart[-namelen]); namelen++)
174 namestart -= namelen - 1;
176 t = create_scalar_type ("function type", sizeof ("function type")-1);
177 do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
179 update_lineno (yytext, yyleng);
182 [^[:alnum:]_]typedef{WS}{ID}{WS}?"*"?{WS}?"("{WS}?"*"{WS}?{ID}{WS}?")"{WS}?"(" {
187 for (namestart = yytext + yyleng - 2; !ISIDNUM (*namestart); namestart--)
189 for (namelen = 1; ISIDNUM (namestart[-namelen]); namelen++)
191 namestart -= namelen - 1;
193 t = create_scalar_type ("function type", sizeof ("function type")-1);
194 do_typedef ((const char *) xmemdup (namestart, namelen, namelen+1), t,
196 update_lineno (yytext, yyleng);
199 [^[:alnum:]_](typedef{WS})?(struct|union){WS}{ID}{WS}/"GTY" {
205 typedef_p = yytext[1] == 't';
207 for (tagstart = yytext + strlen (" typedef ");
212 tagstart = yytext + 1;
214 union_p = tagstart[0] == 'u';
215 tagstart += strlen ("union ");
216 while (ISSPACE (*tagstart))
218 for (taglen = 1; ISIDNUM (tagstart[taglen]); taglen++)
221 yylval.t = find_structure ((const char *) xmemdup (tagstart, taglen,
225 update_lineno (yytext, yyleng);
226 return typedef_p ? ENT_TYPEDEF_STRUCT : ENT_STRUCT;
229 [^[:alnum:]_](extern|static){WS}/"GTY" {
231 update_lineno (yytext, yyleng);
232 return ENT_EXTERNSTATIC;
235 ^"%union"{WS}"{"{WS}/"GTY" {
237 update_lineno (yytext, yyleng);
238 return ENT_YACCUNION;
241 ^"DEF_VEC_"[[:alnum:]_]*{WS}?"("{WS}?{ID}{WS}?(","{WS}?{ID}{WS}?)*")" {
243 unsigned macro_len, arg_len;
245 const char *additional;
248 /* Find the macro name. */
249 for (macro = ptr; *ptr != '(' && !ISSPACE (*ptr); ptr++)
251 for (macro_len = ptr - macro; !(ISALNUM (*ptr) || *ptr == '_'); ptr++)
254 /* Find the argument(s). */
255 for (arg = ptr; *ptr != ')'; ptr++)
259 /* Create the struct and typedef. */
260 ptr = mangle_macro_name ("VEC", 3, arg, arg_len);
262 t = find_structure (ptr, 0);
263 do_typedef (ptr, t, &lexer_line);
265 /* Push the macro for later expansion. */
266 additional = push_macro_expansion (macro, macro_len, arg, arg_len);
270 ptr = mangle_macro_name (ptr, strlen (ptr),
271 additional, strlen (additional));
272 t = find_structure (ptr, 0);
273 do_typedef (ptr, t, &lexer_line);
279 "/*" { BEGIN(in_struct_comment); }
281 ^"%{" { BEGIN(in_yacc_escape); } /* } */
283 {WS} { update_lineno (yytext, yyleng); }
285 "const"/[^[:alnum:]_] /* don't care */
286 "GTY"/[^[:alnum:]_] { return GTY_TOKEN; }
287 "union"/[^[:alnum:]_] { return UNION; }
288 "struct"/[^[:alnum:]_] { return STRUCT; }
289 "enum"/[^[:alnum:]_] { return ENUM; }
290 "ptr_alias"/[^[:alnum:]_] { return ALIAS; }
291 "nested_ptr"/[^[:alnum:]_] { return NESTED_PTR; }
292 [0-9]+ { return NUM; }
293 "param"[0-9]*"_is"/[^[:alnum:]_] {
294 yylval.s = (const char *) xmemdup (yytext, yyleng, yyleng+1);
298 {IWORD}({WS}{IWORD})*/[^[:alnum:]_] |
299 "ENUM_BITFIELD"{WS}?"("{WS}?{ID}{WS}?")" {
302 for (len = yyleng; ISSPACE (yytext[len-1]); len--)
305 yylval.t = create_scalar_type (yytext, len);
306 update_lineno (yytext, yyleng);
310 "VEC"{WS}?"("{WS}?{ID}{WS}?(","{WS}?{ID}{WS}?)*")" {
312 unsigned macro_len, arg_len;
315 /* Find the macro name */
316 for (macro = ptr; *ptr != '(' && !ISSPACE (*ptr); ptr++)
318 for (macro_len = ptr - macro; !(ISALNUM(*ptr) || *ptr == '_'); ptr++)
321 /* Find the arguments. */
322 for (arg = ptr; *ptr != ')'; ptr++)
326 ptr = mangle_macro_name (macro, macro_len, arg, arg_len);
332 yylval.s = (const char *) xmemdup (yytext, yyleng, yyleng+1);
337 yylval.s = (const char *) xmemdup (yytext+1, yyleng-2, yyleng-1);
341 yylval.s = (const char *) xmemdup (yytext+1, yyleng-2, yyleng-1);
345 yylval.s = (const char *) xmemdup (yytext+1, yyleng-1, yyleng);
348 "'"("\\".|[^\\])"'" {
349 yylval.s = (const char *) xmemdup (yytext+1, yyleng-2, yyleng);
353 [(){},*:<>] { return yytext[0]; }
356 if (lexer_toplevel_done)
359 lexer_toplevel_done = 0;
366 return PERCENTPERCENT;
369 "#define"[^\n]*\n {lexer_line.line++;}
372 error_at_line (&lexer_line, "unexpected character `%s'", yytext);
376 "/*" { BEGIN(in_comment); }
377 \n { lexer_line.line++; }
379 "'"("\\".|[^\\])"'" |
380 [^"/\n] /* do nothing */
381 \"([^"\\]|\\.|\\\n)*\" { update_lineno (yytext, yyleng); }
382 "/"/[^*] /* do nothing */
384 <in_comment,in_struct_comment>{
385 \n { lexer_line.line++; }
387 [^*\n] /* do nothing */
388 "*"/[^/] /* do nothing */
390 <in_comment>"*/" { BEGIN(INITIAL); }
391 <in_struct_comment>"*/" { BEGIN(in_struct); }
394 \n { lexer_line.line++; }
396 [^%] /* do nothing */
397 "%"/[^}] /* do nothing */
398 "%}" { BEGIN(in_struct); }
400 error_at_line (&lexer_line,
401 "unterminated %%{; unexpected EOF");
407 <in_struct_comment,in_comment>"*" {
408 error_at_line (&lexer_line,
409 "unterminated comment or string; unexpected EOF");
412 ^"#define"{WS}"GTY(" /* do nothing */
414 error_at_line (&lexer_line, "stray GTY marker");
419 /* Deal with the expansion caused by the DEF_VEC_x macros. */
421 /* Mangle a macro and argument list as done by cpp concatenation in
422 the compiler proper. */
424 mangle_macro_name (const char *macro, unsigned macro_len,
425 const char *arg, unsigned arg_len)
427 char *ptr = (char *) xmemdup (macro, macro_len, macro_len + arg_len + 2);
429 /* Now copy and concatenate each argument */
432 ptr[macro_len++] = '_';
433 for (; arg_len && (ISALNUM(*arg) || *arg == '_'); arg_len--)
434 ptr[macro_len++] = *arg++;
435 for (; arg_len && !(ISALNUM(*arg) || *arg == '_'); arg_len--)
443 typedef struct macro_def
446 const char *expansion;
447 const char *additional;
452 const macro_def_t *def;
454 const char *args[10];
457 static const macro_def_t macro_defs[] =
459 #define IN_GENGTYPE 1
464 /* Chain of macro expansions to do at end of scanning. */
465 static macro_t *macro_expns;
466 static macro_t *macro_expns_end;
468 /* Push macro NAME (NAME_LEN) with argument ARG (ARG_LEN) onto the
469 expansion queue. We ensure NAME is known at this point. */
472 push_macro_expansion (const char *name, unsigned name_len,
473 const char *arg, unsigned arg_len)
477 for (ix = 0; macro_defs[ix].name; ix++)
478 if (strlen (macro_defs[ix].name) == name_len
479 && !memcmp (name, macro_defs[ix].name, name_len))
481 macro_t *expansion = XNEW (macro_t);
483 unsigned argno, last_arg;
485 expansion->def = ¯o_defs[ix];
486 expansion->next = NULL;
487 args = (char *) xmemdup (arg, arg_len, arg_len+1);
489 for (argno = 0; *args;)
491 expansion->args[argno++] = args;
492 while (*args && (ISALNUM (*args) || *args == '_'))
495 expansion->args[argno++] = "base";
499 while (*args && !(ISALNUM (*args) || *args == '_'))
503 for (; argno != 10; argno++)
504 expansion->args[argno] = NULL;
506 macro_expns_end->next = expansion;
508 macro_expns = expansion;
509 macro_expns_end = expansion;
510 if (macro_defs[ix].additional)
512 macro_t *expn2 = XNEW (macro_t);
513 memcpy (expn2, expansion, sizeof (*expn2));
516 expansion->args[last_arg++] = macro_defs[ix].additional;
517 macro_expns_end->next = expansion;
518 macro_expns_end = expansion;
520 if (last_arg > 2 && strcmp (expansion->args[last_arg - 1], "heap"))
521 expansion->args[last_arg++] = "GTY (())";
522 return macro_defs[ix].additional;
524 error_at_line (&lexer_line, "unrecognized macro `%.*s(%.*s)'",
525 name_len, name, arg_len, arg);
529 /* Attempt to read some input. Use fread until we're at the end of
530 file. At end of file expand the next queued macro. We presume the
531 buffer is large enough for the entire expansion. */
534 macro_input (char *buffer, unsigned size)
538 result = fread (buffer, 1, size, yyin);
541 else if (ferror (yyin))
542 YY_FATAL_ERROR ("read of source file failed");
543 else if (macro_expns)
548 for (expn = macro_expns->def->expansion; *expn; expn++)
554 argno = expn[1] - '0';
557 /* Remove inserted space? */
558 if (buffer[result-1] == ' ' && buffer[result-2] == '_')
561 /* Insert the argument value */
562 if (macro_expns->args[argno])
564 len = strlen (macro_expns->args[argno]);
565 memcpy (&buffer[result], macro_expns->args[argno], len);
569 /* Skip next space? */
570 if (expn[1] == ' ' && expn[2] == '_')
575 buffer[result++] = *expn;
576 if (*expn == ';' || *expn == '{')
577 buffer[result++] = '\n';
581 YY_FATAL_ERROR ("buffer too small to expand macro");
582 macro_expns = macro_expns->next;
584 macro_expns_end = NULL;
590 yyerror (const char *s)
592 error_at_line (&lexer_line, s);
596 parse_file (const char *fname)
598 yyin = fopen (fname, "r");
599 lexer_line.file = fname;