Add more C++ support in gengtype.
[gcc.git] / gcc / gengtype-parse.c
1 /* Process source files and output type information.
2 Copyright (C) 2006, 2007, 2010, 2012 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #ifdef GENERATOR_FILE
21 #include "bconfig.h"
22 #else
23 #include "config.h"
24 #endif
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
38 struct token
39 {
40 const char *value;
41 int code;
42 bool valid;
43 };
44 static 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. */
48 static inline int
49 token (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. */
61 static inline const char *
62 advance (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. */
71 static const char *const token_names[] = {
72 "GTY",
73 "typedef",
74 "extern",
75 "static",
76 "union",
77 "struct",
78 "enum",
79 "VEC",
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"
91 };
92
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'",
103 };
104
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)
113 {
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);
123 }
124
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)
129 {
130 return print_token (T.code, T.value);
131 }
132
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, ...)
138 {
139 va_list ap;
140
141 fprintf (stderr, "%s:%d: parse error: ",
142 get_input_file_name (lexer_line.file), lexer_line.line);
143
144 va_start (ap, msg);
145 vfprintf (stderr, msg, ap);
146 va_end (ap);
147
148 fputc ('\n', stderr);
149
150 hit_error = true;
151 }
152
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)
157 {
158 int u = token ();
159 const char *v = advance ();
160 if (u != t)
161 {
162 parse_error ("expected %s, have %s",
163 print_token (t, 0), print_token (u, v));
164 return 0;
165 }
166 return v;
167 }
168
169 /* If the next token does not have one of the codes T1 or T2, report a
170 parse error; otherwise return the token's value. */
171 static const char *
172 require2 (int t1, int t2)
173 {
174 int u = token ();
175 const char *v = advance ();
176 if (u != t1 && u != t2)
177 {
178 parse_error ("expected %s or %s, have %s",
179 print_token (t1, 0), print_token (t2, 0),
180 print_token (u, v));
181 return 0;
182 }
183 return v;
184 }
185
186 /* Near-terminals. */
187
188 /* C-style string constant concatenation: STRING+
189 Bare STRING should appear nowhere else in this file. */
190 static const char *
191 string_seq (void)
192 {
193 const char *s1, *s2;
194 size_t l1, l2;
195 char *buf;
196
197 s1 = require (STRING);
198 if (s1 == 0)
199 return "";
200 while (token () == STRING)
201 {
202 s2 = advance ();
203
204 l1 = strlen (s1);
205 l2 = strlen (s2);
206 buf = XRESIZEVEC (char, CONST_CAST (char *, s1), l1 + l2 + 1);
207 memcpy (buf + l1, s2, l2 + 1);
208 XDELETE (CONST_CAST (char *, s2));
209 s1 = buf;
210 }
211 return s1;
212 }
213
214
215 /* The caller has detected a template declaration that starts
216 with TMPL_NAME. Parse up to the closing '>'. This recognizes
217 simple template declarations of the form ID<ID1,ID2,...,IDn>.
218 It does not try to parse anything more sophisticated than that.
219
220 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
221
222 static const char *
223 require_template_declaration (const char *tmpl_name)
224 {
225 char *str;
226
227 /* Recognize the opening '<'. */
228 require ('<');
229 str = concat (tmpl_name, "<", (char *) 0);
230
231 /* Read the comma-separated list of identifiers. */
232 while (token () != '>')
233 {
234 const char *id = require2 (ID, ',');
235 if (id == NULL)
236 id = ",";
237 str = concat (str, id, (char *) 0);
238 }
239
240 /* Recognize the closing '>'. */
241 require ('>');
242 str = concat (str, ">", (char *) 0);
243
244 return str;
245 }
246
247
248 /* typedef_name: either an ID, or VEC(x,y), or a template type
249 specification of the form ID<t1,t2,...,tn>.
250
251 FIXME cxx-conversion. VEC(x,y) is currently translated to the
252 template 'vec_t<x>'. This is to support the transition to C++ and
253 avoid re-writing all the 'VEC(x,y)' declarations in the code. This
254 needs to be fixed when the branch is merged into trunk. */
255
256 static const char *
257 typedef_name (void)
258 {
259 if (token () == VEC_TOKEN)
260 {
261 const char *c1, *r;
262 advance ();
263 require ('(');
264 c1 = require2 (ID, SCALAR);
265 require (',');
266 require (ID);
267 require (')');
268 r = concat ("vec_t<", c1, ">", (char *) 0);
269 free (CONST_CAST (char *, c1));
270 return r;
271 }
272
273 const char *id = require (ID);
274 if (token () == '<')
275 return require_template_declaration (id);
276 else
277 return id;
278 }
279
280 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
281 static void
282 consume_balanced (int opener, int closer)
283 {
284 require (opener);
285 for (;;)
286 switch (token ())
287 {
288 default:
289 advance ();
290 break;
291 case '(':
292 consume_balanced ('(', ')');
293 break;
294 case '[':
295 consume_balanced ('[', ']');
296 break;
297 case '{':
298 consume_balanced ('{', '}');
299 break;
300
301 case '}':
302 case ']':
303 case ')':
304 if (token () != closer)
305 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
306 closer, token ());
307 advance ();
308 return;
309
310 case EOF_TOKEN:
311 parse_error ("unexpected end of file within %c%c-delimited construct",
312 opener, closer);
313 return;
314 }
315 }
316
317 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
318 expressions, until we encounter an end-of-statement marker (a ';' or
319 a '}') outside any such delimiters; absorb that too. */
320
321 static void
322 consume_until_eos (void)
323 {
324 for (;;)
325 switch (token ())
326 {
327 case ';':
328 advance ();
329 return;
330
331 case '{':
332 consume_balanced ('{', '}');
333 return;
334
335 case '(':
336 consume_balanced ('(', ')');
337 break;
338
339 case '[':
340 consume_balanced ('[', ']');
341 break;
342
343 case '}':
344 case ']':
345 case ')':
346 parse_error ("unmatched '%c' while scanning for ';'", token ());
347 return;
348
349 case EOF_TOKEN:
350 parse_error ("unexpected end of file while scanning for ';'");
351 return;
352
353 default:
354 advance ();
355 break;
356 }
357 }
358
359 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
360 expressions, until we encounter a comma or semicolon outside any
361 such delimiters; absorb that too. Returns true if the loop ended
362 with a comma. */
363
364 static bool
365 consume_until_comma_or_eos ()
366 {
367 for (;;)
368 switch (token ())
369 {
370 case ',':
371 advance ();
372 return true;
373
374 case ';':
375 advance ();
376 return false;
377
378 case '{':
379 consume_balanced ('{', '}');
380 return false;
381
382 case '(':
383 consume_balanced ('(', ')');
384 break;
385
386 case '[':
387 consume_balanced ('[', ']');
388 break;
389
390 case '}':
391 case ']':
392 case ')':
393 parse_error ("unmatched '%s' while scanning for ',' or ';'",
394 print_cur_token ());
395 return false;
396
397 case EOF_TOKEN:
398 parse_error ("unexpected end of file while scanning for ',' or ';'");
399 return false;
400
401 default:
402 advance ();
403 break;
404 }
405 }
406 \f
407
408 /* GTY(()) option handling. */
409 static type_p type (options_p *optsp, bool nested);
410
411 /* Optional parenthesized string: ('(' string_seq ')')? */
412 static options_p
413 str_optvalue_opt (options_p prev)
414 {
415 const char *name = advance ();
416 const char *value = "";
417 if (token () == '(')
418 {
419 advance ();
420 value = string_seq ();
421 require (')');
422 }
423 return create_string_option (prev, name, value);
424 }
425
426 /* absdecl: type '*'*
427 -- a vague approximation to what the C standard calls an abstract
428 declarator. The only kinds that are actually used are those that
429 are just a bare type and those that have trailing pointer-stars.
430 Further kinds should be implemented if and when they become
431 necessary. Used only within GTY(()) option values, therefore
432 further GTY(()) tags within the type are invalid. Note that the
433 return value has already been run through adjust_field_type. */
434 static type_p
435 absdecl (void)
436 {
437 type_p ty;
438 options_p opts;
439
440 ty = type (&opts, true);
441 while (token () == '*')
442 {
443 ty = create_pointer (ty);
444 advance ();
445 }
446
447 if (opts)
448 parse_error ("nested GTY(()) options are invalid");
449
450 return adjust_field_type (ty, 0);
451 }
452
453 /* Type-option: '(' absdecl ')' */
454 static options_p
455 type_optvalue (options_p prev, const char *name)
456 {
457 type_p ty;
458 require ('(');
459 ty = absdecl ();
460 require (')');
461 return create_type_option (prev, name, ty);
462 }
463
464 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
465 static options_p
466 nestedptr_optvalue (options_p prev)
467 {
468 type_p ty;
469 const char *from, *to;
470
471 require ('(');
472 ty = absdecl ();
473 require (',');
474 to = string_seq ();
475 require (',');
476 from = string_seq ();
477 require (')');
478
479 return create_nested_ptr_option (prev, ty, to, from);
480 }
481
482 /* One GTY(()) option:
483 ID str_optvalue_opt
484 | PTR_ALIAS type_optvalue
485 | PARAM_IS type_optvalue
486 | NESTED_PTR nestedptr_optvalue
487 */
488 static options_p
489 option (options_p prev)
490 {
491 switch (token ())
492 {
493 case ID:
494 return str_optvalue_opt (prev);
495
496 case PTR_ALIAS:
497 advance ();
498 return type_optvalue (prev, "ptr_alias");
499
500 case PARAM_IS:
501 return type_optvalue (prev, advance ());
502
503 case NESTED_PTR:
504 advance ();
505 return nestedptr_optvalue (prev);
506
507 case USER_GTY:
508 advance ();
509 return create_string_option (prev, "user", "");
510
511 default:
512 parse_error ("expected an option keyword, have %s", print_cur_token ());
513 advance ();
514 return create_string_option (prev, "", "");
515 }
516 }
517
518 /* One comma-separated list of options. */
519 static options_p
520 option_seq (void)
521 {
522 options_p o;
523
524 o = option (0);
525 while (token () == ',')
526 {
527 advance ();
528 o = option (o);
529 }
530 return o;
531 }
532
533 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
534 static options_p
535 gtymarker (void)
536 {
537 options_p result = 0;
538 require (GTY_TOKEN);
539 require ('(');
540 require ('(');
541 if (token () != ')')
542 result = option_seq ();
543 require (')');
544 require (')');
545 return result;
546 }
547
548 /* Optional GTY marker. */
549 static options_p
550 gtymarker_opt (void)
551 {
552 if (token () != GTY_TOKEN)
553 return 0;
554 return gtymarker ();
555 }
556
557
558 \f
559 /* Declarators. The logic here is largely lifted from c-parser.c.
560 Note that we do not have to process abstract declarators, which can
561 appear only in parameter type lists or casts (but see absdecl,
562 above). Also, type qualifiers are thrown out in gengtype-lex.l so
563 we don't have to do it. */
564
565 /* array_and_function_declarators_opt:
566 \epsilon
567 array_and_function_declarators_opt ARRAY
568 array_and_function_declarators_opt '(' ... ')'
569
570 where '...' indicates stuff we ignore except insofar as grouping
571 symbols ()[]{} must balance.
572
573 Subroutine of direct_declarator - do not use elsewhere. */
574
575 static type_p
576 array_and_function_declarators_opt (type_p ty)
577 {
578 if (token () == ARRAY)
579 {
580 const char *array = advance ();
581 return create_array (array_and_function_declarators_opt (ty), array);
582 }
583 else if (token () == '(')
584 {
585 /* We don't need exact types for functions. */
586 consume_balanced ('(', ')');
587 array_and_function_declarators_opt (ty);
588 return create_scalar_type ("function type");
589 }
590 else
591 return ty;
592 }
593
594 static type_p inner_declarator (type_p, const char **, options_p *, bool);
595
596 /* direct_declarator:
597 '(' inner_declarator ')'
598 '(' \epsilon ')' <-- C++ ctors/dtors
599 gtymarker_opt ID array_and_function_declarators_opt
600
601 Subroutine of declarator, mutually recursive with inner_declarator;
602 do not use elsewhere.
603
604 IN_STRUCT is true if we are called while parsing structures or classes. */
605
606 static type_p
607 direct_declarator (type_p ty, const char **namep, options_p *optsp,
608 bool in_struct)
609 {
610 /* The first token in a direct-declarator must be an ID, a
611 GTY marker, or an open parenthesis. */
612 switch (token ())
613 {
614 case GTY_TOKEN:
615 *optsp = gtymarker ();
616 /* fall through */
617
618 case ID:
619 *namep = require (ID);
620 /* If the next token is '(', we are parsing a function declaration.
621 Functions are ignored by gengtype, so we return NULL. */
622 if (token () == '(')
623 return NULL;
624 break;
625
626 case '(':
627 /* If the declarator starts with a '(', we have three options. We
628 are either parsing 'TYPE (*ID)' (i.e., a function pointer)
629 or 'TYPE(...)'.
630
631 The latter will be a constructor iff we are inside a
632 structure or class. Otherwise, it could be a typedef, but
633 since we explicitly reject typedefs inside structures, we can
634 assume that we found a ctor and return NULL. */
635 advance ();
636 if (in_struct && token () != '*')
637 {
638 /* Found a constructor. Find and consume the closing ')'. */
639 while (token () != ')')
640 advance ();
641 advance ();
642 /* Tell the caller to ignore this. */
643 return NULL;
644 }
645 ty = inner_declarator (ty, namep, optsp, in_struct);
646 require (')');
647 break;
648
649 case IGNORABLE_CXX_KEYWORD:
650 /* Any C++ keyword like 'operator' means that we are not looking
651 at a regular data declarator. */
652 return NULL;
653
654 default:
655 parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
656 print_cur_token ());
657 /* Do _not_ advance if what we have is a close squiggle brace, as
658 we will get much better error recovery that way. */
659 if (token () != '}')
660 advance ();
661 return 0;
662 }
663 return array_and_function_declarators_opt (ty);
664 }
665
666 /* The difference between inner_declarator and declarator is in the
667 handling of stars. Consider this declaration:
668
669 char * (*pfc) (void)
670
671 It declares a pointer to a function that takes no arguments and
672 returns a char*. To construct the correct type for this
673 declaration, the star outside the parentheses must be processed
674 _before_ the function type, the star inside the parentheses must
675 be processed _after_ the function type. To accomplish this,
676 declarator() creates pointers before recursing (it is actually
677 coded as a while loop), whereas inner_declarator() recurses before
678 creating pointers. */
679
680 /* inner_declarator:
681 '*' inner_declarator
682 direct_declarator
683
684 Mutually recursive subroutine of direct_declarator; do not use
685 elsewhere.
686
687 IN_STRUCT is true if we are called while parsing structures or classes. */
688
689 static type_p
690 inner_declarator (type_p ty, const char **namep, options_p *optsp,
691 bool in_struct)
692 {
693 if (token () == '*')
694 {
695 type_p inner;
696 advance ();
697 inner = inner_declarator (ty, namep, optsp, in_struct);
698 if (inner == 0)
699 return 0;
700 else
701 return create_pointer (ty);
702 }
703 else
704 return direct_declarator (ty, namep, optsp, in_struct);
705 }
706
707 /* declarator: '*'+ direct_declarator
708
709 This is the sole public interface to this part of the grammar.
710 Arguments are the type known so far, a pointer to where the name
711 may be stored, and a pointer to where GTY options may be stored.
712
713 IN_STRUCT is true when we are called to parse declarators inside
714 a structure or class.
715
716 Returns the final type. */
717
718 static type_p
719 declarator (type_p ty, const char **namep, options_p *optsp,
720 bool in_struct = false)
721 {
722 *namep = 0;
723 *optsp = 0;
724 while (token () == '*')
725 {
726 advance ();
727 ty = create_pointer (ty);
728 }
729 return direct_declarator (ty, namep, optsp, in_struct);
730 }
731 \f
732 /* Types and declarations. */
733
734 /* Structure field(s) declaration:
735 (
736 type bitfield ';'
737 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
738 )+
739
740 Knows that such declarations must end with a close brace (or,
741 erroneously, at EOF).
742 */
743 static pair_p
744 struct_field_seq (void)
745 {
746 pair_p f = 0;
747 type_p ty, dty;
748 options_p opts, dopts;
749 const char *name;
750 bool another;
751
752 do
753 {
754 ty = type (&opts, true);
755
756 if (!ty || token () == ':')
757 {
758 consume_until_eos ();
759 continue;
760 }
761
762 do
763 {
764 dty = declarator (ty, &name, &dopts, true);
765
766 /* There could be any number of weird things after the declarator,
767 notably bitfield declarations and __attribute__s. If this
768 function returns true, the last thing was a comma, so we have
769 more than one declarator paired with the current type. */
770 another = consume_until_comma_or_eos ();
771
772 if (!dty)
773 continue;
774
775 if (opts && dopts)
776 parse_error ("two GTY(()) options for field %s", name);
777 if (opts && !dopts)
778 dopts = opts;
779
780 f = create_field_at (f, dty, name, dopts, &lexer_line);
781 }
782 while (another);
783 }
784 while (token () != '}' && token () != EOF_TOKEN);
785 return nreverse_pairs (f);
786 }
787
788 /* Return true if OPTS contain the option named STR. */
789
790 static bool
791 opts_have (options_p opts, const char *str)
792 {
793 for (options_p opt = opts; opt; opt = opt->next)
794 if (strcmp (opt->name, str) == 0)
795 return true;
796 return false;
797 }
798
799
800 /* This is called type(), but what it parses (sort of) is what C calls
801 declaration-specifiers and specifier-qualifier-list:
802
803 SCALAR
804 | ID // typedef
805 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
806 | ENUM ID ( '{' ... '}' )?
807
808 Returns a partial type; under some conditions (notably
809 "struct foo GTY((...)) thing;") it may write an options
810 structure to *OPTSP.
811
812 NESTED is true when parsing a declaration already known to have a
813 GTY marker. In these cases, typedef and enum declarations are not
814 allowed because gengtype only understands types at the global
815 scope. */
816
817 static type_p
818 type (options_p *optsp, bool nested)
819 {
820 const char *s;
821 *optsp = 0;
822 switch (token ())
823 {
824 case SCALAR:
825 s = advance ();
826 return create_scalar_type (s);
827
828 case ID:
829 case VEC_TOKEN:
830 s = typedef_name ();
831 return resolve_typedef (s, &lexer_line);
832
833 case IGNORABLE_CXX_KEYWORD:
834 /* By returning NULL here, we indicate to the caller that they
835 should ignore everything following this keyword up to the
836 next ';' or '}'. */
837 return NULL;
838
839 case STRUCT:
840 case UNION:
841 {
842 options_p opts = 0;
843 /* GTY annotations follow attribute syntax
844 GTY_BEFORE_ID is for union/struct declarations
845 GTY_AFTER_ID is for variable declarations. */
846 enum
847 {
848 NO_GTY,
849 GTY_BEFORE_ID,
850 GTY_AFTER_ID
851 } is_gty = NO_GTY;
852 enum typekind kind = (token () == UNION) ? TYPE_UNION : TYPE_STRUCT;
853 advance ();
854
855 /* Top-level structures that are not explicitly tagged GTY(())
856 are treated as mere forward declarations. This is because
857 there are a lot of structures that we don't need to know
858 about, and some of those have C++ and macro constructs that
859 we cannot handle. */
860 if (nested || token () == GTY_TOKEN)
861 {
862 is_gty = GTY_BEFORE_ID;
863 opts = gtymarker_opt ();
864 }
865
866 if (token () == ID)
867 s = advance ();
868 else
869 s = xasprintf ("anonymous:%s:%d",
870 get_input_file_name (lexer_line.file),
871 lexer_line.line);
872
873 /* Unfortunately above GTY_TOKEN check does not capture the
874 typedef struct_type GTY case. */
875 if (token () == GTY_TOKEN)
876 {
877 is_gty = GTY_AFTER_ID;
878 opts = gtymarker_opt ();
879 }
880
881 if (token () == ':')
882 {
883 /* Skip over C++ inheritance specification. */
884 while (token () != '{')
885 advance ();
886 }
887
888 if (is_gty)
889 {
890 bool is_user_gty = opts_have (opts, "user");
891 if (token () == '{')
892 {
893 pair_p fields;
894
895 if (is_gty == GTY_AFTER_ID)
896 parse_error ("GTY must be specified before identifier");
897
898 if (!is_user_gty)
899 {
900 advance ();
901 fields = struct_field_seq ();
902 require ('}');
903 }
904 else
905 {
906 /* Do not look inside user defined structures. */
907 fields = NULL;
908 kind = TYPE_USER_STRUCT;
909 consume_balanced ('{', '}');
910 }
911
912 return new_structure (s, kind, &lexer_line, fields, opts);
913 }
914 }
915 else if (token () == '{')
916 consume_balanced ('{', '}');
917 if (opts)
918 *optsp = opts;
919 return find_structure (s, kind);
920 }
921
922 case TYPEDEF:
923 /* In C++, a typedef inside a struct/class/union defines a new
924 type for that inner scope. We cannot support this in
925 gengtype because we have no concept of scoping.
926
927 We handle typedefs in the global scope separately (see
928 parse_file), so if we find a 'typedef', we must be inside
929 a struct. */
930 gcc_assert (nested);
931 parse_error ("typedefs not supported in structures marked with "
932 "automatic GTY markers. Use GTY((user)) to mark "
933 "this structure.");
934 advance ();
935 return NULL;
936
937 case ENUM:
938 advance ();
939 if (token () == ID)
940 s = advance ();
941 else
942 s = xasprintf ("anonymous:%s:%d",
943 get_input_file_name (lexer_line.file),
944 lexer_line.line);
945
946 if (token () == '{')
947 consume_balanced ('{', '}');
948
949 /* If after parsing the enum we are at the end of the statement,
950 and we are currently inside a structure, then this was an
951 enum declaration inside this scope.
952
953 We cannot support this for the same reason we cannot support
954 'typedef' inside structures (see the TYPEDEF handler above).
955 If this happens, emit an error and return NULL. */
956 if (nested && token () == ';')
957 {
958 parse_error ("enum definitions not supported in structures marked "
959 "with automatic GTY markers. Use GTY((user)) to mark "
960 "this structure.");
961 advance ();
962 return NULL;
963 }
964
965 return create_scalar_type (s);
966
967 default:
968 parse_error ("expected a type specifier, have %s", print_cur_token ());
969 advance ();
970 return create_scalar_type ("erroneous type");
971 }
972 }
973 \f
974 /* Top level constructs. */
975
976 /* Dispatch declarations beginning with 'typedef'. */
977
978 static void
979 typedef_decl (void)
980 {
981 type_p ty, dty;
982 const char *name;
983 options_p opts;
984 bool another;
985
986 gcc_assert (token () == TYPEDEF);
987 advance ();
988
989 ty = type (&opts, false);
990 if (!ty)
991 return;
992 if (opts)
993 parse_error ("GTY((...)) cannot be applied to a typedef");
994 do
995 {
996 dty = declarator (ty, &name, &opts);
997 if (opts)
998 parse_error ("GTY((...)) cannot be applied to a typedef");
999
1000 /* Yet another place where we could have junk (notably attributes)
1001 after the declarator. */
1002 another = consume_until_comma_or_eos ();
1003 if (dty)
1004 do_typedef (name, dty, &lexer_line);
1005 }
1006 while (another);
1007 }
1008
1009 /* Structure definition: type() does all the work. */
1010
1011 static void
1012 struct_or_union (void)
1013 {
1014 options_p dummy;
1015 type (&dummy, false);
1016 /* There may be junk after the type: notably, we cannot currently
1017 distinguish 'struct foo *function(prototype);' from 'struct foo;'
1018 ... we could call declarator(), but it's a waste of time at
1019 present. Instead, just eat whatever token is currently lookahead
1020 and go back to lexical skipping mode. */
1021 advance ();
1022 }
1023
1024 /* GC root declaration:
1025 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
1026 If the gtymarker is not present, we ignore the rest of the declaration. */
1027 static void
1028 extern_or_static (void)
1029 {
1030 options_p opts, opts2, dopts;
1031 type_p ty, dty;
1032 const char *name;
1033 require2 (EXTERN, STATIC);
1034
1035 if (token () != GTY_TOKEN)
1036 {
1037 advance ();
1038 return;
1039 }
1040
1041 opts = gtymarker ();
1042 ty = type (&opts2, true); /* if we get here, it's got a GTY(()) */
1043 dty = declarator (ty, &name, &dopts);
1044
1045 if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
1046 parse_error ("GTY((...)) specified more than once for %s", name);
1047 else if (opts2)
1048 opts = opts2;
1049 else if (dopts)
1050 opts = dopts;
1051
1052 if (dty)
1053 {
1054 note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
1055 require2 (';', '=');
1056 }
1057 }
1058
1059 /* Parse the file FNAME for GC-relevant declarations and definitions.
1060 This is the only entry point to this file. */
1061 void
1062 parse_file (const char *fname)
1063 {
1064 yybegin (fname);
1065 for (;;)
1066 {
1067 switch (token ())
1068 {
1069 case EXTERN:
1070 case STATIC:
1071 extern_or_static ();
1072 break;
1073
1074 case STRUCT:
1075 case UNION:
1076 struct_or_union ();
1077 break;
1078
1079 case TYPEDEF:
1080 typedef_decl ();
1081 break;
1082
1083 case EOF_TOKEN:
1084 goto eof;
1085
1086 default:
1087 parse_error ("unexpected top level token, %s", print_cur_token ());
1088 goto eof;
1089 }
1090 lexer_toplevel_done = 1;
1091 }
1092
1093 eof:
1094 advance ();
1095 yyend ();
1096 }