xref: /linux/scripts/genksyms/parse.y (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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 %token VA_LIST_KEYW
102 
103 %token EXPORT_SYMBOL_KEYW
104 
105 %token ASM_PHRASE
106 %token ATTRIBUTE_PHRASE
107 %token TYPEOF_PHRASE
108 %token BRACE_PHRASE
109 %token BRACKET_PHRASE
110 %token EXPRESSION_PHRASE
111 
112 %token CHAR
113 %token DOTS
114 %token IDENT
115 %token INT
116 %token REAL
117 %token STRING
118 %token TYPE
119 %token OTHER
120 %token FILENAME
121 
122 %%
123 
124 declaration_seq:
125 	declaration
126 	| declaration_seq declaration
127 	;
128 
129 declaration:
130 	{ is_typedef = 0; is_extern = 0; current_name = NULL; decl_spec = NULL; }
131 	declaration1
132 	{ free_list(*$2, NULL); *$2 = NULL; }
133 	;
134 
135 declaration1:
136 	EXTENSION_KEYW TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
137 		{ $$ = $4; }
138 	| TYPEDEF_KEYW { is_typedef = 1; } simple_declaration
139 		{ $$ = $3; }
140 	| simple_declaration
141 	| function_definition
142 	| asm_definition
143 	| export_definition
144 	| error ';'				{ $$ = $2; }
145 	| error '}'				{ $$ = $2; }
146 	;
147 
148 simple_declaration:
149 	decl_specifier_seq_opt init_declarator_list_opt ';'
150 		{ if (current_name) {
151 		    struct string_list *decl = (*$3)->next;
152 		    (*$3)->next = NULL;
153 		    add_symbol(current_name,
154 			       is_typedef ? SYM_TYPEDEF : SYM_NORMAL,
155 			       decl, is_extern);
156 		    current_name = NULL;
157 		  }
158 		  $$ = $3;
159 		}
160 	;
161 
162 init_declarator_list_opt:
163 	/* empty */				{ $$ = NULL; }
164 	| init_declarator_list
165 	;
166 
167 init_declarator_list:
168 	init_declarator
169 		{ struct string_list *decl = *$1;
170 		  *$1 = NULL;
171 		  add_symbol(current_name,
172 			     is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
173 		  current_name = NULL;
174 		  $$ = $1;
175 		}
176 	| init_declarator_list ',' init_declarator
177 		{ struct string_list *decl = *$3;
178 		  *$3 = NULL;
179 		  free_list(*$2, NULL);
180 		  *$2 = decl_spec;
181 		  add_symbol(current_name,
182 			     is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
183 		  current_name = NULL;
184 		  $$ = $3;
185 		}
186 	;
187 
188 init_declarator:
189 	declarator asm_phrase_opt attribute_opt initializer_opt
190 		{ $$ = $4 ? $4 : $3 ? $3 : $2 ? $2 : $1; }
191 	;
192 
193 /* Hang on to the specifiers so that we can reuse them.  */
194 decl_specifier_seq_opt:
195 	/* empty */				{ decl_spec = NULL; }
196 	| decl_specifier_seq
197 	;
198 
199 decl_specifier_seq:
200 	decl_specifier				{ decl_spec = *$1; }
201 	| decl_specifier_seq decl_specifier	{ decl_spec = *$2; }
202 	;
203 
204 decl_specifier:
205 	storage_class_specifier
206 		{ /* Version 2 checksumming ignores storage class, as that
207 		     is really irrelevant to the linkage.  */
208 		  remove_node($1);
209 		  $$ = $1;
210 		}
211 	| type_specifier
212 	;
213 
214 storage_class_specifier:
215 	AUTO_KEYW
216 	| REGISTER_KEYW
217 	| STATIC_KEYW
218 	| EXTERN_KEYW	{ is_extern = 1; $$ = $1; }
219 	| INLINE_KEYW	{ is_extern = 0; $$ = $1; }
220 	;
221 
222 type_specifier:
223 	simple_type_specifier
224 	| cvar_qualifier
225 	| TYPEOF_KEYW '(' parameter_declaration ')'
226 	| TYPEOF_PHRASE
227 
228 	/* References to s/u/e's defined elsewhere.  Rearrange things
229 	   so that it is easier to expand the definition fully later.  */
230 	| STRUCT_KEYW IDENT
231 		{ remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
232 	| UNION_KEYW IDENT
233 		{ remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
234 	| ENUM_KEYW IDENT
235 		{ remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
236 
237 	/* Full definitions of an s/u/e.  Record it.  */
238 	| STRUCT_KEYW IDENT class_body
239 		{ record_compound($1, $2, $3, SYM_STRUCT); $$ = $3; }
240 	| UNION_KEYW IDENT class_body
241 		{ record_compound($1, $2, $3, SYM_UNION); $$ = $3; }
242 	| ENUM_KEYW IDENT enum_body
243 		{ record_compound($1, $2, $3, SYM_ENUM); $$ = $3; }
244 	/*
245 	 * Anonymous enum definition. Tell add_symbol() to restart its counter.
246 	 */
247 	| ENUM_KEYW enum_body
248 		{ add_symbol(NULL, SYM_ENUM, NULL, 0); $$ = $2; }
249 	/* Anonymous s/u definitions.  Nothing needs doing.  */
250 	| STRUCT_KEYW class_body			{ $$ = $2; }
251 	| UNION_KEYW class_body				{ $$ = $2; }
252 	;
253 
254 simple_type_specifier:
255 	CHAR_KEYW
256 	| SHORT_KEYW
257 	| INT_KEYW
258 	| LONG_KEYW
259 	| SIGNED_KEYW
260 	| UNSIGNED_KEYW
261 	| FLOAT_KEYW
262 	| DOUBLE_KEYW
263 	| VOID_KEYW
264 	| BOOL_KEYW
265 	| VA_LIST_KEYW
266 	| TYPE			{ (*$1)->tag = SYM_TYPEDEF; $$ = $1; }
267 	;
268 
269 ptr_operator:
270 	'*' cvar_qualifier_seq_opt
271 		{ $$ = $2 ? $2 : $1; }
272 	;
273 
274 cvar_qualifier_seq_opt:
275 	/* empty */					{ $$ = NULL; }
276 	| cvar_qualifier_seq
277 	;
278 
279 cvar_qualifier_seq:
280 	cvar_qualifier
281 	| cvar_qualifier_seq cvar_qualifier		{ $$ = $2; }
282 	;
283 
284 cvar_qualifier:
285 	CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
286 	| RESTRICT_KEYW
287 		{ /* restrict has no effect in prototypes so ignore it */
288 		  remove_node($1);
289 		  $$ = $1;
290 		}
291 	;
292 
293 declarator:
294 	ptr_operator declarator			{ $$ = $2; }
295 	| direct_declarator
296 	;
297 
298 direct_declarator:
299 	IDENT
300 		{ if (current_name != NULL) {
301 		    error_with_pos("unexpected second declaration name");
302 		    YYERROR;
303 		  } else {
304 		    current_name = (*$1)->string;
305 		    $$ = $1;
306 		  }
307 		}
308 	| TYPE
309 		{ if (current_name != NULL) {
310 		    error_with_pos("unexpected second declaration name");
311 		    YYERROR;
312 		  } else {
313 		    current_name = (*$1)->string;
314 		    $$ = $1;
315 		  }
316 		}
317 	| direct_declarator '(' parameter_declaration_clause ')'
318 		{ $$ = $4; }
319 	| direct_declarator '(' error ')'
320 		{ $$ = $4; }
321 	| direct_declarator BRACKET_PHRASE
322 		{ $$ = $2; }
323 	| '(' declarator ')'
324 		{ $$ = $3; }
325 	| '(' error ')'
326 		{ $$ = $3; }
327 	;
328 
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
334 	;
335 
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; }
349 	;
350 
351 parameter_declaration_clause:
352 	parameter_declaration_list_opt DOTS		{ $$ = $2; }
353 	| parameter_declaration_list_opt
354 	| parameter_declaration_list ',' DOTS		{ $$ = $3; }
355 	;
356 
357 parameter_declaration_list_opt:
358 	/* empty */					{ $$ = NULL; }
359 	| parameter_declaration_list
360 	;
361 
362 parameter_declaration_list:
363 	parameter_declaration
364 	| parameter_declaration_list ',' parameter_declaration
365 		{ $$ = $3; }
366 	;
367 
368 parameter_declaration:
369 	decl_specifier_seq m_abstract_declarator
370 		{ $$ = $2 ? $2 : $1; }
371 	;
372 
373 m_abstract_declarator:
374 	ptr_operator m_abstract_declarator
375 		{ $$ = $2 ? $2 : $1; }
376 	| direct_m_abstract_declarator
377 	;
378 
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;
386 		}
387 	/* This wasn't really a typedef name but an identifier that
388 	   shadows one.  */
389 	| TYPE
390 		{ remove_node($1);
391 		  $$ = $1;
392 		}
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; }
403 	;
404 
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;
411 		}
412 	;
413 
414 initializer_opt:
415 	/* empty */					{ $$ = NULL; }
416 	| initializer
417 	;
418 
419 /* We never care about the contents of an initializer.  */
420 initializer:
421 	'=' EXPRESSION_PHRASE
422 		{ remove_list($2, &(*$1)->next); $$ = $2; }
423 	;
424 
425 class_body:
426 	'{' member_specification_opt '}'		{ $$ = $3; }
427 	| '{' error '}'					{ $$ = $3; }
428 	;
429 
430 member_specification_opt:
431 	/* empty */					{ $$ = NULL; }
432 	| member_specification
433 	;
434 
435 member_specification:
436 	member_declaration
437 	| member_specification member_declaration	{ $$ = $2; }
438 	;
439 
440 member_declaration:
441 	decl_specifier_seq_opt member_declarator_list_opt ';'
442 		{ $$ = $3; }
443 	| error ';'
444 		{ $$ = $2; }
445 	;
446 
447 member_declarator_list_opt:
448 	/* empty */					{ $$ = NULL; }
449 	| member_declarator_list
450 	;
451 
452 member_declarator_list:
453 	member_declarator
454 	| member_declarator_list ',' member_declarator	{ $$ = $3; }
455 	;
456 
457 member_declarator:
458 	nested_declarator attribute_opt			{ $$ = $2 ? $2 : $1; }
459 	| IDENT member_bitfield_declarator		{ $$ = $2; }
460 	| member_bitfield_declarator
461 	;
462 
463 member_bitfield_declarator:
464 	':' EXPRESSION_PHRASE				{ $$ = $2; }
465 	;
466 
467 attribute_opt:
468 	/* empty */					{ $$ = NULL; }
469 	| attribute_opt ATTRIBUTE_PHRASE
470 	;
471 
472 enum_body:
473 	'{' enumerator_list '}'				{ $$ = $3; }
474 	| '{' enumerator_list ',' '}'			{ $$ = $4; }
475 	 ;
476 
477 enumerator_list:
478 	enumerator
479 	| enumerator_list ',' enumerator
480 
481 enumerator:
482 	IDENT
483 		{
484 			const char *name = strdup((*$1)->string);
485 			add_symbol(name, SYM_ENUM_CONST, NULL, 0);
486 		}
487 	| IDENT '=' EXPRESSION_PHRASE
488 		{
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);
492 		}
493 
494 asm_definition:
495 	ASM_PHRASE ';'					{ $$ = $2; }
496 	;
497 
498 asm_phrase_opt:
499 	/* empty */					{ $$ = NULL; }
500 	| ASM_PHRASE
501 	;
502 
503 export_definition:
504 	EXPORT_SYMBOL_KEYW '(' IDENT ')' ';'
505 		{ export_symbol((*$3)->string); $$ = $5; }
506 	;
507 
508 
509 %%
510 
511 static void
512 yyerror(const char *e)
513 {
514   error_with_pos("%s", e);
515 }
516