]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gengtype-parse.c
Forgot the CL entry for the Hoyt and arcsine distributions.
[thirdparty/gcc.git] / gcc / gengtype-parse.c
CommitLineData
703d89ab 1/* Process source files and output type information.
0823efed 2 Copyright (C) 2006, 2007, 2010, 2012 Free Software Foundation, Inc.
703d89ab 3
e1b793e7 4 This file is part of GCC.
703d89ab 5
e1b793e7
BS
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
703d89ab 10
e1b793e7
BS
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
703d89ab 15
e1b793e7
BS
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
703d89ab 19
f142b5bc 20#ifdef GENERATOR_FILE
703d89ab 21#include "bconfig.h"
f142b5bc
RG
22#else
23#include "config.h"
24#endif
703d89ab
ZW
25#include "system.h"
26#include "gengtype.h"
27
28/* This is a simple recursive-descent parser which understands a subset of
29 the C type grammar.
30
31 Rule functions are suffixed _seq if they scan a sequence of items;
32 _opt if they may consume zero tokens; _seqopt if both are true. The
33 "consume_" prefix indicates that a sequence of tokens is parsed for
34 syntactic correctness and then thrown away. */
35
36/* Simple one-token lookahead mechanism. */
37
38struct token
39{
40 const char *value;
41 int code;
42 bool valid;
43};
44static struct token T;
45
46/* Retrieve the code of the current token; if there is no current token,
47 get the next one from the lexer. */
48static inline int
49token (void)
50{
51 if (!T.valid)
52 {
53 T.code = yylex (&T.value);
54 T.valid = true;
55 }
56 return T.code;
57}
58
59/* Retrieve the value of the current token (if any) and mark it consumed.
60 The next call to token() will get another token from the lexer. */
61static inline const char *
62advance (void)
63{
64 T.valid = false;
65 return T.value;
66}
67
68/* Diagnostics. */
69
70/* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
71static const char *const token_names[] = {
72 "GTY",
73 "typedef",
74 "extern",
75 "static",
76 "union",
77 "struct",
78 "enum",
79 "VEC",
703d89ab
ZW
80 "...",
81 "ptr_alias",
82 "nested_ptr",
83 "a param<N>_is option",
84 "a number",
85 "a scalar type",
86 "an identifier",
87 "a string constant",
88 "a character constant",
89 "an array declarator",
90};
91
92/* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
93static const char *const token_value_format[] = {
94 "%s",
95 "'%s'",
96 "'%s'",
97 "'%s'",
98 "'\"%s\"'",
99 "\"'%s'\"",
100 "'[%s]'",
101};
102
103/* Produce a printable representation for a token defined by CODE and
104 VALUE. This sometimes returns pointers into malloc memory and
105 sometimes not, therefore it is unsafe to free the pointer it
106 returns, so that memory is leaked. This does not matter, as this
107 function is only used for diagnostics, and in a successful run of
108 the program there will be none. */
109static const char *
110print_token (int code, const char *value)
111{
112 if (code < CHAR_TOKEN_OFFSET)
113 return xasprintf ("'%c'", code);
114 else if (code < FIRST_TOKEN_WITH_VALUE)
115 return xasprintf ("'%s'", token_names[code - CHAR_TOKEN_OFFSET]);
116 else if (!value)
e1b793e7 117 return token_names[code - CHAR_TOKEN_OFFSET]; /* don't quote these */
703d89ab
ZW
118 else
119 return xasprintf (token_value_format[code - FIRST_TOKEN_WITH_VALUE],
120 value);
121}
122
123/* Convenience wrapper around print_token which produces the printable
124 representation of the current token. */
125static inline const char *
126print_cur_token (void)
127{
128 return print_token (T.code, T.value);
129}
130
131/* Report a parse error on the current line, with diagnostic MSG.
132 Behaves as standard printf with respect to additional arguments and
133 format escapes. */
134static void ATTRIBUTE_PRINTF_1
135parse_error (const char *msg, ...)
136{
137 va_list ap;
138
14c4815e
BS
139 fprintf (stderr, "%s:%d: parse error: ",
140 get_input_file_name (lexer_line.file), lexer_line.line);
703d89ab
ZW
141
142 va_start (ap, msg);
143 vfprintf (stderr, msg, ap);
144 va_end (ap);
145
ad58aabe
DN
146 fputc ('\n', stderr);
147
703d89ab
ZW
148 hit_error = true;
149}
150
151/* If the next token does not have code T, report a parse error; otherwise
152 return the token's value. */
153static const char *
154require (int t)
155{
156 int u = token ();
157 const char *v = advance ();
158 if (u != t)
159 {
160 parse_error ("expected %s, have %s",
161 print_token (t, 0), print_token (u, v));
162 return 0;
163 }
164 return v;
165}
166
167/* If the next token does not have one of the codes T1 or T2, report a
168 parse error; otherwise return the token's value. */
169static const char *
170require2 (int t1, int t2)
171{
172 int u = token ();
173 const char *v = advance ();
174 if (u != t1 && u != t2)
175 {
176 parse_error ("expected %s or %s, have %s",
177 print_token (t1, 0), print_token (t2, 0),
178 print_token (u, v));
179 return 0;
180 }
181 return v;
182}
183
184/* Near-terminals. */
185
186/* C-style string constant concatenation: STRING+
187 Bare STRING should appear nowhere else in this file. */
188static const char *
189string_seq (void)
190{
191 const char *s1, *s2;
192 size_t l1, l2;
193 char *buf;
194
195 s1 = require (STRING);
196 if (s1 == 0)
197 return "";
198 while (token () == STRING)
199 {
200 s2 = advance ();
201
202 l1 = strlen (s1);
203 l2 = strlen (s2);
e1b793e7 204 buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
703d89ab 205 memcpy (buf + l1, s2, l2 + 1);
b1d5455a 206 XDELETE (CONST_CAST (char *, s2));
703d89ab
ZW
207 s1 = buf;
208 }
209 return s1;
210}
211
0823efed
DN
212
213/* The caller has detected a template declaration that starts
214 with TMPL_NAME. Parse up to the closing '>'. This recognizes
215 simple template declarations of the form ID<ID1,ID2,...,IDn>.
216 It does not try to parse anything more sophisticated than that.
217
218 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
219
220static const char *
221require_template_declaration (const char *tmpl_name)
222{
223 char *str;
224
225 /* Recognize the opening '<'. */
226 require ('<');
227 str = concat (tmpl_name, "<", (char *) 0);
228
229 /* Read the comma-separated list of identifiers. */
230 while (token () != '>')
231 {
232 const char *id = require2 (ID, ',');
233 if (id == NULL)
234 id = ",";
235 str = concat (str, id, (char *) 0);
236 }
237
238 /* Recognize the closing '>'. */
239 require ('>');
240 str = concat (str, ">", (char *) 0);
241
242 return str;
243}
244
245
246/* typedef_name: either an ID, or VEC(x,y), or a template type
247 specification of the form ID<t1,t2,...,tn>.
248
249 FIXME cxx-conversion. VEC(x,y) is currently translated to the
250 template 'vec_t<x>'. This is to support the transition to C++ and
251 avoid re-writing all the 'VEC(x,y)' declarations in the code. This
252 needs to be fixed when the branch is merged into trunk. */
253
703d89ab
ZW
254static const char *
255typedef_name (void)
256{
257 if (token () == VEC_TOKEN)
258 {
0823efed 259 const char *c1, *r;
703d89ab
ZW
260 advance ();
261 require ('(');
262 c1 = require2 (ID, SCALAR);
263 require (',');
0823efed 264 require (ID);
703d89ab 265 require (')');
0823efed 266 r = concat ("vec_t<", c1, ">", (char *) 0);
b1d5455a 267 free (CONST_CAST (char *, c1));
703d89ab
ZW
268 return r;
269 }
0823efed
DN
270
271 const char *id = require (ID);
272 if (token () == '<')
273 return require_template_declaration (id);
703d89ab 274 else
0823efed 275 return id;
703d89ab
ZW
276}
277
278/* Absorb a sequence of tokens delimited by balanced ()[]{}. */
279static void
280consume_balanced (int opener, int closer)
281{
282 require (opener);
283 for (;;)
284 switch (token ())
285 {
e1b793e7
BS
286 default:
287 advance ();
288 break;
289 case '(':
290 consume_balanced ('(', ')');
291 break;
292 case '[':
293 consume_balanced ('[', ']');
294 break;
295 case '{':
296 consume_balanced ('{', '}');
297 break;
703d89ab
ZW
298
299 case '}':
300 case ']':
301 case ')':
302 if (token () != closer)
303 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
304 closer, token ());
e1b793e7
BS
305 advance ();
306 return;
703d89ab
ZW
307
308 case EOF_TOKEN:
309 parse_error ("unexpected end of file within %c%c-delimited construct",
310 opener, closer);
311 return;
312 }
313}
314
315/* Absorb a sequence of tokens, possibly including ()[]{}-delimited
316 expressions, until we encounter a semicolon outside any such
317 delimiters; absorb that too. If IMMEDIATE is true, it is an error
318 if the semicolon is not the first token encountered. */
319static void
320consume_until_semi (bool immediate)
321{
322 if (immediate && token () != ';')
323 require (';');
324 for (;;)
325 switch (token ())
326 {
e1b793e7
BS
327 case ';':
328 advance ();
329 return;
330 default:
331 advance ();
332 break;
703d89ab 333
e1b793e7
BS
334 case '(':
335 consume_balanced ('(', ')');
336 break;
337 case '[':
338 consume_balanced ('[', ']');
339 break;
340 case '{':
341 consume_balanced ('{', '}');
342 break;
703d89ab
ZW
343
344 case '}':
345 case ']':
346 case ')':
347 parse_error ("unmatched '%c' while scanning for ';'", token ());
e1b793e7 348 return;
703d89ab
ZW
349
350 case EOF_TOKEN:
351 parse_error ("unexpected end of file while scanning for ';'");
352 return;
353 }
354}
355
356/* Absorb a sequence of tokens, possibly including ()[]{}-delimited
357 expressions, until we encounter a comma or semicolon outside any
358 such delimiters; absorb that too. If IMMEDIATE is true, it is an
359 error if the comma or semicolon is not the first token encountered.
360 Returns true if the loop ended with a comma. */
361static bool
362consume_until_comma_or_semi (bool immediate)
363{
364 if (immediate && token () != ',' && token () != ';')
365 require2 (',', ';');
366 for (;;)
367 switch (token ())
368 {
e1b793e7
BS
369 case ',':
370 advance ();
371 return true;
372 case ';':
373 advance ();
374 return false;
375 default:
376 advance ();
377 break;
703d89ab 378
e1b793e7
BS
379 case '(':
380 consume_balanced ('(', ')');
381 break;
382 case '[':
383 consume_balanced ('[', ']');
384 break;
385 case '{':
386 consume_balanced ('{', '}');
387 break;
703d89ab
ZW
388
389 case '}':
390 case ']':
391 case ')':
392 parse_error ("unmatched '%s' while scanning for ',' or ';'",
393 print_cur_token ());
e1b793e7 394 return false;
703d89ab
ZW
395
396 case EOF_TOKEN:
397 parse_error ("unexpected end of file while scanning for ',' or ';'");
398 return false;
399 }
400}
703d89ab 401\f
e1b793e7 402
703d89ab
ZW
403/* GTY(()) option handling. */
404static type_p type (options_p *optsp, bool nested);
405
406/* Optional parenthesized string: ('(' string_seq ')')? */
407static options_p
408str_optvalue_opt (options_p prev)
409{
410 const char *name = advance ();
411 const char *value = "";
412 if (token () == '(')
413 {
414 advance ();
415 value = string_seq ();
416 require (')');
417 }
412dc29d 418 return create_string_option (prev, name, value);
703d89ab
ZW
419}
420
421/* absdecl: type '*'*
422 -- a vague approximation to what the C standard calls an abstract
423 declarator. The only kinds that are actually used are those that
424 are just a bare type and those that have trailing pointer-stars.
425 Further kinds should be implemented if and when they become
426 necessary. Used only within GTY(()) option values, therefore
427 further GTY(()) tags within the type are invalid. Note that the
428 return value has already been run through adjust_field_type. */
429static type_p
430absdecl (void)
431{
432 type_p ty;
433 options_p opts;
434
435 ty = type (&opts, true);
436 while (token () == '*')
437 {
438 ty = create_pointer (ty);
439 advance ();
440 }
441
442 if (opts)
443 parse_error ("nested GTY(()) options are invalid");
444
445 return adjust_field_type (ty, 0);
446}
447
448/* Type-option: '(' absdecl ')' */
449static options_p
450type_optvalue (options_p prev, const char *name)
451{
452 type_p ty;
453 require ('(');
454 ty = absdecl ();
455 require (')');
412dc29d 456 return create_type_option (prev, name, ty);
703d89ab
ZW
457}
458
459/* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
460static options_p
461nestedptr_optvalue (options_p prev)
462{
463 type_p ty;
464 const char *from, *to;
465
466 require ('(');
467 ty = absdecl ();
468 require (',');
469 to = string_seq ();
470 require (',');
471 from = string_seq ();
472 require (')');
473
474 return create_nested_ptr_option (prev, ty, to, from);
475}
476
477/* One GTY(()) option:
e1b793e7
BS
478 ID str_optvalue_opt
479 | PTR_ALIAS type_optvalue
480 | PARAM_IS type_optvalue
481 | NESTED_PTR nestedptr_optvalue
482*/
703d89ab
ZW
483static options_p
484option (options_p prev)
485{
486 switch (token ())
487 {
488 case ID:
489 return str_optvalue_opt (prev);
490
491 case PTR_ALIAS:
492 advance ();
493 return type_optvalue (prev, "ptr_alias");
494
495 case PARAM_IS:
496 return type_optvalue (prev, advance ());
497
498 case NESTED_PTR:
499 advance ();
500 return nestedptr_optvalue (prev);
501
0823efed
DN
502 case USER_GTY:
503 advance ();
504 return create_string_option (prev, "user", "");
505
703d89ab 506 default:
e1b793e7 507 parse_error ("expected an option keyword, have %s", print_cur_token ());
703d89ab 508 advance ();
412dc29d 509 return create_string_option (prev, "", "");
703d89ab
ZW
510 }
511}
512
513/* One comma-separated list of options. */
514static options_p
515option_seq (void)
516{
517 options_p o;
518
519 o = option (0);
520 while (token () == ',')
521 {
522 advance ();
523 o = option (o);
524 }
525 return o;
526}
527
528/* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
529static options_p
530gtymarker (void)
531{
532 options_p result = 0;
533 require (GTY_TOKEN);
534 require ('(');
535 require ('(');
536 if (token () != ')')
537 result = option_seq ();
538 require (')');
539 require (')');
540 return result;
541}
542
543/* Optional GTY marker. */
544static options_p
545gtymarker_opt (void)
546{
547 if (token () != GTY_TOKEN)
548 return 0;
549 return gtymarker ();
550}
551\f
552/* Declarators. The logic here is largely lifted from c-parser.c.
553 Note that we do not have to process abstract declarators, which can
554 appear only in parameter type lists or casts (but see absdecl,
555 above). Also, type qualifiers are thrown out in gengtype-lex.l so
556 we don't have to do it. */
557
558/* array_and_function_declarators_opt:
e1b793e7
BS
559 \epsilon
560 array_and_function_declarators_opt ARRAY
561 array_and_function_declarators_opt '(' ... ')'
703d89ab
ZW
562
563 where '...' indicates stuff we ignore except insofar as grouping
564 symbols ()[]{} must balance.
565
566 Subroutine of direct_declarator - do not use elsewhere. */
567
568static type_p
569array_and_function_declarators_opt (type_p ty)
570{
571 if (token () == ARRAY)
572 {
573 const char *array = advance ();
574 return create_array (array_and_function_declarators_opt (ty), array);
575 }
576 else if (token () == '(')
577 {
578 /* We don't need exact types for functions. */
579 consume_balanced ('(', ')');
580 array_and_function_declarators_opt (ty);
581 return create_scalar_type ("function type");
582 }
583 else
584 return ty;
585}
586
587static type_p inner_declarator (type_p, const char **, options_p *);
588
589/* direct_declarator:
e1b793e7
BS
590 '(' inner_declarator ')'
591 gtymarker_opt ID array_and_function_declarators_opt
703d89ab
ZW
592
593 Subroutine of declarator, mutually recursive with inner_declarator;
594 do not use elsewhere. */
595static type_p
596direct_declarator (type_p ty, const char **namep, options_p *optsp)
597{
598 /* The first token in a direct-declarator must be an ID, a
599 GTY marker, or an open parenthesis. */
600 switch (token ())
601 {
602 case GTY_TOKEN:
603 *optsp = gtymarker ();
604 /* fall through */
605 case ID:
606 *namep = require (ID);
607 break;
608
609 case '(':
610 advance ();
611 ty = inner_declarator (ty, namep, optsp);
612 require (')');
613 break;
614
615 default:
616 parse_error ("expected '(', 'GTY', or an identifier, have %s",
617 print_cur_token ());
618 /* Do _not_ advance if what we have is a close squiggle brace, as
619 we will get much better error recovery that way. */
620 if (token () != '}')
621 advance ();
622 return 0;
623 }
624 return array_and_function_declarators_opt (ty);
625}
626
627/* The difference between inner_declarator and declarator is in the
628 handling of stars. Consider this declaration:
629
e1b793e7 630 char * (*pfc) (void)
703d89ab
ZW
631
632 It declares a pointer to a function that takes no arguments and
633 returns a char*. To construct the correct type for this
634 declaration, the star outside the parentheses must be processed
635 _before_ the function type, the star inside the parentheses must
636 be processed _after_ the function type. To accomplish this,
637 declarator() creates pointers before recursing (it is actually
638 coded as a while loop), whereas inner_declarator() recurses before
639 creating pointers. */
640
641/* inner_declarator:
e1b793e7
BS
642 '*' inner_declarator
643 direct_declarator
703d89ab
ZW
644
645 Mutually recursive subroutine of direct_declarator; do not use
646 elsewhere. */
647
648static type_p
649inner_declarator (type_p ty, const char **namep, options_p *optsp)
650{
651 if (token () == '*')
652 {
653 type_p inner;
654 advance ();
655 inner = inner_declarator (ty, namep, optsp);
656 if (inner == 0)
657 return 0;
658 else
659 return create_pointer (ty);
660 }
661 else
662 return direct_declarator (ty, namep, optsp);
663}
664
665/* declarator: '*'+ direct_declarator
666
667 This is the sole public interface to this part of the grammar.
668 Arguments are the type known so far, a pointer to where the name
669 may be stored, and a pointer to where GTY options may be stored.
670 Returns the final type. */
671
672static type_p
673declarator (type_p ty, const char **namep, options_p *optsp)
674{
675 *namep = 0;
676 *optsp = 0;
677 while (token () == '*')
678 {
679 advance ();
680 ty = create_pointer (ty);
681 }
682 return direct_declarator (ty, namep, optsp);
683}
684\f
685/* Types and declarations. */
686
687/* Structure field(s) declaration:
688 (
e1b793e7
BS
689 type bitfield ';'
690 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
703d89ab
ZW
691 )+
692
693 Knows that such declarations must end with a close brace (or,
694 erroneously, at EOF).
e1b793e7 695*/
703d89ab
ZW
696static pair_p
697struct_field_seq (void)
698{
699 pair_p f = 0;
700 type_p ty, dty;
701 options_p opts, dopts;
702 const char *name;
703 bool another;
704
705 do
706 {
707 ty = type (&opts, true);
703d89ab
ZW
708
709 if (!ty || token () == ':')
710 {
711 consume_until_semi (false);
712 continue;
713 }
714
715 do
716 {
717 dty = declarator (ty, &name, &dopts);
718 /* There could be any number of weird things after the declarator,
719 notably bitfield declarations and __attribute__s. If this
720 function returns true, the last thing was a comma, so we have
721 more than one declarator paired with the current type. */
722 another = consume_until_comma_or_semi (false);
723
724 if (!dty)
725 continue;
726
727 if (opts && dopts)
728 parse_error ("two GTY(()) options for field %s", name);
729 if (opts && !dopts)
730 dopts = opts;
731
732 f = create_field_at (f, dty, name, dopts, &lexer_line);
733 }
734 while (another);
735 }
736 while (token () != '}' && token () != EOF_TOKEN);
737 return nreverse_pairs (f);
738}
739
0823efed
DN
740/* Return true if OPTS contain the option named STR. */
741
742static bool
743opts_have (options_p opts, const char *str)
744{
745 for (options_p opt = opts; opt; opt = opt->next)
746 if (strcmp (opt->name, str) == 0)
747 return true;
748 return false;
749}
750
751
703d89ab
ZW
752/* This is called type(), but what it parses (sort of) is what C calls
753 declaration-specifiers and specifier-qualifier-list:
754
e1b793e7 755 SCALAR
703d89ab
ZW
756 | ID // typedef
757 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
758 | ENUM ID ( '{' ... '}' )?
759
760 Returns a partial type; under some conditions (notably
761 "struct foo GTY((...)) thing;") it may write an options
762 structure to *OPTSP.
e1b793e7 763*/
703d89ab
ZW
764static type_p
765type (options_p *optsp, bool nested)
766{
767 const char *s;
703d89ab
ZW
768 *optsp = 0;
769 switch (token ())
770 {
771 case SCALAR:
772 s = advance ();
773 return create_scalar_type (s);
774
775 case ID:
776 case VEC_TOKEN:
777 s = typedef_name ();
778 return resolve_typedef (s, &lexer_line);
779
780 case STRUCT:
781 case UNION:
782 {
783 options_p opts = 0;
e1b793e7
BS
784 /* GTY annotations follow attribute syntax
785 GTY_BEFORE_ID is for union/struct declarations
786 GTY_AFTER_ID is for variable declarations. */
787 enum
788 {
789 NO_GTY,
790 GTY_BEFORE_ID,
791 GTY_AFTER_ID
792 } is_gty = NO_GTY;
0823efed 793 enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
703d89ab
ZW
794 advance ();
795
703d89ab
ZW
796 /* Top-level structures that are not explicitly tagged GTY(())
797 are treated as mere forward declarations. This is because
798 there are a lot of structures that we don't need to know
799 about, and some of those have weird macro stuff in them
800 that we can't handle. */
801 if (nested || token () == GTY_TOKEN)
802 {
e1b793e7
BS
803 is_gty = GTY_BEFORE_ID;
804 opts = gtymarker_opt ();
703d89ab 805 }
d1b38208
TG
806
807 if (token () == ID)
808 s = advance ();
809 else
a618dbe3
L
810 s = xasprintf ("anonymous:%s:%d",
811 get_input_file_name (lexer_line.file),
812 lexer_line.line);
d1b38208 813
e1b793e7
BS
814 /* Unfortunately above GTY_TOKEN check does not capture the
815 typedef struct_type GTY case. */
d1b38208
TG
816 if (token () == GTY_TOKEN)
817 {
e1b793e7
BS
818 is_gty = GTY_AFTER_ID;
819 opts = gtymarker_opt ();
d1b38208 820 }
b8698a0f 821
e1b793e7
BS
822 if (is_gty)
823 {
0823efed 824 bool is_user_gty = opts_have (opts, "user");
e1b793e7
BS
825 if (token () == '{')
826 {
827 pair_p fields;
828
829 if (is_gty == GTY_AFTER_ID)
830 parse_error ("GTY must be specified before identifier");
831
0823efed
DN
832 if (!is_user_gty)
833 {
834 advance ();
835 fields = struct_field_seq ();
836 require ('}');
837 }
838 else
839 {
840 /* Do not look inside user defined structures. */
841 fields = NULL;
842 kind = TYPE_USER_STRUCT;
843 consume_balanced ('{', '}');
844 }
845
846 return new_structure (s, kind, &lexer_line, fields, opts);
e1b793e7
BS
847 }
848 }
849 else if (token () == '{')
850 consume_balanced ('{', '}');
703d89ab
ZW
851 if (opts)
852 *optsp = opts;
0823efed 853 return find_structure (s, kind);
703d89ab
ZW
854 }
855
856 case ENUM:
857 advance ();
e1b793e7
BS
858 if (token () == ID)
859 s = advance ();
860 else
a618dbe3
L
861 s = xasprintf ("anonymous:%s:%d",
862 get_input_file_name (lexer_line.file),
863 lexer_line.line);
703d89ab
ZW
864
865 if (token () == '{')
e1b793e7 866 consume_balanced ('{', '}');
703d89ab
ZW
867 return create_scalar_type (s);
868
869 default:
870 parse_error ("expected a type specifier, have %s", print_cur_token ());
871 advance ();
872 return create_scalar_type ("erroneous type");
873 }
874}
875\f
876/* Top level constructs. */
877
878/* Dispatch declarations beginning with 'typedef'. */
879
880static void
881typedef_decl (void)
882{
883 type_p ty, dty;
884 const char *name;
885 options_p opts;
886 bool another;
887
888 gcc_assert (token () == TYPEDEF);
889 advance ();
890
891 ty = type (&opts, false);
892 if (!ty)
893 return;
894 if (opts)
895 parse_error ("GTY((...)) cannot be applied to a typedef");
896 do
897 {
898 dty = declarator (ty, &name, &opts);
899 if (opts)
900 parse_error ("GTY((...)) cannot be applied to a typedef");
901
902 /* Yet another place where we could have junk (notably attributes)
903 after the declarator. */
904 another = consume_until_comma_or_semi (false);
905 if (dty)
906 do_typedef (name, dty, &lexer_line);
907 }
908 while (another);
909}
910
911/* Structure definition: type() does all the work. */
912
913static void
914struct_or_union (void)
915{
916 options_p dummy;
917 type (&dummy, false);
918 /* There may be junk after the type: notably, we cannot currently
919 distinguish 'struct foo *function(prototype);' from 'struct foo;'
920 ... we could call declarator(), but it's a waste of time at
921 present. Instead, just eat whatever token is currently lookahead
922 and go back to lexical skipping mode. */
923 advance ();
924}
925
926/* GC root declaration:
e1b793e7 927 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
703d89ab
ZW
928 If the gtymarker is not present, we ignore the rest of the declaration. */
929static void
930extern_or_static (void)
931{
932 options_p opts, opts2, dopts;
933 type_p ty, dty;
934 const char *name;
935 require2 (EXTERN, STATIC);
936
937 if (token () != GTY_TOKEN)
938 {
939 advance ();
940 return;
941 }
942
943 opts = gtymarker ();
e1b793e7
BS
944 ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
945 dty = declarator (ty, &name, &dopts);
703d89ab
ZW
946
947 if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
948 parse_error ("GTY((...)) specified more than once for %s", name);
949 else if (opts2)
950 opts = opts2;
951 else if (dopts)
952 opts = dopts;
953
954 if (dty)
955 {
956 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
957 require2 (';', '=');
958 }
959}
960
703d89ab
ZW
961/* Parse the file FNAME for GC-relevant declarations and definitions.
962 This is the only entry point to this file. */
963void
964parse_file (const char *fname)
965{
966 yybegin (fname);
967 for (;;)
968 {
969 switch (token ())
970 {
971 case EXTERN:
972 case STATIC:
973 extern_or_static ();
974 break;
975
976 case STRUCT:
977 case UNION:
978 struct_or_union ();
979 break;
980
981 case TYPEDEF:
982 typedef_decl ();
983 break;
984
703d89ab
ZW
985 case EOF_TOKEN:
986 goto eof;
987
988 default:
989 parse_error ("unexpected top level token, %s", print_cur_token ());
990 goto eof;
991 }
992 lexer_toplevel_done = 1;
993 }
994
995 eof:
996 advance ();
997 yyend ();
998}