ch7.c 15 KB

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