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
83 %token STATIC_ASSERT_KEYW
93 %token EXPORT_SYMBOL_KEYW
96 %token ATTRIBUTE_PHRASE
100 %token EXPRESSION_PHRASE
101 %token STATIC_ASSERT_PHRASE
117 | declaration_seq declaration
121 { is_typedef = 0; is_extern = 0; current_name = NULL; decl_spec = NULL; }
123 { free_list(*$2, NULL); *$2 = NULL; }
127 EXTENSION_KEYW TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
129 | TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
132 | function_definition
136 | error ';' { $$ = $2; }
137 | error '}' { $$ = $2; }
141 decl_specifier_seq_opt init_declarator_list_opt ';'
142 { if (current_name) {
143 struct string_list *decl = (*$3)->next;
145 add_symbol(current_name,
146 is_typedef ? SYM_TYPEDEF : SYM_NORMAL,
154 init_declarator_list_opt:
155 /* empty */ { $$ = NULL; }
156 | init_declarator_list
159 init_declarator_list:
161 { struct string_list *decl = *$1;
163 add_symbol(current_name,
164 is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
168 | init_declarator_list ',' init_declarator
169 { struct string_list *decl = *$3;
171 free_list(*$2, NULL);
173 add_symbol(current_name,
174 is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
181 declarator asm_phrase_opt attribute_opt initializer_opt
182 { $$ = $4 ? $4 : $3 ? $3 : $2 ? $2 : $1; }
185 /* Hang on to the specifiers so that we can reuse them. */
186 decl_specifier_seq_opt:
187 /* empty */ { decl_spec = NULL; }
192 decl_specifier { decl_spec = *$1; }
193 | decl_specifier_seq decl_specifier { decl_spec = *$2; }
197 storage_class_specifier
198 { /* Version 2 checksumming ignores storage class, as that
199 is really irrelevant to the linkage. */
206 storage_class_specifier:
210 | EXTERN_KEYW { is_extern = 1; $$ = $1; }
211 | INLINE_KEYW { is_extern = 0; $$ = $1; }
215 simple_type_specifier
217 | TYPEOF_KEYW '(' parameter_declaration ')'
220 /* References to s/u/e's defined elsewhere. Rearrange things
221 so that it is easier to expand the definition fully later. */
223 { remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
225 { remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
227 { remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
229 /* Full definitions of an s/u/e. Record it. */
230 | STRUCT_KEYW IDENT class_body
231 { record_compound($1, $2, $3, SYM_STRUCT); $$ = $3; }
232 | UNION_KEYW IDENT class_body
233 { record_compound($1, $2, $3, SYM_UNION); $$ = $3; }
234 | ENUM_KEYW IDENT enum_body
235 { record_compound($1, $2, $3, SYM_ENUM); $$ = $3; }
237 * Anonymous enum definition. Tell add_symbol() to restart its counter.
239 | ENUM_KEYW enum_body
240 { add_symbol(NULL, SYM_ENUM, NULL, 0); $$ = $2; }
241 /* Anonymous s/u definitions. Nothing needs doing. */
242 | STRUCT_KEYW class_body { $$ = $2; }
243 | UNION_KEYW class_body { $$ = $2; }
246 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;
302 { if (current_name != NULL) {
303 error_with_pos("unexpected second declaration name");
306 current_name = (*$1)->string;
310 | direct_declarator '(' parameter_declaration_clause ')'
312 | direct_declarator '(' error ')'
314 | direct_declarator BRACKET_PHRASE
320 /* Nested declarators differ from regular declarators in that they do
321 not record the symbols they find in the global symbol table. */
323 ptr_operator nested_declarator { $$ = $2; }
324 | direct_nested_declarator
327 direct_nested_declarator:
330 | direct_nested_declarator '(' parameter_declaration_clause ')'
332 | direct_nested_declarator '(' error ')'
334 | direct_nested_declarator BRACKET_PHRASE
336 | '(' nested_declarator ')'
342 parameter_declaration_clause:
343 parameter_declaration_list_opt DOTS { $$ = $2; }
344 | parameter_declaration_list_opt
345 | parameter_declaration_list ',' DOTS { $$ = $3; }
348 parameter_declaration_list_opt:
349 /* empty */ { $$ = NULL; }
350 | parameter_declaration_list
353 parameter_declaration_list:
354 parameter_declaration
355 | parameter_declaration_list ',' parameter_declaration
359 parameter_declaration:
360 decl_specifier_seq m_abstract_declarator
361 { $$ = $2 ? $2 : $1; }
364 m_abstract_declarator:
365 ptr_operator m_abstract_declarator
366 { $$ = $2 ? $2 : $1; }
367 | direct_m_abstract_declarator
370 direct_m_abstract_declarator:
371 /* empty */ { $$ = NULL; }
373 { /* For version 2 checksums, we don't want to remember
374 private parameter names. */
378 /* This wasn't really a typedef name but an identifier that
384 | direct_m_abstract_declarator '(' parameter_declaration_clause ')'
386 | direct_m_abstract_declarator '(' error ')'
388 | direct_m_abstract_declarator BRACKET_PHRASE
390 | '(' m_abstract_declarator ')'
397 decl_specifier_seq_opt declarator BRACE_PHRASE
398 { struct string_list *decl = *$2;
400 add_symbol(current_name, SYM_NORMAL, decl, is_extern);
406 /* empty */ { $$ = NULL; }
410 /* We never care about the contents of an initializer. */
412 '=' EXPRESSION_PHRASE
413 { remove_list($2, &(*$1)->next); $$ = $2; }
417 '{' member_specification_opt '}' { $$ = $3; }
418 | '{' error '}' { $$ = $3; }
421 member_specification_opt:
422 /* empty */ { $$ = NULL; }
423 | member_specification
426 member_specification:
428 | member_specification member_declaration { $$ = $2; }
432 decl_specifier_seq_opt member_declarator_list_opt ';'
438 member_declarator_list_opt:
439 /* empty */ { $$ = NULL; }
440 | member_declarator_list
443 member_declarator_list:
445 | member_declarator_list ',' member_declarator { $$ = $3; }
449 nested_declarator attribute_opt { $$ = $2 ? $2 : $1; }
450 | IDENT member_bitfield_declarator { $$ = $2; }
451 | member_bitfield_declarator
454 member_bitfield_declarator:
455 ':' EXPRESSION_PHRASE { $$ = $2; }
459 /* empty */ { $$ = NULL; }
460 | attribute_opt ATTRIBUTE_PHRASE
464 '{' enumerator_list '}' { $$ = $3; }
465 | '{' enumerator_list ',' '}' { $$ = $4; }
470 | enumerator_list ',' enumerator
475 const char *name = strdup((*$1)->string);
476 add_symbol(name, SYM_ENUM_CONST, NULL, 0);
478 | IDENT '=' EXPRESSION_PHRASE
480 const char *name = strdup((*$1)->string);
481 struct string_list *expr = copy_list_range(*$3, *$2);
482 add_symbol(name, SYM_ENUM_CONST, expr, 0);
486 ASM_PHRASE ';' { $$ = $2; }
490 /* empty */ { $$ = NULL; }
495 EXPORT_SYMBOL_KEYW '(' IDENT ')' ';'
496 { export_symbol((*$3)->string); $$ = $5; }
499 /* Ignore any module scoped _Static_assert(...) */
501 STATIC_ASSERT_PHRASE ';' { $$ = $2; }
507 yyerror(const char *e)
509 error_with_pos("%s", e);