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