1 /* Process source files and output type information.
2 Copyright (C) 2006-2014 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 "a param<N>_is option",
87 "a character constant",
88 "an array declarator",
89 "a C++ keyword to ignore"
92 /* This array is indexed by token code minus FIRST_TOKEN_WITH_VALUE. */
93 static const char *const token_value_format
[] = {
104 /* Produce a printable representation for a token defined by CODE and
105 VALUE. This sometimes returns pointers into malloc memory and
106 sometimes not, therefore it is unsafe to free the pointer it
107 returns, so that memory is leaked. This does not matter, as this
108 function is only used for diagnostics, and in a successful run of
109 the program there will be none. */
111 print_token (int code
, const char *value
)
113 if (code
< CHAR_TOKEN_OFFSET
)
114 return xasprintf ("'%c'", code
);
115 else if (code
< FIRST_TOKEN_WITH_VALUE
)
116 return xasprintf ("'%s'", token_names
[code
- CHAR_TOKEN_OFFSET
]);
118 return token_names
[code
- CHAR_TOKEN_OFFSET
]; /* don't quote these */
120 return xasprintf (token_value_format
[code
- FIRST_TOKEN_WITH_VALUE
],
124 /* Convenience wrapper around print_token which produces the printable
125 representation of the current token. */
126 static inline const char *
127 print_cur_token (void)
129 return print_token (T
.code
, T
.value
);
132 /* Report a parse error on the current line, with diagnostic MSG.
133 Behaves as standard printf with respect to additional arguments and
135 static void ATTRIBUTE_PRINTF_1
136 parse_error (const char *msg
, ...)
140 fprintf (stderr
, "%s:%d: parse error: ",
141 get_input_file_name (lexer_line
.file
), lexer_line
.line
);
144 vfprintf (stderr
, msg
, ap
);
147 fputc ('\n', stderr
);
152 /* If the next token does not have code T, report a parse error; otherwise
153 return the token's value. */
158 const char *v
= advance ();
161 parse_error ("expected %s, have %s",
162 print_token (t
, 0), print_token (u
, v
));
168 /* As per require, but do not advance. */
170 require_without_advance (int t
)
173 const char *v
= T
.value
;
176 parse_error ("expected %s, have %s",
177 print_token (t
, 0), print_token (u
, v
));
183 /* If the next token does not have one of the codes T1 or T2, report a
184 parse error; otherwise return the token's value. */
186 require2 (int t1
, int t2
)
189 const char *v
= advance ();
190 if (u
!= t1
&& u
!= t2
)
192 parse_error ("expected %s or %s, have %s",
193 print_token (t1
, 0), print_token (t2
, 0),
200 /* If the next token does not have one of the codes T1, T2 or T3, report a
201 parse error; otherwise return the token's value. */
203 require3 (int t1
, int t2
, int t3
)
206 const char *v
= advance ();
207 if (u
!= t1
&& u
!= t2
&& u
!= t3
)
209 parse_error ("expected %s, %s or %s, have %s",
210 print_token (t1
, 0), print_token (t2
, 0),
211 print_token (t3
, 0), print_token (u
, v
));
217 /* Near-terminals. */
219 /* C-style string constant concatenation: STRING+
220 Bare STRING should appear nowhere else in this file. */
228 s1
= require (STRING
);
231 while (token () == STRING
)
237 buf
= XRESIZEVEC (char, CONST_CAST (char *, s1
), l1
+ l2
+ 1);
238 memcpy (buf
+ l1
, s2
, l2
+ 1);
239 XDELETE (CONST_CAST (char *, s2
));
246 /* The caller has detected a template declaration that starts
247 with TMPL_NAME. Parse up to the closing '>'. This recognizes
248 simple template declarations of the form ID<ID1,ID2,...,IDn>.
249 It does not try to parse anything more sophisticated than that.
251 Returns the template declaration string "ID<ID1,ID2,...,IDn>". */
254 require_template_declaration (const char *tmpl_name
)
258 /* Recognize the opening '<'. */
260 str
= concat (tmpl_name
, "<", (char *) 0);
262 /* Read the comma-separated list of identifiers. */
266 if (token () == ENUM
)
269 str
= concat (str
, "enum ", (char *) 0);
274 str
= concat (str
, advance (), (char *) 0);
280 str
= concat (str
, ":", (char *) 0);
286 str
= concat (str
, "<", (char *) 0);
293 str
= concat (str
, ">", (char *) 0);
297 const char *id
= require3 (SCALAR
, ID
, ',');
300 str
= concat (str
, id
, (char *) 0);
306 /* typedef_name: either an ID, or a template type
307 specification of the form ID<t1,t2,...,tn>. */
312 const char *id
= require (ID
);
314 return require_template_declaration (id
);
319 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
321 consume_balanced (int opener
, int closer
)
331 consume_balanced ('(', ')');
334 consume_balanced ('[', ']');
337 consume_balanced ('{', '}');
343 if (token () != closer
)
344 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
350 parse_error ("unexpected end of file within %c%c-delimited construct",
356 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
357 expressions, until we encounter an end-of-statement marker (a ';' or
358 a '}') outside any such delimiters; absorb that too. */
361 consume_until_eos (void)
371 consume_balanced ('{', '}');
375 consume_balanced ('(', ')');
379 consume_balanced ('[', ']');
385 parse_error ("unmatched '%c' while scanning for ';'", token ());
389 parse_error ("unexpected end of file while scanning for ';'");
398 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
399 expressions, until we encounter a comma or semicolon outside any
400 such delimiters; absorb that too. Returns true if the loop ended
404 consume_until_comma_or_eos ()
418 consume_balanced ('{', '}');
422 consume_balanced ('(', ')');
426 consume_balanced ('[', ']');
432 parse_error ("unmatched '%s' while scanning for ',' or ';'",
437 parse_error ("unexpected end of file while scanning for ',' or ';'");
447 /* GTY(()) option handling. */
448 static type_p
type (options_p
*optsp
, bool nested
);
450 /* Optional parenthesized string: ('(' string_seq ')')? */
452 str_optvalue_opt (options_p prev
)
454 const char *name
= advance ();
455 const char *value
= "";
459 value
= string_seq ();
462 return create_string_option (prev
, name
, value
);
465 /* absdecl: type '*'*
466 -- a vague approximation to what the C standard calls an abstract
467 declarator. The only kinds that are actually used are those that
468 are just a bare type and those that have trailing pointer-stars.
469 Further kinds should be implemented if and when they become
470 necessary. Used only within GTY(()) option values, therefore
471 further GTY(()) tags within the type are invalid. Note that the
472 return value has already been run through adjust_field_type. */
479 ty
= type (&opts
, true);
480 while (token () == '*')
482 ty
= create_pointer (ty
);
487 parse_error ("nested GTY(()) options are invalid");
489 return adjust_field_type (ty
, 0);
492 /* Type-option: '(' absdecl ')' */
494 type_optvalue (options_p prev
, const char *name
)
500 return create_type_option (prev
, name
, ty
);
503 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
505 nestedptr_optvalue (options_p prev
)
508 const char *from
, *to
;
515 from
= string_seq ();
518 return create_nested_ptr_option (prev
, ty
, to
, from
);
521 /* One GTY(()) option:
523 | PTR_ALIAS type_optvalue
524 | PARAM_IS type_optvalue
525 | NESTED_PTR nestedptr_optvalue
528 option (options_p prev
)
533 return str_optvalue_opt (prev
);
537 return type_optvalue (prev
, "ptr_alias");
540 return type_optvalue (prev
, advance ());
544 return nestedptr_optvalue (prev
);
548 return create_string_option (prev
, "user", "");
551 parse_error ("expected an option keyword, have %s", print_cur_token ());
553 return create_string_option (prev
, "", "");
557 /* One comma-separated list of options. */
564 while (token () == ',')
572 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
576 options_p result
= 0;
581 result
= option_seq ();
587 /* Optional GTY marker. */
591 if (token () != GTY_TOKEN
)
598 /* Declarators. The logic here is largely lifted from c-parser.c.
599 Note that we do not have to process abstract declarators, which can
600 appear only in parameter type lists or casts (but see absdecl,
601 above). Also, type qualifiers are thrown out in gengtype-lex.l so
602 we don't have to do it. */
604 /* array_and_function_declarators_opt:
606 array_and_function_declarators_opt ARRAY
607 array_and_function_declarators_opt '(' ... ')'
609 where '...' indicates stuff we ignore except insofar as grouping
610 symbols ()[]{} must balance.
612 Subroutine of direct_declarator - do not use elsewhere. */
615 array_and_function_declarators_opt (type_p ty
)
617 if (token () == ARRAY
)
619 const char *array
= advance ();
620 return create_array (array_and_function_declarators_opt (ty
), array
);
622 else if (token () == '(')
624 /* We don't need exact types for functions. */
625 consume_balanced ('(', ')');
626 array_and_function_declarators_opt (ty
);
627 return create_scalar_type ("function type");
633 static type_p
inner_declarator (type_p
, const char **, options_p
*, bool);
635 /* direct_declarator:
636 '(' inner_declarator ')'
637 '(' \epsilon ')' <-- C++ ctors/dtors
638 gtymarker_opt ID array_and_function_declarators_opt
640 Subroutine of declarator, mutually recursive with inner_declarator;
641 do not use elsewhere.
643 IN_STRUCT is true if we are called while parsing structures or classes. */
646 direct_declarator (type_p ty
, const char **namep
, options_p
*optsp
,
649 /* The first token in a direct-declarator must be an ID, a
650 GTY marker, or an open parenthesis. */
654 *optsp
= gtymarker ();
658 *namep
= require (ID
);
659 /* If the next token is '(', we are parsing a function declaration.
660 Functions are ignored by gengtype, so we return NULL. */
666 /* If the declarator starts with a '(', we have three options. We
667 are either parsing 'TYPE (*ID)' (i.e., a function pointer)
670 The latter will be a constructor iff we are inside a
671 structure or class. Otherwise, it could be a typedef, but
672 since we explicitly reject typedefs inside structures, we can
673 assume that we found a ctor and return NULL. */
675 if (in_struct
&& token () != '*')
677 /* Found a constructor. Find and consume the closing ')'. */
678 while (token () != ')')
681 /* Tell the caller to ignore this. */
684 ty
= inner_declarator (ty
, namep
, optsp
, in_struct
);
688 case IGNORABLE_CXX_KEYWORD
:
689 /* Any C++ keyword like 'operator' means that we are not looking
690 at a regular data declarator. */
694 parse_error ("expected '(', ')', 'GTY', or an identifier, have %s",
696 /* Do _not_ advance if what we have is a close squiggle brace, as
697 we will get much better error recovery that way. */
702 return array_and_function_declarators_opt (ty
);
705 /* The difference between inner_declarator and declarator is in the
706 handling of stars. Consider this declaration:
710 It declares a pointer to a function that takes no arguments and
711 returns a char*. To construct the correct type for this
712 declaration, the star outside the parentheses must be processed
713 _before_ the function type, the star inside the parentheses must
714 be processed _after_ the function type. To accomplish this,
715 declarator() creates pointers before recursing (it is actually
716 coded as a while loop), whereas inner_declarator() recurses before
717 creating pointers. */
723 Mutually recursive subroutine of direct_declarator; do not use
726 IN_STRUCT is true if we are called while parsing structures or classes. */
729 inner_declarator (type_p ty
, const char **namep
, options_p
*optsp
,
736 inner
= inner_declarator (ty
, namep
, optsp
, in_struct
);
740 return create_pointer (ty
);
743 return direct_declarator (ty
, namep
, optsp
, in_struct
);
746 /* declarator: '*'+ direct_declarator
748 This is the sole public interface to this part of the grammar.
749 Arguments are the type known so far, a pointer to where the name
750 may be stored, and a pointer to where GTY options may be stored.
752 IN_STRUCT is true when we are called to parse declarators inside
753 a structure or class.
755 Returns the final type. */
758 declarator (type_p ty
, const char **namep
, options_p
*optsp
,
759 bool in_struct
= false)
763 while (token () == '*')
766 ty
= create_pointer (ty
);
768 return direct_declarator (ty
, namep
, optsp
, in_struct
);
771 /* Types and declarations. */
773 /* Structure field(s) declaration:
776 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
779 Knows that such declarations must end with a close brace (or,
780 erroneously, at EOF).
783 struct_field_seq (void)
787 options_p opts
, dopts
;
791 while (token () != '}' && token () != EOF_TOKEN
)
793 ty
= type (&opts
, true);
795 /* Ignore access-control keywords ("public:" etc). */
796 while (!ty
&& token () == IGNORABLE_CXX_KEYWORD
)
798 const char *keyword
= advance ();
799 if (strcmp (keyword
, "public:") != 0
800 && strcmp (keyword
, "private:") != 0
801 && strcmp (keyword
, "protected:") != 0)
803 ty
= type (&opts
, true);
806 if (!ty
|| token () == ':')
808 consume_until_eos ();
814 dty
= declarator (ty
, &name
, &dopts
, true);
816 /* There could be any number of weird things after the declarator,
817 notably bitfield declarations and __attribute__s. If this
818 function returns true, the last thing was a comma, so we have
819 more than one declarator paired with the current type. */
820 another
= consume_until_comma_or_eos ();
826 parse_error ("two GTY(()) options for field %s", name
);
830 f
= create_field_at (f
, dty
, name
, dopts
, &lexer_line
);
834 return nreverse_pairs (f
);
837 /* Return true if OPTS contain the option named STR. */
840 opts_have (options_p opts
, const char *str
)
842 for (options_p opt
= opts
; opt
; opt
= opt
->next
)
843 if (strcmp (opt
->name
, str
) == 0)
849 /* This is called type(), but what it parses (sort of) is what C calls
850 declaration-specifiers and specifier-qualifier-list:
854 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
855 | ENUM ID ( '{' ... '}' )?
857 Returns a partial type; under some conditions (notably
858 "struct foo GTY((...)) thing;") it may write an options
861 NESTED is true when parsing a declaration already known to have a
862 GTY marker. In these cases, typedef and enum declarations are not
863 allowed because gengtype only understands types at the global
867 type (options_p
*optsp
, bool nested
)
875 return create_scalar_type (s
);
879 return resolve_typedef (s
, &lexer_line
);
881 case IGNORABLE_CXX_KEYWORD
:
882 /* By returning NULL here, we indicate to the caller that they
883 should ignore everything following this keyword up to the
890 type_p base_class
= NULL
;
892 /* GTY annotations follow attribute syntax
893 GTY_BEFORE_ID is for union/struct declarations
894 GTY_AFTER_ID is for variable declarations. */
901 enum typekind kind
= (token () == UNION
) ? TYPE_UNION
: TYPE_STRUCT
;
904 /* Top-level structures that are not explicitly tagged GTY(())
905 are treated as mere forward declarations. This is because
906 there are a lot of structures that we don't need to know
907 about, and some of those have C++ and macro constructs that
909 if (nested
|| token () == GTY_TOKEN
)
911 is_gty
= GTY_BEFORE_ID
;
912 opts
= gtymarker_opt ();
918 s
= xasprintf ("anonymous:%s:%d",
919 get_input_file_name (lexer_line
.file
),
922 /* Unfortunately above GTY_TOKEN check does not capture the
923 typedef struct_type GTY case. */
924 if (token () == GTY_TOKEN
)
926 is_gty
= GTY_AFTER_ID
;
927 opts
= gtymarker_opt ();
930 bool is_user_gty
= opts_have (opts
, "user");
934 if (is_gty
&& !is_user_gty
)
936 /* For GTY-marked types that are not "user", parse some C++
937 inheritance specifications.
938 We require single-inheritance from a non-template type. */
940 const char *basename
= require (ID
);
941 /* This may be either an access specifier, or the base name. */
942 if (0 == strcmp (basename
, "public")
943 || 0 == strcmp (basename
, "protected")
944 || 0 == strcmp (basename
, "private"))
945 basename
= require (ID
);
946 base_class
= find_structure (basename
, TYPE_STRUCT
);
948 parse_error ("unrecognized base class: %s", basename
);
949 require_without_advance ('{');
953 /* For types lacking GTY-markings, skip over C++ inheritance
954 specification (and thus avoid having to parse e.g. template
956 while (token () != '{')
967 if (is_gty
== GTY_AFTER_ID
)
968 parse_error ("GTY must be specified before identifier");
973 fields
= struct_field_seq ();
978 /* Do not look inside user defined structures. */
980 kind
= TYPE_USER_STRUCT
;
981 consume_balanced ('{', '}');
982 return create_user_defined_type (s
, &lexer_line
);
985 return new_structure (s
, kind
, &lexer_line
, fields
, opts
,
989 else if (token () == '{')
990 consume_balanced ('{', '}');
993 return find_structure (s
, kind
);
997 /* In C++, a typedef inside a struct/class/union defines a new
998 type for that inner scope. We cannot support this in
999 gengtype because we have no concept of scoping.
1001 We handle typedefs in the global scope separately (see
1002 parse_file), so if we find a 'typedef', we must be inside
1004 gcc_assert (nested
);
1005 parse_error ("typedefs not supported in structures marked with "
1006 "automatic GTY markers. Use GTY((user)) to mark "
1016 s
= xasprintf ("anonymous:%s:%d",
1017 get_input_file_name (lexer_line
.file
),
1020 if (token () == '{')
1021 consume_balanced ('{', '}');
1023 /* If after parsing the enum we are at the end of the statement,
1024 and we are currently inside a structure, then this was an
1025 enum declaration inside this scope.
1027 We cannot support this for the same reason we cannot support
1028 'typedef' inside structures (see the TYPEDEF handler above).
1029 If this happens, emit an error and return NULL. */
1030 if (nested
&& token () == ';')
1032 parse_error ("enum definitions not supported in structures marked "
1033 "with automatic GTY markers. Use GTY((user)) to mark "
1039 return create_scalar_type (s
);
1042 parse_error ("expected a type specifier, have %s", print_cur_token ());
1044 return create_scalar_type ("erroneous type");
1048 /* Top level constructs. */
1050 /* Dispatch declarations beginning with 'typedef'. */
1060 gcc_assert (token () == TYPEDEF
);
1063 ty
= type (&opts
, false);
1067 parse_error ("GTY((...)) cannot be applied to a typedef");
1070 dty
= declarator (ty
, &name
, &opts
);
1072 parse_error ("GTY((...)) cannot be applied to a typedef");
1074 /* Yet another place where we could have junk (notably attributes)
1075 after the declarator. */
1076 another
= consume_until_comma_or_eos ();
1078 do_typedef (name
, dty
, &lexer_line
);
1083 /* Structure definition: type() does all the work. */
1086 struct_or_union (void)
1089 type (&dummy
, false);
1090 /* There may be junk after the type: notably, we cannot currently
1091 distinguish 'struct foo *function(prototype);' from 'struct foo;'
1092 ... we could call declarator(), but it's a waste of time at
1093 present. Instead, just eat whatever token is currently lookahead
1094 and go back to lexical skipping mode. */
1098 /* GC root declaration:
1099 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
1100 If the gtymarker is not present, we ignore the rest of the declaration. */
1102 extern_or_static (void)
1104 options_p opts
, opts2
, dopts
;
1107 require2 (EXTERN
, STATIC
);
1109 if (token () != GTY_TOKEN
)
1115 opts
= gtymarker ();
1116 ty
= type (&opts2
, true); /* if we get here, it's got a GTY(()) */
1117 dty
= declarator (ty
, &name
, &dopts
);
1119 if ((opts
&& dopts
) || (opts
&& opts2
) || (opts2
&& dopts
))
1120 parse_error ("GTY((...)) specified more than once for %s", name
);
1128 note_variable (name
, adjust_field_type (dty
, opts
), opts
, &lexer_line
);
1129 require2 (';', '=');
1133 /* Parse the file FNAME for GC-relevant declarations and definitions.
1134 This is the only entry point to this file. */
1136 parse_file (const char *fname
)
1145 extern_or_static ();
1161 parse_error ("unexpected top level token, %s", print_cur_token ());
1164 lexer_toplevel_done
= 1;