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