GNU Linux-libre 4.4.283-gnu1
[releases.git] / scripts / genksyms / parse.y
1 /* C global declaration parser for genksyms.
2    Copyright 1996, 1997 Linux International.
3
4    New implementation contributed by Richard Henderson <rth@tamu.edu>
5    Based on original work by Bjorn Ekwall <bj0rn@blox.se>
6
7    This file is part of the Linux modutils.
8
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.
13
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.
18
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.  */
22
23
24 %{
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "genksyms.h"
30
31 static int is_typedef;
32 static int is_extern;
33 static char *current_name;
34 static struct string_list *decl_spec;
35
36 static void yyerror(const char *);
37
38 static inline void
39 remove_node(struct string_list **p)
40 {
41   struct string_list *node = *p;
42   *p = node->next;
43   free_node(node);
44 }
45
46 static inline void
47 remove_list(struct string_list **pb, struct string_list **pe)
48 {
49   struct string_list *b = *pb, *e = *pe;
50   *pb = e;
51   free_list(b, e);
52 }
53
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)
59 {
60         struct string_list *b = *body, *i = *ident, *r;
61
62         if (i->in_source_file) {
63                 remove_node(keyw);
64                 (*ident)->tag = type;
65                 remove_list(body, ident);
66                 return;
67         }
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);
71 }
72
73 %}
74
75 %token ASM_KEYW
76 %token ATTRIBUTE_KEYW
77 %token AUTO_KEYW
78 %token BOOL_KEYW
79 %token CHAR_KEYW
80 %token CONST_KEYW
81 %token DOUBLE_KEYW
82 %token ENUM_KEYW
83 %token EXTERN_KEYW
84 %token EXTENSION_KEYW
85 %token FLOAT_KEYW
86 %token INLINE_KEYW
87 %token INT_KEYW
88 %token LONG_KEYW
89 %token REGISTER_KEYW
90 %token RESTRICT_KEYW
91 %token SHORT_KEYW
92 %token SIGNED_KEYW
93 %token STATIC_KEYW
94 %token STRUCT_KEYW
95 %token TYPEDEF_KEYW
96 %token UNION_KEYW
97 %token UNSIGNED_KEYW
98 %token VOID_KEYW
99 %token VOLATILE_KEYW
100 %token TYPEOF_KEYW
101
102 %token EXPORT_SYMBOL_KEYW
103
104 %token ASM_PHRASE
105 %token ATTRIBUTE_PHRASE
106 %token TYPEOF_PHRASE
107 %token BRACE_PHRASE
108 %token BRACKET_PHRASE
109 %token EXPRESSION_PHRASE
110
111 %token CHAR
112 %token DOTS
113 %token IDENT
114 %token INT
115 %token REAL
116 %token STRING
117 %token TYPE
118 %token OTHER
119 %token FILENAME
120
121 %%
122
123 declaration_seq:
124         declaration
125         | declaration_seq declaration
126         ;
127
128 declaration:
129         { is_typedef = 0; is_extern = 0; current_name = NULL; decl_spec = NULL; }
130         declaration1
131         { free_list(*$2, NULL); *$2 = NULL; }
132         ;
133
134 declaration1:
135         EXTENSION_KEYW TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
136                 { $$ = $4; }
137         | TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
138                 { $$ = $3; }
139         | simple_declaration
140         | function_definition
141         | asm_definition
142         | export_definition
143         | error ';'                             { $$ = $2; }
144         | error '}'                             { $$ = $2; }
145         ;
146
147 simple_declaration:
148         decl_specifier_seq_opt init_declarator_list_opt ';'
149                 { if (current_name) {
150                     struct string_list *decl = (*$3)->next;
151                     (*$3)->next = NULL;
152                     add_symbol(current_name,
153                                is_typedef ? SYM_TYPEDEF : SYM_NORMAL,
154                                decl, is_extern);
155                     current_name = NULL;
156                   }
157                   $$ = $3;
158                 }
159         ;
160
161 init_declarator_list_opt:
162         /* empty */                             { $$ = NULL; }
163         | init_declarator_list
164         ;
165
166 init_declarator_list:
167         init_declarator
168                 { struct string_list *decl = *$1;
169                   *$1 = NULL;
170                   add_symbol(current_name,
171                              is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
172                   current_name = NULL;
173                   $$ = $1;
174                 }
175         | init_declarator_list ',' init_declarator
176                 { struct string_list *decl = *$3;
177                   *$3 = NULL;
178                   free_list(*$2, NULL);
179                   *$2 = decl_spec;
180                   add_symbol(current_name,
181                              is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
182                   current_name = NULL;
183                   $$ = $3;
184                 }
185         ;
186
187 init_declarator:
188         declarator asm_phrase_opt attribute_opt initializer_opt
189                 { $$ = $4 ? $4 : $3 ? $3 : $2 ? $2 : $1; }
190         ;
191
192 /* Hang on to the specifiers so that we can reuse them.  */
193 decl_specifier_seq_opt:
194         /* empty */                             { decl_spec = NULL; }
195         | decl_specifier_seq
196         ;
197
198 decl_specifier_seq:
199         decl_specifier                          { decl_spec = *$1; }
200         | decl_specifier_seq decl_specifier     { decl_spec = *$2; }
201         ;
202
203 decl_specifier:
204         storage_class_specifier
205                 { /* Version 2 checksumming ignores storage class, as that
206                      is really irrelevant to the linkage.  */
207                   remove_node($1);
208                   $$ = $1;
209                 }
210         | type_specifier
211         ;
212
213 storage_class_specifier:
214         AUTO_KEYW
215         | REGISTER_KEYW
216         | STATIC_KEYW
217         | EXTERN_KEYW   { is_extern = 1; $$ = $1; }
218         | INLINE_KEYW   { is_extern = 0; $$ = $1; }
219         ;
220
221 type_specifier:
222         simple_type_specifier
223         | cvar_qualifier
224         | TYPEOF_KEYW '(' parameter_declaration ')'
225         | TYPEOF_PHRASE
226
227         /* References to s/u/e's defined elsewhere.  Rearrange things
228            so that it is easier to expand the definition fully later.  */
229         | STRUCT_KEYW IDENT
230                 { remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
231         | UNION_KEYW IDENT
232                 { remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
233         | ENUM_KEYW IDENT
234                 { remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
235
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; }
243         /*
244          * Anonymous enum definition. Tell add_symbol() to restart its counter.
245          */
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; }
251         ;
252
253 simple_type_specifier:
254         CHAR_KEYW
255         | SHORT_KEYW
256         | INT_KEYW
257         | LONG_KEYW
258         | SIGNED_KEYW
259         | UNSIGNED_KEYW
260         | FLOAT_KEYW
261         | DOUBLE_KEYW
262         | VOID_KEYW
263         | BOOL_KEYW
264         | TYPE                  { (*$1)->tag = SYM_TYPEDEF; $$ = $1; }
265         ;
266
267 ptr_operator:
268         '*' cvar_qualifier_seq_opt
269                 { $$ = $2 ? $2 : $1; }
270         ;
271
272 cvar_qualifier_seq_opt:
273         /* empty */                                     { $$ = NULL; }
274         | cvar_qualifier_seq
275         ;
276
277 cvar_qualifier_seq:
278         cvar_qualifier
279         | cvar_qualifier_seq cvar_qualifier             { $$ = $2; }
280         ;
281
282 cvar_qualifier:
283         CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
284         | RESTRICT_KEYW
285                 { /* restrict has no effect in prototypes so ignore it */
286                   remove_node($1);
287                   $$ = $1;
288                 }
289         ;
290
291 declarator:
292         ptr_operator declarator                 { $$ = $2; }
293         | direct_declarator
294         ;
295
296 direct_declarator:
297         IDENT
298                 { if (current_name != NULL) {
299                     error_with_pos("unexpected second declaration name");
300                     YYERROR;
301                   } else {
302                     current_name = (*$1)->string;
303                     $$ = $1;
304                   }
305                 }
306         | TYPE
307                 { if (current_name != NULL) {
308                     error_with_pos("unexpected second declaration name");
309                     YYERROR;
310                   } else {
311                     current_name = (*$1)->string;
312                     $$ = $1;
313                   }
314                 }
315         | direct_declarator '(' parameter_declaration_clause ')'
316                 { $$ = $4; }
317         | direct_declarator '(' error ')'
318                 { $$ = $4; }
319         | direct_declarator BRACKET_PHRASE
320                 { $$ = $2; }
321         | '(' declarator ')'
322                 { $$ = $3; }
323         ;
324
325 /* Nested declarators differ from regular declarators in that they do
326    not record the symbols they find in the global symbol table.  */
327 nested_declarator:
328         ptr_operator nested_declarator          { $$ = $2; }
329         | direct_nested_declarator
330         ;
331
332 direct_nested_declarator:
333         IDENT
334         | TYPE
335         | direct_nested_declarator '(' parameter_declaration_clause ')'
336                 { $$ = $4; }
337         | direct_nested_declarator '(' error ')'
338                 { $$ = $4; }
339         | direct_nested_declarator BRACKET_PHRASE
340                 { $$ = $2; }
341         | '(' nested_declarator ')'
342                 { $$ = $3; }
343         | '(' error ')'
344                 { $$ = $3; }
345         ;
346
347 parameter_declaration_clause:
348         parameter_declaration_list_opt DOTS             { $$ = $2; }
349         | parameter_declaration_list_opt
350         | parameter_declaration_list ',' DOTS           { $$ = $3; }
351         ;
352
353 parameter_declaration_list_opt:
354         /* empty */                                     { $$ = NULL; }
355         | parameter_declaration_list
356         ;
357
358 parameter_declaration_list:
359         parameter_declaration
360         | parameter_declaration_list ',' parameter_declaration
361                 { $$ = $3; }
362         ;
363
364 parameter_declaration:
365         decl_specifier_seq m_abstract_declarator
366                 { $$ = $2 ? $2 : $1; }
367         ;
368
369 m_abstract_declarator:
370         ptr_operator m_abstract_declarator
371                 { $$ = $2 ? $2 : $1; }
372         | direct_m_abstract_declarator
373         ;
374
375 direct_m_abstract_declarator:
376         /* empty */                                     { $$ = NULL; }
377         | IDENT
378                 { /* For version 2 checksums, we don't want to remember
379                      private parameter names.  */
380                   remove_node($1);
381                   $$ = $1;
382                 }
383         /* This wasn't really a typedef name but an identifier that
384            shadows one.  */
385         | TYPE
386                 { remove_node($1);
387                   $$ = $1;
388                 }
389         | direct_m_abstract_declarator '(' parameter_declaration_clause ')'
390                 { $$ = $4; }
391         | direct_m_abstract_declarator '(' error ')'
392                 { $$ = $4; }
393         | direct_m_abstract_declarator BRACKET_PHRASE
394                 { $$ = $2; }
395         | '(' m_abstract_declarator ')'
396                 { $$ = $3; }
397         | '(' error ')'
398                 { $$ = $3; }
399         ;
400
401 function_definition:
402         decl_specifier_seq_opt declarator BRACE_PHRASE
403                 { struct string_list *decl = *$2;
404                   *$2 = NULL;
405                   add_symbol(current_name, SYM_NORMAL, decl, is_extern);
406                   $$ = $3;
407                 }
408         ;
409
410 initializer_opt:
411         /* empty */                                     { $$ = NULL; }
412         | initializer
413         ;
414
415 /* We never care about the contents of an initializer.  */
416 initializer:
417         '=' EXPRESSION_PHRASE
418                 { remove_list($2, &(*$1)->next); $$ = $2; }
419         ;
420
421 class_body:
422         '{' member_specification_opt '}'                { $$ = $3; }
423         | '{' error '}'                                 { $$ = $3; }
424         ;
425
426 member_specification_opt:
427         /* empty */                                     { $$ = NULL; }
428         | member_specification
429         ;
430
431 member_specification:
432         member_declaration
433         | member_specification member_declaration       { $$ = $2; }
434         ;
435
436 member_declaration:
437         decl_specifier_seq_opt member_declarator_list_opt ';'
438                 { $$ = $3; }
439         | error ';'
440                 { $$ = $2; }
441         ;
442
443 member_declarator_list_opt:
444         /* empty */                                     { $$ = NULL; }
445         | member_declarator_list
446         ;
447
448 member_declarator_list:
449         member_declarator
450         | member_declarator_list ',' member_declarator  { $$ = $3; }
451         ;
452
453 member_declarator:
454         nested_declarator attribute_opt                 { $$ = $2 ? $2 : $1; }
455         | IDENT member_bitfield_declarator              { $$ = $2; }
456         | member_bitfield_declarator
457         ;
458
459 member_bitfield_declarator:
460         ':' EXPRESSION_PHRASE                           { $$ = $2; }
461         ;
462
463 attribute_opt:
464         /* empty */                                     { $$ = NULL; }
465         | attribute_opt ATTRIBUTE_PHRASE
466         ;
467
468 enum_body:
469         '{' enumerator_list '}'                         { $$ = $3; }
470         | '{' enumerator_list ',' '}'                   { $$ = $4; }
471          ;
472
473 enumerator_list:
474         enumerator
475         | enumerator_list ',' enumerator
476
477 enumerator:
478         IDENT
479                 {
480                         const char *name = strdup((*$1)->string);
481                         add_symbol(name, SYM_ENUM_CONST, NULL, 0);
482                 }
483         | IDENT '=' EXPRESSION_PHRASE
484                 {
485                         const char *name = strdup((*$1)->string);
486                         struct string_list *expr = copy_list_range(*$3, *$2);
487                         add_symbol(name, SYM_ENUM_CONST, expr, 0);
488                 }
489
490 asm_definition:
491         ASM_PHRASE ';'                                  { $$ = $2; }
492         ;
493
494 asm_phrase_opt:
495         /* empty */                                     { $$ = NULL; }
496         | ASM_PHRASE
497         ;
498
499 export_definition:
500         EXPORT_SYMBOL_KEYW '(' IDENT ')' ';'
501                 { export_symbol((*$3)->string); $$ = $5; }
502         ;
503
504
505 %%
506
507 static void
508 yyerror(const char *e)
509 {
510   error_with_pos("%s", e);
511 }