ch7.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /* $Header$ */
  2. /* 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 */
  3. #include "debug.h"
  4. #include "nobitfield.h"
  5. #include "idf.h"
  6. #include "arith.h"
  7. #include "type.h"
  8. #include "struct.h"
  9. #include "label.h"
  10. #include "expr.h"
  11. #include "def.h"
  12. #include "Lpars.h"
  13. #include "assert.h"
  14. extern char options[];
  15. extern char *symbol2str();
  16. /* Most expression-handling routines have a pointer to a
  17. (struct type *) as first parameter. The object under the pointer
  18. gets updated in the process.
  19. */
  20. ch7sel(expp, oper, idf)
  21. register struct expr **expp;
  22. struct idf *idf;
  23. {
  24. /* The selector idf is applied to *expp; oper may be '.' or
  25. ARROW.
  26. */
  27. register struct type *tp = (*expp)->ex_type;
  28. register struct sdef *sd;
  29. if (oper == ARROW) {
  30. if (tp->tp_fund == POINTER) /* normal case */
  31. tp = tp->tp_up;
  32. else { /* constructions like "12->selector" and
  33. "char c; c->selector"
  34. */
  35. switch (tp->tp_fund) {
  36. case CHAR:
  37. case SHORT:
  38. case INT:
  39. case LONG:
  40. case ENUM:
  41. /* Allowed by RM 14.1 */
  42. ch7cast(expp, CAST, pa_type);
  43. sd = idf2sdef(idf, tp);
  44. tp = sd->sd_stype;
  45. break;
  46. default:
  47. expr_error(*expp, "-> applied to %s",
  48. symbol2str(tp->tp_fund));
  49. case ERRONEOUS:
  50. (*expp)->ex_type = error_type;
  51. return;
  52. }
  53. } /* tp->tp_fund != POINTER */
  54. } /* oper == ARROW */
  55. else { /* oper == '.' */
  56. /* filter out illegal expressions "non_lvalue.sel" */
  57. if (!(*expp)->ex_lvalue) {
  58. expr_error(*expp, "dot requires lvalue");
  59. return;
  60. }
  61. }
  62. switch (tp->tp_fund) {
  63. case POINTER: /* for int *p; p->next = ... */
  64. case STRUCT:
  65. case UNION:
  66. break;
  67. case CHAR:
  68. case SHORT:
  69. case INT:
  70. case LONG:
  71. case ENUM:
  72. /* warning will be given by idf2sdef() */
  73. break;
  74. default:
  75. if (!is_anon_idf(idf))
  76. expr_error(*expp, "selector %s applied to %s",
  77. idf->id_text, symbol2str(tp->tp_fund));
  78. case ERRONEOUS:
  79. (*expp)->ex_type = error_type;
  80. return;
  81. }
  82. sd = idf2sdef(idf, tp);
  83. if (oper == '.') {
  84. /* there are 3 cases in which the selection can be
  85. performed compile-time:
  86. I: n.sel (n either an identifier or a constant)
  87. II: (e.s1).s2 (transformed into (e.(s1+s2)))
  88. III: (e->s1).s2 (transformed into (e->(s1+s2)))
  89. The code performing these conversions is
  90. extremely obscure.
  91. */
  92. if ((*expp)->ex_class == Value) {
  93. /* It is an object we know the address of; so
  94. we can calculate the address of the
  95. selected member
  96. */
  97. (*expp)->VL_VALUE += sd->sd_offset;
  98. (*expp)->ex_type = sd->sd_type;
  99. if ((*expp)->ex_type == error_type)
  100. (*expp)->ex_flags |= EX_ERROR;
  101. }
  102. else
  103. if ((*expp)->ex_class == Oper) {
  104. struct oper *op = &((*expp)->ex_object.ex_oper);
  105. if (op->op_oper == '.' || op->op_oper == ARROW) {
  106. op->op_right->VL_VALUE += sd->sd_offset;
  107. (*expp)->ex_type = sd->sd_type;
  108. if ((*expp)->ex_type == error_type)
  109. (*expp)->ex_flags |= EX_ERROR;
  110. }
  111. else
  112. *expp = new_oper(sd->sd_type, *expp, '.',
  113. intexpr(sd->sd_offset, INT));
  114. }
  115. }
  116. else {
  117. /* oper == ARROW */
  118. *expp = new_oper(sd->sd_type,
  119. *expp, oper, intexpr(sd->sd_offset, INT));
  120. }
  121. (*expp)->ex_lvalue = (sd->sd_type->tp_fund != ARRAY);
  122. }
  123. ch7incr(expp, oper)
  124. register struct expr **expp;
  125. {
  126. /* The monadic prefix/postfix incr/decr operator oper is
  127. applied to *expp.
  128. */
  129. arith addend;
  130. struct expr *expr;
  131. register int fund = (*expp)->ex_type->tp_fund;
  132. if (!(*expp)->ex_lvalue) {
  133. expr_error(*expp, "no lvalue with %s", symbol2str(oper));
  134. return;
  135. }
  136. if (fund == ENUM) {
  137. expr_warning(*expp, "%s on enum", symbol2str(oper));
  138. addend = (arith)1;
  139. }
  140. else
  141. if (is_arith_type((*expp)->ex_type))
  142. addend = (arith)1;
  143. else
  144. if (fund == POINTER)
  145. addend = size_of_type((*expp)->ex_type->tp_up, "object");
  146. #ifndef NOBITFIELD
  147. else
  148. if (fund == FIELD)
  149. addend = (arith)1;
  150. #endif NOBITFIELD
  151. else {
  152. expr_error(*expp, "%s on %s",
  153. symbol2str(oper),
  154. symbol2str((*expp)->ex_type->tp_fund)
  155. );
  156. return;
  157. }
  158. expr = intexpr(addend, INT);
  159. ch7cast(&expr, CAST, (*expp)->ex_type);
  160. #ifndef NOBITFIELD
  161. if (fund == FIELD)
  162. *expp = new_oper((*expp)->ex_type->tp_up, *expp, oper, expr);
  163. else
  164. #endif NOBITFIELD
  165. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  166. }
  167. ch7cast(expp, oper, tp)
  168. register struct expr **expp;
  169. register struct type *tp;
  170. {
  171. /* The expression *expp is cast to type tp; the cast is
  172. caused by the operator oper. If the cast has
  173. to be passed on to run time, its left operand will be an
  174. expression of class Type.
  175. */
  176. register struct type *oldtp;
  177. if ((*expp)->ex_type->tp_fund == FUNCTION)
  178. function2pointer(expp);
  179. if ((*expp)->ex_type->tp_fund == ARRAY)
  180. array2pointer(expp);
  181. oldtp = (*expp)->ex_type;
  182. if (oldtp == tp)
  183. {} /* life is easy */
  184. else
  185. #ifndef NOBITFIELD
  186. if (oldtp->tp_fund == FIELD) {
  187. field2arith(expp);
  188. ch7cast(expp, oper, tp);
  189. }
  190. else
  191. if (tp->tp_fund == FIELD)
  192. ch7cast(expp, oper, tp->tp_up);
  193. else
  194. #endif NOBITFIELD
  195. if (tp->tp_fund == VOID) /* Easy again */
  196. (*expp)->ex_type = void_type;
  197. else
  198. if (is_arith_type(oldtp) && is_arith_type(tp)) {
  199. int oldi = is_integral_type(oldtp);
  200. int i = is_integral_type(tp);
  201. if (oldi && i) {
  202. if ( oldtp->tp_fund == ENUM &&
  203. tp->tp_fund == ENUM &&
  204. oper != CAST
  205. )
  206. expr_warning(*expp,
  207. "%s on enums of different types",
  208. symbol2str(oper));
  209. int2int(expp, tp);
  210. }
  211. else
  212. if (oldi && !i) {
  213. if (oldtp->tp_fund == ENUM && oper != CAST)
  214. expr_warning(*expp,
  215. "conversion of enum to %s\n",
  216. symbol2str(tp->tp_fund));
  217. int2float(expp, tp);
  218. }
  219. else
  220. if (!oldi && i)
  221. float2int(expp, tp);
  222. else /* !oldi && !i */
  223. float2float(expp, tp);
  224. }
  225. else
  226. if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
  227. if (oper != CAST)
  228. expr_warning(*expp, "incompatible pointers in %s",
  229. symbol2str(oper));
  230. (*expp)->ex_type = tp; /* free conversion */
  231. }
  232. else
  233. if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
  234. /* from pointer to integral */
  235. if (oper != CAST)
  236. expr_warning(*expp,
  237. "illegal conversion of pointer to %s",
  238. symbol2str(tp->tp_fund));
  239. if (oldtp->tp_size > tp->tp_size)
  240. expr_warning(*expp,
  241. "conversion of pointer to %s loses accuracy",
  242. symbol2str(tp->tp_fund));
  243. if (oldtp->tp_size != tp->tp_size)
  244. int2int(expp, tp);
  245. else
  246. (*expp)->ex_type = tp;
  247. }
  248. else
  249. if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
  250. /* from integral to pointer */
  251. switch (oper) {
  252. case CAST:
  253. break;
  254. case EQUAL:
  255. case NOTEQUAL:
  256. case '=':
  257. case RETURN:
  258. if (is_cp_cst(*expp) && (*expp)->VL_VALUE == (arith)0)
  259. break;
  260. default:
  261. expr_warning(*expp,
  262. "illegal conversion of %s to pointer",
  263. symbol2str(oldtp->tp_fund));
  264. break;
  265. }
  266. if (oldtp->tp_size > tp->tp_size)
  267. expr_warning(*expp,
  268. "conversion of %s to pointer loses accuracy",
  269. symbol2str(oldtp->tp_fund));
  270. if (oldtp->tp_size != tp->tp_size)
  271. int2int(expp, tp);
  272. else
  273. (*expp)->ex_type = tp;
  274. }
  275. else
  276. if (oldtp->tp_fund == ERRONEOUS) {
  277. /* we just won't look */
  278. (*expp)->ex_type = tp; /* brute force */
  279. }
  280. else
  281. if (oldtp->tp_size == tp->tp_size && oper == CAST) {
  282. expr_warning(*expp, "dubious conversion based on equal size");
  283. (*expp)->ex_type = tp; /* brute force */
  284. }
  285. else {
  286. if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
  287. expr_error(*expp, "cannot convert %s to %s",
  288. symbol2str(oldtp->tp_fund),
  289. symbol2str(tp->tp_fund)
  290. );
  291. (*expp)->ex_type = tp; /* brute force */
  292. }
  293. }
  294. ch7asgn(expp, oper, expr)
  295. register struct expr **expp;
  296. struct expr *expr;
  297. {
  298. /* The assignment operators.
  299. */
  300. int fund = (*expp)->ex_type->tp_fund;
  301. /* We expect an lvalue */
  302. if (!(*expp)->ex_lvalue) {
  303. expr_error(*expp, "no lvalue in lhs of %s", symbol2str(oper));
  304. (*expp)->ex_depth = 99; /* no direct store/load at EVAL() */
  305. /* what is 99 ??? DG */
  306. }
  307. switch (oper) {
  308. case '=':
  309. ch7cast(&expr, oper, (*expp)->ex_type);
  310. break;
  311. case TIMESAB:
  312. case DIVAB:
  313. case MODAB:
  314. check_arith_type(expp, oper);
  315. any2arith(&expr, oper);
  316. ch7cast(&expr, CAST, (*expp)->ex_type);
  317. break;
  318. case PLUSAB:
  319. case MINAB:
  320. any2arith(&expr, oper);
  321. if (fund == POINTER) {
  322. check_integral_type(&expr, oper);
  323. ch7bin(&expr, '*',
  324. intexpr(
  325. size_of_type(
  326. (*expp)->ex_type->tp_up,
  327. "object"
  328. ),
  329. pa_type->tp_fund
  330. )
  331. );
  332. }
  333. else {
  334. check_arith_type(expp, oper);
  335. ch7cast(&expr, CAST, (*expp)->ex_type);
  336. }
  337. break;
  338. case LEFTAB:
  339. case RIGHTAB:
  340. check_integral_type(expp, oper);
  341. ch7cast(&expr, oper, int_type);
  342. break;
  343. case ANDAB:
  344. case XORAB:
  345. case ORAB:
  346. check_integral_type(expp, oper);
  347. ch7cast(&expr, oper, (*expp)->ex_type);
  348. break;
  349. }
  350. #ifndef NOBITFIELD
  351. if (fund == FIELD)
  352. *expp = new_oper((*expp)->ex_type->tp_up, *expp, oper, expr);
  353. else
  354. #endif NOBITFIELD
  355. *expp = new_oper((*expp)->ex_type, *expp, oper, expr);
  356. }
  357. /* Some interesting (?) questions answered.
  358. */
  359. int
  360. is_integral_type(tp)
  361. struct type *tp;
  362. {
  363. switch (tp->tp_fund) {
  364. case CHAR:
  365. case SHORT:
  366. case INT:
  367. case LONG:
  368. case ENUM:
  369. return 1;
  370. #ifndef NOBITFIELD
  371. case FIELD:
  372. return is_integral_type(tp->tp_up);
  373. #endif NOBITFIELD
  374. default:
  375. return 0;
  376. }
  377. }
  378. check_integral_type(expp, oper)
  379. struct expr **expp;
  380. {
  381. register struct expr *expr = *expp;
  382. if (!is_integral_type(expr->ex_type)) {
  383. expr_error(expr, "%s on non-integral type (%s)",
  384. symbol2str(oper), symbol2str(expr->ex_type->tp_fund));
  385. erroneous2int(expp);
  386. }
  387. }
  388. int
  389. is_arith_type(tp)
  390. struct type *tp;
  391. {
  392. switch (tp->tp_fund) {
  393. case CHAR:
  394. case SHORT:
  395. case INT:
  396. case LONG:
  397. case ENUM:
  398. case FLOAT:
  399. case DOUBLE:
  400. return 1;
  401. #ifndef NOBITFIELD
  402. case FIELD:
  403. return is_arith_type(tp->tp_up);
  404. #endif NOBITFIELD
  405. default:
  406. return 0;
  407. }
  408. }
  409. check_arith_type(expp, oper)
  410. struct expr **expp;
  411. {
  412. register struct expr *expr = *expp;
  413. if (!is_arith_type(expr->ex_type)) {
  414. expr_error(expr, "%s on non-arithmetical type (%s)",
  415. symbol2str(oper), symbol2str(expr->ex_type->tp_fund));
  416. erroneous2int(expp);
  417. }
  418. }