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