parse.y 10 KB

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