type.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /* $Header$ */
  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, *label_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. } else if (count >= 0 && tp->tp_size < 0) {
  97. error("cannot construct array of unknown type");
  98. count = (arith)-1;
  99. }
  100. if (count > (arith)0)
  101. count *= tp->tp_size;
  102. dtp = array_of(tp, count, qual);
  103. break;
  104. default:
  105. crash("bad constructor in construct_type");
  106. /*NOTREACHED*/
  107. }
  108. return dtp;
  109. }
  110. struct type *
  111. function_of(tp, pl, qual)
  112. register struct type *tp;
  113. struct proto *pl;
  114. int qual;
  115. {
  116. #if 0
  117. /* See comment below */
  118. register struct type *dtp = tp->tp_function;
  119. #else
  120. register struct type *dtp;
  121. #endif
  122. /* look for a type with the right qualifier */
  123. #if 0
  124. /* the code doesn't work in the following case:
  125. int func();
  126. int func(int a, int b) { return q(a); }
  127. because updating the type works inside the data-structures for that type
  128. thus, a new type is created for very function. This may change in the
  129. future, when declarations with empty parameter lists become obsolete.
  130. When it does, change type.str, decspecs.c, and this routine. Search for
  131. the function_of pattern to find the places.
  132. */
  133. while (dtp && (dtp->tp_typequal != qual || dtp->tp_proto != pl))
  134. dtp = dtp->next;
  135. #else
  136. dtp = 0;
  137. #endif
  138. if (!dtp) {
  139. dtp = create_type(FUNCTION);
  140. dtp->tp_up = tp;
  141. /* dtp->tp_size = pointer_size; ??? */
  142. dtp->tp_size = -1; /* function size is not known */
  143. dtp->tp_align = pointer_align;
  144. dtp->tp_typequal = qual;
  145. dtp->tp_proto = pl;
  146. #if 0
  147. /* See comment above */
  148. dtp->next = tp->tp_function;
  149. tp->tp_function = dtp;
  150. #endif
  151. }
  152. return dtp;
  153. }
  154. struct type *
  155. pointer_to(tp, qual)
  156. register struct type *tp;
  157. int qual;
  158. {
  159. register struct type *dtp = tp->tp_pointer;
  160. /* look for a type with the right qualifier */
  161. while (dtp && dtp->tp_typequal != qual)
  162. dtp = dtp->next;
  163. if (!dtp) {
  164. dtp = create_type(POINTER);
  165. dtp->tp_unsigned = 1;
  166. dtp->tp_up = tp;
  167. dtp->tp_size = pointer_size;
  168. dtp->tp_align = pointer_align;
  169. dtp->tp_typequal = qual;
  170. dtp->next = tp->tp_pointer;
  171. tp->tp_pointer = dtp;
  172. }
  173. return dtp;
  174. }
  175. struct type *
  176. array_of(tp, count, qual)
  177. register struct type *tp;
  178. arith count;
  179. int qual;
  180. {
  181. register struct type *dtp = tp->tp_array;
  182. /* look for a type with the right size */
  183. while (dtp && (dtp->tp_size != count || dtp->tp_typequal != qual))
  184. dtp = dtp->next;
  185. if (!dtp) {
  186. dtp = create_type(ARRAY);
  187. dtp->tp_up = tp;
  188. dtp->tp_size = count;
  189. dtp->tp_align = tp->tp_align;
  190. dtp->tp_typequal = qual;
  191. dtp->next = tp->tp_array;
  192. tp->tp_array = dtp;
  193. }
  194. return dtp;
  195. }
  196. #ifndef NOBITFIELD
  197. struct type *
  198. field_of(tp, qual)
  199. register struct type *tp;
  200. int qual;
  201. {
  202. register struct type *dtp = create_type(FIELD);
  203. dtp->tp_up = tp;
  204. dtp->tp_align = tp->tp_align;
  205. dtp->tp_size = tp->tp_size;
  206. dtp->tp_typequal = qual;
  207. return dtp;
  208. }
  209. #endif NOBITFIELD
  210. arith
  211. size_of_type(tp, nm)
  212. struct type *tp;
  213. char nm[];
  214. {
  215. arith sz = tp->tp_size;
  216. if (sz < 0) {
  217. error("size of %s unknown", nm);
  218. sz = (arith)1;
  219. }
  220. return sz;
  221. }
  222. idf2type(idf, tpp)
  223. struct idf *idf;
  224. struct type **tpp;
  225. {
  226. /* Decoding a typedef-ed identifier: if the size is yet
  227. unknown we have to make copy of the type descriptor to
  228. prevent garbage at the initialisation of arrays with
  229. unknown size.
  230. */
  231. register struct type *tp = idf->id_def->df_type;
  232. if ( tp->tp_size < (arith)0 && tp->tp_fund == ARRAY) {
  233. *tpp = new_type();
  234. **tpp = *tp;
  235. /* this is really a structure assignment, AAGH!!! */
  236. }
  237. else {
  238. *tpp = tp;
  239. }
  240. }
  241. arith
  242. align(pos, al)
  243. arith pos;
  244. int al;
  245. {
  246. return ((pos + al - 1) / al) * al;
  247. }
  248. struct type *
  249. standard_type(fund, sgn, algn, sz)
  250. int algn; arith sz;
  251. {
  252. register struct type *tp = create_type(fund);
  253. tp->tp_unsigned = sgn;
  254. tp->tp_align = algn;
  255. tp->tp_size = sz;
  256. return tp;
  257. }