expr.c 10 KB

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