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".
5 /* $Id: declar.g,v 1.32 1997/02/21 17:11:03 ceriel Exp $ */
6 /* DECLARATION SYNTAX PARSER */
12 #include "nobitfield.h"
14 #include <flt_arith.h>
35 extern char options[];
44 init_declarator_list(&Ds)?
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;
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.
68 The existence of declarations like
70 makes all hope of writing a specific grammar for typedefs illusory.
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.
87 decl_specifiers /* non-empty */ (register struct decspecs *ds;)
88 /* Reads a non-empty decl_specifiers and fills the struct
92 single_decl_specifier(ds)
93 [ %while( (DOT==TYPE_IDENTIFIER
95 && ds->ds_unsigned == 0
96 && ds->ds_type == (struct type *)0)
97 || AHEAD == IDENTIFIER) /* always an error */
98 single_decl_specifier(ds)
103 single_decl_specifier /* non_empty */ (register struct decspecs *ds;)
105 [ AUTO | STATIC | EXTERN | TYPEDEF | REGISTER ]
106 { if (ds->ds_sc_given)
107 error("repeated storage class specifier");
113 { if (ds->ds_typequal & TQ_VOLATILE)
114 error("repeated type qualifier");
115 ds->ds_typequal |= TQ_VOLATILE;
119 { if (ds->ds_typequal & TQ_CONST)
120 error("repeated type qualifier");
121 ds->ds_typequal |= TQ_CONST;
126 error("repeated size specifier");
130 [ SIGNED | UNSIGNED ]
131 { if (ds->ds_unsigned != 0)
132 error("repeated sign specifier");
133 ds->ds_unsigned = DOT;
136 [ VOID | CHAR | INT | FLOAT | DOUBLE ]
138 idf2type(dot.tk_idf, &ds->ds_type);
142 %default TYPE_IDENTIFIER
144 idf2type(dot.tk_idf, &ds->ds_type);
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;
162 struct_or_union_specifier(&ds->ds_type)
164 enum_specifier(&ds->ds_type)
168 type_specifier(struct type **tpp;)
169 /* Used in struct/union declarations and in casts; only the
172 {struct decspecs Ds; Ds = null_decspecs;}
177 error("storage class ignored");
178 if (Ds.ds_sc == REGISTER)
179 error("register ignored");
185 init_declarator_list(struct decspecs *ds;):
187 [ ',' init_declarator(ds) ]*
190 init_declarator(register struct decspecs *ds;)
192 struct declarator Dc;
196 Dc = null_declarator;
202 declare_idf(ds, &Dc, level);
204 lint_declare_idf(Dc.dc_idf, ds->ds_sc);
208 initializer(Dc.dc_idf, ds->ds_sc)
210 { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
217 remove_declarator(&Dc);
221 /* 3.5.7: initializer */
222 initializer(struct idf *idf; int sc;)
224 struct expr *expr = (struct expr *) 0;
225 int fund = idf->id_def->df_type->tp_fund;
226 int autoagg = (level >= L_LOCAL
231 int globalflag = level == L_GLOBAL
232 || (level >= L_LOCAL && sc == STATIC);
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;
239 if (level == L_FORMAL2)
240 error("illegal initialization of formal parameter");
244 if (AHEAD != '{' && AHEAD != STRING ) autoagg = 0;
250 code_declaration(idf, &ex, level, sc);
253 loc_init((struct expr *) 0, idf);
255 initial_value((globalflag || autoagg) ?
256 &(idf->id_def->df_type)
259 { if (! globalflag) {
260 if (idf->id_def->df_type->tp_fund == FUNCTION) {
261 free_expression(expr);
265 print_expr("initializer-expression", expr);
268 change_state(idf, SET);
271 if (options['g'] && level >= L_LOCAL && expr) {
272 db_line(expr->ex_file, (unsigned) expr->ex_line);
274 #endif /* DBSYMTAB */
276 loc_init((struct expr *) 0, idf);
277 else code_declaration(idf, expr, level, sc);
280 if (options['g'] && globalflag) {
281 stb_string(idf->id_def, sc, idf->id_text);
283 #endif /* DBSYMTAB */
284 idf_initialized(idf);
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
296 declarator(register struct declarator *dc;)
297 { struct formal *fm = NO_PARAMS;
298 struct proto *pl = NO_PROTO;
303 primary_declarator(dc)
306 [ %if (DOT != IDENTIFIER)
307 parameter_type_list(&pl)
314 { add_decl_unary(dc, FUNCTION, 0, (arith)0, fm, pl);
319 {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
322 pointer(&qual) declarator(dc)
323 {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
326 primary_declarator(register struct declarator *dc;) :
327 identifier(&dc->dc_idf)
329 '(' declarator(dc) ')'
332 arrayer(arith *sizep;)
333 { struct expr *expr; }
336 { *sizep = (arith)-1; }
338 constant_expression(&expr)
340 check_array_subscript(expr);
341 *sizep = expr->VL_VALUE;
342 free_expression(expr);
348 formal_list (struct formal **fmp;)
350 formal(fmp) [ %persistent ',' formal(fmp) ]*
353 formal(struct formal **fmp;)
358 register struct formal *new = new_formal();
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);
370 enum_specifier(register struct type **tpp;)
376 {if (*tpp) error("multiple types in declaration");}
379 {declare_struct(ENUM, (struct idf *) 0, tpp);}
380 enumerator_pack(*tpp, &l)
384 {declare_struct(ENUM, idf, tpp);}
385 enumerator_pack(*tpp, &l)
389 stb_tag(idf->id_tag, idf->id_text);
394 {apply_struct(ENUM, idf, tpp);}
400 enumerator_pack(register struct type *tp; arith *lp;) :
403 [%while (AHEAD != '}')
408 ',' {warning("unexpected trailing comma in enumerator pack");}
411 {tp->tp_size = int_size;}
412 /* fancy implementations that put small enums in 1 byte
413 or so should start here.
417 enumerator(struct type *tp; arith *lp;)
426 constant_expression(&expr)
428 *lp = expr->VL_VALUE;
429 free_expression(expr);
432 {declare_enum(tp, idf, (*lp)++);}
436 struct_or_union_specifier(register struct type **tpp;)
440 register struct idf *idf;
443 {if (*tpp) error("multiple types in declaration");}
448 declare_struct(fund, (struct idf *)0, tpp);
450 struct_declaration_pack(*tpp)
452 identifier(&idfX) { idf = idfX; }
455 declare_struct(fund, idf, tpp);
456 (idf->id_tag->tg_busy)++;
458 struct_declaration_pack(*tpp)
460 (idf->id_tag->tg_busy)--;
463 stb_tag(idf->id_tag, idf->id_text);
469 /* a ';' means an empty declaration (probably)
470 * this means that we have to declare a new
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);
485 struct_declaration_pack(register struct type *stp;)
487 struct sdef **sdefp = &stp->tp_sdef;
488 arith size = (arith)0;
491 /* The size is only filled in after the whole struct has
492 been read, to prevent recursive definitions.
495 struct_declaration(stp, &sdefp, &size)+
497 {stp->tp_size = align(size, stp->tp_align);
502 struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
505 type_specifier(&tp) struct_declarator_list(tp, stp, sdefpp, szp) ';'
508 struct_declarator_list(struct type *tp; struct type *stp;
509 struct sdef ***sdefpp; arith *szp;)
511 struct_declarator(tp, stp, sdefpp, szp)
512 [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
515 struct_declarator(struct type *tp; struct type *stp;
516 struct sdef ***sdefpp; arith *szp;)
518 struct declarator Dc;
519 struct field *fd = 0;
523 Dc = null_declarator;
527 {reject_params(&Dc);}
530 {Dc.dc_idf = gen_idf();}
533 {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
534 {remove_declarator(&Dc);}
537 bit_expression(struct field **fd;)
538 { struct expr *expr; }
544 constant_expression(&expr)
546 (*fd)->fd_width = expr->VL_VALUE;
547 free_expression(expr);
549 error("bitfields are not implemented");
550 #endif /* NOBITFIELD */
555 cast(struct type **tpp;)
556 {struct declarator Dc;}
558 {Dc = null_declarator;}
561 abstract_declarator(&Dc)
563 {*tpp = declare_type(*tpp, &Dc);}
564 {remove_declarator(&Dc);}
567 /* This code is an abject copy of that of 'declarator', for lack of
570 abstract_declarator(register struct declarator *dc;)
571 { struct proto *pl = NO_PROTO;
576 primary_abstract_declarator(dc)
580 parameter_type_list(&pl)
585 {add_decl_unary(dc, FUNCTION, 0, (arith)0, NO_PARAMS, pl);
586 if (pl) remove_proto_idfs(pl);
590 {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
593 pointer(&qual) abstract_declarator(dc)
594 {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
597 %first first_of_parameter_type_list, parameter_type_list;
599 primary_abstract_declarator(struct declarator *dc;)
601 [%if (AHEAD == ')' || first_of_parameter_type_list(AHEAD))
604 '(' abstract_declarator(dc) ')'
608 parameter_type_list(struct proto **plp;)
611 { if (level > L_PROTO) {
616 parameter_decl_list(plp)
619 { register struct proto *new = new_proto();
622 new->pl_flag = PL_ELLIPSIS;
627 { check_for_void(*plp);
628 if (level == L_PROTO)
634 parameter_decl_list(struct proto **plp;)
637 [ %while (AHEAD != ELLIPSIS)
639 ',' parameter_decl(plp)
643 parameter_decl(struct proto **plp;)
644 { register struct proto *new = new_proto();
645 struct declarator Dc;
649 { Dc = null_declarator;
653 parameter_declarator(&Dc)
654 { add_proto(new, &Ds, &Dc, level);
657 remove_declarator(&Dc);
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
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.
683 parameter_declarator(register struct declarator *dc;)
684 { struct formal *fm = NO_PARAMS;
685 struct proto *pl = NO_PROTO;
690 primary_parameter_declarator(dc)
693 [ %if (DOT != IDENTIFIER)
694 parameter_type_list(&pl)
701 { add_decl_unary(dc, FUNCTION, 0, (arith)0, fm, pl);
706 {add_decl_unary(dc, ARRAY, 0, count, NO_PARAMS, NO_PROTO);}
709 pointer(&qual) parameter_declarator(dc)
710 {add_decl_unary(dc, POINTER, qual, (arith)0, NO_PARAMS, NO_PROTO);}
713 primary_parameter_declarator(register struct declarator *dc;)
715 [%if (AHEAD == ')' || first_of_parameter_type_list(AHEAD)
716 && (AHEAD != IDENTIFIER))
719 identifier(&dc->dc_idf)
721 '(' parameter_declarator(dc) ')'
727 '*' type_qualifier_list(qual)
730 /* Type qualifiers may come in three flavours:
731 volatile, const, const volatile.
732 These all have different semantic properties:
735 means that the object can be modified
736 without prior knowledge of the implementation.
739 means that the object can not be modified; thus
740 it's illegal to use this as a l-value.
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.
748 type_qualifier_list(int *qual;)
753 { if (*qual & TQ_VOLATILE)
754 error("repeated type qualifier");
755 *qual |= TQ_VOLATILE;
759 { if (*qual & TQ_CONST)
760 error("repeated type qualifier");