ch7.c 9.9 KB

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