parse.y 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <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 { free_list(decl_spec, NULL); $$ = $1; }
  138. ;
  139. init_declarator_list:
  140. init_declarator
  141. { struct string_list *decl = *$1;
  142. *$1 = NULL;
  143. /* avoid sharing among multiple init_declarators */
  144. if (decl_spec)
  145. decl_spec = copy_list_range(decl_spec, 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. /* avoid sharing among multiple init_declarators */
  157. if (decl_spec)
  158. decl_spec = copy_list_range(decl_spec, NULL);
  159. add_symbol(current_name,
  160. is_typedef ? SYM_TYPEDEF : SYM_NORMAL, decl, is_extern);
  161. current_name = NULL;
  162. $$ = $3;
  163. }
  164. ;
  165. init_declarator:
  166. declarator asm_phrase_opt attribute_opt initializer_opt
  167. { $$ = $4 ? $4 : $3 ? $3 : $2 ? $2 : $1; }
  168. ;
  169. /* Hang on to the specifiers so that we can reuse them. */
  170. decl_specifier_seq_opt:
  171. /* empty */ { decl_spec = NULL; }
  172. | decl_specifier_seq
  173. ;
  174. decl_specifier_seq:
  175. decl_specifier { decl_spec = *$1; }
  176. | decl_specifier_seq decl_specifier { decl_spec = *$2; }
  177. ;
  178. decl_specifier:
  179. storage_class_specifier
  180. { /* Version 2 checksumming ignores storage class, as that
  181. is really irrelevant to the linkage. */
  182. remove_node($1);
  183. $$ = $1;
  184. }
  185. | type_specifier
  186. ;
  187. storage_class_specifier:
  188. AUTO_KEYW
  189. | REGISTER_KEYW
  190. | STATIC_KEYW
  191. | EXTERN_KEYW { is_extern = 1; $$ = $1; }
  192. | INLINE_KEYW { is_extern = 0; $$ = $1; }
  193. ;
  194. type_specifier:
  195. simple_type_specifier
  196. | cvar_qualifier
  197. | TYPEOF_KEYW '(' parameter_declaration ')'
  198. | TYPEOF_PHRASE
  199. /* References to s/u/e's defined elsewhere. Rearrange things
  200. so that it is easier to expand the definition fully later. */
  201. | STRUCT_KEYW IDENT
  202. { remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
  203. | UNION_KEYW IDENT
  204. { remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
  205. | ENUM_KEYW IDENT
  206. { remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
  207. /* Full definitions of an s/u/e. Record it. */
  208. | STRUCT_KEYW IDENT class_body
  209. { record_compound($1, $2, $3, SYM_STRUCT); $$ = $3; }
  210. | UNION_KEYW IDENT class_body
  211. { record_compound($1, $2, $3, SYM_UNION); $$ = $3; }
  212. | ENUM_KEYW IDENT enum_body
  213. { record_compound($1, $2, $3, SYM_ENUM); $$ = $3; }
  214. /*
  215. * Anonymous enum definition. Tell add_symbol() to restart its counter.
  216. */
  217. | ENUM_KEYW enum_body
  218. { add_symbol(NULL, SYM_ENUM, NULL, 0); $$ = $2; }
  219. /* Anonymous s/u definitions. Nothing needs doing. */
  220. | STRUCT_KEYW class_body { $$ = $2; }
  221. | UNION_KEYW class_body { $$ = $2; }
  222. ;
  223. simple_type_specifier:
  224. CHAR_KEYW
  225. | SHORT_KEYW
  226. | INT_KEYW
  227. | LONG_KEYW
  228. | SIGNED_KEYW
  229. | UNSIGNED_KEYW
  230. | FLOAT_KEYW
  231. | DOUBLE_KEYW
  232. | VOID_KEYW
  233. | BOOL_KEYW
  234. | VA_LIST_KEYW
  235. | BUILTIN_INT_KEYW
  236. | TYPE { (*$1)->tag = SYM_TYPEDEF; $$ = $1; }
  237. ;
  238. ptr_operator:
  239. '*' cvar_qualifier_seq_opt
  240. { $$ = $2 ? $2 : $1; }
  241. ;
  242. cvar_qualifier_seq_opt:
  243. /* empty */ { $$ = NULL; }
  244. | cvar_qualifier_seq
  245. ;
  246. cvar_qualifier_seq:
  247. cvar_qualifier
  248. | cvar_qualifier_seq cvar_qualifier { $$ = $2; }
  249. ;
  250. cvar_qualifier:
  251. CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
  252. | RESTRICT_KEYW
  253. { /* restrict has no effect in prototypes so ignore it */
  254. remove_node($1);
  255. $$ = $1;
  256. }
  257. ;
  258. declarator:
  259. ptr_operator declarator { $$ = $2; }
  260. | direct_declarator
  261. ;
  262. direct_declarator:
  263. IDENT
  264. { if (current_name != NULL) {
  265. error_with_pos("unexpected second declaration name");
  266. YYERROR;
  267. } else {
  268. current_name = (*$1)->string;
  269. $$ = $1;
  270. }
  271. }
  272. | TYPE
  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. }
  281. | direct_declarator '(' parameter_declaration_clause ')'
  282. { $$ = $4; }
  283. | direct_declarator '(' error ')'
  284. { $$ = $4; }
  285. | direct_declarator BRACKET_PHRASE
  286. { $$ = $2; }
  287. | '(' declarator ')'
  288. { $$ = $3; }
  289. ;
  290. /* Nested declarators differ from regular declarators in that they do
  291. not record the symbols they find in the global symbol table. */
  292. nested_declarator:
  293. ptr_operator nested_declarator { $$ = $2; }
  294. | direct_nested_declarator
  295. ;
  296. direct_nested_declarator:
  297. IDENT
  298. | TYPE
  299. | direct_nested_declarator '(' parameter_declaration_clause ')'
  300. { $$ = $4; }
  301. | direct_nested_declarator '(' error ')'
  302. { $$ = $4; }
  303. | direct_nested_declarator BRACKET_PHRASE
  304. { $$ = $2; }
  305. | '(' nested_declarator ')'
  306. { $$ = $3; }
  307. | '(' error ')'
  308. { $$ = $3; }
  309. ;
  310. parameter_declaration_clause:
  311. parameter_declaration_list_opt DOTS { $$ = $2; }
  312. | parameter_declaration_list_opt
  313. | parameter_declaration_list ',' DOTS { $$ = $3; }
  314. ;
  315. parameter_declaration_list_opt:
  316. /* empty */ { $$ = NULL; }
  317. | parameter_declaration_list
  318. ;
  319. parameter_declaration_list:
  320. parameter_declaration
  321. | parameter_declaration_list ',' parameter_declaration
  322. { $$ = $3; }
  323. ;
  324. parameter_declaration:
  325. decl_specifier_seq m_abstract_declarator
  326. { $$ = $2 ? $2 : $1; }
  327. ;
  328. m_abstract_declarator:
  329. ptr_operator m_abstract_declarator
  330. { $$ = $2 ? $2 : $1; }
  331. | direct_m_abstract_declarator
  332. ;
  333. direct_m_abstract_declarator:
  334. /* empty */ { $$ = NULL; }
  335. | IDENT
  336. { /* For version 2 checksums, we don't want to remember
  337. private parameter names. */
  338. remove_node($1);
  339. $$ = $1;
  340. }
  341. /* This wasn't really a typedef name but an identifier that
  342. shadows one. */
  343. | TYPE
  344. { remove_node($1);
  345. $$ = $1;
  346. }
  347. | direct_m_abstract_declarator '(' parameter_declaration_clause ')'
  348. { $$ = $4; }
  349. | direct_m_abstract_declarator '(' error ')'
  350. { $$ = $4; }
  351. | direct_m_abstract_declarator BRACKET_PHRASE
  352. { $$ = $2; }
  353. | '(' m_abstract_declarator ')'
  354. { $$ = $3; }
  355. | '(' error ')'
  356. { $$ = $3; }
  357. ;
  358. function_definition:
  359. decl_specifier_seq_opt declarator BRACE_PHRASE
  360. { struct string_list *decl = *$2;
  361. *$2 = NULL;
  362. add_symbol(current_name, SYM_NORMAL, decl, is_extern);
  363. $$ = $3;
  364. }
  365. ;
  366. initializer_opt:
  367. /* empty */ { $$ = NULL; }
  368. | initializer
  369. ;
  370. /* We never care about the contents of an initializer. */
  371. initializer:
  372. '=' EXPRESSION_PHRASE
  373. { remove_list($2, &(*$1)->next); $$ = $2; }
  374. ;
  375. class_body:
  376. '{' member_specification_opt '}' { $$ = $3; }
  377. | '{' error '}' { $$ = $3; }
  378. ;
  379. member_specification_opt:
  380. /* empty */ { $$ = NULL; }
  381. | member_specification
  382. ;
  383. member_specification:
  384. member_declaration
  385. | member_specification member_declaration { $$ = $2; }
  386. ;
  387. member_declaration:
  388. decl_specifier_seq_opt member_declarator_list_opt ';'
  389. { $$ = $3; }
  390. | error ';'
  391. { $$ = $2; }
  392. ;
  393. member_declarator_list_opt:
  394. /* empty */ { $$ = NULL; }
  395. | member_declarator_list
  396. ;
  397. member_declarator_list:
  398. member_declarator
  399. | member_declarator_list ',' member_declarator { $$ = $3; }
  400. ;
  401. member_declarator:
  402. nested_declarator attribute_opt { $$ = $2 ? $2 : $1; }
  403. | IDENT member_bitfield_declarator { $$ = $2; }
  404. | member_bitfield_declarator
  405. ;
  406. member_bitfield_declarator:
  407. ':' EXPRESSION_PHRASE { $$ = $2; }
  408. ;
  409. attribute_opt:
  410. /* empty */ { $$ = NULL; }
  411. | attribute_opt ATTRIBUTE_PHRASE
  412. ;
  413. enum_body:
  414. '{' enumerator_list '}' { $$ = $3; }
  415. | '{' enumerator_list ',' '}' { $$ = $4; }
  416. ;
  417. enumerator_list:
  418. enumerator
  419. | enumerator_list ',' enumerator
  420. enumerator:
  421. IDENT
  422. {
  423. const char *name = (*$1)->string;
  424. add_symbol(name, SYM_ENUM_CONST, NULL, 0);
  425. }
  426. | IDENT '=' EXPRESSION_PHRASE
  427. {
  428. const char *name = (*$1)->string;
  429. struct string_list *expr = copy_list_range(*$3, *$2);
  430. add_symbol(name, SYM_ENUM_CONST, expr, 0);
  431. }
  432. asm_definition:
  433. ASM_PHRASE ';' { $$ = $2; }
  434. ;
  435. asm_phrase_opt:
  436. /* empty */ { $$ = NULL; }
  437. | ASM_PHRASE
  438. ;
  439. export_definition:
  440. EXPORT_SYMBOL_KEYW '(' IDENT ')' ';'
  441. { export_symbol((*$3)->string); $$ = $5; }
  442. ;
  443. /* Ignore any module scoped _Static_assert(...) */
  444. static_assert:
  445. STATIC_ASSERT_PHRASE ';' { $$ = $2; }
  446. ;
  447. %%
  448. static void
  449. yyerror(const char *e)
  450. {
  451. error_with_pos("%s", e);
  452. }