expr.c 11 KB

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