2013-12-14 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / gengtype-parse.c
blob8328e3adf6e1d7d7c82f107f0508bf8baf7f2b56
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 /* As per require, but do not advance. */
169 static const char *
170 require_without_advance (int t)
172 int u = token ();
173 const char *v = T.value;
174 if (u != t)
176 parse_error ("expected %s, have %s",
177 print_token (t, 0), print_token (u, v));
178 return 0;
180 return v;
183 /* If the next token does not have one of the codes T1 or T2, report a
184 parse error; otherwise return the token's value. */
185 static const char *
186 require2 (int t1, int t2)
188 int u = token ();
189 const char *v = advance ();
190 if (u != t1 && u != t2)
192 parse_error ("expected %s or %s, have %s",
193 print_token (t1, 0), print_token (t2, 0),
194 print_token (u, v));
195 return 0;
197 return v;
200 /* Near-terminals. */
202 /* C-style string constant concatenation: STRING+
203 Bare STRING should appear nowhere else in this file. */
204 static const char *
205 string_seq (void)
207 const char *s1, *s2;
208 size_t l1, l2;
209 char *buf;
211 s1 = require (STRING);
212 if (s1 == 0)
213 return "";
214 while (token () == STRING)
216 s2 = advance ();
218 l1 = strlen (s1);
219 l2 = strlen (s2);
220 buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
221 memcpy (buf + l1, s2, l2 + 1);
222 XDELETE (CONST_CAST (char *, s2));
223 s1 = buf;
225 return s1;
229 /* The caller has detected a template declaration that starts
230 with TMPL_NAME. Parse up to the closing '>'. This recognizes
231 simple template declarations of the form ID<ID1,ID2,...,IDn>.
232 It does not try to parse anything more sophisticated than that.
234 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
236 static const char *
237 require_template_declaration (const char *tmpl_name)
239 char *str;
241 /* Recognize the opening '<'. */
242 require ('<');
243 str = concat (tmpl_name, "<", (char *) 0);
245 /* Read the comma-separated list of identifiers. */
246 while (token () != '>')
248 const char *id = require2 (ID, ',');
249 if (id == NULL)
250 id = ",";
251 str = concat (str, id, (char *) 0);
254 /* Recognize the closing '>'. */
255 require ('>');
256 str = concat (str, ">", (char *) 0);
258 return str;
262 /* typedef_name: either an ID, or a template type
263 specification of the form ID<t1,t2,...,tn>. */
265 static const char *
266 typedef_name (void)
268 const char *id = require (ID);
269 if (token () == '<')
270 return require_template_declaration (id);
271 else
272 return id;
275 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
276 static void
277 consume_balanced (int opener, int closer)
279 require (opener);
280 for (;;)
281 switch (token ())
283 default:
284 advance ();
285 break;
286 case '(':
287 consume_balanced ('(', ')');
288 break;
289 case '[':
290 consume_balanced ('[', ']');
291 break;
292 case '{':
293 consume_balanced ('{', '}');
294 break;
296 case '}':
297 case ']':
298 case ')':
299 if (token () != closer)
300 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
301 closer, token ());
302 advance ();
303 return;
305 case EOF_TOKEN:
306 parse_error ("unexpected end of file within %c%c-delimited construct",
307 opener, closer);
308 return;
312 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
313 expressions, until we encounter an end-of-statement marker (a ';' or
314 a '}') outside any such delimiters; absorb that too. */
316 static void
317 consume_until_eos (void)
319 for (;;)
320 switch (token ())
322 case ';':
323 advance ();
324 return;
326 case '{':
327 consume_balanced ('{', '}');
328 return;
330 case '(':
331 consume_balanced ('(', ')');
332 break;
334 case '[':
335 consume_balanced ('[', ']');
336 break;
338 case '}':
339 case ']':
340 case ')':
341 parse_error ("unmatched '%c' while scanning for ';'", token ());
342 return;
344 case EOF_TOKEN:
345 parse_error ("unexpected end of file while scanning for ';'");
346 return;
348 default:
349 advance ();
350 break;
354 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
355 expressions, until we encounter a comma or semicolon outside any
356 such delimiters; absorb that too. Returns true if the loop ended
357 with a comma. */
359 static bool
360 consume_until_comma_or_eos ()
362 for (;;)
363 switch (token ())
365 case ',':
366 advance ();
367 return true;
369 case ';':
370 advance ();
371 return false;
373 case '{':
374 consume_balanced ('{', '}');
375 return false;
377 case '(':
378 consume_balanced ('(', ')');
379 break;
381 case '[':
382 consume_balanced ('[', ']');
383 break;
385 case '}':
386 case ']':
387 case ')':
388 parse_error ("unmatched '%s' while scanning for ',' or ';'",
389 print_cur_token ());
390 return false;
392 case EOF_TOKEN:
393 parse_error ("unexpected end of file while scanning for ',' or ';'");
394 return false;
396 default:
397 advance ();
398 break;
403 /* GTY(()) option handling. */
404 static type_p type (options_p *optsp, bool nested);
406 /* Optional parenthesized string: ('(' string_seq ')')? */
407 static options_p
408 str_optvalue_opt (options_p prev)
410 const char *name = advance ();
411 const char *value = "";
412 if (token () == '(')
414 advance ();
415 value = string_seq ();
416 require (')');
418 return create_string_option (prev, name, value);
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. */
429 static type_p
430 absdecl (void)
432 type_p ty;
433 options_p opts;
435 ty = type (&opts, true);
436 while (token () == '*')
438 ty = create_pointer (ty);
439 advance ();
442 if (opts)
443 parse_error ("nested GTY(()) options are invalid");
445 return adjust_field_type (ty, 0);
448 /* Type-option: '(' absdecl ')' */
449 static options_p
450 type_optvalue (options_p prev, const char *name)
452 type_p ty;
453 require ('(');
454 ty = absdecl ();
455 require (')');
456 return create_type_option (prev, name, ty);
459 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
460 static options_p
461 nestedptr_optvalue (options_p prev)
463 type_p ty;
464 const char *from, *to;
466 require ('(');
467 ty = absdecl ();
468 require (',');
469 to = string_seq ();
470 require (',');
471 from = string_seq ();
472 require (')');
474 return create_nested_ptr_option (prev, ty, to, from);
477 /* One GTY(()) option:
478 ID str_optvalue_opt
479 | PTR_ALIAS type_optvalue
480 | PARAM_IS type_optvalue
481 | NESTED_PTR nestedptr_optvalue
483 static options_p
484 option (options_p prev)
486 switch (token ())
488 case ID:
489 return str_optvalue_opt (prev);
491 case PTR_ALIAS:
492 advance ();
493 return type_optvalue (prev, "ptr_alias");
495 case PARAM_IS:
496 return type_optvalue (prev, advance ());
498 case NESTED_PTR:
499 advance ();
500 return nestedptr_optvalue (prev);
502 case USER_GTY:
503 advance ();
504 return create_string_option (prev, "user", "");
506 default:
507 parse_error ("expected an option keyword, have %s", print_cur_token ());
508 advance ();
509 return create_string_option (prev, "", "");
513 /* One comma-separated list of options. */
514 static options_p
515 option_seq (void)
517 options_p o;
519 o = option (0);
520 while (token () == ',')
522 advance ();
523 o = option (o);
525 return o;
528 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
529 static options_p
530 gtymarker (void)
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;
543 /* Optional GTY marker. */
544 static options_p
545 gtymarker_opt (void)
547 if (token () != GTY_TOKEN)
548 return 0;
549 return gtymarker ();
554 /* Declarators. The logic here is largely lifted from c-parser.c.
555 Note that we do not have to process abstract declarators, which can
556 appear only in parameter type lists or casts (but see absdecl,
557 above). Also, type qualifiers are thrown out in gengtype-lex.l so
558 we don't have to do it. */
560 /* array_and_function_declarators_opt:
561 \epsilon
562 array_and_function_declarators_opt ARRAY
563 array_and_function_declarators_opt '(' ... ')'
565 where '...' indicates stuff we ignore except insofar as grouping
566 symbols ()[]{} must balance.
568 Subroutine of direct_declarator - do not use elsewhere. */
570 static type_p
571 array_and_function_declarators_opt (type_p ty)
573 if (token () == ARRAY)
575 const char *array = advance ();
576 return create_array (array_and_function_declarators_opt (ty), array);
578 else if (token () == '(')
580 /* We don't need exact types for functions. */
581 consume_balanced ('(', ')');
582 array_and_function_declarators_opt (ty);
583 return create_scalar_type ("function type");
585 else
586 return ty;
589 static type_p inner_declarator (type_p, const char **, options_p *, bool);
591 /* direct_declarator:
592 '(' inner_declarator ')'
593 '(' \epsilon ')' <-- C++ ctors/dtors
594 gtymarker_opt ID array_and_function_declarators_opt
596 Subroutine of declarator, mutually recursive with inner_declarator;
597 do not use elsewhere.
599 IN_STRUCT is true if we are called while parsing structures or classes. */
601 static type_p
602 direct_declarator (type_p ty, const char **namep, options_p *optsp,
603 bool in_struct)
605 /* The first token in a direct-declarator must be an ID, a
606 GTY marker, or an open parenthesis. */
607 switch (token ())
609 case GTY_TOKEN:
610 *optsp = gtymarker ();
611 /* fall through */
613 case ID:
614 *namep = require (ID);
615 /* If the next token is '(', we are parsing a function declaration.
616 Functions are ignored by gengtype, so we return NULL. */
617 if (token () == '(')
618 return NULL;
619 break;
621 case '(':
622 /* If the declarator starts with a '(', we have three options. We
623 are either parsing 'TYPE (*ID)' (i.e., a function pointer)
624 or 'TYPE(...)'.
626 The latter will be a constructor iff we are inside a
627 structure or class. Otherwise, it could be a typedef, but
628 since we explicitly reject typedefs inside structures, we can
629 assume that we found a ctor and return NULL. */
630 advance ();
631 if (in_struct && token () != '*')
633 /* Found a constructor. Find and consume the closing ')'. */
634 while (token () != ')')
635 advance ();
636 advance ();
637 /* Tell the caller to ignore this. */
638 return NULL;
640 ty = inner_declarator (ty, namep, optsp, in_struct);
641 require (')');
642 break;
644 case IGNORABLE_CXX_KEYWORD:
645 /* Any C++ keyword like 'operator' means that we are not looking
646 at a regular data declarator. */
647 return NULL;
649 default:
650 parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
651 print_cur_token ());
652 /* Do _not_ advance if what we have is a close squiggle brace, as
653 we will get much better error recovery that way. */
654 if (token () != '}')
655 advance ();
656 return 0;
658 return array_and_function_declarators_opt (ty);
661 /* The difference between inner_declarator and declarator is in the
662 handling of stars. Consider this declaration:
664 char * (*pfc) (void)
666 It declares a pointer to a function that takes no arguments and
667 returns a char*. To construct the correct type for this
668 declaration, the star outside the parentheses must be processed
669 _before_ the function type, the star inside the parentheses must
670 be processed _after_ the function type. To accomplish this,
671 declarator() creates pointers before recursing (it is actually
672 coded as a while loop), whereas inner_declarator() recurses before
673 creating pointers. */
675 /* inner_declarator:
676 '*' inner_declarator
677 direct_declarator
679 Mutually recursive subroutine of direct_declarator; do not use
680 elsewhere.
682 IN_STRUCT is true if we are called while parsing structures or classes. */
684 static type_p
685 inner_declarator (type_p ty, const char **namep, options_p *optsp,
686 bool in_struct)
688 if (token () == '*')
690 type_p inner;
691 advance ();
692 inner = inner_declarator (ty, namep, optsp, in_struct);
693 if (inner == 0)
694 return 0;
695 else
696 return create_pointer (ty);
698 else
699 return direct_declarator (ty, namep, optsp, in_struct);
702 /* declarator: '*'+ direct_declarator
704 This is the sole public interface to this part of the grammar.
705 Arguments are the type known so far, a pointer to where the name
706 may be stored, and a pointer to where GTY options may be stored.
708 IN_STRUCT is true when we are called to parse declarators inside
709 a structure or class.
711 Returns the final type. */
713 static type_p
714 declarator (type_p ty, const char **namep, options_p *optsp,
715 bool in_struct = false)
717 *namep = 0;
718 *optsp = 0;
719 while (token () == '*')
721 advance ();
722 ty = create_pointer (ty);
724 return direct_declarator (ty, namep, optsp, in_struct);
727 /* Types and declarations. */
729 /* Structure field(s) declaration:
731 type bitfield ';'
732 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
735 Knows that such declarations must end with a close brace (or,
736 erroneously, at EOF).
738 static pair_p
739 struct_field_seq (void)
741 pair_p f = 0;
742 type_p ty, dty;
743 options_p opts, dopts;
744 const char *name;
745 bool another;
747 while (token () != '}' && token () != EOF_TOKEN)
749 ty = type (&opts, true);
751 /* Ignore access-control keywords ("public:" etc). */
752 while (!ty && token () == IGNORABLE_CXX_KEYWORD)
754 const char *keyword = advance ();
755 if (strcmp (keyword, "public:") != 0
756 && strcmp (keyword, "private:") != 0
757 && strcmp (keyword, "protected:") != 0)
758 break;
759 ty = type (&opts, true);
762 if (!ty || token () == ':')
764 consume_until_eos ();
765 continue;
770 dty = declarator (ty, &name, &dopts, true);
772 /* There could be any number of weird things after the declarator,
773 notably bitfield declarations and __attribute__s. If this
774 function returns true, the last thing was a comma, so we have
775 more than one declarator paired with the current type. */
776 another = consume_until_comma_or_eos ();
778 if (!dty)
779 continue;
781 if (opts && dopts)
782 parse_error ("two GTY(()) options for field %s", name);
783 if (opts && !dopts)
784 dopts = opts;
786 f = create_field_at (f, dty, name, dopts, &lexer_line);
788 while (another);
790 return nreverse_pairs (f);
793 /* Return true if OPTS contain the option named STR. */
795 bool
796 opts_have (options_p opts, const char *str)
798 for (options_p opt = opts; opt; opt = opt->next)
799 if (strcmp (opt->name, str) == 0)
800 return true;
801 return false;
805 /* This is called type(), but what it parses (sort of) is what C calls
806 declaration-specifiers and specifier-qualifier-list:
808 SCALAR
809 | ID // typedef
810 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
811 | ENUM ID ( '{' ... '}' )?
813 Returns a partial type; under some conditions (notably
814 "struct foo GTY((...)) thing;") it may write an options
815 structure to *OPTSP.
817 NESTED is true when parsing a declaration already known to have a
818 GTY marker. In these cases, typedef and enum declarations are not
819 allowed because gengtype only understands types at the global
820 scope. */
822 static type_p
823 type (options_p *optsp, bool nested)
825 const char *s;
826 *optsp = 0;
827 switch (token ())
829 case SCALAR:
830 s = advance ();
831 return create_scalar_type (s);
833 case ID:
834 s = typedef_name ();
835 return resolve_typedef (s, &lexer_line);
837 case IGNORABLE_CXX_KEYWORD:
838 /* By returning NULL here, we indicate to the caller that they
839 should ignore everything following this keyword up to the
840 next ';' or '}'. */
841 return NULL;
843 case STRUCT:
844 case UNION:
846 type_p base_class = NULL;
847 options_p opts = 0;
848 /* GTY annotations follow attribute syntax
849 GTY_BEFORE_ID is for union/struct declarations
850 GTY_AFTER_ID is for variable declarations. */
851 enum
853 NO_GTY,
854 GTY_BEFORE_ID,
855 GTY_AFTER_ID
856 } is_gty = NO_GTY;
857 enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
858 advance ();
860 /* Top-level structures that are not explicitly tagged GTY(())
861 are treated as mere forward declarations. This is because
862 there are a lot of structures that we don't need to know
863 about, and some of those have C++ and macro constructs that
864 we cannot handle. */
865 if (nested || token () == GTY_TOKEN)
867 is_gty = GTY_BEFORE_ID;
868 opts = gtymarker_opt ();
871 if (token () == ID)
872 s = advance ();
873 else
874 s = xasprintf ("anonymous:%s:%d",
875 get_input_file_name (lexer_line.file),
876 lexer_line.line);
878 /* Unfortunately above GTY_TOKEN check does not capture the
879 typedef struct_type GTY case. */
880 if (token () == GTY_TOKEN)
882 is_gty = GTY_AFTER_ID;
883 opts = gtymarker_opt ();
886 bool is_user_gty = opts_have (opts, "user");
888 if (token () == ':')
890 if (is_gty && !is_user_gty)
892 /* For GTY-marked types that are not "user", parse some C++
893 inheritance specifications.
894 We require single-inheritance from a non-template type. */
895 advance ();
896 const char *basename = require (ID);
897 /* This may be either an access specifier, or the base name. */
898 if (0 == strcmp (basename, "public")
899 || 0 == strcmp (basename, "protected")
900 || 0 == strcmp (basename, "private"))
901 basename = require (ID);
902 base_class = find_structure (basename, TYPE_STRUCT);
903 if (!base_class)
904 parse_error ("unrecognized base class: %s", basename);
905 require_without_advance ('{');
907 else
909 /* For types lacking GTY-markings, skip over C++ inheritance
910 specification (and thus avoid having to parse e.g. template
911 types). */
912 while (token () != '{')
913 advance ();
917 if (is_gty)
919 if (token () == '{')
921 pair_p fields;
923 if (is_gty == GTY_AFTER_ID)
924 parse_error ("GTY must be specified before identifier");
926 if (!is_user_gty)
928 advance ();
929 fields = struct_field_seq ();
930 require ('}');
932 else
934 /* Do not look inside user defined structures. */
935 fields = NULL;
936 kind = TYPE_USER_STRUCT;
937 consume_balanced ('{', '}');
938 return create_user_defined_type (s, &lexer_line);
941 return new_structure (s, kind, &lexer_line, fields, opts,
942 base_class);
945 else if (token () == '{')
946 consume_balanced ('{', '}');
947 if (opts)
948 *optsp = opts;
949 return find_structure (s, kind);
952 case TYPEDEF:
953 /* In C++, a typedef inside a struct/class/union defines a new
954 type for that inner scope. We cannot support this in
955 gengtype because we have no concept of scoping.
957 We handle typedefs in the global scope separately (see
958 parse_file), so if we find a 'typedef', we must be inside
959 a struct. */
960 gcc_assert (nested);
961 parse_error ("typedefs not supported in structures marked with "
962 "automatic GTY markers. Use GTY((user)) to mark "
963 "this structure.");
964 advance ();
965 return NULL;
967 case ENUM:
968 advance ();
969 if (token () == ID)
970 s = advance ();
971 else
972 s = xasprintf ("anonymous:%s:%d",
973 get_input_file_name (lexer_line.file),
974 lexer_line.line);
976 if (token () == '{')
977 consume_balanced ('{', '}');
979 /* If after parsing the enum we are at the end of the statement,
980 and we are currently inside a structure, then this was an
981 enum declaration inside this scope.
983 We cannot support this for the same reason we cannot support
984 'typedef' inside structures (see the TYPEDEF handler above).
985 If this happens, emit an error and return NULL. */
986 if (nested && token () == ';')
988 parse_error ("enum definitions not supported in structures marked "
989 "with automatic GTY markers. Use GTY((user)) to mark "
990 "this structure.");
991 advance ();
992 return NULL;
995 return create_scalar_type (s);
997 default:
998 parse_error ("expected a type specifier, have %s", print_cur_token ());
999 advance ();
1000 return create_scalar_type ("erroneous type");
1004 /* Top level constructs. */
1006 /* Dispatch declarations beginning with 'typedef'. */
1008 static void
1009 typedef_decl (void)
1011 type_p ty, dty;
1012 const char *name;
1013 options_p opts;
1014 bool another;
1016 gcc_assert (token () == TYPEDEF);
1017 advance ();
1019 ty = type (&opts, false);
1020 if (!ty)
1021 return;
1022 if (opts)
1023 parse_error ("GTY((...)) cannot be applied to a typedef");
1026 dty = declarator (ty, &name, &opts);
1027 if (opts)
1028 parse_error ("GTY((...)) cannot be applied to a typedef");
1030 /* Yet another place where we could have junk (notably attributes)
1031 after the declarator. */
1032 another = consume_until_comma_or_eos ();
1033 if (dty)
1034 do_typedef (name, dty, &lexer_line);
1036 while (another);
1039 /* Structure definition: type() does all the work. */
1041 static void
1042 struct_or_union (void)
1044 options_p dummy;
1045 type (&dummy, false);
1046 /* There may be junk after the type: notably, we cannot currently
1047 distinguish 'struct foo *function(prototype);' from 'struct foo;'
1048 ... we could call declarator(), but it's a waste of time at
1049 present. Instead, just eat whatever token is currently lookahead
1050 and go back to lexical skipping mode. */
1051 advance ();
1054 /* GC root declaration:
1055 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
1056 If the gtymarker is not present, we ignore the rest of the declaration. */
1057 static void
1058 extern_or_static (void)
1060 options_p opts, opts2, dopts;
1061 type_p ty, dty;
1062 const char *name;
1063 require2 (EXTERN, STATIC);
1065 if (token () != GTY_TOKEN)
1067 advance ();
1068 return;
1071 opts = gtymarker ();
1072 ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
1073 dty = declarator (ty, &name, &dopts);
1075 if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
1076 parse_error ("GTY((...)) specified more than once for %s", name);
1077 else if (opts2)
1078 opts = opts2;
1079 else if (dopts)
1080 opts = dopts;
1082 if (dty)
1084 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
1085 require2 (';', '=');
1089 /* Parse the file FNAME for GC-relevant declarations and definitions.
1090 This is the only entry point to this file. */
1091 void
1092 parse_file (const char *fname)
1094 yybegin (fname);
1095 for (;;)
1097 switch (token ())
1099 case EXTERN:
1100 case STATIC:
1101 extern_or_static ();
1102 break;
1104 case STRUCT:
1105 case UNION:
1106 struct_or_union ();
1107 break;
1109 case TYPEDEF:
1110 typedef_decl ();
1111 break;
1113 case EOF_TOKEN:
1114 goto eof;
1116 default:
1117 parse_error ("unexpected top level token, %s", print_cur_token ());
1118 goto eof;
1120 lexer_toplevel_done = 1;
1123 eof:
1124 advance ();
1125 yyend ();