Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / type.c
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: 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        */
7
8 #include        "nobitfield.h"
9 #include        "debug.h"
10 #include        "botch_free.h"
11 #include        <alloc.h>
12 #include        "Lpars.h"
13 #include        "arith.h"
14 #include        "type.h"
15 #include        "idf.h"
16 #include        "def.h"
17 #include        "proto.h"
18 #include        "sizes.h"
19 #include        "align.h"
20 #include        "decspecs.h"
21
22 extern struct type *function_of(), *array_of();
23 #ifndef NOBITFIELD
24 extern struct type *field_of();
25 #endif /* NOBITFIELD */
26
27 /*      To be created dynamically in main() from defaults or from command
28         line parameters.
29 */
30 struct type
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,
37         *void_type,
38         *string_type, *funint_type, *error_type;
39
40 struct type *pa_type;   /* Pointer-Arithmetic type      */
41
42 struct type *
43 create_type(fund)
44         int fund;
45 {
46         /*      A brand new struct type is created, and its tp_fund set
47                 to fund.
48         */
49         register struct type *ntp = new_type();
50
51         ntp->tp_fund = fund;
52         ntp->tp_size = (arith)-1;
53
54         return ntp;
55 }
56
57 struct type *
58 promoted_type(tp)
59 struct type *tp;
60 {
61         if (tp->tp_fund == CHAR || tp->tp_fund == SHORT) {
62                 if (tp->tp_unsigned && (int) tp->tp_size == (int) int_size)
63                         return uint_type;
64                 else return int_type;
65         } else if (tp->tp_fund == FLOAT)
66                 return double_type;
67         else return tp;
68 }
69
70 struct type *
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 */
75         int qual;
76 {
77         /*      fund must be a type constructor: FIELD, FUNCTION, POINTER or
78                 ARRAY. The pointer to the constructed type is returned.
79         */
80         register struct type *dtp;
81
82         switch (fund)   {
83 #ifndef NOBITFIELD
84         case FIELD:
85                 dtp = field_of(tp, qual);
86                 break;
87 #endif /* NOBITFIELD */
88
89         case FUNCTION:
90                 if (tp->tp_fund == FUNCTION)    {
91                         error("function cannot yield function");
92                         return error_type;
93                 }
94                 if (tp->tp_fund == ARRAY)       {
95                         error("function cannot yield array");
96                         return error_type;
97                 }
98
99                 dtp = function_of(tp, pl, qual);
100                 break;
101         case POINTER:
102                 dtp = pointer_to(tp, qual);
103                 break;
104         case ARRAY:
105                 if (tp->tp_fund == VOID) {
106                         error("cannot construct array of void");
107                         count = (arith) -1;
108                 }
109                 dtp = array_of(tp, count, qual);
110                 break;
111         default:
112                 crash("bad constructor in construct_type");
113                 /*NOTREACHED*/
114         }
115         return dtp;
116 }
117
118 struct type *
119 function_of(tp, pl, qual)
120         register struct type *tp;
121         struct proto *pl;
122         int qual;
123 {
124 #if 0
125 /* See comment below */
126         register struct type *dtp = tp->tp_function;
127 #else
128         register struct type *dtp;
129 #endif
130
131         /* look for a type with the right qualifier */
132 #if 0
133 /* the code doesn't work in the following case:
134         int func();
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.
141 */
142         while (dtp && (dtp->tp_typequal != qual || dtp->tp_proto != pl))
143                 dtp = dtp->next;
144 #else
145         dtp = 0;
146 #endif
147
148         if (!dtp)       {
149                 dtp = create_type(FUNCTION);
150                 dtp->tp_up = tp;
151                 dtp->tp_size = -1;      /* function size is not known */
152                 dtp->tp_align = pointer_align;
153                 dtp->tp_typequal = qual;
154                 dtp->tp_proto = pl;
155 #if 0
156 /* See comment above */
157                 dtp->next = tp->tp_function;
158                 tp->tp_function = dtp;
159 #endif
160         }
161         return dtp;
162 }
163
164 struct type *
165 pointer_to(tp, qual)
166         register struct type *tp;
167         int qual;
168 {
169         register struct type *dtp = tp->tp_pointer;
170
171         /* look for a type with the right qualifier */
172         while (dtp && dtp->tp_typequal != qual)
173                 dtp = dtp->next;
174
175         if (!dtp)       {
176                 dtp = create_type(POINTER);
177                 dtp->tp_unsigned = 1;
178                 dtp->tp_up = tp;
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;
184         }
185         return dtp;
186 }
187
188 struct type *
189 array_of(tp, count, qual)
190         register struct type *tp;
191         arith count;
192         int qual;
193 {
194         register struct type *dtp = tp->tp_array;
195
196         /* look for a type with the right size */
197         while (dtp && (dtp->tp_nel != count || dtp->tp_typequal != qual))
198                 dtp = dtp->next;
199
200         if (!dtp)       {
201                 dtp = create_type(ARRAY);
202                 dtp->tp_up = tp;
203                 dtp->tp_nel = count;
204                 dtp->tp_align = tp->tp_align;
205                 dtp->tp_typequal = qual;
206                 dtp->next = tp->tp_array;
207                 tp->tp_array = dtp;
208                 if (tp->tp_size >= 0 && count >= 0) {
209                         dtp->tp_size = count * tp->tp_size;
210                 }
211                 else    dtp->tp_size = -1;
212         }
213         return dtp;
214 }
215
216 #ifndef NOBITFIELD
217 struct type *
218 field_of(tp, qual)
219         register struct type *tp;
220         int qual;
221 {
222         register struct type *dtp = create_type(FIELD);
223
224         dtp->tp_up = tp;
225         dtp->tp_align = tp->tp_align;
226         dtp->tp_size = tp->tp_size;
227         dtp->tp_typequal = qual;
228         return dtp;
229 }
230 #endif /* NOBITFIELD */
231
232 arith
233 size_of_type(tp, nm)
234         struct type *tp;
235         char nm[];
236 {
237         arith sz = tp->tp_size;
238
239         if (sz < 0)     {
240                 error("size of %s unknown", nm);
241                 sz = (arith)1;
242         }
243         return sz;
244 }
245
246 idf2type(idf, tpp)
247         struct idf *idf;
248         struct type **tpp;
249 {
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.
254         */
255         register struct type *tp = idf->id_def->df_type;
256
257         if (*tpp) error("multiple types in declaration");
258         if (    tp->tp_size < (arith)0 && tp->tp_fund == ARRAY) {
259                 *tpp = new_type();
260                 **tpp = *tp;
261                         /* this is really a structure assignment, AAGH!!! */
262         }
263         else    {
264                 *tpp = tp;
265         }
266 }
267
268 arith
269 align(pos, al)
270         arith pos;
271         int al;
272 {
273         return ((pos + al - 1) / al) * al;
274 }
275
276 struct type *
277 standard_type(fund, sgn, algn, sz)
278         int algn; arith sz;
279 {
280         register struct type *tp = create_type(fund);
281
282         tp->tp_unsigned = sgn != 0;
283         tp->tp_align = algn;
284         tp->tp_size = sz;
285
286         return tp;
287 }
288
289 completed(tp)
290         struct type *tp;
291 {
292         register struct type *atp = tp->tp_array;
293         register struct type *etp = tp;
294
295         switch(etp->tp_fund) {
296         case STRUCT:
297         case UNION:
298         case ENUM:
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;
303                 }
304                 break;
305         }
306         while (atp) {
307                 if (atp->tp_nel >= 0) {
308                         atp->tp_size = atp->tp_nel * tp->tp_size;
309                 }
310                 atp = atp->next;
311         }
312 }