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: type.c,v 1.17 1994/06/27 08:03:07 ceriel Exp $ */
6 /* T Y P E D E F I N I T I O N M E C H A N I S M */
8 #include "nobitfield.h"
10 #include "botch_free.h"
22 extern struct type *function_of(), *array_of();
24 extern struct type *field_of();
25 #endif /* NOBITFIELD */
27 /* To be created dynamically in main() from defaults or from command
31 *schar_type, *uchar_type,
32 *short_type, *ushort_type,
33 *word_type, *uword_type,
34 *int_type, *uint_type,
35 *long_type, *ulong_type,
36 *float_type, *double_type, *lngdbl_type,
38 *string_type, *funint_type, *error_type;
40 struct type *pa_type; /* Pointer-Arithmetic type */
46 /* A brand new struct type is created, and its tp_fund set
49 register struct type *ntp = new_type();
52 ntp->tp_size = (arith)-1;
61 if (tp->tp_fund == CHAR || tp->tp_fund == SHORT) {
62 if (tp->tp_unsigned && (int) tp->tp_size == (int) int_size)
65 } else if (tp->tp_fund == FLOAT)
71 construct_type(fund, tp, qual, count, pl)
72 register struct type *tp;
73 register struct proto *pl;
74 arith count; /* for fund == ARRAY only */
77 /* fund must be a type constructor: FIELD, FUNCTION, POINTER or
78 ARRAY. The pointer to the constructed type is returned.
80 register struct type *dtp;
85 dtp = field_of(tp, qual);
87 #endif /* NOBITFIELD */
90 if (tp->tp_fund == FUNCTION) {
91 error("function cannot yield function");
94 if (tp->tp_fund == ARRAY) {
95 error("function cannot yield array");
99 dtp = function_of(tp, pl, qual);
102 dtp = pointer_to(tp, qual);
105 if (tp->tp_fund == VOID) {
106 error("cannot construct array of void");
109 dtp = array_of(tp, count, qual);
112 crash("bad constructor in construct_type");
119 function_of(tp, pl, qual)
120 register struct type *tp;
125 /* See comment below */
126 register struct type *dtp = tp->tp_function;
128 register struct type *dtp;
131 /* look for a type with the right qualifier */
133 /* the code doesn't work in the following case:
135 int func(int a, int b) { return q(a); }
136 because updating the type works inside the data-structures for that type
137 thus, a new type is created for very function. This may change in the
138 future, when declarations with empty parameter lists become obsolete.
139 When it does, change type.str, decspecs.c, and this routine. Search for
140 the function_of pattern to find the places.
142 while (dtp && (dtp->tp_typequal != qual || dtp->tp_proto != pl))
149 dtp = create_type(FUNCTION);
151 dtp->tp_size = -1; /* function size is not known */
152 dtp->tp_align = pointer_align;
153 dtp->tp_typequal = qual;
156 /* See comment above */
157 dtp->next = tp->tp_function;
158 tp->tp_function = dtp;
166 register struct type *tp;
169 register struct type *dtp = tp->tp_pointer;
171 /* look for a type with the right qualifier */
172 while (dtp && dtp->tp_typequal != qual)
176 dtp = create_type(POINTER);
177 dtp->tp_unsigned = 1;
179 dtp->tp_size = pointer_size;
180 dtp->tp_align = pointer_align;
181 dtp->tp_typequal = qual;
182 dtp->next = tp->tp_pointer;
183 tp->tp_pointer = dtp;
189 array_of(tp, count, qual)
190 register struct type *tp;
194 register struct type *dtp = tp->tp_array;
196 /* look for a type with the right size */
197 while (dtp && (dtp->tp_nel != count || dtp->tp_typequal != qual))
201 dtp = create_type(ARRAY);
204 dtp->tp_align = tp->tp_align;
205 dtp->tp_typequal = qual;
206 dtp->next = tp->tp_array;
208 if (tp->tp_size >= 0 && count >= 0) {
209 dtp->tp_size = count * tp->tp_size;
211 else dtp->tp_size = -1;
219 register struct type *tp;
222 register struct type *dtp = create_type(FIELD);
225 dtp->tp_align = tp->tp_align;
226 dtp->tp_size = tp->tp_size;
227 dtp->tp_typequal = qual;
230 #endif /* NOBITFIELD */
237 arith sz = tp->tp_size;
240 error("size of %s unknown", nm);
250 /* Decoding a typedef-ed identifier or basic type: if the
251 size is yet unknown we have to make copy of the type
252 descriptor to prevent garbage at the initialisation of
253 arrays with unknown size.
255 register struct type *tp = idf->id_def->df_type;
257 if (*tpp) error("multiple types in declaration");
258 if ( tp->tp_size < (arith)0 && tp->tp_fund == ARRAY) {
261 /* this is really a structure assignment, AAGH!!! */
273 return ((pos + al - 1) / al) * al;
277 standard_type(fund, sgn, algn, sz)
280 register struct type *tp = create_type(fund);
282 tp->tp_unsigned = sgn != 0;
292 register struct type *atp = tp->tp_array;
293 register struct type *etp = tp;
295 switch(etp->tp_fund) {
299 while (etp = etp->next) {
300 if (! etp->tp_sdef) etp->tp_sdef = tp->tp_sdef;
301 etp->tp_size = tp->tp_size;
302 etp->tp_align = tp->tp_align;
307 if (atp->tp_nel >= 0) {
308 atp->tp_size = atp->tp_nel * tp->tp_size;