1 /* Process source files and output type information.
2 Copyright (C) 2006, 2007, 2010 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/>. */
28 /* This is a simple recursive-descent parser which understands a subset of
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. */
36 /* Simple one-token lookahead mechanism. */
44 static struct token T
;
46 /* Retrieve the code of the current token; if there is no current token,
47 get the next one from the lexer. */
53 T
.code
= yylex (&T
.value
);
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 *
70 /* This array is indexed by the token code minus CHAR_TOKEN_OFFSET. */
71 static const char *const token_names
[] = {
82 "DEF_VEC_ALLOC_[IOP]",
86 "a param<N>_is option",
91 "a character constant",
92 "an array declarator",
95 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
96 static const char *const token_value_format
[] = {
106 /* Produce a printable representation for a token defined by CODE and
107 VALUE. This sometimes returns pointers into malloc memory and
108 sometimes not, therefore it is unsafe to free the pointer it
109 returns, so that memory is leaked. This does not matter, as this
110 function is only used for diagnostics, and in a successful run of
111 the program there will be none. */
113 print_token (int code
, const char *value
)
115 if (code
< CHAR_TOKEN_OFFSET
)
116 return xasprintf ("'%c'", code
);
117 else if (code
< FIRST_TOKEN_WITH_VALUE
)
118 return xasprintf ("'%s'", token_names
[code
- CHAR_TOKEN_OFFSET
]);
120 return token_names
[code
- CHAR_TOKEN_OFFSET
]; /* don't quote these */
122 return xasprintf (token_value_format
[code
- FIRST_TOKEN_WITH_VALUE
],
126 /* Convenience wrapper around print_token which produces the printable
127 representation of the current token. */
128 static inline const char *
129 print_cur_token (void)
131 return print_token (T
.code
, T
.value
);
134 /* Report a parse error on the current line, with diagnostic MSG.
135 Behaves as standard printf with respect to additional arguments and
137 static void ATTRIBUTE_PRINTF_1
138 parse_error (const char *msg
, ...)
142 fprintf (stderr
, "%s:%d: parse error: ",
143 get_input_file_name (lexer_line
.file
), lexer_line
.line
);
146 vfprintf (stderr
, msg
, ap
);
149 fputc ('\n', stderr
);
154 /* If the next token does not have code T, report a parse error; otherwise
155 return the token's value. */
160 const char *v
= advance ();
163 parse_error ("expected %s, have %s",
164 print_token (t
, 0), print_token (u
, v
));
170 /* If the next token does not have one of the codes T1 or T2, report a
171 parse error; otherwise return the token's value. */
173 require2 (int t1
, int t2
)
176 const char *v
= advance ();
177 if (u
!= t1
&& u
!= t2
)
179 parse_error ("expected %s or %s, have %s",
180 print_token (t1
, 0), print_token (t2
, 0),
187 /* Near-terminals. */
189 /* C-style string constant concatenation: STRING+
190 Bare STRING should appear nowhere else in this file. */
198 s1
= require (STRING
);
201 while (token () == STRING
)
207 buf
= XRESIZEVEC (char, CONST_CAST (char *, s1
), l1
+ l2
+ 1);
208 memcpy (buf
+ l1
, s2
, l2
+ 1);
209 XDELETE (CONST_CAST (char *, s2
));
215 /* typedef_name: either an ID, or VEC(x,y) which is translated to VEC_x_y.
216 Use only where VEC(x,y) is legitimate, i.e. in positions where a
217 typedef name may appear. */
221 if (token () == VEC_TOKEN
)
223 const char *c1
, *c2
, *r
;
226 c1
= require2 (ID
, SCALAR
);
230 r
= concat ("VEC_", c1
, "_", c2
, (char *) 0);
231 free (CONST_CAST (char *, c1
));
232 free (CONST_CAST (char *, c2
));
239 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
241 consume_balanced (int opener
, int closer
)
251 consume_balanced ('(', ')');
254 consume_balanced ('[', ']');
257 consume_balanced ('{', '}');
263 if (token () != closer
)
264 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
270 parse_error ("unexpected end of file within %c%c-delimited construct",
276 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
277 expressions, until we encounter a semicolon outside any such
278 delimiters; absorb that too. If IMMEDIATE is true, it is an error
279 if the semicolon is not the first token encountered. */
281 consume_until_semi (bool immediate
)
283 if (immediate
&& token () != ';')
296 consume_balanced ('(', ')');
299 consume_balanced ('[', ']');
302 consume_balanced ('{', '}');
308 parse_error ("unmatched '%c' while scanning for ';'", token ());
312 parse_error ("unexpected end of file while scanning for ';'");
317 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
318 expressions, until we encounter a comma or semicolon outside any
319 such delimiters; absorb that too. If IMMEDIATE is true, it is an
320 error if the comma or semicolon is not the first token encountered.
321 Returns true if the loop ended with a comma. */
323 consume_until_comma_or_semi (bool immediate
)
325 if (immediate
&& token () != ',' && token () != ';')
341 consume_balanced ('(', ')');
344 consume_balanced ('[', ']');
347 consume_balanced ('{', '}');
353 parse_error ("unmatched '%s' while scanning for ',' or ';'",
358 parse_error ("unexpected end of file while scanning for ',' or ';'");
364 /* GTY(()) option handling. */
365 static type_p
type (options_p
*optsp
, bool nested
);
367 /* Optional parenthesized string: ('(' string_seq ')')? */
369 str_optvalue_opt (options_p prev
)
371 const char *name
= advance ();
372 const char *value
= "";
376 value
= string_seq ();
379 return create_string_option (prev
, name
, value
);
382 /* absdecl: type '*'*
383 -- a vague approximation to what the C standard calls an abstract
384 declarator. The only kinds that are actually used are those that
385 are just a bare type and those that have trailing pointer-stars.
386 Further kinds should be implemented if and when they become
387 necessary. Used only within GTY(()) option values, therefore
388 further GTY(()) tags within the type are invalid. Note that the
389 return value has already been run through adjust_field_type. */
396 ty
= type (&opts
, true);
397 while (token () == '*')
399 ty
= create_pointer (ty
);
404 parse_error ("nested GTY(()) options are invalid");
406 return adjust_field_type (ty
, 0);
409 /* Type-option: '(' absdecl ')' */
411 type_optvalue (options_p prev
, const char *name
)
417 return create_type_option (prev
, name
, ty
);
420 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
422 nestedptr_optvalue (options_p prev
)
425 const char *from
, *to
;
432 from
= string_seq ();
435 return create_nested_ptr_option (prev
, ty
, to
, from
);
438 /* One GTY(()) option:
440 | PTR_ALIAS type_optvalue
441 | PARAM_IS type_optvalue
442 | NESTED_PTR nestedptr_optvalue
445 option (options_p prev
)
450 return str_optvalue_opt (prev
);
454 return type_optvalue (prev
, "ptr_alias");
457 return type_optvalue (prev
, advance ());
461 return nestedptr_optvalue (prev
);
464 parse_error ("expected an option keyword, have %s", print_cur_token ());
466 return create_string_option (prev
, "", "");
470 /* One comma-separated list of options. */
477 while (token () == ',')
485 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
489 options_p result
= 0;
494 result
= option_seq ();
500 /* Optional GTY marker. */
504 if (token () != GTY_TOKEN
)
509 /* Declarators. The logic here is largely lifted from c-parser.c.
510 Note that we do not have to process abstract declarators, which can
511 appear only in parameter type lists or casts (but see absdecl,
512 above). Also, type qualifiers are thrown out in gengtype-lex.l so
513 we don't have to do it. */
515 /* array_and_function_declarators_opt:
517 array_and_function_declarators_opt ARRAY
518 array_and_function_declarators_opt '(' ... ')'
520 where '...' indicates stuff we ignore except insofar as grouping
521 symbols ()[]{} must balance.
523 Subroutine of direct_declarator - do not use elsewhere. */
526 array_and_function_declarators_opt (type_p ty
)
528 if (token () == ARRAY
)
530 const char *array
= advance ();
531 return create_array (array_and_function_declarators_opt (ty
), array
);
533 else if (token () == '(')
535 /* We don't need exact types for functions. */
536 consume_balanced ('(', ')');
537 array_and_function_declarators_opt (ty
);
538 return create_scalar_type ("function type");
544 static type_p
inner_declarator (type_p
, const char **, options_p
*);
546 /* direct_declarator:
547 '(' inner_declarator ')'
548 gtymarker_opt ID array_and_function_declarators_opt
550 Subroutine of declarator, mutually recursive with inner_declarator;
551 do not use elsewhere. */
553 direct_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
555 /* The first token in a direct-declarator must be an ID, a
556 GTY marker, or an open parenthesis. */
560 *optsp
= gtymarker ();
563 *namep
= require (ID
);
568 ty
= inner_declarator (ty
, namep
, optsp
);
573 parse_error ("expected '(', 'GTY', or an identifier, have %s",
575 /* Do _not_ advance if what we have is a close squiggle brace, as
576 we will get much better error recovery that way. */
581 return array_and_function_declarators_opt (ty
);
584 /* The difference between inner_declarator and declarator is in the
585 handling of stars. Consider this declaration:
589 It declares a pointer to a function that takes no arguments and
590 returns a char*. To construct the correct type for this
591 declaration, the star outside the parentheses must be processed
592 _before_ the function type, the star inside the parentheses must
593 be processed _after_ the function type. To accomplish this,
594 declarator() creates pointers before recursing (it is actually
595 coded as a while loop), whereas inner_declarator() recurses before
596 creating pointers. */
602 Mutually recursive subroutine of direct_declarator; do not use
606 inner_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
612 inner
= inner_declarator (ty
, namep
, optsp
);
616 return create_pointer (ty
);
619 return direct_declarator (ty
, namep
, optsp
);
622 /* declarator: '*'+ direct_declarator
624 This is the sole public interface to this part of the grammar.
625 Arguments are the type known so far, a pointer to where the name
626 may be stored, and a pointer to where GTY options may be stored.
627 Returns the final type. */
630 declarator (type_p ty
, const char **namep
, options_p
*optsp
)
634 while (token () == '*')
637 ty
= create_pointer (ty
);
639 return direct_declarator (ty
, namep
, optsp
);
642 /* Types and declarations. */
644 /* Structure field(s) declaration:
647 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
650 Knows that such declarations must end with a close brace (or,
651 erroneously, at EOF).
654 struct_field_seq (void)
658 options_p opts
, dopts
;
664 ty
= type (&opts
, true);
665 /* Another piece of the IFCVT_EXTRA_FIELDS special case, see type(). */
666 if (!ty
&& token () == '}')
669 if (!ty
|| token () == ':')
671 consume_until_semi (false);
677 dty
= declarator (ty
, &name
, &dopts
);
678 /* There could be any number of weird things after the declarator,
679 notably bitfield declarations and __attribute__s. If this
680 function returns true, the last thing was a comma, so we have
681 more than one declarator paired with the current type. */
682 another
= consume_until_comma_or_semi (false);
688 parse_error ("two GTY(()) options for field %s", name
);
692 f
= create_field_at (f
, dty
, name
, dopts
, &lexer_line
);
696 while (token () != '}' && token () != EOF_TOKEN
);
697 return nreverse_pairs (f
);
700 /* This is called type(), but what it parses (sort of) is what C calls
701 declaration-specifiers and specifier-qualifier-list:
705 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
706 | ENUM ID ( '{' ... '}' )?
708 Returns a partial type; under some conditions (notably
709 "struct foo GTY((...)) thing;") it may write an options
713 type (options_p
*optsp
, bool nested
)
721 return create_scalar_type (s
);
726 return resolve_typedef (s
, &lexer_line
);
732 /* GTY annotations follow attribute syntax
733 GTY_BEFORE_ID is for union/struct declarations
734 GTY_AFTER_ID is for variable declarations. */
741 bool is_union
= (token () == UNION
);
744 /* Top-level structures that are not explicitly tagged GTY(())
745 are treated as mere forward declarations. This is because
746 there are a lot of structures that we don't need to know
747 about, and some of those have weird macro stuff in them
748 that we can't handle. */
749 if (nested
|| token () == GTY_TOKEN
)
751 is_gty
= GTY_BEFORE_ID
;
752 opts
= gtymarker_opt ();
758 s
= xasprintf ("anonymous:%s:%d",
759 get_input_file_name (lexer_line
.file
),
762 /* Unfortunately above GTY_TOKEN check does not capture the
763 typedef struct_type GTY case. */
764 if (token () == GTY_TOKEN
)
766 is_gty
= GTY_AFTER_ID
;
767 opts
= gtymarker_opt ();
776 if (is_gty
== GTY_AFTER_ID
)
777 parse_error ("GTY must be specified before identifier");
780 fields
= struct_field_seq ();
782 return new_structure (s
, is_union
, &lexer_line
, fields
, opts
);
785 else if (token () == '{')
786 consume_balanced ('{', '}');
789 return find_structure (s
, is_union
);
797 s
= xasprintf ("anonymous:%s:%d",
798 get_input_file_name (lexer_line
.file
),
802 consume_balanced ('{', '}');
803 return create_scalar_type (s
);
806 parse_error ("expected a type specifier, have %s", print_cur_token ());
808 return create_scalar_type ("erroneous type");
812 /* Top level constructs. */
814 /* Dispatch declarations beginning with 'typedef'. */
824 gcc_assert (token () == TYPEDEF
);
827 ty
= type (&opts
, false);
831 parse_error ("GTY((...)) cannot be applied to a typedef");
834 dty
= declarator (ty
, &name
, &opts
);
836 parse_error ("GTY((...)) cannot be applied to a typedef");
838 /* Yet another place where we could have junk (notably attributes)
839 after the declarator. */
840 another
= consume_until_comma_or_semi (false);
842 do_typedef (name
, dty
, &lexer_line
);
847 /* Structure definition: type() does all the work. */
850 struct_or_union (void)
853 type (&dummy
, false);
854 /* There may be junk after the type: notably, we cannot currently
855 distinguish 'struct foo *function(prototype);' from 'struct foo;'
856 ... we could call declarator(), but it's a waste of time at
857 present. Instead, just eat whatever token is currently lookahead
858 and go back to lexical skipping mode. */
862 /* GC root declaration:
863 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
864 If the gtymarker is not present, we ignore the rest of the declaration. */
866 extern_or_static (void)
868 options_p opts
, opts2
, dopts
;
871 require2 (EXTERN
, STATIC
);
873 if (token () != GTY_TOKEN
)
880 ty
= type (&opts2
, true); /* if we get here, it's got a GTY(()) */
881 dty
= declarator (ty
, &name
, &dopts
);
883 if ((opts
&& dopts
) || (opts
&& opts2
) || (opts2
&& dopts
))
884 parse_error ("GTY((...)) specified more than once for %s", name
);
892 note_variable (name
, adjust_field_type (dty
, opts
), opts
, &lexer_line
);
897 /* Definition of a generic VEC structure:
899 'DEF_VEC_[IPO]' '(' id ')' ';'
901 Scalar VECs require slightly different treatment than otherwise -
902 that's handled in note_def_vec, we just pass it along.*/
906 bool is_scalar
= (token () == DEFVEC_I
);
909 require2 (DEFVEC_OP
, DEFVEC_I
);
911 type
= require2 (ID
, SCALAR
);
918 note_def_vec (type
, is_scalar
, &lexer_line
);
919 note_def_vec_alloc (type
, "none", &lexer_line
);
922 /* Definition of an allocation strategy for a VEC structure:
924 'DEF_VEC_ALLOC_[IPO]' '(' id ',' id ')' ';'
926 For purposes of gengtype, this just declares a wrapper structure. */
930 const char *type
, *astrat
;
932 require (DEFVEC_ALLOC
);
934 type
= require2 (ID
, SCALAR
);
936 astrat
= require (ID
);
940 if (!type
|| !astrat
)
943 note_def_vec_alloc (type
, astrat
, &lexer_line
);
946 /* Parse the file FNAME for GC-relevant declarations and definitions.
947 This is the only entry point to this file. */
949 parse_file (const char *fname
)
983 parse_error ("unexpected top level token, %s", print_cur_token ());
986 lexer_toplevel_done
= 1;