type.c 6.0 KB

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