ch7.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 "proto.h"
  14. #include "type.h"
  15. #include "struct.h"
  16. #include "label.h"
  17. #include "expr.h"
  18. #include "def.h"
  19. #include "Lpars.h"
  20. #include "assert.h"
  21. #include "file_info.h"
  22. extern char options[];
  23. extern char *symbol2str();
  24. /* Most expression-handling routines have a pointer to a
  25. (struct type *) as first parameter. The object under the pointer
  26. gets updated in the process.
  27. */
  28. ch7sel(expp, oper, idf)
  29. struct expr **expp;
  30. struct idf *idf;
  31. {
  32. /* The selector idf is applied to *expp; oper may be '.' or
  33. ARROW.
  34. */
  35. register struct expr *exp;
  36. register struct type *tp;
  37. register struct sdef *sd;
  38. any2opnd(expp, oper);
  39. exp = *expp;
  40. tp = exp->ex_type;
  41. if (oper == ARROW) {
  42. if (tp->tp_fund == POINTER &&
  43. ( tp->tp_up->tp_fund == STRUCT ||
  44. tp->tp_up->tp_fund == UNION)) /* normal case */
  45. tp = tp->tp_up;
  46. else { /* constructions like "12->selector" and
  47. "char c; c->selector"
  48. */
  49. switch (tp->tp_fund) {
  50. case INT:
  51. case LONG:
  52. /* Allowed by RM 14.1 */
  53. ch7cast(expp, CAST, pa_type);
  54. sd = idf2sdef(idf, tp);
  55. tp = sd->sd_stype;
  56. break;
  57. case POINTER:
  58. break;
  59. default:
  60. expr_error(exp, "-> applied to %s",
  61. symbol2str(tp->tp_fund));
  62. case ERRONEOUS:
  63. exp->ex_type = error_type;
  64. return;
  65. }
  66. }
  67. } /* oper == ARROW */
  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 INT:
  75. case LONG:
  76. /* warning will be given by idf2sdef() */
  77. break;
  78. default:
  79. if (!is_anon_idf(idf))
  80. expr_error(exp, "selector %s applied to %s",
  81. idf->id_text, symbol2str(tp->tp_fund));
  82. case ERRONEOUS:
  83. exp->ex_type = error_type;
  84. return;
  85. }
  86. sd = idf2sdef(idf, tp);
  87. if (oper == '.') {
  88. /* there are 3 cases in which the selection can be
  89. performed compile-time:
  90. I: n.sel (n either an identifier or a constant)
  91. II: (e.s1).s2 (transformed into (e.(s1+s2)))
  92. III: (e->s1).s2 (transformed into (e->(s1+s2)))
  93. The code performing these conversions is
  94. extremely obscure.
  95. */
  96. if (exp->ex_class == Value) {
  97. /* It is an object we know the address of; so
  98. we can calculate the address of the
  99. selected member
  100. */
  101. exp->VL_VALUE += sd->sd_offset;
  102. exp->ex_type = sd->sd_type;
  103. if (exp->ex_type == error_type)
  104. exp->ex_flags |= EX_ERROR;
  105. }
  106. else
  107. if (exp->ex_class == Oper) {
  108. struct oper *op = &(exp->ex_object.ex_oper);
  109. if (op->op_oper == '.' || op->op_oper == ARROW) {
  110. ASSERT(is_cp_cst(op->op_right));
  111. op->op_right->VL_VALUE += sd->sd_offset;
  112. exp->ex_type = sd->sd_type;
  113. if (exp->ex_type == error_type)
  114. exp->ex_flags |= EX_ERROR;
  115. }
  116. else {
  117. exp = new_oper(sd->sd_type, exp, '.',
  118. intexpr(sd->sd_offset, INT));
  119. exp->ex_lvalue = exp->OP_LEFT->ex_lvalue;
  120. }
  121. }
  122. }
  123. else { /* oper == ARROW */
  124. exp = new_oper(sd->sd_type,
  125. exp, oper, intexpr(sd->sd_offset, INT));
  126. exp->ex_lvalue = (sd->sd_type->tp_fund != ARRAY);
  127. }
  128. if (sd->sd_type->tp_typequal & TQ_CONST)
  129. exp->ex_flags |= EX_READONLY;
  130. if (sd->sd_type->tp_typequal & TQ_VOLATILE)
  131. exp->ex_flags |= EX_VOLATILE;
  132. *expp = exp;
  133. }
  134. ch7incr(expp, oper)
  135. struct expr **expp;
  136. {
  137. /* The monadic prefix/postfix incr/decr operator oper is
  138. applied to *expp.
  139. */
  140. ch7asgn(expp, oper, intexpr((arith)1, INT));
  141. }
  142. ch7cast(expp, oper, tp)
  143. register struct expr **expp;
  144. register struct type *tp;
  145. {
  146. /* The expression *expp is cast to type tp; the cast is
  147. caused by the operator oper. If the cast has
  148. to be passed on to run time, its left operand will be an
  149. expression of class Type.
  150. */
  151. register struct type *oldtp;
  152. if ((*expp)->ex_type->tp_fund == FUNCTION)
  153. function2pointer(*expp);
  154. if ((*expp)->ex_type->tp_fund == ARRAY)
  155. array2pointer(*expp);
  156. if ((*expp)->ex_class == String)
  157. string2pointer(*expp);
  158. oldtp = (*expp)->ex_type;
  159. #ifndef NOBITFIELD
  160. if (oldtp->tp_fund == FIELD) {
  161. field2arith(expp);
  162. ch7cast(expp, oper, tp);
  163. }
  164. else
  165. if (tp->tp_fund == FIELD) {
  166. ch7cast(expp, oper, tp->tp_up);
  167. }
  168. else
  169. #endif NOBITFIELD
  170. if (equal_type(tp, oldtp)) {
  171. /* life is easy */
  172. }
  173. else
  174. if (tp->tp_fund == VOID) {
  175. /* easy again */
  176. (*expp)->ex_type = void_type;
  177. }
  178. else
  179. if (is_arith_type(oldtp) && is_arith_type(tp)) {
  180. int oldi = is_integral_type(oldtp);
  181. int i = is_integral_type(tp);
  182. if (oldi && i) {
  183. if ( oper != CAST
  184. && ( tp->tp_fund == ENUM
  185. || oldtp->tp_fund == ENUM
  186. )
  187. ) {
  188. expr_warning(*expp,
  189. "dubious %s on enum",
  190. symbol2str(oper));
  191. }
  192. #ifdef LINT
  193. if (oper == CAST)
  194. (*expp)->ex_type = tp;
  195. else
  196. int2int(expp, tp);
  197. #else LINT
  198. int2int(expp, tp);
  199. #endif LINT
  200. }
  201. #ifndef NOFLOAT
  202. else
  203. if (oldi && !i) {
  204. if (oldtp->tp_fund == ENUM && oper != CAST)
  205. expr_warning(*expp,
  206. "conversion of enum to %s\n",
  207. symbol2str(tp->tp_fund));
  208. #ifdef LINT
  209. if (oper == CAST)
  210. (*expp)->ex_type = tp;
  211. else
  212. int2float(expp, tp);
  213. #else LINT
  214. int2float(expp, tp);
  215. #endif LINT
  216. }
  217. else
  218. if (!oldi && i) {
  219. #ifdef LINT
  220. if (oper == CAST)
  221. (*expp)->ex_type = tp;
  222. else
  223. float2int(expp, tp);
  224. #else LINT
  225. float2int(expp, tp);
  226. #endif LINT
  227. }
  228. else {
  229. /* !oldi && !i */
  230. #ifdef LINT
  231. if (oper == CAST)
  232. (*expp)->ex_type = tp;
  233. else
  234. float2float(expp, tp);
  235. #else LINT
  236. float2float(expp, tp);
  237. #endif LINT
  238. }
  239. #else NOFLOAT
  240. else {
  241. crash("(ch7cast) floats not implemented\n");
  242. /*NOTREACHED*/
  243. }
  244. #endif NOFLOAT
  245. }
  246. else
  247. if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
  248. if (oper == CASTAB)
  249. expr_warning(*expp, "incompatible pointers");
  250. else
  251. if (oper != CAST)
  252. expr_warning(*expp, "incompatible pointers in %s",
  253. symbol2str(oper));
  254. #ifdef LINT
  255. if (oper != CAST)
  256. lint_ptr_conv(oldtp->tp_up->tp_fund, tp->tp_up->tp_fund);
  257. #endif LINT
  258. (*expp)->ex_type = tp; /* free conversion */
  259. }
  260. else
  261. if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
  262. /* from pointer to integral */
  263. if (oper != CAST)
  264. expr_warning(*expp,
  265. "illegal conversion of pointer to %s",
  266. symbol2str(tp->tp_fund));
  267. if (oldtp->tp_size > tp->tp_size)
  268. expr_warning(*expp,
  269. "conversion of pointer to %s loses accuracy",
  270. symbol2str(tp->tp_fund));
  271. if (oldtp->tp_size != tp->tp_size)
  272. int2int(expp, tp);
  273. else
  274. (*expp)->ex_type = tp;
  275. }
  276. else
  277. if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
  278. /* from integral to pointer */
  279. switch (oper) {
  280. case CAST:
  281. break;
  282. case CASTAB:
  283. case EQUAL:
  284. case NOTEQUAL:
  285. case '=':
  286. case RETURN:
  287. if (is_cp_cst(*expp) && (*expp)->VL_VALUE == (arith)0)
  288. break;
  289. default:
  290. expr_warning(*expp,
  291. "illegal conversion of %s to pointer",
  292. symbol2str(oldtp->tp_fund));
  293. break;
  294. }
  295. if (oldtp->tp_size > tp->tp_size)
  296. expr_warning(*expp,
  297. "conversion of %s to pointer loses accuracy",
  298. symbol2str(oldtp->tp_fund));
  299. if (oldtp->tp_size != tp->tp_size)
  300. int2int(expp, tp);
  301. else
  302. (*expp)->ex_type = tp;
  303. }
  304. else
  305. if (oldtp->tp_fund == ERRONEOUS) {
  306. /* we just won't look */
  307. (*expp)->ex_type = tp; /* brute force */
  308. }
  309. else
  310. if (oldtp->tp_size == tp->tp_size && oper == CAST) {
  311. expr_warning(*expp, "dubious conversion based on equal size");
  312. (*expp)->ex_type = tp; /* brute force */
  313. }
  314. else {
  315. if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
  316. expr_error(*expp, "cannot convert %s to %s",
  317. symbol2str(oldtp->tp_fund),
  318. symbol2str(tp->tp_fund)
  319. );
  320. (*expp)->ex_type = tp; /* brute force */
  321. }
  322. }
  323. /* Determine whether two types are equal.
  324. */
  325. equal_type(tp, otp)
  326. register struct type *tp, *otp;
  327. {
  328. if (tp == otp)
  329. return 1;
  330. if (!tp || !otp)
  331. return 0;
  332. if (tp->tp_fund != otp->tp_fund)
  333. return 0;
  334. if (tp->tp_unsigned != otp->tp_unsigned)
  335. return 0;
  336. if (tp->tp_align != otp->tp_align)
  337. return 0;
  338. if (tp->tp_fund != ARRAY)
  339. if (tp->tp_size != otp->tp_size)
  340. return 0;
  341. switch (tp->tp_fund) {
  342. case FUNCTION:
  343. /* If both types have parameter type lists, the type of
  344. each parameter in the composite parameter type list
  345. is the composite type of the corresponding paramaters.
  346. */
  347. if (tp->tp_proto && otp->tp_proto &&
  348. !equal_proto(tp->tp_proto, otp->tp_proto))
  349. return 0;
  350. return equal_type(tp->tp_up, otp->tp_up);
  351. case ARRAY:
  352. /* If one type is an array of known size, the composite
  353. type is an array of that size
  354. */
  355. if (tp->tp_size != otp->tp_size &&
  356. (tp->tp_size != -1 && otp->tp_size != -1))
  357. return 0;
  358. return equal_type(tp->tp_up, otp->tp_up);
  359. case POINTER:
  360. case FIELD:
  361. return equal_type(tp->tp_up, otp->tp_up);
  362. case STRUCT:
  363. case UNION:
  364. case ENUM:
  365. return tp->tp_idf == otp->tp_idf && tp->tp_sdef == otp->tp_sdef;
  366. default:
  367. return 1;
  368. }
  369. }
  370. equal_proto(pl, opl)
  371. register struct proto *pl, *opl;
  372. {
  373. if (pl == opl)
  374. return 1;
  375. /* If only one type is a function type with a parameter type list
  376. (a function prototype), the composite type is a function
  377. prototype with parameter type list.
  378. */
  379. if (pl == 0 || opl == 0) return 0;
  380. if (pl->pl_flag != opl->pl_flag)
  381. return 0;
  382. if (!equal_type(pl->pl_type, opl->pl_type))
  383. return 0;
  384. return equal_proto(pl->next, opl->next);
  385. }
  386. ch7asgn(expp, oper, expr)
  387. struct expr **expp;
  388. struct expr *expr;
  389. {
  390. /* The assignment operators.
  391. "f op= e" should be interpreted as
  392. "f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
  393. and not as "f = f op (typeof f)e".
  394. Consider, for example, (i == 10) i *= 0.9; (i == 9), where
  395. typeof i == int.
  396. The resulting expression tree becomes:
  397. op=
  398. / \
  399. / \
  400. f (typeof (f op e))e
  401. EVAL should however take care of evaluating (typeof (f op e))f
  402. */
  403. register struct expr *exp = *expp;
  404. int fund = exp->ex_type->tp_fund;
  405. int vol = 0;
  406. struct type *tp;
  407. /* We expect an lvalue */
  408. if (!exp->ex_lvalue) {
  409. expr_error(exp, "no lvalue in lhs of %s", symbol2str(oper));
  410. exp->ex_depth = 99; /* no direct store/load at EVAL() */
  411. /* what is 99 ??? DG */
  412. }
  413. if (exp->ex_flags & EX_READONLY)
  414. strict("lhs of assignment is read-only");
  415. /* Preserve volatile markers across the tree.
  416. This is questionable, depending on the way the optimizer
  417. wants this information.
  418. vol = (exp->ex_flags & EX_VOLATILE) || (expr->ex_flags & EX_VOLATILE);
  419. */
  420. if (oper == '=') {
  421. ch7cast(&expr, oper, exp->ex_type);
  422. tp = expr->ex_type;
  423. }
  424. else { /* turn e into e' where typeof(e') = typeof (f op e) */
  425. struct expr *extmp = intexpr((arith)0, INT);
  426. /* this is really $#@&*%$# ! */
  427. /* if you correct this, please correct lint_new_oper() too */
  428. extmp->ex_lvalue = 1;
  429. extmp->ex_type = exp->ex_type;
  430. ch7bin(&extmp, oper, expr);
  431. /* Note that ch7bin creates a tree of the expression
  432. ((typeof (f op e))f op (typeof (f op e))e),
  433. where f ~ extmp and e ~ expr.
  434. We want to use (typeof (f op e))e.
  435. Ch7bin does not create a tree if both operands
  436. were illegal or constants!
  437. */
  438. tp = extmp->ex_type; /* perform the arithmetic in type tp */
  439. if (extmp->ex_class == Oper) {
  440. expr = extmp->OP_RIGHT;
  441. extmp->OP_RIGHT = NILEXPR;
  442. free_expression(extmp);
  443. }
  444. else
  445. expr = extmp;
  446. }
  447. #ifndef NOBITFIELD
  448. if (fund == FIELD)
  449. exp = new_oper(exp->ex_type->tp_up, exp, oper, expr);
  450. else
  451. exp = new_oper(exp->ex_type, exp, oper, expr);
  452. #else NOBITFIELD
  453. exp = new_oper(exp->ex_type, exp, oper, expr);
  454. #endif NOBITFIELD
  455. exp->OP_TYPE = tp; /* for EVAL() */
  456. exp->ex_flags |= vol ? (EX_SIDEEFFECTS|EX_VOLATILE) : EX_SIDEEFFECTS;
  457. *expp = exp;
  458. }
  459. /* Some interesting (?) questions answered.
  460. */
  461. int
  462. is_integral_type(tp)
  463. register struct type *tp;
  464. {
  465. switch (tp->tp_fund) {
  466. case GENERIC:
  467. case CHAR:
  468. case SHORT:
  469. case INT:
  470. case LONG:
  471. case ENUM:
  472. return 1;
  473. #ifndef NOBITFIELD
  474. case FIELD:
  475. return is_integral_type(tp->tp_up);
  476. #endif NOBITFIELD
  477. default:
  478. return 0;
  479. }
  480. }
  481. int
  482. is_arith_type(tp)
  483. register struct type *tp;
  484. {
  485. switch (tp->tp_fund) {
  486. case GENERIC:
  487. case CHAR:
  488. case SHORT:
  489. case INT:
  490. case LONG:
  491. case ENUM:
  492. #ifndef NOFLOAT
  493. case FLOAT:
  494. case DOUBLE:
  495. case LNGDBL:
  496. #endif NOFLOAT
  497. return 1;
  498. #ifndef NOBITFIELD
  499. case FIELD:
  500. return is_arith_type(tp->tp_up);
  501. #endif NOBITFIELD
  502. default:
  503. return 0;
  504. }
  505. }