1 /* type.c - type and storage-class routines for bcc */
3 /* Copyright (C) 1992 Bruce Evans */
7 #include "gencode.h" /* s.b. switches.h */
16 PUBLIC uoffset_T ctypesize
= 1;
17 PUBLIC uoffset_T dtypesize
= 8;
18 PUBLIC uoffset_T ftypesize
= 0;
19 PUBLIC uoffset_T itypesize
= 2;
20 PUBLIC uoffset_T ptypesize
= 2;
21 PUBLIC uoffset_T stypesize
= 2;
26 PUBLIC
struct typestruct
*addstruct(structname
)
30 struct symstruct
*symptr
;
31 struct typestruct
*structype
;
33 (structype
= newtype())->constructor
= STRUCTU
;
34 structype
->alignmask
= ctype
->alignmask
;
37 ++skey1
; /* must be nonzero or key string would end */
38 ++skey0
; /* will never reach 'A' to be an identifier */
40 structype
->structkey
[0] = skey0
;
41 structype
->structkey
[1] = skey1
;
44 symptr
= addlorg(structname
, structype
);
46 symptr
->flags
= STRUCTNAME
;
47 if (charptr
+ (namelength
= strlen(structname
)) >= chartop
)
48 growheap(namelength
+ 1);
51 ts_s_structname
+= namelength
+ 1;
53 structype
->tname
= strcpy(charptr
, structname
);
54 charptr
+= namelength
+ 1;
59 PUBLIC
struct typestruct
*iscalartotype(scalar
)
64 if (scalar
& UNSIGNED
)
68 if (scalar
& UNSIGNED
)
73 PUBLIC
struct typestruct
*newtype()
75 register struct typestruct
*type
;
77 type
= qmalloc(sizeof *type
);
80 ts_s_type
+= sizeof *type
;
82 type
->typesize
= /* (uoffset_T) */
83 type
->scalar
= /* (scalar_t) */
84 type
->constructor
= /* (constr_t) */
85 type
->structkey
[0] = 0;
88 type
->sidetype
= NULL
;
89 type
->listtype
= NULL
;
94 PUBLIC
void outntypechar(type
)
95 struct typestruct
*type
;
97 outnbyte(*type
->tname
);
100 PUBLIC
struct typestruct
*pointype(type
)
101 struct typestruct
*type
;
103 return prefix(POINTER
, ptypesize
, type
);
106 PUBLIC
struct typestruct
*prefix(constructor
, size
, type
)
107 constr_pt constructor
;
109 struct typestruct
*type
;
111 register struct typestruct
*searchtype
;
113 for (searchtype
= type
->prevtype
; searchtype
!= NULL
;
114 searchtype
= searchtype
->sidetype
)
115 if (searchtype
->constructor
== (constr_t
) constructor
&&
116 searchtype
->typesize
== size
)
118 switch ((searchtype
= newtype())->constructor
= constructor
)
121 searchtype
->alignmask
= type
->alignmask
;
124 searchtype
->alignmask
= ~(uoffset_T
) 0;
127 searchtype
->alignmask
= ~(ptypesize
- 1) | alignmask
;
130 bugerror("prefixing structure/union");
131 searchtype
->alignmask
= alignmask
;
134 searchtype
->typesize
= size
;
135 searchtype
->nexttype
= type
;
136 searchtype
->sidetype
= type
->prevtype
;
137 return type
->prevtype
= searchtype
;
140 PUBLIC
struct typestruct
*promote(type
)
141 struct typestruct
*type
;
145 if ((scalar
= type
->scalar
) & (CHAR
| SHORT
))
147 if (scalar
& UNSIGNED
)
153 if (type
->constructor
& ARRAY
)
154 return pointype(type
->nexttype
);
155 if (type
->constructor
& FUNCTION
)
156 return pointype(type
);
160 PUBLIC
struct typestruct
*tosigned(type
)
161 struct typestruct
*type
;
163 switch (type
->scalar
& ~(UNSIGNED
| DLONG
))
174 error("signed only applies to integral types");
179 PUBLIC
struct typestruct
*tounsigned(type
)
180 struct typestruct
*type
;
182 switch (type
->scalar
& ~(UNSIGNED
| DLONG
))
193 error("unsigned only applies to integral types");
198 PUBLIC
void typeinit()
212 itype
->alignmask
= ~(uoffset_T
) (4 - 1);
213 ltype
->scalar
= LONG
; /* not DLONG */
214 ultype
->scalar
= UNSIGNED
| LONG
;
217 fitype
= prefix(FUNCTION
, ftypesize
, itype
);
218 pctype
= pointype(ctype
);
220 vtype
->constructor
= VOID
;