chiark / gitweb /
Import upstream sources.
[cparse] / expr.c
1 #include "cparse.h"
2
3 struct expression *expr(enum expression_type type, const struct location *where) {
4   struct expression *e;
5
6   NEW(e);
7   e->type = type;
8   e->where = *where;
9   return e;
10 }
11
12 struct expression *binary(int op,
13                           struct expression *l,
14                           struct expression *r,
15                           const struct location *where) {
16   struct expression *e;
17   struct declarator *valuetype = 0;
18
19   /* all binary operators require array decay (C99 6.3.2.1) */
20   l = array_decay(l);
21   r = array_decay(r);
22
23   if(l->valuetype && r->valuetype) {
24     switch(op) {
25     case '[':
26       /* C99 6.5.2.1 */
27       
28       /* one side must be pointer to T, the other integral, the result type
29        * is T */
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);
36       } else {
37         inputerror(where, "invalid operands to [] operator");
38 #if 1
39         {
40           dump_state *dump = dump_new(stderr);
41           dump_locations(dump, 0);
42           dump_declaration_specifiers(dump,
43                                       l->valuetype->declaration_specifiers,
44                                       2);
45           dump_declarator(dump, l->valuetype, 2);
46           fputc('\n', stderr);
47           
48           dump = dump_new(stderr);
49           dump_locations(dump, 0);
50           dump_declaration_specifiers(dump,
51                                       r->valuetype->declaration_specifiers,
52                                       2);
53           dump_declarator(dump, r->valuetype, 2);
54           fputc('\n', stderr);
55           abort();
56         }
57 #endif
58       }
59       break;
60       
61     case '.':
62     case MEMBER:
63       /* XXX */
64       break;
65
66     case '*':
67     case '/':
68       /* C99 6.5.5 */
69       if(!is_arithmetic_type(l->valuetype)
70          || !is_arithmetic_type(r->valuetype))
71         inputerror(where, "arguments to %c must have arithmetic type");
72       else
73         valuetype = usual_arithmetic_conversions(&l, &r, where);
74       break;
75
76     case '%':
77       /* C99 6.5.5 */
78       if(!is_integral_type(l->valuetype)
79          || !is_integral_type(r->valuetype))
80         inputerror(where, "arguments to %c must have integral type", op);
81       else
82         valuetype = usual_arithmetic_conversions(&l, &r, where);
83       break;
84       
85     case '+':
86     case '-':
87       /* C99 6.5.6 */
88       /* XXX constraints */
89       if(is_arithmetic_type(l->valuetype)
90          && is_arithmetic_type(r->valuetype))
91         valuetype = usual_arithmetic_conversions(&l, &r, where);
92       /* XXX pointers */
93       break;
94       
95     case SL:
96     case SR:
97       /* C99 6.5.7 */
98       /* XXX constraints */
99       if(!is_integral_type(l->valuetype)
100          || !is_integral_type(r->valuetype))
101         inputerror(where,
102                    "arguments to shift operators must have integral type");
103       else {
104         l = integer_promotions(l);
105         r = integer_promotions(r);
106         valuetype = l->valuetype;
107       }
108       break;
109
110       break;
111
112     case '<':
113     case '>':
114     case LE:
115     case GE:
116       /* C99 6.5.8 */
117       /* XXX constraints */
118       if(is_arithmetic_type(l->valuetype)
119          && is_arithmetic_type(r->valuetype))
120         usual_arithmetic_conversions(&l, &r, where);
121       /* XXX valuetype */
122       break;
123       
124     case EQ:
125     case NE:
126       /* C99 6.5.9 */
127       break;
128       
129     case '&':
130     case '|':
131     case '^':
132       /* C99 6.5.10-12 */
133       if(!is_integral_type(l->valuetype)
134          || !is_integral_type(r->valuetype))
135         inputerror(where, "arguments to %c must have integral type", op);
136       else
137         valuetype = usual_arithmetic_conversions(&l, &r, where);
138       break;
139
140     case AND:
141     case OR:
142       /* C99 6.5.13-14 */
143       if(!is_scalar_type(l->valuetype)
144          || !is_scalar_type(r->valuetype))
145         inputerror(where,
146                    "arguments to logical operators must have scalar type", op);
147       /* XXX valuetype */
148       break;
149
150     case ':':
151       /* C99 6.5.15 */
152       /* XXX constraints */
153       /* XXX valuetype */
154       break;
155
156     case '?':
157       /* C99 6.5.15 */
158       if(!is_scalar_type(l->valuetype))
159         inputerror(where,
160                    "left argument of ? operator must have scalar type");
161       /* XXX valuetype will be bad until ':' is done */
162       break;
163
164       /* XXX assignment operators */
165       
166     }
167   }
168   e = expr(ex_binary, where);
169   e->operator = op;
170   e->u.binary.l = l;
171   e->u.binary.r = r;
172   e->valuetype = valuetype;
173   return e;
174 }
175
176   struct expression *prefix(int op, struct expression *r,
177                           const struct location *where) {
178   struct expression *e = expr(ex_prefix, where);
179   e->operator = op;
180   e->u.unary = r;
181   /* XXX constraints */
182   /* XXX valuetype */
183   return e;
184 }
185
186 struct expression *postfix(int op, struct expression *l,
187                            const struct location *where) {
188   struct expression *e = expr(ex_postfix, where);
189   e->operator = op;
190   e->u.unary = l;
191   /* XXX constraints */
192   /* XXX valuetype */
193   return e;
194 }
195
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;
201   return e;
202 }
203
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;
208
209   if(e->valuetype
210      && e->valuetype->declarator_type
211      && e->valuetype->declarator_type->type == dt_array) {
212     NEW(ne);
213     ne->type = ex_implicit_conversion;
214     NEW(ne->valuetype);
215     *ne->valuetype = *e->valuetype;
216     NEW(dt);
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;
221     ne->u.cast = e;
222     return ne;
223   }
224   return e;
225 }
226
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
231  *
232  * This function determines the promoted type of e, or returns 0 if it does not
233  * have known or integral type.
234  */
235
236 static unsigned long integer_promoted_type(struct expression *e) {
237   unsigned long basic, size, sign, ts;
238
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;
245   }
246   size = e->valuetype->declaration_specifiers->type_specifiers & TS__LENGTH;
247   sign = e->valuetype->declaration_specifiers->type_specifiers & TS__SIGN;
248   switch(basic) {
249   case 0:
250   case TS_INT:
251     if(size == TS_SHORT) {
252       if(sign == TS_UNSIGNED) {
253         /* unsigned short */
254         if(P_USHRT_MAX <= P_INT_MAX)
255           return TS_INT;
256         else
257           return TS_UNSIGNED;
258       } else {
259         /* (signed) short */
260         return TS_INT;
261       }
262     }
263     break;
264   case TS_CHAR:
265     if(sign == TS_UNSIGNED || (!sign && CHAR_MIN == 0)) {
266       /* (unsigned) char */
267       if(P_UCHAR_MAX <= P_INT_MAX)
268         return TS_INT;
269       else
270         return TS_UNSIGNED;
271     } else if(sign == TS_SIGNED || (!sign && CHAR_MIN < 0)) {
272       /* (signed) char */
273       return TS_INT;
274     }
275     break;
276
277   case TS_BOOL:
278     return TS_INT;
279     break;
280
281     /* XXX bit fields? */
282   }
283   return ts;
284 }
285
286 /* add an implicit conversion performing integer promotions if necessary */
287 struct expression *integer_promotions(struct expression *e) {
288   struct expression *ne;
289   unsigned long ts;
290
291   if(e->valuetype
292      && !e->valuetype->declarator_type) {
293     ts = integer_promoted_type(e);
294     if(ts != e->valuetype->declaration_specifiers->type_specifiers) {
295       NEW(ne);
296       ne->type = ex_implicit_conversion;
297       NEW(ne->valuetype);
298       NEW(ne->valuetype->declaration_specifiers);
299       ne->valuetype->declaration_specifiers->type_specifiers = ts;
300       ne->u.cast = e;
301       return ne;
302     }
303   }
304   return e;
305 }
306
307 static unsigned long corresponding_real_type(struct declarator *d) {
308   unsigned long u;
309
310   if(!d->declarator_type)
311     switch((u = d->declaration_specifiers->type_specifiers & ~TS__CI)) {
312     case TS_FLOAT:
313     case TS_DOUBLE:
314     case TS_LONG|TS_DOUBLE:
315       return u;
316     }
317   return 0;
318 }
319
320 static struct expression *simple_implicit_conversion
321      (struct expression *e,
322       unsigned long nt,
323       const struct location *where) {
324   struct expression *ne;
325   struct declarator *d;
326
327   assert(e->valuetype != 0);
328   assert(e->valuetype->declarator_type == 0);
329   if(nt == e->valuetype->declaration_specifiers->type_specifiers)
330     return e;
331   NEW(d);
332   NEW(d->declaration_specifiers);
333   d->declaration_specifiers->type_specifiers = nt;
334   d->where = *where;
335   ne = expr(ex_implicit_conversion, where);
336   ne->valuetype = d;
337   ne->u.cast = e;
338   return ne;
339 }
340
341 static const unsigned long rank_table[] = {
342   TS_BOOL,
343   TS_CHAR,
344   TS_SHORT|TS_INT,
345   TS_INT,
346   TS_LONG|TS_INT,
347   TS_LONGLONG|TS_INT
348 };
349
350 static int rank(const struct declaration_specifiers *d) {
351   unsigned n;
352   unsigned long t = d->type_specifiers;
353
354   if((t & TS__BASIC) == TS_ENUM)
355     t = d->enum_compat_type;
356   t &= (TS__BASIC|TS__LENGTH);
357   if(t & TS__BASIC)
358     t |= TS_INT;
359   for(n = 0; n < sizeof rank_table / sizeof *rank_table; ++n)
360     if(rank_table[n] == t)
361       return n;
362   return -1;
363 }
364
365 static int sign(const struct declaration_specifiers *d) {
366   unsigned long t = d->type_specifiers;
367
368   if((t & TS__BASIC) == TS_ENUM)
369     t = d->enum_compat_type;
370   if(t & TS__SIGN)
371     return t & TS__SIGN;
372   switch(t & TS__BASIC) {
373   case TS_BOOL:
374     return TS_UNSIGNED;                 /* XXX erm */
375   case TS_CHAR:
376     return TS_SIGNED;                   /* XXX depends on compiler */
377   default:
378     return TS_SIGNED;                   /* everything else is signed */
379   }
380 }
381
382 /* C99 6.3.1.8 */
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 */
391
392   /* if type is unknown do nothing */
393   if(!l->valuetype || !r->valuetype) return 0;
394
395   /* find corresponding real type */
396   lcrt = corresponding_real_type(l->valuetype);
397   rcrt = corresponding_real_type(r->valuetype);
398   if(rcrt > lcrt)
399     return usual_arithmetic_conversions(rp, lp, where);
400   if(lcrt > rcrt) {
401     *rp = simple_implicit_conversion
402            (r,
403             lcrt
404             | (r->valuetype->declaration_specifiers->type_specifiers & TS__CI),
405             where);
406     return 0;
407   }
408
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);
414   
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 */
422     if(lsign == rsign) {
423       if(lrank > rrank)
424         rpt = lpt;
425       else
426         lpt = rpt;
427     /* if rank(unsigned type) >= rank(signed type), unsigned type wins */
428     } else if(lsign == TS_UNSIGNED && lrank >= rrank)
429       rpt = lpt;
430     else if(rsign == TS_UNSIGNED && rrank >= lrank)
431       lpt = rpt;
432     /* if signed type \superset unsigned type, signed type wins */
433     else if(lsign == TS_SIGNED && width(lpt) > width(rpt))
434       rpt = lpt;
435     else if(rsign == TS_SIGNED && width(rpt) > width(lpt))
436       lpt = rpt;
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;
440     else 
441       rpt = lpt = (rpt & ~TS__SIGN) | TS_UNSIGNED;
442     l = simple_implicit_conversion(l, lpt, where);
443     r = simple_implicit_conversion(r, rpt, where);
444   }
445
446   /* pass the answers back */
447   *lp = l;
448   *rp = r;
449
450   return l->valuetype;
451 }
452
453 struct expression *stringexpr(enum expression_type type,
454                               char *value,
455                               const struct location *where) {
456   struct expression *e;
457
458   e = expr(type, where);
459   e->u.constant = value;
460   NEW(e->valuetype);
461   NEW(e->valuetype->declaration_specifiers);
462   switch(type) {
463   case ex_string:
464     e->valuetype->declaration_specifiers->type_specifiers = TS_CHAR;
465     break;
466   case ex_char:
467     e->valuetype->declaration_specifiers->type_specifiers = TS_INT;
468     break;
469   case ex_wstring:
470     e->valuetype->declaration_specifiers->type_specifiers = P_WCHAR_T;
471     break;
472   case ex_wchar:
473     e->valuetype->declaration_specifiers->type_specifiers = P_WCHAR_T;
474     break;
475   default:
476     abort();
477   }
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;
483   }
484   NEW(e->valuetype->declarator_type);
485   e->valuetype->declarator_type->type = dt_pointer;
486   return e;
487 }
488
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;
494   
495   e = expr(ex_fncall, where);
496   e->u.fncall.fn = f;
497   e->u.fncall.args = l;
498   if(f->valuetype) {
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)
503       dt = dt->next;
504   } else
505     dt = 0;
506   if(dt && (dt->type == dt_function
507             || dt->type == dt_old_function))
508     e->valuetype = target_type(f->valuetype);
509   else
510     inputerror(where, "left operand to () operator is not a function");
511   return e;
512
513
514 /*
515 Local Variables:
516 c-basic-offset:2
517 comment-column:40
518 fill-column:79
519 End:
520 */