declar.g 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. %erroneous
  128. IDENTIFIER
  129. {
  130. error("%s is not a type identifier", dot.tk_idf->id_text);
  131. ds->ds_type = error_type;
  132. if (dot.tk_idf->id_def) {
  133. dot.tk_idf->id_def->df_type = error_type;
  134. dot.tk_idf->id_def->df_sc = TYPEDEF;
  135. }
  136. }
  137. |
  138. %illegal
  139. IDENTIFIER
  140. |
  141. struct_or_union_specifier(&ds->ds_type)
  142. |
  143. enum_specifier(&ds->ds_type)
  144. ;
  145. /* 8.3 */
  146. init_declarator_list(struct decspecs *ds;):
  147. init_declarator(ds)
  148. [ ',' init_declarator(ds) ]*
  149. ;
  150. init_declarator(register struct decspecs *ds;)
  151. {
  152. struct declarator Dc;
  153. }
  154. :
  155. {
  156. Dc = null_declarator;
  157. }
  158. [
  159. declarator(&Dc)
  160. {
  161. reject_params(&Dc);
  162. declare_idf(ds, &Dc, level);
  163. #ifdef LINT
  164. lint_declare_idf(Dc.dc_idf, ds->ds_sc);
  165. #endif /* LINT */
  166. }
  167. [
  168. initializer(Dc.dc_idf, ds->ds_sc)
  169. |
  170. { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
  171. ]
  172. ]
  173. {
  174. #ifdef LINT
  175. add_auto(Dc.dc_idf);
  176. #endif /* LINT */
  177. remove_declarator(&Dc);
  178. }
  179. ;
  180. /* 8.6: initializer */
  181. initializer(struct idf *idf; int sc;)
  182. {
  183. struct expr *expr = (struct expr *) 0;
  184. int globalflag = level == L_GLOBAL ||
  185. (level >= L_LOCAL && sc == STATIC);
  186. }
  187. :
  188. { if (idf->id_def->df_type->tp_fund == FUNCTION) {
  189. error("illegal initialization of function");
  190. idf->id_def->df_type->tp_fund = ERRONEOUS;
  191. }
  192. if (level == L_FORMAL2)
  193. warning("illegal initialization of formal parameter (ignored)");
  194. }
  195. '=' /* used to be optional because of V6 */
  196. {
  197. #ifdef LINT
  198. lint_statement();
  199. #endif /* LINT */
  200. if (globalflag) {
  201. struct expr ex;
  202. code_declaration(idf, &ex, level, sc);
  203. }
  204. }
  205. initial_value(globalflag ? &(idf->id_def->df_type) : (struct type **)0,
  206. &expr)
  207. { if (! globalflag) {
  208. if (idf->id_def->df_type->tp_fund == FUNCTION) {
  209. free_expression(expr);
  210. expr = 0;
  211. }
  212. #ifdef DEBUG
  213. print_expr("initializer-expression", expr);
  214. #endif /* DEBUG */
  215. #ifdef LINT
  216. change_state(idf, SET);
  217. #endif /* LINT */
  218. #ifdef DBSYMTAB
  219. if (options['g'] && level >= L_LOCAL && expr) {
  220. db_line(expr->ex_file, (unsigned) expr->ex_line)
  221. ;
  222. }
  223. #endif /* DBSYMTAB */
  224. code_declaration(idf, expr, level, sc);
  225. }
  226. #ifdef DBSYMTAB
  227. if (options['g'] && globalflag) {
  228. stb_string(idf->id_def, sc, idf->id_text);
  229. }
  230. #endif /* DBSYMTAB */
  231. init_idf(idf);
  232. }
  233. ;
  234. /*
  235. Functions yielding pointers to functions must be declared as, e.g.,
  236. int (*hehe(par1, par2))() char *par1, *par2; {}
  237. Since the function heading is read as a normal declarator,
  238. we just include the (formal) parameter list in the declarator
  239. description list dc.
  240. */
  241. declarator(register struct declarator *dc;)
  242. {
  243. arith count;
  244. struct formal *fm = 0;
  245. }
  246. :
  247. primary_declarator(dc)
  248. [
  249. '('
  250. formal_list(&fm) ? /* semantic check later... */
  251. ')'
  252. {
  253. add_decl_unary(dc, FUNCTION, (arith)0, fm);
  254. fm = 0;
  255. }
  256. |
  257. arrayer(&count)
  258. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  259. ]*
  260. |
  261. '*' declarator(dc)
  262. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  263. ;
  264. primary_declarator(register struct declarator *dc;) :
  265. identifier(&dc->dc_idf)
  266. |
  267. '(' declarator(dc) ')'
  268. ;
  269. arrayer(arith *sizep;)
  270. { struct expr *expr; }
  271. :
  272. '['
  273. [
  274. constant_expression(&expr)
  275. {
  276. check_array_subscript(expr);
  277. *sizep = expr->VL_VALUE;
  278. free_expression(expr);
  279. }
  280. |
  281. empty
  282. { *sizep = (arith)-1; }
  283. ]
  284. ']'
  285. ;
  286. formal_list (struct formal **fmp;)
  287. :
  288. formal(fmp) [ %persistent ',' formal(fmp) ]*
  289. ;
  290. formal(struct formal **fmp;)
  291. {struct idf *idf; }
  292. :
  293. identifier(&idf)
  294. {
  295. register struct formal *new = new_formal();
  296. new->fm_idf = idf;
  297. new->next = *fmp;
  298. *fmp = new;
  299. }
  300. ;
  301. /* Change 2 */
  302. enum_specifier(register struct type **tpp;)
  303. {
  304. struct idf *idf;
  305. arith l = (arith)0;
  306. }
  307. :
  308. ENUM
  309. [
  310. {declare_struct(ENUM, (struct idf *) 0, tpp);}
  311. enumerator_pack(*tpp, &l)
  312. |
  313. identifier(&idf)
  314. [
  315. {declare_struct(ENUM, idf, tpp);}
  316. enumerator_pack(*tpp, &l)
  317. {
  318. #ifdef DBSYMTAB
  319. if (options['g']) {
  320. stb_tag(idf->id_enum, idf->id_text);
  321. }
  322. #endif /*DBSYMTAB */
  323. }
  324. |
  325. {apply_struct(ENUM, idf, tpp);}
  326. empty
  327. ]
  328. ]
  329. ;
  330. enumerator_pack(register struct type *tp; arith *lp;) :
  331. '{'
  332. enumerator(tp, lp)
  333. [%while(AHEAD != '}') /* >>> conflict on ',' */
  334. ','
  335. enumerator(tp, lp)
  336. ]*
  337. ','? /* optional trailing comma */
  338. '}'
  339. {tp->tp_size = int_size;}
  340. /* fancy implementations that put small enums in 1 byte
  341. or so should start here.
  342. */
  343. ;
  344. enumerator(struct type *tp; arith *lp;)
  345. {
  346. struct idf *idf;
  347. struct expr *expr;
  348. }
  349. :
  350. identifier(&idf)
  351. [
  352. '='
  353. constant_expression(&expr)
  354. {
  355. *lp = expr->VL_VALUE;
  356. free_expression(expr);
  357. }
  358. ]?
  359. {declare_enum(tp, idf, (*lp)++);}
  360. ;
  361. /* 8.5 */
  362. struct_or_union_specifier(register struct type **tpp;)
  363. {
  364. int fund;
  365. struct idf *idfX;
  366. register struct idf *idf;
  367. }
  368. :
  369. [ STRUCT | UNION ]
  370. {fund = DOT;}
  371. [
  372. {
  373. declare_struct(fund, (struct idf *)0, tpp);
  374. }
  375. struct_declaration_pack(*tpp)
  376. |
  377. identifier(&idfX) { idf = idfX; }
  378. [
  379. {
  380. declare_struct(fund, idf, tpp);
  381. (idf->id_struct->tg_busy)++;
  382. }
  383. struct_declaration_pack(*tpp)
  384. {
  385. (idf->id_struct->tg_busy)--;
  386. #ifdef DBSYMTAB
  387. if (options['g']) {
  388. stb_tag(idf->id_struct, idf->id_text);
  389. }
  390. #endif /*DBSYMTAB */
  391. }
  392. |
  393. {apply_struct(fund, idf, tpp);}
  394. empty
  395. ]
  396. ]
  397. ;
  398. struct_declaration_pack(register struct type *stp;)
  399. {
  400. struct sdef **sdefp = &stp->tp_sdef;
  401. arith size = (arith)0;
  402. }
  403. :
  404. /* The size is only filled in after the whole struct has
  405. been read, to prevent recursive definitions.
  406. */
  407. '{'
  408. struct_declaration(stp, &sdefp, &size)+
  409. '}'
  410. {stp->tp_size = align(size, stp->tp_align);}
  411. ;
  412. struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
  413. {struct type *tp;}
  414. :
  415. type_specifier(&tp)
  416. struct_declarator_list(tp, stp, sdefpp, szp)
  417. [ /* in some standard UNIX compilers the semicolon
  418. is optional, would you believe!
  419. */
  420. ';'
  421. |
  422. empty
  423. {warning("no semicolon after declarator");}
  424. ]
  425. ;
  426. struct_declarator_list(struct type *tp; struct type *stp;
  427. struct sdef ***sdefpp; arith *szp;)
  428. :
  429. struct_declarator(tp, stp, sdefpp, szp)
  430. [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
  431. ;
  432. struct_declarator(struct type *tp; struct type *stp;
  433. struct sdef ***sdefpp; arith *szp;)
  434. {
  435. struct declarator Dc;
  436. struct field *fd = 0;
  437. }
  438. :
  439. {
  440. Dc = null_declarator;
  441. }
  442. [
  443. declarator(&Dc)
  444. {reject_params(&Dc);}
  445. bit_expression(&fd)?
  446. |
  447. {Dc.dc_idf = gen_idf();}
  448. bit_expression(&fd)
  449. ]
  450. {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
  451. {remove_declarator(&Dc);}
  452. ;
  453. bit_expression(struct field **fd;)
  454. { struct expr *expr; }
  455. :
  456. {
  457. *fd = new_field();
  458. }
  459. ':'
  460. constant_expression(&expr)
  461. {
  462. (*fd)->fd_width = expr->VL_VALUE;
  463. free_expression(expr);
  464. #ifdef NOBITFIELD
  465. error("bitfields are not implemented");
  466. #endif /* NOBITFIELD */
  467. }
  468. ;
  469. /* 8.7 */
  470. cast(struct type **tpp;) {struct declarator Dc;} :
  471. {Dc = null_declarator;}
  472. '('
  473. type_specifier(tpp)
  474. abstract_declarator(&Dc)
  475. ')'
  476. {*tpp = declare_type(*tpp, &Dc);}
  477. {remove_declarator(&Dc);}
  478. ;
  479. /* This code is an abject copy of that of 'declarator', for lack of
  480. a two-level grammar.
  481. */
  482. abstract_declarator(register struct declarator *dc;)
  483. {arith count;}
  484. :
  485. primary_abstract_declarator(dc)
  486. [
  487. '(' ')'
  488. {add_decl_unary(dc, FUNCTION, (arith)0, NO_PARAMS);}
  489. |
  490. arrayer(&count)
  491. {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
  492. ]*
  493. |
  494. '*' abstract_declarator(dc)
  495. {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
  496. ;
  497. primary_abstract_declarator(struct declarator *dc;) :
  498. [%if (AHEAD == ')')
  499. empty
  500. |
  501. '(' abstract_declarator(dc) ')'
  502. ]
  503. ;
  504. empty:
  505. ;
  506. /* 8.8 */
  507. /* included in the IDENTIFIER/TYPE_IDENTIFIER mechanism */