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
);
147 /* If the next token does not have code T, report a parse error; otherwise
148 return the token's value. */
153 const char *v
= advance ();
156 parse_error ("expected %s, have %s",
157 print_token (t
, 0), print_token (u
, v
));
163 /* If the next token does not have one of the codes T1 or T2, report a
164 parse error; otherwise return the token's value. */
166 require2 (int t1
, int t2
)
169 const char *v
= advance ();
170 if (u
!= t1
&& u
!= t2
)
172 parse_error ("expected %s or %s, have %s",
173 print_token (t1
, 0), print_token (t2
, 0),
180 /* Near-terminals. */
182 /* C-style string constant concatenation: STRING+
183 Bare STRING should appear nowhere else in this file. */
191 s1
= require (STRING
);
194 while (token () == STRING
)
200 buf
= XRESIZEVEC (char, CONST_CAST(char *, s1
), l1
+ l2
+ 1);
201 memcpy (buf
+ l1
, s2
, l2
+ 1);
202 XDELETE (CONST_CAST (char *, s2
));
208 /* typedef_name: either an ID, or VEC(x,y) which is translated to VEC_x_y.
209 Use only where VEC(x,y) is legitimate, i.e. in positions where a
210 typedef name may appear. */
214 if (token () == VEC_TOKEN
)
216 const char *c1
, *c2
, *r
;
219 c1
= require2 (ID
, SCALAR
);
223 r
= concat ("VEC_", c1
, "_", c2
, (char *)0);
224 free (CONST_CAST (char *, c1
));
225 free (CONST_CAST (char *, c2
));
232 /* Absorb a sequence of tokens delimited by balanced ()[]{}. */
234 consume_balanced (int opener
, int closer
)
240 default: advance (); break;
241 case '(': consume_balanced ('(',')'); break;
242 case '[': consume_balanced ('[',']'); break;
243 case '{': consume_balanced ('{','}'); break;
248 if (token () != closer
)
249 parse_error ("unbalanced delimiters - expected '%c', have '%c'",
255 parse_error ("unexpected end of file within %c%c-delimited construct",
261 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
262 expressions, until we encounter a semicolon outside any such
263 delimiters; absorb that too. If IMMEDIATE is true, it is an error
264 if the semicolon is not the first token encountered. */
266 consume_until_semi (bool immediate
)
268 if (immediate
&& token () != ';')
273 case ';': advance (); return;
274 default: advance (); break;
276 case '(': consume_balanced ('(',')'); break;
277 case '[': consume_balanced ('[',']'); break;
278 case '{': consume_balanced ('{','}'); break;
283 parse_error ("unmatched '%c' while scanning for ';'", token ());
287 parse_error ("unexpected end of file while scanning for ';'");
292 /* Absorb a sequence of tokens, possibly including ()[]{}-delimited
293 expressions, until we encounter a comma or semicolon outside any
294 such delimiters; absorb that too. If IMMEDIATE is true, it is an
295 error if the comma or semicolon is not the first token encountered.
296 Returns true if the loop ended with a comma. */
298 consume_until_comma_or_semi (bool immediate
)
300 if (immediate
&& token () != ',' && token () != ';')
305 case ',': advance (); return true;
306 case ';': advance (); return false;
307 default: advance (); break;
309 case '(': consume_balanced ('(',')'); break;
310 case '[': consume_balanced ('[',']'); break;
311 case '{': consume_balanced ('{','}'); break;
316 parse_error ("unmatched '%s' while scanning for ',' or ';'",
321 parse_error ("unexpected end of file while scanning for ',' or ';'");
327 /* GTY(()) option handling. */
328 static type_p
type (options_p
*optsp
, bool nested
);
330 /* Optional parenthesized string: ('(' string_seq ')')? */
332 str_optvalue_opt (options_p prev
)
334 const char *name
= advance ();
335 const char *value
= "";
339 value
= string_seq ();
342 return create_option (prev
, name
, value
);
345 /* absdecl: type '*'*
346 -- a vague approximation to what the C standard calls an abstract
347 declarator. The only kinds that are actually used are those that
348 are just a bare type and those that have trailing pointer-stars.
349 Further kinds should be implemented if and when they become
350 necessary. Used only within GTY(()) option values, therefore
351 further GTY(()) tags within the type are invalid. Note that the
352 return value has already been run through adjust_field_type. */
359 ty
= type (&opts
, true);
360 while (token () == '*')
362 ty
= create_pointer (ty
);
367 parse_error ("nested GTY(()) options are invalid");
369 return adjust_field_type (ty
, 0);
372 /* Type-option: '(' absdecl ')' */
374 type_optvalue (options_p prev
, const char *name
)
380 return create_option (prev
, name
, ty
);
383 /* Nested pointer data: '(' type '*'* ',' string_seq ',' string_seq ')' */
385 nestedptr_optvalue (options_p prev
)
388 const char *from
, *to
;
395 from
= string_seq ();
398 return create_nested_ptr_option (prev
, ty
, to
, from
);
401 /* One GTY(()) option:
403 | PTR_ALIAS type_optvalue
404 | PARAM_IS type_optvalue
405 | NESTED_PTR nestedptr_optvalue
408 option (options_p prev
)
413 return str_optvalue_opt (prev
);
417 return type_optvalue (prev
, "ptr_alias");
420 return type_optvalue (prev
, advance ());
424 return nestedptr_optvalue (prev
);
427 parse_error ("expected an option keyword, have %s",
430 return create_option (prev
, "", "");
434 /* One comma-separated list of options. */
441 while (token () == ',')
449 /* GTY marker: 'GTY' '(' '(' option_seq? ')' ')' */
453 options_p result
= 0;
458 result
= option_seq ();
464 /* Optional GTY marker. */
468 if (token () != GTY_TOKEN
)
473 /* Declarators. The logic here is largely lifted from c-parser.c.
474 Note that we do not have to process abstract declarators, which can
475 appear only in parameter type lists or casts (but see absdecl,
476 above). Also, type qualifiers are thrown out in gengtype-lex.l so
477 we don't have to do it. */
479 /* array_and_function_declarators_opt:
481 array_and_function_declarators_opt ARRAY
482 array_and_function_declarators_opt '(' ... ')'
484 where '...' indicates stuff we ignore except insofar as grouping
485 symbols ()[]{} must balance.
487 Subroutine of direct_declarator - do not use elsewhere. */
490 array_and_function_declarators_opt (type_p ty
)
492 if (token () == ARRAY
)
494 const char *array
= advance ();
495 return create_array (array_and_function_declarators_opt (ty
), array
);
497 else if (token () == '(')
499 /* We don't need exact types for functions. */
500 consume_balanced ('(', ')');
501 array_and_function_declarators_opt (ty
);
502 return create_scalar_type ("function type");
508 static type_p
inner_declarator (type_p
, const char **, options_p
*);
510 /* direct_declarator:
511 '(' inner_declarator ')'
512 gtymarker_opt ID array_and_function_declarators_opt
514 Subroutine of declarator, mutually recursive with inner_declarator;
515 do not use elsewhere. */
517 direct_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
519 /* The first token in a direct-declarator must be an ID, a
520 GTY marker, or an open parenthesis. */
524 *optsp
= gtymarker ();
527 *namep
= require (ID
);
532 ty
= inner_declarator (ty
, namep
, optsp
);
537 parse_error ("expected '(', 'GTY', or an identifier, have %s",
539 /* Do _not_ advance if what we have is a close squiggle brace, as
540 we will get much better error recovery that way. */
545 return array_and_function_declarators_opt (ty
);
548 /* The difference between inner_declarator and declarator is in the
549 handling of stars. Consider this declaration:
553 It declares a pointer to a function that takes no arguments and
554 returns a char*. To construct the correct type for this
555 declaration, the star outside the parentheses must be processed
556 _before_ the function type, the star inside the parentheses must
557 be processed _after_ the function type. To accomplish this,
558 declarator() creates pointers before recursing (it is actually
559 coded as a while loop), whereas inner_declarator() recurses before
560 creating pointers. */
566 Mutually recursive subroutine of direct_declarator; do not use
570 inner_declarator (type_p ty
, const char **namep
, options_p
*optsp
)
576 inner
= inner_declarator (ty
, namep
, optsp
);
580 return create_pointer (ty
);
583 return direct_declarator (ty
, namep
, optsp
);
586 /* declarator: '*'+ direct_declarator
588 This is the sole public interface to this part of the grammar.
589 Arguments are the type known so far, a pointer to where the name
590 may be stored, and a pointer to where GTY options may be stored.
591 Returns the final type. */
594 declarator (type_p ty
, const char **namep
, options_p
*optsp
)
598 while (token () == '*')
601 ty
= create_pointer (ty
);
603 return direct_declarator (ty
, namep
, optsp
);
606 /* Types and declarations. */
608 /* Structure field(s) declaration:
611 | type declarator bitfield? ( ',' declarator bitfield? )+ ';'
614 Knows that such declarations must end with a close brace (or,
615 erroneously, at EOF).
618 struct_field_seq (void)
622 options_p opts
, dopts
;
628 ty
= type (&opts
, true);
629 /* Another piece of the IFCVT_EXTRA_FIELDS special case, see type(). */
630 if (!ty
&& token () == '}')
633 if (!ty
|| token () == ':')
635 consume_until_semi (false);
641 dty
= declarator (ty
, &name
, &dopts
);
642 /* There could be any number of weird things after the declarator,
643 notably bitfield declarations and __attribute__s. If this
644 function returns true, the last thing was a comma, so we have
645 more than one declarator paired with the current type. */
646 another
= consume_until_comma_or_semi (false);
652 parse_error ("two GTY(()) options for field %s", name
);
656 f
= create_field_at (f
, dty
, name
, dopts
, &lexer_line
);
660 while (token () != '}' && token () != EOF_TOKEN
);
661 return nreverse_pairs (f
);
664 /* This is called type(), but what it parses (sort of) is what C calls
665 declaration-specifiers and specifier-qualifier-list:
669 | (STRUCT|UNION) ID? gtymarker? ( '{' gtymarker? struct_field_seq '}' )?
670 | ENUM ID ( '{' ... '}' )?
672 Returns a partial type; under some conditions (notably
673 "struct foo GTY((...)) thing;") it may write an options
677 type (options_p
*optsp
, bool nested
)
686 return create_scalar_type (s
);
691 return resolve_typedef (s
, &lexer_line
);
698 is_union
= (token() == UNION
);
704 s
= xasprintf ("anonymous:%s:%d", lexer_line
.file
, lexer_line
.line
);
706 /* Top-level structures that are not explicitly tagged GTY(())
707 are treated as mere forward declarations. This is because
708 there are a lot of structures that we don't need to know
709 about, and some of those have weird macro stuff in them
710 that we can't handle. */
711 if (nested
|| token () == GTY_TOKEN
)
713 opts
= gtymarker_opt ();
718 fields
= struct_field_seq ();
720 return new_structure (s
, is_union
, &lexer_line
, fields
, opts
);
723 else if (token () == '{')
724 consume_balanced ('{', '}');
727 return find_structure (s
, is_union
);
735 s
= xasprintf ("anonymous:%s:%d", lexer_line
.file
, lexer_line
.line
);
738 consume_balanced ('{','}');
739 return create_scalar_type (s
);
742 parse_error ("expected a type specifier, have %s", print_cur_token ());
744 return create_scalar_type ("erroneous type");
748 /* Top level constructs. */
750 /* Dispatch declarations beginning with 'typedef'. */
760 gcc_assert (token () == TYPEDEF
);
763 ty
= type (&opts
, false);
767 parse_error ("GTY((...)) cannot be applied to a typedef");
770 dty
= declarator (ty
, &name
, &opts
);
772 parse_error ("GTY((...)) cannot be applied to a typedef");
774 /* Yet another place where we could have junk (notably attributes)
775 after the declarator. */
776 another
= consume_until_comma_or_semi (false);
778 do_typedef (name
, dty
, &lexer_line
);
783 /* Structure definition: type() does all the work. */
786 struct_or_union (void)
789 type (&dummy
, false);
790 /* There may be junk after the type: notably, we cannot currently
791 distinguish 'struct foo *function(prototype);' from 'struct foo;'
792 ... we could call declarator(), but it's a waste of time at
793 present. Instead, just eat whatever token is currently lookahead
794 and go back to lexical skipping mode. */
798 /* GC root declaration:
799 (extern|static) gtymarker? type ID array_declarators_opt (';'|'=')
800 If the gtymarker is not present, we ignore the rest of the declaration. */
802 extern_or_static (void)
804 options_p opts
, opts2
, dopts
;
807 require2 (EXTERN
, STATIC
);
809 if (token () != GTY_TOKEN
)
816 ty
= type (&opts2
, true); /* if we get here, it's got a GTY(()) */
817 dty
= declarator (ty
, &name
, &dopts
);
819 if ((opts
&& dopts
) || (opts
&& opts2
) || (opts2
&& dopts
))
820 parse_error ("GTY((...)) specified more than once for %s", name
);
828 note_variable (name
, adjust_field_type (dty
, opts
), opts
, &lexer_line
);
833 /* Definition of a generic VEC structure:
835 'DEF_VEC_[IPO]' '(' id ')' ';'
837 Scalar VECs require slightly different treatment than otherwise -
838 that's handled in note_def_vec, we just pass it along.*/
842 bool is_scalar
= (token() == DEFVEC_I
);
845 require2 (DEFVEC_OP
, DEFVEC_I
);
847 type
= require2 (ID
, SCALAR
);
854 note_def_vec (type
, is_scalar
, &lexer_line
);
855 note_def_vec_alloc (type
, "none", &lexer_line
);
858 /* Definition of an allocation strategy for a VEC structure:
860 'DEF_VEC_ALLOC_[IPO]' '(' id ',' id ')' ';'
862 For purposes of gengtype, this just declares a wrapper structure. */
866 const char *type
, *astrat
;
868 require (DEFVEC_ALLOC
);
870 type
= require2 (ID
, SCALAR
);
872 astrat
= require (ID
);
876 if (!type
|| !astrat
)
879 note_def_vec_alloc (type
, astrat
, &lexer_line
);
882 /* Parse the file FNAME for GC-relevant declarations and definitions.
883 This is the only entry point to this file. */
885 parse_file (const char *fname
)
919 parse_error ("unexpected top level token, %s", print_cur_token ());
922 lexer_toplevel_done
= 1;