ch3.c 17 KB

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