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 3.27 1997/02/21 17:10:40 ceriel Exp $ */
6 /* DECLARATION SYNTAX PARSER */
12 #include "nobitfield.h"
33 extern char options[];
42 [%default /* missing identifier derailed parser */
43 init_declarator_list(&Ds)
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;
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.
70 The existence of declarations like
72 makes all hope of writing a specific grammar for typedefs illusory.
75 decl_specifiers /* non-empty */ (register struct decspecs *ds;)
76 /* Reads a non-empty decl_specifiers and fills the struct
82 [%if (DOT != IDENTIFIER || AHEAD == IDENTIFIER)
83 /* the thin ice in R.M. 11.1 */
84 single_type_specifier(ds) other_specifier(ds)*
89 single_type_specifier(ds) other_specifier(ds)*
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");
105 error("repeated size specifier");
110 { if (ds->ds_unsigned)
111 error("unsigned specified twice");
117 type_specifier(struct type **tpp;)
118 /* Used in struct/union declarations and in casts; only the
121 {struct decspecs Ds; Ds = null_decspecs;}
126 error("storage class ignored");
127 if (Ds.ds_sc == REGISTER)
128 error("register ignored");
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);}
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;
151 struct_or_union_specifier(&ds->ds_type)
153 enum_specifier(&ds->ds_type)
157 init_declarator_list(struct decspecs *ds;):
159 [ ',' init_declarator(ds) ]*
162 init_declarator(register struct decspecs *ds;)
164 struct declarator Dc;
168 Dc = null_declarator;
174 declare_idf(ds, &Dc, level);
176 lint_declare_idf(Dc.dc_idf, ds->ds_sc);
180 initializer(Dc.dc_idf, ds->ds_sc)
182 { code_declaration(Dc.dc_idf, (struct expr *) 0, level, ds->ds_sc); }
189 remove_declarator(&Dc);
193 /* 8.6: initializer */
194 initializer(struct idf *idf; int sc;)
196 struct expr *expr = (struct expr *) 0;
197 int globalflag = level == L_GLOBAL ||
198 (level >= L_LOCAL && sc == STATIC);
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;
205 if (level == L_FORMAL2)
206 warning("illegal initialization of formal parameter (ignored)");
208 '=' /* used to be optional because of V6 */
215 code_declaration(idf, &ex, level, sc);
218 initial_value(globalflag ? &(idf->id_def->df_type) : (struct type **)0,
220 { if (! globalflag) {
221 if (idf->id_def->df_type->tp_fund == FUNCTION) {
222 free_expression(expr);
226 print_expr("initializer-expression", expr);
229 change_state(idf, SET);
232 if (options['g'] && level >= L_LOCAL && expr) {
233 db_line(expr->ex_file, (unsigned) expr->ex_line)
236 #endif /* DBSYMTAB */
237 code_declaration(idf, expr, level, sc);
240 if (options['g'] && globalflag) {
241 stb_string(idf->id_def, sc, idf->id_text);
243 #endif /* DBSYMTAB */
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
255 declarator(register struct declarator *dc;)
258 struct formal *fm = 0;
261 primary_declarator(dc)
264 formal_list(&fm) ? /* semantic check later... */
267 add_decl_unary(dc, FUNCTION, (arith)0, fm);
272 {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
276 {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
279 primary_declarator(register struct declarator *dc;) :
280 identifier(&dc->dc_idf)
282 '(' declarator(dc) ')'
285 arrayer(arith *sizep;)
286 { struct expr *expr; }
290 constant_expression(&expr)
292 check_array_subscript(expr);
293 *sizep = expr->VL_VALUE;
294 free_expression(expr);
298 { *sizep = (arith)-1; }
303 formal_list (struct formal **fmp;)
305 formal(fmp) [ %persistent ',' formal(fmp) ]*
308 formal(struct formal **fmp;)
313 register struct formal *new = new_formal();
322 enum_specifier(register struct type **tpp;)
330 {declare_struct(ENUM, (struct idf *) 0, tpp);}
331 enumerator_pack(*tpp, &l)
335 {declare_struct(ENUM, idf, tpp);}
336 enumerator_pack(*tpp, &l)
340 stb_tag(idf->id_enum, idf->id_text);
345 {apply_struct(ENUM, idf, tpp);}
351 enumerator_pack(register struct type *tp; arith *lp;) :
354 [%while(AHEAD != '}') /* >>> conflict on ',' */
358 ','? /* optional trailing comma */
360 {tp->tp_size = int_size;}
361 /* fancy implementations that put small enums in 1 byte
362 or so should start here.
366 enumerator(struct type *tp; arith *lp;)
375 constant_expression(&expr)
377 *lp = expr->VL_VALUE;
378 free_expression(expr);
381 {declare_enum(tp, idf, (*lp)++);}
385 struct_or_union_specifier(register struct type **tpp;)
389 register struct idf *idf;
396 declare_struct(fund, (struct idf *)0, tpp);
398 struct_declaration_pack(*tpp)
400 identifier(&idfX) { idf = idfX; }
403 declare_struct(fund, idf, tpp);
404 (idf->id_struct->tg_busy)++;
406 struct_declaration_pack(*tpp)
408 (idf->id_struct->tg_busy)--;
411 stb_tag(idf->id_struct, idf->id_text);
416 {apply_struct(fund, idf, tpp);}
422 struct_declaration_pack(register struct type *stp;)
424 struct sdef **sdefp = &stp->tp_sdef;
425 arith size = (arith)0;
428 /* The size is only filled in after the whole struct has
429 been read, to prevent recursive definitions.
432 struct_declaration(stp, &sdefp, &size)+
434 {stp->tp_size = align(size, stp->tp_align);}
437 struct_declaration(struct type *stp; struct sdef ***sdefpp; arith *szp;)
441 struct_declarator_list(tp, stp, sdefpp, szp)
442 [ /* in some standard UNIX compilers the semicolon
443 is optional, would you believe!
448 {warning("no semicolon after declarator");}
452 struct_declarator_list(struct type *tp; struct type *stp;
453 struct sdef ***sdefpp; arith *szp;)
455 struct_declarator(tp, stp, sdefpp, szp)
456 [ ',' struct_declarator(tp, stp, sdefpp, szp) ]*
459 struct_declarator(struct type *tp; struct type *stp;
460 struct sdef ***sdefpp; arith *szp;)
462 struct declarator Dc;
463 struct field *fd = 0;
467 Dc = null_declarator;
471 {reject_params(&Dc);}
474 {Dc.dc_idf = gen_idf();}
477 {add_sel(stp, declare_type(tp, &Dc), Dc.dc_idf, sdefpp, szp, fd);}
478 {remove_declarator(&Dc);}
481 bit_expression(struct field **fd;)
482 { struct expr *expr; }
488 constant_expression(&expr)
490 (*fd)->fd_width = expr->VL_VALUE;
491 free_expression(expr);
493 error("bitfields are not implemented");
494 #endif /* NOBITFIELD */
499 cast(struct type **tpp;) {struct declarator Dc;} :
500 {Dc = null_declarator;}
503 abstract_declarator(&Dc)
505 {*tpp = declare_type(*tpp, &Dc);}
506 {remove_declarator(&Dc);}
509 /* This code is an abject copy of that of 'declarator', for lack of
512 abstract_declarator(register struct declarator *dc;)
515 primary_abstract_declarator(dc)
518 {add_decl_unary(dc, FUNCTION, (arith)0, NO_PARAMS);}
521 {add_decl_unary(dc, ARRAY, count, NO_PARAMS);}
524 '*' abstract_declarator(dc)
525 {add_decl_unary(dc, POINTER, (arith)0, NO_PARAMS);}
528 primary_abstract_declarator(struct declarator *dc;) :
532 '(' abstract_declarator(dc) ')'
540 /* included in the IDENTIFIER/TYPE_IDENTIFIER mechanism */