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
;
54 /* Record definition of a struct/union/enum */
55 static void record_compound
(struct string_list
**keyw
,
56 struct string_list
**ident
,
57 struct string_list
**body
,
58 enum symbol_type type
)
60 struct string_list
*b
= *body
, *i
= *ident
, *r
;
62 if
(i
->in_source_file
) {
65 remove_list
(body
, ident
);
68 r
= copy_node
(i
); r
->tag
= type
;
69 r
->next
= (*keyw
)->next
; *body
= r
; (*keyw
)->next
= NULL
;
70 add_symbol
(i
->string, type
, b
, is_extern
);
102 %token EXPORT_SYMBOL_KEYW
105 %token ATTRIBUTE_PHRASE
108 %token BRACKET_PHRASE
109 %token EXPRESSION_PHRASE
125 | declaration_seq declaration
129 { is_typedef
= 0; is_extern
= 0; current_name
= NULL
; decl_spec
= NULL
; }
131 { free_list
(*$2, NULL
); *$2 = NULL
; }
135 EXTENSION_KEYW TYPEDEF_KEYW
{ is_typedef
= 1; } simple_declaration
137 | TYPEDEF_KEYW
{ is_typedef
= 1; } simple_declaration
140 | function_definition
143 |
error ';' { $$
= $2; }
144 |
error '}' { $$
= $2; }
148 decl_specifier_seq_opt init_declarator_list_opt
';'
149 { if
(current_name
) {
150 struct string_list
*decl
= (*$3)->next
;
152 add_symbol
(current_name
,
153 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
,
161 init_declarator_list_opt:
162 /* empty */ { $$
= NULL
; }
163 | init_declarator_list
166 init_declarator_list:
168 { struct string_list
*decl
= *$1;
170 add_symbol
(current_name
,
171 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
, decl
, is_extern
);
175 | init_declarator_list
',' init_declarator
176 { struct string_list
*decl
= *$3;
178 free_list
(*$2, NULL
);
180 add_symbol
(current_name
,
181 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
, decl
, is_extern
);
188 declarator asm_phrase_opt attribute_opt initializer_opt
189 { $$
= $4 ?
$4 : $3 ?
$3 : $2 ?
$2 : $1; }
192 /* Hang on to the specifiers so that we can reuse them. */
193 decl_specifier_seq_opt:
194 /* empty */ { decl_spec
= NULL
; }
199 decl_specifier
{ decl_spec
= *$1; }
200 | decl_specifier_seq decl_specifier
{ decl_spec
= *$2; }
204 storage_class_specifier
205 { /* Version 2 checksumming ignores storage class, as that
206 is really irrelevant to the linkage. */
213 storage_class_specifier:
217 | EXTERN_KEYW
{ is_extern
= 1; $$
= $1; }
218 | INLINE_KEYW
{ is_extern
= 0; $$
= $1; }
222 simple_type_specifier
224 | TYPEOF_KEYW
'(' parameter_declaration
')'
227 /* References to s/u/e's defined elsewhere. Rearrange things
228 so that it is easier to expand the definition fully later. */
230 { remove_node
($1); (*$2)->tag
= SYM_STRUCT
; $$
= $2; }
232 { remove_node
($1); (*$2)->tag
= SYM_UNION
; $$
= $2; }
234 { remove_node
($1); (*$2)->tag
= SYM_ENUM
; $$
= $2; }
236 /* Full definitions of an s/u/e. Record it. */
237 | STRUCT_KEYW IDENT class_body
238 { record_compound
($1, $2, $3, SYM_STRUCT
); $$
= $3; }
239 | UNION_KEYW IDENT class_body
240 { record_compound
($1, $2, $3, SYM_UNION
); $$
= $3; }
241 | ENUM_KEYW IDENT enum_body
242 { record_compound
($1, $2, $3, SYM_ENUM
); $$
= $3; }
244 * Anonymous enum definition. Tell add_symbol() to restart its counter.
246 | ENUM_KEYW enum_body
247 { add_symbol
(NULL
, SYM_ENUM
, NULL
, 0); $$
= $2; }
248 /* Anonymous s/u definitions. Nothing needs doing. */
249 | STRUCT_KEYW class_body
{ $$
= $2; }
250 | UNION_KEYW class_body
{ $$
= $2; }
253 simple_type_specifier:
264 | TYPE
{ (*$1)->tag
= SYM_TYPEDEF
; $$
= $1; }
268 '*' cvar_qualifier_seq_opt
269 { $$
= $2 ?
$2 : $1; }
272 cvar_qualifier_seq_opt:
273 /* empty */ { $$
= NULL
; }
279 | cvar_qualifier_seq cvar_qualifier
{ $$
= $2; }
283 CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
285 { /* restrict has no effect in prototypes so ignore it */
292 ptr_operator declarator
{ $$
= $2; }
298 { if
(current_name
!= NULL
) {
299 error_with_pos
("unexpected second declaration name");
302 current_name
= (*$1)->string;
307 { if
(current_name
!= NULL
) {
308 error_with_pos
("unexpected second declaration name");
311 current_name
= (*$1)->string;
315 | direct_declarator
'(' parameter_declaration_clause
')'
317 | direct_declarator
'(' error ')'
319 | direct_declarator BRACKET_PHRASE
327 /* Nested declarators differ from regular declarators in that they do
328 not record the symbols they find in the global symbol table. */
330 ptr_operator nested_declarator
{ $$
= $2; }
331 | direct_nested_declarator
334 direct_nested_declarator:
337 | direct_nested_declarator
'(' parameter_declaration_clause
')'
339 | direct_nested_declarator
'(' error ')'
341 | direct_nested_declarator BRACKET_PHRASE
343 |
'(' nested_declarator
')'
349 parameter_declaration_clause:
350 parameter_declaration_list_opt DOTS
{ $$
= $2; }
351 | parameter_declaration_list_opt
352 | parameter_declaration_list
',' DOTS
{ $$
= $3; }
355 parameter_declaration_list_opt:
356 /* empty */ { $$
= NULL
; }
357 | parameter_declaration_list
360 parameter_declaration_list:
361 parameter_declaration
362 | parameter_declaration_list
',' parameter_declaration
366 parameter_declaration:
367 decl_specifier_seq m_abstract_declarator
368 { $$
= $2 ?
$2 : $1; }
371 m_abstract_declarator:
372 ptr_operator m_abstract_declarator
373 { $$
= $2 ?
$2 : $1; }
374 | direct_m_abstract_declarator
377 direct_m_abstract_declarator:
378 /* empty */ { $$
= NULL
; }
380 { /* For version 2 checksums, we don't want to remember
381 private parameter names. */
385 /* This wasn't really a typedef name but an identifier that
391 | direct_m_abstract_declarator
'(' parameter_declaration_clause
')'
393 | direct_m_abstract_declarator
'(' error ')'
395 | direct_m_abstract_declarator BRACKET_PHRASE
397 |
'(' m_abstract_declarator
')'
404 decl_specifier_seq_opt declarator BRACE_PHRASE
405 { struct string_list
*decl
= *$2;
407 add_symbol
(current_name
, SYM_NORMAL
, decl
, is_extern
);
413 /* empty */ { $$
= NULL
; }
417 /* We never care about the contents of an initializer. */
419 '=' EXPRESSION_PHRASE
420 { remove_list
($2, &(*$1)->next
); $$
= $2; }
424 '{' member_specification_opt
'}' { $$
= $3; }
425 |
'{' error '}' { $$
= $3; }
428 member_specification_opt:
429 /* empty */ { $$
= NULL
; }
430 | member_specification
433 member_specification:
435 | member_specification member_declaration
{ $$
= $2; }
439 decl_specifier_seq_opt member_declarator_list_opt
';'
445 member_declarator_list_opt:
446 /* empty */ { $$
= NULL
; }
447 | member_declarator_list
450 member_declarator_list:
452 | member_declarator_list
',' member_declarator
{ $$
= $3; }
456 nested_declarator attribute_opt
{ $$
= $2 ?
$2 : $1; }
457 | IDENT member_bitfield_declarator
{ $$
= $2; }
458 | member_bitfield_declarator
461 member_bitfield_declarator:
462 ':' EXPRESSION_PHRASE
{ $$
= $2; }
466 /* empty */ { $$
= NULL
; }
467 | attribute_opt ATTRIBUTE_PHRASE
471 '{' enumerator_list
'}' { $$
= $3; }
472 |
'{' enumerator_list
',' '}' { $$
= $4; }
477 | enumerator_list
',' enumerator
482 const char *name
= strdup
((*$1)->string);
483 add_symbol
(name
, SYM_ENUM_CONST
, NULL
, 0);
485 | IDENT
'=' EXPRESSION_PHRASE
487 const char *name
= strdup
((*$1)->string);
488 struct string_list
*expr
= copy_list_range
(*$3, *$2);
489 add_symbol
(name
, SYM_ENUM_CONST
, expr
, 0);
493 ASM_PHRASE
';' { $$
= $2; }
497 /* empty */ { $$
= NULL
; }
502 EXPORT_SYMBOL_KEYW
'(' IDENT
')' ';'
503 { export_symbol
((*$3)->string); $$
= $5; }
510 yyerror(const char *e
)
512 error_with_pos
("%s", e
);