gcc/
[official-gcc.git] / gcc / gengtype-parse.c
blob5663a268ec69a5d459cde705f75f95c60e9185a0
1 /* Process source files and output type information.
2 Copyright (C) 2006-2015 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 HOST_GENERATOR_FILE
21 #include "config.h"
22 #define GENERATOR_FILE 1
23 #else
24 #include "bconfig.h"
25 #endif
26 #include "system.h"
27 #include "gengtype.h"
29 /* This is a simple recursive-descent parser which understands a subset of
30 the C type grammar.
32 Rule functions are suffixed _seq if they scan a sequence of items;
33 _opt if they may consume zero tokens; _seqopt if both are true. The
34 "consume_" prefix indicates that a sequence of tokens is parsed for
35 syntactic correctness and then thrown away. */
37 /* Simple one-token lookahead mechanism. */
39 struct token
41 const char *value;
42 int code;
43 bool valid;
45 static struct token T;
47 /* Retrieve the code of the current token; if there is no current token,
48 get the next one from the lexer. */
49 static inline int
50 token (void)
52 if (!T.valid)
54 T.code = yylex (&T.value);
55 T.valid = true;
57 return T.code;
60 /* Retrieve the value of the current token (if any) and mark it consumed.
61 The next call to token() will get another token from the lexer. */
62 static inline const char *
63 advance (void)
65 T.valid = false;
66 return T.value;
69 /* Diagnostics. */
71 /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
72 static const char *const token_names[] = {
73 "GTY",
74 "typedef",
75 "extern",
76 "static",
77 "union",
78 "struct",
79 "enum",
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 "a C++ keyword to ignore"
93 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
94 static const char *const token_value_format[] = {
95 "%s",
96 "'%s'",
97 "'%s'",
98 "'%s'",
99 "'\"%s\"'",
100 "\"'%s'\"",
101 "'[%s]'",
102 "'%s'",
105 /* Produce a printable representation for a token defined by CODE and
106 VALUE. This sometimes returns pointers into malloc memory and
107 sometimes not, therefore it is unsafe to free the pointer it
108 returns, so that memory is leaked. This does not matter, as this
109 function is only used for diagnostics, and in a successful run of
110 the program there will be none. */
111 static const char *
112 print_token (int code, const char *value)
114 if (code < CHAR_TOKEN_OFFSET)
115 return xasprintf ("'%c'", code);
116 else if (code < FIRST_TOKEN_WITH_VALUE)
117 return xasprintf ("'%s'", token_names[code - CHAR_TOKEN_OFFSET]);
118 else if (!value)
119 return token_names[code - CHAR_TOKEN_OFFSET]; /* don't quote these */
120 else
121 return xasprintf (token_value_format[code - FIRST_TOKEN_WITH_VALUE],
122 value);
125 /* Convenience wrapper around print_token which produces the printable
126 representation of the current token. */
127 static inline const char *
128 print_cur_token (void)
130 return print_token (T.code, T.value);
133 /* Report a parse error on the current line, with diagnostic MSG.
134 Behaves as standard printf with respect to additional arguments and
135 format escapes. */
136 static void ATTRIBUTE_PRINTF_1
137 parse_error (const char *msg, ...)
139 va_list ap;
141 fprintf (stderr, "%s:%d: parse error: ",
142 get_input_file_name (lexer_line.file), lexer_line.line);
144 va_start (ap, msg);
145 vfprintf (stderr, msg, ap);
146 va_end (ap);
148 fputc ('\n', stderr);
150 hit_error = true;
153 /* If the next token does not have code T, report a parse error; otherwise
154 return the token's value. */
155 static const char *
156 require (int t)
158 int u = token ();
159 const char *v = advance ();
160 if (u != t)
162 parse_error ("expected %s, have %s",
163 print_token (t, 0), print_token (u, v));
164 return 0;
166 return v;
169 /* As per require, but do not advance. */
170 static const char *
171 require_without_advance (int t)
173 int u = token ();
174 const char *v = T.value;
175 if (u != t)
177 parse_error ("expected %s, have %s",
178 print_token (t, 0), print_token (u, v));
179 return 0;
181 return v;
184 /* If the next token does not have one of the codes T1 or T2, report a
185 parse error; otherwise return the token's value. */
186 static const char *
187 require2 (int t1, int t2)
189 int u = token ();
190 const char *v = advance ();
191 if (u != t1 && u != t2)
193 parse_error ("expected %s or %s, have %s",
194 print_token (t1, 0), print_token (t2, 0),
195 print_token (u, v));
196 return 0;
198 return v;
201 /* If the next token does not have one of the codes T1, T2, T3 or T4, report a
202 parse error; otherwise return the token's value. */
203 static const char *
204 require4 (int t1, int t2, int t3, int t4)
206 int u = token ();
207 const char *v = advance ();
208 if (u != t1 && u != t2 && u != t3 && u != t4)
210 parse_error ("expected %s, %s, %s or %s, have %s",
211 print_token (t1, 0), print_token (t2, 0),
212 print_token (t3, 0), print_token (t4, 0),
213 print_token (u, v));
214 return 0;
216 return v;
219 /* Near-terminals. */
221 /* C-style string constant concatenation: STRING+
222 Bare STRING should appear nowhere else in this file. */
223 static const char *
224 string_seq (void)
226 const char *s1, *s2;
227 size_t l1, l2;
228 char *buf;
230 s1 = require (STRING);
231 if (s1 == 0)
232 return "";
233 while (token () == STRING)
235 s2 = advance ();
237 l1 = strlen (s1);
238 l2 = strlen (s2);
239 buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
240 memcpy (buf + l1, s2, l2 + 1);
241 XDELETE (CONST_CAST (char *, s2));
242 s1 = buf;
244 return s1;
248 /* The caller has detected a template declaration that starts
249 with TMPL_NAME. Parse up to the closing '>'. This recognizes
250 simple template declarations of the form ID<ID1,ID2,...,IDn>,
251 potentially with a single level of indirection e.g.
252 ID<ID1 *, ID2, ID3 *, ..., IDn>.
253 It does not try to parse anything more sophisticated than that.
255 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
257 static const char *
258 require_template_declaration (const char *tmpl_name)
260 char *str;
261 int num_indirections = 0;
263 /* Recognize the opening '<'. */
264 require ('<');
265 str = concat (tmpl_name, "<", (char *) 0);
267 /* Read the comma-separated list of identifiers. */
268 int depth = 1;
269 while (depth > 0)
271 if (token () == ENUM)
273 advance ();
274 str = concat (str, "enum ", (char *) 0);
275 continue;
277 if (token () == NUM)
279 str = concat (str, advance (), (char *) 0);
280 continue;
282 if (token () == ':')
284 advance ();
285 str = concat (str, ":", (char *) 0);
286 continue;
288 if (token () == '<')
290 advance ();
291 str = concat (str, "<", (char *) 0);
292 depth += 1;
293 continue;
295 if (token () == '>')
297 advance ();
298 str = concat (str, ">", (char *) 0);
299 depth -= 1;
300 continue;
302 const char *id = require4 (SCALAR, ID, '*', ',');
303 if (id == NULL)
305 if (T.code == '*')
307 id = "*";
308 if (num_indirections++)
309 parse_error ("only one level of indirection is supported"
310 " in template arguments");
312 else
313 id = ",";
315 else
316 num_indirections = 0;
317 str = concat (str, id, (char *) 0);
319 return str;
323 /* typedef_name: either an ID, or a template type
324 specification of the form ID<t1,t2,...,tn>. */
326 static const char *
327 typedef_name (void)
329 const char *id = require (ID);
330 if (token () == '<')
331 return require_template_declaration (id);
332 else
333 return id;
336 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
337 static void
338 consume_balanced (int opener, int closer)
340 require (opener);
341 for (;;)
342 switch (token ())
344 default:
345 advance ();
346 break;
347 case '(':
348 consume_balanced ('(', ')');
349 break;
350 case '[':
351 consume_balanced ('[', ']');
352 break;
353 case '{':
354 consume_balanced ('{', '}');
355 break;
357 case '}':
358 case ']':
359 case ')':
360 if (token () != closer)
361 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
362 closer, token ());
363 advance ();
364 return;
366 case EOF_TOKEN:
367 parse_error ("unexpected end of file within %c%c-delimited construct",
368 opener, closer);
369 return;
373 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
374 expressions, until we encounter an end-of-statement marker (a ';' or
375 a '}') outside any such delimiters; absorb that too. */
377 static void
378 consume_until_eos (void)
380 for (;;)
381 switch (token ())
383 case ';':
384 advance ();
385 return;
387 case '{':
388 consume_balanced ('{', '}');
389 return;
391 case '(':
392 consume_balanced ('(', ')');
393 break;
395 case '[':
396 consume_balanced ('[', ']');
397 break;
399 case '}':
400 case ']':
401 case ')':
402 parse_error ("unmatched '%c' while scanning for ';'", token ());
403 return;
405 case EOF_TOKEN:
406 parse_error ("unexpected end of file while scanning for ';'");
407 return;
409 default:
410 advance ();
411 break;
415 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
416 expressions, until we encounter a comma or semicolon outside any
417 such delimiters; absorb that too. Returns true if the loop ended
418 with a comma. */
420 static bool
421 consume_until_comma_or_eos ()
423 for (;;)
424 switch (token ())
426 case ',':
427 advance ();
428 return true;
430 case ';':
431 advance ();
432 return false;
434 case '{':
435 consume_balanced ('{', '}');
436 return false;
438 case '(':
439 consume_balanced ('(', ')');
440 break;
442 case '[':
443 consume_balanced ('[', ']');
444 break;
446 case '}':
447 case ']':
448 case ')':
449 parse_error ("unmatched '%s' while scanning for ',' or ';'",
450 print_cur_token ());
451 return false;
453 case EOF_TOKEN:
454 parse_error ("unexpected end of file while scanning for ',' or ';'");
455 return false;
457 default:
458 advance ();
459 break;
464 /* GTY(()) option handling. */
465 static type_p type (options_p *optsp, bool nested);
467 /* Optional parenthesized string: ('(' string_seq ')')? */
468 static options_p
469 str_optvalue_opt (options_p prev)
471 const char *name = advance ();
472 const char *value = "";
473 if (token () == '(')
475 advance ();
476 value = string_seq ();
477 require (')');
479 return create_string_option (prev, name, value);
482 /* absdecl: type '*'*
483 -- a vague approximation to what the C standard calls an abstract
484 declarator. The only kinds that are actually used are those that
485 are just a bare type and those that have trailing pointer-stars.
486 Further kinds should be implemented if and when they become
487 necessary. Used only within GTY(()) option values, therefore
488 further GTY(()) tags within the type are invalid. Note that the
489 return value has already been run through adjust_field_type. */
490 static type_p
491 absdecl (void)
493 type_p ty;
494 options_p opts;
496 ty = type (&opts, true);
497 while (token () == '*')
499 ty = create_pointer (ty);
500 advance ();
503 if (opts)
504 parse_error ("nested GTY(()) options are invalid");
506 return adjust_field_type (ty, 0);
509 /* Type-option: '(' absdecl ')' */
510 static options_p
511 type_optvalue (options_p prev, const char *name)
513 type_p ty;
514 require ('(');
515 ty = absdecl ();
516 require (')');
517 return create_type_option (prev, name, ty);
520 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
521 static options_p
522 nestedptr_optvalue (options_p prev)
524 type_p ty;
525 const char *from, *to;
527 require ('(');
528 ty = absdecl ();
529 require (',');
530 to = string_seq ();
531 require (',');
532 from = string_seq ();
533 require (')');
535 return create_nested_ptr_option (prev, ty, to, from);
538 /* One GTY(()) option:
539 ID str_optvalue_opt
540 | PTR_ALIAS type_optvalue
541 | NESTED_PTR nestedptr_optvalue
543 static options_p
544 option (options_p prev)
546 switch (token ())
548 case ID:
549 return str_optvalue_opt (prev);
551 case PTR_ALIAS:
552 advance ();
553 return type_optvalue (prev, "ptr_alias");
555 case NESTED_PTR:
556 advance ();
557 return nestedptr_optvalue (prev);
559 case USER_GTY:
560 advance ();
561 return create_string_option (prev, "user", "");
563 default:
564 parse_error ("expected an option keyword, have %s", print_cur_token ());
565 advance ();
566 return create_string_option (prev, "", "");
570 /* One comma-separated list of options. */
571 static options_p
572 option_seq (void)
574 options_p o;
576 o = option (0);
577 while (token () == ',')
579 advance ();
580 o = option (o);
582 return o;
585 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
586 static options_p
587 gtymarker (void)
589 options_p result = 0;
590 require (GTY_TOKEN);
591 require ('(');
592 require ('(');
593 if (token () != ')')
594 result = option_seq ();
595 require (')');
596 require (')');
597 return result;
600 /* Optional GTY marker. */
601 static options_p
602 gtymarker_opt (void)
604 if (token () != GTY_TOKEN)
605 return 0;
606 return gtymarker ();
611 /* Declarators. The logic here is largely lifted from c-parser.c.
612 Note that we do not have to process abstract declarators, which can
613 appear only in parameter type lists or casts (but see absdecl,
614 above). Also, type qualifiers are thrown out in gengtype-lex.l so
615 we don't have to do it. */
617 /* array_and_function_declarators_opt:
618 \epsilon
619 array_and_function_declarators_opt ARRAY
620 array_and_function_declarators_opt '(' ... ')'
622 where '...' indicates stuff we ignore except insofar as grouping
623 symbols ()[]{} must balance.
625 Subroutine of direct_declarator - do not use elsewhere. */
627 static type_p
628 array_and_function_declarators_opt (type_p ty)
630 if (token () == ARRAY)
632 const char *array = advance ();
633 return create_array (array_and_function_declarators_opt (ty), array);
635 else if (token () == '(')
637 /* We don't need exact types for functions. */
638 consume_balanced ('(', ')');
639 array_and_function_declarators_opt (ty);
640 return create_scalar_type ("function type");
642 else
643 return ty;
646 static type_p inner_declarator (type_p, const char **, options_p *, bool);
648 /* direct_declarator:
649 '(' inner_declarator ')'
650 '(' \epsilon ')' <-- C++ ctors/dtors
651 gtymarker_opt ID array_and_function_declarators_opt
653 Subroutine of declarator, mutually recursive with inner_declarator;
654 do not use elsewhere.
656 IN_STRUCT is true if we are called while parsing structures or classes. */
658 static type_p
659 direct_declarator (type_p ty, const char **namep, options_p *optsp,
660 bool in_struct)
662 /* The first token in a direct-declarator must be an ID, a
663 GTY marker, or an open parenthesis. */
664 switch (token ())
666 case GTY_TOKEN:
667 *optsp = gtymarker ();
668 /* fall through */
670 case ID:
671 *namep = require (ID);
672 /* If the next token is '(', we are parsing a function declaration.
673 Functions are ignored by gengtype, so we return NULL. */
674 if (token () == '(')
675 return NULL;
676 break;
678 case '(':
679 /* If the declarator starts with a '(', we have three options. We
680 are either parsing 'TYPE (*ID)' (i.e., a function pointer)
681 or 'TYPE(...)'.
683 The latter will be a constructor iff we are inside a
684 structure or class. Otherwise, it could be a typedef, but
685 since we explicitly reject typedefs inside structures, we can
686 assume that we found a ctor and return NULL. */
687 advance ();
688 if (in_struct && token () != '*')
690 /* Found a constructor. Find and consume the closing ')'. */
691 while (token () != ')')
692 advance ();
693 advance ();
694 /* Tell the caller to ignore this. */
695 return NULL;
697 ty = inner_declarator (ty, namep, optsp, in_struct);
698 require (')');
699 break;
701 case IGNORABLE_CXX_KEYWORD:
702 /* Any C++ keyword like 'operator' means that we are not looking
703 at a regular data declarator. */
704 return NULL;
706 default:
707 parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
708 print_cur_token ());
709 /* Do _not_ advance if what we have is a close squiggle brace, as
710 we will get much better error recovery that way. */
711 if (token () != '}')
712 advance ();
713 return 0;
715 return array_and_function_declarators_opt (ty);
718 /* The difference between inner_declarator and declarator is in the
719 handling of stars. Consider this declaration:
721 char * (*pfc) (void)
723 It declares a pointer to a function that takes no arguments and
724 returns a char*. To construct the correct type for this
725 declaration, the star outside the parentheses must be processed
726 _before_ the function type, the star inside the parentheses must
727 be processed _after_ the function type. To accomplish this,
728 declarator() creates pointers before recursing (it is actually
729 coded as a while loop), whereas inner_declarator() recurses before
730 creating pointers. */
732 /* inner_declarator:
733 '*' inner_declarator
734 direct_declarator
736 Mutually recursive subroutine of direct_declarator; do not use
737 elsewhere.
739 IN_STRUCT is true if we are called while parsing structures or classes. */
741 static type_p
742 inner_declarator (type_p ty, const char **namep, options_p *optsp,
743 bool in_struct)
745 if (token () == '*')
747 type_p inner;
748 advance ();
749 inner = inner_declarator (ty, namep, optsp, in_struct);
750 if (inner == 0)
751 return 0;
752 else
753 return create_pointer (ty);
755 else
756 return direct_declarator (ty, namep, optsp, in_struct);
759 /* declarator: '*'+ direct_declarator
761 This is the sole public interface to this part of the grammar.
762 Arguments are the type known so far, a pointer to where the name
763 may be stored, and a pointer to where GTY options may be stored.
765 IN_STRUCT is true when we are called to parse declarators inside
766 a structure or class.
768 Returns the final type. */
770 static type_p
771 declarator (type_p ty, const char **namep, options_p *optsp,
772 bool in_struct = false)
774 *namep = 0;
775 *optsp = 0;
776 while (token () == '*')
778 advance ();
779 ty = create_pointer (ty);
781 return direct_declarator (ty, namep, optsp, in_struct);
784 /* Types and declarations. */
786 /* Structure field(s) declaration:
788 type bitfield ';'
789 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
792 Knows that such declarations must end with a close brace (or,
793 erroneously, at EOF).
795 static pair_p
796 struct_field_seq (void)
798 pair_p f = 0;
799 type_p ty, dty;
800 options_p opts, dopts;
801 const char *name;
802 bool another;
804 while (token () != '}' && token () != EOF_TOKEN)
806 ty = type (&opts, true);
808 /* Ignore access-control keywords ("public:" etc). */
809 while (!ty && token () == IGNORABLE_CXX_KEYWORD)
811 const char *keyword = advance ();
812 if (strcmp (keyword, "public:") != 0
813 && strcmp (keyword, "private:") != 0
814 && strcmp (keyword, "protected:") != 0)
815 break;
816 ty = type (&opts, true);
819 if (!ty || token () == ':')
821 consume_until_eos ();
822 continue;
827 dty = declarator (ty, &name, &dopts, true);
829 /* There could be any number of weird things after the declarator,
830 notably bitfield declarations and __attribute__s. If this
831 function returns true, the last thing was a comma, so we have
832 more than one declarator paired with the current type. */
833 another = consume_until_comma_or_eos ();
835 if (!dty)
836 continue;
838 if (opts && dopts)
839 parse_error ("two GTY(()) options for field %s", name);
840 if (opts && !dopts)
841 dopts = opts;
843 f = create_field_at (f, dty, name, dopts, &lexer_line);
845 while (another);
847 return nreverse_pairs (f);
850 /* Return true if OPTS contain the option named STR. */
852 bool
853 opts_have (options_p opts, const char *str)
855 for (options_p opt = opts; opt; opt = opt->next)
856 if (strcmp (opt->name, str) == 0)
857 return true;
858 return false;
862 /* This is called type(), but what it parses (sort of) is what C calls
863 declaration-specifiers and specifier-qualifier-list:
865 SCALAR
866 | ID // typedef
867 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
868 | ENUM ID ( '{' ... '}' )?
870 Returns a partial type; under some conditions (notably
871 "struct foo GTY((...)) thing;") it may write an options
872 structure to *OPTSP.
874 NESTED is true when parsing a declaration already known to have a
875 GTY marker. In these cases, typedef and enum declarations are not
876 allowed because gengtype only understands types at the global
877 scope. */
879 static type_p
880 type (options_p *optsp, bool nested)
882 const char *s;
883 *optsp = 0;
884 switch (token ())
886 case SCALAR:
887 s = advance ();
888 return create_scalar_type (s);
890 case ID:
891 s = typedef_name ();
892 return resolve_typedef (s, &lexer_line);
894 case IGNORABLE_CXX_KEYWORD:
895 /* By returning NULL here, we indicate to the caller that they
896 should ignore everything following this keyword up to the
897 next ';' or '}'. */
898 return NULL;
900 case STRUCT:
901 case UNION:
903 type_p base_class = NULL;
904 options_p opts = 0;
905 /* GTY annotations follow attribute syntax
906 GTY_BEFORE_ID is for union/struct declarations
907 GTY_AFTER_ID is for variable declarations. */
908 enum
910 NO_GTY,
911 GTY_BEFORE_ID,
912 GTY_AFTER_ID
913 } is_gty = NO_GTY;
914 enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
915 advance ();
917 /* Top-level structures that are not explicitly tagged GTY(())
918 are treated as mere forward declarations. This is because
919 there are a lot of structures that we don't need to know
920 about, and some of those have C++ and macro constructs that
921 we cannot handle. */
922 if (nested || token () == GTY_TOKEN)
924 is_gty = GTY_BEFORE_ID;
925 opts = gtymarker_opt ();
928 if (token () == ID)
929 s = advance ();
930 else
931 s = xasprintf ("anonymous:%s:%d",
932 get_input_file_name (lexer_line.file),
933 lexer_line.line);
935 /* Unfortunately above GTY_TOKEN check does not capture the
936 typedef struct_type GTY case. */
937 if (token () == GTY_TOKEN)
939 is_gty = GTY_AFTER_ID;
940 opts = gtymarker_opt ();
943 bool is_user_gty = opts_have (opts, "user");
945 if (token () == ':')
947 if (is_gty && !is_user_gty)
949 /* For GTY-marked types that are not "user", parse some C++
950 inheritance specifications.
951 We require single-inheritance from a non-template type. */
952 advance ();
953 const char *basename = require (ID);
954 /* This may be either an access specifier, or the base name. */
955 if (0 == strcmp (basename, "public")
956 || 0 == strcmp (basename, "protected")
957 || 0 == strcmp (basename, "private"))
958 basename = require (ID);
959 base_class = find_structure (basename, TYPE_STRUCT);
960 if (!base_class)
961 parse_error ("unrecognized base class: %s", basename);
962 require_without_advance ('{');
964 else
966 /* For types lacking GTY-markings, skip over C++ inheritance
967 specification (and thus avoid having to parse e.g. template
968 types). */
969 while (token () != '{')
970 advance ();
974 if (is_gty)
976 if (token () == '{')
978 pair_p fields;
980 if (is_gty == GTY_AFTER_ID)
981 parse_error ("GTY must be specified before identifier");
983 if (!is_user_gty)
985 advance ();
986 fields = struct_field_seq ();
987 require ('}');
989 else
991 /* Do not look inside user defined structures. */
992 fields = NULL;
993 kind = TYPE_USER_STRUCT;
994 consume_balanced ('{', '}');
995 return create_user_defined_type (s, &lexer_line);
998 return new_structure (s, kind, &lexer_line, fields, opts,
999 base_class);
1002 else if (token () == '{')
1003 consume_balanced ('{', '}');
1004 if (opts)
1005 *optsp = opts;
1006 return find_structure (s, kind);
1009 case TYPEDEF:
1010 /* In C++, a typedef inside a struct/class/union defines a new
1011 type for that inner scope. We cannot support this in
1012 gengtype because we have no concept of scoping.
1014 We handle typedefs in the global scope separately (see
1015 parse_file), so if we find a 'typedef', we must be inside
1016 a struct. */
1017 gcc_assert (nested);
1018 parse_error ("typedefs not supported in structures marked with "
1019 "automatic GTY markers. Use GTY((user)) to mark "
1020 "this structure.");
1021 advance ();
1022 return NULL;
1024 case ENUM:
1025 advance ();
1026 if (token () == ID)
1027 s = advance ();
1028 else
1029 s = xasprintf ("anonymous:%s:%d",
1030 get_input_file_name (lexer_line.file),
1031 lexer_line.line);
1033 if (token () == '{')
1034 consume_balanced ('{', '}');
1036 /* If after parsing the enum we are at the end of the statement,
1037 and we are currently inside a structure, then this was an
1038 enum declaration inside this scope.
1040 We cannot support this for the same reason we cannot support
1041 'typedef' inside structures (see the TYPEDEF handler above).
1042 If this happens, emit an error and return NULL. */
1043 if (nested && token () == ';')
1045 parse_error ("enum definitions not supported in structures marked "
1046 "with automatic GTY markers. Use GTY((user)) to mark "
1047 "this structure.");
1048 advance ();
1049 return NULL;
1052 return create_scalar_type (s);
1054 default:
1055 parse_error ("expected a type specifier, have %s", print_cur_token ());
1056 advance ();
1057 return create_scalar_type ("erroneous type");
1061 /* Top level constructs. */
1063 /* Dispatch declarations beginning with 'typedef'. */
1065 static void
1066 typedef_decl (void)
1068 type_p ty, dty;
1069 const char *name;
1070 options_p opts;
1071 bool another;
1073 gcc_assert (token () == TYPEDEF);
1074 advance ();
1076 ty = type (&opts, false);
1077 if (!ty)
1078 return;
1079 if (opts)
1080 parse_error ("GTY((...)) cannot be applied to a typedef");
1083 dty = declarator (ty, &name, &opts);
1084 if (opts)
1085 parse_error ("GTY((...)) cannot be applied to a typedef");
1087 /* Yet another place where we could have junk (notably attributes)
1088 after the declarator. */
1089 another = consume_until_comma_or_eos ();
1090 if (dty)
1091 do_typedef (name, dty, &lexer_line);
1093 while (another);
1096 /* Structure definition: type() does all the work. */
1098 static void
1099 struct_or_union (void)
1101 options_p dummy;
1102 type (&dummy, false);
1103 /* There may be junk after the type: notably, we cannot currently
1104 distinguish 'struct foo *function(prototype);' from 'struct foo;'
1105 ... we could call declarator(), but it's a waste of time at
1106 present. Instead, just eat whatever token is currently lookahead
1107 and go back to lexical skipping mode. */
1108 advance ();
1111 /* GC root declaration:
1112 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
1113 If the gtymarker is not present, we ignore the rest of the declaration. */
1114 static void
1115 extern_or_static (void)
1117 options_p opts, opts2, dopts;
1118 type_p ty, dty;
1119 const char *name;
1120 require2 (EXTERN, STATIC);
1122 if (token () != GTY_TOKEN)
1124 advance ();
1125 return;
1128 opts = gtymarker ();
1129 ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
1130 dty = declarator (ty, &name, &dopts);
1132 if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
1133 parse_error ("GTY((...)) specified more than once for %s", name);
1134 else if (opts2)
1135 opts = opts2;
1136 else if (dopts)
1137 opts = dopts;
1139 if (dty)
1141 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
1142 require2 (';', '=');
1146 /* Parse the file FNAME for GC-relevant declarations and definitions.
1147 This is the only entry point to this file. */
1148 void
1149 parse_file (const char *fname)
1151 yybegin (fname);
1152 for (;;)
1154 switch (token ())
1156 case EXTERN:
1157 case STATIC:
1158 extern_or_static ();
1159 break;
1161 case STRUCT:
1162 case UNION:
1163 struct_or_union ();
1164 break;
1166 case TYPEDEF:
1167 typedef_decl ();
1168 break;
1170 case EOF_TOKEN:
1171 goto eof;
1173 default:
1174 parse_error ("unexpected top level token, %s", print_cur_token ());
1175 goto eof;
1177 lexer_toplevel_done = 1;
1180 eof:
1181 advance ();
1182 yyend ();