ch7.c 9.2 KB

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