parse.y 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. | TYPEOF_KEYW '(' decl_specifier_seq ')'
  172. /* References to s/u/e's defined elsewhere. Rearrange things
  173. so that it is easier to expand the definition fully later. */
  174. | STRUCT_KEYW IDENT
  175. { remove_node($1); (*$2)->tag = SYM_STRUCT; $$ = $2; }
  176. | UNION_KEYW IDENT
  177. { remove_node($1); (*$2)->tag = SYM_UNION; $$ = $2; }
  178. | ENUM_KEYW IDENT
  179. { remove_node($1); (*$2)->tag = SYM_ENUM; $$ = $2; }
  180. /* Full definitions of an s/u/e. Record it. */
  181. | STRUCT_KEYW IDENT class_body
  182. { struct string_list *s = *$3, *i = *$2, *r;
  183. r = copy_node(i); r->tag = SYM_STRUCT;
  184. r->next = (*$1)->next; *$3 = r; (*$1)->next = NULL;
  185. add_symbol(i->string, SYM_STRUCT, s, is_extern);
  186. $$ = $3;
  187. }
  188. | UNION_KEYW IDENT class_body
  189. { struct string_list *s = *$3, *i = *$2, *r;
  190. r = copy_node(i); r->tag = SYM_UNION;
  191. r->next = (*$1)->next; *$3 = r; (*$1)->next = NULL;
  192. add_symbol(i->string, SYM_UNION, s, is_extern);
  193. $$ = $3;
  194. }
  195. | ENUM_KEYW IDENT BRACE_PHRASE
  196. { struct string_list *s = *$3, *i = *$2, *r;
  197. r = copy_node(i); r->tag = SYM_ENUM;
  198. r->next = (*$1)->next; *$3 = r; (*$1)->next = NULL;
  199. add_symbol(i->string, SYM_ENUM, s, is_extern);
  200. $$ = $3;
  201. }
  202. /* Anonymous s/u/e definitions. Nothing needs doing. */
  203. | ENUM_KEYW BRACE_PHRASE { $$ = $2; }
  204. | STRUCT_KEYW class_body { $$ = $2; }
  205. | UNION_KEYW class_body { $$ = $2; }
  206. ;
  207. simple_type_specifier:
  208. CHAR_KEYW
  209. | SHORT_KEYW
  210. | INT_KEYW
  211. | LONG_KEYW
  212. | SIGNED_KEYW
  213. | UNSIGNED_KEYW
  214. | FLOAT_KEYW
  215. | DOUBLE_KEYW
  216. | VOID_KEYW
  217. | BOOL_KEYW
  218. | TYPE { (*$1)->tag = SYM_TYPEDEF; $$ = $1; }
  219. ;
  220. ptr_operator:
  221. '*' cvar_qualifier_seq_opt
  222. { $$ = $2 ? $2 : $1; }
  223. ;
  224. cvar_qualifier_seq_opt:
  225. /* empty */ { $$ = NULL; }
  226. | cvar_qualifier_seq
  227. ;
  228. cvar_qualifier_seq:
  229. cvar_qualifier
  230. | cvar_qualifier_seq cvar_qualifier { $$ = $2; }
  231. ;
  232. cvar_qualifier:
  233. CONST_KEYW | VOLATILE_KEYW | ATTRIBUTE_PHRASE
  234. | RESTRICT_KEYW
  235. { /* restrict has no effect in prototypes so ignore it */
  236. remove_node($1);
  237. $$ = $1;
  238. }
  239. ;
  240. declarator:
  241. ptr_operator declarator { $$ = $2; }
  242. | direct_declarator
  243. ;
  244. direct_declarator:
  245. IDENT
  246. { if (current_name != NULL) {
  247. error_with_pos("unexpected second declaration name");
  248. YYERROR;
  249. } else {
  250. current_name = (*$1)->string;
  251. $$ = $1;
  252. }
  253. }
  254. | direct_declarator '(' parameter_declaration_clause ')'
  255. { $$ = $4; }
  256. | direct_declarator '(' error ')'
  257. { $$ = $4; }
  258. | direct_declarator BRACKET_PHRASE
  259. { $$ = $2; }
  260. | '(' declarator ')'
  261. { $$ = $3; }
  262. | '(' error ')'
  263. { $$ = $3; }
  264. ;
  265. /* Nested declarators differ from regular declarators in that they do
  266. not record the symbols they find in the global symbol table. */
  267. nested_declarator:
  268. ptr_operator nested_declarator { $$ = $2; }
  269. | direct_nested_declarator
  270. ;
  271. direct_nested_declarator:
  272. IDENT
  273. | TYPE
  274. | direct_nested_declarator '(' parameter_declaration_clause ')'
  275. { $$ = $4; }
  276. | direct_nested_declarator '(' error ')'
  277. { $$ = $4; }
  278. | direct_nested_declarator BRACKET_PHRASE
  279. { $$ = $2; }
  280. | '(' nested_declarator ')'
  281. { $$ = $3; }
  282. | '(' error ')'
  283. { $$ = $3; }
  284. ;
  285. parameter_declaration_clause:
  286. parameter_declaration_list_opt DOTS { $$ = $2; }
  287. | parameter_declaration_list_opt
  288. | parameter_declaration_list ',' DOTS { $$ = $3; }
  289. ;
  290. parameter_declaration_list_opt:
  291. /* empty */ { $$ = NULL; }
  292. | parameter_declaration_list
  293. ;
  294. parameter_declaration_list:
  295. parameter_declaration
  296. | parameter_declaration_list ',' parameter_declaration
  297. { $$ = $3; }
  298. ;
  299. parameter_declaration:
  300. decl_specifier_seq m_abstract_declarator
  301. { $$ = $2 ? $2 : $1; }
  302. ;
  303. m_abstract_declarator:
  304. ptr_operator m_abstract_declarator
  305. { $$ = $2 ? $2 : $1; }
  306. | direct_m_abstract_declarator
  307. ;
  308. direct_m_abstract_declarator:
  309. /* empty */ { $$ = NULL; }
  310. | IDENT
  311. { /* For version 2 checksums, we don't want to remember
  312. private parameter names. */
  313. remove_node($1);
  314. $$ = $1;
  315. }
  316. /* This wasn't really a typedef name but an identifier that
  317. shadows one. */
  318. | TYPE
  319. { remove_node($1);
  320. $$ = $1;
  321. }
  322. | direct_m_abstract_declarator '(' parameter_declaration_clause ')'
  323. { $$ = $4; }
  324. | direct_m_abstract_declarator '(' error ')'
  325. { $$ = $4; }
  326. | direct_m_abstract_declarator BRACKET_PHRASE
  327. { $$ = $2; }
  328. | '(' m_abstract_declarator ')'
  329. { $$ = $3; }
  330. | '(' error ')'
  331. { $$ = $3; }
  332. ;
  333. function_definition:
  334. decl_specifier_seq_opt declarator BRACE_PHRASE
  335. { struct string_list *decl = *$2;
  336. *$2 = NULL;
  337. add_symbol(current_name, SYM_NORMAL, decl, is_extern);
  338. $$ = $3;
  339. }
  340. ;
  341. initializer_opt:
  342. /* empty */ { $$ = NULL; }
  343. | initializer
  344. ;
  345. /* We never care about the contents of an initializer. */
  346. initializer:
  347. '=' EXPRESSION_PHRASE
  348. { remove_list($2, &(*$1)->next); $$ = $2; }
  349. ;
  350. class_body:
  351. '{' member_specification_opt '}' { $$ = $3; }
  352. | '{' error '}' { $$ = $3; }
  353. ;
  354. member_specification_opt:
  355. /* empty */ { $$ = NULL; }
  356. | member_specification
  357. ;
  358. member_specification:
  359. member_declaration
  360. | member_specification member_declaration { $$ = $2; }
  361. ;
  362. member_declaration:
  363. decl_specifier_seq_opt member_declarator_list_opt ';'
  364. { $$ = $3; }
  365. | error ';'
  366. { $$ = $2; }
  367. ;
  368. member_declarator_list_opt:
  369. /* empty */ { $$ = NULL; }
  370. | member_declarator_list
  371. ;
  372. member_declarator_list:
  373. member_declarator
  374. | member_declarator_list ',' member_declarator { $$ = $3; }
  375. ;
  376. member_declarator:
  377. nested_declarator attribute_opt { $$ = $2 ? $2 : $1; }
  378. | IDENT member_bitfield_declarator { $$ = $2; }
  379. | member_bitfield_declarator
  380. ;
  381. member_bitfield_declarator:
  382. ':' EXPRESSION_PHRASE { $$ = $2; }
  383. ;
  384. attribute_opt:
  385. /* empty */ { $$ = NULL; }
  386. | ATTRIBUTE_PHRASE
  387. ;
  388. asm_definition:
  389. ASM_PHRASE ';' { $$ = $2; }
  390. ;
  391. asm_phrase_opt:
  392. /* empty */ { $$ = NULL; }
  393. | ASM_PHRASE
  394. ;
  395. export_definition:
  396. EXPORT_SYMBOL_KEYW '(' IDENT ')' ';'
  397. { export_symbol((*$3)->string); $$ = $5; }
  398. ;
  399. %%
  400. static void
  401. yyerror(const char *e)
  402. {
  403. error_with_pos("%s", e);
  404. }