expr.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* $Header$ */
  2. /* EXPRESSION TREE HANDLING */
  3. #include "nofloat.h"
  4. #include "botch_free.h"
  5. #include <alloc.h>
  6. #include "idf.h"
  7. #include "arith.h"
  8. #include "def.h"
  9. #include "type.h"
  10. #include "label.h"
  11. #include "expr.h"
  12. #include "LLlex.h"
  13. #include "Lpars.h"
  14. #include "decspecs.h"
  15. #include "declar.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(ex)
  91. register struct expr *ex;
  92. {
  93. /* Returns the rank of the top node in the expression.
  94. */
  95. if (!ex || (ex->ex_flags & EX_PARENS) || ex->ex_class != Oper)
  96. return 0;
  97. return rank_of(ex->OP_OPER);
  98. }
  99. check_conditional(expr, oper, pos_descr)
  100. register 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. register struct expr *ex = new_expr();
  118. *expp = ex;
  119. ex->ex_file = dot.tk_file;
  120. ex->ex_line = dot.tk_line;
  121. switch (DOT) {
  122. case IDENTIFIER:
  123. idf2expr(ex);
  124. break;
  125. case STRING:
  126. string2expr(ex);
  127. break;
  128. case INTEGER:
  129. int2expr(ex);
  130. break;
  131. #ifndef NOFLOAT
  132. case FLOATING:
  133. float2expr(ex);
  134. break;
  135. #endif NOFLOAT
  136. default:
  137. crash("bad conversion to expression");
  138. break;
  139. }
  140. }
  141. idf2expr(expr)
  142. register 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 == '(') /* function call, declare name IMPLICITly */
  153. add_def(idf, IMPLICIT, funint_type, level); /* RM 13 */
  154. else {
  155. if (!is_anon_idf(idf))
  156. error("%s undefined", idf->id_text);
  157. /* declare 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. expr->ex_type = error_type;
  166. }
  167. else {
  168. def->df_used = 1;
  169. expr->ex_type = def->df_type;
  170. if (expr->ex_type == error_type)
  171. expr->ex_flags |= EX_ERROR;
  172. }
  173. expr->ex_lvalue =
  174. ( def->df_type->tp_fund == FUNCTION ||
  175. def->df_type->tp_fund == ARRAY ||
  176. def->df_sc == ENUM
  177. ) ? 0 : 1;
  178. expr->ex_class = Value;
  179. if (def->df_sc == ENUM) {
  180. expr->VL_CLASS = Const;
  181. expr->VL_VALUE = def->df_address;
  182. }
  183. else
  184. if (def->df_sc == STATIC && def->df_level >= L_LOCAL) {
  185. expr->VL_CLASS = Label;
  186. expr->VL_LBL = def->df_address;
  187. expr->VL_VALUE = (arith)0;
  188. }
  189. else {
  190. expr->VL_CLASS = Name;
  191. expr->VL_IDF = idf;
  192. expr->VL_VALUE = (arith)0;
  193. }
  194. }
  195. string2expr(expr)
  196. register struct expr *expr;
  197. {
  198. /* Dot contains a string which is turned into an expression.
  199. */
  200. expr->ex_type = string_type;
  201. expr->ex_lvalue = 0;
  202. expr->ex_class = String;
  203. expr->SG_VALUE = dot.tk_bts;
  204. expr->SG_LEN = dot.tk_len;
  205. expr->SG_DATLAB = 0;
  206. }
  207. int2expr(expr)
  208. struct expr *expr;
  209. {
  210. /* Dot contains an integer constant which is turned
  211. into an expression.
  212. */
  213. fill_int_expr(expr, dot.tk_ival, dot.tk_fund);
  214. }
  215. #ifndef NOFLOAT
  216. float2expr(expr)
  217. register struct expr *expr;
  218. {
  219. /* Dot contains a floating point constant which is turned
  220. into an expression.
  221. */
  222. expr->ex_type = double_type;
  223. expr->ex_class = Float;
  224. expr->FL_VALUE = dot.tk_fval;
  225. expr->FL_DATLAB = 0;
  226. }
  227. #endif NOFLOAT
  228. struct expr*
  229. intexpr(ivalue, fund)
  230. arith ivalue;
  231. int fund;
  232. {
  233. /* The value ivalue is turned into an integer expression of
  234. the size indicated by fund.
  235. */
  236. register struct expr *expr = new_expr();
  237. expr->ex_file = dot.tk_file;
  238. expr->ex_line = dot.tk_line;
  239. fill_int_expr(expr, ivalue, fund);
  240. return expr;
  241. }
  242. fill_int_expr(ex, ivalue, fund)
  243. register struct expr *ex;
  244. arith ivalue;
  245. int fund;
  246. {
  247. /* Details derived from ivalue and fund are put into the
  248. constant integer expression ex.
  249. */
  250. switch (fund) {
  251. case INT:
  252. ex->ex_type = int_type;
  253. break;
  254. case LONG:
  255. ex->ex_type = long_type;
  256. break;
  257. case UNSIGNED:
  258. /* We cannot make a test like
  259. ivalue <= max_unsigned
  260. because, if
  261. sizeof(long) == int_size
  262. holds, max_unsigned may be a negative long in
  263. which case the comparison results in an unexpected
  264. answer. We assume that the type "unsigned long"
  265. is not part of portable C !
  266. */
  267. ex->ex_type = (ivalue & ~max_unsigned) ? long_type : uint_type;
  268. break;
  269. case INTEGER:
  270. ex->ex_type = (ivalue <= max_int) ? int_type : long_type;
  271. break;
  272. default:
  273. crash("(intexpr) bad fund %s\n", symbol2str(fund));
  274. }
  275. ex->ex_class = Value;
  276. ex->VL_CLASS = Const;
  277. ex->VL_VALUE = ivalue;
  278. cut_size(ex);
  279. }
  280. struct expr *
  281. new_oper(tp, e1, oper, e2)
  282. struct type *tp;
  283. register struct expr *e1, *e2;
  284. {
  285. /* A new expression is constructed which consists of the
  286. operator oper which has e1 and e2 as operands; for a
  287. monadic operator e1 == NILEXPR.
  288. During the construction of the right recursive initialisation
  289. tree it is possible for e2 to be NILEXPR.
  290. */
  291. register struct expr *expr = new_expr();
  292. register struct oper *op;
  293. if (e2) {
  294. register struct expr *e = e2;
  295. while (e->ex_class == Oper && e->OP_LEFT)
  296. e = e->OP_LEFT;
  297. expr->ex_file = e->ex_file;
  298. expr->ex_line = e->ex_line;
  299. }
  300. else
  301. if (e1) {
  302. register struct expr *e = e1;
  303. while (e->ex_class == Oper && e->OP_RIGHT)
  304. e = e->OP_RIGHT;
  305. expr->ex_file = e->ex_file;
  306. expr->ex_line = e->ex_line;
  307. }
  308. else {
  309. expr->ex_file = dot.tk_file;
  310. expr->ex_line = dot.tk_line;
  311. }
  312. expr->ex_type = tp;
  313. expr->ex_class = Oper;
  314. /* combine depths and flags of both expressions */
  315. if (e2) {
  316. int e1_depth = e1 ? e1->ex_depth : 0;
  317. int e1_flags = e1 ? e1->ex_flags : 0;
  318. expr->ex_depth =
  319. (e1_depth > e2->ex_depth ? e1_depth : e2->ex_depth) + 1;
  320. expr->ex_flags = (e1_flags | e2->ex_flags) & ~EX_PARENS;
  321. }
  322. op = &expr->ex_object.ex_oper;
  323. op->op_type = tp;
  324. op->op_oper = oper;
  325. op->op_left = e1;
  326. op->op_right = e2;
  327. return expr;
  328. }
  329. chk_cst_expr(expp)
  330. register struct expr **expp;
  331. {
  332. /* The expression expr is checked for constancy.
  333. There are 6 places where constant expressions occur in C:
  334. 1. after #if
  335. 2. in a global initialization
  336. 3. as size in an array declaration
  337. 4. as value in an enum declaration
  338. 5. as width in a bit field
  339. 6. as case value in a switch
  340. The constant expression in a global initialization is
  341. handled separately (by IVAL()).
  342. There are various disparate restrictions on each of
  343. the others in the various C compilers. I have tried some
  344. hypotheses to unify them, but all have failed.
  345. This routine will give a warning for those operators
  346. not allowed by K&R, under the R-option only. The anomalies
  347. are cast, logical operators and the expression comma.
  348. Special problems (of which there is only one, sizeof in
  349. Preprocessor #if) have to be dealt with locally
  350. Note that according to K&R the negation ! is illegal in
  351. constant expressions and is indeed rejected by the
  352. Ritchie compiler.
  353. */
  354. register struct expr *expr = *expp;
  355. register int fund = expr->ex_type->tp_fund;
  356. register int flags = expr->ex_flags;
  357. int err = 0;
  358. #ifdef DEBUG
  359. print_expr("constant_expression", expr);
  360. #endif DEBUG
  361. if ( fund != CHAR && fund != SHORT && fund != INT &&
  362. fund != ENUM && fund != LONG
  363. )
  364. expr_error(expr, "non-numerical constant expression"), err++;
  365. else
  366. if (!is_ld_cst(expr))
  367. expr_error(expr, "expression is not constant"), err++;
  368. if (options['R']) {
  369. if (flags & EX_CAST)
  370. expr_warning(expr, "cast in constant expression");
  371. if (flags & EX_LOGICAL)
  372. expr_warning(expr,
  373. "logical operator in constant expression");
  374. if (flags & EX_COMMA)
  375. expr_warning(expr,
  376. "expression comma in constant expression");
  377. }
  378. if (err)
  379. erroneous2int(expp);
  380. }
  381. init_expression(eppp, expr)
  382. register struct expr ***eppp, *expr;
  383. {
  384. /* The expression expr is added to the tree designated
  385. indirectly by **eppp.
  386. The natural form of a tree representing an
  387. initial_value_list is right-recursive, ie. with the
  388. left-most comma as main operator. The iterative grammar in
  389. expression.g, however, tends to produce a left-recursive
  390. tree, ie. one with the right-most comma as its main
  391. operator.
  392. To produce a right-recursive tree from the iterative
  393. grammar, we keep track of the address of the pointer where
  394. the next expression must be hooked in.
  395. */
  396. **eppp = new_oper(void_type, expr, INITCOMMA, NILEXPR);
  397. *eppp = &(**eppp)->OP_RIGHT;
  398. }
  399. int
  400. is_ld_cst(expr)
  401. register struct expr *expr;
  402. {
  403. /* An expression is a `load-time constant' if it is of the form
  404. <idf> +/- <integral> or <integral>.
  405. */
  406. return expr->ex_lvalue == 0 && expr->ex_class == Value;
  407. }
  408. int
  409. is_cp_cst(expr)
  410. register struct expr *expr;
  411. {
  412. /* An expression is a `compile-time constant' if it is a
  413. load-time constant, and the idf is not there.
  414. */
  415. return is_ld_cst(expr) && expr->VL_CLASS == Const;
  416. }
  417. #ifndef NOFLOAT
  418. int
  419. is_fp_cst(expr)
  420. register struct expr *expr;
  421. {
  422. /* An expression is a `floating-point constant' if it consists
  423. of the float only.
  424. */
  425. return expr->ex_class == Float;
  426. }
  427. #endif NOFLOAT
  428. free_expression(expr)
  429. register struct expr *expr;
  430. {
  431. /* The expression expr is freed recursively.
  432. */
  433. if (expr) {
  434. if (expr->ex_class == Oper) {
  435. free_expression(expr->OP_LEFT);
  436. free_expression(expr->OP_RIGHT);
  437. }
  438. free_expr(expr);
  439. }
  440. }