2 * The complicated C rule set for type conversion. This is a full research
3 * oriented approach, run against the standard, and the tons of trial and
6 * A little bit about what is involed in type conversion:
7 * - Arithmetic type rules
8 * - Implicit conversion
9 * - Explicit conversion
11 * 1. Arithmetic type rules:
12 * The C standard defines a set of rules about arithmetic type
13 * conversion, known as the conversion rank rules, which
14 * essentially dictate which sides of an expression need to be
18 * If the left hand side of an expression isn't an arithmetic type
19 * or the right hand side of an expression isn't an arithmetic type
20 * no conversion takes place.
23 * If the conversion rank of the left hand side expression type
24 * is less than the conversion rank of the right hand side
25 * expression type, then the left hand side of that expressions type
26 * gets converted to the right hands type.
29 * If the conversion rank of the left hand expression type doesn't
30 * compare equal to the right hands type, then the right hand side of
31 * that expressions type gets converted to the left hands type.
34 * If none of the above applies, then nothing is subjected to conversion,
35 * and doesn't need to be converted, unless the following:
37 * The binary expression in which each operand is associated with happens
38 * to be of a relational one in which case the type is converted to
41 * The expression happens to be of array type, in which case the array
42 * decays to a pointer of it's base type.
44 * 2. Implicit conversion
45 * Implicit type conversion takes place in two senarios, 1, when
46 * conversion ranking is involved (promoted types), or when the
47 * subject of a shift operation where the larger types is always
48 * assumed to satisfy the shift operation.
50 * 3. Explicit conversion
51 * The type which is assumed in explicit conversion (casting) is
52 * the type in which the operand is converted to, unless the conversion
53 * isn't legal (vector -> scalar for instance)
58 bool conv_capable(data_type_t *type) {
59 return ast_type_isinteger(type) || ast_type_isfloating(type);
62 int conv_rank(data_type_t *type) {
63 if (!conv_capable(type))
67 case TYPE_BOOL: return 0;
68 case TYPE_CHAR: return 1;
69 case TYPE_SHORT: return 2;
70 case TYPE_INT: return 3;
71 case TYPE_LONG: return 4;
72 case TYPE_LLONG: return 5;
73 case TYPE_FLOAT: return 6;
74 case TYPE_DOUBLE: return 7;
75 case TYPE_LDOUBLE: return 8;
81 compile_ice("conv_rank");
84 data_type_t *conv_senority(data_type_t *lhs, data_type_t *rhs) {
85 return conv_rank(lhs) < conv_rank(rhs) ? rhs : lhs;
88 ast_t *conv_usual(int operation, ast_t *left, ast_t *right) {
89 if (!conv_capable(left->ctype) || !conv_capable(right->ctype)) {
98 result = ast_data_table[AST_DATA_INT];
101 result = ast_array_convert(left->ctype);
105 return ast_new_binary(result, operation, left, right);
108 int lrank = conv_rank(left->ctype);
109 int rrank = conv_rank(right->ctype);
112 left = ast_type_convert(right->ctype, left);
113 else if (lrank != rrank)
114 right = ast_type_convert(left->ctype, right);
116 data_type_t *result = ast_result_type(operation, left->ctype);
117 return ast_new_binary(result, operation, left, right);