expr.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 "declarator.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. warning("%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. error("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. }
  171. expr->ex_lvalue =
  172. ( def->df_type->tp_fund == FUNCTION ||
  173. def->df_type->tp_fund == ARRAY ||
  174. def->df_sc == ENUM
  175. ) ? 0 : 1;
  176. expr->ex_class = Value;
  177. if (def->df_sc == ENUM) {
  178. expr->VL_IDF = 0;
  179. expr->VL_VALUE = def->df_address;
  180. }
  181. else {
  182. expr->VL_IDF = idf;
  183. expr->VL_VALUE = (arith)0;
  184. }
  185. }
  186. string2expr(expr)
  187. struct expr *expr;
  188. {
  189. /* Dot contains a string which is turned into an expression.
  190. */
  191. expr->ex_type = string_type;
  192. expr->ex_lvalue = 0;
  193. expr->ex_class = String;
  194. expr->SG_VALUE = dot.tk_str;
  195. expr->SG_DATLAB = 0;
  196. }
  197. struct expr*
  198. intexpr(ivalue, fund)
  199. arith ivalue;
  200. {
  201. /* The value ivalue is turned into an integer expression of
  202. the size indicated by fund.
  203. */
  204. struct expr *expr = new_expr();
  205. clear((char *)expr, sizeof(struct expr));
  206. expr->ex_file = dot.tk_file;
  207. expr->ex_line = dot.tk_line;
  208. switch (fund) {
  209. case INT:
  210. expr->ex_type = int_type;
  211. break;
  212. case LONG:
  213. expr->ex_type = long_type;
  214. break;
  215. case UNSIGNED:
  216. /* We cannot make a test like "ivalue <= max_unsigned"
  217. because, if sizeof(long) == int_size holds, max_unsigned
  218. may be a negative long in which case the comparison
  219. results in an unexpected answer. We assume that
  220. the type "unsigned long" is not part of portable C !
  221. */
  222. expr->ex_type =
  223. (ivalue & ~max_unsigned) ? long_type : uint_type;
  224. break;
  225. case INTEGER:
  226. expr->ex_type = (ivalue <= max_int) ? int_type : long_type;
  227. break;
  228. default:
  229. crash("(intexpr) bad fund %s\n", symbol2str(fund));
  230. }
  231. expr->ex_class = Value;
  232. expr->VL_VALUE = ivalue;
  233. cut_size(expr);
  234. return expr;
  235. }
  236. float2expr(expr)
  237. struct expr *expr;
  238. {
  239. /* Dot contains a floating point constant which is turned
  240. into an expression.
  241. */
  242. expr->ex_type = double_type;
  243. expr->ex_class = Float;
  244. expr->FL_VALUE = dot.tk_fval;
  245. expr->FL_DATLAB = 0;
  246. }
  247. struct expr *
  248. new_oper(tp, e1, oper, e2)
  249. struct type *tp;
  250. struct expr *e1, *e2;
  251. {
  252. /* A new expression is constructed which consists of the
  253. operator oper which has e1 and e2 as operands; for a
  254. monadic operator e1 == NILEXPR.
  255. During the construction of the right recursive initialisation
  256. tree it is possible for e2 to be NILEXPR.
  257. */
  258. struct expr *expr = new_expr();
  259. struct oper *op;
  260. clear((char *)expr, sizeof(struct expr));
  261. if (!e1 || !e2) {
  262. expr->ex_file = dot.tk_file;
  263. expr->ex_line = dot.tk_line;
  264. }
  265. else {
  266. expr->ex_file = e2->ex_file;
  267. expr->ex_line = e2->ex_line;
  268. }
  269. expr->ex_type = tp;
  270. expr->ex_class = Oper;
  271. /* combine depths and flags of both expressions */
  272. if (e2) {
  273. int e1_depth = e1 ? e1->ex_depth : 0;
  274. int e1_flags = e1 ? e1->ex_flags : 0;
  275. expr->ex_depth =
  276. (e1_depth > e2->ex_depth ? e1_depth : e2->ex_depth)
  277. + 1;
  278. expr->ex_flags = (e1_flags | e2->ex_flags) & ~EX_PARENS;
  279. }
  280. op = &expr->ex_object.ex_oper;
  281. op->op_type = tp;
  282. op->op_oper = oper;
  283. op->op_left = e1;
  284. op->op_right = e2;
  285. return expr;
  286. }
  287. chk_cst_expr(expp)
  288. register struct expr **expp;
  289. {
  290. /* The expression expr is checked for constancy.
  291. There are 6 places where constant expressions occur in C:
  292. 1. after #if
  293. 2. in a global initialization
  294. 3. as size in an array declaration
  295. 4. as value in an enum declaration
  296. 5. as width in a bit field
  297. 6. as case value in a switch
  298. The constant expression in a global initialization is
  299. handled separately (by IVAL()).
  300. There are various disparate restrictions on each of
  301. the others in the various C compilers. I have tried some
  302. hypotheses to unify them, but all have failed.
  303. This routine will give a warning for those operators
  304. not allowed by K&R, under the R-option only. The anomalies
  305. are cast, logical operators and the expression comma.
  306. Special problems (of which there is only one, sizeof in
  307. Preprocessor #if) have to be dealt with locally
  308. Note that according to K&R the negation ! is illegal in
  309. constant expressions and is indeed rejected by the
  310. Ritchie compiler.
  311. */
  312. register struct expr *expr = *expp;
  313. register int fund = expr->ex_type->tp_fund;
  314. register int flags = expr->ex_flags;
  315. register int err = 0;
  316. #ifdef DEBUG
  317. print_expr("constant_expression", expr);
  318. #endif DEBUG
  319. if ( fund != CHAR && fund != SHORT && fund != INT &&
  320. fund != ENUM && fund != LONG
  321. ) {
  322. expr_error(expr, "non-numerical constant expression"), err++;
  323. }
  324. else
  325. if (!is_ld_cst(expr))
  326. expr_error(expr, "expression is not constant"), err++;
  327. if (options['R']) {
  328. if (flags & EX_CAST)
  329. expr_warning(expr,
  330. "cast in constant expression");
  331. if (flags & EX_LOGICAL)
  332. expr_warning(expr,
  333. "logical operator in constant expression");
  334. if (flags & EX_COMMA)
  335. expr_warning(expr,
  336. "expression comma in constant expression");
  337. }
  338. if (err) {
  339. free_expression(expr);
  340. *expp = intexpr((arith)1, INT);
  341. (*expp)->ex_type = error_type;
  342. }
  343. }
  344. init_expression(eppp, expr)
  345. struct expr ***eppp, *expr;
  346. {
  347. /* The expression expr is added to the tree designated
  348. indirectly by **eppp.
  349. The natural form of a tree representing an
  350. initial_value_list is right-recursive, ie. with the
  351. left-most comma as main operator. The iterative grammar in
  352. expression.g, however, tends to produce a left-recursive
  353. tree, ie. one with the right-most comma as its main
  354. operator.
  355. To produce a right-recursive tree from the iterative
  356. grammar, we keep track of the address of the pointer where
  357. the next expression must be hooked in.
  358. */
  359. **eppp = new_oper(void_type, expr, INITCOMMA, NILEXPR);
  360. *eppp = &(**eppp)->OP_RIGHT;
  361. }
  362. free_expression(expr)
  363. struct expr *expr;
  364. {
  365. /* The expression expr is freed recursively.
  366. */
  367. if (!expr)
  368. return;
  369. if (expr->ex_class == Oper) {
  370. free_expression(expr->OP_LEFT);
  371. free_expression(expr->OP_RIGHT);
  372. }
  373. free_expr(expr);
  374. }