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