ch7.c 9.4 KB

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