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