1 /* Process source files and output type information.
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
24 /* This is a simple recursive-descent parser which understands a subset of
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. */
32 /* Simple one-token lookahead mechanism. */
40 static struct token T
;
42 /* Retrieve the code of the current token; if there is no current token,
43 get the next one from the lexer. */
49 T
.code
= yylex (&T
.value
);
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 *
66 /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
67 static const char *const token_names
[] = {
78 "DEF_VEC_ALLOC_[IOP]",
82 "a param<N>_is option",
87 "a character constant",
88 "an array declarator",
91 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
92 static const char *const token_value_format
[] = {
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. */
109 print_token (int code
, const char *value
)
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
]);
116 return token_names
[code
- CHAR_TOKEN_OFFSET
]; /* don't quote these */
118 return xasprintf (token_value_format
[code
- FIRST_TOKEN_WITH_VALUE
],
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)
127 return print_token (T
.code
, T
.value
);
130 /* Report a parse error on the current line, with diagnostic MSG.
131 Behaves as standard printf with respect to additional arguments and
133 static void ATTRIBUTE_PRINTF_1
134 parse_error (const char *msg
, ...)
138 fprintf (stderr
, "%s:%d: parse error: ", lexer_line
.file
, lexer_line
.line
);
141 vfprintf (stderr
, msg
, ap
);
144 fputc ('\n', stderr
);
149 /* If the next token does not have code T, report a parse error; otherwise
150 return the token's value. */
155 const char *v
= advance ();
158 parse_error ("expected %s, have %s",
159 print_token (t
, 0), print_token (u
, v
));
165 /* If the next token does not have one of the codes T1 or T2, report a
166 parse error; otherwise return the token's value. */
168 require2 (int t1
, int t2
)
171 const char *v
= advance ();
172 if (u
!= t1
&& u
!= t2
)
174 parse_error ("expected %s or %s, have %s",
175 print_token (t1
, 0), print_token (t2
, 0),
182 /* Near-terminals. */
184 /* C-style string constant concatenation: STRING+
185 Bare STRING should appear nowhere else in this file. */
193 s1
= require (STRING
);
196 while (token () == STRING
)
202 buf
= XRESIZEVEC (char, CONST_CAST (char *, s1
), l1
+ l2
+ 1);
203 memcpy (buf
+ l1
, s2
, l2
+ 1);
204 XDELETE (CONST_CAST (char *, s2
));
210 /* typedef_name: either an ID, or VEC(x,y) which is translated to VEC_x_y.
211 Use only where VEC(x,y) is legitimate, i.e. in positions where a
212 typedef name may appear. */
216 if (token () == VEC_TOKEN
)
218 const char *c1
, *c2
, *r
;
221 c1
= require2 (ID
, SCALAR
);
225 r
= concat ("VEC_", c1
, "_", c2
, (char *) 0);
226 free (CONST_CAST (char *, c1
));
227 free (CONST_CAST (char *, c2
));
234 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
236 consume_balanced (int opener
, int closer
)
246 consume_balanced ('(', ')');
249 consume_balanced ('[', ']');
252 consume_balanced ('{', '}');
258 if (token () != closer
)
259 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
265 parse_error ("unexpected end of file within %c%c-delimited construct",
271 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
272 expressions, until we encounter a semicolon outside any such
273 delimiters; absorb that too. If IMMEDIATE is true, it is an error
274 if the semicolon is not the first token encountered. */
276 consume_until_semi (bool immediate
)
278 if (immediate
&& token () != ';')
291 consume_balanced ('(', ')');
294 consume_balanced ('[', ']');
297 consume_balanced ('{', '}');
303 parse_error ("unmatched '%c' while scanning for ';'", token ());
307 parse_error ("unexpected end of file while scanning for ';'");
312 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
313 expressions, until we encounter a comma or semicolon outside any
314 such delimiters; absorb that too. If IMMEDIATE is true, it is an
315 error if the comma or semicolon is not the first token encountered.
316 Returns true if the loop ended with a comma. */
318 consume_until_comma_or_semi (bool immediate
)
320 if (immediate
&& token () != ',' && token () != ';')
336 consume_balanced ('(', ')');
339 consume_balanced ('[', ']');
342 consume_balanced ('{', '}');
348 parse_error ("unmatched '%s' while scanning for ',' or ';'",
353 parse_error ("unexpected end of file while scanning for ',' or ';'");
359 /* GTY(()) option handling. */
360 static type_p
type (options_p
*optsp
, bool nested
);
362 /* Optional parenthesized string: ('(' string_seq ')')? */
364 str_optvalue_opt (options_p prev
)
366 const char *name
= advance ();
367 const char *value
= "";
371 value
= string_seq ();
374 return create_option (prev
, name
, value
);
377 /* absdecl: type '*'*
378 -- a vague approximation to what the C standard calls an abstract
379 declarator. The only kinds that are actually used are those that
380 are just a bare type and those that have trailing pointer-stars.
381 Further kinds should be implemented if and when they become
382 necessary. Used only within GTY(()) option values, therefore
383 further GTY(()) tags within the type are invalid. Note that the
384 return value has already been run through adjust_field_type. */
391 ty
= type (&opts
, true);
392 while (token () == '*')
394 ty
= create_pointer (ty
);
399 parse_error ("nested GTY(()) options are invalid");
401 return adjust_field_type (ty
, 0);
404 /* Type-option: '(' absdecl ')' */
406 type_optvalue (options_p prev
, const char *name
)
412 return create_option (prev
, name
, ty
);
415 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
417 nestedptr_optvalue (options_p prev
)
420 const char *from
, *to
;
427 from
= string_seq ();
430 return create_nested_ptr_option (prev
, ty
, to
, from
);
433 /* One GTY(()) option:
435 | PTR_ALIAS type_optvalue
436 | PARAM_IS type_optvalue
437 | NESTED_PTR nestedptr_optvalue
440 option (options_p prev
)
445 return str_optvalue_opt (prev
);
449 return type_optvalue (prev
, "ptr_alias");
452 return type_optvalue (prev
, advance ());
456 return nestedptr_optvalue (prev
);
459 parse_error ("expected an option keyword, have %s", print_cur_token ());
461 return create_option (prev
, "", "");
465 /* One comma-separated list of options. */
472 while (token () == ',')
480 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
484 options_p result
= 0;
489 result
= option_seq ();
495 /* Optional GTY marker. */
499 if (token () != GTY_TOKEN
)
504 /* Declarators. The logic here is largely lifted from c-parser.c.
505 Note that we do not have to process abstract declarators, which can
506 appear only in parameter type lists or casts (but see absdecl,
507 above). Also, type qualifiers are thrown out in gengtype-lex.l so
508 we don't have to do it. */
510 /* array_and_function_declarators_opt:
512 array_and_function_declarators_opt ARRAY
513 array_and_function_declarators_opt '(' ... ')'
515 where '...' indicates stuff we ignore except insofar as grouping
516 symbols ()[]{} must balance.
518 Subroutine of direct_declarator - do not use elsewhere. */
521 array_and_function_declarators_opt (type_p ty
)
523 if (token () == ARRAY
)
525 const char *array
= advance ();
526 return create_array (array_and_function_declarators_opt (ty
), array
);
528 else if (token () == '(')
530 /* We don't need exact types for functions. */
531 consume_balanced ('(', ')');
532 array_and_function_declarators_opt (ty
);
533 return create_scalar_type ("function type");
539 static type_p
inner_declarator (type_p
, const char **, options_p
*);
541 /* direct_declarator:
542 '(' inner_declarator ')'
543 gtymarker_opt ID array_and_function_declarators_opt
545 Subroutine of declarator, mutually recursive with inner_declarator;
546 do not use elsewhere. */
548 direct_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
550 /* The first token in a direct-declarator must be an ID, a
551 GTY marker, or an open parenthesis. */
555 *optsp
= gtymarker ();
558 *namep
= require (ID
);
563 ty
= inner_declarator (ty
, namep
, optsp
);
568 parse_error ("expected '(', 'GTY', or an identifier, have %s",
570 /* Do _not_ advance if what we have is a close squiggle brace, as
571 we will get much better error recovery that way. */
576 return array_and_function_declarators_opt (ty
);
579 /* The difference between inner_declarator and declarator is in the
580 handling of stars. Consider this declaration:
584 It declares a pointer to a function that takes no arguments and
585 returns a char*. To construct the correct type for this
586 declaration, the star outside the parentheses must be processed
587 _before_ the function type, the star inside the parentheses must
588 be processed _after_ the function type. To accomplish this,
589 declarator() creates pointers before recursing (it is actually
590 coded as a while loop), whereas inner_declarator() recurses before
591 creating pointers. */
597 Mutually recursive subroutine of direct_declarator; do not use
601 inner_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
607 inner
= inner_declarator (ty
, namep
, optsp
);
611 return create_pointer (ty
);
614 return direct_declarator (ty
, namep
, optsp
);
617 /* declarator: '*'+ direct_declarator
619 This is the sole public interface to this part of the grammar.
620 Arguments are the type known so far, a pointer to where the name
621 may be stored, and a pointer to where GTY options may be stored.
622 Returns the final type. */
625 declarator (type_p ty
, const char **namep
, options_p
*optsp
)
629 while (token () == '*')
632 ty
= create_pointer (ty
);
634 return direct_declarator (ty
, namep
, optsp
);
637 /* Types and declarations. */
639 /* Structure field(s) declaration:
642 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
645 Knows that such declarations must end with a close brace (or,
646 erroneously, at EOF).
649 struct_field_seq (void)
653 options_p opts
, dopts
;
659 ty
= type (&opts
, true);
660 /* Another piece of the IFCVT_EXTRA_FIELDS special case, see type(). */
661 if (!ty
&& token () == '}')
664 if (!ty
|| token () == ':')
666 consume_until_semi (false);
672 dty
= declarator (ty
, &name
, &dopts
);
673 /* There could be any number of weird things after the declarator,
674 notably bitfield declarations and __attribute__s. If this
675 function returns true, the last thing was a comma, so we have
676 more than one declarator paired with the current type. */
677 another
= consume_until_comma_or_semi (false);
683 parse_error ("two GTY(()) options for field %s", name
);
687 f
= create_field_at (f
, dty
, name
, dopts
, &lexer_line
);
691 while (token () != '}' && token () != EOF_TOKEN
);
692 return nreverse_pairs (f
);
695 /* This is called type(), but what it parses (sort of) is what C calls
696 declaration-specifiers and specifier-qualifier-list:
700 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
701 | ENUM ID ( '{' ... '}' )?
703 Returns a partial type; under some conditions (notably
704 "struct foo GTY((...)) thing;") it may write an options
708 type (options_p
*optsp
, bool nested
)
716 return create_scalar_type (s
);
721 return resolve_typedef (s
, &lexer_line
);
727 /* GTY annotations follow attribute syntax
728 GTY_BEFORE_ID is for union/struct declarations
729 GTY_AFTER_ID is for variable declarations. */
736 bool is_union
= (token () == UNION
);
739 /* Top-level structures that are not explicitly tagged GTY(())
740 are treated as mere forward declarations. This is because
741 there are a lot of structures that we don't need to know
742 about, and some of those have weird macro stuff in them
743 that we can't handle. */
744 if (nested
|| token () == GTY_TOKEN
)
746 is_gty
= GTY_BEFORE_ID
;
747 opts
= gtymarker_opt ();
753 s
= xasprintf ("anonymous:%s:%d", lexer_line
.file
, lexer_line
.line
);
755 /* Unfortunately above GTY_TOKEN check does not capture the
756 typedef struct_type GTY case. */
757 if (token () == GTY_TOKEN
)
759 is_gty
= GTY_AFTER_ID
;
760 opts
= gtymarker_opt ();
769 if (is_gty
== GTY_AFTER_ID
)
770 parse_error ("GTY must be specified before identifier");
773 fields
= struct_field_seq ();
775 return new_structure (s
, is_union
, &lexer_line
, fields
, opts
);
778 else if (token () == '{')
779 consume_balanced ('{', '}');
782 return find_structure (s
, is_union
);
790 s
= xasprintf ("anonymous:%s:%d", lexer_line
.file
, lexer_line
.line
);
793 consume_balanced ('{', '}');
794 return create_scalar_type (s
);
797 parse_error ("expected a type specifier, have %s", print_cur_token ());
799 return create_scalar_type ("erroneous type");
803 /* Top level constructs. */
805 /* Dispatch declarations beginning with 'typedef'. */
815 gcc_assert (token () == TYPEDEF
);
818 ty
= type (&opts
, false);
822 parse_error ("GTY((...)) cannot be applied to a typedef");
825 dty
= declarator (ty
, &name
, &opts
);
827 parse_error ("GTY((...)) cannot be applied to a typedef");
829 /* Yet another place where we could have junk (notably attributes)
830 after the declarator. */
831 another
= consume_until_comma_or_semi (false);
833 do_typedef (name
, dty
, &lexer_line
);
838 /* Structure definition: type() does all the work. */
841 struct_or_union (void)
844 type (&dummy
, false);
845 /* There may be junk after the type: notably, we cannot currently
846 distinguish 'struct foo *function(prototype);' from 'struct foo;'
847 ... we could call declarator(), but it's a waste of time at
848 present. Instead, just eat whatever token is currently lookahead
849 and go back to lexical skipping mode. */
853 /* GC root declaration:
854 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
855 If the gtymarker is not present, we ignore the rest of the declaration. */
857 extern_or_static (void)
859 options_p opts
, opts2
, dopts
;
862 require2 (EXTERN
, STATIC
);
864 if (token () != GTY_TOKEN
)
871 ty
= type (&opts2
, true); /* if we get here, it's got a GTY(()) */
872 dty
= declarator (ty
, &name
, &dopts
);
874 if ((opts
&& dopts
) || (opts
&& opts2
) || (opts2
&& dopts
))
875 parse_error ("GTY((...)) specified more than once for %s", name
);
883 note_variable (name
, adjust_field_type (dty
, opts
), opts
, &lexer_line
);
888 /* Definition of a generic VEC structure:
890 'DEF_VEC_[IPO]' '(' id ')' ';'
892 Scalar VECs require slightly different treatment than otherwise -
893 that's handled in note_def_vec, we just pass it along.*/
897 bool is_scalar
= (token () == DEFVEC_I
);
900 require2 (DEFVEC_OP
, DEFVEC_I
);
902 type
= require2 (ID
, SCALAR
);
909 note_def_vec (type
, is_scalar
, &lexer_line
);
910 note_def_vec_alloc (type
, "none", &lexer_line
);
913 /* Definition of an allocation strategy for a VEC structure:
915 'DEF_VEC_ALLOC_[IPO]' '(' id ',' id ')' ';'
917 For purposes of gengtype, this just declares a wrapper structure. */
921 const char *type
, *astrat
;
923 require (DEFVEC_ALLOC
);
925 type
= require2 (ID
, SCALAR
);
927 astrat
= require (ID
);
931 if (!type
|| !astrat
)
934 note_def_vec_alloc (type
, astrat
, &lexer_line
);
937 /* Parse the file FNAME for GC-relevant declarations and definitions.
938 This is the only entry point to this file. */
940 parse_file (const char *fname
)
974 parse_error ("unexpected top level token, %s", print_cur_token ());
977 lexer_toplevel_done
= 1;