ch3mon.c 4.3 KB

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