decspecs.c 4.1 KB

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