ch3.c 17 KB

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