expr.c 10 KB

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