1 /* Process source files and output type information.
2 Copyright (C) 2006, 2007, 2010, 2012 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
[] = {
83 "a param<N>_is option",
88 "a character constant",
89 "an array declarator",
92 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
93 static const char *const token_value_format
[] = {
103 /* Produce a printable representation for a token defined by CODE and
104 VALUE. This sometimes returns pointers into malloc memory and
105 sometimes not, therefore it is unsafe to free the pointer it
106 returns, so that memory is leaked. This does not matter, as this
107 function is only used for diagnostics, and in a successful run of
108 the program there will be none. */
110 print_token (int code
, const char *value
)
112 if (code
< CHAR_TOKEN_OFFSET
)
113 return xasprintf ("'%c'", code
);
114 else if (code
< FIRST_TOKEN_WITH_VALUE
)
115 return xasprintf ("'%s'", token_names
[code
- CHAR_TOKEN_OFFSET
]);
117 return token_names
[code
- CHAR_TOKEN_OFFSET
]; /* don't quote these */
119 return xasprintf (token_value_format
[code
- FIRST_TOKEN_WITH_VALUE
],
123 /* Convenience wrapper around print_token which produces the printable
124 representation of the current token. */
125 static inline const char *
126 print_cur_token (void)
128 return print_token (T
.code
, T
.value
);
131 /* Report a parse error on the current line, with diagnostic MSG.
132 Behaves as standard printf with respect to additional arguments and
134 static void ATTRIBUTE_PRINTF_1
135 parse_error (const char *msg
, ...)
139 fprintf (stderr
, "%s:%d: parse error: ",
140 get_input_file_name (lexer_line
.file
), lexer_line
.line
);
143 vfprintf (stderr
, msg
, ap
);
146 fputc ('\n', stderr
);
151 /* If the next token does not have code T, report a parse error; otherwise
152 return the token's value. */
157 const char *v
= advance ();
160 parse_error ("expected %s, have %s",
161 print_token (t
, 0), print_token (u
, v
));
167 /* If the next token does not have one of the codes T1 or T2, report a
168 parse error; otherwise return the token's value. */
170 require2 (int t1
, int t2
)
173 const char *v
= advance ();
174 if (u
!= t1
&& u
!= t2
)
176 parse_error ("expected %s or %s, have %s",
177 print_token (t1
, 0), print_token (t2
, 0),
184 /* Near-terminals. */
186 /* C-style string constant concatenation: STRING+
187 Bare STRING should appear nowhere else in this file. */
195 s1
= require (STRING
);
198 while (token () == STRING
)
204 buf
= XRESIZEVEC (char, CONST_CAST (char *, s1
), l1
+ l2
+ 1);
205 memcpy (buf
+ l1
, s2
, l2
+ 1);
206 XDELETE (CONST_CAST (char *, s2
));
213 /* The caller has detected a template declaration that starts
214 with TMPL_NAME. Parse up to the closing '>'. This recognizes
215 simple template declarations of the form ID<ID1,ID2,...,IDn>.
216 It does not try to parse anything more sophisticated than that.
218 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
221 require_template_declaration (const char *tmpl_name
)
225 /* Recognize the opening '<'. */
227 str
= concat (tmpl_name
, "<", (char *) 0);
229 /* Read the comma-separated list of identifiers. */
230 while (token () != '>')
232 const char *id
= require2 (ID
, ',');
235 str
= concat (str
, id
, (char *) 0);
238 /* Recognize the closing '>'. */
240 str
= concat (str
, ">", (char *) 0);
246 /* typedef_name: either an ID, or VEC(x,y), or a template type
247 specification of the form ID<t1,t2,...,tn>.
249 FIXME cxx-conversion. VEC(x,y) is currently translated to the
250 template 'vec_t<x>'. This is to support the transition to C++ and
251 avoid re-writing all the 'VEC(x,y)' declarations in the code. This
252 needs to be fixed when the branch is merged into trunk. */
257 if (token () == VEC_TOKEN
)
262 c1
= require2 (ID
, SCALAR
);
266 r
= concat ("vec_t<", c1
, ">", (char *) 0);
267 free (CONST_CAST (char *, c1
));
271 const char *id
= require (ID
);
273 return require_template_declaration (id
);
278 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
280 consume_balanced (int opener
, int closer
)
290 consume_balanced ('(', ')');
293 consume_balanced ('[', ']');
296 consume_balanced ('{', '}');
302 if (token () != closer
)
303 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
309 parse_error ("unexpected end of file within %c%c-delimited construct",
315 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
316 expressions, until we encounter a semicolon outside any such
317 delimiters; absorb that too. If IMMEDIATE is true, it is an error
318 if the semicolon is not the first token encountered. */
320 consume_until_semi (bool immediate
)
322 if (immediate
&& token () != ';')
335 consume_balanced ('(', ')');
338 consume_balanced ('[', ']');
341 consume_balanced ('{', '}');
347 parse_error ("unmatched '%c' while scanning for ';'", token ());
351 parse_error ("unexpected end of file while scanning for ';'");
356 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
357 expressions, until we encounter a comma or semicolon outside any
358 such delimiters; absorb that too. If IMMEDIATE is true, it is an
359 error if the comma or semicolon is not the first token encountered.
360 Returns true if the loop ended with a comma. */
362 consume_until_comma_or_semi (bool immediate
)
364 if (immediate
&& token () != ',' && token () != ';')
380 consume_balanced ('(', ')');
383 consume_balanced ('[', ']');
386 consume_balanced ('{', '}');
392 parse_error ("unmatched '%s' while scanning for ',' or ';'",
397 parse_error ("unexpected end of file while scanning for ',' or ';'");
403 /* GTY(()) option handling. */
404 static type_p
type (options_p
*optsp
, bool nested
);
406 /* Optional parenthesized string: ('(' string_seq ')')? */
408 str_optvalue_opt (options_p prev
)
410 const char *name
= advance ();
411 const char *value
= "";
415 value
= string_seq ();
418 return create_string_option (prev
, name
, value
);
421 /* absdecl: type '*'*
422 -- a vague approximation to what the C standard calls an abstract
423 declarator. The only kinds that are actually used are those that
424 are just a bare type and those that have trailing pointer-stars.
425 Further kinds should be implemented if and when they become
426 necessary. Used only within GTY(()) option values, therefore
427 further GTY(()) tags within the type are invalid. Note that the
428 return value has already been run through adjust_field_type. */
435 ty
= type (&opts
, true);
436 while (token () == '*')
438 ty
= create_pointer (ty
);
443 parse_error ("nested GTY(()) options are invalid");
445 return adjust_field_type (ty
, 0);
448 /* Type-option: '(' absdecl ')' */
450 type_optvalue (options_p prev
, const char *name
)
456 return create_type_option (prev
, name
, ty
);
459 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
461 nestedptr_optvalue (options_p prev
)
464 const char *from
, *to
;
471 from
= string_seq ();
474 return create_nested_ptr_option (prev
, ty
, to
, from
);
477 /* One GTY(()) option:
479 | PTR_ALIAS type_optvalue
480 | PARAM_IS type_optvalue
481 | NESTED_PTR nestedptr_optvalue
484 option (options_p prev
)
489 return str_optvalue_opt (prev
);
493 return type_optvalue (prev
, "ptr_alias");
496 return type_optvalue (prev
, advance ());
500 return nestedptr_optvalue (prev
);
504 return create_string_option (prev
, "user", "");
507 parse_error ("expected an option keyword, have %s", print_cur_token ());
509 return create_string_option (prev
, "", "");
513 /* One comma-separated list of options. */
520 while (token () == ',')
528 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
532 options_p result
= 0;
537 result
= option_seq ();
543 /* Optional GTY marker. */
547 if (token () != GTY_TOKEN
)
552 /* Declarators. The logic here is largely lifted from c-parser.c.
553 Note that we do not have to process abstract declarators, which can
554 appear only in parameter type lists or casts (but see absdecl,
555 above). Also, type qualifiers are thrown out in gengtype-lex.l so
556 we don't have to do it. */
558 /* array_and_function_declarators_opt:
560 array_and_function_declarators_opt ARRAY
561 array_and_function_declarators_opt '(' ... ')'
563 where '...' indicates stuff we ignore except insofar as grouping
564 symbols ()[]{} must balance.
566 Subroutine of direct_declarator - do not use elsewhere. */
569 array_and_function_declarators_opt (type_p ty
)
571 if (token () == ARRAY
)
573 const char *array
= advance ();
574 return create_array (array_and_function_declarators_opt (ty
), array
);
576 else if (token () == '(')
578 /* We don't need exact types for functions. */
579 consume_balanced ('(', ')');
580 array_and_function_declarators_opt (ty
);
581 return create_scalar_type ("function type");
587 static type_p
inner_declarator (type_p
, const char **, options_p
*);
589 /* direct_declarator:
590 '(' inner_declarator ')'
591 gtymarker_opt ID array_and_function_declarators_opt
593 Subroutine of declarator, mutually recursive with inner_declarator;
594 do not use elsewhere. */
596 direct_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
598 /* The first token in a direct-declarator must be an ID, a
599 GTY marker, or an open parenthesis. */
603 *optsp
= gtymarker ();
606 *namep
= require (ID
);
611 ty
= inner_declarator (ty
, namep
, optsp
);
616 parse_error ("expected '(', 'GTY', or an identifier, have %s",
618 /* Do _not_ advance if what we have is a close squiggle brace, as
619 we will get much better error recovery that way. */
624 return array_and_function_declarators_opt (ty
);
627 /* The difference between inner_declarator and declarator is in the
628 handling of stars. Consider this declaration:
632 It declares a pointer to a function that takes no arguments and
633 returns a char*. To construct the correct type for this
634 declaration, the star outside the parentheses must be processed
635 _before_ the function type, the star inside the parentheses must
636 be processed _after_ the function type. To accomplish this,
637 declarator() creates pointers before recursing (it is actually
638 coded as a while loop), whereas inner_declarator() recurses before
639 creating pointers. */
645 Mutually recursive subroutine of direct_declarator; do not use
649 inner_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
655 inner
= inner_declarator (ty
, namep
, optsp
);
659 return create_pointer (ty
);
662 return direct_declarator (ty
, namep
, optsp
);
665 /* declarator: '*'+ direct_declarator
667 This is the sole public interface to this part of the grammar.
668 Arguments are the type known so far, a pointer to where the name
669 may be stored, and a pointer to where GTY options may be stored.
670 Returns the final type. */
673 declarator (type_p ty
, const char **namep
, options_p
*optsp
)
677 while (token () == '*')
680 ty
= create_pointer (ty
);
682 return direct_declarator (ty
, namep
, optsp
);
685 /* Types and declarations. */
687 /* Structure field(s) declaration:
690 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
693 Knows that such declarations must end with a close brace (or,
694 erroneously, at EOF).
697 struct_field_seq (void)
701 options_p opts
, dopts
;
707 ty
= type (&opts
, true);
709 if (!ty
|| token () == ':')
711 consume_until_semi (false);
717 dty
= declarator (ty
, &name
, &dopts
);
718 /* There could be any number of weird things after the declarator,
719 notably bitfield declarations and __attribute__s. If this
720 function returns true, the last thing was a comma, so we have
721 more than one declarator paired with the current type. */
722 another
= consume_until_comma_or_semi (false);
728 parse_error ("two GTY(()) options for field %s", name
);
732 f
= create_field_at (f
, dty
, name
, dopts
, &lexer_line
);
736 while (token () != '}' && token () != EOF_TOKEN
);
737 return nreverse_pairs (f
);
740 /* Return true if OPTS contain the option named STR. */
743 opts_have (options_p opts
, const char *str
)
745 for (options_p opt
= opts
; opt
; opt
= opt
->next
)
746 if (strcmp (opt
->name
, str
) == 0)
752 /* This is called type(), but what it parses (sort of) is what C calls
753 declaration-specifiers and specifier-qualifier-list:
757 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
758 | ENUM ID ( '{' ... '}' )?
760 Returns a partial type; under some conditions (notably
761 "struct foo GTY((...)) thing;") it may write an options
765 type (options_p
*optsp
, bool nested
)
773 return create_scalar_type (s
);
778 return resolve_typedef (s
, &lexer_line
);
784 /* GTY annotations follow attribute syntax
785 GTY_BEFORE_ID is for union/struct declarations
786 GTY_AFTER_ID is for variable declarations. */
793 enum typekind kind
= (token () == UNION
) ? TYPE_UNION
: TYPE_STRUCT
;
796 /* Top-level structures that are not explicitly tagged GTY(())
797 are treated as mere forward declarations. This is because
798 there are a lot of structures that we don't need to know
799 about, and some of those have weird macro stuff in them
800 that we can't handle. */
801 if (nested
|| token () == GTY_TOKEN
)
803 is_gty
= GTY_BEFORE_ID
;
804 opts
= gtymarker_opt ();
810 s
= xasprintf ("anonymous:%s:%d",
811 get_input_file_name (lexer_line
.file
),
814 /* Unfortunately above GTY_TOKEN check does not capture the
815 typedef struct_type GTY case. */
816 if (token () == GTY_TOKEN
)
818 is_gty
= GTY_AFTER_ID
;
819 opts
= gtymarker_opt ();
824 bool is_user_gty
= opts_have (opts
, "user");
829 if (is_gty
== GTY_AFTER_ID
)
830 parse_error ("GTY must be specified before identifier");
835 fields
= struct_field_seq ();
840 /* Do not look inside user defined structures. */
842 kind
= TYPE_USER_STRUCT
;
843 consume_balanced ('{', '}');
846 return new_structure (s
, kind
, &lexer_line
, fields
, opts
);
849 else if (token () == '{')
850 consume_balanced ('{', '}');
853 return find_structure (s
, kind
);
861 s
= xasprintf ("anonymous:%s:%d",
862 get_input_file_name (lexer_line
.file
),
866 consume_balanced ('{', '}');
867 return create_scalar_type (s
);
870 parse_error ("expected a type specifier, have %s", print_cur_token ());
872 return create_scalar_type ("erroneous type");
876 /* Top level constructs. */
878 /* Dispatch declarations beginning with 'typedef'. */
888 gcc_assert (token () == TYPEDEF
);
891 ty
= type (&opts
, false);
895 parse_error ("GTY((...)) cannot be applied to a typedef");
898 dty
= declarator (ty
, &name
, &opts
);
900 parse_error ("GTY((...)) cannot be applied to a typedef");
902 /* Yet another place where we could have junk (notably attributes)
903 after the declarator. */
904 another
= consume_until_comma_or_semi (false);
906 do_typedef (name
, dty
, &lexer_line
);
911 /* Structure definition: type() does all the work. */
914 struct_or_union (void)
917 type (&dummy
, false);
918 /* There may be junk after the type: notably, we cannot currently
919 distinguish 'struct foo *function(prototype);' from 'struct foo;'
920 ... we could call declarator(), but it's a waste of time at
921 present. Instead, just eat whatever token is currently lookahead
922 and go back to lexical skipping mode. */
926 /* GC root declaration:
927 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
928 If the gtymarker is not present, we ignore the rest of the declaration. */
930 extern_or_static (void)
932 options_p opts
, opts2
, dopts
;
935 require2 (EXTERN
, STATIC
);
937 if (token () != GTY_TOKEN
)
944 ty
= type (&opts2
, true); /* if we get here, it's got a GTY(()) */
945 dty
= declarator (ty
, &name
, &dopts
);
947 if ((opts
&& dopts
) || (opts
&& opts2
) || (opts2
&& dopts
))
948 parse_error ("GTY((...)) specified more than once for %s", name
);
956 note_variable (name
, adjust_field_type (dty
, opts
), opts
, &lexer_line
);
961 /* Parse the file FNAME for GC-relevant declarations and definitions.
962 This is the only entry point to this file. */
964 parse_file (const char *fname
)
989 parse_error ("unexpected top level token, %s", print_cur_token ());
992 lexer_toplevel_done
= 1;