parse.y 11 KB

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