declar.g 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* DECLARATION SYNTAX PARSER */
  7. {
  8. #include "lint.h"
  9. #include "dbsymtab.h"
  10. #include <alloc.h>
  11. #include "nobitfield.h"
  12. #include "debug.h"
  13. #include "arith.h"
  14. #include "LLlex.h"
  15. #include "label.h"
  16. #include "code.h"
  17. #include "idf.h"
  18. #include "type.h"
  19. #include "struct.h"
  20. #include "field.h"
  21. #include "decspecs.h"
  22. #include "def.h"
  23. #include "declar.h"
  24. #include "label.h"
  25. #include "expr.h"
  26. #include "sizes.h"
  27. #include "level.h"
  28. #ifdef LINT
  29. #include "l_lint.h"
  30. #endif /* LINT */
  31. extern char options[];
  32. }
  33. /* 8 */
  34. declaration
  35. {struct decspecs Ds;}
  36. :
  37. {Ds = null_decspecs;}
  38. decl_specifiers(&Ds)
  39. [%default /* missing identifier derailed parser */
  40. init_declarator_list(&Ds)
  41. |
  42. empty
  43. ]
  44. ';'
  45. ;
  46. /* A `decl_specifiers' describes a sequence of a storage_class_specifier,
  47. an unsigned_specifier, a size_specifier and a simple type_specifier,
  48. which may occur in arbitrary order and each of which may be absent;
  49. at least one of them must be present, however, since the totally
  50. empty case has already be dealt with in `external_definition'.
  51. This means that something like:
  52. unsigned extern int short xx;
  53. is perfectly good C.
  54. On top of that, multiple occurrences of storage_class_specifiers,
  55. unsigned_specifiers and size_specifiers are errors, but a second
  56. type_specifier should end the decl_specifiers and be treated as
  57. the name to be declared (see the thin ice in RM11.1).
  58. Such a language is not easily expressed in a grammar; enumeration
  59. of the permutations is unattractive. We solve the problem by
  60. having a regular grammar for the "soft" items, handling the single
  61. occurrence of the type_specifier in the grammar (we have no choice),
  62. collecting all data in a `struct decspecs' and turning that data
  63. structure into what we want.
  64. The existence of declarations like
  65. short typedef yepp;
  66. makes all hope of writing a specific grammar for typedefs illusory.
  67. */
  68. decl_specifiers /* non-empty */ (register struct decspecs *ds;)
  69. /* Reads a non-empty decl_specifiers and fills the struct
  70. decspecs *ds.
  71. */
  72. :
  73. [
  74. other_specifier(ds)+
  75. [%if (DOT != IDENTIFIER || AHEAD == IDENTIFIER)
  76. /* the thin ice in R.M. 11.1 */
  77. single_type_specifier(ds) other_specifier(ds)*
  78. |
  79. empty
  80. ]
  81. |
  82. single_type_specifier(ds) other_specifier(ds)*
  83. ]
  84. {do_decspecs(ds);}
  85. ;
  86. /* 8.1 */
  87. other_specifier(register struct decspecs *ds;):
  88. [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
  89. { if (ds->ds_sc_given)
  90. error("repeated storage class specifier");
  91. ds->ds_sc_given = 1;
  92. ds->ds_sc = DOT;
  93. }
  94. |
  95. [ SHORT | LONG ]
  96. { if (ds->ds_size)
  97. error("repeated size specifier");
  98. ds->ds_size = DOT;
  99. }
  100. |
  101. UNSIGNED
  102. { if (ds->ds_unsigned)
  103. error("unsigned specified twice");
  104. ds->ds_unsigned = 1;
  105. }
  106. ;
  107. /* 8.2 */
  108. type_specifier(struct type **tpp;)
  109. /* Used in struct/union declarations and in casts; only the
  110. type is relevant.
  111. */
  112. {struct decspecs Ds; Ds = null_decspecs;}
  113. :
  114. decl_specifiers(&Ds)
  115. {
  116. if (Ds.ds_sc_given)
  117. error("storage class ignored");
  118. if (Ds.ds_sc == REGISTER)
  119. error("register ignored");
  120. }
  121. {*tpp = Ds.ds_type;}
  122. ;
  123. single_type_specifier(register struct decspecs *ds;):
  124. %default TYPE_IDENTIFIER /* this includes INT, CHAR, etc. */
  125. {idf2type(dot.tk_idf, &ds->ds_type);}
  126. |
  127. IDENTIFIER
  128. {
  129. error("%s is not a type identifier", dot.tk_idf->id_text);
  130. ds->ds_type = error_type;
  131. if (dot.tk_idf->id_def) {
  132. dot.tk_idf->id_def->df_type = error_type;
  133. dot.tk_idf->id_def->df_sc = TYPEDEF;
  134. }
  135. }
  136. |
  137. struct_or_union_specifier(&ds->ds_type)
  138. |
  139. enum_specifier(&ds->ds_type)
  140. ;
  141. /* 8.3 */
  142. init_declarator_list(struct decspecs *ds;):
  143. init_declarator(ds)
  144. [ ',' init_declarator(ds) ]*
  145. ;
  146. init_declarator(register struct decspecs *ds;)
  147. {
  148. struct declarator Dc;
  149. }
  150. :
  151. {
  152. Dc = null_declarator;
  153. }
  154. [
  155. declarator(&Dc)
  156. {
  157. reject_params(&Dc);
  158. declare_idf(ds, &Dc, level);
  159. #ifdef LINT
  160. lint_declare_idf(Dc.dc_idf, ds->ds_sc);
  161. #endif /* LINT */
  162. }
  163. [
  164. initializer(Dc.dc_idf, ds->ds_sc)
  165. |
  166. { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
  167. ]
  168. ]
  169. {
  170. #ifdef LINT
  171. add_auto(Dc.dc_idf);
  172. #endif /* LINT */
  173. remove_declarator(&Dc);
  174. }
  175. ;
  176. /* 8.6: initializer */
  177. initializer(struct idf *idf; int sc;)
  178. {
  179. struct expr *expr = (struct expr *) 0;
  180. int globalflag = level == L_GLOBAL ||
  181. (level >= L_LOCAL && sc == STATIC);
  182. }
  183. :
  184. { if (idf->id_def->df_type->tp_fund == FUNCTION) {
  185. error("illegal initialization of function");
  186. idf->id_def->df_type->tp_fund = ERRONEOUS;
  187. }
  188. if (level == L_FORMAL2)
  189. warning("illegal initialization of formal parameter (ignored)");
  190. }
  191. '=' /* used to be optional because of V6 */
  192. {
  193. #ifdef LINT
  194. lint_statement();
  195. #endif /* LINT */
  196. if (globalflag) {
  197. struct expr ex;
  198. code_declaration(idf, &ex, level, sc);
  199. }
  200. }
  201. initial_value(globalflag ? &(idf->id_def->df_type) : (struct type **)0,
  202. &expr)
  203. { if (! globalflag) {
  204. if (idf->id_def->df_type->tp_fund == FUNCTION) {
  205. free_expression(expr);
  206. expr = 0;
  207. }
  208. #ifdef DEBUG
  209. print_expr("initializer-expression", expr);
  210. #endif /* DEBUG */
  211. #ifdef LINT
  212. change_state(idf, SET);
  213. #endif /* LINT */
  214. #ifdef DBSYMTAB
  215. if (options['g'] && level >= L_LOCAL && expr) {
  216. db_line(expr->ex_file, (unsigned) expr->ex_line)
  217. ;
  218. }
  219. #endif /* DBSYMTAB */
  220. code_declaration(idf, expr, level, sc);
  221. }
  222. #ifdef DBSYMTAB
  223. if (options['g'] && globalflag) {
  224. stb_string(idf->id_def, sc, idf->id_text);
  225. }
  226. #endif /* DBSYMTAB */
  227. init_idf(idf);
  228. }
  229. ;
  230. /*
  231. Functions yielding pointers to functions must be declared as, e.g.,
  232. int (*hehe(par1, par2))() char *par1, *par2; {}
  233. Since the function heading is read as a normal declarator,
  234. we just include the (formal) parameter list in the declarator
  235. description list dc.
  236. */
  237. declarator(register struct declarator *dc;)
  238. {
  239. arith count;
  240. struct formal *fm = 0;
  241. }
  242. :
  243. primary_declarator(dc)
  244. [
  245. '('
  246. formal_list(&fm) ? /* semantic check later... */
  247. ')'
  248. {
  249. add_decl_unary(dc, FUNCTION, (arith)0, fm);
  250. fm = 0;
  251. }
  252. |
  253. arrayer(&count)
  254. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  255. ]*
  256. |
  257. '*' declarator(dc)
  258. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  259. ;
  260. primary_declarator(register struct declarator *dc;) :
  261. identifier(&dc->dc_idf)
  262. |
  263. '(' declarator(dc) ')'
  264. ;
  265. arrayer(arith *sizep;)
  266. { struct expr *expr; }
  267. :
  268. '['
  269. [
  270. constant_expression(&expr)
  271. {
  272. check_array_subscript(expr);
  273. *sizep = expr->VL_VALUE;
  274. free_expression(expr);
  275. }
  276. |
  277. empty
  278. { *sizep = (arith)-1; }
  279. ]
  280. ']'
  281. ;
  282. formal_list (struct formal **fmp;)
  283. :
  284. formal(fmp) [ %persistent ',' formal(fmp) ]*
  285. ;
  286. formal(struct formal **fmp;)
  287. {struct idf *idf; }
  288. :
  289. identifier(&idf)
  290. {
  291. register struct formal *new = new_formal();
  292. new->fm_idf = idf;
  293. new->next = *fmp;
  294. *fmp = new;
  295. }
  296. ;
  297. /* Change 2 */
  298. enum_specifier(register struct type **tpp;)
  299. {
  300. struct idf *idf;
  301. arith l = (arith)0;
  302. }
  303. :
  304. ENUM
  305. [
  306. {declare_struct(ENUM, (struct idf *) 0, tpp);}
  307. enumerator_pack(*tpp, &l)
  308. |
  309. identifier(&idf)
  310. [
  311. {declare_struct(ENUM, idf, tpp);}
  312. enumerator_pack(*tpp, &l)
  313. {
  314. #ifdef DBSYMTAB
  315. if (options['g']) {
  316. stb_tag(idf->id_enum, idf->id_text);
  317. }
  318. #endif /*DBSYMTAB */
  319. }
  320. |
  321. {apply_struct(ENUM, idf, tpp);}
  322. empty
  323. ]
  324. ]
  325. ;
  326. enumerator_pack(register struct type *tp; arith *lp;) :
  327. '{'
  328. enumerator(tp, lp)
  329. [%while(AHEAD != '}') /* >>> conflict on ',' */
  330. ','
  331. enumerator(tp, lp)
  332. ]*
  333. ','? /* optional trailing comma */
  334. '}'
  335. {tp->tp_size = int_size;}
  336. /* fancy implementations that put small enums in 1 byte
  337. or so should start here.
  338. */
  339. ;
  340. enumerator(struct type *tp; arith *lp;)
  341. {
  342. struct idf *idf;
  343. struct expr *expr;
  344. }
  345. :
  346. identifier(&idf)
  347. [
  348. '='
  349. constant_expression(&expr)
  350. {
  351. *lp = expr->VL_VALUE;
  352. free_expression(expr);
  353. }
  354. ]?
  355. {declare_enum(tp, idf, (*lp)++);}
  356. ;
  357. /* 8.5 */
  358. struct_or_union_specifier(register struct type **tpp;)
  359. {
  360. int fund;
  361. struct idf *idfX;
  362. register struct idf *idf;
  363. }
  364. :
  365. [ STRUCT | UNION ]
  366. {fund = DOT;}
  367. [
  368. {
  369. declare_struct(fund, (struct idf *)0, tpp);
  370. }
  371. struct_declaration_pack(*tpp)
  372. |
  373. identifier(&idfX) { idf = idfX; }
  374. [
  375. {
  376. declare_struct(fund, idf, tpp);
  377. (idf->id_struct->tg_busy)++;
  378. }
  379. struct_declaration_pack(*tpp)
  380. {
  381. (idf->id_struct->tg_busy)--;
  382. #ifdef DBSYMTAB
  383. if (options['g']) {
  384. stb_tag(idf->id_struct, idf->id_text);
  385. }
  386. #endif /*DBSYMTAB */
  387. }
  388. |
  389. {apply_struct(fund, idf, tpp);}
  390. empty
  391. ]
  392. ]
  393. ;
  394. struct_declaration_pack(register struct type *stp;)
  395. {
  396. struct sdef **sdefp = &stp->tp_sdef;
  397. arith size = (arith)0;
  398. }
  399. :
  400. /* The size is only filled in after the whole struct has
  401. been read, to prevent recursive definitions.
  402. */
  403. '{'
  404. struct_declaration(stp, &sdefp, &size)+
  405. '}'
  406. {stp->tp_size = align(size, stp->tp_align);}
  407. ;
  408. struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
  409. {struct type *tp;}
  410. :
  411. type_specifier(&tp)
  412. struct_declarator_list(tp, stp, sdefpp, szp)
  413. [ /* in some standard UNIX compilers the semicolon
  414. is optional, would you believe!
  415. */
  416. ';'
  417. |
  418. empty
  419. {warning("no semicolon after declarator");}
  420. ]
  421. ;
  422. struct_declarator_list(struct type *tp; struct type *stp;
  423. struct sdef ***sdefpp; arith *szp;)
  424. :
  425. struct_declarator(tp, stp, sdefpp, szp)
  426. [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
  427. ;
  428. struct_declarator(struct type *tp; struct type *stp;
  429. struct sdef ***sdefpp; arith *szp;)
  430. {
  431. struct declarator Dc;
  432. struct field *fd = 0;
  433. }
  434. :
  435. {
  436. Dc = null_declarator;
  437. }
  438. [
  439. declarator(&Dc)
  440. {reject_params(&Dc);}
  441. bit_expression(&fd)?
  442. |
  443. {Dc.dc_idf = gen_idf();}
  444. bit_expression(&fd)
  445. ]
  446. {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
  447. {remove_declarator(&Dc);}
  448. ;
  449. bit_expression(struct field **fd;)
  450. { struct expr *expr; }
  451. :
  452. {
  453. *fd = new_field();
  454. }
  455. ':'
  456. constant_expression(&expr)
  457. {
  458. (*fd)->fd_width = expr->VL_VALUE;
  459. free_expression(expr);
  460. #ifdef NOBITFIELD
  461. error("bitfields are not implemented");
  462. #endif /* NOBITFIELD */
  463. }
  464. ;
  465. /* 8.7 */
  466. cast(struct type **tpp;) {struct declarator Dc;} :
  467. {Dc = null_declarator;}
  468. '('
  469. type_specifier(tpp)
  470. abstract_declarator(&Dc)
  471. ')'
  472. {*tpp = declare_type(*tpp, &Dc);}
  473. {remove_declarator(&Dc);}
  474. ;
  475. /* This code is an abject copy of that of 'declarator', for lack of
  476. a two-level grammar.
  477. */
  478. abstract_declarator(register struct declarator *dc;)
  479. {arith count;}
  480. :
  481. primary_abstract_declarator(dc)
  482. [
  483. '(' ')'
  484. {add_decl_unary(dc, FUNCTION, (arith)0, NO_PARAMS);}
  485. |
  486. arrayer(&count)
  487. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  488. ]*
  489. |
  490. '*' abstract_declarator(dc)
  491. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  492. ;
  493. primary_abstract_declarator(struct declarator *dc;) :
  494. [%if (AHEAD == ')')
  495. empty
  496. |
  497. '(' abstract_declarator(dc) ')'
  498. ]
  499. ;
  500. empty:
  501. ;
  502. /* 8.8 */
  503. /* included in the IDENTIFIER/TYPE_IDENTIFIER mechanism */