ch7.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /* $Id$ */
  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. }
  166. else
  167. #endif /* NOBITFIELD */
  168. if (oldtp == tp) {
  169. /* life is easy */
  170. }
  171. else
  172. if (tp->tp_fund == VOID) {
  173. /* Easy again */
  174. (*expp)->ex_type = void_type;
  175. }
  176. else
  177. if (is_arith_type(oldtp) && is_arith_type(tp)) {
  178. int oldi = is_integral_type(oldtp);
  179. int i = is_integral_type(tp);
  180. if (oldi && i) {
  181. if ( oper != CAST
  182. && ( tp->tp_fund == ENUM
  183. || oldtp->tp_fund == ENUM
  184. )
  185. ) {
  186. expr_warning(*expp,
  187. "dubious %s on enum",
  188. symbol2str(oper));
  189. }
  190. #ifdef LINT
  191. if (oper == CAST)
  192. (*expp)->ex_type = tp;
  193. else
  194. int2int(expp, tp);
  195. #else /* LINT */
  196. int2int(expp, tp);
  197. #endif /* LINT */
  198. }
  199. #ifndef NOFLOAT
  200. else
  201. if (oldi && !i) {
  202. if (oldtp->tp_fund == ENUM && oper != CAST)
  203. expr_warning(*expp,
  204. "conversion of enum to %s\n",
  205. symbol2str(tp->tp_fund));
  206. #ifdef LINT
  207. if (oper == CAST)
  208. (*expp)->ex_type = tp;
  209. else
  210. int2float(expp, tp);
  211. #else /* LINT */
  212. int2float(expp, tp);
  213. #endif /* LINT */
  214. }
  215. else
  216. if (!oldi && i) {
  217. #ifdef LINT
  218. if (oper == CAST)
  219. (*expp)->ex_type = tp;
  220. else
  221. float2int(expp, tp);
  222. #else /* LINT */
  223. float2int(expp, tp);
  224. #endif /* LINT */
  225. }
  226. else {
  227. /* !oldi && !i */
  228. #ifdef LINT
  229. if (oper == CAST)
  230. (*expp)->ex_type = tp;
  231. else
  232. float2float(expp, tp);
  233. #else /* LINT */
  234. float2float(expp, tp);
  235. #endif /* LINT */
  236. }
  237. #else /* NOFLOAT */
  238. else {
  239. crash("(ch7cast) floats not implemented\n");
  240. /*NOTREACHED*/
  241. }
  242. #endif /* NOFLOAT */
  243. }
  244. else
  245. if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
  246. if (oper != CAST)
  247. expr_warning(*expp, "incompatible pointers in %s",
  248. symbol2str(oper));
  249. #ifdef LINT
  250. if (oper != CAST)
  251. lint_ptr_conv(oldtp->tp_up->tp_fund, tp->tp_up->tp_fund);
  252. #endif /* LINT */
  253. (*expp)->ex_type = tp; /* free conversion */
  254. }
  255. else
  256. if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
  257. /* from pointer to integral */
  258. if (oper != CAST)
  259. expr_warning(*expp,
  260. "illegal conversion of pointer to %s",
  261. symbol2str(tp->tp_fund));
  262. if (oldtp->tp_size > tp->tp_size)
  263. expr_warning(*expp,
  264. "conversion of pointer to %s loses accuracy",
  265. symbol2str(tp->tp_fund));
  266. if (oldtp->tp_size != tp->tp_size)
  267. int2int(expp, tp);
  268. else
  269. (*expp)->ex_type = tp;
  270. }
  271. else
  272. if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
  273. /* from integral to pointer */
  274. switch (oper) {
  275. case CAST:
  276. break;
  277. case EQUAL:
  278. case NOTEQUAL:
  279. case ':':
  280. case '=':
  281. case RETURN:
  282. if (is_cp_cst(*expp) && (*expp)->VL_VALUE == (arith)0)
  283. break;
  284. default:
  285. expr_warning(*expp,
  286. "dubious conversion of %s to pointer",
  287. symbol2str(oldtp->tp_fund));
  288. break;
  289. }
  290. if (oldtp->tp_size > tp->tp_size)
  291. expr_warning(*expp,
  292. "conversion of %s to pointer loses accuracy",
  293. symbol2str(oldtp->tp_fund));
  294. if (oldtp->tp_size != tp->tp_size)
  295. int2int(expp, tp);
  296. else
  297. (*expp)->ex_type = tp;
  298. }
  299. else
  300. if (oldtp->tp_fund == ERRONEOUS) {
  301. /* we just won't look */
  302. (*expp)->ex_type = tp; /* brute force */
  303. }
  304. else
  305. if (oldtp->tp_size == tp->tp_size && oper == CAST) {
  306. expr_warning(*expp, "dubious conversion based on equal size");
  307. (*expp)->ex_type = tp; /* brute force */
  308. }
  309. else {
  310. if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
  311. expr_error(*expp, "cannot convert %s to %s",
  312. symbol2str(oldtp->tp_fund),
  313. symbol2str(tp->tp_fund)
  314. );
  315. (*expp)->ex_type = tp; /* brute force */
  316. }
  317. }
  318. ch7asgn(expp, oper, expr)
  319. struct expr **expp;
  320. struct expr *expr;
  321. {
  322. /* The assignment operators.
  323. "f op= e" should be interpreted as
  324. "f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
  325. and not as "f = f op (typeof f)e".
  326. Consider, for example, (i == 10) i *= 0.9; (i == 9), where
  327. typeof i == int.
  328. The resulting expression tree becomes:
  329. op=
  330. / \
  331. / \
  332. f (typeof (f op e))e
  333. EVAL should however take care of evaluating (typeof (f op e))f
  334. */
  335. register struct expr *exp = *expp;
  336. int fund = exp->ex_type->tp_fund;
  337. struct type *tp;
  338. /* We expect an lvalue */
  339. if (!exp->ex_lvalue) {
  340. expr_error(exp, "no lvalue in lhs of %s", symbol2str(oper));
  341. exp->ex_depth = 99; /* no direct store/load at EVAL() */
  342. /* what is 99 ??? DG */
  343. }
  344. if (oper == '=') {
  345. ch7cast(&expr, oper, exp->ex_type);
  346. tp = expr->ex_type;
  347. }
  348. else { /* turn e into e' where typeof(e') = typeof (f op e) */
  349. struct expr *extmp = intexpr((arith)0, INT);
  350. /* this is really $#@&*%$# ! */
  351. /* if you correct this, please correct lint_new_oper() too */
  352. extmp->ex_lvalue = 1;
  353. extmp->ex_type = exp->ex_type;
  354. ch7bin(&extmp, oper, expr);
  355. /* Note that ch7bin creates a tree of the expression
  356. ((typeof (f op e))f op (typeof (f op e))e),
  357. where f ~ extmp and e ~ expr.
  358. We want to use (typeof (f op e))e.
  359. Ch7bin does not create a tree if both operands
  360. were illegal or constants!
  361. */
  362. tp = extmp->ex_type; /* perform the arithmetic in type tp */
  363. if (extmp->ex_class == Oper) {
  364. expr = extmp->OP_RIGHT;
  365. extmp->OP_RIGHT = NILEXPR;
  366. free_expression(extmp);
  367. }
  368. else
  369. expr = extmp;
  370. }
  371. #ifndef NOBITFIELD
  372. if (fund == FIELD)
  373. exp = new_oper(exp->ex_type->tp_up, exp, oper, expr);
  374. else
  375. exp = new_oper(exp->ex_type, exp, oper, expr);
  376. #else /* NOBITFIELD */
  377. exp = new_oper(exp->ex_type, exp, oper, expr);
  378. #endif /* NOBITFIELD */
  379. exp->OP_TYPE = tp; /* for EVAL() */
  380. exp->ex_flags |= EX_SIDEEFFECTS;
  381. *expp = exp;
  382. }
  383. /* Some interesting (?) questions answered.
  384. */
  385. int
  386. is_integral_type(tp)
  387. register struct type *tp;
  388. {
  389. switch (tp->tp_fund) {
  390. case CHAR:
  391. case SHORT:
  392. case INT:
  393. case LONG:
  394. case ENUM:
  395. return 1;
  396. #ifndef NOBITFIELD
  397. case FIELD:
  398. return is_integral_type(tp->tp_up);
  399. #endif /* NOBITFIELD */
  400. default:
  401. return 0;
  402. }
  403. }
  404. int
  405. is_arith_type(tp)
  406. register struct type *tp;
  407. {
  408. switch (tp->tp_fund) {
  409. case CHAR:
  410. case SHORT:
  411. case INT:
  412. case LONG:
  413. case ENUM:
  414. #ifndef NOFLOAT
  415. case FLOAT:
  416. case DOUBLE:
  417. #endif /* NOFLOAT */
  418. return 1;
  419. #ifndef NOBITFIELD
  420. case FIELD:
  421. return is_arith_type(tp->tp_up);
  422. #endif /* NOBITFIELD */
  423. default:
  424. return 0;
  425. }
  426. }