expression.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * sparse/expression.c
  3. *
  4. * Copyright (C) 2003 Transmeta Corp.
  5. * 2003-2004 Linus Torvalds
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * This is the expression parsing part of parsing C.
  26. */
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <errno.h>
  35. #include <limits.h>
  36. #include "lib.h"
  37. #include "allocate.h"
  38. #include "token.h"
  39. #include "parse.h"
  40. #include "symbol.h"
  41. #include "scope.h"
  42. #include "expression.h"
  43. #include "target.h"
  44. #include "char.h"
  45. ALLOCATOR(type_expression, "type-expr-maps");
  46. static int match_oplist(int op, ...)
  47. {
  48. va_list args;
  49. int nextop;
  50. va_start(args, op);
  51. do {
  52. nextop = va_arg(args, int);
  53. } while (nextop != 0 && nextop != op);
  54. va_end(args);
  55. return nextop != 0;
  56. }
  57. static struct token *comma_expression(struct token *, struct expression **);
  58. struct token *parens_expression(struct token *token, struct expression **expr, const char *where)
  59. {
  60. struct token *p;
  61. token = expect(token, '(', where);
  62. p = token;
  63. if (match_op(token, '{')) {
  64. struct expression *e = alloc_expression(token->pos, EXPR_STATEMENT);
  65. struct statement *stmt = alloc_statement(token->pos, STMT_COMPOUND);
  66. *expr = e;
  67. e->statement = stmt;
  68. start_label_scope();
  69. token = compound_statement(token->next, stmt);
  70. end_label_scope();
  71. token = expect(token, '}', "at end of statement expression");
  72. } else
  73. token = parse_expression(token, expr);
  74. if (token == p)
  75. sparse_error(token->pos, "an expression is expected before ')'");
  76. return expect(token, ')', where);
  77. }
  78. struct token *string_expression(struct token *token, struct expression **expr, const char *where)
  79. {
  80. struct token *next = primary_expression(token, expr);
  81. if (!*expr || (*expr)->type != EXPR_STRING) {
  82. sparse_error(token->pos, "string literal expected for %s", where);
  83. *expr = NULL;
  84. }
  85. return next;
  86. }
  87. /*
  88. * Handle __func__, __FUNCTION__ and __PRETTY_FUNCTION__ token
  89. * conversion
  90. */
  91. static struct symbol *handle_func(struct token *token)
  92. {
  93. struct ident *ident = token->ident;
  94. struct symbol *decl, *array;
  95. struct string *string;
  96. int len;
  97. if (ident != &__func___ident &&
  98. ident != &__FUNCTION___ident &&
  99. ident != &__PRETTY_FUNCTION___ident)
  100. return NULL;
  101. if (!current_fn || !current_fn->ident)
  102. return NULL;
  103. /* OK, it's one of ours */
  104. array = alloc_symbol(token->pos, SYM_ARRAY);
  105. array->ctype.base_type = &char_ctype;
  106. array->ctype.alignment = 1;
  107. array->endpos = token->pos;
  108. decl = alloc_symbol(token->pos, SYM_NODE);
  109. decl->ctype.base_type = array;
  110. decl->ctype.alignment = 1;
  111. decl->ctype.modifiers = MOD_STATIC;
  112. decl->endpos = token->pos;
  113. /* NS_SYMBOL but in function-scope */
  114. bind_symbol_with_scope(decl, ident, NS_SYMBOL, function_scope);
  115. len = current_fn->ident->len;
  116. string = __alloc_string(len + 1);
  117. memcpy(string->data, current_fn->ident->name, len);
  118. string->data[len] = 0;
  119. string->length = len + 1;
  120. decl->initializer = alloc_expression(token->pos, EXPR_STRING);
  121. decl->initializer->string = string;
  122. decl->initializer->ctype = decl;
  123. decl->array_size = alloc_const_expression(token->pos, len + 1);
  124. array->array_size = decl->array_size;
  125. decl->bit_size = array->bit_size = bytes_to_bits(len + 1);
  126. return decl;
  127. }
  128. static struct token *parse_type(struct token *token, struct expression **tree)
  129. {
  130. struct symbol *sym;
  131. *tree = alloc_expression(token->pos, EXPR_TYPE);
  132. token = typename(token, &sym, NULL);
  133. if (sym->ident)
  134. sparse_error(token->pos,
  135. "type expression should not include identifier "
  136. "\"%s\"", sym->ident->name);
  137. (*tree)->symbol = sym;
  138. return token;
  139. }
  140. static struct token *builtin_types_compatible_p_expr(struct token *token,
  141. struct expression **tree)
  142. {
  143. struct expression *expr = alloc_expression(
  144. token->pos, EXPR_COMPARE);
  145. expr->op = SPECIAL_EQUAL;
  146. token = token->next;
  147. if (!match_op(token, '('))
  148. return expect(token, '(',
  149. "after __builtin_types_compatible_p");
  150. token = token->next;
  151. token = parse_type(token, &expr->left);
  152. if (!match_op(token, ','))
  153. return expect(token, ',',
  154. "in __builtin_types_compatible_p");
  155. token = token->next;
  156. token = parse_type(token, &expr->right);
  157. if (!match_op(token, ')'))
  158. return expect(token, ')',
  159. "at end of __builtin_types_compatible_p");
  160. token = token->next;
  161. *tree = expr;
  162. return token;
  163. }
  164. static struct token *builtin_offsetof_expr(struct token *token,
  165. struct expression **tree)
  166. {
  167. struct expression *expr = NULL;
  168. struct expression **p = &expr;
  169. struct symbol *sym;
  170. int op = '.';
  171. token = token->next;
  172. if (!match_op(token, '('))
  173. return expect(token, '(', "after __builtin_offset");
  174. token = token->next;
  175. token = typename(token, &sym, NULL);
  176. if (sym->ident)
  177. sparse_error(token->pos,
  178. "type expression should not include identifier "
  179. "\"%s\"", sym->ident->name);
  180. if (!match_op(token, ','))
  181. return expect(token, ',', "in __builtin_offset");
  182. while (1) {
  183. struct expression *e;
  184. switch (op) {
  185. case ')':
  186. expr->in = sym;
  187. *tree = expr;
  188. default:
  189. return expect(token, ')', "at end of __builtin_offset");
  190. case SPECIAL_DEREFERENCE:
  191. e = alloc_expression(token->pos, EXPR_OFFSETOF);
  192. e->op = '[';
  193. *p = e;
  194. p = &e->down;
  195. /* fall through */
  196. case '.':
  197. token = token->next;
  198. e = alloc_expression(token->pos, EXPR_OFFSETOF);
  199. e->op = '.';
  200. if (token_type(token) != TOKEN_IDENT) {
  201. sparse_error(token->pos, "Expected member name");
  202. return token;
  203. }
  204. e->ident = token->ident;
  205. token = token->next;
  206. break;
  207. case '[':
  208. token = token->next;
  209. e = alloc_expression(token->pos, EXPR_OFFSETOF);
  210. e->op = '[';
  211. token = parse_expression(token, &e->index);
  212. token = expect(token, ']',
  213. "at end of array dereference");
  214. if (!e->index)
  215. return token;
  216. }
  217. *p = e;
  218. p = &e->down;
  219. op = token_type(token) == TOKEN_SPECIAL ? token->special : 0;
  220. }
  221. }
  222. #ifndef ULLONG_MAX
  223. #define ULLONG_MAX (~0ULL)
  224. #endif
  225. static unsigned long long parse_num(const char *nptr, char **end)
  226. {
  227. if (nptr[0] == '0' && tolower((unsigned char)nptr[1]) == 'b')
  228. return strtoull(&nptr[2], end, 2);
  229. return strtoull(nptr, end, 0);
  230. }
  231. static void get_number_value(struct expression *expr, struct token *token)
  232. {
  233. const char *str = token->number;
  234. unsigned long long value;
  235. char *end;
  236. int size = 0, want_unsigned = 0;
  237. int overflow = 0, do_warn = 0;
  238. int try_unsigned = 1;
  239. int bits;
  240. errno = 0;
  241. value = parse_num(str, &end);
  242. if (end == str)
  243. goto Float;
  244. if (value == ULLONG_MAX && errno == ERANGE)
  245. overflow = 1;
  246. while (1) {
  247. char c = *end++;
  248. if (!c) {
  249. break;
  250. } else if (c == 'u' || c == 'U') {
  251. if (want_unsigned)
  252. goto Enoint;
  253. want_unsigned = 1;
  254. } else if (c == 'l' || c == 'L') {
  255. if (size)
  256. goto Enoint;
  257. size = 1;
  258. if (*end == c) {
  259. size = 2;
  260. end++;
  261. }
  262. } else
  263. goto Float;
  264. }
  265. if (overflow)
  266. goto Eoverflow;
  267. /* OK, it's a valid integer */
  268. /* decimals can be unsigned only if directly specified as such */
  269. if (str[0] != '0' && !want_unsigned)
  270. try_unsigned = 0;
  271. if (!size) {
  272. bits = bits_in_int - 1;
  273. if (!(value & (~1ULL << bits))) {
  274. if (!(value & (1ULL << bits))) {
  275. goto got_it;
  276. } else if (try_unsigned) {
  277. want_unsigned = 1;
  278. goto got_it;
  279. }
  280. }
  281. size = 1;
  282. do_warn = 1;
  283. }
  284. if (size < 2) {
  285. bits = bits_in_long - 1;
  286. if (!(value & (~1ULL << bits))) {
  287. if (!(value & (1ULL << bits))) {
  288. goto got_it;
  289. } else if (try_unsigned) {
  290. want_unsigned = 1;
  291. goto got_it;
  292. }
  293. do_warn |= 2;
  294. }
  295. size = 2;
  296. do_warn |= 1;
  297. }
  298. bits = bits_in_longlong - 1;
  299. if (value & (~1ULL << bits))
  300. goto Eoverflow;
  301. if (!(value & (1ULL << bits)))
  302. goto got_it;
  303. if (!try_unsigned)
  304. warning(expr->pos, "decimal constant %s is too big for long long",
  305. show_token(token));
  306. want_unsigned = 1;
  307. got_it:
  308. if (do_warn && Wconstant_suffix)
  309. warning(expr->pos, "constant %s is so big it is%s%s%s",
  310. show_token(token),
  311. want_unsigned ? " unsigned":"",
  312. size > 0 ? " long":"",
  313. size > 1 ? " long":"");
  314. if (do_warn & 2)
  315. warning(expr->pos,
  316. "decimal constant %s is between LONG_MAX and ULONG_MAX."
  317. " For C99 that means long long, C90 compilers are very "
  318. "likely to produce unsigned long (and a warning) here",
  319. show_token(token));
  320. expr->type = EXPR_VALUE;
  321. expr->flags = CEF_SET_INT;
  322. expr->ctype = ctype_integer(size, want_unsigned);
  323. expr->value = value;
  324. return;
  325. Eoverflow:
  326. error_die(expr->pos, "constant %s is too big even for unsigned long long",
  327. show_token(token));
  328. return;
  329. Float:
  330. expr->fvalue = string_to_ld(str, &end);
  331. if (str == end)
  332. goto Enoint;
  333. if (*end && end[1])
  334. goto Enoint;
  335. if (*end == 'f' || *end == 'F')
  336. expr->ctype = &float_ctype;
  337. else if (*end == 'l' || *end == 'L')
  338. expr->ctype = &ldouble_ctype;
  339. else if (!*end)
  340. expr->ctype = &double_ctype;
  341. else
  342. goto Enoint;
  343. expr->flags = CEF_SET_FLOAT;
  344. expr->type = EXPR_FVALUE;
  345. return;
  346. Enoint:
  347. sparse_error(expr->pos, "constant %s is not a valid number", show_token(token));
  348. expr->type = EXPR_VALUE;
  349. expr->value = 0;
  350. expr->ctype = &int_ctype;
  351. }
  352. static struct token *generic_selection(struct token *token, struct expression **tree)
  353. {
  354. struct expression *expr = alloc_expression(token->pos, EXPR_GENERIC);
  355. struct type_expression **last = &expr->map;
  356. token = expect(token, '(', "after '_Generic'");
  357. token = assignment_expression(token, &expr->control);
  358. if (!match_op(token, ',')) {
  359. goto end;
  360. }
  361. while (match_op(token, ',')) {
  362. token = token->next;
  363. if (lookup_type(token)) {
  364. struct type_expression *map = __alloc_type_expression(0);
  365. token = typename(token, &map->type, NULL);
  366. token = expect(token, ':', "after typename");
  367. token = assignment_expression(token, &map->expr);
  368. *last = map;
  369. last = &map->next;
  370. } else if (match_ident(token, &default_ident)) {
  371. if (expr->def) {
  372. warning(token->pos, "multiple default in generic expression");
  373. info(expr->def->pos, "note: previous was here");
  374. }
  375. token = token->next;
  376. token = expect(token, ':', "after typename");
  377. token = assignment_expression(token, &expr->def);
  378. }
  379. }
  380. end:
  381. *tree = expr;
  382. return expect(token, ')', "after expression");
  383. }
  384. struct token *primary_expression(struct token *token, struct expression **tree)
  385. {
  386. struct expression *expr = NULL;
  387. switch (token_type(token)) {
  388. case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3:
  389. expr = alloc_expression(token->pos, EXPR_VALUE);
  390. expr->flags = CEF_SET_CHAR;
  391. expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype;
  392. get_char_constant(token, &expr->value);
  393. token = token->next;
  394. break;
  395. case TOKEN_NUMBER:
  396. expr = alloc_expression(token->pos, EXPR_VALUE);
  397. get_number_value(expr, token); /* will see if it's an integer */
  398. token = token->next;
  399. break;
  400. case TOKEN_ZERO_IDENT: {
  401. expr = alloc_expression(token->pos, EXPR_SYMBOL);
  402. expr->flags = CEF_SET_INT;
  403. expr->ctype = &int_ctype;
  404. expr->symbol = &zero_int;
  405. expr->symbol_name = token->ident;
  406. token = token->next;
  407. break;
  408. }
  409. case TOKEN_IDENT: {
  410. struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
  411. struct token *next = token->next;
  412. if (!sym) {
  413. sym = handle_func(token);
  414. if (token->ident == &__builtin_types_compatible_p_ident) {
  415. token = builtin_types_compatible_p_expr(token, &expr);
  416. break;
  417. }
  418. if (token->ident == &__builtin_offsetof_ident) {
  419. token = builtin_offsetof_expr(token, &expr);
  420. break;
  421. }
  422. if (token->ident == &_Generic_ident) {
  423. token = generic_selection(token->next, &expr);
  424. break;
  425. }
  426. } else if (sym->enum_member) {
  427. expr = alloc_expression(token->pos, EXPR_VALUE);
  428. *expr = *sym->initializer;
  429. /* we want the right position reported, thus the copy */
  430. expr->pos = token->pos;
  431. expr->flags = CEF_SET_ENUM;
  432. token = next;
  433. break;
  434. }
  435. expr = alloc_expression(token->pos, EXPR_SYMBOL);
  436. /*
  437. * We support types as real first-class citizens, with type
  438. * comparisons etc:
  439. *
  440. * if (typeof(a) == int) ..
  441. */
  442. if (sym && sym->namespace == NS_TYPEDEF) {
  443. sparse_error(token->pos, "typename in expression");
  444. sym = NULL;
  445. }
  446. expr->symbol_name = token->ident;
  447. expr->symbol = sym;
  448. /*
  449. * A pointer to an lvalue designating a static storage
  450. * duration object is an address constant [6.6(9)].
  451. */
  452. if (sym && (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_STATIC)))
  453. expr->flags = CEF_ADDR;
  454. token = next;
  455. break;
  456. }
  457. case TOKEN_STRING:
  458. case TOKEN_WIDE_STRING:
  459. expr = alloc_expression(token->pos, EXPR_STRING);
  460. token = get_string_constant(token, expr);
  461. break;
  462. case TOKEN_SPECIAL:
  463. if (token->special == '(') {
  464. expr = alloc_expression(token->pos, EXPR_PREOP);
  465. expr->op = '(';
  466. token = parens_expression(token, &expr->unop, "in expression");
  467. break;
  468. }
  469. if (token->special == '[' && lookup_type(token->next)) {
  470. expr = alloc_expression(token->pos, EXPR_TYPE);
  471. token = typename(token->next, &expr->symbol, NULL);
  472. token = expect(token, ']', "in type expression");
  473. break;
  474. }
  475. default:
  476. ;
  477. }
  478. *tree = expr;
  479. return token;
  480. }
  481. static struct token *expression_list(struct token *token, struct expression_list **list)
  482. {
  483. while (!match_op(token, ')')) {
  484. struct expression *expr = NULL;
  485. token = assignment_expression(token, &expr);
  486. if (!expr)
  487. break;
  488. add_expression(list, expr);
  489. if (!match_op(token, ','))
  490. break;
  491. token = token->next;
  492. }
  493. return token;
  494. }
  495. /*
  496. * extend to deal with the ambiguous C grammar for parsing
  497. * a cast expressions followed by an initializer.
  498. */
  499. static struct token *postfix_expression(struct token *token, struct expression **tree, struct expression *cast_init_expr)
  500. {
  501. struct expression *expr = cast_init_expr;
  502. if (!expr)
  503. token = primary_expression(token, &expr);
  504. while (expr && token_type(token) == TOKEN_SPECIAL) {
  505. switch (token->special) {
  506. case '[': { /* Array dereference */
  507. struct expression *deref = alloc_expression(token->pos, EXPR_PREOP);
  508. struct expression *add = alloc_expression(token->pos, EXPR_BINOP);
  509. deref->op = '*';
  510. deref->unop = add;
  511. add->op = '+';
  512. add->left = expr;
  513. token = parse_expression(token->next, &add->right);
  514. token = expect(token, ']', "at end of array dereference");
  515. expr = deref;
  516. continue;
  517. }
  518. case SPECIAL_INCREMENT: /* Post-increment */
  519. case SPECIAL_DECREMENT: { /* Post-decrement */
  520. struct expression *post = alloc_expression(token->pos, EXPR_POSTOP);
  521. post->op = token->special;
  522. post->unop = expr;
  523. expr = post;
  524. token = token->next;
  525. continue;
  526. }
  527. case SPECIAL_DEREFERENCE: { /* Structure pointer member dereference */
  528. /* "x->y" is just shorthand for "(*x).y" */
  529. struct expression *inner = alloc_expression(token->pos, EXPR_PREOP);
  530. inner->op = '*';
  531. inner->unop = expr;
  532. expr = inner;
  533. }
  534. /* Fall through!! */
  535. case '.': { /* Structure member dereference */
  536. struct expression *deref = alloc_expression(token->pos, EXPR_DEREF);
  537. deref->op = '.';
  538. deref->deref = expr;
  539. token = token->next;
  540. if (token_type(token) != TOKEN_IDENT) {
  541. sparse_error(token->pos, "Expected member name");
  542. break;
  543. }
  544. deref->member = token->ident;
  545. token = token->next;
  546. expr = deref;
  547. continue;
  548. }
  549. case '(': { /* Function call */
  550. struct expression *call = alloc_expression(token->pos, EXPR_CALL);
  551. call->op = '(';
  552. call->fn = expr;
  553. token = expression_list(token->next, &call->args);
  554. token = expect(token, ')', "in function call");
  555. expr = call;
  556. continue;
  557. }
  558. default:
  559. break;
  560. }
  561. break;
  562. }
  563. *tree = expr;
  564. return token;
  565. }
  566. static struct token *cast_expression(struct token *token, struct expression **tree);
  567. static struct token *unary_expression(struct token *token, struct expression **tree);
  568. static struct token *type_info_expression(struct token *token,
  569. struct expression **tree, int type)
  570. {
  571. struct expression *expr = alloc_expression(token->pos, type);
  572. struct token *p;
  573. *tree = expr;
  574. expr->flags = CEF_SET_ICE; /* XXX: VLA support will need that changed */
  575. token = token->next;
  576. if (!match_op(token, '(') || !lookup_type(token->next))
  577. return unary_expression(token, &expr->cast_expression);
  578. p = token;
  579. token = typename(token->next, &expr->cast_type, NULL);
  580. if (!match_op(token, ')')) {
  581. static const char * error[] = {
  582. [EXPR_SIZEOF] = "at end of sizeof",
  583. [EXPR_ALIGNOF] = "at end of __alignof__",
  584. [EXPR_PTRSIZEOF] = "at end of __sizeof_ptr__"
  585. };
  586. return expect(token, ')', error[type]);
  587. }
  588. token = token->next;
  589. /*
  590. * C99 ambiguity: the typename might have been the beginning
  591. * of a typed initializer expression..
  592. */
  593. if (match_op(token, '{')) {
  594. struct expression *cast = alloc_expression(p->pos, EXPR_CAST);
  595. cast->cast_type = expr->cast_type;
  596. expr->cast_type = NULL;
  597. expr->cast_expression = cast;
  598. token = initializer(&cast->cast_expression, token);
  599. token = postfix_expression(token, &expr->cast_expression, cast);
  600. }
  601. return token;
  602. }
  603. static struct token *unary_expression(struct token *token, struct expression **tree)
  604. {
  605. if (token_type(token) == TOKEN_IDENT) {
  606. struct ident *ident = token->ident;
  607. if (ident->reserved) {
  608. static const struct {
  609. struct ident *id;
  610. int type;
  611. } type_information[] = {
  612. { &sizeof_ident, EXPR_SIZEOF },
  613. { &__alignof___ident, EXPR_ALIGNOF },
  614. { &__alignof_ident, EXPR_ALIGNOF },
  615. { &_Alignof_ident, EXPR_ALIGNOF },
  616. { &__sizeof_ptr___ident, EXPR_PTRSIZEOF },
  617. };
  618. int i;
  619. for (i = 0; i < ARRAY_SIZE(type_information); i++) {
  620. if (ident == type_information[i].id)
  621. return type_info_expression(token, tree, type_information[i].type);
  622. }
  623. }
  624. }
  625. if (token_type(token) == TOKEN_SPECIAL) {
  626. if (match_oplist(token->special,
  627. SPECIAL_INCREMENT, SPECIAL_DECREMENT,
  628. '&', '*', 0)) {
  629. struct expression *unop;
  630. struct expression *unary;
  631. struct token *next;
  632. next = cast_expression(token->next, &unop);
  633. if (!unop) {
  634. sparse_error(token->pos, "Syntax error in unary expression");
  635. *tree = NULL;
  636. return next;
  637. }
  638. unary = alloc_expression(token->pos, EXPR_PREOP);
  639. unary->op = token->special;
  640. unary->unop = unop;
  641. *tree = unary;
  642. return next;
  643. }
  644. /* possibly constant ones */
  645. if (match_oplist(token->special, '+', '-', '~', '!', 0)) {
  646. struct expression *unop;
  647. struct expression *unary;
  648. struct token *next;
  649. next = cast_expression(token->next, &unop);
  650. if (!unop) {
  651. sparse_error(token->pos, "Syntax error in unary expression");
  652. *tree = NULL;
  653. return next;
  654. }
  655. unary = alloc_expression(token->pos, EXPR_PREOP);
  656. unary->op = token->special;
  657. unary->unop = unop;
  658. *tree = unary;
  659. return next;
  660. }
  661. /* Gcc extension: &&label gives the address of a label */
  662. if (match_op(token, SPECIAL_LOGICAL_AND) &&
  663. token_type(token->next) == TOKEN_IDENT) {
  664. struct expression *label = alloc_expression(token->pos, EXPR_LABEL);
  665. struct symbol *sym = label_symbol(token->next, 1);
  666. if (!(sym->ctype.modifiers & MOD_ADDRESSABLE)) {
  667. sym->ctype.modifiers |= MOD_ADDRESSABLE;
  668. add_symbol(&function_computed_target_list, sym);
  669. }
  670. check_label_usage(sym, token->pos);
  671. label->flags = CEF_ADDR;
  672. label->label_symbol = sym;
  673. *tree = label;
  674. return token->next->next;
  675. }
  676. }
  677. return postfix_expression(token, tree, NULL);
  678. }
  679. /*
  680. * Ambiguity: a '(' can be either a cast-expression or
  681. * a primary-expression depending on whether it is followed
  682. * by a type or not.
  683. *
  684. * additional ambiguity: a "cast expression" followed by
  685. * an initializer is really a postfix-expression.
  686. */
  687. static struct token *cast_expression(struct token *token, struct expression **tree)
  688. {
  689. if (match_op(token, '(')) {
  690. struct token *next = token->next;
  691. if (lookup_type(next)) {
  692. struct expression *cast = alloc_expression(next->pos, EXPR_CAST);
  693. struct expression *v;
  694. struct symbol *sym;
  695. int is_force;
  696. token = typename(next, &sym, &is_force);
  697. cast->cast_type = sym;
  698. token = expect(token, ')', "at end of cast operator");
  699. if (match_op(token, '{')) {
  700. if (toplevel(block_scope))
  701. sym->ctype.modifiers |= MOD_TOPLEVEL;
  702. if (is_force)
  703. warning(sym->pos,
  704. "[force] in compound literal");
  705. token = initializer(&cast->cast_expression, token);
  706. return postfix_expression(token, tree, cast);
  707. }
  708. *tree = cast;
  709. if (is_force)
  710. cast->type = EXPR_FORCE_CAST;
  711. token = cast_expression(token, &v);
  712. if (!v)
  713. return token;
  714. cast->cast_expression = v;
  715. return token;
  716. }
  717. }
  718. return unary_expression(token, tree);
  719. }
  720. /*
  721. * Generic left-to-right binop parsing
  722. *
  723. * This _really_ needs to be inlined, because that makes the inner
  724. * function call statically deterministic rather than a totally
  725. * unpredictable indirect call. But gcc-3 is so "clever" that it
  726. * doesn't do so by default even when you tell it to inline it.
  727. *
  728. * Making it a macro avoids the inlining problem, and also means
  729. * that we can pass in the op-comparison as an expression rather
  730. * than create a data structure for it.
  731. */
  732. #define LR_BINOP_EXPRESSION(__token, tree, type, inner, compare) \
  733. struct expression *left = NULL; \
  734. struct token * next = inner(__token, &left); \
  735. \
  736. if (left) { \
  737. while (token_type(next) == TOKEN_SPECIAL) { \
  738. struct expression *top, *right = NULL; \
  739. int op = next->special; \
  740. \
  741. if (!(compare)) \
  742. goto out; \
  743. top = alloc_expression(next->pos, type); \
  744. next = inner(next->next, &right); \
  745. if (!right) { \
  746. sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \
  747. break; \
  748. } \
  749. top->op = op; \
  750. top->left = left; \
  751. top->right = right; \
  752. left = top; \
  753. } \
  754. } \
  755. out: \
  756. *tree = left; \
  757. return next; \
  758. static struct token *multiplicative_expression(struct token *token, struct expression **tree)
  759. {
  760. LR_BINOP_EXPRESSION(
  761. token, tree, EXPR_BINOP, cast_expression,
  762. (op == '*') || (op == '/') || (op == '%')
  763. );
  764. }
  765. static struct token *additive_expression(struct token *token, struct expression **tree)
  766. {
  767. LR_BINOP_EXPRESSION(
  768. token, tree, EXPR_BINOP, multiplicative_expression,
  769. (op == '+') || (op == '-')
  770. );
  771. }
  772. static struct token *shift_expression(struct token *token, struct expression **tree)
  773. {
  774. LR_BINOP_EXPRESSION(
  775. token, tree, EXPR_BINOP, additive_expression,
  776. (op == SPECIAL_LEFTSHIFT) || (op == SPECIAL_RIGHTSHIFT)
  777. );
  778. }
  779. static struct token *relational_expression(struct token *token, struct expression **tree)
  780. {
  781. LR_BINOP_EXPRESSION(
  782. token, tree, EXPR_COMPARE, shift_expression,
  783. (op == '<') || (op == '>') ||
  784. (op == SPECIAL_LTE) || (op == SPECIAL_GTE)
  785. );
  786. }
  787. static struct token *equality_expression(struct token *token, struct expression **tree)
  788. {
  789. LR_BINOP_EXPRESSION(
  790. token, tree, EXPR_COMPARE, relational_expression,
  791. (op == SPECIAL_EQUAL) || (op == SPECIAL_NOTEQUAL)
  792. );
  793. }
  794. static struct token *bitwise_and_expression(struct token *token, struct expression **tree)
  795. {
  796. LR_BINOP_EXPRESSION(
  797. token, tree, EXPR_BINOP, equality_expression,
  798. (op == '&')
  799. );
  800. }
  801. static struct token *bitwise_xor_expression(struct token *token, struct expression **tree)
  802. {
  803. LR_BINOP_EXPRESSION(
  804. token, tree, EXPR_BINOP, bitwise_and_expression,
  805. (op == '^')
  806. );
  807. }
  808. static struct token *bitwise_or_expression(struct token *token, struct expression **tree)
  809. {
  810. LR_BINOP_EXPRESSION(
  811. token, tree, EXPR_BINOP, bitwise_xor_expression,
  812. (op == '|')
  813. );
  814. }
  815. static struct token *logical_and_expression(struct token *token, struct expression **tree)
  816. {
  817. LR_BINOP_EXPRESSION(
  818. token, tree, EXPR_LOGICAL, bitwise_or_expression,
  819. (op == SPECIAL_LOGICAL_AND)
  820. );
  821. }
  822. static struct token *logical_or_expression(struct token *token, struct expression **tree)
  823. {
  824. LR_BINOP_EXPRESSION(
  825. token, tree, EXPR_LOGICAL, logical_and_expression,
  826. (op == SPECIAL_LOGICAL_OR)
  827. );
  828. }
  829. struct token *conditional_expression(struct token *token, struct expression **tree)
  830. {
  831. token = logical_or_expression(token, tree);
  832. if (*tree && match_op(token, '?')) {
  833. struct expression *expr = alloc_expression(token->pos, EXPR_CONDITIONAL);
  834. expr->op = token->special;
  835. expr->conditional = *tree;
  836. *tree = expr;
  837. token = parse_expression(token->next, &expr->cond_true);
  838. token = expect(token, ':', "in conditional expression");
  839. token = conditional_expression(token, &expr->cond_false);
  840. }
  841. return token;
  842. }
  843. struct token *assignment_expression(struct token *token, struct expression **tree)
  844. {
  845. token = conditional_expression(token, tree);
  846. if (*tree && token_type(token) == TOKEN_SPECIAL) {
  847. static const int assignments[] = {
  848. '=',
  849. SPECIAL_ADD_ASSIGN, SPECIAL_SUB_ASSIGN,
  850. SPECIAL_MUL_ASSIGN, SPECIAL_DIV_ASSIGN,
  851. SPECIAL_MOD_ASSIGN, SPECIAL_SHL_ASSIGN,
  852. SPECIAL_SHR_ASSIGN, SPECIAL_AND_ASSIGN,
  853. SPECIAL_OR_ASSIGN, SPECIAL_XOR_ASSIGN };
  854. int i, op = token->special;
  855. for (i = 0; i < ARRAY_SIZE(assignments); i++)
  856. if (assignments[i] == op) {
  857. struct expression * expr = alloc_expression(token->pos, EXPR_ASSIGNMENT);
  858. struct token *next = token->next;
  859. expr->left = *tree;
  860. expr->op = op;
  861. *tree = expr;
  862. token = assignment_expression(next, &expr->right);
  863. if (token == next)
  864. expression_error(expr, "expression expected before '%s'", show_token(token));
  865. return token;
  866. }
  867. }
  868. return token;
  869. }
  870. static struct token *comma_expression(struct token *token, struct expression **tree)
  871. {
  872. LR_BINOP_EXPRESSION(
  873. token, tree, EXPR_COMMA, assignment_expression,
  874. (op == ',')
  875. );
  876. }
  877. struct token *parse_expression(struct token *token, struct expression **tree)
  878. {
  879. return comma_expression(token,tree);
  880. }