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/>. */
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: ",
139 get_input_file_name (lexer_line
.file
), lexer_line
.line
);
142 vfprintf (stderr
, msg
, ap
);
145 fputc ('\n', stderr
);
150 /* If the next token does not have code T, report a parse error; otherwise
151 return the token's value. */
156 const char *v
= advance ();
159 parse_error ("expected %s, have %s",
160 print_token (t
, 0), print_token (u
, v
));
166 /* If the next token does not have one of the codes T1 or T2, report a
167 parse error; otherwise return the token's value. */
169 require2 (int t1
, int t2
)
172 const char *v
= advance ();
173 if (u
!= t1
&& u
!= t2
)
175 parse_error ("expected %s or %s, have %s",
176 print_token (t1
, 0), print_token (t2
, 0),
183 /* Near-terminals. */
185 /* C-style string constant concatenation: STRING+
186 Bare STRING should appear nowhere else in this file. */
194 s1
= require (STRING
);
197 while (token () == STRING
)
203 buf
= XRESIZEVEC (char, CONST_CAST (char *, s1
), l1
+ l2
+ 1);
204 memcpy (buf
+ l1
, s2
, l2
+ 1);
205 XDELETE (CONST_CAST (char *, s2
));
211 /* typedef_name: either an ID, or VEC(x,y) which is translated to VEC_x_y.
212 Use only where VEC(x,y) is legitimate, i.e. in positions where a
213 typedef name may appear. */
217 if (token () == VEC_TOKEN
)
219 const char *c1
, *c2
, *r
;
222 c1
= require2 (ID
, SCALAR
);
226 r
= concat ("VEC_", c1
, "_", c2
, (char *) 0);
227 free (CONST_CAST (char *, c1
));
228 free (CONST_CAST (char *, c2
));
235 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
237 consume_balanced (int opener
, int closer
)
247 consume_balanced ('(', ')');
250 consume_balanced ('[', ']');
253 consume_balanced ('{', '}');
259 if (token () != closer
)
260 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
266 parse_error ("unexpected end of file within %c%c-delimited construct",
272 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
273 expressions, until we encounter a semicolon outside any such
274 delimiters; absorb that too. If IMMEDIATE is true, it is an error
275 if the semicolon is not the first token encountered. */
277 consume_until_semi (bool immediate
)
279 if (immediate
&& token () != ';')
292 consume_balanced ('(', ')');
295 consume_balanced ('[', ']');
298 consume_balanced ('{', '}');
304 parse_error ("unmatched '%c' while scanning for ';'", token ());
308 parse_error ("unexpected end of file while scanning for ';'");
313 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
314 expressions, until we encounter a comma or semicolon outside any
315 such delimiters; absorb that too. If IMMEDIATE is true, it is an
316 error if the comma or semicolon is not the first token encountered.
317 Returns true if the loop ended with a comma. */
319 consume_until_comma_or_semi (bool immediate
)
321 if (immediate
&& token () != ',' && token () != ';')
337 consume_balanced ('(', ')');
340 consume_balanced ('[', ']');
343 consume_balanced ('{', '}');
349 parse_error ("unmatched '%s' while scanning for ',' or ';'",
354 parse_error ("unexpected end of file while scanning for ',' or ';'");
360 /* GTY(()) option handling. */
361 static type_p
type (options_p
*optsp
, bool nested
);
363 /* Optional parenthesized string: ('(' string_seq ')')? */
365 str_optvalue_opt (options_p prev
)
367 const char *name
= advance ();
368 const char *value
= "";
372 value
= string_seq ();
375 return create_string_option (prev
, name
, value
);
378 /* absdecl: type '*'*
379 -- a vague approximation to what the C standard calls an abstract
380 declarator. The only kinds that are actually used are those that
381 are just a bare type and those that have trailing pointer-stars.
382 Further kinds should be implemented if and when they become
383 necessary. Used only within GTY(()) option values, therefore
384 further GTY(()) tags within the type are invalid. Note that the
385 return value has already been run through adjust_field_type. */
392 ty
= type (&opts
, true);
393 while (token () == '*')
395 ty
= create_pointer (ty
);
400 parse_error ("nested GTY(()) options are invalid");
402 return adjust_field_type (ty
, 0);
405 /* Type-option: '(' absdecl ')' */
407 type_optvalue (options_p prev
, const char *name
)
413 return create_type_option (prev
, name
, ty
);
416 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
418 nestedptr_optvalue (options_p prev
)
421 const char *from
, *to
;
428 from
= string_seq ();
431 return create_nested_ptr_option (prev
, ty
, to
, from
);
434 /* One GTY(()) option:
436 | PTR_ALIAS type_optvalue
437 | PARAM_IS type_optvalue
438 | NESTED_PTR nestedptr_optvalue
441 option (options_p prev
)
446 return str_optvalue_opt (prev
);
450 return type_optvalue (prev
, "ptr_alias");
453 return type_optvalue (prev
, advance ());
457 return nestedptr_optvalue (prev
);
460 parse_error ("expected an option keyword, have %s", print_cur_token ());
462 return create_string_option (prev
, "", "");
466 /* One comma-separated list of options. */
473 while (token () == ',')
481 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
485 options_p result
= 0;
490 result
= option_seq ();
496 /* Optional GTY marker. */
500 if (token () != GTY_TOKEN
)
505 /* Declarators. The logic here is largely lifted from c-parser.c.
506 Note that we do not have to process abstract declarators, which can
507 appear only in parameter type lists or casts (but see absdecl,
508 above). Also, type qualifiers are thrown out in gengtype-lex.l so
509 we don't have to do it. */
511 /* array_and_function_declarators_opt:
513 array_and_function_declarators_opt ARRAY
514 array_and_function_declarators_opt '(' ... ')'
516 where '...' indicates stuff we ignore except insofar as grouping
517 symbols ()[]{} must balance.
519 Subroutine of direct_declarator - do not use elsewhere. */
522 array_and_function_declarators_opt (type_p ty
)
524 if (token () == ARRAY
)
526 const char *array
= advance ();
527 return create_array (array_and_function_declarators_opt (ty
), array
);
529 else if (token () == '(')
531 /* We don't need exact types for functions. */
532 consume_balanced ('(', ')');
533 array_and_function_declarators_opt (ty
);
534 return create_scalar_type ("function type");
540 static type_p
inner_declarator (type_p
, const char **, options_p
*);
542 /* direct_declarator:
543 '(' inner_declarator ')'
544 gtymarker_opt ID array_and_function_declarators_opt
546 Subroutine of declarator, mutually recursive with inner_declarator;
547 do not use elsewhere. */
549 direct_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
551 /* The first token in a direct-declarator must be an ID, a
552 GTY marker, or an open parenthesis. */
556 *optsp
= gtymarker ();
559 *namep
= require (ID
);
564 ty
= inner_declarator (ty
, namep
, optsp
);
569 parse_error ("expected '(', 'GTY', or an identifier, have %s",
571 /* Do _not_ advance if what we have is a close squiggle brace, as
572 we will get much better error recovery that way. */
577 return array_and_function_declarators_opt (ty
);
580 /* The difference between inner_declarator and declarator is in the
581 handling of stars. Consider this declaration:
585 It declares a pointer to a function that takes no arguments and
586 returns a char*. To construct the correct type for this
587 declaration, the star outside the parentheses must be processed
588 _before_ the function type, the star inside the parentheses must
589 be processed _after_ the function type. To accomplish this,
590 declarator() creates pointers before recursing (it is actually
591 coded as a while loop), whereas inner_declarator() recurses before
592 creating pointers. */
598 Mutually recursive subroutine of direct_declarator; do not use
602 inner_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
608 inner
= inner_declarator (ty
, namep
, optsp
);
612 return create_pointer (ty
);
615 return direct_declarator (ty
, namep
, optsp
);
618 /* declarator: '*'+ direct_declarator
620 This is the sole public interface to this part of the grammar.
621 Arguments are the type known so far, a pointer to where the name
622 may be stored, and a pointer to where GTY options may be stored.
623 Returns the final type. */
626 declarator (type_p ty
, const char **namep
, options_p
*optsp
)
630 while (token () == '*')
633 ty
= create_pointer (ty
);
635 return direct_declarator (ty
, namep
, optsp
);
638 /* Types and declarations. */
640 /* Structure field(s) declaration:
643 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
646 Knows that such declarations must end with a close brace (or,
647 erroneously, at EOF).
650 struct_field_seq (void)
654 options_p opts
, dopts
;
660 ty
= type (&opts
, true);
661 /* Another piece of the IFCVT_EXTRA_FIELDS special case, see type(). */
662 if (!ty
&& token () == '}')
665 if (!ty
|| token () == ':')
667 consume_until_semi (false);
673 dty
= declarator (ty
, &name
, &dopts
);
674 /* There could be any number of weird things after the declarator,
675 notably bitfield declarations and __attribute__s. If this
676 function returns true, the last thing was a comma, so we have
677 more than one declarator paired with the current type. */
678 another
= consume_until_comma_or_semi (false);
684 parse_error ("two GTY(()) options for field %s", name
);
688 f
= create_field_at (f
, dty
, name
, dopts
, &lexer_line
);
692 while (token () != '}' && token () != EOF_TOKEN
);
693 return nreverse_pairs (f
);
696 /* This is called type(), but what it parses (sort of) is what C calls
697 declaration-specifiers and specifier-qualifier-list:
701 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
702 | ENUM ID ( '{' ... '}' )?
704 Returns a partial type; under some conditions (notably
705 "struct foo GTY((...)) thing;") it may write an options
709 type (options_p
*optsp
, bool nested
)
717 return create_scalar_type (s
);
722 return resolve_typedef (s
, &lexer_line
);
728 /* GTY annotations follow attribute syntax
729 GTY_BEFORE_ID is for union/struct declarations
730 GTY_AFTER_ID is for variable declarations. */
737 bool is_union
= (token () == UNION
);
740 /* Top-level structures that are not explicitly tagged GTY(())
741 are treated as mere forward declarations. This is because
742 there are a lot of structures that we don't need to know
743 about, and some of those have weird macro stuff in them
744 that we can't handle. */
745 if (nested
|| token () == GTY_TOKEN
)
747 is_gty
= GTY_BEFORE_ID
;
748 opts
= gtymarker_opt ();
754 s
= xasprintf ("anonymous:%s:%d",
755 get_input_file_name (lexer_line
.file
),
758 /* Unfortunately above GTY_TOKEN check does not capture the
759 typedef struct_type GTY case. */
760 if (token () == GTY_TOKEN
)
762 is_gty
= GTY_AFTER_ID
;
763 opts
= gtymarker_opt ();
772 if (is_gty
== GTY_AFTER_ID
)
773 parse_error ("GTY must be specified before identifier");
776 fields
= struct_field_seq ();
778 return new_structure (s
, is_union
, &lexer_line
, fields
, opts
);
781 else if (token () == '{')
782 consume_balanced ('{', '}');
785 return find_structure (s
, is_union
);
793 s
= xasprintf ("anonymous:%s:%d",
794 get_input_file_name (lexer_line
.file
),
798 consume_balanced ('{', '}');
799 return create_scalar_type (s
);
802 parse_error ("expected a type specifier, have %s", print_cur_token ());
804 return create_scalar_type ("erroneous type");
808 /* Top level constructs. */
810 /* Dispatch declarations beginning with 'typedef'. */
820 gcc_assert (token () == TYPEDEF
);
823 ty
= type (&opts
, false);
827 parse_error ("GTY((...)) cannot be applied to a typedef");
830 dty
= declarator (ty
, &name
, &opts
);
832 parse_error ("GTY((...)) cannot be applied to a typedef");
834 /* Yet another place where we could have junk (notably attributes)
835 after the declarator. */
836 another
= consume_until_comma_or_semi (false);
838 do_typedef (name
, dty
, &lexer_line
);
843 /* Structure definition: type() does all the work. */
846 struct_or_union (void)
849 type (&dummy
, false);
850 /* There may be junk after the type: notably, we cannot currently
851 distinguish 'struct foo *function(prototype);' from 'struct foo;'
852 ... we could call declarator(), but it's a waste of time at
853 present. Instead, just eat whatever token is currently lookahead
854 and go back to lexical skipping mode. */
858 /* GC root declaration:
859 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
860 If the gtymarker is not present, we ignore the rest of the declaration. */
862 extern_or_static (void)
864 options_p opts
, opts2
, dopts
;
867 require2 (EXTERN
, STATIC
);
869 if (token () != GTY_TOKEN
)
876 ty
= type (&opts2
, true); /* if we get here, it's got a GTY(()) */
877 dty
= declarator (ty
, &name
, &dopts
);
879 if ((opts
&& dopts
) || (opts
&& opts2
) || (opts2
&& dopts
))
880 parse_error ("GTY((...)) specified more than once for %s", name
);
888 note_variable (name
, adjust_field_type (dty
, opts
), opts
, &lexer_line
);
893 /* Definition of a generic VEC structure:
895 'DEF_VEC_[IPO]' '(' id ')' ';'
897 Scalar VECs require slightly different treatment than otherwise -
898 that's handled in note_def_vec, we just pass it along.*/
902 bool is_scalar
= (token () == DEFVEC_I
);
905 require2 (DEFVEC_OP
, DEFVEC_I
);
907 type
= require2 (ID
, SCALAR
);
914 note_def_vec (type
, is_scalar
, &lexer_line
);
915 note_def_vec_alloc (type
, "none", &lexer_line
);
918 /* Definition of an allocation strategy for a VEC structure:
920 'DEF_VEC_ALLOC_[IPO]' '(' id ',' id ')' ';'
922 For purposes of gengtype, this just declares a wrapper structure. */
926 const char *type
, *astrat
;
928 require (DEFVEC_ALLOC
);
930 type
= require2 (ID
, SCALAR
);
932 astrat
= require (ID
);
936 if (!type
|| !astrat
)
939 note_def_vec_alloc (type
, astrat
, &lexer_line
);
942 /* Parse the file FNAME for GC-relevant declarations and definitions.
943 This is the only entry point to this file. */
945 parse_file (const char *fname
)
979 parse_error ("unexpected top level token, %s", print_cur_token ());
982 lexer_toplevel_done
= 1;