ch3mon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /* SEMANTIC ANALYSIS (CHAPTER 3.3) -- MONADIC OPERATORS */
  7. #include "botch_free.h"
  8. #include "debug.h"
  9. #include <alloc.h>
  10. #include "nobitfield.h"
  11. #include "Lpars.h"
  12. #include <flt_arith.h>
  13. #include "arith.h"
  14. #include "type.h"
  15. #include "label.h"
  16. #include "expr.h"
  17. #include "idf.h"
  18. #include "def.h"
  19. #include "sizes.h"
  20. extern char options[];
  21. extern arith full_mask[/*MAXSIZE + 1*/]; /* cstoper.c */
  22. char *symbol2str();
  23. ch3mon(oper, expp)
  24. register struct expr **expp;
  25. {
  26. /* The monadic prefix operator oper is applied to *expp.
  27. */
  28. register struct expr *expr;
  29. if (oper != PLUSPLUS && oper != MINMIN)
  30. any2opnd(expp, oper);
  31. switch (oper) {
  32. case '*': /* 3.3.3.2 */
  33. /* no FIELD type allowed */
  34. expr = *expp;
  35. if (expr->ex_type->tp_fund != POINTER) {
  36. if (expr->ex_type != error_type) {
  37. expr_error(expr,
  38. "* applied to non-pointer (%s)",
  39. symbol2str(expr->ex_type->tp_fund));
  40. }
  41. } else {
  42. if (is_ld_cst(expr))
  43. /* dereference in administration only */
  44. expr->ex_type = expr->ex_type->tp_up;
  45. else /* runtime code */
  46. *expp = new_oper(expr->ex_type->tp_up, NILEXPR,
  47. '*', expr);
  48. expr = *expp;
  49. expr->ex_lvalue = (
  50. expr->ex_type->tp_fund != ARRAY &&
  51. expr->ex_type->tp_fund != FUNCTION
  52. );
  53. if (expr->ex_lvalue && expr->ex_type->tp_size <= 0) {
  54. expr_error(expr, "incomplete type in expression");
  55. }
  56. if (expr->ex_type->tp_typequal & TQ_CONST)
  57. expr->ex_flags |= EX_READONLY;
  58. if (expr->ex_type->tp_typequal & TQ_VOLATILE)
  59. expr->ex_flags |= EX_VOLATILE;
  60. expr->ex_flags &= ~EX_ILVALUE;
  61. }
  62. break;
  63. case ADDRESSOF:
  64. if ((*expp)->ex_type->tp_fund == ARRAY) {
  65. (*expp)->ex_type = pointer_to((*expp)->ex_type, 0);
  66. }
  67. else
  68. if ((*expp)->ex_type->tp_fund == FUNCTION) {
  69. (*expp)->ex_type = pointer_to((*expp)->ex_type, 0);
  70. }
  71. else
  72. #ifndef NOBITFIELD
  73. if ((*expp)->ex_type->tp_fund == FIELD)
  74. expr_error(*expp, "& applied to field variable");
  75. else
  76. #endif /* NOBITFIELD */
  77. if (!(*expp)->ex_lvalue)
  78. expr_error(*expp, "& applied to non-lvalue");
  79. else if ((*expp)->ex_flags & EX_ILVALUE)
  80. expr_error(*expp, "& applied to illegal lvalue");
  81. else {
  82. /* assume that enums are already filtered out */
  83. if (ISNAME(*expp)) {
  84. register struct def *def =
  85. (*expp)->VL_IDF->id_def;
  86. /* &<var> indicates that <var>
  87. cannot be used as register
  88. anymore
  89. */
  90. if (def->df_sc == REGISTER) {
  91. expr_error(*expp,
  92. "& on register variable not allowed");
  93. break; /* break case ADDRESSOF */
  94. }
  95. }
  96. (*expp)->ex_type = pointer_to((*expp)->ex_type,
  97. (*expp)->ex_type->tp_typequal);
  98. (*expp)->ex_lvalue = 0;
  99. (*expp)->ex_flags &= ~(EX_READONLY | EX_VOLATILE);
  100. }
  101. break;
  102. case '~':
  103. {
  104. int fund = (*expp)->ex_type->tp_fund;
  105. if (fund == FLOAT || fund == DOUBLE || fund == LNGDBL) {
  106. expr_error( *expp,
  107. "~ not allowed on %s operands",
  108. symbol2str(fund));
  109. erroneous2int(expp);
  110. break;
  111. }
  112. /* FALLTHROUGH */
  113. }
  114. case '-':
  115. any2arith(expp, oper);
  116. if (is_cp_cst(*expp)) {
  117. arith o1 = (*expp)->VL_VALUE;
  118. (*expp)->VL_VALUE = (oper == '-') ? -o1 :
  119. ((*expp)->ex_type->tp_unsigned ?
  120. (~o1) & full_mask[(int)(*expp)->ex_type->tp_size] :
  121. ~o1
  122. );
  123. }
  124. else
  125. if (is_fp_cst(*expp))
  126. switch_sign_fp(*expp);
  127. else
  128. *expp = new_oper((*expp)->ex_type,
  129. NILEXPR, oper, *expp);
  130. break;
  131. case '!':
  132. opnd2test(expp, '!');
  133. if (is_cp_cst(*expp)) {
  134. (*expp)->VL_VALUE = !((*expp)->VL_VALUE);
  135. (*expp)->ex_type = int_type; /* a cast ???(EB) */
  136. }
  137. else
  138. *expp = new_oper(int_type, NILEXPR, oper, *expp);
  139. (*expp)->ex_flags |= EX_LOGICAL;
  140. break;
  141. case PLUSPLUS:
  142. case MINMIN:
  143. ch3incr(expp, oper);
  144. break;
  145. case SIZEOF:
  146. if (ISNAME(*expp) && (*expp)->VL_IDF->id_def->df_formal_array)
  147. expr_warning(*expp, "sizeof formal array %s is sizeof pointer!",
  148. (*expp)->VL_IDF->id_text);
  149. expr = intexpr((*expp)->ex_class == String ?
  150. (arith)((*expp)->SG_LEN) :
  151. size_of_type((*expp)->ex_type,
  152. symbol2str((*expp)->ex_type->tp_fund))
  153. , UNSIGNED);
  154. expr->ex_flags |= EX_SIZEOF;
  155. free_expression(*expp);
  156. *expp = expr;
  157. break;
  158. }
  159. }