expr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * Expression Abstract Syntax Tree Functions
  3. *
  4. * Copyright 2002 Ove Kaaven
  5. * Copyright 2006-2008 Robert Shearman
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include "widl.h"
  29. #include "utils.h"
  30. #include "expr.h"
  31. #include "header.h"
  32. #include "typetree.h"
  33. #include "typegen.h"
  34. static int is_integer_type(const type_t *type)
  35. {
  36. switch (type_get_type(type))
  37. {
  38. case TYPE_ENUM:
  39. return TRUE;
  40. case TYPE_BASIC:
  41. switch (type_basic_get_type(type))
  42. {
  43. case TYPE_BASIC_INT8:
  44. case TYPE_BASIC_INT16:
  45. case TYPE_BASIC_INT32:
  46. case TYPE_BASIC_INT64:
  47. case TYPE_BASIC_INT:
  48. case TYPE_BASIC_INT3264:
  49. case TYPE_BASIC_LONG:
  50. case TYPE_BASIC_CHAR:
  51. case TYPE_BASIC_HYPER:
  52. case TYPE_BASIC_BYTE:
  53. case TYPE_BASIC_WCHAR:
  54. case TYPE_BASIC_ERROR_STATUS_T:
  55. return TRUE;
  56. case TYPE_BASIC_FLOAT:
  57. case TYPE_BASIC_DOUBLE:
  58. case TYPE_BASIC_HANDLE:
  59. return FALSE;
  60. }
  61. return FALSE;
  62. default:
  63. return FALSE;
  64. }
  65. }
  66. static int is_signed_integer_type(const type_t *type)
  67. {
  68. switch (type_get_type(type))
  69. {
  70. case TYPE_ENUM:
  71. return FALSE;
  72. case TYPE_BASIC:
  73. switch (type_basic_get_type(type))
  74. {
  75. case TYPE_BASIC_INT8:
  76. case TYPE_BASIC_INT16:
  77. case TYPE_BASIC_INT32:
  78. case TYPE_BASIC_INT64:
  79. case TYPE_BASIC_INT:
  80. case TYPE_BASIC_INT3264:
  81. case TYPE_BASIC_LONG:
  82. return type_basic_get_sign(type) < 0;
  83. case TYPE_BASIC_CHAR:
  84. return TRUE;
  85. case TYPE_BASIC_HYPER:
  86. case TYPE_BASIC_BYTE:
  87. case TYPE_BASIC_WCHAR:
  88. case TYPE_BASIC_ERROR_STATUS_T:
  89. case TYPE_BASIC_FLOAT:
  90. case TYPE_BASIC_DOUBLE:
  91. case TYPE_BASIC_HANDLE:
  92. return FALSE;
  93. }
  94. /* FALLTHROUGH */
  95. default:
  96. return FALSE;
  97. }
  98. }
  99. static int is_float_type(const type_t *type)
  100. {
  101. return (type_get_type(type) == TYPE_BASIC &&
  102. (type_basic_get_type(type) == TYPE_BASIC_FLOAT ||
  103. type_basic_get_type(type) == TYPE_BASIC_DOUBLE));
  104. }
  105. expr_t *make_expr(enum expr_type type)
  106. {
  107. expr_t *e = xmalloc(sizeof(expr_t));
  108. e->type = type;
  109. e->ref = NULL;
  110. e->u.lval = 0;
  111. e->is_const = FALSE;
  112. e->cval = 0;
  113. return e;
  114. }
  115. expr_t *make_exprl(enum expr_type type, int val)
  116. {
  117. expr_t *e = xmalloc(sizeof(expr_t));
  118. e->type = type;
  119. e->ref = NULL;
  120. e->u.lval = val;
  121. e->is_const = FALSE;
  122. /* check for numeric constant */
  123. if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE)
  124. {
  125. /* make sure true/false value is valid */
  126. assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
  127. e->is_const = TRUE;
  128. e->cval = val;
  129. }
  130. return e;
  131. }
  132. expr_t *make_exprd(enum expr_type type, double val)
  133. {
  134. expr_t *e = xmalloc(sizeof(expr_t));
  135. e->type = type;
  136. e->ref = NULL;
  137. e->u.dval = val;
  138. e->is_const = TRUE;
  139. e->cval = val;
  140. return e;
  141. }
  142. expr_t *make_exprs(enum expr_type type, char *val)
  143. {
  144. expr_t *e;
  145. e = xmalloc(sizeof(expr_t));
  146. e->type = type;
  147. e->ref = NULL;
  148. e->u.sval = val;
  149. e->is_const = FALSE;
  150. /* check for predefined constants */
  151. switch (type)
  152. {
  153. case EXPR_IDENTIFIER:
  154. {
  155. var_t *c = find_const(val, 0);
  156. if (c)
  157. {
  158. e->u.sval = c->name;
  159. free(val);
  160. e->is_const = TRUE;
  161. e->cval = c->eval->cval;
  162. }
  163. break;
  164. }
  165. case EXPR_CHARCONST:
  166. if (!val[0])
  167. error_loc("empty character constant\n");
  168. else if (val[1])
  169. error_loc("multi-character constants are endian dependent\n");
  170. else
  171. {
  172. e->is_const = TRUE;
  173. e->cval = *val;
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. return e;
  180. }
  181. expr_t *make_exprt(enum expr_type type, var_t *var, expr_t *expr)
  182. {
  183. expr_t *e;
  184. type_t *tref;
  185. if (var->declspec.stgclass != STG_NONE && var->declspec.stgclass != STG_REGISTER)
  186. error_loc("invalid storage class for type expression\n");
  187. tref = var->declspec.type;
  188. e = xmalloc(sizeof(expr_t));
  189. e->type = type;
  190. e->ref = expr;
  191. e->u.tref = var->declspec;
  192. e->is_const = FALSE;
  193. if (type == EXPR_SIZEOF)
  194. {
  195. /* only do this for types that should be the same on all platforms */
  196. if (is_integer_type(tref) || is_float_type(tref))
  197. {
  198. e->is_const = TRUE;
  199. e->cval = type_memsize(tref);
  200. }
  201. }
  202. /* check for cast of constant expression */
  203. if (type == EXPR_CAST && expr->is_const)
  204. {
  205. if (is_integer_type(tref))
  206. {
  207. unsigned int cast_type_bits = type_memsize(tref) * 8;
  208. unsigned int cast_mask;
  209. e->is_const = TRUE;
  210. if (is_signed_integer_type(tref))
  211. {
  212. cast_mask = (1u << (cast_type_bits - 1)) - 1;
  213. if (expr->cval & (1u << (cast_type_bits - 1)))
  214. e->cval = -((-expr->cval) & cast_mask);
  215. else
  216. e->cval = expr->cval & cast_mask;
  217. }
  218. else
  219. {
  220. /* calculate ((1 << cast_type_bits) - 1) avoiding overflow */
  221. cast_mask = ((1u << (cast_type_bits - 1)) - 1) |
  222. 1u << (cast_type_bits - 1);
  223. e->cval = expr->cval & cast_mask;
  224. }
  225. }
  226. else
  227. {
  228. e->is_const = TRUE;
  229. e->cval = expr->cval;
  230. }
  231. }
  232. free(var);
  233. return e;
  234. }
  235. expr_t *make_expr1(enum expr_type type, expr_t *expr)
  236. {
  237. expr_t *e;
  238. e = xmalloc(sizeof(expr_t));
  239. e->type = type;
  240. e->ref = expr;
  241. e->u.lval = 0;
  242. e->is_const = FALSE;
  243. /* check for compile-time optimization */
  244. if (expr->is_const)
  245. {
  246. e->is_const = TRUE;
  247. switch (type)
  248. {
  249. case EXPR_LOGNOT:
  250. e->cval = !expr->cval;
  251. break;
  252. case EXPR_POS:
  253. e->cval = +expr->cval;
  254. break;
  255. case EXPR_NEG:
  256. e->cval = -expr->cval;
  257. break;
  258. case EXPR_NOT:
  259. e->cval = ~expr->cval;
  260. break;
  261. default:
  262. e->is_const = FALSE;
  263. break;
  264. }
  265. }
  266. return e;
  267. }
  268. expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
  269. {
  270. expr_t *e;
  271. e = xmalloc(sizeof(expr_t));
  272. e->type = type;
  273. e->ref = expr1;
  274. e->u.ext = expr2;
  275. e->is_const = FALSE;
  276. /* check for compile-time optimization */
  277. if (expr1->is_const && expr2->is_const)
  278. {
  279. e->is_const = TRUE;
  280. switch (type)
  281. {
  282. case EXPR_ADD:
  283. e->cval = expr1->cval + expr2->cval;
  284. break;
  285. case EXPR_SUB:
  286. e->cval = expr1->cval - expr2->cval;
  287. break;
  288. case EXPR_MOD:
  289. if (expr2->cval == 0)
  290. {
  291. error_loc("divide by zero in expression\n");
  292. e->cval = 0;
  293. }
  294. else
  295. e->cval = expr1->cval % expr2->cval;
  296. break;
  297. case EXPR_MUL:
  298. e->cval = expr1->cval * expr2->cval;
  299. break;
  300. case EXPR_DIV:
  301. if (expr2->cval == 0)
  302. {
  303. error_loc("divide by zero in expression\n");
  304. e->cval = 0;
  305. }
  306. else
  307. e->cval = expr1->cval / expr2->cval;
  308. break;
  309. case EXPR_OR:
  310. e->cval = expr1->cval | expr2->cval;
  311. break;
  312. case EXPR_AND:
  313. e->cval = expr1->cval & expr2->cval;
  314. break;
  315. case EXPR_SHL:
  316. e->cval = expr1->cval << expr2->cval;
  317. break;
  318. case EXPR_SHR:
  319. e->cval = expr1->cval >> expr2->cval;
  320. break;
  321. case EXPR_LOGOR:
  322. e->cval = expr1->cval || expr2->cval;
  323. break;
  324. case EXPR_LOGAND:
  325. e->cval = expr1->cval && expr2->cval;
  326. break;
  327. case EXPR_XOR:
  328. e->cval = expr1->cval ^ expr2->cval;
  329. break;
  330. case EXPR_EQUALITY:
  331. e->cval = expr1->cval == expr2->cval;
  332. break;
  333. case EXPR_INEQUALITY:
  334. e->cval = expr1->cval != expr2->cval;
  335. break;
  336. case EXPR_GTR:
  337. e->cval = expr1->cval > expr2->cval;
  338. break;
  339. case EXPR_LESS:
  340. e->cval = expr1->cval < expr2->cval;
  341. break;
  342. case EXPR_GTREQL:
  343. e->cval = expr1->cval >= expr2->cval;
  344. break;
  345. case EXPR_LESSEQL:
  346. e->cval = expr1->cval <= expr2->cval;
  347. break;
  348. default:
  349. e->is_const = FALSE;
  350. break;
  351. }
  352. }
  353. return e;
  354. }
  355. expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
  356. {
  357. expr_t *e;
  358. e = xmalloc(sizeof(expr_t));
  359. e->type = type;
  360. e->ref = expr1;
  361. e->u.ext = expr2;
  362. e->ext2 = expr3;
  363. e->is_const = FALSE;
  364. /* check for compile-time optimization */
  365. if (expr1->is_const && expr2->is_const && expr3->is_const)
  366. {
  367. e->is_const = TRUE;
  368. switch (type)
  369. {
  370. case EXPR_COND:
  371. e->cval = expr1->cval ? expr2->cval : expr3->cval;
  372. break;
  373. default:
  374. e->is_const = FALSE;
  375. break;
  376. }
  377. }
  378. return e;
  379. }
  380. struct expression_type
  381. {
  382. int is_variable; /* is the expression resolved to a variable? */
  383. int is_temporary; /* should the type be freed? */
  384. type_t *type;
  385. };
  386. static void check_scalar_type(const struct expr_loc *expr_loc,
  387. const type_t *cont_type, const type_t *type)
  388. {
  389. if (!cont_type || (!is_integer_type(type) && !is_ptr(type) &&
  390. !is_float_type(type)))
  391. error_loc_info(&expr_loc->v->loc_info, "scalar type required in expression%s%s\n",
  392. expr_loc->attr ? " for attribute " : "",
  393. expr_loc->attr ? expr_loc->attr : "");
  394. }
  395. static void check_arithmetic_type(const struct expr_loc *expr_loc,
  396. const type_t *cont_type, const type_t *type)
  397. {
  398. if (!cont_type || (!is_integer_type(type) && !is_float_type(type)))
  399. error_loc_info(&expr_loc->v->loc_info, "arithmetic type required in expression%s%s\n",
  400. expr_loc->attr ? " for attribute " : "",
  401. expr_loc->attr ? expr_loc->attr : "");
  402. }
  403. static void check_integer_type(const struct expr_loc *expr_loc,
  404. const type_t *cont_type, const type_t *type)
  405. {
  406. if (!cont_type || !is_integer_type(type))
  407. error_loc_info(&expr_loc->v->loc_info, "integer type required in expression%s%s\n",
  408. expr_loc->attr ? " for attribute " : "",
  409. expr_loc->attr ? expr_loc->attr : "");
  410. }
  411. static type_t *find_identifier(const char *identifier, const type_t *cont_type, int *found_in_cont_type)
  412. {
  413. type_t *type = NULL;
  414. const var_t *field;
  415. const var_list_t *fields = NULL;
  416. *found_in_cont_type = 0;
  417. if (cont_type)
  418. {
  419. switch (type_get_type(cont_type))
  420. {
  421. case TYPE_FUNCTION:
  422. fields = type_function_get_args(cont_type);
  423. break;
  424. case TYPE_STRUCT:
  425. fields = type_struct_get_fields(cont_type);
  426. break;
  427. case TYPE_UNION:
  428. case TYPE_ENCAPSULATED_UNION:
  429. fields = type_union_get_cases(cont_type);
  430. break;
  431. case TYPE_VOID:
  432. case TYPE_BASIC:
  433. case TYPE_ENUM:
  434. case TYPE_MODULE:
  435. case TYPE_COCLASS:
  436. case TYPE_INTERFACE:
  437. case TYPE_POINTER:
  438. case TYPE_ARRAY:
  439. case TYPE_BITFIELD:
  440. case TYPE_APICONTRACT:
  441. case TYPE_RUNTIMECLASS:
  442. case TYPE_PARAMETERIZED_TYPE:
  443. case TYPE_PARAMETER:
  444. case TYPE_DELEGATE:
  445. /* nothing to do */
  446. break;
  447. case TYPE_ALIAS:
  448. /* shouldn't get here because of using type_get_type above */
  449. assert(0);
  450. break;
  451. }
  452. }
  453. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  454. if (field->name && !strcmp(identifier, field->name))
  455. {
  456. type = field->declspec.type;
  457. *found_in_cont_type = 1;
  458. break;
  459. }
  460. if (!type)
  461. {
  462. var_t *const_var = find_const(identifier, 0);
  463. if (const_var) type = const_var->declspec.type;
  464. }
  465. return type;
  466. }
  467. static int is_valid_member_operand(const type_t *type)
  468. {
  469. switch (type_get_type(type))
  470. {
  471. case TYPE_STRUCT:
  472. case TYPE_UNION:
  473. case TYPE_ENUM:
  474. return TRUE;
  475. default:
  476. return FALSE;
  477. }
  478. }
  479. static struct expression_type resolve_expression(const struct expr_loc *expr_loc,
  480. const type_t *cont_type,
  481. const expr_t *e)
  482. {
  483. struct expression_type result;
  484. result.is_variable = FALSE;
  485. result.is_temporary = FALSE;
  486. result.type = NULL;
  487. switch (e->type)
  488. {
  489. case EXPR_VOID:
  490. break;
  491. case EXPR_HEXNUM:
  492. case EXPR_NUM:
  493. case EXPR_TRUEFALSE:
  494. result.is_temporary = FALSE;
  495. result.type = type_new_int(TYPE_BASIC_INT, 0);
  496. break;
  497. case EXPR_STRLIT:
  498. result.is_temporary = TRUE;
  499. result.type = type_new_pointer(type_new_int(TYPE_BASIC_CHAR, 0));
  500. break;
  501. case EXPR_WSTRLIT:
  502. result.is_temporary = TRUE;
  503. result.type = type_new_pointer(type_new_int(TYPE_BASIC_WCHAR, 0));
  504. break;
  505. case EXPR_CHARCONST:
  506. result.is_temporary = TRUE;
  507. result.type = type_new_int(TYPE_BASIC_CHAR, 0);
  508. break;
  509. case EXPR_DOUBLE:
  510. result.is_temporary = TRUE;
  511. result.type = type_new_basic(TYPE_BASIC_DOUBLE);
  512. break;
  513. case EXPR_IDENTIFIER:
  514. {
  515. int found_in_cont_type;
  516. result.is_variable = TRUE;
  517. result.is_temporary = FALSE;
  518. result.type = find_identifier(e->u.sval, cont_type, &found_in_cont_type);
  519. if (!result.type)
  520. {
  521. error_loc_info(&expr_loc->v->loc_info, "identifier %s cannot be resolved in expression%s%s\n",
  522. e->u.sval, expr_loc->attr ? " for attribute " : "",
  523. expr_loc->attr ? expr_loc->attr : "");
  524. }
  525. break;
  526. }
  527. case EXPR_LOGNOT:
  528. result = resolve_expression(expr_loc, cont_type, e->ref);
  529. check_scalar_type(expr_loc, cont_type, result.type);
  530. result.is_variable = FALSE;
  531. result.is_temporary = FALSE;
  532. result.type = type_new_int(TYPE_BASIC_INT, 0);
  533. break;
  534. case EXPR_NOT:
  535. result = resolve_expression(expr_loc, cont_type, e->ref);
  536. check_integer_type(expr_loc, cont_type, result.type);
  537. result.is_variable = FALSE;
  538. break;
  539. case EXPR_POS:
  540. case EXPR_NEG:
  541. result = resolve_expression(expr_loc, cont_type, e->ref);
  542. check_arithmetic_type(expr_loc, cont_type, result.type);
  543. result.is_variable = FALSE;
  544. break;
  545. case EXPR_ADDRESSOF:
  546. result = resolve_expression(expr_loc, cont_type, e->ref);
  547. if (!result.is_variable)
  548. error_loc_info(&expr_loc->v->loc_info, "address-of operator applied to non-variable type in expression%s%s\n",
  549. expr_loc->attr ? " for attribute " : "",
  550. expr_loc->attr ? expr_loc->attr : "");
  551. result.is_variable = FALSE;
  552. result.is_temporary = TRUE;
  553. result.type = type_new_pointer(result.type);
  554. break;
  555. case EXPR_PPTR:
  556. result = resolve_expression(expr_loc, cont_type, e->ref);
  557. if (result.type && is_ptr(result.type))
  558. result.type = type_pointer_get_ref_type(result.type);
  559. else if(result.type && is_array(result.type)
  560. && type_array_is_decl_as_ptr(result.type))
  561. result.type = type_array_get_element_type(result.type);
  562. else
  563. error_loc_info(&expr_loc->v->loc_info, "dereference operator applied to non-pointer type in expression%s%s\n",
  564. expr_loc->attr ? " for attribute " : "",
  565. expr_loc->attr ? expr_loc->attr : "");
  566. break;
  567. case EXPR_CAST:
  568. result = resolve_expression(expr_loc, cont_type, e->ref);
  569. result.type = e->u.tref.type;
  570. break;
  571. case EXPR_SIZEOF:
  572. result.is_temporary = FALSE;
  573. result.type = type_new_int(TYPE_BASIC_INT, 0);
  574. break;
  575. case EXPR_SHL:
  576. case EXPR_SHR:
  577. case EXPR_MOD:
  578. case EXPR_MUL:
  579. case EXPR_DIV:
  580. case EXPR_ADD:
  581. case EXPR_SUB:
  582. case EXPR_AND:
  583. case EXPR_OR:
  584. case EXPR_XOR:
  585. {
  586. struct expression_type result_right;
  587. result = resolve_expression(expr_loc, cont_type, e->ref);
  588. result.is_variable = FALSE;
  589. result_right = resolve_expression(expr_loc, cont_type, e->u.ext);
  590. /* FIXME: these checks aren't strict enough for some of the operators */
  591. check_scalar_type(expr_loc, cont_type, result.type);
  592. check_scalar_type(expr_loc, cont_type, result_right.type);
  593. break;
  594. }
  595. case EXPR_LOGOR:
  596. case EXPR_LOGAND:
  597. case EXPR_EQUALITY:
  598. case EXPR_INEQUALITY:
  599. case EXPR_GTR:
  600. case EXPR_LESS:
  601. case EXPR_GTREQL:
  602. case EXPR_LESSEQL:
  603. {
  604. struct expression_type result_left, result_right;
  605. result_left = resolve_expression(expr_loc, cont_type, e->ref);
  606. result_right = resolve_expression(expr_loc, cont_type, e->u.ext);
  607. check_scalar_type(expr_loc, cont_type, result_left.type);
  608. check_scalar_type(expr_loc, cont_type, result_right.type);
  609. result.is_temporary = FALSE;
  610. result.type = type_new_int(TYPE_BASIC_INT, 0);
  611. break;
  612. }
  613. case EXPR_MEMBER:
  614. result = resolve_expression(expr_loc, cont_type, e->ref);
  615. if (result.type && is_valid_member_operand(result.type))
  616. result = resolve_expression(expr_loc, result.type, e->u.ext);
  617. else
  618. error_loc_info(&expr_loc->v->loc_info, "'.' or '->' operator applied to a type that isn't a structure, union or enumeration in expression%s%s\n",
  619. expr_loc->attr ? " for attribute " : "",
  620. expr_loc->attr ? expr_loc->attr : "");
  621. break;
  622. case EXPR_COND:
  623. {
  624. struct expression_type result_first, result_second, result_third;
  625. result_first = resolve_expression(expr_loc, cont_type, e->ref);
  626. check_scalar_type(expr_loc, cont_type, result_first.type);
  627. result_second = resolve_expression(expr_loc, cont_type, e->u.ext);
  628. result_third = resolve_expression(expr_loc, cont_type, e->ext2);
  629. check_scalar_type(expr_loc, cont_type, result_second.type);
  630. check_scalar_type(expr_loc, cont_type, result_third.type);
  631. if (!is_ptr(result_second.type) ^ !is_ptr(result_third.type))
  632. error_loc_info(&expr_loc->v->loc_info, "type mismatch in ?: expression\n" );
  633. /* FIXME: determine the correct return type */
  634. result = result_second;
  635. result.is_variable = FALSE;
  636. break;
  637. }
  638. case EXPR_ARRAY:
  639. result = resolve_expression(expr_loc, cont_type, e->ref);
  640. if (result.type && is_array(result.type))
  641. {
  642. struct expression_type index_result;
  643. result.type = type_array_get_element_type(result.type);
  644. index_result = resolve_expression(expr_loc, cont_type /* FIXME */, e->u.ext);
  645. if (!index_result.type || !is_integer_type(index_result.type))
  646. error_loc_info(&expr_loc->v->loc_info, "array subscript not of integral type in expression%s%s\n",
  647. expr_loc->attr ? " for attribute " : "",
  648. expr_loc->attr ? expr_loc->attr : "");
  649. }
  650. else
  651. error_loc_info(&expr_loc->v->loc_info, "array subscript operator applied to non-array type in expression%s%s\n",
  652. expr_loc->attr ? " for attribute " : "",
  653. expr_loc->attr ? expr_loc->attr : "");
  654. break;
  655. }
  656. return result;
  657. }
  658. const type_t *expr_resolve_type(const struct expr_loc *expr_loc, const type_t *cont_type, const expr_t *expr)
  659. {
  660. struct expression_type expr_type;
  661. expr_type = resolve_expression(expr_loc, cont_type, expr);
  662. return expr_type.type;
  663. }
  664. void write_expr(FILE *h, const expr_t *e, int brackets,
  665. int toplevel, const char *toplevel_prefix,
  666. const type_t *cont_type, const char *local_var_prefix)
  667. {
  668. switch (e->type)
  669. {
  670. case EXPR_VOID:
  671. break;
  672. case EXPR_NUM:
  673. fprintf(h, "%u", e->u.lval);
  674. break;
  675. case EXPR_HEXNUM:
  676. fprintf(h, "0x%x", e->u.lval);
  677. break;
  678. case EXPR_DOUBLE:
  679. fprintf(h, "%#.15g", e->u.dval);
  680. break;
  681. case EXPR_TRUEFALSE:
  682. if (e->u.lval == 0)
  683. fprintf(h, "FALSE");
  684. else
  685. fprintf(h, "TRUE");
  686. break;
  687. case EXPR_IDENTIFIER:
  688. if (toplevel && toplevel_prefix && cont_type)
  689. {
  690. int found_in_cont_type;
  691. find_identifier(e->u.sval, cont_type, &found_in_cont_type);
  692. if (found_in_cont_type)
  693. {
  694. fprintf(h, "%s%s", toplevel_prefix, e->u.sval);
  695. break;
  696. }
  697. }
  698. fprintf(h, "%s%s", local_var_prefix, e->u.sval);
  699. break;
  700. case EXPR_STRLIT:
  701. fprintf(h, "\"%s\"", e->u.sval);
  702. break;
  703. case EXPR_WSTRLIT:
  704. fprintf(h, "L\"%s\"", e->u.sval);
  705. break;
  706. case EXPR_CHARCONST:
  707. fprintf(h, "'%s'", e->u.sval);
  708. break;
  709. case EXPR_LOGNOT:
  710. fprintf(h, "!");
  711. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  712. break;
  713. case EXPR_NOT:
  714. fprintf(h, "~");
  715. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  716. break;
  717. case EXPR_POS:
  718. fprintf(h, "+");
  719. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  720. break;
  721. case EXPR_NEG:
  722. fprintf(h, "-");
  723. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  724. break;
  725. case EXPR_ADDRESSOF:
  726. fprintf(h, "&");
  727. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  728. break;
  729. case EXPR_PPTR:
  730. fprintf(h, "*");
  731. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  732. break;
  733. case EXPR_CAST:
  734. fprintf(h, "(");
  735. write_type_decl(h, &e->u.tref, NULL);
  736. fprintf(h, ")");
  737. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  738. break;
  739. case EXPR_SIZEOF:
  740. fprintf(h, "sizeof(");
  741. write_type_decl(h, &e->u.tref, NULL);
  742. fprintf(h, ")");
  743. break;
  744. case EXPR_SHL:
  745. case EXPR_SHR:
  746. case EXPR_MOD:
  747. case EXPR_MUL:
  748. case EXPR_DIV:
  749. case EXPR_ADD:
  750. case EXPR_SUB:
  751. case EXPR_AND:
  752. case EXPR_OR:
  753. case EXPR_LOGOR:
  754. case EXPR_LOGAND:
  755. case EXPR_XOR:
  756. case EXPR_EQUALITY:
  757. case EXPR_INEQUALITY:
  758. case EXPR_GTR:
  759. case EXPR_LESS:
  760. case EXPR_GTREQL:
  761. case EXPR_LESSEQL:
  762. if (brackets) fprintf(h, "(");
  763. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  764. switch (e->type)
  765. {
  766. case EXPR_SHL: fprintf(h, " << "); break;
  767. case EXPR_SHR: fprintf(h, " >> "); break;
  768. case EXPR_MOD: fprintf(h, " %% "); break;
  769. case EXPR_MUL: fprintf(h, " * "); break;
  770. case EXPR_DIV: fprintf(h, " / "); break;
  771. case EXPR_ADD: fprintf(h, " + "); break;
  772. case EXPR_SUB: fprintf(h, " - "); break;
  773. case EXPR_AND: fprintf(h, " & "); break;
  774. case EXPR_OR: fprintf(h, " | "); break;
  775. case EXPR_LOGOR: fprintf(h, " || "); break;
  776. case EXPR_LOGAND: fprintf(h, " && "); break;
  777. case EXPR_XOR: fprintf(h, " ^ "); break;
  778. case EXPR_EQUALITY: fprintf(h, " == "); break;
  779. case EXPR_INEQUALITY: fprintf(h, " != "); break;
  780. case EXPR_GTR: fprintf(h, " > "); break;
  781. case EXPR_LESS: fprintf(h, " < "); break;
  782. case EXPR_GTREQL: fprintf(h, " >= "); break;
  783. case EXPR_LESSEQL: fprintf(h, " <= "); break;
  784. default: break;
  785. }
  786. write_expr(h, e->u.ext, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  787. if (brackets) fprintf(h, ")");
  788. break;
  789. case EXPR_MEMBER:
  790. if (brackets) fprintf(h, "(");
  791. if (e->ref->type == EXPR_PPTR)
  792. {
  793. write_expr(h, e->ref->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  794. fprintf(h, "->");
  795. }
  796. else
  797. {
  798. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  799. fprintf(h, ".");
  800. }
  801. write_expr(h, e->u.ext, 1, 0, toplevel_prefix, cont_type, "");
  802. if (brackets) fprintf(h, ")");
  803. break;
  804. case EXPR_COND:
  805. if (brackets) fprintf(h, "(");
  806. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  807. fprintf(h, " ? ");
  808. write_expr(h, e->u.ext, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  809. fprintf(h, " : ");
  810. write_expr(h, e->ext2, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  811. if (brackets) fprintf(h, ")");
  812. break;
  813. case EXPR_ARRAY:
  814. if (brackets) fprintf(h, "(");
  815. write_expr(h, e->ref, 1, toplevel, toplevel_prefix, cont_type, local_var_prefix);
  816. fprintf(h, "[");
  817. write_expr(h, e->u.ext, 1, 1, toplevel_prefix, cont_type, local_var_prefix);
  818. fprintf(h, "]");
  819. if (brackets) fprintf(h, ")");
  820. break;
  821. }
  822. }
  823. /* This is actually fairly involved to implement precisely, due to the
  824. effects attributes may have and things like that. Right now this is
  825. only used for optimization, so just check for a very small set of
  826. criteria that guarantee the types are equivalent; assume every thing
  827. else is different. */
  828. static int compare_type(const type_t *a, const type_t *b)
  829. {
  830. if (a == b
  831. || (a->name
  832. && b->name
  833. && strcmp(a->name, b->name) == 0))
  834. return 0;
  835. /* Ordering doesn't need to be implemented yet. */
  836. return 1;
  837. }
  838. int compare_expr(const expr_t *a, const expr_t *b)
  839. {
  840. int ret;
  841. if (a->type != b->type)
  842. return a->type - b->type;
  843. switch (a->type)
  844. {
  845. case EXPR_NUM:
  846. case EXPR_HEXNUM:
  847. case EXPR_TRUEFALSE:
  848. return a->u.lval - b->u.lval;
  849. case EXPR_DOUBLE:
  850. return a->u.dval - b->u.dval;
  851. case EXPR_IDENTIFIER:
  852. case EXPR_STRLIT:
  853. case EXPR_WSTRLIT:
  854. case EXPR_CHARCONST:
  855. return strcmp(a->u.sval, b->u.sval);
  856. case EXPR_COND:
  857. ret = compare_expr(a->ref, b->ref);
  858. if (ret != 0)
  859. return ret;
  860. ret = compare_expr(a->u.ext, b->u.ext);
  861. if (ret != 0)
  862. return ret;
  863. return compare_expr(a->ext2, b->ext2);
  864. case EXPR_OR:
  865. case EXPR_AND:
  866. case EXPR_ADD:
  867. case EXPR_SUB:
  868. case EXPR_MOD:
  869. case EXPR_MUL:
  870. case EXPR_DIV:
  871. case EXPR_SHL:
  872. case EXPR_SHR:
  873. case EXPR_MEMBER:
  874. case EXPR_ARRAY:
  875. case EXPR_LOGOR:
  876. case EXPR_LOGAND:
  877. case EXPR_XOR:
  878. case EXPR_EQUALITY:
  879. case EXPR_INEQUALITY:
  880. case EXPR_GTR:
  881. case EXPR_LESS:
  882. case EXPR_GTREQL:
  883. case EXPR_LESSEQL:
  884. ret = compare_expr(a->ref, b->ref);
  885. if (ret != 0)
  886. return ret;
  887. return compare_expr(a->u.ext, b->u.ext);
  888. case EXPR_CAST:
  889. ret = compare_type(a->u.tref.type, b->u.tref.type);
  890. if (ret != 0)
  891. return ret;
  892. /* Fall through. */
  893. case EXPR_NOT:
  894. case EXPR_NEG:
  895. case EXPR_PPTR:
  896. case EXPR_ADDRESSOF:
  897. case EXPR_LOGNOT:
  898. case EXPR_POS:
  899. return compare_expr(a->ref, b->ref);
  900. case EXPR_SIZEOF:
  901. return compare_type(a->u.tref.type, b->u.tref.type);
  902. case EXPR_VOID:
  903. return 0;
  904. }
  905. return -1;
  906. }