ch7mon.c 3.8 KB

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