ch7.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. else
  215. if (oldi && !i) {
  216. if (oldtp->tp_fund == ENUM && oper != CAST)
  217. expr_warning(*expp,
  218. "conversion of enum to %s\n",
  219. symbol2str(tp->tp_fund));
  220. int2float(expp, tp);
  221. }
  222. else
  223. if (!oldi && i)
  224. float2int(expp, tp);
  225. else /* !oldi && !i */
  226. float2float(expp, tp);
  227. }
  228. else
  229. if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
  230. if (oper != CAST)
  231. expr_warning(*expp, "incompatible pointers in %s",
  232. symbol2str(oper));
  233. (*expp)->ex_type = tp; /* free conversion */
  234. }
  235. else
  236. if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
  237. /* from pointer to integral */
  238. if (oper != CAST)
  239. expr_warning(*expp,
  240. "illegal conversion of pointer to %s",
  241. symbol2str(tp->tp_fund));
  242. if (oldtp->tp_size > tp->tp_size)
  243. expr_warning(*expp,
  244. "conversion of pointer to %s loses accuracy",
  245. symbol2str(tp->tp_fund));
  246. if (oldtp->tp_size != tp->tp_size)
  247. int2int(expp, tp);
  248. else
  249. (*expp)->ex_type = tp;
  250. }
  251. else
  252. if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
  253. /* from integral to pointer */
  254. switch (oper) {
  255. case CAST:
  256. break;
  257. case EQUAL:
  258. case NOTEQUAL:
  259. case '=':
  260. case RETURN:
  261. if (is_cp_cst(*expp) && (*expp)->VL_VALUE == (arith)0)
  262. break;
  263. default:
  264. expr_warning(*expp,
  265. "illegal conversion of %s to pointer",
  266. symbol2str(oldtp->tp_fund));
  267. break;
  268. }
  269. if (oldtp->tp_size > tp->tp_size)
  270. expr_warning(*expp,
  271. "conversion of %s to pointer loses accuracy",
  272. symbol2str(oldtp->tp_fund));
  273. if (oldtp->tp_size != tp->tp_size)
  274. int2int(expp, tp);
  275. else
  276. (*expp)->ex_type = tp;
  277. }
  278. else
  279. if (oldtp->tp_fund == ERRONEOUS) {
  280. /* we just won't look */
  281. (*expp)->ex_type = tp; /* brute force */
  282. }
  283. else
  284. if (oldtp->tp_size == tp->tp_size && oper == CAST) {
  285. expr_warning(*expp, "dubious conversion based on equal size");
  286. (*expp)->ex_type = tp; /* brute force */
  287. }
  288. else {
  289. if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
  290. expr_error(*expp, "cannot convert %s to %s",
  291. symbol2str(oldtp->tp_fund),
  292. symbol2str(tp->tp_fund)
  293. );
  294. (*expp)->ex_type = tp; /* brute force */
  295. }
  296. }
  297. ch7asgn(expp, oper, expr)
  298. register struct expr **expp;
  299. struct expr *expr;
  300. {
  301. /* The assignment operators.
  302. "f op= e" should be interpreted as
  303. "f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
  304. and not as "f = f op (typeof f)e".
  305. Consider, for example, (i == 10) i *= 0.9; (i == 9), where
  306. typeof i == int.
  307. The resulting expression tree becomes:
  308. op=
  309. / \
  310. / \
  311. f (typeof (f op e))e
  312. EVAL should however take care of evaluating (typeof (f op e))f
  313. */
  314. int fund = (*expp)->ex_type->tp_fund;
  315. /* We expect an lvalue */
  316. if (!(*expp)->ex_lvalue) {
  317. expr_error(*expp, "no lvalue in lhs of %s", symbol2str(oper));
  318. (*expp)->ex_depth = 99; /* no direct store/load at EVAL() */
  319. /* what is 99 ??? DG */
  320. }
  321. if (oper == '=') {
  322. ch7cast(&expr, oper, (*expp)->ex_type);
  323. }
  324. else { /* turn e into e' where typeof(e') = typeof (f op e) */
  325. struct expr *extmp = intexpr(0, INT);
  326. /* this is really $#@&*%$# ! */
  327. extmp->ex_lvalue = 1;
  328. extmp->ex_type = (*expp)->ex_type;
  329. ch7bin(&extmp, oper, expr);
  330. /* note that ch7bin creates a tree of the expression
  331. ((typeof (f op e))f op (typeof (f op e))e),
  332. where f ~ extmp and e ~ expr;
  333. we have to use (typeof (f op e))e
  334. Ch7bin does not create a tree if both operands
  335. were illegal or constants!
  336. */
  337. if (extmp->ex_class == Oper) {
  338. expr = extmp->OP_RIGHT;
  339. extmp->OP_RIGHT = NILEXPR;
  340. free_expression(extmp);
  341. }
  342. else
  343. expr = extmp;
  344. }
  345. #ifndef NOBITFIELD
  346. if (fund == FIELD)
  347. *expp = new_oper((*expp)->ex_type->tp_up, *expp, oper, expr);
  348. else
  349. #endif NOBITFIELD
  350. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  351. (*expp)->OP_TYPE = expr->ex_type; /* for EVAL() */
  352. }
  353. /* Some interesting (?) questions answered.
  354. */
  355. int
  356. is_integral_type(tp)
  357. 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. return 1;
  366. #ifndef NOBITFIELD
  367. case FIELD:
  368. return is_integral_type(tp->tp_up);
  369. #endif NOBITFIELD
  370. default:
  371. return 0;
  372. }
  373. }
  374. int
  375. is_arith_type(tp)
  376. struct type *tp;
  377. {
  378. switch (tp->tp_fund) {
  379. case CHAR:
  380. case SHORT:
  381. case INT:
  382. case LONG:
  383. case ENUM:
  384. case FLOAT:
  385. case DOUBLE:
  386. return 1;
  387. #ifndef NOBITFIELD
  388. case FIELD:
  389. return is_arith_type(tp->tp_up);
  390. #endif NOBITFIELD
  391. default:
  392. return 0;
  393. }
  394. }