parse.y 11 KB

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