ch7.c 9.2 KB

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