|
|
1.1 root 1: /* -*- Mode:C++ -*- */
2: /*ident "@(#)ctrans:src/typ2.c 1.1.5.15" */
3: /*
4: $Source: /usr3/lang/benson/work/stripped_cfront/RCS/typ2.c,v $ $RCSfile: typ2.c,v $
5: $Revision: 1.1 $ $Date: 89/11/20 08:51:09 $
6: $Author: benson $ $Locker: $
7: $State: Exp $
8:
9:
10: */
11: /**************************************************************************
12:
13: C++ source for cfront, the C++ compiler front-end
14: written in the computer science research center of Bell Labs
15:
16: Copyright (c) 1984 AT&T, Inc. All rigths Reserved
17: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T, INC.
18:
19: typ2.c:
20:
21: ***************************************************************************/
22:
23: #include "cfront.h"
24: #include "size.h"
25:
26: extern int chars_in_largest;
27: int largest_int;
28:
29: void typ_init()
30: {
31: chars_in_largest = strlen(LARGEST_INT);
32: largest_int = int(str_to_long(LARGEST_INT));
33:
34: defa_type = new basetype(INT,0); // note defa_type!=int_type
35: int_type = new basetype(INT,0); // but they both represent `int'
36:
37: PERM(int_type); int_type->defined = DEFINED ;
38: PERM(defa_type); defa_type->defined = DEFINED ;
39:
40: moe_type = new basetype(INT,0);
41: PERM(moe_type); moe_type->defined = DEFINED ;
42: moe_type->b_const = 1;
43: moe_type->check(0);
44:
45: uint_type = new basetype(INT,0);
46: PERM(uint_type); uint_type->defined = DEFINED ;
47: uint_type->type_adj(UNSIGNED);
48: uint_type->check(0);
49:
50: long_type = new basetype(LONG,0);
51: PERM(long_type); long_type->defined = DEFINED ;
52: long_type->check(0);
53:
54: ulong_type = new basetype(LONG,0);
55: PERM(ulong_type); ulong_type->defined = DEFINED ;
56: ulong_type->type_adj(UNSIGNED);
57: ulong_type->check(0);
58:
59: short_type = new basetype(SHORT,0);
60: PERM(short_type); short_type->defined = DEFINED ;
61: short_type->check(0);
62:
63: ushort_type = new basetype(SHORT,0);
64: PERM(ushort_type); ushort_type->defined = DEFINED ;
65: ushort_type->type_adj(UNSIGNED);
66: ushort_type->check(0);
67:
68: float_type = new basetype(FLOAT,0);
69: PERM(float_type); float_type->defined = DEFINED ;
70:
71: double_type = new basetype(DOUBLE,0);
72: PERM(double_type); double_type->defined = DEFINED ;
73:
74: ldouble_type = new basetype(LDOUBLE,0);
75: PERM(ldouble_type); ldouble_type->defined = DEFINED ;
76:
77: zero_type = new basetype(ZTYPE,0);
78: PERM(zero_type); zero_type->defined = DEFINED ;
79: zero->tp = zero_type;
80:
81: void_type = new basetype(VOID,0);
82: PERM(void_type); void_type->defined = DEFINED ;
83:
84: char_type = new basetype(CHAR,0);
85: PERM(char_type); char_type->defined = DEFINED ;
86:
87: uchar_type = new basetype(CHAR,0);
88: PERM(uchar_type); uchar_type->defined = DEFINED ;
89: uchar_type->type_adj(UNSIGNED);
90: uchar_type->check(0);
91:
92: Pchar_type = char_type->addrof();
93: PERM(Pchar_type); Pchar_type->defined = DEFINED ;
94:
95: Pint_type = int_type->addrof();
96: PERM(Pint_type); Pint_type->defined = DEFINED ;
97:
98: Pvoid_type = void_type->addrof();
99: PERM(Pvoid_type); Pvoid_type->defined = DEFINED ;
100:
101: Pfctvec_type = new fct(int_type,0,0); // must be last, see basetype::normalize()
102: Pfctvec_type = Pfctvec_type->addrof();
103: Pfctvec_type = Pfctvec_type->addrof();
104: PERM(Pfctvec_type); Pfctvec_type->defined = DEFINED ;
105:
106: any_tbl = new table(TBLSIZE,0,0);
107: gtbl = new table(GTBLSIZE,0,0);
108: gtbl->t_name = new name("global");
109: }
110:
111: Pbase basetype::arit_conv(Pbase t)
112: /*
113: perform the "usual arithmetic conversions" C ref Manual 6.6
114: on "this" op "t"
115: "this" and "t" are integral or floating
116: "t" may be 0
117: */
118: {
119: while (base == TYPE) this = Pbase(Pbase(this)->b_name->tp);
120:
121: bit l;
122: bit u;
123: bit f;
124: bit l1 = (base == LONG);
125: bit u1 = b_unsigned;
126: bit f1 = (base==FLOAT || base==DOUBLE || base==LDOUBLE);
127: if (t) {
128: while (t->base == TYPE) t = Pbase(Pbase(t)->b_name->tp);
129: bit l2 = (t->base == LONG);
130: bit u2 = t->b_unsigned;
131: bit f2 = (t->base==FLOAT || t->base==DOUBLE || base==LDOUBLE);
132: l = l1 || l2;
133: u = u1 || u2;
134: f = f1 || f2;
135: }
136: else {
137: l = l1;
138: u = u1;
139: f = f1;
140: }
141:
142: if (f) {
143: if (base==LDOUBLE || (t && t->base==LDOUBLE)) return ldouble_type;
144: if (base==DOUBLE || (t && t->base==DOUBLE)) return double_type;
145: return float_type;
146: }
147: if (l & u) return ulong_type;
148: if (l & !u) return long_type;
149: if (u) {
150: if (base==INT || (t && t->base==INT)) return uint_type;
151: if (SZ_SHORT==SZ_INT) // ANSIism
152: if (base==SHORT || (t && t->base==SHORT)) return uint_type;
153: return int_type;
154: }
155: return int_type;
156: }
157:
158: bit vec_const = 0;
159: bit fct_const = 0;
160:
161: bit type::tconst()
162: /*
163: is this type a constant
164: */
165: {
166: Ptype t = this;
167: vec_const = 0;
168: fct_const = 0;
169: //error('d',"tconst %t",t);
170: xxx:
171: switch (t->base) {
172: case TYPE:
173: if (Pbase(t)->b_const) return 1;
174: t = Pbase(t)->b_name->tp;
175: goto xxx;
176: case VEC:
177: vec_const = 1;
178: return 1;
179: case PTR:
180: case RPTR:
181: return Pptr(t)->rdo;
182: case FCT:
183: case OVERLOAD:
184: fct_const = 1;
185: return 1;
186: default:
187: return Pbase(t)->b_const;
188: }
189: }
190:
191: TOK type::set_const(bit mode)
192: /*
193: make someting a constant or variable, return old status
194: */
195: {
196: Ptype t = this;
197: int m;
198: xxx:
199: switch (t->base) {
200: case TYPE:
201: m = Pbase(t)->b_const;
202: Pbase(t)->b_const = mode;
203: t = Pbase(t)->b_name->tp;
204: goto xxx;
205: case ANY:
206: case RPTR:
207: case VEC:
208: return t->base; // constant by definition
209: case PTR:
210: m = Pptr(t)->rdo;
211: Pptr(t)->rdo = mode;
212: return m;
213: default:
214: m = Pbase(t)->b_const;
215: Pbase(t)->b_const = mode;
216: return m;
217: }
218: }
219:
220: Pptr type::is_ref()
221: {
222: Ptype t = this;
223: xxx:
224: switch (t->base) {
225: case TYPE: t = Pbase(t)->b_name->tp; goto xxx;
226: case RPTR: return Pptr(t);
227: default: return 0;
228: }
229: }
230:
231: Pclass Mptr;
232:
233: Pptr type::is_ptr()
234: {
235: Ptype t = this;
236: xxx:
237: switch (t->base) {
238: case TYPE: t = Pbase(t)->b_name->tp; goto xxx;
239: case PTR:
240: case VEC: Mptr = Pptr(t)->memof;
241: return Pptr(t);
242: default: return 0;
243: }
244: }
245:
246: Pptr type::is_ptr_or_ref()
247: {
248: Ptype t = this;
249: xxx:
250: switch (t->base) {
251: case TYPE: t = Pbase(t)->b_name->tp; goto xxx;
252: case PTR:
253: case RPTR:
254: case VEC: Mptr = Pptr(t)->memof;
255: return Pptr(t);
256: default: return 0;
257: }
258: }
259:
260: int type::align()
261: {
262: Ptype t = this;
263: xx:
264: /*fprintf(stderr,"align %d %d\n",t,t->base);*/
265: switch (t->base) {
266: case TYPE: t = Pbase(t)->b_name->tp; goto xx;
267: case COBJ: t = Pbase(t)->b_name->tp; goto xx;
268: case VEC: t = Pvec(t)->typ; goto xx;
269: case ANY: return 1;
270: case CHAR: return AL_CHAR;
271: case SHORT: return AL_SHORT;
272: case INT: return AL_INT;
273: case LONG: return AL_LONG;
274: case FLOAT: return AL_FLOAT;
275: case DOUBLE: return AL_DOUBLE;
276: case LDOUBLE: return AL_LDOUBLE;
277: case PTR:
278: case RPTR: return AL_WPTR;
279: case CLASS: return Pclass(t)->obj_align;
280: case ENUM:
281: case EOBJ: return AL_INT;
282: case VOID: error("illegal use of void"); return AL_INT;
283: default: error('i',"(%d,%k)->type::align",t,t->base);
284: }
285: }
286:
287: bit fake_sizeof;
288:
289: int type::tsizeof()
290: /*
291: the sizeof type operator
292: return the size in bytes of the types representation
293: */
294: {
295: Ptype t = this;
296: zx:
297: //error('d',"zx %t %d",t,t->base);
298: if (t == 0) error('i',"typ.tsizeof(t==0)");
299: switch (t->base) {
300: case TYPE:
301: t = Pbase(t)->b_name->tp;
302: goto zx;
303: case COBJ:
304: t = Pbase(t)->b_name->tp;
305: if (t == 0) return 0; // ``fake'' generated classes: _Sdd
306: goto zx;
307:
308: case ANY: return 1;
309: case VOID: return 0;
310: case ZTYPE: return SZ_WPTR; /* assume pointer */
311: case CHAR: return SZ_CHAR;
312: case SHORT: return SZ_SHORT;
313: case INT: return SZ_INT;
314: case LONG: return SZ_LONG;
315: case FLOAT: return SZ_FLOAT;
316: case DOUBLE: return SZ_DOUBLE;
317: case LDOUBLE: return SZ_LDOUBLE;
318:
319: case VEC:
320: { Pvec v = Pvec(t);
321: if (v->size == 0) {
322: if (fake_sizeof == 0) error('w',"sizeof array with undeclared dimension");
323: return SZ_WPTR; // vector argument has sizeof ptr
324: }
325: return v->size * v->typ->tsizeof();
326: }
327: case RPTR:
328: case PTR:
329: { int k = (Pptr(t)->memof)?sizeof(short)+sizeof(short):0;
330: t = Pptr(t)->typ;
331: xxx:
332: switch (t->base) {
333: default: return SZ_WPTR;
334: case CHAR: return SZ_BPTR;
335: case FCT: return SZ_WPTR+k;
336: case TYPE: t = Pbase(t)->b_name->tp; goto xxx;
337: }
338: }
339: case FIELD:
340: return Pbase(t)->b_bits/BI_IN_BYTE+1;
341:
342: case CLASS:
343: {
344: Pclass cl = Pclass(t);
345: if ((cl->defined&(DEFINED|SIMPLIFIED)) == 0) {
346: error("%tU, size not known",cl);
347: return SZ_INT;
348: }
349: if (cl->c_body == 1) // detect first allocation or sizeof
350: cl->dcl_print(0);
351: return cl->obj_size;
352: }
353:
354: case EOBJ:
355: case ENUM: return SZ_INT;
356:
357: default: return 0; // deref can be called for any type
358: //error('i',"sizeof(%d)",t->base);
359: }
360: }
361:
362: bit type::vec_type()
363: {
364: Ptype t = this;
365: xx:
366: switch (t->base) {
367: case ANY:
368: case VEC:
369: case PTR:
370: case RPTR: return 1;
371: case TYPE: t = Pbase(t)->b_name->tp; goto xx;
372: default: return 0;
373: }
374: }
375:
376: int ref_initializer;
377:
378: Ptype type::deref()
379: /* index==1: *p
380: index==0: p[expr]
381: */
382: {
383: //error('d',"%t -> deref() refd %d",this,ref_initializer);
384: Ptype t = this;
385: xx:
386: switch (t->base) {
387: case TYPE:
388: t = Pbase(t)->b_name->tp;
389: goto xx;
390: case PTR:
391: case RPTR:
392: case VEC:
393: { if (t == Pvoid_type) error("void* deRd");
394: Ptype tt = t = Pvec(t)->typ;
395: if (ref_initializer == 0) {
396: while (tt->base == TYPE) tt = Pbase(tt)->b_name->tp;
397: if (tt->base == COBJ) {
398: tt = Pbase(tt)->b_name->tp;
399: if (tt && Pclass(tt)->defined&(DEFINED|SIMPLIFIED))
400: (void) t->tsizeof();
401: }
402: }
403: // no break
404: }
405: case ANY:
406: return t;
407: default:
408: error("nonP deRd");
409: return any_type;
410: }
411: }
412:
413: Pfct type::memptr()
414: // is ``this'' a pointer to member function
415: {
416: Ptype t = this;
417: while (t->base == TYPE) t = Pbase(t)->b_name->tp;
418: if (t->base != PTR || Pptr(t)->memof==0) return 0;
419:
420: t = Pptr(t)->typ;
421: while (t->base == TYPE) t = Pbase(t)->b_name->tp;
422: return (t->base == FCT) ? Pfct(t) : 0;
423: }
424:
425: /* ODI notes
426:
427: turn on DEFINED for predefined types to control tree copying
428: for parameterized types
429: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.