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