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