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