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: struct.c,v 3.18 1994/06/24 12:06:21 ceriel Exp $ */
6 /* ADMINISTRATION OF STRUCT AND UNION DECLARATIONS */
8 #include "nobitfield.h"
10 #include "botch_free.h"
25 #include "noRoption.h"
27 /* Type of previous selector declared with a field width specified,
28 if any. If a selector is declared with no field with it is set to 0.
30 static field_busy = 0;
32 extern char options[];
35 /* The semantics of the identification of structure/union tags is
36 obscure. Some highly regarded compilers are found out to accept,
38 f(xp) struct aap *xp; {
39 struct aap {char *za;};
42 Equally highly regarded software uses this feature, so we shall
45 1. A structure definition applies at the level where it is
46 found, unless there is a structure declaration without a
47 definition on an outer level, in which case the definition
48 is applied at that level.
49 2. A selector is applied on the same level as on which its
50 structure is being defined.
52 If below struct is mentioned, union is implied (and sometimes enum
56 add_sel(stp, tp, idf, sdefpp, szp, fd) /* this is horrible */
57 register struct type *stp; /* type of the structure */
58 struct type *tp; /* type of the selector */
59 register struct idf *idf; /* idf of the selector */
60 struct sdef ***sdefpp; /* address of hook to selector definition */
61 arith *szp; /* pointer to struct size upto here */
64 /* The selector idf with type tp is added to two chains: the
65 selector identification chain starting at idf->id_sdef,
66 and to the end of the member list starting at stp->tp_sdef.
67 The address of the hook in the latest member (sdef) is
68 given in sdefpp; the hook itself must still be empty.
72 extern arith add_field();
73 #endif /* NOBITFIELD */
75 struct tag *tg = stp->tp_idf->id_struct; /* or union */
76 struct sdef *sdef = idf->id_sdef;
77 register struct sdef *newsdef;
78 int lvl = tg->tg_level;
81 if (options['R'] && !is_anon_idf(idf)) {
83 if (idf->id_struct && idf->id_struct->tg_level == level)
84 warning("%s is also a struct/union tag", idf->id_text);
88 if (stp->tp_fund == STRUCT) {
90 if (fd == 0) { /* no field width specified */
91 offset = align(*szp, tp->tp_align);
95 /* if something is wrong, the type of the
96 specified selector remains unchanged; its
97 bitfield specifier, however, is thrown away.
99 offset = add_field(szp, fd, &tp, idf, stp);
101 #else /* NOBITFIELD */
102 offset = align(*szp, tp->tp_align);
104 #endif /* NOBITFIELD */
106 else { /* (stp->tp_fund == UNION) */
108 error("fields not allowed in unions");
115 check_selector(idf, stp);
118 if ( sdef && sdef->sd_level == lvl &&
119 ( sdef->sd_offset != offset || sdef->sd_type != tp )
121 warning("selector %s redeclared", idf->id_text);
125 newsdef = new_sdef();
126 newsdef->sd_sdef = (struct sdef *) 0;
128 /* link into selector descriptor list of this id
130 newsdef->next = sdef;
131 idf->id_sdef = newsdef;
133 newsdef->sd_level = lvl;
134 newsdef->sd_idf = idf;
135 newsdef->sd_stype = stp;
136 newsdef->sd_type = tp;
137 newsdef->sd_offset = offset;
140 if (tp->tp_fund == FIELD)
141 tp->tp_field->fd_sdef = newsdef;
142 #endif /* NOBITFIELD */
144 stack_idf(idf, stack_level_of(lvl));
146 /* link into selector definition list of the struct/union
149 *sdefpp = &newsdef->sd_sdef;
151 /* update the size of the struct/union upward */
152 if (stp->tp_fund == STRUCT && fd == 0) {
153 /* Note: the case that a bitfield is declared is
154 handled by add_field() !
156 *szp = offset + size_of_type(tp, "member");
157 stp->tp_align = lcm(stp->tp_align, tp->tp_align);
160 if (stp->tp_fund == UNION) {
161 arith sel_size = size_of_type(tp, "member");
165 stp->tp_align = lcm(stp->tp_align, tp->tp_align);
169 check_selector(idf, stp)
170 register struct idf *idf;
171 struct type *stp; /* the type of the struct */
173 /* checks if idf occurs already as a selector in
174 struct or union *stp.
176 register struct sdef *sdef = stp->tp_sdef;
179 if (sdef->sd_idf == idf)
180 error("multiple selector %s", idf->id_text);
181 sdef = sdef->sd_sdef;
187 declare_struct(fund, idf, tpp)
188 register struct idf *idf;
191 /* A struct, union or enum (depending on fund) with tag (!)
192 idf is declared, and its type (incomplete as it may be) is
194 The idf may be missing (i.e. idf == 0), in which case an
195 anonymous struct etc. is defined.
197 register struct tag **tgp;
198 register struct tag *tg;
202 tgp = (fund == ENUM ? &idf->id_enum : &idf->id_struct);
205 if (options['R'] && !is_anon_idf(idf)) {
208 idf->id_sdef && idf->id_sdef->sd_level == level
210 warning("%s is also a selector", idf->id_text);
213 idf->id_def && idf->id_def->df_level == level
215 warning("%s is also a variable", idf->id_text);
221 if (tg && tg->tg_type->tp_size < 0 && tg->tg_type->tp_fund == fund) {
222 /* An unfinished declaration has preceded it, possibly on
223 an earlier level. We just fill in the answer.
226 error("recursive declaration of struct/union %s",
228 declare_struct(fund, gen_idf(), tpp);
232 if (options['R'] && tg->tg_level != level)
233 warning("%s declares %s in different range",
234 idf->id_text, symbol2str(fund));
240 if (tg && tg->tg_level == level) {
241 /* There is an already defined struct/union of this name
244 error("redeclaration of struct/union %s", idf->id_text);
245 declare_struct(fund, gen_idf(), tpp);
246 /* to allow a second struct_declaration_pack */
249 /* The struct is new. */
250 /* Hook in a new struct tag */
254 tg->tg_level = level;
255 /* and supply room for a type */
256 tg->tg_type = create_type(fund);
257 tg->tg_type->tp_align =
258 fund == ENUM ? int_align :
259 fund == STRUCT ? struct_align :
260 /* fund == UNION */ union_align;
261 tg->tg_type->tp_idf = idf;
263 stack_idf(idf, local_level);
267 apply_struct(fund, idf, tpp)
268 register struct idf *idf;
271 /* The occurrence of a struct, union or enum (depending on
272 fund) with tag idf is noted. It may or may not have been
273 declared before. Its type (complete or incomplete) is
276 register struct tag **tgp;
278 tgp = (is_struct_or_union(fund) ? &idf->id_struct : &idf->id_enum);
281 *tpp = (*tgp)->tg_type;
283 declare_struct(fund, idf, tpp);
288 register struct idf *idf;
291 /* The identifier idf is identified as a selector, preferably
292 in the struct tp, but we will settle for any unique
294 If the attempt fails, a selector of type error_type is
297 register struct sdef **sdefp = &idf->id_sdef, *sdef;
299 /* Follow chain from idf, to meet tp. */
300 while ((sdef = *sdefp)) {
301 if (sdef->sd_stype == tp)
303 sdefp = &(*sdefp)->next;
306 /* Tp not met; any unique identification will do. */
307 if (sdef = idf->id_sdef) {
308 /* There is an identification */
309 if (uniq_selector(sdef)) {
310 /* and it is unique, so we accept */
311 warning("selector %s applied to alien type",
315 /* it is ambiguous */
316 error("ambiguous use of selector %s", idf->id_text);
321 /* No luck; create an error entry. */
322 if (!is_anon_idf(idf))
323 error("unknown selector %s", idf->id_text);
324 *sdefp = sdef = new_sdef();
326 sdef->sd_stype = sdef->sd_type = error_type;
331 uniq_selector(idf_sdef)
332 register struct sdef *idf_sdef;
334 /* Returns true if idf_sdef (which is guaranteed to exist)
335 is unique for this level, i.e there is no other selector
336 on this level with the same name or the other selectors
337 with the same name have the same offset.
338 See /usr/src/cmd/sed/sed.h for an example of this absurd
342 register struct sdef *sdef = idf_sdef->next;
344 while (sdef && sdef->sd_level == idf_sdef->sd_level) {
345 if ( sdef->sd_type != idf_sdef->sd_type
346 || sdef->sd_offset != idf_sdef->sd_offset
348 return 0; /* ambiguity found */
357 add_field(szp, fd, fdtpp, idf, stp)
358 arith *szp; /* size of struct upto here */
359 register struct field *fd; /* bitfield, containing width */
360 register struct type **fdtpp; /* type of selector */
361 struct idf *idf; /* name of selector */
362 register struct type *stp; /* current struct descriptor */
364 /* The address where this selector is put is returned. If the
365 selector with specified width does not fit in the word, or
366 an explicit alignment is given, a new address is needed.
367 Note that the fields are packed into machine words (according
370 int bits_in_type = (int) (*fdtpp)->tp_size * 8;
371 static int field_offset = (arith)0;
372 static struct type *current_struct = 0;
373 static int bits_declared; /* nr of bits used in *field_offset */
375 if (current_struct != stp) {
376 /* This struct differs from the last one
379 current_struct = stp;
382 if ( fd->fd_width < 0 ||
383 (fd->fd_width == 0 && !is_anon_idf(idf)) ||
384 fd->fd_width > bits_in_type
386 error("illegal field-width specified");
391 switch ((*fdtpp)->tp_fund) {
397 /* right type; size OK? */
398 if ((int) (*fdtpp)->tp_size > (int) word_size) {
399 error("bit field type %s does not fit in a word",
400 symbol2str((*fdtpp)->tp_fund));
407 /* wrong type altogether */
408 error("field type cannot be %s",
409 symbol2str((*fdtpp)->tp_fund));
414 if (field_busy == 0) {
415 /* align this selector on the next boundary :
416 the previous selector wasn't a bitfield.
418 field_offset = align(*szp, int_align);
419 *szp = field_offset + int_size;
420 stp->tp_align = lcm(stp->tp_align, int_align);
425 if (fd->fd_width > bits_in_type - bits_declared) {
426 /* field overflow: fetch next memory unit
428 field_offset = align(*szp, int_align);
429 *szp = field_offset + int_size;
430 stp->tp_align = lcm(stp->tp_align, int_align);
431 bits_declared = fd->fd_width;
434 if (fd->fd_width == 0)
435 /* next field should be aligned on the next boundary.
436 This will take care that no field will fit in the
437 space allocated upto here.
439 bits_declared = bits_in_type + 1;
440 else /* the bitfield fits in the current field */
441 bits_declared += fd->fd_width;
443 /* Arrived here, the place where the selector is stored in the
445 Now we need a mask to use its value in expressions.
447 *fdtpp = construct_type(FIELD, *fdtpp, (arith)0);
448 (*fdtpp)->tp_field = fd;
450 /* Set the mask right shifted. This solution avoids the
451 problem of having sign extension when using the mask for
452 extracting the value from the field-int.
453 Sign extension could occur on some machines when shifting
454 the mask to the left.
456 if (fd->fd_width >= 8*sizeof(arith)) fd->fd_mask = -1;
457 else fd->fd_mask = (1L << fd->fd_width) - 1;
459 if (options['r']) /* adjust the field at the right */
460 fd->fd_shift = bits_declared - fd->fd_width;
461 else /* adjust the field at the left */
462 fd->fd_shift = bits_in_type - bits_declared;
466 #endif /* NOBITFIELD */
470 is_struct_or_union(fund)
473 return fund == STRUCT || fund == UNION;
476 /* Greatest Common Divisor
492 /* Least Common Multiple
498 return m * (n / gcd(m, n));