2013-10-22 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / gengtype-parse.c
blobe5204c1a71dd84df06663ff0b554f3ca68546097
1 /* Process source files and output type information.
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifdef GENERATOR_FILE
21 #include "bconfig.h"
22 #else
23 #include "config.h"
24 #endif
25 #include "system.h"
26 #include "gengtype.h"
28 /* This is a simple recursive-descent parser which understands a subset of
29 the C type grammar.
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. */
36 /* Simple one-token lookahead mechanism. */
38 struct token
40 const char *value;
41 int code;
42 bool valid;
44 static struct token T;
46 /* Retrieve the code of the current token; if there is no current token,
47 get the next one from the lexer. */
48 static inline int
49 token (void)
51 if (!T.valid)
53 T.code = yylex (&T.value);
54 T.valid = true;
56 return T.code;
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. */
61 static inline const char *
62 advance (void)
64 T.valid = false;
65 return T.value;
68 /* Diagnostics. */
70 /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
71 static const char *const token_names[] = {
72 "GTY",
73 "typedef",
74 "extern",
75 "static",
76 "union",
77 "struct",
78 "enum",
79 "...",
80 "ptr_alias",
81 "nested_ptr",
82 "a param<N>_is option",
83 "a number",
84 "a scalar type",
85 "an identifier",
86 "a string constant",
87 "a character constant",
88 "an array declarator",
89 "a C++ keyword to ignore"
92 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
93 static const char *const token_value_format[] = {
94 "%s",
95 "'%s'",
96 "'%s'",
97 "'%s'",
98 "'\"%s\"'",
99 "\"'%s'\"",
100 "'[%s]'",
101 "'%s'",
104 /* Produce a printable representation for a token defined by CODE and
105 VALUE. This sometimes returns pointers into malloc memory and
106 sometimes not, therefore it is unsafe to free the pointer it
107 returns, so that memory is leaked. This does not matter, as this
108 function is only used for diagnostics, and in a successful run of
109 the program there will be none. */
110 static const char *
111 print_token (int code, const char *value)
113 if (code < CHAR_TOKEN_OFFSET)
114 return xasprintf ("'%c'", code);
115 else if (code < FIRST_TOKEN_WITH_VALUE)
116 return xasprintf ("'%s'", token_names[code - CHAR_TOKEN_OFFSET]);
117 else if (!value)
118 return token_names[code - CHAR_TOKEN_OFFSET]; /* don't quote these */
119 else
120 return xasprintf (token_value_format[code - FIRST_TOKEN_WITH_VALUE],
121 value);
124 /* Convenience wrapper around print_token which produces the printable
125 representation of the current token. */
126 static inline const char *
127 print_cur_token (void)
129 return print_token (T.code, T.value);
132 /* Report a parse error on the current line, with diagnostic MSG.
133 Behaves as standard printf with respect to additional arguments and
134 format escapes. */
135 static void ATTRIBUTE_PRINTF_1
136 parse_error (const char *msg, ...)
138 va_list ap;
140 fprintf (stderr, "%s:%d: parse error: ",
141 get_input_file_name (lexer_line.file), lexer_line.line);
143 va_start (ap, msg);
144 vfprintf (stderr, msg, ap);
145 va_end (ap);
147 fputc ('\n', stderr);
149 hit_error = true;
152 /* If the next token does not have code T, report a parse error; otherwise
153 return the token's value. */
154 static const char *
155 require (int t)
157 int u = token ();
158 const char *v = advance ();
159 if (u != t)
161 parse_error ("expected %s, have %s",
162 print_token (t, 0), print_token (u, v));
163 return 0;
165 return v;
168 /* If the next token does not have one of the codes T1 or T2, report a
169 parse error; otherwise return the token's value. */
170 static const char *
171 require2 (int t1, int t2)
173 int u = token ();
174 const char *v = advance ();
175 if (u != t1 && u != t2)
177 parse_error ("expected %s or %s, have %s",
178 print_token (t1, 0), print_token (t2, 0),
179 print_token (u, v));
180 return 0;
182 return v;
185 /* Near-terminals. */
187 /* C-style string constant concatenation: STRING+
188 Bare STRING should appear nowhere else in this file. */
189 static const char *
190 string_seq (void)
192 const char *s1, *s2;
193 size_t l1, l2;
194 char *buf;
196 s1 = require (STRING);
197 if (s1 == 0)
198 return "";
199 while (token () == STRING)
201 s2 = advance ();
203 l1 = strlen (s1);
204 l2 = strlen (s2);
205 buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
206 memcpy (buf + l1, s2, l2 + 1);
207 XDELETE (CONST_CAST (char *, s2));
208 s1 = buf;
210 return s1;
214 /* The caller has detected a template declaration that starts
215 with TMPL_NAME. Parse up to the closing '>'. This recognizes
216 simple template declarations of the form ID<ID1,ID2,...,IDn>.
217 It does not try to parse anything more sophisticated than that.
219 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
221 static const char *
222 require_template_declaration (const char *tmpl_name)
224 char *str;
226 /* Recognize the opening '<'. */
227 require ('<');
228 str = concat (tmpl_name, "<", (char *) 0);
230 /* Read the comma-separated list of identifiers. */
231 while (token () != '>')
233 const char *id = require2 (ID, ',');
234 if (id == NULL)
235 id = ",";
236 str = concat (str, id, (char *) 0);
239 /* Recognize the closing '>'. */
240 require ('>');
241 str = concat (str, ">", (char *) 0);
243 return str;
247 /* typedef_name: either an ID, or a template type
248 specification of the form ID<t1,t2,...,tn>. */
250 static const char *
251 typedef_name (void)
253 const char *id = require (ID);
254 if (token () == '<')
255 return require_template_declaration (id);
256 else
257 return id;
260 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
261 static void
262 consume_balanced (int opener, int closer)
264 require (opener);
265 for (;;)
266 switch (token ())
268 default:
269 advance ();
270 break;
271 case '(':
272 consume_balanced ('(', ')');
273 break;
274 case '[':
275 consume_balanced ('[', ']');
276 break;
277 case '{':
278 consume_balanced ('{', '}');
279 break;
281 case '}':
282 case ']':
283 case ')':
284 if (token () != closer)
285 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
286 closer, token ());
287 advance ();
288 return;
290 case EOF_TOKEN:
291 parse_error ("unexpected end of file within %c%c-delimited construct",
292 opener, closer);
293 return;
297 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
298 expressions, until we encounter an end-of-statement marker (a ';' or
299 a '}') outside any such delimiters; absorb that too. */
301 static void
302 consume_until_eos (void)
304 for (;;)
305 switch (token ())
307 case ';':
308 advance ();
309 return;
311 case '{':
312 consume_balanced ('{', '}');
313 return;
315 case '(':
316 consume_balanced ('(', ')');
317 break;
319 case '[':
320 consume_balanced ('[', ']');
321 break;
323 case '}':
324 case ']':
325 case ')':
326 parse_error ("unmatched '%c' while scanning for ';'", token ());
327 return;
329 case EOF_TOKEN:
330 parse_error ("unexpected end of file while scanning for ';'");
331 return;
333 default:
334 advance ();
335 break;
339 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
340 expressions, until we encounter a comma or semicolon outside any
341 such delimiters; absorb that too. Returns true if the loop ended
342 with a comma. */
344 static bool
345 consume_until_comma_or_eos ()
347 for (;;)
348 switch (token ())
350 case ',':
351 advance ();
352 return true;
354 case ';':
355 advance ();
356 return false;
358 case '{':
359 consume_balanced ('{', '}');
360 return false;
362 case '(':
363 consume_balanced ('(', ')');
364 break;
366 case '[':
367 consume_balanced ('[', ']');
368 break;
370 case '}':
371 case ']':
372 case ')':
373 parse_error ("unmatched '%s' while scanning for ',' or ';'",
374 print_cur_token ());
375 return false;
377 case EOF_TOKEN:
378 parse_error ("unexpected end of file while scanning for ',' or ';'");
379 return false;
381 default:
382 advance ();
383 break;
388 /* GTY(()) option handling. */
389 static type_p type (options_p *optsp, bool nested);
391 /* Optional parenthesized string: ('(' string_seq ')')? */
392 static options_p
393 str_optvalue_opt (options_p prev)
395 const char *name = advance ();
396 const char *value = "";
397 if (token () == '(')
399 advance ();
400 value = string_seq ();
401 require (')');
403 return create_string_option (prev, name, value);
406 /* absdecl: type '*'*
407 -- a vague approximation to what the C standard calls an abstract
408 declarator. The only kinds that are actually used are those that
409 are just a bare type and those that have trailing pointer-stars.
410 Further kinds should be implemented if and when they become
411 necessary. Used only within GTY(()) option values, therefore
412 further GTY(()) tags within the type are invalid. Note that the
413 return value has already been run through adjust_field_type. */
414 static type_p
415 absdecl (void)
417 type_p ty;
418 options_p opts;
420 ty = type (&opts, true);
421 while (token () == '*')
423 ty = create_pointer (ty);
424 advance ();
427 if (opts)
428 parse_error ("nested GTY(()) options are invalid");
430 return adjust_field_type (ty, 0);
433 /* Type-option: '(' absdecl ')' */
434 static options_p
435 type_optvalue (options_p prev, const char *name)
437 type_p ty;
438 require ('(');
439 ty = absdecl ();
440 require (')');
441 return create_type_option (prev, name, ty);
444 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
445 static options_p
446 nestedptr_optvalue (options_p prev)
448 type_p ty;
449 const char *from, *to;
451 require ('(');
452 ty = absdecl ();
453 require (',');
454 to = string_seq ();
455 require (',');
456 from = string_seq ();
457 require (')');
459 return create_nested_ptr_option (prev, ty, to, from);
462 /* One GTY(()) option:
463 ID str_optvalue_opt
464 | PTR_ALIAS type_optvalue
465 | PARAM_IS type_optvalue
466 | NESTED_PTR nestedptr_optvalue
468 static options_p
469 option (options_p prev)
471 switch (token ())
473 case ID:
474 return str_optvalue_opt (prev);
476 case PTR_ALIAS:
477 advance ();
478 return type_optvalue (prev, "ptr_alias");
480 case PARAM_IS:
481 return type_optvalue (prev, advance ());
483 case NESTED_PTR:
484 advance ();
485 return nestedptr_optvalue (prev);
487 case USER_GTY:
488 advance ();
489 return create_string_option (prev, "user", "");
491 default:
492 parse_error ("expected an option keyword, have %s", print_cur_token ());
493 advance ();
494 return create_string_option (prev, "", "");
498 /* One comma-separated list of options. */
499 static options_p
500 option_seq (void)
502 options_p o;
504 o = option (0);
505 while (token () == ',')
507 advance ();
508 o = option (o);
510 return o;
513 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
514 static options_p
515 gtymarker (void)
517 options_p result = 0;
518 require (GTY_TOKEN);
519 require ('(');
520 require ('(');
521 if (token () != ')')
522 result = option_seq ();
523 require (')');
524 require (')');
525 return result;
528 /* Optional GTY marker. */
529 static options_p
530 gtymarker_opt (void)
532 if (token () != GTY_TOKEN)
533 return 0;
534 return gtymarker ();
539 /* Declarators. The logic here is largely lifted from c-parser.c.
540 Note that we do not have to process abstract declarators, which can
541 appear only in parameter type lists or casts (but see absdecl,
542 above). Also, type qualifiers are thrown out in gengtype-lex.l so
543 we don't have to do it. */
545 /* array_and_function_declarators_opt:
546 \epsilon
547 array_and_function_declarators_opt ARRAY
548 array_and_function_declarators_opt '(' ... ')'
550 where '...' indicates stuff we ignore except insofar as grouping
551 symbols ()[]{} must balance.
553 Subroutine of direct_declarator - do not use elsewhere. */
555 static type_p
556 array_and_function_declarators_opt (type_p ty)
558 if (token () == ARRAY)
560 const char *array = advance ();
561 return create_array (array_and_function_declarators_opt (ty), array);
563 else if (token () == '(')
565 /* We don't need exact types for functions. */
566 consume_balanced ('(', ')');
567 array_and_function_declarators_opt (ty);
568 return create_scalar_type ("function type");
570 else
571 return ty;
574 static type_p inner_declarator (type_p, const char **, options_p *, bool);
576 /* direct_declarator:
577 '(' inner_declarator ')'
578 '(' \epsilon ')' <-- C++ ctors/dtors
579 gtymarker_opt ID array_and_function_declarators_opt
581 Subroutine of declarator, mutually recursive with inner_declarator;
582 do not use elsewhere.
584 IN_STRUCT is true if we are called while parsing structures or classes. */
586 static type_p
587 direct_declarator (type_p ty, const char **namep, options_p *optsp,
588 bool in_struct)
590 /* The first token in a direct-declarator must be an ID, a
591 GTY marker, or an open parenthesis. */
592 switch (token ())
594 case GTY_TOKEN:
595 *optsp = gtymarker ();
596 /* fall through */
598 case ID:
599 *namep = require (ID);
600 /* If the next token is '(', we are parsing a function declaration.
601 Functions are ignored by gengtype, so we return NULL. */
602 if (token () == '(')
603 return NULL;
604 break;
606 case '(':
607 /* If the declarator starts with a '(', we have three options. We
608 are either parsing 'TYPE (*ID)' (i.e., a function pointer)
609 or 'TYPE(...)'.
611 The latter will be a constructor iff we are inside a
612 structure or class. Otherwise, it could be a typedef, but
613 since we explicitly reject typedefs inside structures, we can
614 assume that we found a ctor and return NULL. */
615 advance ();
616 if (in_struct && token () != '*')
618 /* Found a constructor. Find and consume the closing ')'. */
619 while (token () != ')')
620 advance ();
621 advance ();
622 /* Tell the caller to ignore this. */
623 return NULL;
625 ty = inner_declarator (ty, namep, optsp, in_struct);
626 require (')');
627 break;
629 case IGNORABLE_CXX_KEYWORD:
630 /* Any C++ keyword like 'operator' means that we are not looking
631 at a regular data declarator. */
632 return NULL;
634 default:
635 parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
636 print_cur_token ());
637 /* Do _not_ advance if what we have is a close squiggle brace, as
638 we will get much better error recovery that way. */
639 if (token () != '}')
640 advance ();
641 return 0;
643 return array_and_function_declarators_opt (ty);
646 /* The difference between inner_declarator and declarator is in the
647 handling of stars. Consider this declaration:
649 char * (*pfc) (void)
651 It declares a pointer to a function that takes no arguments and
652 returns a char*. To construct the correct type for this
653 declaration, the star outside the parentheses must be processed
654 _before_ the function type, the star inside the parentheses must
655 be processed _after_ the function type. To accomplish this,
656 declarator() creates pointers before recursing (it is actually
657 coded as a while loop), whereas inner_declarator() recurses before
658 creating pointers. */
660 /* inner_declarator:
661 '*' inner_declarator
662 direct_declarator
664 Mutually recursive subroutine of direct_declarator; do not use
665 elsewhere.
667 IN_STRUCT is true if we are called while parsing structures or classes. */
669 static type_p
670 inner_declarator (type_p ty, const char **namep, options_p *optsp,
671 bool in_struct)
673 if (token () == '*')
675 type_p inner;
676 advance ();
677 inner = inner_declarator (ty, namep, optsp, in_struct);
678 if (inner == 0)
679 return 0;
680 else
681 return create_pointer (ty);
683 else
684 return direct_declarator (ty, namep, optsp, in_struct);
687 /* declarator: '*'+ direct_declarator
689 This is the sole public interface to this part of the grammar.
690 Arguments are the type known so far, a pointer to where the name
691 may be stored, and a pointer to where GTY options may be stored.
693 IN_STRUCT is true when we are called to parse declarators inside
694 a structure or class.
696 Returns the final type. */
698 static type_p
699 declarator (type_p ty, const char **namep, options_p *optsp,
700 bool in_struct = false)
702 *namep = 0;
703 *optsp = 0;
704 while (token () == '*')
706 advance ();
707 ty = create_pointer (ty);
709 return direct_declarator (ty, namep, optsp, in_struct);
712 /* Types and declarations. */
714 /* Structure field(s) declaration:
716 type bitfield ';'
717 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
720 Knows that such declarations must end with a close brace (or,
721 erroneously, at EOF).
723 static pair_p
724 struct_field_seq (void)
726 pair_p f = 0;
727 type_p ty, dty;
728 options_p opts, dopts;
729 const char *name;
730 bool another;
734 ty = type (&opts, true);
736 /* Ignore access-control keywords ("public:" etc). */
737 while (!ty && token () == IGNORABLE_CXX_KEYWORD)
739 const char *keyword = advance ();
740 if (strcmp (keyword, "public:") != 0
741 && strcmp (keyword, "private:") != 0
742 && strcmp (keyword, "protected:") != 0)
743 break;
744 ty = type (&opts, true);
747 if (!ty || token () == ':')
749 consume_until_eos ();
750 continue;
755 dty = declarator (ty, &name, &dopts, true);
757 /* There could be any number of weird things after the declarator,
758 notably bitfield declarations and __attribute__s. If this
759 function returns true, the last thing was a comma, so we have
760 more than one declarator paired with the current type. */
761 another = consume_until_comma_or_eos ();
763 if (!dty)
764 continue;
766 if (opts && dopts)
767 parse_error ("two GTY(()) options for field %s", name);
768 if (opts && !dopts)
769 dopts = opts;
771 f = create_field_at (f, dty, name, dopts, &lexer_line);
773 while (another);
775 while (token () != '}' && token () != EOF_TOKEN);
776 return nreverse_pairs (f);
779 /* Return true if OPTS contain the option named STR. */
781 static bool
782 opts_have (options_p opts, const char *str)
784 for (options_p opt = opts; opt; opt = opt->next)
785 if (strcmp (opt->name, str) == 0)
786 return true;
787 return false;
791 /* This is called type(), but what it parses (sort of) is what C calls
792 declaration-specifiers and specifier-qualifier-list:
794 SCALAR
795 | ID // typedef
796 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
797 | ENUM ID ( '{' ... '}' )?
799 Returns a partial type; under some conditions (notably
800 "struct foo GTY((...)) thing;") it may write an options
801 structure to *OPTSP.
803 NESTED is true when parsing a declaration already known to have a
804 GTY marker. In these cases, typedef and enum declarations are not
805 allowed because gengtype only understands types at the global
806 scope. */
808 static type_p
809 type (options_p *optsp, bool nested)
811 const char *s;
812 *optsp = 0;
813 switch (token ())
815 case SCALAR:
816 s = advance ();
817 return create_scalar_type (s);
819 case ID:
820 s = typedef_name ();
821 return resolve_typedef (s, &lexer_line);
823 case IGNORABLE_CXX_KEYWORD:
824 /* By returning NULL here, we indicate to the caller that they
825 should ignore everything following this keyword up to the
826 next ';' or '}'. */
827 return NULL;
829 case STRUCT:
830 case UNION:
832 options_p opts = 0;
833 /* GTY annotations follow attribute syntax
834 GTY_BEFORE_ID is for union/struct declarations
835 GTY_AFTER_ID is for variable declarations. */
836 enum
838 NO_GTY,
839 GTY_BEFORE_ID,
840 GTY_AFTER_ID
841 } is_gty = NO_GTY;
842 enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
843 advance ();
845 /* Top-level structures that are not explicitly tagged GTY(())
846 are treated as mere forward declarations. This is because
847 there are a lot of structures that we don't need to know
848 about, and some of those have C++ and macro constructs that
849 we cannot handle. */
850 if (nested || token () == GTY_TOKEN)
852 is_gty = GTY_BEFORE_ID;
853 opts = gtymarker_opt ();
856 if (token () == ID)
857 s = advance ();
858 else
859 s = xasprintf ("anonymous:%s:%d",
860 get_input_file_name (lexer_line.file),
861 lexer_line.line);
863 /* Unfortunately above GTY_TOKEN check does not capture the
864 typedef struct_type GTY case. */
865 if (token () == GTY_TOKEN)
867 is_gty = GTY_AFTER_ID;
868 opts = gtymarker_opt ();
871 if (token () == ':')
873 /* Skip over C++ inheritance specification. */
874 while (token () != '{')
875 advance ();
878 if (is_gty)
880 bool is_user_gty = opts_have (opts, "user");
881 if (token () == '{')
883 pair_p fields;
885 if (is_gty == GTY_AFTER_ID)
886 parse_error ("GTY must be specified before identifier");
888 if (!is_user_gty)
890 advance ();
891 fields = struct_field_seq ();
892 require ('}');
894 else
896 /* Do not look inside user defined structures. */
897 fields = NULL;
898 kind = TYPE_USER_STRUCT;
899 consume_balanced ('{', '}');
900 return create_user_defined_type (s, &lexer_line);
903 return new_structure (s, kind, &lexer_line, fields, opts);
906 else if (token () == '{')
907 consume_balanced ('{', '}');
908 if (opts)
909 *optsp = opts;
910 return find_structure (s, kind);
913 case TYPEDEF:
914 /* In C++, a typedef inside a struct/class/union defines a new
915 type for that inner scope. We cannot support this in
916 gengtype because we have no concept of scoping.
918 We handle typedefs in the global scope separately (see
919 parse_file), so if we find a 'typedef', we must be inside
920 a struct. */
921 gcc_assert (nested);
922 parse_error ("typedefs not supported in structures marked with "
923 "automatic GTY markers. Use GTY((user)) to mark "
924 "this structure.");
925 advance ();
926 return NULL;
928 case ENUM:
929 advance ();
930 if (token () == ID)
931 s = advance ();
932 else
933 s = xasprintf ("anonymous:%s:%d",
934 get_input_file_name (lexer_line.file),
935 lexer_line.line);
937 if (token () == '{')
938 consume_balanced ('{', '}');
940 /* If after parsing the enum we are at the end of the statement,
941 and we are currently inside a structure, then this was an
942 enum declaration inside this scope.
944 We cannot support this for the same reason we cannot support
945 'typedef' inside structures (see the TYPEDEF handler above).
946 If this happens, emit an error and return NULL. */
947 if (nested && token () == ';')
949 parse_error ("enum definitions not supported in structures marked "
950 "with automatic GTY markers. Use GTY((user)) to mark "
951 "this structure.");
952 advance ();
953 return NULL;
956 return create_scalar_type (s);
958 default:
959 parse_error ("expected a type specifier, have %s", print_cur_token ());
960 advance ();
961 return create_scalar_type ("erroneous type");
965 /* Top level constructs. */
967 /* Dispatch declarations beginning with 'typedef'. */
969 static void
970 typedef_decl (void)
972 type_p ty, dty;
973 const char *name;
974 options_p opts;
975 bool another;
977 gcc_assert (token () == TYPEDEF);
978 advance ();
980 ty = type (&opts, false);
981 if (!ty)
982 return;
983 if (opts)
984 parse_error ("GTY((...)) cannot be applied to a typedef");
987 dty = declarator (ty, &name, &opts);
988 if (opts)
989 parse_error ("GTY((...)) cannot be applied to a typedef");
991 /* Yet another place where we could have junk (notably attributes)
992 after the declarator. */
993 another = consume_until_comma_or_eos ();
994 if (dty)
995 do_typedef (name, dty, &lexer_line);
997 while (another);
1000 /* Structure definition: type() does all the work. */
1002 static void
1003 struct_or_union (void)
1005 options_p dummy;
1006 type (&dummy, false);
1007 /* There may be junk after the type: notably, we cannot currently
1008 distinguish 'struct foo *function(prototype);' from 'struct foo;'
1009 ... we could call declarator(), but it's a waste of time at
1010 present. Instead, just eat whatever token is currently lookahead
1011 and go back to lexical skipping mode. */
1012 advance ();
1015 /* GC root declaration:
1016 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
1017 If the gtymarker is not present, we ignore the rest of the declaration. */
1018 static void
1019 extern_or_static (void)
1021 options_p opts, opts2, dopts;
1022 type_p ty, dty;
1023 const char *name;
1024 require2 (EXTERN, STATIC);
1026 if (token () != GTY_TOKEN)
1028 advance ();
1029 return;
1032 opts = gtymarker ();
1033 ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
1034 dty = declarator (ty, &name, &dopts);
1036 if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
1037 parse_error ("GTY((...)) specified more than once for %s", name);
1038 else if (opts2)
1039 opts = opts2;
1040 else if (dopts)
1041 opts = dopts;
1043 if (dty)
1045 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
1046 require2 (';', '=');
1050 /* Parse the file FNAME for GC-relevant declarations and definitions.
1051 This is the only entry point to this file. */
1052 void
1053 parse_file (const char *fname)
1055 yybegin (fname);
1056 for (;;)
1058 switch (token ())
1060 case EXTERN:
1061 case STATIC:
1062 extern_or_static ();
1063 break;
1065 case STRUCT:
1066 case UNION:
1067 struct_or_union ();
1068 break;
1070 case TYPEDEF:
1071 typedef_decl ();
1072 break;
1074 case EOF_TOKEN:
1075 goto eof;
1077 default:
1078 parse_error ("unexpected top level token, %s", print_cur_token ());
1079 goto eof;
1081 lexer_toplevel_done = 1;
1084 eof:
1085 advance ();
1086 yyend ();