decspecs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /* D E C L A R A T I O N S P E C I F I E R C H E C K I N G */
  7. #include "assert.h"
  8. #include "Lpars.h"
  9. #include "decspecs.h"
  10. #include "arith.h"
  11. #include "type.h"
  12. #include "level.h"
  13. #include "def.h"
  14. extern char options[];
  15. extern int level;
  16. extern char *symbol2str();
  17. extern struct type *qualifier_type();
  18. struct decspecs null_decspecs;
  19. do_decspecs(ds)
  20. register struct decspecs *ds;
  21. {
  22. /* The provisional decspecs ds as obtained from the program
  23. is turned into a legal consistent decspecs.
  24. */
  25. register struct type *tp = ds->ds_type;
  26. ASSERT(level != L_FORMAL1);
  27. if ( level == L_GLOBAL &&
  28. (ds->ds_sc == AUTO || ds->ds_sc == REGISTER)
  29. ) {
  30. error("no global %s variable allowed",
  31. symbol2str(ds->ds_sc));
  32. ds->ds_sc = GLOBAL;
  33. }
  34. if (level == L_FORMAL2) {
  35. if (ds->ds_sc_given &&
  36. ds->ds_sc != REGISTER){
  37. error("%s formal illegal", symbol2str(ds->ds_sc));
  38. ds->ds_sc = FORMAL;
  39. }
  40. }
  41. /* Since type qualifiers may be associated with types by means
  42. of typedefs, we have to perform same basic tests down here.
  43. */
  44. if (tp != (struct type *)0) {
  45. if ((ds->ds_typequal & TQ_VOLATILE) && (tp->tp_typequal & TQ_VOLATILE))
  46. error("indirect repeated type qualifier");
  47. if ((ds->ds_typequal & TQ_CONST) && (tp->tp_typequal & TQ_CONST))
  48. error("indirect repeated type qualifier");
  49. ds->ds_typequal |= tp->tp_typequal;
  50. }
  51. /* The tests concerning types require a full knowledge of the
  52. type and will have to be postponed to declare_idf.
  53. */
  54. /* some adjustments as described in 3.5.2. */
  55. if (tp == 0) {
  56. ds->ds_notypegiven = 1;
  57. tp = int_type;
  58. }
  59. if (ds->ds_size) {
  60. register int ds_isshort = (ds->ds_size == SHORT);
  61. if (ds->ds_typedef) goto SIZE_ERROR; /* yes */
  62. if (tp == int_type) {
  63. if (ds_isshort) tp = short_type;
  64. else tp = long_type;
  65. } else if (tp == double_type && !ds_isshort ) {
  66. tp = lngdbl_type;
  67. } else {
  68. SIZE_ERROR:
  69. error("%s with illegal type",symbol2str(ds->ds_size));
  70. }
  71. ds->ds_notypegiven = 0;
  72. }
  73. if (ds->ds_unsigned) {
  74. register int ds_isunsigned = (ds->ds_unsigned == UNSIGNED);
  75. if (ds->ds_typedef) goto SIGN_ERROR; /* yes */
  76. /*
  77. * All integral types are signed by default (char too),
  78. * so the case that ds->ds_unsigned == SIGNED can be ignored.
  79. */
  80. if (tp == schar_type) {
  81. if (ds_isunsigned) tp = uchar_type;
  82. } else if (tp == short_type) {
  83. if (ds_isunsigned) tp = ushort_type;
  84. } else if (tp == int_type) {
  85. if (ds_isunsigned) tp = uint_type;
  86. } else if (tp == long_type) {
  87. if (ds_isunsigned) tp = ulong_type;
  88. } else {
  89. SIGN_ERROR:
  90. error("%s with illegal type"
  91. , symbol2str(ds->ds_unsigned));
  92. }
  93. ds->ds_notypegiven = 0;
  94. }
  95. ds->ds_type = qualifier_type(tp, ds->ds_typequal);
  96. }
  97. /* Make tp into a qualified type. This is not as trivial as it
  98. may seem. If tp is a fundamental type the qualified type is
  99. either existent or will be generated.
  100. In case of a complex type the top of the type list will be
  101. replaced by a qualified version.
  102. */
  103. struct type *
  104. qualifier_type(tp, typequal)
  105. register struct type *tp;
  106. int typequal;
  107. {
  108. register struct type *dtp = tp;
  109. register int fund = tp->tp_fund;
  110. while (dtp && dtp->tp_typequal != typequal)
  111. dtp = dtp->next;
  112. if (!dtp) {
  113. dtp = create_type(fund);
  114. dtp->tp_unsigned = tp->tp_unsigned;
  115. dtp->tp_align = tp->tp_align;
  116. dtp->tp_typequal = typequal;
  117. dtp->tp_size = tp->tp_size;
  118. #if 0
  119. /* The tp_function field does not exist now. See the comment in the
  120. function_of() routine.
  121. */
  122. dtp->tp_function = tp->tp_function;
  123. #endif
  124. switch (fund) {
  125. case ARRAY:
  126. if (typequal) {
  127. tp->tp_up = qualifier_type(tp->tp_up, typequal);
  128. dtp->tp_typequal = typequal = 0;
  129. }
  130. goto nottagged;
  131. case FIELD:
  132. dtp->tp_field = tp->tp_field;
  133. /* fallthrough */
  134. case POINTER:
  135. case FUNCTION: /* dont't assign tp_proto */
  136. nottagged:
  137. dtp->tp_up = tp->tp_up;
  138. break;
  139. case STRUCT:
  140. case UNION:
  141. case ENUM:
  142. dtp->tp_idf = tp->tp_idf;
  143. dtp->tp_sdef = tp->tp_sdef;
  144. break;
  145. default:
  146. break;
  147. }
  148. dtp->next = tp->next; /* don't know head or tail */
  149. tp->next = dtp;
  150. }
  151. return(dtp);
  152. }