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