hashtab.h: Update GTY annotations to new syntax
[platform/upstream/gcc.git] / gcc / gengtype-parse.c
1 /* Process source files and output type information.
2    Copyright (C) 2006, 2007 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 #include "bconfig.h"
21 #include "system.h"
22 #include "gengtype.h"
23
24 /* This is a simple recursive-descent parser which understands a subset of
25    the C type grammar.
26
27    Rule functions are suffixed _seq if they scan a sequence of items;
28    _opt if they may consume zero tokens; _seqopt if both are true.  The
29    "consume_" prefix indicates that a sequence of tokens is parsed for
30    syntactic correctness and then thrown away.  */
31
32 /* Simple one-token lookahead mechanism.  */
33
34 struct token
35 {
36   const char *value;
37   int code;
38   bool valid;
39 };
40 static struct token T;
41
42 /* Retrieve the code of the current token; if there is no current token,
43    get the next one from the lexer.  */
44 static inline int
45 token (void)
46 {
47   if (!T.valid)
48     {
49       T.code = yylex (&T.value);
50       T.valid = true;
51     }
52   return T.code;
53 }
54
55 /* Retrieve the value of the current token (if any) and mark it consumed.
56    The next call to token() will get another token from the lexer.  */
57 static inline const char *
58 advance (void)
59 {
60   T.valid = false;
61   return T.value;
62 }
63
64 /* Diagnostics.  */
65
66 /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET.  */
67 static const char *const token_names[] = {
68   "GTY",
69   "typedef",
70   "extern",
71   "static",
72   "union",
73   "struct",
74   "enum",
75   "VEC",
76   "DEF_VEC_[OP]",
77   "DEF_VEC_I",
78   "DEF_VEC_ALLOC_[IOP]",
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 };
90
91 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE.  */
92 static const char *const token_value_format[] = {
93   "%s",
94   "'%s'",
95   "'%s'",
96   "'%s'",
97   "'\"%s\"'",
98   "\"'%s'\"",
99   "'[%s]'",
100 };
101
102 /* Produce a printable representation for a token defined by CODE and
103    VALUE.  This sometimes returns pointers into malloc memory and
104    sometimes not, therefore it is unsafe to free the pointer it
105    returns, so that memory is leaked.  This does not matter, as this
106    function is only used for diagnostics, and in a successful run of
107    the program there will be none.  */
108 static const char *
109 print_token (int code, const char *value)
110 {
111   if (code < CHAR_TOKEN_OFFSET)
112     return xasprintf ("'%c'", code);
113   else if (code < FIRST_TOKEN_WITH_VALUE)
114     return xasprintf ("'%s'", token_names[code - CHAR_TOKEN_OFFSET]);
115   else if (!value)
116     return token_names[code - CHAR_TOKEN_OFFSET]; /* don't quote these */
117   else
118     return xasprintf (token_value_format[code - FIRST_TOKEN_WITH_VALUE],
119                       value);
120 }
121
122 /* Convenience wrapper around print_token which produces the printable
123    representation of the current token.  */
124 static inline const char *
125 print_cur_token (void)
126 {
127   return print_token (T.code, T.value);
128 }
129
130 /* Report a parse error on the current line, with diagnostic MSG.
131    Behaves as standard printf with respect to additional arguments and
132    format escapes.  */
133 static void ATTRIBUTE_PRINTF_1
134 parse_error (const char *msg, ...)
135 {
136   va_list ap;
137
138   fprintf (stderr, "%s:%d: parse error: ", lexer_line.file, lexer_line.line);
139
140   va_start (ap, msg);
141   vfprintf (stderr, msg, ap);
142   va_end (ap);
143
144   hit_error = true;
145 }
146
147 /* If the next token does not have code T, report a parse error; otherwise
148    return the token's value.  */
149 static const char *
150 require (int t)
151 {
152   int u = token ();
153   const char *v = advance ();
154   if (u != t)
155     {
156       parse_error ("expected %s, have %s",
157                    print_token (t, 0), print_token (u, v));
158       return 0;
159     }
160   return v;
161 }
162
163 /* If the next token does not have one of the codes T1 or T2, report a
164    parse error; otherwise return the token's value.  */
165 static const char *
166 require2 (int t1, int t2)
167 {
168   int u = token ();
169   const char *v = advance ();
170   if (u != t1 && u != t2)
171     {
172       parse_error ("expected %s or %s, have %s",
173                    print_token (t1, 0), print_token (t2, 0),
174                    print_token (u, v));
175       return 0;
176     }
177   return v;
178 }
179
180 /* Near-terminals.  */
181
182 /* C-style string constant concatenation: STRING+
183    Bare STRING should appear nowhere else in this file.  */
184 static const char *
185 string_seq (void)
186 {
187   const char *s1, *s2;
188   size_t l1, l2;
189   char *buf;
190
191   s1 = require (STRING);
192   if (s1 == 0)
193     return "";
194   while (token () == STRING)
195     {
196       s2 = advance ();
197
198       l1 = strlen (s1);
199       l2 = strlen (s2);
200       buf = XRESIZEVEC (char, CONST_CAST(char *, s1), l1 + l2 + 1);
201       memcpy (buf + l1, s2, l2 + 1);
202       XDELETE (CONST_CAST (char *, s2));
203       s1 = buf;
204     }
205   return s1;
206 }
207
208 /* typedef_name: either an ID, or VEC(x,y) which is translated to VEC_x_y.
209    Use only where VEC(x,y) is legitimate, i.e. in positions where a
210    typedef name may appear.  */
211 static const char *
212 typedef_name (void)
213 {
214   if (token () == VEC_TOKEN)
215     {
216       const char *c1, *c2, *r;
217       advance ();
218       require ('(');
219       c1 = require2 (ID, SCALAR);
220       require (',');
221       c2 = require (ID);
222       require (')');
223       r = concat ("VEC_", c1, "_", c2, (char *)0);
224       free (CONST_CAST (char *, c1));
225       free (CONST_CAST (char *, c2));
226       return r;
227     }
228   else
229     return require (ID);
230 }
231
232 /* Absorb a sequence of tokens delimited by balanced ()[]{}.  */
233 static void
234 consume_balanced (int opener, int closer)
235 {
236   require (opener);
237   for (;;)
238     switch (token ())
239       {
240       default: advance (); break;
241       case '(': consume_balanced ('(',')'); break;
242       case '[': consume_balanced ('[',']'); break;
243       case '{': consume_balanced ('{','}'); break;
244
245       case '}':
246       case ']':
247       case ')':
248         if (token () != closer)
249           parse_error ("unbalanced delimiters - expected '%c', have '%c'",
250                        closer, token ());
251         advance ();
252         return;
253
254       case EOF_TOKEN:
255         parse_error ("unexpected end of file within %c%c-delimited construct",
256                      opener, closer);
257         return;
258       }
259 }
260
261 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
262    expressions, until we encounter a semicolon outside any such
263    delimiters; absorb that too.  If IMMEDIATE is true, it is an error
264    if the semicolon is not the first token encountered.  */
265 static void
266 consume_until_semi (bool immediate)
267 {
268   if (immediate && token () != ';')
269     require (';');
270   for (;;)
271     switch (token ())
272       {
273       case ';': advance (); return;
274       default:  advance (); break;
275
276       case '(': consume_balanced ('(',')'); break;
277       case '[': consume_balanced ('[',']'); break;
278       case '{': consume_balanced ('{','}'); break;
279
280       case '}':
281       case ']':
282       case ')':
283         parse_error ("unmatched '%c' while scanning for ';'", token ());
284         return;
285
286       case EOF_TOKEN:
287         parse_error ("unexpected end of file while scanning for ';'");
288         return;
289       }
290 }
291
292 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
293    expressions, until we encounter a comma or semicolon outside any
294    such delimiters; absorb that too.  If IMMEDIATE is true, it is an
295    error if the comma or semicolon is not the first token encountered.
296    Returns true if the loop ended with a comma.  */
297 static bool
298 consume_until_comma_or_semi (bool immediate)
299 {
300   if (immediate && token () != ',' && token () != ';')
301     require2 (',', ';');
302   for (;;)
303     switch (token ())
304       {
305       case ',': advance (); return true;
306       case ';': advance (); return false;
307       default:  advance (); break;
308
309       case '(': consume_balanced ('(',')'); break;
310       case '[': consume_balanced ('[',']'); break;
311       case '{': consume_balanced ('{','}'); break;
312
313       case '}':
314       case ']':
315       case ')':
316         parse_error ("unmatched '%s' while scanning for ',' or ';'",
317                      print_cur_token ());
318         return false;
319
320       case EOF_TOKEN:
321         parse_error ("unexpected end of file while scanning for ',' or ';'");
322         return false;
323       }
324 }
325
326 \f
327 /* GTY(()) option handling.  */
328 static type_p type (options_p *optsp, bool nested);
329
330 /* Optional parenthesized string: ('(' string_seq ')')? */
331 static options_p
332 str_optvalue_opt (options_p prev)
333 {
334   const char *name = advance ();
335   const char *value = "";
336   if (token () == '(')
337     {
338       advance ();
339       value = string_seq ();
340       require (')');
341     }
342   return create_option (prev, name, value);
343 }
344
345 /* absdecl: type '*'*
346    -- a vague approximation to what the C standard calls an abstract
347    declarator.  The only kinds that are actually used are those that
348    are just a bare type and those that have trailing pointer-stars.
349    Further kinds should be implemented if and when they become
350    necessary.  Used only within GTY(()) option values, therefore
351    further GTY(()) tags within the type are invalid.  Note that the
352    return value has already been run through adjust_field_type.  */
353 static type_p
354 absdecl (void)
355 {
356   type_p ty;
357   options_p opts;
358
359   ty = type (&opts, true);
360   while (token () == '*')
361     {
362       ty = create_pointer (ty);
363       advance ();
364     }
365
366   if (opts)
367     parse_error ("nested GTY(()) options are invalid");
368
369   return adjust_field_type (ty, 0);
370 }
371
372 /* Type-option: '(' absdecl ')' */
373 static options_p
374 type_optvalue (options_p prev, const char *name)
375 {
376   type_p ty;
377   require ('(');
378   ty = absdecl ();
379   require (')');
380   return create_option (prev, name, ty);
381 }
382
383 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
384 static options_p
385 nestedptr_optvalue (options_p prev)
386 {
387   type_p ty;
388   const char *from, *to;
389
390   require ('(');
391   ty = absdecl ();
392   require (',');
393   to = string_seq ();
394   require (',');
395   from = string_seq ();
396   require (')');
397
398   return create_nested_ptr_option (prev, ty, to, from);
399 }
400
401 /* One GTY(()) option:
402          ID str_optvalue_opt
403        | PTR_ALIAS type_optvalue
404        | PARAM_IS type_optvalue
405        | NESTED_PTR nestedptr_optvalue
406  */
407 static options_p
408 option (options_p prev)
409 {
410   switch (token ())
411     {
412     case ID:
413       return str_optvalue_opt (prev);
414
415     case PTR_ALIAS:
416       advance ();
417       return type_optvalue (prev, "ptr_alias");
418
419     case PARAM_IS:
420       return type_optvalue (prev, advance ());
421
422     case NESTED_PTR:
423       advance ();
424       return nestedptr_optvalue (prev);
425
426     default:
427       parse_error ("expected an option keyword, have %s",
428                    print_cur_token ());
429       advance ();
430       return create_option (prev, "", "");
431     }
432 }
433
434 /* One comma-separated list of options.  */
435 static options_p
436 option_seq (void)
437 {
438   options_p o;
439
440   o = option (0);
441   while (token () == ',')
442     {
443       advance ();
444       o = option (o);
445     }
446   return o;
447 }
448
449 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
450 static options_p
451 gtymarker (void)
452 {
453   options_p result = 0;
454   require (GTY_TOKEN);
455   require ('(');
456   require ('(');
457   if (token () != ')')
458     result = option_seq ();
459   require (')');
460   require (')');
461   return result;
462 }
463
464 /* Optional GTY marker.  */
465 static options_p
466 gtymarker_opt (void)
467 {
468   if (token () != GTY_TOKEN)
469     return 0;
470   return gtymarker ();
471 }
472 \f
473 /* Declarators. The logic here is largely lifted from c-parser.c.
474    Note that we do not have to process abstract declarators, which can
475    appear only in parameter type lists or casts (but see absdecl,
476    above).  Also, type qualifiers are thrown out in gengtype-lex.l so
477    we don't have to do it.  */
478
479 /* array_and_function_declarators_opt:
480       \epsilon
481       array_and_function_declarators_opt ARRAY
482       array_and_function_declarators_opt '(' ... ')'
483
484    where '...' indicates stuff we ignore except insofar as grouping
485    symbols ()[]{} must balance.
486
487    Subroutine of direct_declarator - do not use elsewhere. */
488
489 static type_p
490 array_and_function_declarators_opt (type_p ty)
491 {
492   if (token () == ARRAY)
493     {
494       const char *array = advance ();
495       return create_array (array_and_function_declarators_opt (ty), array);
496     }
497   else if (token () == '(')
498     {
499       /* We don't need exact types for functions.  */
500       consume_balanced ('(', ')');
501       array_and_function_declarators_opt (ty);
502       return create_scalar_type ("function type");
503     }
504   else
505     return ty;
506 }
507
508 static type_p inner_declarator (type_p, const char **, options_p *);
509
510 /* direct_declarator:
511       '(' inner_declarator ')'
512       gtymarker_opt ID array_and_function_declarators_opt
513
514    Subroutine of declarator, mutually recursive with inner_declarator;
515    do not use elsewhere.  */
516 static type_p
517 direct_declarator (type_p ty, const char **namep, options_p *optsp)
518 {
519   /* The first token in a direct-declarator must be an ID, a
520      GTY marker, or an open parenthesis.  */
521   switch (token ())
522     {
523     case GTY_TOKEN:
524       *optsp = gtymarker ();
525       /* fall through */
526     case ID:
527       *namep = require (ID);
528       break;
529
530     case '(':
531       advance ();
532       ty = inner_declarator (ty, namep, optsp);
533       require (')');
534       break;
535
536     default:
537       parse_error ("expected '(', 'GTY', or an identifier, have %s",
538                    print_cur_token ());
539       /* Do _not_ advance if what we have is a close squiggle brace, as
540          we will get much better error recovery that way.  */
541       if (token () != '}')
542         advance ();
543       return 0;
544     }
545   return array_and_function_declarators_opt (ty);
546 }
547
548 /* The difference between inner_declarator and declarator is in the
549    handling of stars.  Consider this declaration:
550
551       char * (*pfc) (void)
552
553    It declares a pointer to a function that takes no arguments and
554    returns a char*.  To construct the correct type for this
555    declaration, the star outside the parentheses must be processed
556    _before_ the function type, the star inside the parentheses must
557    be processed _after_ the function type.  To accomplish this,
558    declarator() creates pointers before recursing (it is actually
559    coded as a while loop), whereas inner_declarator() recurses before
560    creating pointers.  */
561
562 /* inner_declarator:
563      '*' inner_declarator
564      direct_declarator
565
566    Mutually recursive subroutine of direct_declarator; do not use
567    elsewhere.  */
568
569 static type_p
570 inner_declarator (type_p ty, const char **namep, options_p *optsp)
571 {
572   if (token () == '*')
573     {
574       type_p inner;
575       advance ();
576       inner = inner_declarator (ty, namep, optsp);
577       if (inner == 0)
578         return 0;
579       else
580         return create_pointer (ty);
581     }
582   else
583     return direct_declarator (ty, namep, optsp);
584 }
585
586 /* declarator: '*'+ direct_declarator
587
588    This is the sole public interface to this part of the grammar.
589    Arguments are the type known so far, a pointer to where the name
590    may be stored, and a pointer to where GTY options may be stored.
591    Returns the final type. */
592
593 static type_p
594 declarator (type_p ty, const char **namep, options_p *optsp)
595 {
596   *namep = 0;
597   *optsp = 0;
598   while (token () == '*')
599     {
600       advance ();
601       ty = create_pointer (ty);
602     }
603   return direct_declarator (ty, namep, optsp);
604 }
605 \f
606 /* Types and declarations.  */
607
608 /* Structure field(s) declaration:
609    (
610        type bitfield ';'
611      | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
612    )+
613
614    Knows that such declarations must end with a close brace (or,
615    erroneously, at EOF).
616  */
617 static pair_p
618 struct_field_seq (void)
619 {
620   pair_p f = 0;
621   type_p ty, dty;
622   options_p opts, dopts;
623   const char *name;
624   bool another;
625
626   do
627     {
628       ty = type (&opts, true);
629       /* Another piece of the IFCVT_EXTRA_FIELDS special case, see type().  */
630       if (!ty && token () == '}')
631         break;
632
633       if (!ty || token () == ':')
634         {
635           consume_until_semi (false);
636           continue;
637         }
638
639       do
640         {
641           dty = declarator (ty, &name, &dopts);
642           /* There could be any number of weird things after the declarator,
643              notably bitfield declarations and __attribute__s.  If this
644              function returns true, the last thing was a comma, so we have
645              more than one declarator paired with the current type.  */
646           another = consume_until_comma_or_semi (false);
647
648           if (!dty)
649             continue;
650
651           if (opts && dopts)
652             parse_error ("two GTY(()) options for field %s", name);
653           if (opts && !dopts)
654             dopts = opts;
655
656           f = create_field_at (f, dty, name, dopts, &lexer_line);
657         }
658       while (another);
659     }
660   while (token () != '}' && token () != EOF_TOKEN);
661   return nreverse_pairs (f);
662 }
663
664 /* This is called type(), but what it parses (sort of) is what C calls
665    declaration-specifiers and specifier-qualifier-list:
666
667      SCALAR
668    | ID     // typedef
669    | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
670    | ENUM ID ( '{' ... '}' )?
671
672    Returns a partial type; under some conditions (notably
673    "struct foo GTY((...)) thing;") it may write an options
674    structure to *OPTSP.
675  */
676 static type_p
677 type (options_p *optsp, bool nested)
678 {
679   const char *s;
680   *optsp = 0;
681   switch (token ())
682     {
683     case SCALAR:
684       s = advance ();
685       return create_scalar_type (s);
686
687     case ID:
688     case VEC_TOKEN:
689       s = typedef_name ();
690       return resolve_typedef (s, &lexer_line);
691
692     case STRUCT:
693     case UNION:
694       {
695         options_p opts = 0;
696     /* GTY annotations follow attribute syntax 
697        GTY_BEFORE_ID is for union/struct declarations
698        GTY_AFTER_ID is for variable declarations.  */
699     enum {
700         NO_GTY,
701         GTY_BEFORE_ID,
702         GTY_AFTER_ID
703     } is_gty = NO_GTY;
704     bool is_union = (token () == UNION);
705         advance ();
706
707         /* Top-level structures that are not explicitly tagged GTY(())
708            are treated as mere forward declarations.  This is because
709            there are a lot of structures that we don't need to know
710            about, and some of those have weird macro stuff in them
711            that we can't handle.  */
712         if (nested || token () == GTY_TOKEN)
713           {
714         is_gty = GTY_BEFORE_ID;
715         opts = gtymarker_opt ();
716           }
717
718         if (token () == ID)
719           s = advance ();
720         else
721           s = xasprintf ("anonymous:%s:%d", lexer_line.file, lexer_line.line);
722
723         /* Unfortunately above GTY_TOKEN check does not capture the
724            typedef struct_type GTY case.  */
725         if (token () == GTY_TOKEN)
726           {
727         is_gty = GTY_AFTER_ID;
728         opts = gtymarker_opt ();
729           }
730         
731     if (is_gty) 
732       {
733         if (token () == '{')
734           {
735             pair_p fields;
736
737             if (is_gty == GTY_AFTER_ID) 
738                 parse_error ("GTY must be specified before identifier");
739               
740             advance ();
741             fields = struct_field_seq ();
742             require ('}');
743             return new_structure (s, is_union, &lexer_line, fields, opts);
744           }
745       } 
746     else if (token () == '{')
747       consume_balanced ('{', '}');
748         if (opts)
749           *optsp = opts;
750         return find_structure (s, is_union);
751       }
752
753     case ENUM:
754       advance ();
755         if (token () == ID)
756           s = advance ();
757         else
758           s = xasprintf ("anonymous:%s:%d", lexer_line.file, lexer_line.line);
759
760       if (token () == '{')
761         consume_balanced ('{','}');
762       return create_scalar_type (s);
763
764     default:
765       parse_error ("expected a type specifier, have %s", print_cur_token ());
766       advance ();
767       return create_scalar_type ("erroneous type");
768     }
769 }
770 \f
771 /* Top level constructs.  */
772
773 /* Dispatch declarations beginning with 'typedef'.  */
774
775 static void
776 typedef_decl (void)
777 {
778   type_p ty, dty;
779   const char *name;
780   options_p opts;
781   bool another;
782
783   gcc_assert (token () == TYPEDEF);
784   advance ();
785
786   ty = type (&opts, false);
787   if (!ty)
788     return;
789   if (opts)
790     parse_error ("GTY((...)) cannot be applied to a typedef");
791   do
792     {
793       dty = declarator (ty, &name, &opts);
794       if (opts)
795         parse_error ("GTY((...)) cannot be applied to a typedef");
796
797       /* Yet another place where we could have junk (notably attributes)
798          after the declarator.  */
799       another = consume_until_comma_or_semi (false);
800       if (dty)
801         do_typedef (name, dty, &lexer_line);
802     }
803   while (another);
804 }
805
806 /* Structure definition: type() does all the work.  */
807
808 static void
809 struct_or_union (void)
810 {
811   options_p dummy;
812   type (&dummy, false);
813   /* There may be junk after the type: notably, we cannot currently
814      distinguish 'struct foo *function(prototype);' from 'struct foo;'
815      ...  we could call declarator(), but it's a waste of time at
816      present.  Instead, just eat whatever token is currently lookahead
817      and go back to lexical skipping mode. */
818   advance ();
819 }
820
821 /* GC root declaration:
822      (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
823    If the gtymarker is not present, we ignore the rest of the declaration.  */
824 static void
825 extern_or_static (void)
826 {
827   options_p opts, opts2, dopts;
828   type_p ty, dty;
829   const char *name;
830   require2 (EXTERN, STATIC);
831
832   if (token () != GTY_TOKEN)
833     {
834       advance ();
835       return;
836     }
837
838   opts = gtymarker ();
839   ty   = type (&opts2, true);  /* if we get here, it's got a GTY(()) */
840   dty  = declarator (ty, &name, &dopts);
841
842   if ((opts && dopts) || (opts && opts2) || (opts2 && dopts))
843     parse_error ("GTY((...)) specified more than once for %s", name);
844   else if (opts2)
845     opts = opts2;
846   else if (dopts)
847     opts = dopts;
848
849   if (dty)
850     {
851       note_variable (name, adjust_field_type (dty, opts), opts, &lexer_line);
852       require2 (';', '=');
853     }
854 }
855
856 /* Definition of a generic VEC structure:
857
858    'DEF_VEC_[IPO]' '(' id ')' ';'
859
860    Scalar VECs require slightly different treatment than otherwise -
861    that's handled in note_def_vec, we just pass it along.*/
862 static void
863 def_vec (void)
864 {
865   bool is_scalar = (token() == DEFVEC_I);
866   const char *type;
867
868   require2 (DEFVEC_OP, DEFVEC_I);
869   require ('(');
870   type = require2 (ID, SCALAR);
871   require (')');
872   require (';');
873
874   if (!type)
875     return;
876
877   note_def_vec (type, is_scalar, &lexer_line);
878   note_def_vec_alloc (type, "none", &lexer_line);
879 }
880
881 /* Definition of an allocation strategy for a VEC structure:
882
883    'DEF_VEC_ALLOC_[IPO]' '(' id ',' id ')' ';'
884
885    For purposes of gengtype, this just declares a wrapper structure.  */
886 static void
887 def_vec_alloc (void)
888 {
889   const char *type, *astrat;
890
891   require (DEFVEC_ALLOC);
892   require ('(');
893   type = require2 (ID, SCALAR);
894   require (',');
895   astrat = require (ID);
896   require (')');
897   require (';');
898
899   if (!type || !astrat)
900     return;
901
902   note_def_vec_alloc (type, astrat, &lexer_line);
903 }
904
905 /* Parse the file FNAME for GC-relevant declarations and definitions.
906    This is the only entry point to this file.  */
907 void
908 parse_file (const char *fname)
909 {
910   yybegin (fname);
911   for (;;)
912     {
913       switch (token ())
914         {
915         case EXTERN:
916         case STATIC:
917           extern_or_static ();
918           break;
919
920         case STRUCT:
921         case UNION:
922           struct_or_union ();
923           break;
924
925         case TYPEDEF:
926           typedef_decl ();
927           break;
928
929         case DEFVEC_OP:
930         case DEFVEC_I:
931           def_vec ();
932           break;
933
934         case DEFVEC_ALLOC:
935           def_vec_alloc ();
936           break;
937
938         case EOF_TOKEN:
939           goto eof;
940
941         default:
942           parse_error ("unexpected top level token, %s", print_cur_token ());
943           goto eof;
944         }
945       lexer_toplevel_done = 1;
946     }
947
948  eof:
949   advance ();
950   yyend ();
951 }