parse.y 12 KB

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