type.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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$ */
  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. #include "nobitfield.h"
  8. #include "debug.h"
  9. #include "botch_free.h"
  10. #include <alloc.h>
  11. #include "Lpars.h"
  12. #include "arith.h"
  13. #include "type.h"
  14. #include "idf.h"
  15. #include "def.h"
  16. #include "proto.h"
  17. #include "sizes.h"
  18. #include "align.h"
  19. #include "decspecs.h"
  20. extern struct type *function_of(), *array_of();
  21. #ifndef NOBITFIELD
  22. extern struct type *field_of();
  23. #endif /* NOBITFIELD */
  24. /* To be created dynamically in main() from defaults or from command
  25. line parameters.
  26. */
  27. struct type
  28. *schar_type, *uchar_type,
  29. *short_type, *ushort_type,
  30. *word_type, *uword_type,
  31. *int_type, *uint_type,
  32. *long_type, *ulong_type,
  33. *float_type, *double_type, *lngdbl_type,
  34. *void_type,
  35. *string_type, *funint_type, *error_type;
  36. struct type *pa_type; /* Pointer-Arithmetic type */
  37. struct type *
  38. create_type(fund)
  39. int fund;
  40. {
  41. /* A brand new struct type is created, and its tp_fund set
  42. to fund.
  43. */
  44. register struct type *ntp = new_type();
  45. ntp->tp_fund = fund;
  46. ntp->tp_size = (arith)-1;
  47. return ntp;
  48. }
  49. struct type *
  50. promoted_type(tp)
  51. struct type *tp;
  52. {
  53. if (tp->tp_fund == CHAR || tp->tp_fund == SHORT) {
  54. if (tp->tp_unsigned && (int) tp->tp_size == (int) int_size)
  55. return uint_type;
  56. else return int_type;
  57. } else if (tp->tp_fund == FLOAT)
  58. return double_type;
  59. else return tp;
  60. }
  61. struct type *
  62. construct_type(fund, tp, qual, count, pl)
  63. register struct type *tp;
  64. register struct proto *pl;
  65. arith count; /* for fund == ARRAY only */
  66. int qual;
  67. {
  68. /* fund must be a type constructor: FIELD, FUNCTION, POINTER or
  69. ARRAY. The pointer to the constructed type is returned.
  70. */
  71. register struct type *dtp;
  72. switch (fund) {
  73. #ifndef NOBITFIELD
  74. case FIELD:
  75. dtp = field_of(tp, qual);
  76. break;
  77. #endif /* NOBITFIELD */
  78. case FUNCTION:
  79. if (tp->tp_fund == FUNCTION) {
  80. error("function cannot yield function");
  81. return error_type;
  82. }
  83. if (tp->tp_fund == ARRAY) {
  84. error("function cannot yield array");
  85. return error_type;
  86. }
  87. dtp = function_of(tp, pl, qual);
  88. break;
  89. case POINTER:
  90. dtp = pointer_to(tp, qual);
  91. break;
  92. case ARRAY:
  93. if (tp->tp_fund == VOID) {
  94. error("cannot construct array of void");
  95. count = (arith) -1;
  96. }
  97. dtp = array_of(tp, count, qual);
  98. break;
  99. default:
  100. crash("bad constructor in construct_type");
  101. /*NOTREACHED*/
  102. }
  103. return dtp;
  104. }
  105. struct type *
  106. function_of(tp, pl, qual)
  107. register struct type *tp;
  108. struct proto *pl;
  109. int qual;
  110. {
  111. #if 0
  112. /* See comment below */
  113. register struct type *dtp = tp->tp_function;
  114. #else
  115. register struct type *dtp;
  116. #endif
  117. /* look for a type with the right qualifier */
  118. #if 0
  119. /* the code doesn't work in the following case:
  120. int func();
  121. int func(int a, int b) { return q(a); }
  122. because updating the type works inside the data-structures for that type
  123. thus, a new type is created for very function. This may change in the
  124. future, when declarations with empty parameter lists become obsolete.
  125. When it does, change type.str, decspecs.c, and this routine. Search for
  126. the function_of pattern to find the places.
  127. */
  128. while (dtp && (dtp->tp_typequal != qual || dtp->tp_proto != pl))
  129. dtp = dtp->next;
  130. #else
  131. dtp = 0;
  132. #endif
  133. if (!dtp) {
  134. dtp = create_type(FUNCTION);
  135. dtp->tp_up = tp;
  136. dtp->tp_size = -1; /* function size is not known */
  137. dtp->tp_align = pointer_align;
  138. dtp->tp_typequal = qual;
  139. dtp->tp_proto = pl;
  140. #if 0
  141. /* See comment above */
  142. dtp->next = tp->tp_function;
  143. tp->tp_function = dtp;
  144. #endif
  145. }
  146. return dtp;
  147. }
  148. struct type *
  149. pointer_to(tp, qual)
  150. register struct type *tp;
  151. int qual;
  152. {
  153. register struct type *dtp = tp->tp_pointer;
  154. /* look for a type with the right qualifier */
  155. while (dtp && dtp->tp_typequal != qual)
  156. dtp = dtp->next;
  157. if (!dtp) {
  158. dtp = create_type(POINTER);
  159. dtp->tp_unsigned = 1;
  160. dtp->tp_up = tp;
  161. dtp->tp_size = pointer_size;
  162. dtp->tp_align = pointer_align;
  163. dtp->tp_typequal = qual;
  164. dtp->next = tp->tp_pointer;
  165. tp->tp_pointer = dtp;
  166. }
  167. return dtp;
  168. }
  169. struct type *
  170. array_of(tp, count, qual)
  171. register struct type *tp;
  172. arith count;
  173. int qual;
  174. {
  175. register struct type *dtp = tp->tp_array;
  176. /* look for a type with the right size */
  177. while (dtp && (dtp->tp_nel != count || dtp->tp_typequal != qual))
  178. dtp = dtp->next;
  179. if (!dtp) {
  180. dtp = create_type(ARRAY);
  181. dtp->tp_up = tp;
  182. dtp->tp_nel = count;
  183. dtp->tp_align = tp->tp_align;
  184. dtp->tp_typequal = qual;
  185. dtp->next = tp->tp_array;
  186. tp->tp_array = dtp;
  187. if (tp->tp_size >= 0 && count >= 0) {
  188. dtp->tp_size = count * tp->tp_size;
  189. }
  190. else dtp->tp_size = -1;
  191. }
  192. return dtp;
  193. }
  194. #ifndef NOBITFIELD
  195. struct type *
  196. field_of(tp, qual)
  197. register struct type *tp;
  198. int qual;
  199. {
  200. register struct type *dtp = create_type(FIELD);
  201. dtp->tp_up = tp;
  202. dtp->tp_align = tp->tp_align;
  203. dtp->tp_size = tp->tp_size;
  204. dtp->tp_typequal = qual;
  205. return dtp;
  206. }
  207. #endif /* NOBITFIELD */
  208. arith
  209. size_of_type(tp, nm)
  210. struct type *tp;
  211. char nm[];
  212. {
  213. arith sz = tp->tp_size;
  214. if (sz < 0) {
  215. error("size of %s unknown", nm);
  216. sz = (arith)1;
  217. }
  218. return sz;
  219. }
  220. idf2type(idf, tpp)
  221. struct idf *idf;
  222. struct type **tpp;
  223. {
  224. /* Decoding a typedef-ed identifier or basic type: if the
  225. size is yet unknown we have to make copy of the type
  226. descriptor to prevent garbage at the initialisation of
  227. arrays with unknown size.
  228. */
  229. register struct type *tp = idf->id_def->df_type;
  230. if (*tpp) error("multiple types in declaration");
  231. if ( tp->tp_size < (arith)0 && tp->tp_fund == ARRAY) {
  232. *tpp = new_type();
  233. **tpp = *tp;
  234. /* this is really a structure assignment, AAGH!!! */
  235. }
  236. else {
  237. *tpp = tp;
  238. }
  239. }
  240. arith
  241. align(pos, al)
  242. arith pos;
  243. int al;
  244. {
  245. return ((pos + al - 1) / al) * al;
  246. }
  247. struct type *
  248. standard_type(fund, sgn, algn, sz)
  249. int algn; arith sz;
  250. {
  251. register struct type *tp = create_type(fund);
  252. tp->tp_unsigned = sgn != 0;
  253. tp->tp_align = algn;
  254. tp->tp_size = sz;
  255. return tp;
  256. }
  257. completed(tp)
  258. struct type *tp;
  259. {
  260. register struct type *atp = tp->tp_array;
  261. register struct type *etp = tp;
  262. switch(etp->tp_fund) {
  263. case STRUCT:
  264. case UNION:
  265. case ENUM:
  266. while (etp = etp->next) {
  267. if (! etp->tp_sdef) etp->tp_sdef = tp->tp_sdef;
  268. etp->tp_size = tp->tp_size;
  269. etp->tp_align = tp->tp_align;
  270. }
  271. break;
  272. }
  273. while (atp) {
  274. if (atp->tp_nel >= 0) {
  275. atp->tp_size = atp->tp_nel * tp->tp_size;
  276. }
  277. atp = atp->next;
  278. }
  279. }