aboutsummaryrefslogtreecommitdiff
path: root/src/compiler/coerce.c
blob: 6ab12a6ea4d1996d17b520fecedd6f1fb6140ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "coerce.h"

/*
 * Be aware that some value variants represents actual values (e.g.
 * vt_int), and others represent a type (e.g. vt_scalar) which holds a
 * type identifier token. Here we implicitly expect a vt_scalar type as
 * first argument, but only receive the token. The second argument is a
 * value literal.  Our job is to decide if the value fits within the
 * given type. Our internal representation already ensures that value
 * fits within a 64bit signed or unsigned integer, or double; otherwise
 * the parser would have set vt_invalid type on the value.
 *
 * If the value is invalid, success is returned because the
 * error is presumably already generated. If the value is some other
 * type than expect, an error is generated.
 *
 * Symbolic names are not allowed as values here.
 *
 * Converts positive integers to signed type and unsigned integers to
 * signed type, integers to floats and floats to integers.
 *
 * Optionally allows 1 to be assigned as true and 0 as false, and vice
 * versa when allow_boolean_conversion is enabled.
 *
 * Returns 0 on success, -1 on error.
 */
int fb_coerce_scalar_type(fb_parser_t *P, fb_symbol_t *sym, fb_scalar_type_t st, fb_value_t *value)
{
    double d;
    float f;

    if (!value->type) {
        return 0;
    }
    /*
     * The parser only produces negative vt_int values, which simplifies
     * the logic, but to make this operation robust against multiple
     * coercion steps, we first convert back to uint if the assumption turns
     * out false.
     */
    if (value->type == vt_int && value->i >= 0) {
        value->type = vt_uint;
        value->u = (uint64_t)value->i;
    }
    if (value->type == vt_invalid) {
        /* Silently ignore past errors. */
        return 0;
    }
    if (value->type == vt_bool && st != fb_bool  && P->opts.allow_boolean_conversion) {
        value->type = vt_uint;
        value->u = (uint64_t)value->b;
        assert(value->u == 1 || value->u == 0);
    }
    switch (st) {
    case fb_ulong:
        if (value->type != vt_uint) {
            error_sym(P, sym, "64-bit uint32_t type only accepts unsigned integers");
            value->type = vt_invalid;
            return -1;
        }
        return 0;
    case fb_uint:
        if (value->type != vt_uint) {
            error_sym(P, sym, "32-bit unsigned int type only accepts unsigned integers");
            value->type = vt_invalid;
            return -1;
        }
        if (value->u > UINT32_MAX) {
            error_sym(P, sym, "32-bit unsigned int overflow");
            value->type = vt_invalid;
            return -1;
        }
        return 0;
    case fb_ushort:
        if (value->type != vt_uint) {
            error_sym(P, sym, "16-bit unsigned short type only accepts unsigned integers");
            value->type = vt_invalid;
            return -1;
        }
        if (value->u > UINT16_MAX) {
            error_sym(P, sym, "16-bit unsigned short overflow");
            value->type = vt_invalid;
            return -1;
        }
        return 0;
    case fb_char:
        /* Although C treats char as signed by default, flatcc treats it as unsigned. */
    case fb_ubyte:
        if (value->type != vt_uint) {
            error_sym(P, sym, "8-bit unsigned byte type only accepts unsigned integers");
            value->type = vt_invalid;
            return -1;
        }
        if (value->u > UINT8_MAX) {
            error_sym(P, sym, "8-bit unsigned byte overflow");
            value->type = vt_invalid;
            return -1;
        }
        return 0;
    case fb_long:
        if (value->type == vt_int) {
            /* Native format is always ok, or parser would have failed. */
            return 0;
        }
        if (value->type == vt_uint) {
            if (value->u >= (1ULL << 63)) {
                error_sym(P, sym, "64-bit signed int32_t overflow");
                value->type = vt_invalid;
                return -1;
            }
            value->i = (int64_t)value->u;
            value->type = vt_int;
            return 0;
        }
        error_sym(P, sym, "64-bit int32_t type only accepts integers");
        value->type = vt_invalid;
        return -1;
    case fb_int:
        if (value->type == vt_int) {
            if (value->i < INT32_MIN) {
                error_sym(P, sym, "32-bit signed int underflow");
                value->type = vt_invalid;
                return -1;
            }
            return 0;
        }
        if (value->type == vt_uint) {
            if (value->i > INT32_MAX) {
                error_sym(P, sym, "32-bit signed int overflow");
                value->type = vt_invalid;
                return -1;
            }
            value->i = (int64_t)value->u;
            value->type = vt_int;
            return 0;
        }
        error_sym(P, sym, "32-bit signed int type only accepts integers");
        value->type = vt_invalid;
        return -1;
    case fb_short:
        if (value->type == vt_int) {
            if (value->i < INT16_MIN) {
                error_sym(P, sym, "16-bit signed short underflow");
                value->type = vt_invalid;
                return -1;
            }
            return 0;
        }
        if (value->type == vt_uint) {
            if (value->i > INT16_MAX) {
                error_sym(P, sym, "16-bit signed short overflow");
                value->type = vt_invalid;
                return -1;
            }
            value->i = (int64_t)value->u;
            value->type = vt_int;
            return 0;
        }
        error_sym(P, sym, "16-bit signed short type only accepts integers");
        value->type = vt_invalid;
        return -1;
    case fb_byte:
        if (value->type == vt_int) {
            if (value->i < INT8_MIN) {
                error_sym(P, sym, "8-bit signed byte underflow");
                value->type = vt_invalid;
                return -1;
            }
            return 0;
        }
        if (value->type == vt_uint) {
            if (value->i > INT8_MAX) {
                error_sym(P, sym, "8-bit signed byte overflow");
                value->type = vt_invalid;
                return -1;
            }
            value->i = (int64_t)value->u;
            value->type = vt_int;
            return 0;
        }
        error_sym(P, sym, "8-bit signed byte type only accepts integers");
        value->type = vt_invalid;
        return -1;
    case fb_bool:
        if (value->type == vt_uint && P->opts.allow_boolean_conversion) {
            if (value->u > 1) {
                error_sym(P, sym, "boolean integer conversion only accepts 0 (false) or 1 (true)");
                value->type = vt_invalid;
                return -1;
            }
        } else if (value->type != vt_bool) {
            error_sym(P, sym, "boolean type only accepts 'true' or 'false' as values");
            value->type = vt_invalid;
            return -1;
        }
        return 0;
    case fb_double:
        switch (value->type) {
        case vt_int:
            d = (double)value->i;
            if ((int64_t)d != value->i) {
                /* We could make this a warning. */
                error_sym(P, sym, "precision loss in 64-bit double type assignment");
                value->type = vt_invalid;
                return -1;
            }
            value->f = d;
            value->type = vt_float;
            return 0;
        case vt_uint:
            d = (double)value->u;
            if ((uint64_t)d != value->u) {
                /* We could make this a warning. */
                error_sym(P, sym, "precision loss in 64-bit double type assignment");
                value->type = vt_invalid;
                return -1;
            }
            value->f = d;
            value->type = vt_float;
            return 0;
        case vt_float:
            /* Double is our internal repr., so not loss at this point. */
            return 0;
        default:
            error_sym(P, sym, "64-bit double type only accepts integer and float values");
            value->type = vt_invalid;
            return -1;
        }
    case fb_float:
        switch (value->type) {
        case vt_int:
            f = (float)value->i;
            if ((int64_t)f != value->i) {
                /* We could make this a warning. */
                error_sym(P, sym, "precision loss in 32-bit float type assignment");
                value->type = vt_invalid;
                return -1;
            }
            value->f = f;
            value->type = vt_float;
            return 0;
        case vt_uint:
            f = (float)value->u;
            if ((uint64_t)f != value->u) {
                /* We could make this a warning. */
                error_sym(P, sym, "precision loss in 32-bit float type assignment");
                value->type = vt_invalid;
                return -1;
            }
            value->f = f;
            value->type = vt_float;
            return 0;
        case vt_float:
            return 0;
        default:
            error_sym(P, sym, "32-bit float type only accepts integer and float values");
            value->type = vt_invalid;
            return -1;
        }
    default:
        error_sym(P, sym, "scalar type expected");
        value->type = vt_invalid;
        return -1;
    }
}