parse.y 10 KB

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