parse.y 11 KB

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