expr.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* $Header$ */
  2. /* EXPRESSION TREE HANDLING */
  3. #include "botch_free.h" /* UF */
  4. #include "alloc.h"
  5. #include "idf.h"
  6. #include "arith.h"
  7. #include "def.h"
  8. #include "type.h"
  9. #include "label.h"
  10. #include "expr.h"
  11. #include "LLlex.h"
  12. #include "Lpars.h"
  13. #include "decspecs.h"
  14. #include "declar.h"
  15. #include "storage.h"
  16. #include "sizes.h"
  17. extern char *symbol2str();
  18. extern char options[];
  19. int
  20. rank_of(oper)
  21. int oper;
  22. {
  23. /* The rank of the operator oper is returned.
  24. */
  25. switch (oper) {
  26. default:
  27. return 0; /* INT2INT etc. */
  28. case '[':
  29. case '(':
  30. case '.':
  31. case ARROW:
  32. case PARCOMMA:
  33. return 1;
  34. case '!':
  35. case PLUSPLUS:
  36. case MINMIN:
  37. case CAST:
  38. case SIZEOF:
  39. return 2; /* monadic */
  40. case '*':
  41. case '/':
  42. case '%':
  43. return 3;
  44. case '+':
  45. case '-':
  46. return 4;
  47. case LEFT:
  48. case RIGHT:
  49. return 5;
  50. case '<':
  51. case '>':
  52. case LESSEQ:
  53. case GREATEREQ:
  54. return 6;
  55. case EQUAL:
  56. case NOTEQUAL:
  57. return 7;
  58. case '&':
  59. return 8;
  60. case '^':
  61. return 9;
  62. case '|':
  63. return 10;
  64. case AND:
  65. return 11;
  66. case OR:
  67. return 12;
  68. case '?':
  69. case ':':
  70. return 13;
  71. case '=':
  72. case PLUSAB:
  73. case MINAB:
  74. case TIMESAB:
  75. case DIVAB:
  76. case MODAB:
  77. case RIGHTAB:
  78. case LEFTAB:
  79. case ANDAB:
  80. case XORAB:
  81. case ORAB:
  82. return 14;
  83. case ',':
  84. return 15;
  85. }
  86. /*NOTREACHED*/
  87. }
  88. int
  89. rank_of_expression(expr)
  90. struct expr *expr;
  91. {
  92. /* Returns the rank of the top node in the expression.
  93. */
  94. if (!expr || (expr->ex_flags & EX_PARENS) || expr->ex_class != Oper)
  95. return 0;
  96. return rank_of(expr->OP_OPER);
  97. }
  98. check_conditional(expr, oper, pos_descr)
  99. struct expr *expr;
  100. char *pos_descr;
  101. {
  102. /* Warn if restricted C is in effect and the expression expr,
  103. which occurs at the position pos_descr, is not lighter than
  104. the operator oper.
  105. */
  106. if (options['R'] && rank_of_expression(expr) >= rank_of(oper))
  107. expr_warning(expr, "%s %s is ungrammatical",
  108. symbol2str(expr->OP_OPER), pos_descr);
  109. }
  110. dot2expr(expp)
  111. struct expr **expp;
  112. {
  113. /* The token in dot is converted into an expression, a
  114. pointer to which is stored in *expp.
  115. */
  116. *expp = new_expr();
  117. clear((char *)*expp, sizeof(struct expr));
  118. (*expp)->ex_file = dot.tk_file;
  119. (*expp)->ex_line = dot.tk_line;
  120. switch (DOT) {
  121. case IDENTIFIER:
  122. idf2expr(*expp);
  123. break;
  124. case STRING:
  125. string2expr(*expp);
  126. break;
  127. case INTEGER:
  128. *expp = intexpr(dot.tk_ival, dot.tk_fund);
  129. break;
  130. case FLOATING:
  131. float2expr(*expp);
  132. break;
  133. default:
  134. crash("bad conversion to expression");
  135. break;
  136. }
  137. }
  138. idf2expr(expr)
  139. struct expr *expr;
  140. {
  141. /* Dot contains an identifier which is turned into an
  142. expression.
  143. Note that this constitutes an applied occurrence of
  144. the identifier.
  145. */
  146. register struct idf *idf = dot.tk_idf; /* != 0*/
  147. register struct def *def = idf->id_def;
  148. if (def == 0) {
  149. if (AHEAD == '(') {
  150. /* Function call, so declare the name IMPLICITly. */
  151. /* See RM 13. */
  152. add_def(idf, IMPLICIT, funint_type, level);
  153. }
  154. else {
  155. if (!is_anon_idf(idf))
  156. error("%s undefined", idf->id_text);
  157. /* Declare the idf anyway */
  158. add_def(idf, 0, error_type, level);
  159. }
  160. def = idf->id_def;
  161. }
  162. /* now def != 0 */
  163. if (def->df_type->tp_fund == LABEL) {
  164. expr_error(expr, "illegal use of label %s", idf->id_text);
  165. }
  166. else {
  167. def->df_used = 1;
  168. expr->ex_type = def->df_type;
  169. if (expr->ex_type == error_type)
  170. expr->ex_flags |= EX_ERROR;
  171. }
  172. expr->ex_lvalue =
  173. ( def->df_type->tp_fund == FUNCTION ||
  174. def->df_type->tp_fund == ARRAY ||
  175. def->df_sc == ENUM
  176. ) ? 0 : 1;
  177. expr->ex_class = Value;
  178. if (def->df_sc == ENUM) {
  179. expr->VL_IDF = 0;
  180. expr->VL_VALUE = def->df_address;
  181. }
  182. else {
  183. expr->VL_IDF = idf;
  184. expr->VL_VALUE = (arith)0;
  185. }
  186. }
  187. string2expr(expr)
  188. struct expr *expr;
  189. {
  190. /* Dot contains a string which is turned into an expression.
  191. */
  192. expr->ex_type = string_type;
  193. expr->ex_lvalue = 0;
  194. expr->ex_class = String;
  195. expr->SG_VALUE = dot.tk_bts;
  196. expr->SG_LEN = dot.tk_len;
  197. expr->SG_DATLAB = 0;
  198. }
  199. struct expr*
  200. intexpr(ivalue, fund)
  201. arith ivalue;
  202. {
  203. /* The value ivalue is turned into an integer expression of
  204. the size indicated by fund.
  205. */
  206. struct expr *expr = new_expr();
  207. clear((char *)expr, sizeof(struct expr));
  208. expr->ex_file = dot.tk_file;
  209. expr->ex_line = dot.tk_line;
  210. switch (fund) {
  211. case INT:
  212. expr->ex_type = int_type;
  213. break;
  214. case LONG:
  215. expr->ex_type = long_type;
  216. break;
  217. case UNSIGNED:
  218. /* We cannot make a test like
  219. ivalue <= max_unsigned
  220. because, if
  221. sizeof(long) == int_size
  222. holds, max_unsigned may be a negative long in
  223. which case the comparison results in an unexpected
  224. answer. We assume that the type "unsigned long"
  225. is not part of portable C !
  226. */
  227. expr->ex_type =
  228. (ivalue & ~max_unsigned) ? long_type : uint_type;
  229. break;
  230. case INTEGER:
  231. expr->ex_type = (ivalue <= max_int) ? int_type : long_type;
  232. break;
  233. default:
  234. crash("(intexpr) bad fund %s\n", symbol2str(fund));
  235. }
  236. expr->ex_class = Value;
  237. expr->VL_VALUE = ivalue;
  238. cut_size(expr);
  239. return expr;
  240. }
  241. float2expr(expr)
  242. struct expr *expr;
  243. {
  244. /* Dot contains a floating point constant which is turned
  245. into an expression.
  246. */
  247. expr->ex_type = double_type;
  248. expr->ex_class = Float;
  249. expr->FL_VALUE = dot.tk_fval;
  250. expr->FL_DATLAB = 0;
  251. }
  252. struct expr *
  253. new_oper(tp, e1, oper, e2)
  254. struct type *tp;
  255. struct expr *e1, *e2;
  256. {
  257. /* A new expression is constructed which consists of the
  258. operator oper which has e1 and e2 as operands; for a
  259. monadic operator e1 == NILEXPR.
  260. During the construction of the right recursive initialisation
  261. tree it is possible for e2 to be NILEXPR.
  262. */
  263. struct expr *expr = new_expr();
  264. struct oper *op;
  265. clear((char *)expr, sizeof(struct expr));
  266. if (e2) {
  267. struct expr *e = e2;
  268. while (e->ex_class == Oper && e->OP_LEFT)
  269. e = e->OP_LEFT;
  270. expr->ex_file = e->ex_file;
  271. expr->ex_line = e->ex_line;
  272. }
  273. else
  274. if (e1) {
  275. struct expr *e = e1;
  276. while (e->ex_class == Oper && e->OP_RIGHT)
  277. e = e->OP_RIGHT;
  278. expr->ex_file = e->ex_file;
  279. expr->ex_line = e->ex_line;
  280. }
  281. else {
  282. expr->ex_file = dot.tk_file;
  283. expr->ex_line = dot.tk_line;
  284. }
  285. expr->ex_type = tp;
  286. expr->ex_class = Oper;
  287. /* combine depths and flags of both expressions */
  288. if (e2) {
  289. int e1_depth = e1 ? e1->ex_depth : 0;
  290. int e1_flags = e1 ? e1->ex_flags : 0;
  291. expr->ex_depth =
  292. (e1_depth > e2->ex_depth ? e1_depth : e2->ex_depth)
  293. + 1;
  294. expr->ex_flags = (e1_flags | e2->ex_flags) & ~EX_PARENS;
  295. }
  296. op = &expr->ex_object.ex_oper;
  297. op->op_type = tp;
  298. op->op_oper = oper;
  299. op->op_left = e1;
  300. op->op_right = e2;
  301. return expr;
  302. }
  303. chk_cst_expr(expp)
  304. register struct expr **expp;
  305. {
  306. /* The expression expr is checked for constancy.
  307. There are 6 places where constant expressions occur in C:
  308. 1. after #if
  309. 2. in a global initialization
  310. 3. as size in an array declaration
  311. 4. as value in an enum declaration
  312. 5. as width in a bit field
  313. 6. as case value in a switch
  314. The constant expression in a global initialization is
  315. handled separately (by IVAL()).
  316. There are various disparate restrictions on each of
  317. the others in the various C compilers. I have tried some
  318. hypotheses to unify them, but all have failed.
  319. This routine will give a warning for those operators
  320. not allowed by K&R, under the R-option only. The anomalies
  321. are cast, logical operators and the expression comma.
  322. Special problems (of which there is only one, sizeof in
  323. Preprocessor #if) have to be dealt with locally
  324. Note that according to K&R the negation ! is illegal in
  325. constant expressions and is indeed rejected by the
  326. Ritchie compiler.
  327. */
  328. register struct expr *expr = *expp;
  329. register int fund = expr->ex_type->tp_fund;
  330. register int flags = expr->ex_flags;
  331. register int err = 0;
  332. #ifdef DEBUG
  333. print_expr("constant_expression", expr);
  334. #endif DEBUG
  335. if ( fund != CHAR && fund != SHORT && fund != INT &&
  336. fund != ENUM && fund != LONG
  337. ) {
  338. expr_error(expr, "non-numerical constant expression"), err++;
  339. }
  340. else
  341. if (!is_ld_cst(expr))
  342. expr_error(expr, "expression is not constant"), err++;
  343. if (options['R']) {
  344. if (flags & EX_CAST)
  345. expr_warning(expr,
  346. "cast in constant expression");
  347. if (flags & EX_LOGICAL)
  348. expr_warning(expr,
  349. "logical operator in constant expression");
  350. if (flags & EX_COMMA)
  351. expr_warning(expr,
  352. "expression comma in constant expression");
  353. }
  354. if (err) {
  355. erroneous2int(expp);
  356. }
  357. }
  358. init_expression(eppp, expr)
  359. struct expr ***eppp, *expr;
  360. {
  361. /* The expression expr is added to the tree designated
  362. indirectly by **eppp.
  363. The natural form of a tree representing an
  364. initial_value_list is right-recursive, ie. with the
  365. left-most comma as main operator. The iterative grammar in
  366. expression.g, however, tends to produce a left-recursive
  367. tree, ie. one with the right-most comma as its main
  368. operator.
  369. To produce a right-recursive tree from the iterative
  370. grammar, we keep track of the address of the pointer where
  371. the next expression must be hooked in.
  372. */
  373. **eppp = new_oper(void_type, expr, INITCOMMA, NILEXPR);
  374. *eppp = &(**eppp)->OP_RIGHT;
  375. }
  376. int
  377. is_ld_cst(expr)
  378. register struct expr *expr;
  379. {
  380. /* An expression is a `load-time constant' if it is of the form
  381. <idf> +/- <integral> or <integral>.
  382. */
  383. return expr->ex_lvalue == 0 && expr->ex_class == Value;
  384. }
  385. int
  386. is_cp_cst(expr)
  387. register struct expr *expr;
  388. {
  389. /* An expression is a `compile-time constant' if it is a
  390. load-time constant, and the idf is not there.
  391. */
  392. return is_ld_cst(expr) && expr->VL_IDF == 0;
  393. }
  394. int
  395. is_fp_cst(expr)
  396. register struct expr *expr;
  397. {
  398. /* An expression is a `floating-point constant' if it consists
  399. of the float only.
  400. */
  401. return expr->ex_class == Float;
  402. }
  403. free_expression(expr)
  404. struct expr *expr;
  405. {
  406. /* The expression expr is freed recursively.
  407. */
  408. if (!expr)
  409. return;
  410. if (expr->ex_class == Oper) {
  411. free_expression(expr->OP_LEFT);
  412. free_expression(expr->OP_RIGHT);
  413. }
  414. free_expr(expr);
  415. }