Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / 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 1.32 1997/02/21 17:11:03 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        <flt_arith.h>
15 #include        "arith.h"
16 #include        "LLlex.h"
17 #include        "label.h"
18 #include        "code.h"
19 #include        "idf.h"
20 #include        "type.h"
21 #include        "proto.h"
22 #include        "struct.h"
23 #include        "field.h"
24 #include        "decspecs.h"
25 #include        "def.h"
26 #include        "declar.h"
27 #include        "label.h"
28 #include        "expr.h"
29 #include        "sizes.h"
30 #include        "level.h"
31 #ifdef  LINT
32 #include        "l_lint.h"
33 #endif  /* LINT */
34
35 extern char     options[];
36 }
37
38 /* 3.5 */
39 declaration
40         {struct decspecs Ds;}
41 :
42         {Ds = null_decspecs;}
43         decl_specifiers(&Ds)
44         init_declarator_list(&Ds)?
45         ';'
46 ;
47
48 /*      A `decl_specifiers' describes a sequence of a storage_class_specifier,
49         an unsigned_specifier, a size_specifier and a simple type_specifier,
50         which may occur in arbitrary order and each of which may be absent;
51         at least one of them must be present, however, since the totally
52         empty case has already be dealt with in `external_definition'.
53         This means that something like:
54                 unsigned extern int short xx;
55         is perfectly legal C.
56         
57         On top of that, multiple occurrences of storage_class_specifiers,
58         unsigned_specifiers and size_specifiers are errors, but a second
59         type_specifier should end the decl_specifiers and be treated as
60         the name to be declared.
61         Such a language is not easily expressed in a grammar; enumeration
62         of the permutations is unattractive. We solve the problem by
63         having a regular grammar for the "soft" items, handling the single
64         occurrence of the type_specifier in the grammar (we have no choice),
65         collecting all data in a `struct decspecs' and turning that data
66         structure into what we want.
67         
68         The existence of declarations like
69                 short typedef yepp;
70         makes all hope of writing a specific grammar for typedefs illusory.
71 */
72 /*      Accept a single declaration specifier.  Then accept zero or more
73         declaration specifiers.  There can be a conflict on both
74         TYPE_IDENTIFIER and IDENTIFIER.
75         The following rule is used:
76         When we see a TYPE_IDENTIFIER, we accept it if no type-specifier was
77         given, and it is not directly followed by an identifier.  If a
78         type-specifier was given, it is taken as the identifier being
79         declared.  If it is followed by an identifier, we assume that an
80         error has been  made, (e.g. unsigned typedeffed_int x;) and that
81         this will be detected later on.
82         When we see an IDENTIFIER, directly followed by another IDENTIFIER,
83         we assume that a typing mistake has been made, and we accept it as
84         an erroneous type-identifier.
85 */
86
87 decl_specifiers /* non-empty */ (register struct decspecs *ds;)
88         /*      Reads a non-empty decl_specifiers and fills the struct
89                 decspecs *ds.
90         */
91 :
92         single_decl_specifier(ds)
93         [ %while(  (DOT==TYPE_IDENTIFIER
94                         && ds->ds_size == 0
95                         && ds->ds_unsigned == 0
96                         && ds->ds_type == (struct type *)0)
97                     || AHEAD == IDENTIFIER)     /* always an error */
98                 single_decl_specifier(ds)
99         ]*
100         {do_decspecs(ds);}
101 ;
102
103 single_decl_specifier /* non_empty */ (register struct decspecs *ds;)
104 :
105         [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
106         {       if (ds->ds_sc_given)
107                         error("repeated storage class specifier");
108                 ds->ds_sc_given = 1;
109                 ds->ds_sc = DOT;
110         }
111 |
112         VOLATILE
113         {       if (ds->ds_typequal & TQ_VOLATILE)
114                         error("repeated type qualifier");
115                 ds->ds_typequal |= TQ_VOLATILE;
116         }
117 |
118         CONST
119         {       if (ds->ds_typequal & TQ_CONST)
120                         error("repeated type qualifier");
121                 ds->ds_typequal |= TQ_CONST;
122         }
123 |
124         [ SHORT | LONG ]
125         {       if (ds->ds_size)
126                         error("repeated size specifier");
127                 ds->ds_size = DOT;
128         }
129 |
130         [ SIGNED | UNSIGNED ]
131         {       if (ds->ds_unsigned != 0)
132                         error("repeated sign specifier");
133                 ds->ds_unsigned = DOT;
134         }
135 |
136         [ VOID | CHAR | INT | FLOAT | DOUBLE ]
137         {
138                 idf2type(dot.tk_idf, &ds->ds_type);
139                 ds->ds_typedef = 0;
140         }
141 |
142         %default TYPE_IDENTIFIER
143         {
144                 idf2type(dot.tk_idf, &ds->ds_type);
145                 ds->ds_typedef = 1;
146         }
147 |
148         %erroneous
149         IDENTIFIER
150         {
151                 error("%s is not a type identifier", dot.tk_idf->id_text);
152                 ds->ds_type = error_type;
153                 if (dot.tk_idf->id_def) {
154                         dot.tk_idf->id_def->df_type = error_type;
155                         dot.tk_idf->id_def->df_sc = TYPEDEF;
156                 }
157         }
158 |
159         %illegal
160         IDENTIFIER
161 |
162         struct_or_union_specifier(&ds->ds_type)
163 |
164         enum_specifier(&ds->ds_type)
165 ;
166
167 /* 3.5.2 */
168 type_specifier(struct type **tpp;)
169         /*      Used in struct/union declarations and in casts; only the
170                 type is relevant.
171         */
172         {struct decspecs Ds; Ds = null_decspecs;}
173 :
174         decl_specifiers(&Ds)
175         {
176                 if (Ds.ds_sc_given)
177                         error("storage class ignored");
178                 if (Ds.ds_sc == REGISTER)
179                         error("register ignored");
180         }
181         {*tpp = Ds.ds_type;}
182 ;
183
184 /* 3.5 */
185 init_declarator_list(struct decspecs *ds;):
186         init_declarator(ds)
187         [ ',' init_declarator(ds) ]*
188 ;
189
190 init_declarator(register struct decspecs *ds;)
191         {
192                 struct declarator Dc;
193         }
194 :
195         {
196                 Dc = null_declarator;
197         }
198 [
199         declarator(&Dc)
200         {
201                 reject_params(&Dc);
202                 declare_idf(ds, &Dc, level);
203 #ifdef  LINT
204                 lint_declare_idf(Dc.dc_idf, ds->ds_sc);
205 #endif  /* LINT */
206         }
207         [
208                 initializer(Dc.dc_idf, ds->ds_sc)
209         |
210                 { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
211         ]
212 ]
213         {
214 #ifdef  LINT
215                 add_auto(Dc.dc_idf);
216 #endif  /* LINT */
217                 remove_declarator(&Dc);
218         }
219 ;
220
221 /* 3.5.7: initializer */
222 initializer(struct idf *idf; int sc;)
223         {
224                 struct expr *expr = (struct expr *) 0;
225                 int fund = idf->id_def->df_type->tp_fund;
226                 int autoagg = (level >= L_LOCAL
227                                 && sc != STATIC
228                                 && ( fund == STRUCT
229                                     || fund == UNION
230                                     || fund == ARRAY));
231                 int globalflag = level == L_GLOBAL
232                                 || (level >= L_LOCAL && sc == STATIC);
233         }
234 :
235         {       if (idf->id_def->df_type->tp_fund == FUNCTION)  {
236                         error("illegal initialization of function");
237                         idf->id_def->df_type->tp_fund = ERRONEOUS;
238                 }
239                 if (level == L_FORMAL2)
240                         error("illegal initialization of formal parameter");
241         }
242         '='
243         {
244                 if (AHEAD != '{' && AHEAD != STRING ) autoagg = 0;
245 #ifdef  LINT
246                 lint_statement();
247 #endif  /* LINT */
248                 if (globalflag) {
249                         struct expr ex;
250                         code_declaration(idf, &ex, level, sc);
251                 }
252                 else if (autoagg)
253                         loc_init((struct expr *) 0, idf);
254         }
255         initial_value((globalflag || autoagg) ?
256                                 &(idf->id_def->df_type)
257                                 : (struct type **)0,
258                         &expr)
259         {       if (! globalflag) {
260                         if (idf->id_def->df_type->tp_fund == FUNCTION)  {
261                                 free_expression(expr);
262                                 expr = 0;
263                         }
264 #ifdef  DEBUG
265                         print_expr("initializer-expression", expr);
266 #endif  /* DEBUG */
267 #ifdef  LINT
268                         change_state(idf, SET);
269 #endif  /* LINT */
270 #ifdef  DBSYMTAB
271                         if (options['g'] && level >= L_LOCAL && expr) {
272                                 db_line(expr->ex_file, (unsigned) expr->ex_line);
273                         }
274 #endif  /* DBSYMTAB */
275                         if (autoagg)
276                                 loc_init((struct expr *) 0, idf);
277                         else    code_declaration(idf, expr, level, sc);
278                 }
279 #ifdef  DBSYMTAB
280                 if (options['g'] && globalflag) {
281                         stb_string(idf->id_def, sc, idf->id_text);
282                 }
283 #endif  /* DBSYMTAB */
284                 idf_initialized(idf);
285         }
286 ;
287
288 /*
289         Functions yielding pointers to functions must be declared as, e.g.,
290                 int (*hehe(par1, par2))() char *par1, *par2;    {}
291         Since the function heading is read as a normal declarator,
292         we just include the (formal) parameter list in the declarator
293         description list dc.
294 */
295 /* 3.5.4 */
296 declarator(register struct declarator *dc;)
297         {       struct formal *fm = NO_PARAMS;
298                 struct proto *pl = NO_PROTO;
299                 arith count;
300                 int qual;
301         }
302 :
303         primary_declarator(dc)
304         [/*%while(1)*/
305                 '('
306                 [ %if (DOT != IDENTIFIER)
307                         parameter_type_list(&pl)
308                 |
309                         formal_list(&fm)
310                 |
311                         /* empty */
312                 ]
313                 ')'
314                 {       add_decl_unary(dc, FUNCTION, 0, (arith)0, fm, pl);
315                         fm = NO_PARAMS;
316                 }
317         |
318                 arrayer(&count)
319                 {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
320         ]*
321 |
322         pointer(&qual) declarator(dc)
323         {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
324 ;
325
326 primary_declarator(register struct declarator *dc;) :
327         identifier(&dc->dc_idf)
328 |
329         '(' declarator(dc) ')'
330 ;
331
332 arrayer(arith *sizep;)
333         { struct expr *expr; }
334 :
335         '['
336                 { *sizep = (arith)-1; }
337                 [
338                         constant_expression(&expr)
339                         {
340                                 check_array_subscript(expr);
341                                 *sizep = expr->VL_VALUE;
342                                 free_expression(expr);
343                         }
344                 ]?
345         ']'
346 ;
347
348 formal_list (struct formal **fmp;)
349 :
350         formal(fmp) [ %persistent ',' formal(fmp) ]*
351 ;
352
353 formal(struct formal **fmp;)
354         {struct idf *idf;       }
355 :
356         identifier(&idf)
357         {
358                 register struct formal *new = new_formal();
359                 
360                 new->fm_idf = idf;
361                 new->next = *fmp;
362                 *fmp = new;
363                 if (idf->id_def && idf->id_def->df_sc == TYPEDEF) {
364                         error("typedef name %s may not be redeclared as a parameter", idf->id_text);
365                 }
366         }
367 ;
368
369 /* Change 2 */
370 enum_specifier(register struct type **tpp;)
371         {
372                 struct idf *idf;
373                 arith l = (arith)0;
374         }
375 :
376         {if (*tpp) error("multiple types in declaration");}
377         ENUM
378         [
379                 {declare_struct(ENUM, (struct idf *) 0, tpp);}
380                 enumerator_pack(*tpp, &l)
381         |
382                 identifier(&idf)
383                 [
384                         {declare_struct(ENUM, idf, tpp);}
385                         enumerator_pack(*tpp, &l)
386                         {
387 #ifdef DBSYMTAB
388                                 if (options['g']) {
389                                         stb_tag(idf->id_tag, idf->id_text);
390                                 }
391 #endif /*DBSYMTAB */
392                         }
393                 |
394                         {apply_struct(ENUM, idf, tpp);}
395                         /* empty */
396                 ]
397         ]
398 ;
399
400 enumerator_pack(register struct type *tp; arith *lp;) :
401         '{'
402         enumerator(tp, lp)
403         [%while (AHEAD != '}')
404                 ','
405                 enumerator(tp, lp)
406         ]*
407         [
408                 ','     {warning("unexpected trailing comma in enumerator pack");}
409         ]?
410         '}'
411         {tp->tp_size = int_size;}
412         /*      fancy implementations that put small enums in 1 byte
413                 or so should start here.
414         */
415 ;
416
417 enumerator(struct type *tp; arith *lp;)
418         {
419                 struct idf *idf;
420                 struct expr *expr;
421         }
422 :
423         identifier(&idf)
424         [
425                 '='
426                 constant_expression(&expr)
427                 {
428                         *lp = expr->VL_VALUE;
429                         free_expression(expr);
430                 }
431         ]?
432         {declare_enum(tp, idf, (*lp)++);}
433 ;
434
435 /* 8.5 */
436 struct_or_union_specifier(register struct type **tpp;)
437         {
438                 int fund;
439                 struct idf *idfX;
440                 register struct idf *idf;
441         }
442 :
443         {if (*tpp) error("multiple types in declaration");}
444         [ STRUCT | UNION ]
445         {fund = DOT;}
446         [
447                 {
448                         declare_struct(fund, (struct idf *)0, tpp);
449                 }
450                 struct_declaration_pack(*tpp)
451         |
452                 identifier(&idfX)       { idf = idfX; }
453                 [
454                         {
455                                 declare_struct(fund, idf, tpp);
456                                 (idf->id_tag->tg_busy)++;
457                         }
458                         struct_declaration_pack(*tpp)
459                         {
460                                 (idf->id_tag->tg_busy)--;
461 #ifdef DBSYMTAB
462                                 if (options['g']) {
463                                         stb_tag(idf->id_tag, idf->id_text);
464                                 }
465 #endif /*DBSYMTAB */
466                         }
467                 |
468                         {
469                           /* a ';' means an empty declaration (probably)
470                            * this means that we have to declare a new
471                            * structure. (yegh)
472                            */
473                           if (DOT == ';' &&
474                               ( !idf->id_tag ||
475                                  idf->id_tag->tg_level != level ||
476                                  idf->id_tag->tg_type->tp_size < 0
477                              )) declare_struct(fund, idf, tpp);
478                           else apply_struct(fund, idf, tpp);
479                         }
480                         /* empty */
481                 ]
482         ]
483 ;
484
485 struct_declaration_pack(register struct type *stp;)
486         {
487                 struct sdef **sdefp = &stp->tp_sdef;
488                 arith size = (arith)0;
489         }
490 :
491         /*      The size is only filled in after the whole struct has
492                 been read, to prevent recursive definitions.
493         */
494         '{'
495         struct_declaration(stp, &sdefp, &size)+
496         '}'
497         {stp->tp_size = align(size, stp->tp_align);
498          completed(stp);
499         }
500 ;
501
502 struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
503         {struct type *tp;}
504 :
505         type_specifier(&tp) struct_declarator_list(tp, stp, sdefpp, szp) ';'
506 ;
507
508 struct_declarator_list(struct type *tp; struct type *stp;
509                         struct sdef ***sdefpp; arith *szp;)
510 :
511         struct_declarator(tp, stp, sdefpp, szp)
512         [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
513 ;
514
515 struct_declarator(struct type *tp; struct type *stp;
516                         struct sdef ***sdefpp; arith *szp;)
517         {
518                 struct declarator Dc;
519                 struct field *fd = 0;
520         }
521 :
522         {
523                 Dc = null_declarator;
524         }
525 [
526         declarator(&Dc)
527         {reject_params(&Dc);}
528         bit_expression(&fd)?
529 |
530         {Dc.dc_idf = gen_idf();}
531         bit_expression(&fd)
532 ]
533         {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
534         {remove_declarator(&Dc);}
535 ;
536
537 bit_expression(struct field **fd;)
538         { struct expr *expr; }
539 :
540         {
541                 *fd = new_field();
542         }
543         ':'
544         constant_expression(&expr)
545         {
546                 (*fd)->fd_width = expr->VL_VALUE;
547                 free_expression(expr);
548 #ifdef NOBITFIELD
549                 error("bitfields are not implemented");
550 #endif /* NOBITFIELD */
551         }
552 ;
553
554 /* 8.7 */
555 cast(struct type **tpp;)
556         {struct declarator Dc;}
557 :
558         {Dc = null_declarator;}
559         '('
560         type_specifier(tpp)
561         abstract_declarator(&Dc)
562         ')'
563         {*tpp = declare_type(*tpp, &Dc);}
564         {remove_declarator(&Dc);}
565 ;
566
567 /*      This code is an abject copy of that of 'declarator', for lack of
568         a two-level grammar.
569 */
570 abstract_declarator(register struct declarator *dc;)
571         {       struct proto *pl = NO_PROTO;
572                 arith count;
573                 int qual;
574         }
575 :
576         primary_abstract_declarator(dc)
577         [
578                 '('
579                 [
580                         parameter_type_list(&pl)
581                 |
582                         /* empty */
583                 ]
584                 ')'
585                 {add_decl_unary(dc, FUNCTION, 0, (arith)0, NO_PARAMS, pl);
586                  if (pl) remove_proto_idfs(pl);
587                 }
588         |
589                 arrayer(&count)
590                 {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
591         ]*
592 |
593         pointer(&qual) abstract_declarator(dc)
594         {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
595 ;
596
597 %first first_of_parameter_type_list, parameter_type_list;
598
599 primary_abstract_declarator(struct declarator *dc;)
600 :
601 [%if (AHEAD == ')' || first_of_parameter_type_list(AHEAD))
602         /* empty */
603 |
604         '(' abstract_declarator(dc) ')'
605 ]
606 ;
607
608 parameter_type_list(struct proto **plp;)
609         {       int save_level; }
610 :
611         {       if (level > L_PROTO) {
612                         save_level = level;
613                         level = L_PROTO;
614                 } else level--;
615         }
616         parameter_decl_list(plp)
617         [
618                 ',' ELLIPSIS
619                 {       register struct proto *new = new_proto();
620
621                         new->next = *plp;
622                         new->pl_flag = PL_ELLIPSIS;
623                         *plp = new;
624                 }
625
626         ]?
627         {       check_for_void(*plp);
628                 if (level == L_PROTO)
629                         level = save_level;
630                 else level++;
631         }
632 ;
633
634 parameter_decl_list(struct proto **plp;)
635 :
636         parameter_decl(plp)
637         [ %while (AHEAD != ELLIPSIS)
638           %persistent
639                 ',' parameter_decl(plp)
640         ]*
641 ;
642
643 parameter_decl(struct proto **plp;)
644         {       register struct proto *new = new_proto();
645                 struct declarator Dc;
646                 struct decspecs Ds;
647         }
648 :
649         {       Dc = null_declarator;
650                 Ds = null_decspecs;
651         }
652         decl_specifiers(&Ds)
653         parameter_declarator(&Dc)
654         {       add_proto(new, &Ds, &Dc, level);
655                 new->next = *plp;
656                 *plp = new;
657                 remove_declarator(&Dc);
658         }
659 ;
660
661 /*      This is weird. Due to the LR structure of the ANSI C grammar
662         we have to duplicate the actions of 'declarator' and
663         'abstract_declarator'. Calling these separately, as in
664
665         parameter_decl:
666                 decl_specifiers
667                 [
668                         declarator
669                 |
670                         abstract_declarator
671                 ]
672
673
674         gives us a conflict on the terminals '(' and '*'. E.i. on
675         some input, it is impossible to decide which rule we take.
676         Combining the two declarators into one common declarator
677         is out of the question, since this results in an empty
678         string for the non-terminal 'declarator'.
679         So we combine the two only for the use of parameter_decl,
680         since this is the only place where they don't give
681         conflicts. However, this makes the grammar messy.
682 */
683 parameter_declarator(register struct declarator *dc;)
684         {       struct formal *fm = NO_PARAMS;
685                 struct proto *pl = NO_PROTO;
686                 arith count;
687                 int qual;
688         }
689 :
690         primary_parameter_declarator(dc)
691         [
692                 '('
693                 [ %if (DOT != IDENTIFIER)
694                         parameter_type_list(&pl)
695                 |
696                         formal_list(&fm)
697                 |
698                         /* empty */
699                 ]
700                 ')'
701                 {   add_decl_unary(dc, FUNCTION, 0, (arith)0, fm, pl);
702                     reject_params(dc);
703                 }
704         |
705                 arrayer(&count)
706                 {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
707         ]*
708 |
709         pointer(&qual) parameter_declarator(dc)
710         {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
711 ;
712
713 primary_parameter_declarator(register struct declarator *dc;)
714 :
715 [%if (AHEAD == ')' || first_of_parameter_type_list(AHEAD)
716                                     && (AHEAD != IDENTIFIER))
717         /* empty */
718 |
719         identifier(&dc->dc_idf)
720 |
721         '(' parameter_declarator(dc) ')'
722 ]
723 ;
724
725 pointer(int *qual;)
726 :
727         '*' type_qualifier_list(qual)
728 ;
729
730 /*      Type qualifiers may come in three flavours:
731         volatile, const, const volatile.
732         These all have different semantic properties:
733
734         volatile:
735                 means that the object can be modified
736                 without prior knowledge of the implementation.
737
738         const:
739                 means that the object can not be modified; thus
740                 it's illegal to use this as a l-value.
741
742         const volatile:
743                 means  that the object can be modified without
744                 prior knowledge of the implementation, but may
745                 not be used as a l-value.
746 */
747 /* 3.5.4 */
748 type_qualifier_list(int *qual;)
749 :
750         { *qual = 0; }
751         [
752                 VOLATILE
753                 {       if (*qual & TQ_VOLATILE)
754                                 error("repeated type qualifier");
755                         *qual |= TQ_VOLATILE;
756                 }
757         |
758                 CONST
759                 {       if (*qual & TQ_CONST)
760                                 error("repeated type qualifier");
761                         *qual |= TQ_CONST;
762                 }
763         ]*
764 ;
765
766 empty:
767 ;