3 struct expression *expr(enum expression_type type, const struct location *where) {
12 struct expression *binary(int op,
15 const struct location *where) {
17 struct declarator *valuetype = 0;
19 /* all binary operators require array decay (C99 6.3.2.1) */
23 if(l->valuetype && r->valuetype) {
28 /* one side must be pointer to T, the other integral, the result type
30 if(is_integral_type(l->valuetype)
31 && is_pointer_type(r->valuetype)) {
32 valuetype = target_type(r->valuetype);
33 } else if(is_pointer_type(l->valuetype)
34 && is_integral_type(r->valuetype)) {
35 valuetype = target_type(l->valuetype);
37 inputerror(where, "invalid operands to [] operator");
40 dump_state *dump = dump_new(stderr);
41 dump_locations(dump, 0);
42 dump_declaration_specifiers(dump,
43 l->valuetype->declaration_specifiers,
45 dump_declarator(dump, l->valuetype, 2);
48 dump = dump_new(stderr);
49 dump_locations(dump, 0);
50 dump_declaration_specifiers(dump,
51 r->valuetype->declaration_specifiers,
53 dump_declarator(dump, r->valuetype, 2);
69 if(!is_arithmetic_type(l->valuetype)
70 || !is_arithmetic_type(r->valuetype))
71 inputerror(where, "arguments to %c must have arithmetic type");
73 valuetype = usual_arithmetic_conversions(&l, &r, where);
78 if(!is_integral_type(l->valuetype)
79 || !is_integral_type(r->valuetype))
80 inputerror(where, "arguments to %c must have integral type", op);
82 valuetype = usual_arithmetic_conversions(&l, &r, where);
89 if(is_arithmetic_type(l->valuetype)
90 && is_arithmetic_type(r->valuetype))
91 valuetype = usual_arithmetic_conversions(&l, &r, where);
99 if(!is_integral_type(l->valuetype)
100 || !is_integral_type(r->valuetype))
102 "arguments to shift operators must have integral type");
104 l = integer_promotions(l);
105 r = integer_promotions(r);
106 valuetype = l->valuetype;
117 /* XXX constraints */
118 if(is_arithmetic_type(l->valuetype)
119 && is_arithmetic_type(r->valuetype))
120 usual_arithmetic_conversions(&l, &r, where);
133 if(!is_integral_type(l->valuetype)
134 || !is_integral_type(r->valuetype))
135 inputerror(where, "arguments to %c must have integral type", op);
137 valuetype = usual_arithmetic_conversions(&l, &r, where);
143 if(!is_scalar_type(l->valuetype)
144 || !is_scalar_type(r->valuetype))
146 "arguments to logical operators must have scalar type", op);
152 /* XXX constraints */
158 if(!is_scalar_type(l->valuetype))
160 "left argument of ? operator must have scalar type");
161 /* XXX valuetype will be bad until ':' is done */
164 /* XXX assignment operators */
168 e = expr(ex_binary, where);
172 e->valuetype = valuetype;
176 struct expression *prefix(int op, struct expression *r,
177 const struct location *where) {
178 struct expression *e = expr(ex_prefix, where);
181 /* XXX constraints */
186 struct expression *postfix(int op, struct expression *l,
187 const struct location *where) {
188 struct expression *e = expr(ex_postfix, where);
191 /* XXX constraints */
196 struct expression *paren(struct expression *content,
197 const struct location *where) {
198 struct expression *e = expr(ex_paren, where);
199 e->u.unary = content;
200 e->valuetype = content->valuetype;
204 /* array-to-pointer decay (C99 6.3.2.1) */
205 struct expression *array_decay(struct expression *e) {
206 struct expression *ne;
207 struct declarator_type *dt;
210 && e->valuetype->declarator_type
211 && e->valuetype->declarator_type->type == dt_array) {
213 ne->type = ex_implicit_conversion;
215 *ne->valuetype = *e->valuetype;
217 dt->type = dt_pointer;
218 dt->type_qualifiers = e->valuetype->declarator_type->type_qualifiers;
219 dt->next = e->valuetype->declarator_type->next;
220 ne->valuetype->declarator_type = dt;
227 /* integral promotions (C99 6.3.1.1)
228 * anything >= (unsigned) int stays as it is
229 * otherwise if int is good enough we use that
230 * otherwise we use unsigned
232 * This function determines the promoted type of e, or returns 0 if it does not
233 * have known or integral type.
236 static unsigned long integer_promoted_type(struct expression *e) {
237 unsigned long basic, size, sign, ts;
239 if(!e->valuetype || e->valuetype->declarator_type) return 0;
240 ts = e->valuetype->declaration_specifiers->type_specifiers;
241 basic = e->valuetype->declaration_specifiers->type_specifiers & TS__BASIC;
242 if(basic == TS_ENUM) {
243 ts = e->valuetype->declaration_specifiers->enum_compat_type;
244 basic = e->valuetype->declaration_specifiers->type_specifiers & TS__BASIC;
246 size = e->valuetype->declaration_specifiers->type_specifiers & TS__LENGTH;
247 sign = e->valuetype->declaration_specifiers->type_specifiers & TS__SIGN;
251 if(size == TS_SHORT) {
252 if(sign == TS_UNSIGNED) {
254 if(P_USHRT_MAX <= P_INT_MAX)
265 if(sign == TS_UNSIGNED || (!sign && CHAR_MIN == 0)) {
266 /* (unsigned) char */
267 if(P_UCHAR_MAX <= P_INT_MAX)
271 } else if(sign == TS_SIGNED || (!sign && CHAR_MIN < 0)) {
281 /* XXX bit fields? */
286 /* add an implicit conversion performing integer promotions if necessary */
287 struct expression *integer_promotions(struct expression *e) {
288 struct expression *ne;
292 && !e->valuetype->declarator_type) {
293 ts = integer_promoted_type(e);
294 if(ts != e->valuetype->declaration_specifiers->type_specifiers) {
296 ne->type = ex_implicit_conversion;
298 NEW(ne->valuetype->declaration_specifiers);
299 ne->valuetype->declaration_specifiers->type_specifiers = ts;
307 static unsigned long corresponding_real_type(struct declarator *d) {
310 if(!d->declarator_type)
311 switch((u = d->declaration_specifiers->type_specifiers & ~TS__CI)) {
314 case TS_LONG|TS_DOUBLE:
320 static struct expression *simple_implicit_conversion
321 (struct expression *e,
323 const struct location *where) {
324 struct expression *ne;
325 struct declarator *d;
327 assert(e->valuetype != 0);
328 assert(e->valuetype->declarator_type == 0);
329 if(nt == e->valuetype->declaration_specifiers->type_specifiers)
332 NEW(d->declaration_specifiers);
333 d->declaration_specifiers->type_specifiers = nt;
335 ne = expr(ex_implicit_conversion, where);
341 static const unsigned long rank_table[] = {
350 static int rank(const struct declaration_specifiers *d) {
352 unsigned long t = d->type_specifiers;
354 if((t & TS__BASIC) == TS_ENUM)
355 t = d->enum_compat_type;
356 t &= (TS__BASIC|TS__LENGTH);
359 for(n = 0; n < sizeof rank_table / sizeof *rank_table; ++n)
360 if(rank_table[n] == t)
365 static int sign(const struct declaration_specifiers *d) {
366 unsigned long t = d->type_specifiers;
368 if((t & TS__BASIC) == TS_ENUM)
369 t = d->enum_compat_type;
372 switch(t & TS__BASIC) {
374 return TS_UNSIGNED; /* XXX erm */
376 return TS_SIGNED; /* XXX depends on compiler */
378 return TS_SIGNED; /* everything else is signed */
383 struct declarator *usual_arithmetic_conversions(struct expression **lp,
384 struct expression **rp,
385 const struct location *where) {
386 struct expression *l = *lp, *r = *rp;
387 unsigned long lcrt, rcrt; /* common real type */
388 unsigned long lpt, rpt; /* integer-promoted type */
389 unsigned long lsign, rsign; /* sign bits */
390 int lrank, rrank; /* ranks */
392 /* if type is unknown do nothing */
393 if(!l->valuetype || !r->valuetype) return 0;
395 /* find corresponding real type */
396 lcrt = corresponding_real_type(l->valuetype);
397 rcrt = corresponding_real_type(r->valuetype);
399 return usual_arithmetic_conversions(rp, lp, where);
401 *rp = simple_implicit_conversion
404 | (r->valuetype->declaration_specifiers->type_specifiers & TS__CI),
409 /* integer promotions */
410 lpt = integer_promoted_type(l);
411 l = simple_implicit_conversion(l, lpt, where);
412 rpt = integer_promoted_type(r);
413 r = simple_implicit_conversion(r, rpt, where);
415 /* rules for promoted operands */
416 if(lpt && rpt && lpt != rpt) {
417 lrank = rank(l->valuetype->declaration_specifiers);
418 rrank = rank(r->valuetype->declaration_specifiers);
419 lsign = sign(l->valuetype->declaration_specifiers);
420 rsign = sign(r->valuetype->declaration_specifiers);
421 /* same signedness -> highest rank wins */
427 /* if rank(unsigned type) >= rank(signed type), unsigned type wins */
428 } else if(lsign == TS_UNSIGNED && lrank >= rrank)
430 else if(rsign == TS_UNSIGNED && rrank >= lrank)
432 /* if signed type \superset unsigned type, signed type wins */
433 else if(lsign == TS_SIGNED && width(lpt) > width(rpt))
435 else if(rsign == TS_SIGNED && width(rpt) > width(lpt))
437 /* otherwise convert both to the unsigned type matching the signed type */
438 else if(lsign == TS_SIGNED)
439 rpt = lpt = (lpt & ~TS__SIGN) | TS_UNSIGNED;
441 rpt = lpt = (rpt & ~TS__SIGN) | TS_UNSIGNED;
442 l = simple_implicit_conversion(l, lpt, where);
443 r = simple_implicit_conversion(r, rpt, where);
446 /* pass the answers back */
453 struct expression *stringexpr(enum expression_type type,
455 const struct location *where) {
456 struct expression *e;
458 e = expr(type, where);
459 e->u.constant = value;
461 NEW(e->valuetype->declaration_specifiers);
464 e->valuetype->declaration_specifiers->type_specifiers = TS_CHAR;
467 e->valuetype->declaration_specifiers->type_specifiers = TS_INT;
470 e->valuetype->declaration_specifiers->type_specifiers = P_WCHAR_T;
473 e->valuetype->declaration_specifiers->type_specifiers = P_WCHAR_T;
478 if(type == ex_string || type == ex_wstring) {
479 if(constant_string_literals)
480 e->valuetype->declaration_specifiers->type_qualifiers = TQ_CONST;
481 NEW(e->valuetype->declarator_type);
482 e->valuetype->declarator_type->type = dt_pointer;
484 NEW(e->valuetype->declarator_type);
485 e->valuetype->declarator_type->type = dt_pointer;
489 struct expression *fncallexpr(struct expression *f,
490 struct expression_list *l,
491 const struct location *where) {
492 struct declarator_type *dt;
493 struct expression *e;
495 e = expr(ex_fncall, where);
497 e->u.fncall.args = l;
499 /* f can either be a function or a pointer to a function (but not e.g. a
500 * pointer to a pointer to a function) */
501 dt = f->valuetype->declarator_type;
502 if(dt && dt->type == dt_pointer)
506 if(dt && (dt->type == dt_function
507 || dt->type == dt_old_function))
508 e->valuetype = target_type(f->valuetype);
510 inputerror(where, "left operand to () operator is not a function");