1 /* C global declaration parser for genksyms.
2 Copyright 1996, 1997 Linux International.
4 New implementation contributed by Richard Henderson <rth@tamu.edu>
5 Based on original work by Bjorn Ekwall <bj0rn@blox.se>
7 This file is part of the Linux modutils.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 static int is_typedef
;
33 static char *current_name
;
34 static struct string_list
*decl_spec
;
36 static void yyerror(const char *);
39 remove_node
(struct string_list
**p
)
41 struct string_list
*node
= *p
;
47 remove_list
(struct string_list
**pb
, struct string_list
**pe
)
49 struct string_list
*b
= *pb
, *e
= *pe
;
83 %token EXPORT_SYMBOL_KEYW
86 %token ATTRIBUTE_PHRASE
89 %token EXPRESSION_PHRASE
105 | declaration_seq declaration
109 { is_typedef
= 0; is_extern
= 0; current_name
= NULL
; decl_spec
= NULL
; }
111 { free_list
(*$2, NULL
); *$2 = NULL
; }
115 EXTENSION_KEYW TYPEDEF_KEYW
{ is_typedef
= 1; } simple_declaration
117 | TYPEDEF_KEYW
{ is_typedef
= 1; } simple_declaration
120 | function_definition
123 |
error ';' { $$
= $2; }
124 |
error '}' { $$
= $2; }
128 decl_specifier_seq_opt init_declarator_list_opt
';'
129 { if
(current_name
) {
130 struct string_list
*decl
= (*$3)->next
;
132 add_symbol
(current_name
,
133 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
,
141 init_declarator_list_opt:
142 /* empty */ { $$
= NULL
; }
143 | init_declarator_list
146 init_declarator_list:
148 { struct string_list
*decl
= *$1;
150 add_symbol
(current_name
,
151 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
, decl
, is_extern
);
155 | init_declarator_list
',' init_declarator
156 { struct string_list
*decl
= *$3;
158 free_list
(*$2, NULL
);
160 add_symbol
(current_name
,
161 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
, decl
, is_extern
);
168 declarator asm_phrase_opt attribute_opt initializer_opt
169 { $$
= $4 ?
$4 : $3 ?
$3 : $2 ?
$2 : $1; }
172 /* Hang on to the specifiers so that we can reuse them. */
173 decl_specifier_seq_opt:
174 /* empty */ { decl_spec
= NULL
; }
179 decl_specifier
{ decl_spec
= *$1; }
180 | decl_specifier_seq decl_specifier
{ decl_spec
= *$2; }
184 storage_class_specifier
185 { /* Version 2 checksumming ignores storage class, as that
186 is really irrelevant to the linkage. */
193 storage_class_specifier:
197 | EXTERN_KEYW
{ is_extern
= 1; $$
= $1; }
198 | INLINE_KEYW
{ is_extern
= 0; $$
= $1; }
202 simple_type_specifier
204 | TYPEOF_KEYW
'(' decl_specifier_seq
'*' ')'
205 | TYPEOF_KEYW
'(' decl_specifier_seq
')'
207 /* References to s/u/e's defined elsewhere. Rearrange things
208 so that it is easier to expand the definition fully later. */
210 { remove_node
($1); (*$2)->tag
= SYM_STRUCT
; $$
= $2; }
212 { remove_node
($1); (*$2)->tag
= SYM_UNION
; $$
= $2; }
214 { remove_node
($1); (*$2)->tag
= SYM_ENUM
; $$
= $2; }
216 /* Full definitions of an s/u/e. Record it. */
217 | STRUCT_KEYW IDENT class_body
218 { struct string_list
*s
= *$3, *i
= *$2, *r
;
219 r
= copy_node
(i
); r
->tag
= SYM_STRUCT
;
220 r
->next
= (*$1)->next
; *$3 = r
; (*$1)->next
= NULL
;
221 add_symbol
(i
->string, SYM_STRUCT
, s
, is_extern
);
224 | UNION_KEYW IDENT class_body
225 { struct string_list
*s
= *$3, *i
= *$2, *r
;
226 r
= copy_node
(i
); r
->tag
= SYM_UNION
;
227 r
->next
= (*$1)->next
; *$3 = r
; (*$1)->next
= NULL
;
228 add_symbol
(i
->string, SYM_UNION
, s
, is_extern
);
231 | ENUM_KEYW IDENT enum_body
232 { struct string_list
*s
= *$3, *i
= *$2, *r
;
233 r
= copy_node
(i
); r
->tag
= SYM_ENUM
;
234 r
->next
= (*$1)->next
; *$3 = r
; (*$1)->next
= NULL
;
235 add_symbol
(i
->string, SYM_ENUM
, s
, is_extern
);
239 * Anonymous enum definition. Tell add_symbol() to restart its counter.
241 | ENUM_KEYW enum_body
242 { add_symbol
(NULL
, SYM_ENUM
, NULL
, 0); $$
= $2; }
243 /* Anonymous s/u definitions. Nothing needs doing. */
244 | STRUCT_KEYW class_body
{ $$
= $2; }
245 | UNION_KEYW class_body
{ $$
= $2; }
248 simple_type_specifier:
259 | TYPE
{ (*$1)->tag
= SYM_TYPEDEF
; $$
= $1; }
263 '*' cvar_qualifier_seq_opt
264 { $$
= $2 ?
$2 : $1; }
267 cvar_qualifier_seq_opt:
268 /* empty */ { $$
= NULL
; }
274 | cvar_qualifier_seq cvar_qualifier
{ $$
= $2; }
278 CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
280 { /* restrict has no effect in prototypes so ignore it */
287 ptr_operator declarator
{ $$
= $2; }
293 { if
(current_name
!= NULL
) {
294 error_with_pos
("unexpected second declaration name");
297 current_name
= (*$1)->string;
301 | direct_declarator
'(' parameter_declaration_clause
')'
303 | direct_declarator
'(' error ')'
305 | direct_declarator BRACKET_PHRASE
313 /* Nested declarators differ from regular declarators in that they do
314 not record the symbols they find in the global symbol table. */
316 ptr_operator nested_declarator
{ $$
= $2; }
317 | direct_nested_declarator
320 direct_nested_declarator:
323 | direct_nested_declarator
'(' parameter_declaration_clause
')'
325 | direct_nested_declarator
'(' error ')'
327 | direct_nested_declarator BRACKET_PHRASE
329 |
'(' nested_declarator
')'
335 parameter_declaration_clause:
336 parameter_declaration_list_opt DOTS
{ $$
= $2; }
337 | parameter_declaration_list_opt
338 | parameter_declaration_list
',' DOTS
{ $$
= $3; }
341 parameter_declaration_list_opt:
342 /* empty */ { $$
= NULL
; }
343 | parameter_declaration_list
346 parameter_declaration_list:
347 parameter_declaration
348 | parameter_declaration_list
',' parameter_declaration
352 parameter_declaration:
353 decl_specifier_seq m_abstract_declarator
354 { $$
= $2 ?
$2 : $1; }
357 m_abstract_declarator:
358 ptr_operator m_abstract_declarator
359 { $$
= $2 ?
$2 : $1; }
360 | direct_m_abstract_declarator
363 direct_m_abstract_declarator:
364 /* empty */ { $$
= NULL
; }
366 { /* For version 2 checksums, we don't want to remember
367 private parameter names. */
371 /* This wasn't really a typedef name but an identifier that
377 | direct_m_abstract_declarator
'(' parameter_declaration_clause
')'
379 | direct_m_abstract_declarator
'(' error ')'
381 | direct_m_abstract_declarator BRACKET_PHRASE
383 |
'(' m_abstract_declarator
')'
390 decl_specifier_seq_opt declarator BRACE_PHRASE
391 { struct string_list
*decl
= *$2;
393 add_symbol
(current_name
, SYM_NORMAL
, decl
, is_extern
);
399 /* empty */ { $$
= NULL
; }
403 /* We never care about the contents of an initializer. */
405 '=' EXPRESSION_PHRASE
406 { remove_list
($2, &(*$1)->next
); $$
= $2; }
410 '{' member_specification_opt
'}' { $$
= $3; }
411 |
'{' error '}' { $$
= $3; }
414 member_specification_opt:
415 /* empty */ { $$
= NULL
; }
416 | member_specification
419 member_specification:
421 | member_specification member_declaration
{ $$
= $2; }
425 decl_specifier_seq_opt member_declarator_list_opt
';'
431 member_declarator_list_opt:
432 /* empty */ { $$
= NULL
; }
433 | member_declarator_list
436 member_declarator_list:
438 | member_declarator_list
',' member_declarator
{ $$
= $3; }
442 nested_declarator attribute_opt
{ $$
= $2 ?
$2 : $1; }
443 | IDENT member_bitfield_declarator
{ $$
= $2; }
444 | member_bitfield_declarator
447 member_bitfield_declarator:
448 ':' EXPRESSION_PHRASE
{ $$
= $2; }
452 /* empty */ { $$
= NULL
; }
453 | attribute_opt ATTRIBUTE_PHRASE
457 '{' enumerator_list
'}' { $$
= $3; }
458 |
'{' enumerator_list
',' '}' { $$
= $4; }
463 | enumerator_list
',' enumerator
468 const char *name
= strdup
((*$1)->string);
469 add_symbol
(name
, SYM_ENUM_CONST
, NULL
, 0);
471 | IDENT
'=' EXPRESSION_PHRASE
473 const char *name
= strdup
((*$1)->string);
474 struct string_list
*expr
= copy_list_range
(*$3, *$2);
475 add_symbol
(name
, SYM_ENUM_CONST
, expr
, 0);
479 ASM_PHRASE
';' { $$
= $2; }
483 /* empty */ { $$
= NULL
; }
488 EXPORT_SYMBOL_KEYW
'(' IDENT
')' ';'
489 { export_symbol
((*$3)->string); $$
= $5; }
496 yyerror(const char *e
)
498 error_with_pos
("%s", e
);