Linux 4.19.133
[linux/fpc-iii.git] / scripts / genksyms / parse.y
blob1ebcf52cd0f9e0402624b748d3818c27d40384ee
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. */
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "genksyms.h"
31 static int is_typedef;
32 static int is_extern;
33 static char *current_name;
34 static struct string_list *decl_spec;
36 static void yyerror(const char *);
38 static inline void
39 remove_node(struct string_list **p)
41 struct string_list *node = *p;
42 *p = node->next;
43 free_node(node);
46 static inline void
47 remove_list(struct string_list **pb, struct string_list **pe)
49 struct string_list *b = *pb, *e = *pe;
50 *pb = e;
51 free_list(b, e);
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) {
63 remove_node(keyw);
64 (*ident)->tag = type;
65 remove_list(body, ident);
66 return;
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);
75 %token ASM_KEYW
76 %token ATTRIBUTE_KEYW
77 %token AUTO_KEYW
78 %token BOOL_KEYW
79 %token BUILTIN_INT_KEYW
80 %token CHAR_KEYW
81 %token CONST_KEYW
82 %token DOUBLE_KEYW
83 %token ENUM_KEYW
84 %token EXTERN_KEYW
85 %token EXTENSION_KEYW
86 %token FLOAT_KEYW
87 %token INLINE_KEYW
88 %token INT_KEYW
89 %token LONG_KEYW
90 %token REGISTER_KEYW
91 %token RESTRICT_KEYW
92 %token SHORT_KEYW
93 %token SIGNED_KEYW
94 %token STATIC_KEYW
95 %token STRUCT_KEYW
96 %token TYPEDEF_KEYW
97 %token UNION_KEYW
98 %token UNSIGNED_KEYW
99 %token VOID_KEYW
100 %token VOLATILE_KEYW
101 %token TYPEOF_KEYW
102 %token VA_LIST_KEYW
104 %token EXPORT_SYMBOL_KEYW
106 %token ASM_PHRASE
107 %token ATTRIBUTE_PHRASE
108 %token TYPEOF_PHRASE
109 %token BRACE_PHRASE
110 %token BRACKET_PHRASE
111 %token EXPRESSION_PHRASE
113 %token CHAR
114 %token DOTS
115 %token IDENT
116 %token INT
117 %token REAL
118 %token STRING
119 %token TYPE
120 %token OTHER
121 %token FILENAME
125 declaration_seq:
126 declaration
127 | declaration_seq declaration
130 declaration:
131 { is_typedef = 0; is_extern = 0; current_name = NULL; decl_spec = NULL; }
132 declaration1
133 { free_list(*$2, NULL); *$2 = NULL; }
136 declaration1:
137 EXTENSION_KEYW TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
138 { $$ = $4; }
139 | TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
140 { $$ = $3; }
141 | simple_declaration
142 | function_definition
143 | asm_definition
144 | export_definition
145 | error ';' { $$ = $2; }
146 | error '}' { $$ = $2; }
149 simple_declaration:
150 decl_specifier_seq_opt init_declarator_list_opt ';'
151 { if (current_name) {
152 struct string_list *decl = (*$3)->next;
153 (*$3)->next = NULL;
154 add_symbol(current_name,
155 is_typedef ? SYM_TYPEDEF : SYM_NORMAL,
156 decl, is_extern);
157 current_name = NULL;
159 $$ = $3;
163 init_declarator_list_opt:
164 /* empty */ { $$ = NULL; }
165 | init_declarator_list
168 init_declarator_list:
169 init_declarator
170 { struct string_list *decl = *$1;
171 *$1 = NULL;
172 add_symbol(current_name,
173 is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
174 current_name = NULL;
175 $$ = $1;
177 | init_declarator_list ',' init_declarator
178 { struct string_list *decl = *$3;
179 *$3 = NULL;
180 free_list(*$2, NULL);
181 *$2 = decl_spec;
182 add_symbol(current_name,
183 is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
184 current_name = NULL;
185 $$ = $3;
189 init_declarator:
190 declarator asm_phrase_opt attribute_opt initializer_opt
191 { $$ = $4 ? $4 : $3 ? $3 : $2 ? $2 : $1; }
194 /* Hang on to the specifiers so that we can reuse them. */
195 decl_specifier_seq_opt:
196 /* empty */ { decl_spec = NULL; }
197 | decl_specifier_seq
200 decl_specifier_seq:
201 decl_specifier { decl_spec = *$1; }
202 | decl_specifier_seq decl_specifier { decl_spec = *$2; }
205 decl_specifier:
206 storage_class_specifier
207 { /* Version 2 checksumming ignores storage class, as that
208 is really irrelevant to the linkage. */
209 remove_node($1);
210 $$ = $1;
212 | type_specifier
215 storage_class_specifier:
216 AUTO_KEYW
217 | REGISTER_KEYW
218 | STATIC_KEYW
219 | EXTERN_KEYW { is_extern = 1; $$ = $1; }
220 | INLINE_KEYW { is_extern = 0; $$ = $1; }
223 type_specifier:
224 simple_type_specifier
225 | cvar_qualifier
226 | TYPEOF_KEYW '(' parameter_declaration ')'
227 | TYPEOF_PHRASE
229 /* References to s/u/e's defined elsewhere. Rearrange things
230 so that it is easier to expand the definition fully later. */
231 | STRUCT_KEYW IDENT
232 { remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
233 | UNION_KEYW IDENT
234 { remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
235 | ENUM_KEYW IDENT
236 { remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
238 /* Full definitions of an s/u/e. Record it. */
239 | STRUCT_KEYW IDENT class_body
240 { record_compound($1, $2, $3, SYM_STRUCT); $$ = $3; }
241 | UNION_KEYW IDENT class_body
242 { record_compound($1, $2, $3, SYM_UNION); $$ = $3; }
243 | ENUM_KEYW IDENT enum_body
244 { record_compound($1, $2, $3, SYM_ENUM); $$ = $3; }
246 * Anonymous enum definition. Tell add_symbol() to restart its counter.
248 | ENUM_KEYW enum_body
249 { add_symbol(NULL, SYM_ENUM, NULL, 0); $$ = $2; }
250 /* Anonymous s/u definitions. Nothing needs doing. */
251 | STRUCT_KEYW class_body { $$ = $2; }
252 | UNION_KEYW class_body { $$ = $2; }
255 simple_type_specifier:
256 CHAR_KEYW
257 | SHORT_KEYW
258 | INT_KEYW
259 | LONG_KEYW
260 | SIGNED_KEYW
261 | UNSIGNED_KEYW
262 | FLOAT_KEYW
263 | DOUBLE_KEYW
264 | VOID_KEYW
265 | BOOL_KEYW
266 | VA_LIST_KEYW
267 | BUILTIN_INT_KEYW
268 | TYPE { (*$1)->tag = SYM_TYPEDEF; $$ = $1; }
271 ptr_operator:
272 '*' cvar_qualifier_seq_opt
273 { $$ = $2 ? $2 : $1; }
276 cvar_qualifier_seq_opt:
277 /* empty */ { $$ = NULL; }
278 | cvar_qualifier_seq
281 cvar_qualifier_seq:
282 cvar_qualifier
283 | cvar_qualifier_seq cvar_qualifier { $$ = $2; }
286 cvar_qualifier:
287 CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
288 | RESTRICT_KEYW
289 { /* restrict has no effect in prototypes so ignore it */
290 remove_node($1);
291 $$ = $1;
295 declarator:
296 ptr_operator declarator { $$ = $2; }
297 | direct_declarator
300 direct_declarator:
301 IDENT
302 { if (current_name != NULL) {
303 error_with_pos("unexpected second declaration name");
304 YYERROR;
305 } else {
306 current_name = (*$1)->string;
307 $$ = $1;
310 | TYPE
311 { if (current_name != NULL) {
312 error_with_pos("unexpected second declaration name");
313 YYERROR;
314 } else {
315 current_name = (*$1)->string;
316 $$ = $1;
319 | direct_declarator '(' parameter_declaration_clause ')'
320 { $$ = $4; }
321 | direct_declarator '(' error ')'
322 { $$ = $4; }
323 | direct_declarator BRACKET_PHRASE
324 { $$ = $2; }
325 | '(' declarator ')'
326 { $$ = $3; }
329 /* Nested declarators differ from regular declarators in that they do
330 not record the symbols they find in the global symbol table. */
331 nested_declarator:
332 ptr_operator nested_declarator { $$ = $2; }
333 | direct_nested_declarator
336 direct_nested_declarator:
337 IDENT
338 | TYPE
339 | direct_nested_declarator '(' parameter_declaration_clause ')'
340 { $$ = $4; }
341 | direct_nested_declarator '(' error ')'
342 { $$ = $4; }
343 | direct_nested_declarator BRACKET_PHRASE
344 { $$ = $2; }
345 | '(' nested_declarator ')'
346 { $$ = $3; }
347 | '(' error ')'
348 { $$ = $3; }
351 parameter_declaration_clause:
352 parameter_declaration_list_opt DOTS { $$ = $2; }
353 | parameter_declaration_list_opt
354 | parameter_declaration_list ',' DOTS { $$ = $3; }
357 parameter_declaration_list_opt:
358 /* empty */ { $$ = NULL; }
359 | parameter_declaration_list
362 parameter_declaration_list:
363 parameter_declaration
364 | parameter_declaration_list ',' parameter_declaration
365 { $$ = $3; }
368 parameter_declaration:
369 decl_specifier_seq m_abstract_declarator
370 { $$ = $2 ? $2 : $1; }
373 m_abstract_declarator:
374 ptr_operator m_abstract_declarator
375 { $$ = $2 ? $2 : $1; }
376 | direct_m_abstract_declarator
379 direct_m_abstract_declarator:
380 /* empty */ { $$ = NULL; }
381 | IDENT
382 { /* For version 2 checksums, we don't want to remember
383 private parameter names. */
384 remove_node($1);
385 $$ = $1;
387 /* This wasn't really a typedef name but an identifier that
388 shadows one. */
389 | TYPE
390 { remove_node($1);
391 $$ = $1;
393 | direct_m_abstract_declarator '(' parameter_declaration_clause ')'
394 { $$ = $4; }
395 | direct_m_abstract_declarator '(' error ')'
396 { $$ = $4; }
397 | direct_m_abstract_declarator BRACKET_PHRASE
398 { $$ = $2; }
399 | '(' m_abstract_declarator ')'
400 { $$ = $3; }
401 | '(' error ')'
402 { $$ = $3; }
405 function_definition:
406 decl_specifier_seq_opt declarator BRACE_PHRASE
407 { struct string_list *decl = *$2;
408 *$2 = NULL;
409 add_symbol(current_name, SYM_NORMAL, decl, is_extern);
410 $$ = $3;
414 initializer_opt:
415 /* empty */ { $$ = NULL; }
416 | initializer
419 /* We never care about the contents of an initializer. */
420 initializer:
421 '=' EXPRESSION_PHRASE
422 { remove_list($2, &(*$1)->next); $$ = $2; }
425 class_body:
426 '{' member_specification_opt '}' { $$ = $3; }
427 | '{' error '}' { $$ = $3; }
430 member_specification_opt:
431 /* empty */ { $$ = NULL; }
432 | member_specification
435 member_specification:
436 member_declaration
437 | member_specification member_declaration { $$ = $2; }
440 member_declaration:
441 decl_specifier_seq_opt member_declarator_list_opt ';'
442 { $$ = $3; }
443 | error ';'
444 { $$ = $2; }
447 member_declarator_list_opt:
448 /* empty */ { $$ = NULL; }
449 | member_declarator_list
452 member_declarator_list:
453 member_declarator
454 | member_declarator_list ',' member_declarator { $$ = $3; }
457 member_declarator:
458 nested_declarator attribute_opt { $$ = $2 ? $2 : $1; }
459 | IDENT member_bitfield_declarator { $$ = $2; }
460 | member_bitfield_declarator
463 member_bitfield_declarator:
464 ':' EXPRESSION_PHRASE { $$ = $2; }
467 attribute_opt:
468 /* empty */ { $$ = NULL; }
469 | attribute_opt ATTRIBUTE_PHRASE
472 enum_body:
473 '{' enumerator_list '}' { $$ = $3; }
474 | '{' enumerator_list ',' '}' { $$ = $4; }
477 enumerator_list:
478 enumerator
479 | enumerator_list ',' enumerator
481 enumerator:
482 IDENT
484 const char *name = strdup((*$1)->string);
485 add_symbol(name, SYM_ENUM_CONST, NULL, 0);
487 | IDENT '=' EXPRESSION_PHRASE
489 const char *name = strdup((*$1)->string);
490 struct string_list *expr = copy_list_range(*$3, *$2);
491 add_symbol(name, SYM_ENUM_CONST, expr, 0);
494 asm_definition:
495 ASM_PHRASE ';' { $$ = $2; }
498 asm_phrase_opt:
499 /* empty */ { $$ = NULL; }
500 | ASM_PHRASE
503 export_definition:
504 EXPORT_SYMBOL_KEYW '(' IDENT ')' ';'
505 { export_symbol((*$3)->string); $$ = $5; }
511 static void
512 yyerror(const char *e)
514 error_with_pos("%s", e);