ch7.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* $Header$ */
  2. /* S E M A N T I C A N A L Y S I S -- C H A P T E R 7 RM */
  3. #include "nofloat.h"
  4. #include "debug.h"
  5. #include "nobitfield.h"
  6. #include "idf.h"
  7. #include "arith.h"
  8. #include "type.h"
  9. #include "struct.h"
  10. #include "label.h"
  11. #include "expr.h"
  12. #include "def.h"
  13. #include "Lpars.h"
  14. #include "assert.h"
  15. extern char options[];
  16. extern char *symbol2str();
  17. /* Most expression-handling routines have a pointer to a
  18. (struct type *) as first parameter. The object under the pointer
  19. gets updated in the process.
  20. */
  21. ch7sel(expp, oper, idf)
  22. register struct expr **expp;
  23. struct idf *idf;
  24. {
  25. /* The selector idf is applied to *expp; oper may be '.' or
  26. ARROW.
  27. */
  28. register struct type *tp = (*expp)->ex_type;
  29. register struct sdef *sd;
  30. if (oper == ARROW) {
  31. if (tp->tp_fund == POINTER) /* normal case */
  32. tp = tp->tp_up;
  33. else { /* constructions like "12->selector" and
  34. "char c; c->selector"
  35. */
  36. switch (tp->tp_fund) {
  37. case CHAR:
  38. case SHORT:
  39. case INT:
  40. case LONG:
  41. case ENUM:
  42. /* Allowed by RM 14.1 */
  43. ch7cast(expp, CAST, pa_type);
  44. sd = idf2sdef(idf, tp);
  45. tp = sd->sd_stype;
  46. break;
  47. default:
  48. expr_error(*expp, "-> applied to %s",
  49. symbol2str(tp->tp_fund));
  50. case ERRONEOUS:
  51. (*expp)->ex_type = error_type;
  52. return;
  53. }
  54. } /* tp->tp_fund != POINTER */
  55. } /* oper == ARROW */
  56. else { /* oper == '.' */
  57. /* filter out illegal expressions "non_lvalue.sel" */
  58. if (!(*expp)->ex_lvalue) {
  59. expr_error(*expp, "dot requires lvalue");
  60. return;
  61. }
  62. }
  63. switch (tp->tp_fund) {
  64. case POINTER: /* for int *p; p->next = ... */
  65. case STRUCT:
  66. case UNION:
  67. break;
  68. case CHAR:
  69. case SHORT:
  70. case INT:
  71. case LONG:
  72. case ENUM:
  73. /* warning will be given by idf2sdef() */
  74. break;
  75. default:
  76. if (!is_anon_idf(idf))
  77. expr_error(*expp, "selector %s applied to %s",
  78. idf->id_text, symbol2str(tp->tp_fund));
  79. case ERRONEOUS:
  80. (*expp)->ex_type = error_type;
  81. return;
  82. }
  83. sd = idf2sdef(idf, tp);
  84. if (oper == '.') {
  85. /* there are 3 cases in which the selection can be
  86. performed compile-time:
  87. I: n.sel (n either an identifier or a constant)
  88. II: (e.s1).s2 (transformed into (e.(s1+s2)))
  89. III: (e->s1).s2 (transformed into (e->(s1+s2)))
  90. The code performing these conversions is
  91. extremely obscure.
  92. */
  93. if ((*expp)->ex_class == Value) {
  94. /* It is an object we know the address of; so
  95. we can calculate the address of the
  96. selected member
  97. */
  98. (*expp)->VL_VALUE += sd->sd_offset;
  99. (*expp)->ex_type = sd->sd_type;
  100. if ((*expp)->ex_type == error_type)
  101. (*expp)->ex_flags |= EX_ERROR;
  102. }
  103. else
  104. if ((*expp)->ex_class == Oper) {
  105. struct oper *op = &((*expp)->ex_object.ex_oper);
  106. if (op->op_oper == '.' || op->op_oper == ARROW) {
  107. ASSERT(is_cp_cst(op->op_right));
  108. op->op_right->VL_VALUE += sd->sd_offset;
  109. (*expp)->ex_type = sd->sd_type;
  110. if ((*expp)->ex_type == error_type)
  111. (*expp)->ex_flags |= EX_ERROR;
  112. }
  113. else
  114. *expp = new_oper(sd->sd_type, *expp, '.',
  115. intexpr(sd->sd_offset, INT));
  116. }
  117. }
  118. else /* oper == ARROW */
  119. *expp = new_oper(sd->sd_type,
  120. *expp, oper, intexpr(sd->sd_offset, INT));
  121. (*expp)->ex_lvalue = (sd->sd_type->tp_fund != ARRAY);
  122. }
  123. ch7incr(expp, oper)
  124. struct expr **expp;
  125. {
  126. /* The monadic prefix/postfix incr/decr operator oper is
  127. applied to *expp.
  128. */
  129. ch7asgn(expp, oper, intexpr((arith)1, INT));
  130. }
  131. ch7cast(expp, oper, tp)
  132. register struct expr **expp;
  133. register struct type *tp;
  134. {
  135. /* The expression *expp is cast to type tp; the cast is
  136. caused by the operator oper. If the cast has
  137. to be passed on to run time, its left operand will be an
  138. expression of class Type.
  139. */
  140. register struct type *oldtp;
  141. if ((*expp)->ex_type->tp_fund == FUNCTION)
  142. function2pointer(expp);
  143. if ((*expp)->ex_type->tp_fund == ARRAY)
  144. array2pointer(expp);
  145. if ((*expp)->ex_class == String)
  146. string2pointer(expp);
  147. oldtp = (*expp)->ex_type;
  148. #ifndef NOBITFIELD
  149. if (oldtp->tp_fund == FIELD) {
  150. field2arith(expp);
  151. ch7cast(expp, oper, tp);
  152. }
  153. else
  154. if (tp->tp_fund == FIELD)
  155. ch7cast(expp, oper, tp->tp_up);
  156. else
  157. #endif NOBITFIELD
  158. if (oldtp == tp)
  159. {} /* life is easy */
  160. else
  161. if (tp->tp_fund == VOID) /* Easy again */
  162. (*expp)->ex_type = void_type;
  163. else
  164. if (is_arith_type(oldtp) && is_arith_type(tp)) {
  165. int oldi = is_integral_type(oldtp);
  166. int i = is_integral_type(tp);
  167. if (oldi && i) {
  168. if ( oldtp->tp_fund == ENUM &&
  169. tp->tp_fund == ENUM &&
  170. oper != CAST
  171. )
  172. expr_warning(*expp,
  173. "%s on enums of different types",
  174. symbol2str(oper));
  175. int2int(expp, tp);
  176. }
  177. #ifndef NOFLOAT
  178. else
  179. if (oldi && !i) {
  180. if (oldtp->tp_fund == ENUM && oper != CAST)
  181. expr_warning(*expp,
  182. "conversion of enum to %s\n",
  183. symbol2str(tp->tp_fund));
  184. int2float(expp, tp);
  185. }
  186. else
  187. if (!oldi && i)
  188. float2int(expp, tp);
  189. else /* !oldi && !i */
  190. float2float(expp, tp);
  191. #else NOFLOAT
  192. else
  193. crash("(ch7cast) floats not implemented\n");
  194. #endif NOFLOAT
  195. }
  196. else
  197. if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
  198. if (oper != CAST)
  199. expr_warning(*expp, "incompatible pointers in %s",
  200. symbol2str(oper));
  201. (*expp)->ex_type = tp; /* free conversion */
  202. }
  203. else
  204. if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
  205. /* from pointer to integral */
  206. if (oper != CAST)
  207. expr_warning(*expp,
  208. "illegal conversion of pointer to %s",
  209. symbol2str(tp->tp_fund));
  210. if (oldtp->tp_size > tp->tp_size)
  211. expr_warning(*expp,
  212. "conversion of pointer to %s loses accuracy",
  213. symbol2str(tp->tp_fund));
  214. if (oldtp->tp_size != tp->tp_size)
  215. int2int(expp, tp);
  216. else
  217. (*expp)->ex_type = tp;
  218. }
  219. else
  220. if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
  221. /* from integral to pointer */
  222. switch (oper) {
  223. case CAST:
  224. break;
  225. case EQUAL:
  226. case NOTEQUAL:
  227. case '=':
  228. case RETURN:
  229. if (is_cp_cst(*expp) && (*expp)->VL_VALUE == (arith)0)
  230. break;
  231. default:
  232. expr_warning(*expp,
  233. "illegal conversion of %s to pointer",
  234. symbol2str(oldtp->tp_fund));
  235. break;
  236. }
  237. if (oldtp->tp_size > tp->tp_size)
  238. expr_warning(*expp,
  239. "conversion of %s to pointer loses accuracy",
  240. symbol2str(oldtp->tp_fund));
  241. if (oldtp->tp_size != tp->tp_size)
  242. int2int(expp, tp);
  243. else
  244. (*expp)->ex_type = tp;
  245. }
  246. else
  247. if (oldtp->tp_fund == ERRONEOUS) /* we just won't look */
  248. (*expp)->ex_type = tp; /* brute force */
  249. else
  250. if (oldtp->tp_size == tp->tp_size && oper == CAST) {
  251. expr_warning(*expp, "dubious conversion based on equal size");
  252. (*expp)->ex_type = tp; /* brute force */
  253. }
  254. else {
  255. if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
  256. expr_error(*expp, "cannot convert %s to %s",
  257. symbol2str(oldtp->tp_fund),
  258. symbol2str(tp->tp_fund)
  259. );
  260. (*expp)->ex_type = tp; /* brute force */
  261. }
  262. }
  263. ch7asgn(expp, oper, expr)
  264. register struct expr **expp;
  265. struct expr *expr;
  266. {
  267. /* The assignment operators.
  268. "f op= e" should be interpreted as
  269. "f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
  270. and not as "f = f op (typeof f)e".
  271. Consider, for example, (i == 10) i *= 0.9; (i == 9), where
  272. typeof i == int.
  273. The resulting expression tree becomes:
  274. op=
  275. / \
  276. / \
  277. f (typeof (f op e))e
  278. EVAL should however take care of evaluating (typeof (f op e))f
  279. */
  280. int fund = (*expp)->ex_type->tp_fund;
  281. struct type *tp;
  282. /* We expect an lvalue */
  283. if (!(*expp)->ex_lvalue) {
  284. expr_error(*expp, "no lvalue in lhs of %s", symbol2str(oper));
  285. (*expp)->ex_depth = 99; /* no direct store/load at EVAL() */
  286. /* what is 99 ??? DG */
  287. }
  288. if (oper == '=') {
  289. ch7cast(&expr, oper, (*expp)->ex_type);
  290. tp = expr->ex_type;
  291. }
  292. else { /* turn e into e' where typeof(e') = typeof (f op e) */
  293. struct expr *extmp = intexpr((arith)0, INT);
  294. /* this is really $#@&*%$# ! */
  295. extmp->ex_lvalue = 1;
  296. extmp->ex_type = (*expp)->ex_type;
  297. ch7bin(&extmp, oper, expr);
  298. /* Note that ch7bin creates a tree of the expression
  299. ((typeof (f op e))f op (typeof (f op e))e),
  300. where f ~ extmp and e ~ expr.
  301. We want to use (typeof (f op e))e.
  302. Ch7bin does not create a tree if both operands
  303. were illegal or constants!
  304. */
  305. tp = extmp->ex_type; /* perform the arithmetic in type tp */
  306. if (extmp->ex_class == Oper) {
  307. expr = extmp->OP_RIGHT;
  308. extmp->OP_RIGHT = NILEXPR;
  309. free_expression(extmp);
  310. }
  311. else
  312. expr = extmp;
  313. }
  314. #ifndef NOBITFIELD
  315. if (fund == FIELD)
  316. *expp = new_oper((*expp)->ex_type->tp_up, *expp, oper, expr);
  317. else
  318. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  319. #else NOBITFIELD
  320. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  321. #endif NOBITFIELD
  322. (*expp)->OP_TYPE = tp; /* for EVAL() */
  323. (*expp)->ex_flags |= EX_SIDEEFFECTS;
  324. }
  325. /* Some interesting (?) questions answered.
  326. */
  327. int
  328. is_integral_type(tp)
  329. register struct type *tp;
  330. {
  331. switch (tp->tp_fund) {
  332. case CHAR:
  333. case SHORT:
  334. case INT:
  335. case LONG:
  336. case ENUM:
  337. return 1;
  338. #ifndef NOBITFIELD
  339. case FIELD:
  340. return is_integral_type(tp->tp_up);
  341. #endif NOBITFIELD
  342. default:
  343. return 0;
  344. }
  345. }
  346. int
  347. is_arith_type(tp)
  348. register struct type *tp;
  349. {
  350. switch (tp->tp_fund) {
  351. case CHAR:
  352. case SHORT:
  353. case INT:
  354. case LONG:
  355. case ENUM:
  356. #ifndef NOFLOAT
  357. case FLOAT:
  358. case DOUBLE:
  359. #endif NOFLOAT
  360. return 1;
  361. #ifndef NOBITFIELD
  362. case FIELD:
  363. return is_arith_type(tp->tp_up);
  364. #endif NOBITFIELD
  365. default:
  366. return 0;
  367. }
  368. }