ch3.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. 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. ch3sel(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. ch3cast(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. if (is_ld_cst(exp)) {
  134. exp->VL_VALUE += sd->sd_offset;
  135. exp->ex_type = sd->sd_type;
  136. }
  137. else {
  138. exp = new_oper(sd->sd_type,
  139. exp, oper, intexpr(sd->sd_offset, INT));
  140. }
  141. exp->ex_lvalue = (sd->sd_type->tp_fund != ARRAY);
  142. exp->ex_flags &= ~EX_ILVALUE;
  143. }
  144. if ((sd->sd_type->tp_typequal & TQ_CONST)
  145. || (tp->tp_typequal & TQ_CONST))
  146. exp->ex_flags |= EX_READONLY;
  147. if ((sd->sd_type->tp_typequal & TQ_VOLATILE)
  148. || (tp->tp_typequal & TQ_VOLATILE))
  149. exp->ex_flags |= EX_VOLATILE;
  150. if (oper == '.' && exp->ex_flags & EX_READONLY) {
  151. exp->ex_type = qualifier_type(exp->ex_type, TQ_CONST);
  152. }
  153. if (exp->ex_flags & EX_VOLATILE) {
  154. exp->ex_type = qualifier_type(exp->ex_type, TQ_VOLATILE);
  155. }
  156. *expp = exp;
  157. }
  158. ch3incr(expp, oper)
  159. struct expr **expp;
  160. {
  161. /* The monadic prefix/postfix incr/decr operator oper is
  162. applied to *expp.
  163. */
  164. ch3asgn(expp, oper, intexpr((arith)1, INT));
  165. }
  166. ch3cast(expp, oper, tp)
  167. register struct expr **expp;
  168. register struct type *tp;
  169. {
  170. /* The expression *expp is cast to type tp; the cast is
  171. caused by the operator oper. If the cast has
  172. to be passed on to run time, its left operand will be an
  173. expression of class Type.
  174. */
  175. register struct type *oldtp;
  176. register struct expr *exp = *expp;
  177. int qual_lev, ascompat = 0;
  178. if (oper == RETURN && tp->tp_fund == VOID) {
  179. expr_strict(exp, "return <expression> in function returning void");
  180. exp->ex_type = void_type;
  181. return;
  182. }
  183. if (exp->ex_type->tp_fund == FUNCTION) {
  184. function2pointer(exp);
  185. }
  186. if (exp->ex_type->tp_fund == ARRAY)
  187. array2pointer(exp);
  188. if (exp->ex_class == String)
  189. string2pointer(exp);
  190. oldtp = exp->ex_type;
  191. if (oldtp->tp_size <= 0 && oldtp->tp_fund != VOID) {
  192. expr_error(exp,"incomplete type in expression");
  193. }
  194. #ifndef NOBITFIELD
  195. if (oldtp->tp_fund == FIELD) {
  196. field2arith(expp);
  197. ch3cast(expp, oper, tp);
  198. return;
  199. }
  200. if (tp->tp_fund == FIELD) {
  201. ch3cast(expp, oper, tp->tp_up);
  202. return;
  203. }
  204. #endif /* NOBITFIELD */
  205. switch (oper) {
  206. default: qual_lev = -1; break;
  207. case CAST: qual_lev = -999; break; /* ??? hack */
  208. case CASTAB:
  209. case '=':
  210. case RETURN: ascompat = 1; /* assignment compatibility */
  211. /* fallthrough */
  212. case '-':
  213. case '<':
  214. case '>':
  215. case LESSEQ:
  216. case GREATEREQ:
  217. case EQUAL:
  218. case NOTEQUAL:
  219. case ':':
  220. qual_lev = -2;
  221. break;
  222. }
  223. if (equal_type(tp, oldtp, qual_lev, 0)) {
  224. /* life is easy */
  225. if (ascompat && tp->tp_fund == POINTER) {
  226. if ((tp->tp_up->tp_typequal & oldtp->tp_up->tp_typequal)
  227. != oldtp->tp_up->tp_typequal) {
  228. expr_strict( exp, "qualifier error");
  229. }
  230. }
  231. exp->ex_type = tp; /* so qualifiers are allright */
  232. }
  233. else
  234. if (tp->tp_fund == VOID) {
  235. /* easy again */
  236. exp->ex_type = void_type;
  237. }
  238. else
  239. if (is_arith_type(oldtp) && is_arith_type(tp)) {
  240. int oldi = is_integral_type(oldtp);
  241. int i = is_integral_type(tp);
  242. if (oldi && i) {
  243. #ifdef LINT
  244. if (oper == CAST)
  245. exp->ex_type = tp;
  246. else {
  247. int2int(expp, tp);
  248. }
  249. #else /* LINT */
  250. int2int(expp, tp);
  251. #endif /* LINT */
  252. }
  253. else
  254. if (oldi && !i) {
  255. #ifdef LINT
  256. if (oper == CAST)
  257. exp->ex_type = tp;
  258. else {
  259. int2float(expp, tp);
  260. }
  261. #else /* LINT */
  262. int2float(expp, tp);
  263. #endif /* LINT */
  264. }
  265. else
  266. if (!oldi && i) {
  267. #ifdef LINT
  268. if (oper == CAST)
  269. exp->ex_type = tp;
  270. else {
  271. float2int(expp, tp);
  272. }
  273. #else /* LINT */
  274. float2int(expp, tp);
  275. #endif /* LINT */
  276. }
  277. else {
  278. /* !oldi && !i */
  279. #ifdef LINT
  280. if (oper == CAST)
  281. exp->ex_type = tp;
  282. else {
  283. float2float(expp, tp);
  284. }
  285. #else /* LINT */
  286. float2float(expp, tp);
  287. #endif /* LINT */
  288. }
  289. }
  290. else
  291. if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
  292. switch (oper) {
  293. case EQUAL:
  294. case NOTEQUAL:
  295. case '=':
  296. case CASTAB:
  297. case RETURN:
  298. case ':':
  299. if (tp->tp_up && oldtp->tp_up) {
  300. if (tp->tp_up->tp_fund == VOID
  301. && oldtp->tp_up->tp_fund != FUNCTION) {
  302. break; /* switch */
  303. }
  304. if (oldtp->tp_up->tp_fund == VOID
  305. && tp->tp_up->tp_fund != FUNCTION) {
  306. break; /* switch */
  307. }
  308. if (oldtp->tp_up->tp_fund == VOID
  309. && is_cp_cst(exp)
  310. && exp->VL_VALUE == (arith)0)
  311. break; /* switch */
  312. }
  313. /* falltrough */
  314. default:
  315. if (oper == CASTAB)
  316. expr_strict(exp, "incompatible pointers in call");
  317. else
  318. expr_strict(exp, "incompatible pointers in %s",
  319. symbol2str(oper));
  320. break;
  321. case CAST: break;
  322. }
  323. #ifdef LINT
  324. if (oper != CAST)
  325. lint_ptr_conv(oldtp->tp_up->tp_fund, tp->tp_up->tp_fund);
  326. #endif /* LINT */
  327. exp->ex_type = tp; /* free conversion */
  328. }
  329. else
  330. if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
  331. /* from pointer to integral */
  332. if (oper != CAST)
  333. expr_strict(exp,
  334. "illegal conversion of pointer to %s",
  335. symbol2str(tp->tp_fund));
  336. if (oldtp->tp_size > tp->tp_size)
  337. expr_warning(exp,
  338. "conversion of pointer to %s loses accuracy",
  339. symbol2str(tp->tp_fund));
  340. if (oldtp->tp_size != tp->tp_size) {
  341. int2int(expp, tp);
  342. } else
  343. exp->ex_type = tp;
  344. }
  345. else
  346. if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
  347. /* from integral to pointer */
  348. switch (oper) {
  349. case CAST:
  350. break;
  351. case CASTAB:
  352. case EQUAL:
  353. case NOTEQUAL:
  354. case '=':
  355. case RETURN:
  356. if (is_cp_cst(exp) && exp->VL_VALUE == (arith)0)
  357. break;
  358. default:
  359. expr_strict(exp,
  360. "illegal conversion of %s to pointer",
  361. symbol2str(oldtp->tp_fund));
  362. break;
  363. }
  364. if (oldtp->tp_size > tp->tp_size)
  365. expr_warning(exp,
  366. "conversion of %s to pointer loses accuracy",
  367. symbol2str(oldtp->tp_fund));
  368. if (oldtp->tp_size != tp->tp_size) {
  369. int2int(expp, tp);
  370. } else
  371. exp->ex_type = tp;
  372. }
  373. else
  374. if (oldtp->tp_fund == ERRONEOUS) {
  375. /* we just won't look */
  376. exp->ex_type = tp; /* brute force */
  377. }
  378. else
  379. if (oldtp->tp_size == tp->tp_size && oper == CAST) {
  380. expr_strict(exp, "dubious conversion based on equal size");
  381. exp->ex_type = tp; /* brute force */
  382. }
  383. else {
  384. if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
  385. expr_error(exp, "cannot convert %s to %s",
  386. symbol2str(oldtp->tp_fund),
  387. symbol2str(tp->tp_fund)
  388. );
  389. exp->ex_type = tp; /* brute force */
  390. }
  391. /* re-initialize exp, since *expp may have changed */
  392. exp = *expp;
  393. if (oper == CAST) {
  394. exp->ex_flags |= EX_ILVALUE;
  395. }
  396. }
  397. /* Determine whether two types are equal.
  398. */
  399. equal_type(tp, otp, qual_lev, diag)
  400. register struct type *tp, *otp;
  401. int qual_lev, 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. check_pseudoproto(pl, opl, diag)
  454. register struct proto *pl, *opl;
  455. {
  456. int retval = 1;
  457. if (pl->pl_flag & PL_ELLIPSIS) {
  458. if (diag) {
  459. error("illegal ellipsis terminator");
  460. pl->pl_flag |= PL_ERRGIVEN;
  461. opl->pl_flag |= PL_ERRGIVEN;
  462. }
  463. return 0;
  464. }
  465. if (opl->pl_flag & PL_VOID) {
  466. if (!(pl->pl_flag & PL_VOID)) {
  467. if (diag) {
  468. strict("function is defined without parameters");
  469. }
  470. return 0;
  471. }
  472. return 1;
  473. }
  474. while (pl && opl) {
  475. if (!equal_type(pl->pl_type, opl->pl_type, -1, diag)) {
  476. if (diag) {
  477. if (!(pl->pl_flag & PL_ERRGIVEN)
  478. && !(opl->pl_flag & PL_ERRGIVEN))
  479. error("incorrect type for parameter %s of definition",
  480. opl->pl_idf->id_text);
  481. pl->pl_flag |= PL_ERRGIVEN;
  482. opl->pl_flag |= PL_ERRGIVEN;
  483. }
  484. retval = 0;
  485. }
  486. pl = pl->next;
  487. opl = opl->next;
  488. }
  489. if (pl || opl) {
  490. if (diag) error("incorrect number of parameters");
  491. retval = 0;
  492. }
  493. return retval;
  494. }
  495. legal_mixture(tp, otp, diag)
  496. struct type *tp, *otp;
  497. int diag;
  498. {
  499. struct proto *pl = tp->tp_proto, *opl = otp->tp_proto;
  500. int retval = 1;
  501. register 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. equal_proto(pl, opl, diag)
  536. register struct proto *pl, *opl;
  537. int diag;
  538. {
  539. if (pl == opl)
  540. return 1;
  541. /* If only one type is a function type with a parameter type list
  542. (a function prototype), the composite type is a function
  543. prototype with parameter type list.
  544. */
  545. while ( pl && opl) {
  546. if ((pl->pl_flag & ~PL_ERRGIVEN) != (opl->pl_flag & ~PL_ERRGIVEN) ||
  547. !equal_type(pl->pl_type, opl->pl_type, -1, diag))
  548. return 0;
  549. pl = pl->next;
  550. opl = opl->next;
  551. }
  552. return !(pl || opl);
  553. }
  554. /* check if a type has a consqualified member */
  555. recurqual(tp, qual)
  556. struct type *tp;
  557. int qual;
  558. {
  559. register struct sdef *sdf;
  560. ASSERT(tp);
  561. if (tp->tp_typequal & qual) return 1;
  562. switch(tp->tp_fund) {
  563. case UNION:
  564. case STRUCT:
  565. case ENUM:
  566. sdf = tp->tp_sdef;
  567. while (sdf) {
  568. if (recurqual(sdf->sd_type, qual))
  569. return 1;
  570. sdf = sdf->sd_sdef;
  571. }
  572. break;
  573. }
  574. return 0;
  575. }
  576. ch3asgn(expp, oper, expr)
  577. struct expr **expp;
  578. struct expr *expr;
  579. {
  580. /* The assignment operators.
  581. "f op= e" should be interpreted as
  582. "f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
  583. and not as "f = f op (typeof f)e".
  584. Consider, for example, (i == 10) i *= 0.9; (i == 9), where
  585. typeof i == int.
  586. The resulting expression tree becomes:
  587. op=
  588. / \
  589. / \
  590. f (typeof (f op e))e
  591. EVAL should however take care of evaluating (typeof (f op e))f
  592. */
  593. register struct expr *exp = *expp;
  594. int fund = exp->ex_type->tp_fund;
  595. struct type *tp;
  596. char *oper_string = symbol2str(oper);
  597. /* We expect an lvalue */
  598. if (fund == ARRAY || fund == FUNCTION) exp->ex_lvalue = 0;
  599. if (!exp->ex_lvalue) {
  600. expr_error(exp, "no lvalue in operand of %s", oper_string);
  601. } else if (exp->ex_flags & EX_ILVALUE) {
  602. expr_strict(exp, "incorrect lvalue in operand of %s", oper_string);
  603. } else if (exp->ex_flags & EX_READONLY) {
  604. expr_error(exp, "operand of %s is read-only", oper_string);
  605. } else if (fund == STRUCT || fund == UNION) {
  606. if (recurqual(exp->ex_type, TQ_CONST))
  607. expr_error(exp,"operand of %s contains a const-qualified member",
  608. oper_string);
  609. }
  610. if (oper == '=') {
  611. ch3cast(&expr, oper, exp->ex_type);
  612. tp = expr->ex_type;
  613. }
  614. else { /* turn e into e' where typeof(e') = typeof (f op e) */
  615. struct expr *extmp = intexpr((arith)0, INT);
  616. /* this is really $#@&*%$# ! */
  617. /* if you correct this, please correct lint_new_oper() too */
  618. extmp->ex_lvalue = 1;
  619. extmp->ex_type = exp->ex_type;
  620. ch3bin(&extmp, oper, expr);
  621. /* Note that ch3bin creates a tree of the expression
  622. ((typeof (f op e))f op (typeof (f op e))e),
  623. where f ~ extmp and e ~ expr.
  624. We want to use (typeof (f op e))e.
  625. Ch3bin does not create a tree if both operands
  626. were illegal or constants!
  627. */
  628. tp = extmp->ex_type; /* perform the arithmetic in type tp */
  629. if (extmp->ex_class == Oper) {
  630. expr = extmp->OP_RIGHT;
  631. extmp->OP_RIGHT = NILEXPR;
  632. free_expression(extmp);
  633. }
  634. else
  635. expr = extmp;
  636. }
  637. #ifndef NOBITFIELD
  638. exp = new_oper(fund == FIELD ? exp->ex_type->tp_up : exp->ex_type,
  639. exp, oper, expr);
  640. #else /* NOBITFIELD */
  641. exp = new_oper(exp->ex_type, exp, oper, expr);
  642. #endif /* NOBITFIELD */
  643. exp->OP_TYPE = tp; /* for EVAL() */
  644. exp->ex_flags |= EX_SIDEEFFECTS;
  645. *expp = exp;
  646. }
  647. /* Some interesting (?) questions answered.
  648. */
  649. int
  650. is_integral_type(tp)
  651. register struct type *tp;
  652. {
  653. switch (tp->tp_fund) {
  654. case CHAR:
  655. case SHORT:
  656. case INT:
  657. case LONG:
  658. case ENUM:
  659. return 1;
  660. #ifndef NOBITFIELD
  661. case FIELD:
  662. return is_integral_type(tp->tp_up);
  663. #endif /* NOBITFIELD */
  664. default:
  665. return 0;
  666. }
  667. }
  668. int
  669. is_arith_type(tp)
  670. register struct type *tp;
  671. {
  672. switch (tp->tp_fund) {
  673. case CHAR:
  674. case SHORT:
  675. case INT:
  676. case LONG:
  677. case ENUM:
  678. case FLOAT:
  679. case DOUBLE:
  680. case LNGDBL:
  681. return 1;
  682. #ifndef NOBITFIELD
  683. case FIELD:
  684. return is_arith_type(tp->tp_up);
  685. #endif /* NOBITFIELD */
  686. default:
  687. return 0;
  688. }
  689. }