parse.y 11 KB

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