parse.y 11 KB

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