1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * C global declaration parser for genksyms.
4 * Copyright 1996, 1997 Linux International.
6 * New implementation contributed by Richard Henderson <rth@tamu.edu>
7 * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
9 * This file is part of the Linux modutils.
19 static int is_typedef
;
21 static char *current_name
;
22 static struct string_list
*decl_spec
;
24 static void yyerror(const char *);
27 remove_node
(struct string_list
**p
)
29 struct string_list
*node
= *p
;
35 remove_list
(struct string_list
**pb
, struct string_list
**pe
)
37 struct string_list
*b
= *pb
, *e
= *pe
;
42 /* Record definition of a struct/union/enum */
43 static void record_compound
(struct string_list
**keyw
,
44 struct string_list
**ident
,
45 struct string_list
**body
,
46 enum symbol_type type
)
48 struct string_list
*b
= *body
, *i
= *ident
, *r
;
50 if
(i
->in_source_file
) {
53 remove_list
(body
, ident
);
56 r
= copy_node
(i
); r
->tag
= type
;
57 r
->next
= (*keyw
)->next
; *body
= r
; (*keyw
)->next
= NULL
;
58 add_symbol
(i
->string, type
, b
, is_extern
);
67 %token BUILTIN_INT_KEYW
92 %token EXPORT_SYMBOL_KEYW
95 %token ATTRIBUTE_PHRASE
99 %token EXPRESSION_PHRASE
115 | declaration_seq declaration
119 { is_typedef
= 0; is_extern
= 0; current_name
= NULL
; decl_spec
= NULL
; }
121 { free_list
(*$2, NULL
); *$2 = NULL
; }
125 EXTENSION_KEYW TYPEDEF_KEYW
{ is_typedef
= 1; } simple_declaration
127 | TYPEDEF_KEYW
{ is_typedef
= 1; } simple_declaration
130 | function_definition
133 |
error ';' { $$
= $2; }
134 |
error '}' { $$
= $2; }
138 decl_specifier_seq_opt init_declarator_list_opt
';'
139 { if
(current_name
) {
140 struct string_list
*decl
= (*$3)->next
;
142 add_symbol
(current_name
,
143 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
,
151 init_declarator_list_opt:
152 /* empty */ { $$
= NULL
; }
153 | init_declarator_list
156 init_declarator_list:
158 { struct string_list
*decl
= *$1;
160 add_symbol
(current_name
,
161 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
, decl
, is_extern
);
165 | init_declarator_list
',' init_declarator
166 { struct string_list
*decl
= *$3;
168 free_list
(*$2, NULL
);
170 add_symbol
(current_name
,
171 is_typedef ? SYM_TYPEDEF
: SYM_NORMAL
, decl
, is_extern
);
178 declarator asm_phrase_opt attribute_opt initializer_opt
179 { $$
= $4 ?
$4 : $3 ?
$3 : $2 ?
$2 : $1; }
182 /* Hang on to the specifiers so that we can reuse them. */
183 decl_specifier_seq_opt:
184 /* empty */ { decl_spec
= NULL
; }
189 decl_specifier
{ decl_spec
= *$1; }
190 | decl_specifier_seq decl_specifier
{ decl_spec
= *$2; }
194 storage_class_specifier
195 { /* Version 2 checksumming ignores storage class, as that
196 is really irrelevant to the linkage. */
203 storage_class_specifier:
207 | EXTERN_KEYW
{ is_extern
= 1; $$
= $1; }
208 | INLINE_KEYW
{ is_extern
= 0; $$
= $1; }
212 simple_type_specifier
214 | TYPEOF_KEYW
'(' parameter_declaration
')'
217 /* References to s/u/e's defined elsewhere. Rearrange things
218 so that it is easier to expand the definition fully later. */
220 { remove_node
($1); (*$2)->tag
= SYM_STRUCT
; $$
= $2; }
222 { remove_node
($1); (*$2)->tag
= SYM_UNION
; $$
= $2; }
224 { remove_node
($1); (*$2)->tag
= SYM_ENUM
; $$
= $2; }
226 /* Full definitions of an s/u/e. Record it. */
227 | STRUCT_KEYW IDENT class_body
228 { record_compound
($1, $2, $3, SYM_STRUCT
); $$
= $3; }
229 | UNION_KEYW IDENT class_body
230 { record_compound
($1, $2, $3, SYM_UNION
); $$
= $3; }
231 | ENUM_KEYW IDENT enum_body
232 { record_compound
($1, $2, $3, SYM_ENUM
); $$
= $3; }
234 * Anonymous enum definition. Tell add_symbol() to restart its counter.
236 | ENUM_KEYW enum_body
237 { add_symbol
(NULL
, SYM_ENUM
, NULL
, 0); $$
= $2; }
238 /* Anonymous s/u definitions. Nothing needs doing. */
239 | STRUCT_KEYW class_body
{ $$
= $2; }
240 | UNION_KEYW class_body
{ $$
= $2; }
243 simple_type_specifier:
256 | TYPE
{ (*$1)->tag
= SYM_TYPEDEF
; $$
= $1; }
260 '*' cvar_qualifier_seq_opt
261 { $$
= $2 ?
$2 : $1; }
264 cvar_qualifier_seq_opt:
265 /* empty */ { $$
= NULL
; }
271 | cvar_qualifier_seq cvar_qualifier
{ $$
= $2; }
275 CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
277 { /* restrict has no effect in prototypes so ignore it */
284 ptr_operator declarator
{ $$
= $2; }
290 { if
(current_name
!= NULL
) {
291 error_with_pos
("unexpected second declaration name");
294 current_name
= (*$1)->string;
299 { if
(current_name
!= NULL
) {
300 error_with_pos
("unexpected second declaration name");
303 current_name
= (*$1)->string;
307 | direct_declarator
'(' parameter_declaration_clause
')'
309 | direct_declarator
'(' error ')'
311 | direct_declarator BRACKET_PHRASE
317 /* Nested declarators differ from regular declarators in that they do
318 not record the symbols they find in the global symbol table. */
320 ptr_operator nested_declarator
{ $$
= $2; }
321 | direct_nested_declarator
324 direct_nested_declarator:
327 | direct_nested_declarator
'(' parameter_declaration_clause
')'
329 | direct_nested_declarator
'(' error ')'
331 | direct_nested_declarator BRACKET_PHRASE
333 |
'(' nested_declarator
')'
339 parameter_declaration_clause:
340 parameter_declaration_list_opt DOTS
{ $$
= $2; }
341 | parameter_declaration_list_opt
342 | parameter_declaration_list
',' DOTS
{ $$
= $3; }
345 parameter_declaration_list_opt:
346 /* empty */ { $$
= NULL
; }
347 | parameter_declaration_list
350 parameter_declaration_list:
351 parameter_declaration
352 | parameter_declaration_list
',' parameter_declaration
356 parameter_declaration:
357 decl_specifier_seq m_abstract_declarator
358 { $$
= $2 ?
$2 : $1; }
361 m_abstract_declarator:
362 ptr_operator m_abstract_declarator
363 { $$
= $2 ?
$2 : $1; }
364 | direct_m_abstract_declarator
367 direct_m_abstract_declarator:
368 /* empty */ { $$
= NULL
; }
370 { /* For version 2 checksums, we don't want to remember
371 private parameter names. */
375 /* This wasn't really a typedef name but an identifier that
381 | direct_m_abstract_declarator
'(' parameter_declaration_clause
')'
383 | direct_m_abstract_declarator
'(' error ')'
385 | direct_m_abstract_declarator BRACKET_PHRASE
387 |
'(' m_abstract_declarator
')'
394 decl_specifier_seq_opt declarator BRACE_PHRASE
395 { struct string_list
*decl
= *$2;
397 add_symbol
(current_name
, SYM_NORMAL
, decl
, is_extern
);
403 /* empty */ { $$
= NULL
; }
407 /* We never care about the contents of an initializer. */
409 '=' EXPRESSION_PHRASE
410 { remove_list
($2, &(*$1)->next
); $$
= $2; }
414 '{' member_specification_opt
'}' { $$
= $3; }
415 |
'{' error '}' { $$
= $3; }
418 member_specification_opt:
419 /* empty */ { $$
= NULL
; }
420 | member_specification
423 member_specification:
425 | member_specification member_declaration
{ $$
= $2; }
429 decl_specifier_seq_opt member_declarator_list_opt
';'
435 member_declarator_list_opt:
436 /* empty */ { $$
= NULL
; }
437 | member_declarator_list
440 member_declarator_list:
442 | member_declarator_list
',' member_declarator
{ $$
= $3; }
446 nested_declarator attribute_opt
{ $$
= $2 ?
$2 : $1; }
447 | IDENT member_bitfield_declarator
{ $$
= $2; }
448 | member_bitfield_declarator
451 member_bitfield_declarator:
452 ':' EXPRESSION_PHRASE
{ $$
= $2; }
456 /* empty */ { $$
= NULL
; }
457 | attribute_opt ATTRIBUTE_PHRASE
461 '{' enumerator_list
'}' { $$
= $3; }
462 |
'{' enumerator_list
',' '}' { $$
= $4; }
467 | enumerator_list
',' enumerator
472 const char *name
= strdup
((*$1)->string);
473 add_symbol
(name
, SYM_ENUM_CONST
, NULL
, 0);
475 | IDENT
'=' EXPRESSION_PHRASE
477 const char *name
= strdup
((*$1)->string);
478 struct string_list
*expr
= copy_list_range
(*$3, *$2);
479 add_symbol
(name
, SYM_ENUM_CONST
, expr
, 0);
483 ASM_PHRASE
';' { $$
= $2; }
487 /* empty */ { $$
= NULL
; }
492 EXPORT_SYMBOL_KEYW
'(' IDENT
')' ';'
493 { export_symbol
((*$3)->string); $$
= $5; }
500 yyerror(const char *e
)
502 error_with_pos
("%s", e
);