tree.c revision 1.693 1 1.693 rillig /* $NetBSD: tree.c,v 1.693 2025/09/14 14:42:52 rillig Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
5 1.1 cgd * All Rights Reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.563 rillig * This product includes software developed by Jochen Pohl for
18 1.1 cgd * The NetBSD Project.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.37 jmc #if HAVE_NBTOOL_CONFIG_H
35 1.37 jmc #include "nbtool_config.h"
36 1.37 jmc #endif
37 1.37 jmc
38 1.14 christos #include <sys/cdefs.h>
39 1.442 rillig #if defined(__RCSID)
40 1.693 rillig __RCSID("$NetBSD: tree.c,v 1.693 2025/09/14 14:42:52 rillig Exp $");
41 1.1 cgd #endif
42 1.1 cgd
43 1.1 cgd #include <float.h>
44 1.1 cgd #include <limits.h>
45 1.1 cgd #include <math.h>
46 1.49 christos #include <signal.h>
47 1.92 rillig #include <stdlib.h>
48 1.92 rillig #include <string.h>
49 1.1 cgd
50 1.1 cgd #include "lint1.h"
51 1.1 cgd
52 1.506 rillig
53 1.444 rillig typedef struct integer_constraints {
54 1.444 rillig int64_t smin; /* signed minimum */
55 1.444 rillig int64_t smax; /* signed maximum */
56 1.444 rillig uint64_t umin; /* unsigned minimum */
57 1.444 rillig uint64_t umax; /* unsigned maximum */
58 1.444 rillig uint64_t bclr; /* bits that are definitely clear */
59 1.444 rillig } integer_constraints;
60 1.444 rillig
61 1.1 cgd
62 1.656 rillig static int64_t
63 1.656 rillig s64_min(int64_t a, int64_t b)
64 1.656 rillig {
65 1.656 rillig return a < b ? a : b;
66 1.656 rillig }
67 1.656 rillig
68 1.656 rillig static int64_t
69 1.656 rillig s64_max(int64_t a, int64_t b)
70 1.656 rillig {
71 1.656 rillig return a > b ? a : b;
72 1.656 rillig }
73 1.656 rillig
74 1.656 rillig static uint64_t
75 1.656 rillig s64_abs(int64_t x)
76 1.656 rillig {
77 1.656 rillig return x >= 0 ? (uint64_t)x : -(uint64_t)x;
78 1.656 rillig }
79 1.656 rillig
80 1.667 rillig static int64_t
81 1.667 rillig s64_shr(int64_t x, unsigned amount)
82 1.667 rillig {
83 1.667 rillig return x >= 0
84 1.667 rillig ? (int64_t)((uint64_t)x >> amount)
85 1.667 rillig : (int64_t)~(~(uint64_t)x >> amount);
86 1.667 rillig }
87 1.667 rillig
88 1.667 rillig static uint64_t
89 1.667 rillig u64_min(uint64_t a, uint64_t b)
90 1.667 rillig {
91 1.667 rillig return a < b ? a : b;
92 1.667 rillig }
93 1.667 rillig
94 1.656 rillig static uint64_t
95 1.656 rillig u64_max(uint64_t a, uint64_t b)
96 1.656 rillig {
97 1.656 rillig return a > b ? a : b;
98 1.656 rillig }
99 1.656 rillig
100 1.666 rillig static uint64_t
101 1.667 rillig u64_fill_right(uint64_t x)
102 1.667 rillig {
103 1.667 rillig x |= x >> 1;
104 1.667 rillig x |= x >> 2;
105 1.667 rillig x |= x >> 4;
106 1.667 rillig x |= x >> 8;
107 1.667 rillig x |= x >> 16;
108 1.667 rillig x |= x >> 32;
109 1.667 rillig return x;
110 1.667 rillig }
111 1.667 rillig
112 1.693 rillig static unsigned
113 1.693 rillig u64_width(uint64_t x)
114 1.693 rillig {
115 1.693 rillig unsigned m = 0;
116 1.693 rillig if (x >> 32 != 0)
117 1.693 rillig x >>= 32, m += 32;
118 1.693 rillig if (x >> 16 != 0)
119 1.693 rillig x >>= 16, m += 16;
120 1.693 rillig if (x >> 8 != 0)
121 1.693 rillig x >>= 8, m += 8;
122 1.693 rillig if (x >> 4 != 0)
123 1.693 rillig x >>= 4, m += 4;
124 1.693 rillig if (x >> 2 != 0)
125 1.693 rillig x >>= 2, m += 2;
126 1.693 rillig if (x >> 1 != 0)
127 1.693 rillig x >>= 1, m++;
128 1.693 rillig if (x > 0)
129 1.693 rillig m++;
130 1.693 rillig return m;
131 1.693 rillig }
132 1.693 rillig
133 1.667 rillig static int
134 1.667 rillig portable_rank_cmp(tspec_t t1, tspec_t t2)
135 1.667 rillig {
136 1.667 rillig const ttab_t *p1 = type_properties(t1), *p2 = type_properties(t2);
137 1.667 rillig lint_assert(p1->tt_rank_kind == p2->tt_rank_kind);
138 1.667 rillig lint_assert(p1->tt_rank_value > 0);
139 1.667 rillig lint_assert(p2->tt_rank_value > 0);
140 1.667 rillig return (int)p1->tt_rank_value - (int)p2->tt_rank_value;
141 1.667 rillig }
142 1.667 rillig
143 1.667 rillig static unsigned
144 1.667 rillig width_in_bits(const type_t *tp)
145 1.667 rillig {
146 1.667 rillig lint_assert(is_integer(tp->t_tspec));
147 1.667 rillig return tp->t_bitfield
148 1.667 rillig ? tp->t_bit_field_width
149 1.667 rillig : size_in_bits(tp->t_tspec);
150 1.667 rillig }
151 1.667 rillig
152 1.667 rillig static uint64_t
153 1.666 rillig ui_max_value(const type_t *tp)
154 1.665 rillig {
155 1.667 rillig return value_bits(width_in_bits(tp));
156 1.665 rillig }
157 1.665 rillig
158 1.665 rillig static int64_t
159 1.665 rillig si_max_value(const type_t *tp)
160 1.665 rillig {
161 1.666 rillig return (int64_t)(ui_max_value(tp) >> 1);
162 1.665 rillig }
163 1.665 rillig
164 1.666 rillig static int64_t
165 1.666 rillig si_min_value(const type_t *tp)
166 1.665 rillig {
167 1.666 rillig return -si_max_value(tp) - 1;
168 1.665 rillig }
169 1.665 rillig
170 1.665 rillig static int64_t
171 1.669 rillig si_mult_sat(const type_t *tp, int64_t l, int64_t r)
172 1.669 rillig {
173 1.669 rillig uint64_t al = s64_abs(l);
174 1.669 rillig uint64_t ar = s64_abs(r);
175 1.669 rillig bool neg = (l >= 0) != (r >= 0);
176 1.676 rillig int64_t max = si_max_value(tp);
177 1.676 rillig uint64_t max_prod = (uint64_t)max + (neg ? 1 : 0);
178 1.669 rillig if (al == 0 || ar <= max_prod / al)
179 1.669 rillig return l * r;
180 1.669 rillig else if (neg)
181 1.676 rillig return -1 - max;
182 1.669 rillig else
183 1.676 rillig return max;
184 1.669 rillig }
185 1.669 rillig
186 1.669 rillig static int64_t
187 1.665 rillig si_plus_sat(const type_t *tp, int64_t a, int64_t b)
188 1.665 rillig {
189 1.666 rillig if (b >= 0) {
190 1.665 rillig int64_t max = si_max_value(tp);
191 1.666 rillig return a <= max - b ? a + b : max;
192 1.665 rillig } else {
193 1.665 rillig int64_t min = si_min_value(tp);
194 1.666 rillig return a >= min - b ? a + b : min;
195 1.665 rillig }
196 1.665 rillig }
197 1.665 rillig
198 1.666 rillig static int64_t
199 1.666 rillig si_minus_sat(const type_t *tp, int64_t a, int64_t b)
200 1.665 rillig {
201 1.666 rillig if (b >= 0) {
202 1.666 rillig int64_t min = si_min_value(tp);
203 1.666 rillig return a >= min + b ? a - b : min;
204 1.666 rillig } else {
205 1.666 rillig int64_t max = si_max_value(tp);
206 1.666 rillig return a <= max + b ? a - b : max;
207 1.666 rillig }
208 1.665 rillig }
209 1.665 rillig
210 1.506 rillig static bool
211 1.667 rillig ic_maybe_signed(const type_t *tp, integer_constraints ic)
212 1.506 rillig {
213 1.667 rillig return !is_uinteger(tp->t_tspec) && ic.bclr >> 63 == 0;
214 1.473 rillig }
215 1.473 rillig
216 1.449 rillig static bool
217 1.667 rillig ic_maybe_signed_binary(const type_t *tp,
218 1.667 rillig integer_constraints a, integer_constraints b)
219 1.449 rillig {
220 1.667 rillig return !is_uinteger(tp->t_tspec) && (a.bclr & b.bclr) >> 63 == 0;
221 1.449 rillig }
222 1.449 rillig
223 1.444 rillig static integer_constraints
224 1.444 rillig ic_any(const type_t *tp)
225 1.444 rillig {
226 1.444 rillig integer_constraints c;
227 1.444 rillig
228 1.666 rillig unsigned width = width_in_bits(tp);
229 1.666 rillig uint64_t vbits = value_bits(width);
230 1.444 rillig if (is_uinteger(tp->t_tspec)) {
231 1.666 rillig c.smin = width < 64 ? 0 : INT64_MIN;
232 1.666 rillig c.smax = width < 64 ? (int64_t)vbits : INT64_MAX;
233 1.444 rillig c.umin = 0;
234 1.444 rillig c.umax = vbits;
235 1.444 rillig c.bclr = ~c.umax;
236 1.444 rillig } else {
237 1.667 rillig c.smin = -1 - (int64_t)(vbits >> 1);
238 1.444 rillig c.smax = (int64_t)(vbits >> 1);
239 1.444 rillig c.umin = 0;
240 1.444 rillig c.umax = UINT64_MAX;
241 1.444 rillig c.bclr = 0;
242 1.444 rillig }
243 1.444 rillig return c;
244 1.444 rillig }
245 1.444 rillig
246 1.444 rillig static integer_constraints
247 1.653 rillig ic_mult(const type_t *tp, integer_constraints a, integer_constraints b)
248 1.653 rillig {
249 1.653 rillig integer_constraints c;
250 1.653 rillig
251 1.669 rillig if (ic_maybe_signed_binary(tp, a, b)) {
252 1.669 rillig int64_t ll = si_mult_sat(tp, a.smin, b.smin);
253 1.669 rillig int64_t lu = si_mult_sat(tp, a.smin, b.smax);
254 1.669 rillig int64_t ul = si_mult_sat(tp, a.smax, b.smin);
255 1.669 rillig int64_t uu = si_mult_sat(tp, a.smax, b.smax);
256 1.669 rillig
257 1.669 rillig c.smin = s64_min(ll, s64_min(lu, s64_min(ul, uu)));
258 1.669 rillig c.smax = s64_max(ll, s64_max(lu, s64_max(ul, uu)));
259 1.669 rillig c.umin = c.smin >= 0 ? (uint64_t)c.smin : 0;
260 1.669 rillig c.umax = c.smin >= 0 ? (uint64_t)c.smax : UINT64_MAX;
261 1.669 rillig c.bclr = ~u64_fill_right(c.umax);
262 1.669 rillig return c;
263 1.669 rillig }
264 1.669 rillig
265 1.669 rillig if (a.umax > 0 && b.umax > ic_any(tp).umax / a.umax)
266 1.653 rillig return ic_any(tp);
267 1.653 rillig
268 1.653 rillig c.smin = INT64_MIN;
269 1.653 rillig c.smax = INT64_MAX;
270 1.653 rillig c.umin = a.umin * b.umin;
271 1.653 rillig c.umax = a.umax * b.umax;
272 1.653 rillig c.bclr = ~u64_fill_right(c.umax);
273 1.653 rillig return c;
274 1.653 rillig }
275 1.653 rillig
276 1.653 rillig static integer_constraints
277 1.655 rillig ic_div(const type_t *tp, integer_constraints a, integer_constraints b)
278 1.444 rillig {
279 1.669 rillig if (ic_maybe_signed_binary(tp, a, b)) {
280 1.669 rillig if (b.smin >= 0)
281 1.669 rillig return a;
282 1.655 rillig return ic_any(tp);
283 1.669 rillig }
284 1.444 rillig
285 1.667 rillig integer_constraints c;
286 1.444 rillig c.smin = INT64_MIN;
287 1.444 rillig c.smax = INT64_MAX;
288 1.667 rillig c.umin = a.umin / u64_max(b.umax, 1);
289 1.667 rillig c.umax = a.umax / u64_max(b.umin, 1);
290 1.655 rillig c.bclr = ~u64_fill_right(c.umax);
291 1.444 rillig return c;
292 1.444 rillig }
293 1.444 rillig
294 1.444 rillig static integer_constraints
295 1.667 rillig ic_mod(const type_t *tp, integer_constraints a, integer_constraints b)
296 1.656 rillig {
297 1.667 rillig if (ic_maybe_signed_binary(tp, a, b)) {
298 1.667 rillig uint64_t max_abs_b = u64_max(s64_abs(b.smin), s64_abs(b.smax));
299 1.667 rillig if (max_abs_b >> 63 != 0 || max_abs_b == 0)
300 1.667 rillig return a;
301 1.656 rillig
302 1.667 rillig integer_constraints c;
303 1.667 rillig c.smin = s64_max(a.smin, -(int64_t)(max_abs_b - 1));
304 1.667 rillig c.smax = s64_min(a.smax, (int64_t)(max_abs_b - 1));
305 1.667 rillig c.umin = 0;
306 1.667 rillig c.umax = UINT64_MAX;
307 1.667 rillig c.bclr = 0;
308 1.667 rillig return c;
309 1.667 rillig }
310 1.656 rillig
311 1.472 rillig integer_constraints c;
312 1.472 rillig c.smin = INT64_MIN;
313 1.472 rillig c.smax = INT64_MAX;
314 1.472 rillig c.umin = 0;
315 1.472 rillig c.umax = b.umax - 1;
316 1.472 rillig c.bclr = ~u64_fill_right(c.umax);
317 1.472 rillig return c;
318 1.472 rillig }
319 1.472 rillig
320 1.472 rillig static integer_constraints
321 1.665 rillig ic_plus(const type_t *tp, integer_constraints a, integer_constraints b)
322 1.665 rillig {
323 1.667 rillig if (ic_maybe_signed_binary(tp, a, b)) {
324 1.666 rillig integer_constraints c;
325 1.666 rillig c.smin = si_plus_sat(tp, a.smin, b.smin);
326 1.666 rillig c.smax = si_plus_sat(tp, a.smax, b.smax);
327 1.667 rillig c.umin = c.smin >= 0 ? (uint64_t)c.smin : 0;
328 1.667 rillig c.umax = c.smin >= 0 ? (uint64_t)c.smax : UINT64_MAX;
329 1.666 rillig c.bclr = 0;
330 1.666 rillig return c;
331 1.666 rillig }
332 1.665 rillig
333 1.666 rillig uint64_t max = ui_max_value(tp);
334 1.665 rillig integer_constraints c;
335 1.666 rillig c.smin = INT64_MIN;
336 1.666 rillig c.smax = INT64_MAX;
337 1.666 rillig if (b.umax <= max - a.umax) {
338 1.666 rillig c.umin = a.umin + b.umin;
339 1.666 rillig c.umax = a.umax + b.umax;
340 1.666 rillig } else {
341 1.666 rillig c.umin = 0;
342 1.666 rillig c.umax = max;
343 1.666 rillig }
344 1.667 rillig if (c.umax >> 63 == 0) {
345 1.667 rillig c.smin = 0;
346 1.667 rillig c.smax = (int64_t)c.umax;
347 1.667 rillig }
348 1.666 rillig c.bclr = ~u64_fill_right(c.umax);
349 1.666 rillig return c;
350 1.666 rillig }
351 1.666 rillig
352 1.666 rillig static integer_constraints
353 1.666 rillig ic_minus(const type_t *tp, integer_constraints a, integer_constraints b)
354 1.666 rillig {
355 1.666 rillig integer_constraints c;
356 1.666 rillig c.smin = si_minus_sat(tp, a.smin, b.smax);
357 1.666 rillig c.smax = si_minus_sat(tp, a.smax, b.smin);
358 1.667 rillig
359 1.667 rillig if (ic_maybe_signed_binary(tp, a, b)) {
360 1.667 rillig c.umin = c.smin >= 0 ? (uint64_t)c.smin : 0;
361 1.667 rillig c.umax = c.smin >= 0 ? (uint64_t)c.smax : UINT64_MAX;
362 1.667 rillig } else if (a.umin >= b.umax) {
363 1.666 rillig c.umin = a.umin - b.umax;
364 1.666 rillig c.umax = a.umax - b.umin;
365 1.666 rillig } else {
366 1.666 rillig c.umin = 0;
367 1.666 rillig c.umax = is_uinteger(tp->t_tspec) ? ui_max_value(tp)
368 1.666 rillig : UINT64_MAX;
369 1.666 rillig }
370 1.665 rillig c.bclr = ~u64_fill_right(c.umax);
371 1.665 rillig return c;
372 1.665 rillig }
373 1.665 rillig
374 1.665 rillig static integer_constraints
375 1.444 rillig ic_shl(const type_t *tp, integer_constraints a, integer_constraints b)
376 1.444 rillig {
377 1.667 rillig if (ic_maybe_signed(tp, a))
378 1.449 rillig return ic_any(tp);
379 1.449 rillig
380 1.667 rillig unsigned amount;
381 1.444 rillig if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
382 1.667 rillig amount = (unsigned)b.smin;
383 1.444 rillig else if (b.umin == b.umax && b.umin < 64)
384 1.667 rillig amount = (unsigned)b.umin;
385 1.444 rillig else
386 1.444 rillig return ic_any(tp);
387 1.444 rillig
388 1.667 rillig integer_constraints c;
389 1.667 rillig c.umin = a.umin << amount;
390 1.667 rillig c.umax = a.umax << amount;
391 1.667 rillig if (c.umax >> (width_in_bits(tp) - 1) == 0) {
392 1.667 rillig c.smin = (int64_t)c.umin;
393 1.667 rillig c.smax = (int64_t)c.umax;
394 1.667 rillig } else {
395 1.667 rillig c.smin = INT64_MIN;
396 1.667 rillig c.smax = INT64_MAX;
397 1.667 rillig }
398 1.444 rillig c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1);
399 1.444 rillig return c;
400 1.444 rillig }
401 1.444 rillig
402 1.444 rillig static integer_constraints
403 1.449 rillig ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
404 1.449 rillig {
405 1.667 rillig if (ic_maybe_signed(tp, a))
406 1.449 rillig return ic_any(tp);
407 1.449 rillig
408 1.667 rillig unsigned amount;
409 1.449 rillig if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
410 1.667 rillig amount = (unsigned)b.smin;
411 1.449 rillig else if (b.umin == b.umax && b.umin < 64)
412 1.667 rillig amount = (unsigned)b.umin;
413 1.449 rillig else
414 1.449 rillig return ic_any(tp);
415 1.449 rillig
416 1.667 rillig integer_constraints c;
417 1.667 rillig c.smin = s64_shr(a.smin, amount);
418 1.667 rillig c.smax = s64_shr(a.smax, amount);
419 1.667 rillig c.umin = a.umin >> amount;
420 1.667 rillig c.umax = a.umax >> amount;
421 1.449 rillig c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
422 1.449 rillig return c;
423 1.449 rillig }
424 1.449 rillig
425 1.449 rillig static integer_constraints
426 1.655 rillig ic_bitand(integer_constraints a, integer_constraints b)
427 1.655 rillig {
428 1.655 rillig integer_constraints c;
429 1.667 rillig c.smin = a.smin & b.smin;
430 1.667 rillig c.smax = a.smax & b.smax;
431 1.667 rillig c.umin = a.umin & b.umin;
432 1.667 rillig c.umax = a.umax & b.umax;
433 1.655 rillig c.bclr = a.bclr | b.bclr;
434 1.655 rillig return c;
435 1.655 rillig }
436 1.655 rillig
437 1.655 rillig static integer_constraints
438 1.666 rillig ic_bitxor(const type_t *tp, integer_constraints a, integer_constraints b)
439 1.666 rillig {
440 1.667 rillig if (ic_maybe_signed_binary(tp, a, b))
441 1.667 rillig return ic_any(tp);
442 1.667 rillig
443 1.667 rillig integer_constraints c;
444 1.667 rillig c.smin = a.smin & b.smin;
445 1.667 rillig c.smax = a.smax | b.smax;
446 1.667 rillig c.umin = a.umin & b.umin;
447 1.666 rillig c.umax = a.umax | b.umax;
448 1.666 rillig c.bclr = a.bclr & b.bclr;
449 1.666 rillig return c;
450 1.666 rillig }
451 1.666 rillig
452 1.666 rillig static integer_constraints
453 1.655 rillig ic_bitor(integer_constraints a, integer_constraints b)
454 1.655 rillig {
455 1.655 rillig integer_constraints c;
456 1.667 rillig c.smin = a.smin | b.smin;
457 1.667 rillig c.smax = a.smax | b.smax;
458 1.667 rillig c.umin = a.umin | b.umin;
459 1.667 rillig c.umax = a.umax | b.umax;
460 1.655 rillig c.bclr = a.bclr & b.bclr;
461 1.655 rillig return c;
462 1.655 rillig }
463 1.655 rillig
464 1.655 rillig static integer_constraints
465 1.655 rillig ic_quest_colon(integer_constraints a, integer_constraints b)
466 1.521 rillig {
467 1.521 rillig integer_constraints c;
468 1.667 rillig c.smin = s64_min(a.smin, b.smin);
469 1.667 rillig c.smax = s64_max(a.smax, b.smax);
470 1.667 rillig c.umin = u64_min(a.umin, b.umin);
471 1.667 rillig c.umax = u64_max(a.umax, b.umax);
472 1.521 rillig c.bclr = a.bclr & b.bclr;
473 1.521 rillig return c;
474 1.521 rillig }
475 1.521 rillig
476 1.521 rillig static integer_constraints
477 1.655 rillig ic_con(const type_t *tp, const val_t *v)
478 1.655 rillig {
479 1.655 rillig lint_assert(is_integer(tp->t_tspec));
480 1.655 rillig int64_t si = v->u.integer;
481 1.655 rillig uint64_t ui = (uint64_t)si;
482 1.667 rillig
483 1.667 rillig integer_constraints c;
484 1.655 rillig c.smin = si;
485 1.655 rillig c.smax = si;
486 1.655 rillig c.umin = ui;
487 1.655 rillig c.umax = ui;
488 1.655 rillig c.bclr = ~ui;
489 1.655 rillig return c;
490 1.655 rillig }
491 1.655 rillig
492 1.655 rillig static integer_constraints
493 1.655 rillig ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
494 1.655 rillig {
495 1.668 rillig unsigned new_width = width_in_bits(ntp);
496 1.668 rillig unsigned old_width = width_in_bits(otp);
497 1.668 rillig bool new_unsigned = is_uinteger(ntp->t_tspec);
498 1.668 rillig bool old_unsigned = is_uinteger(otp->t_tspec);
499 1.655 rillig
500 1.668 rillig if (new_width >= old_width && new_unsigned == old_unsigned)
501 1.655 rillig return a;
502 1.668 rillig if (new_width > old_width && old_unsigned)
503 1.655 rillig return a;
504 1.668 rillig if (new_unsigned && (~value_bits(new_width) & ~a.bclr) == 0)
505 1.667 rillig return a;
506 1.655 rillig return ic_any(ntp);
507 1.655 rillig }
508 1.655 rillig
509 1.655 rillig static integer_constraints
510 1.444 rillig ic_expr(const tnode_t *tn)
511 1.444 rillig {
512 1.444 rillig integer_constraints lc, rc;
513 1.444 rillig
514 1.451 rillig lint_assert(is_integer(tn->tn_type->t_tspec));
515 1.451 rillig
516 1.444 rillig switch (tn->tn_op) {
517 1.653 rillig case MULT:
518 1.670 rillig lc = ic_expr(tn->u.ops.left);
519 1.670 rillig rc = ic_expr(tn->u.ops.right);
520 1.653 rillig return ic_mult(tn->tn_type, lc, rc);
521 1.650 rillig case DIV:
522 1.670 rillig lc = ic_expr(tn->u.ops.left);
523 1.670 rillig rc = ic_expr(tn->u.ops.right);
524 1.650 rillig return ic_div(tn->tn_type, lc, rc);
525 1.472 rillig case MOD:
526 1.670 rillig lc = ic_expr(tn->u.ops.left);
527 1.670 rillig rc = ic_expr(tn->u.ops.right);
528 1.472 rillig return ic_mod(tn->tn_type, lc, rc);
529 1.665 rillig case PLUS:
530 1.670 rillig lc = ic_expr(tn->u.ops.left);
531 1.670 rillig rc = ic_expr(tn->u.ops.right);
532 1.665 rillig return ic_plus(tn->tn_type, lc, rc);
533 1.666 rillig case MINUS:
534 1.668 rillig if (tn->u.ops.left->tn_type->t_tspec == PTR)
535 1.668 rillig return ic_any(tn->tn_type);
536 1.670 rillig lc = ic_expr(tn->u.ops.left);
537 1.670 rillig rc = ic_expr(tn->u.ops.right);
538 1.666 rillig return ic_minus(tn->tn_type, lc, rc);
539 1.444 rillig case SHL:
540 1.612 rillig lc = ic_expr(tn->u.ops.left);
541 1.612 rillig rc = ic_expr(tn->u.ops.right);
542 1.444 rillig return ic_shl(tn->tn_type, lc, rc);
543 1.449 rillig case SHR:
544 1.612 rillig lc = ic_expr(tn->u.ops.left);
545 1.612 rillig rc = ic_expr(tn->u.ops.right);
546 1.449 rillig return ic_shr(tn->tn_type, lc, rc);
547 1.444 rillig case BITAND:
548 1.612 rillig lc = ic_expr(tn->u.ops.left);
549 1.612 rillig rc = ic_expr(tn->u.ops.right);
550 1.444 rillig return ic_bitand(lc, rc);
551 1.666 rillig case BITXOR:
552 1.666 rillig lc = ic_expr(tn->u.ops.left);
553 1.666 rillig rc = ic_expr(tn->u.ops.right);
554 1.666 rillig return ic_bitxor(tn->tn_type, lc, rc);
555 1.444 rillig case BITOR:
556 1.612 rillig lc = ic_expr(tn->u.ops.left);
557 1.612 rillig rc = ic_expr(tn->u.ops.right);
558 1.444 rillig return ic_bitor(lc, rc);
559 1.521 rillig case QUEST:
560 1.612 rillig lc = ic_expr(tn->u.ops.right->u.ops.left);
561 1.612 rillig rc = ic_expr(tn->u.ops.right->u.ops.right);
562 1.655 rillig return ic_quest_colon(lc, rc);
563 1.655 rillig case CON:
564 1.655 rillig return ic_con(tn->tn_type, &tn->u.value);
565 1.655 rillig case CVT:
566 1.655 rillig if (!is_integer(tn->u.ops.left->tn_type->t_tspec))
567 1.655 rillig return ic_any(tn->tn_type);
568 1.655 rillig lc = ic_expr(tn->u.ops.left);
569 1.655 rillig return ic_cvt(tn->tn_type, tn->u.ops.left->tn_type, lc);
570 1.444 rillig default:
571 1.444 rillig return ic_any(tn->tn_type);
572 1.444 rillig }
573 1.444 rillig }
574 1.444 rillig
575 1.608 rillig uint64_t
576 1.608 rillig possible_bits(const tnode_t *tn)
577 1.608 rillig {
578 1.608 rillig return ~ic_expr(tn).bclr;
579 1.608 rillig }
580 1.608 rillig
581 1.648 rillig bool
582 1.648 rillig attributes_contain(const attribute_list *attrs, const char *str)
583 1.648 rillig {
584 1.648 rillig for (size_t i = 0, n = attrs->len; i < n; i++) {
585 1.648 rillig const attribute *attr = attrs->attrs + i;
586 1.648 rillig if (attr->prefix == NULL && strcmp(attr->name, str) == 0)
587 1.648 rillig return true;
588 1.648 rillig }
589 1.648 rillig return false;
590 1.648 rillig }
591 1.648 rillig
592 1.257 rillig /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
593 1.1 cgd type_t *
594 1.406 rillig block_derive_type(type_t *tp, tspec_t t)
595 1.1 cgd {
596 1.1 cgd
597 1.605 rillig type_t *tp2 = block_zero_alloc(sizeof(*tp2), "type");
598 1.1 cgd tp2->t_tspec = t;
599 1.1 cgd tp2->t_subt = tp;
600 1.95 rillig return tp2;
601 1.1 cgd }
602 1.1 cgd
603 1.1 cgd /*
604 1.365 rillig * Derive 'pointer to tp' or 'function returning tp'.
605 1.365 rillig * The memory is freed at the end of the current expression.
606 1.1 cgd */
607 1.1 cgd type_t *
608 1.257 rillig expr_derive_type(type_t *tp, tspec_t t)
609 1.1 cgd {
610 1.1 cgd
611 1.605 rillig type_t *tp2 = expr_zero_alloc(sizeof(*tp2), "type");
612 1.1 cgd tp2->t_tspec = t;
613 1.1 cgd tp2->t_subt = tp;
614 1.95 rillig return tp2;
615 1.1 cgd }
616 1.1 cgd
617 1.683 rillig static const char *
618 1.683 rillig function_call_descr(const function_call *call)
619 1.683 rillig {
620 1.683 rillig if ((call->func->tn_op == ADDR || call->func->tn_op == LOAD)
621 1.683 rillig && call->func->u.ops.left->tn_op == NAME)
622 1.683 rillig return call->func->u.ops.left->u.sym->s_name;
623 1.683 rillig return type_name(call->func->tn_type->t_subt);
624 1.683 rillig }
625 1.683 rillig
626 1.686 rillig static size_t
627 1.686 rillig str_len(const tnode_t *tn)
628 1.686 rillig {
629 1.686 rillig const buffer *buf = tn->u.str_literals;
630 1.686 rillig if (tn->tn_type->t_subt->t_tspec != CHAR)
631 1.686 rillig return buf->len;
632 1.686 rillig quoted_iterator it = { .end = 0 };
633 1.686 rillig size_t len = 0;
634 1.686 rillig while (quoted_next(buf, &it))
635 1.686 rillig len++;
636 1.686 rillig return len;
637 1.686 rillig }
638 1.686 rillig
639 1.564 rillig /* Create an expression from a unary or binary operator and its operands. */
640 1.506 rillig static tnode_t *
641 1.565 rillig build_op(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
642 1.506 rillig {
643 1.506 rillig
644 1.506 rillig tnode_t *ntn = expr_alloc_tnode();
645 1.506 rillig ntn->tn_op = op;
646 1.506 rillig ntn->tn_type = type;
647 1.506 rillig ntn->tn_sys = sys;
648 1.612 rillig ntn->u.ops.left = ln;
649 1.612 rillig ntn->u.ops.right = rn;
650 1.506 rillig
651 1.506 rillig if (op == INDIR || op == FSEL) {
652 1.506 rillig lint_assert(ln->tn_type->t_tspec == PTR);
653 1.506 rillig tspec_t t = ln->tn_type->t_subt->t_tspec;
654 1.566 rillig ntn->tn_lvalue = t != FUNC && t != VOID;
655 1.506 rillig }
656 1.506 rillig
657 1.506 rillig return ntn;
658 1.506 rillig }
659 1.506 rillig
660 1.1 cgd tnode_t *
661 1.318 rillig build_constant(type_t *tp, val_t *v)
662 1.1 cgd {
663 1.1 cgd
664 1.536 rillig tnode_t *n = expr_alloc_tnode();
665 1.1 cgd n->tn_op = CON;
666 1.1 cgd n->tn_type = tp;
667 1.612 rillig n->u.value = *v;
668 1.612 rillig n->u.value.v_tspec = tp->t_tspec;
669 1.1 cgd free(v);
670 1.95 rillig return n;
671 1.1 cgd }
672 1.1 cgd
673 1.1 cgd static tnode_t *
674 1.544 rillig build_integer_constant(tspec_t t, int64_t si)
675 1.1 cgd {
676 1.1 cgd
677 1.536 rillig tnode_t *n = expr_alloc_tnode();
678 1.1 cgd n->tn_op = CON;
679 1.1 cgd n->tn_type = gettyp(t);
680 1.612 rillig n->u.value.v_tspec = t;
681 1.612 rillig n->u.value.v_unsigned_since_c90 = false;
682 1.612 rillig n->u.value.v_char_constant = false;
683 1.612 rillig n->u.value.u.integer = si;
684 1.95 rillig return n;
685 1.1 cgd }
686 1.1 cgd
687 1.167 rillig static void
688 1.167 rillig fallback_symbol(sym_t *sym)
689 1.167 rillig {
690 1.167 rillig
691 1.493 rillig if (Tflag && fallback_symbol_strict_bool(sym))
692 1.167 rillig return;
693 1.167 rillig
694 1.645 rillig if (funcsym != NULL && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
695 1.167 rillig strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
696 1.167 rillig /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
697 1.167 rillig gnuism(316);
698 1.645 rillig goto return_function_name;
699 1.167 rillig }
700 1.167 rillig
701 1.591 rillig if (funcsym != NULL && strcmp(sym->s_name, "__func__") == 0) {
702 1.437 rillig if (!allow_c99)
703 1.409 rillig /* __func__ is a C99 feature */
704 1.167 rillig warning(317);
705 1.503 rillig /* C11 6.4.2.2 */
706 1.645 rillig return_function_name:
707 1.504 rillig sym->s_type = block_derive_type(gettyp(CHAR), ARRAY);
708 1.167 rillig sym->s_type->t_const = true;
709 1.611 rillig sym->s_type->u.dimension = (int)strlen(funcsym->s_name) + 1;
710 1.167 rillig return;
711 1.167 rillig }
712 1.167 rillig
713 1.251 rillig /* '%s' undefined */
714 1.167 rillig error(99, sym->s_name);
715 1.167 rillig }
716 1.167 rillig
717 1.358 rillig /*
718 1.361 rillig * Functions that are predeclared by GCC or other compilers can be called
719 1.361 rillig * with arbitrary arguments. Since lint usually runs after a successful
720 1.361 rillig * compilation, it's the compiler's job to catch any errors.
721 1.358 rillig */
722 1.358 rillig bool
723 1.361 rillig is_compiler_builtin(const char *name)
724 1.303 rillig {
725 1.361 rillig /* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */
726 1.429 rillig if (allow_gcc) {
727 1.362 rillig if (strncmp(name, "__atomic_", 9) == 0 ||
728 1.362 rillig strncmp(name, "__builtin_", 10) == 0 ||
729 1.400 rillig strcmp(name, "alloca") == 0 ||
730 1.362 rillig /* obsolete but still in use, as of 2021 */
731 1.362 rillig strncmp(name, "__sync_", 7) == 0)
732 1.362 rillig return true;
733 1.362 rillig }
734 1.361 rillig
735 1.361 rillig /* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */
736 1.361 rillig if (strncmp(name, "_mm_", 4) == 0)
737 1.361 rillig return true;
738 1.361 rillig
739 1.361 rillig return false;
740 1.303 rillig }
741 1.303 rillig
742 1.667 rillig static bool
743 1.667 rillig str_ends_with(const char *haystack, const char *needle)
744 1.667 rillig {
745 1.667 rillig size_t hlen = strlen(haystack);
746 1.667 rillig size_t nlen = strlen(needle);
747 1.667 rillig
748 1.667 rillig return nlen <= hlen &&
749 1.667 rillig memcmp(haystack + hlen - nlen, needle, nlen) == 0;
750 1.667 rillig }
751 1.667 rillig
752 1.372 rillig /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */
753 1.372 rillig static bool
754 1.372 rillig is_gcc_bool_builtin(const char *name)
755 1.372 rillig {
756 1.372 rillig return strncmp(name, "__builtin_", 10) == 0 &&
757 1.584 rillig (str_ends_with(name, "_overflow") ||
758 1.536 rillig str_ends_with(name, "_overflow_p"));
759 1.372 rillig }
760 1.372 rillig
761 1.371 rillig static void
762 1.371 rillig build_name_call(sym_t *sym)
763 1.371 rillig {
764 1.371 rillig
765 1.371 rillig if (is_compiler_builtin(sym->s_name)) {
766 1.371 rillig /*
767 1.586 rillig * Do not warn about these, just assume that they are regular
768 1.586 rillig * functions compatible with non-prototype calling conventions.
769 1.371 rillig */
770 1.429 rillig if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
771 1.372 rillig sym->s_type = gettyp(BOOL);
772 1.604 rillig } else if (allow_c99)
773 1.371 rillig /* function '%s' implicitly declared to return int */
774 1.371 rillig error(215, sym->s_name);
775 1.604 rillig else if (!allow_trad)
776 1.371 rillig /* function '%s' implicitly declared to return int */
777 1.371 rillig warning(215, sym->s_name);
778 1.371 rillig
779 1.438 rillig /* XXX if !allow_c90, the symbol should be exported to level 0 */
780 1.406 rillig sym->s_type = block_derive_type(sym->s_type, FUNC);
781 1.371 rillig }
782 1.371 rillig
783 1.402 rillig /* Create a node for a name (symbol table entry). */
784 1.1 cgd tnode_t *
785 1.399 rillig build_name(sym_t *sym, bool is_funcname)
786 1.1 cgd {
787 1.1 cgd
788 1.582 rillig if (sym->s_scl == NO_SCL && !in_gcc_attribute) {
789 1.1 cgd sym->s_scl = EXTERN;
790 1.1 cgd sym->s_def = DECL;
791 1.399 rillig if (is_funcname)
792 1.371 rillig build_name_call(sym);
793 1.399 rillig else
794 1.167 rillig fallback_symbol(sym);
795 1.1 cgd }
796 1.1 cgd
797 1.597 rillig lint_assert(sym->s_kind == SK_VCFT || sym->s_kind == SK_MEMBER);
798 1.1 cgd
799 1.536 rillig tnode_t *n = expr_alloc_tnode();
800 1.1 cgd n->tn_type = sym->s_type;
801 1.422 rillig if (sym->s_scl == BOOL_CONST) {
802 1.401 rillig n->tn_op = CON;
803 1.612 rillig n->u.value.v_tspec = BOOL;
804 1.612 rillig n->u.value.v_unsigned_since_c90 = false;
805 1.612 rillig n->u.value.v_char_constant = false;
806 1.612 rillig n->u.value.u.integer = sym->u.s_bool_constant ? 1 : 0;
807 1.422 rillig } else if (sym->s_scl == ENUM_CONST) {
808 1.422 rillig n->tn_op = CON;
809 1.612 rillig n->u.value.v_tspec = INT; /* ENUM is in n->tn_type */
810 1.612 rillig n->u.value.v_unsigned_since_c90 = false;
811 1.612 rillig n->u.value.v_char_constant = false;
812 1.612 rillig n->u.value.u.integer = sym->u.s_enum_constant;
813 1.401 rillig } else {
814 1.1 cgd n->tn_op = NAME;
815 1.612 rillig n->u.sym = sym;
816 1.597 rillig if (sym->s_kind == SK_VCFT && sym->s_type->t_tspec != FUNC)
817 1.154 rillig n->tn_lvalue = true;
818 1.1 cgd }
819 1.1 cgd
820 1.95 rillig return n;
821 1.1 cgd }
822 1.1 cgd
823 1.1 cgd tnode_t *
824 1.602 rillig build_string(buffer *lit)
825 1.1 cgd {
826 1.602 rillig size_t value_len = lit->len;
827 1.602 rillig if (lit->data != NULL) {
828 1.609 rillig quoted_iterator it = { .end = 0 };
829 1.602 rillig for (value_len = 0; quoted_next(lit, &it); value_len++)
830 1.602 rillig continue;
831 1.602 rillig }
832 1.1 cgd
833 1.568 rillig type_t *tp = expr_zero_alloc(sizeof(*tp), "type");
834 1.365 rillig tp->t_tspec = ARRAY;
835 1.602 rillig tp->t_subt = gettyp(lit->data != NULL ? CHAR : WCHAR_TSPEC);
836 1.611 rillig tp->u.dimension = (int)(value_len + 1);
837 1.365 rillig
838 1.536 rillig tnode_t *n = expr_alloc_tnode();
839 1.1 cgd n->tn_op = STRING;
840 1.365 rillig n->tn_type = tp;
841 1.154 rillig n->tn_lvalue = true;
842 1.1 cgd
843 1.612 rillig n->u.str_literals = expr_zero_alloc(sizeof(*n->u.str_literals), "tnode.string");
844 1.612 rillig n->u.str_literals->len = lit->len;
845 1.1 cgd
846 1.602 rillig if (lit->data != NULL) {
847 1.612 rillig n->u.str_literals->data = expr_zero_alloc(lit->len + 1,
848 1.599 rillig "tnode.string.data");
849 1.612 rillig (void)memcpy(n->u.str_literals->data, lit->data, lit->len + 1);
850 1.602 rillig free(lit->data);
851 1.599 rillig }
852 1.602 rillig free(lit);
853 1.1 cgd
854 1.95 rillig return n;
855 1.1 cgd }
856 1.1 cgd
857 1.291 rillig tnode_t *
858 1.291 rillig build_generic_selection(const tnode_t *expr,
859 1.313 rillig struct generic_association *sel)
860 1.291 rillig {
861 1.291 rillig tnode_t *default_result = NULL;
862 1.291 rillig
863 1.411 rillig for (; sel != NULL; sel = sel->ga_prev) {
864 1.292 rillig if (expr != NULL &&
865 1.479 rillig types_compatible(sel->ga_arg, expr->tn_type,
866 1.479 rillig false, false, NULL))
867 1.313 rillig return sel->ga_result;
868 1.684 rillig if (sel->ga_arg == NULL)
869 1.313 rillig default_result = sel->ga_result;
870 1.411 rillig }
871 1.291 rillig return default_result;
872 1.291 rillig }
873 1.291 rillig
874 1.506 rillig static bool
875 1.506 rillig is_out_of_char_range(const tnode_t *tn)
876 1.506 rillig {
877 1.506 rillig return tn->tn_op == CON &&
878 1.612 rillig !tn->u.value.v_char_constant &&
879 1.612 rillig !(0 <= tn->u.value.u.integer &&
880 1.612 rillig tn->u.value.u.integer < 1 << (CHAR_SIZE - 1));
881 1.506 rillig }
882 1.506 rillig
883 1.583 rillig static bool
884 1.583 rillig check_nonportable_char_comparison(op_t op,
885 1.583 rillig const tnode_t *ln, tspec_t lt,
886 1.583 rillig const tnode_t *rn, tspec_t rt)
887 1.583 rillig {
888 1.583 rillig if (!(hflag || pflag))
889 1.583 rillig return true;
890 1.583 rillig
891 1.583 rillig if (lt == CHAR && is_out_of_char_range(rn)) {
892 1.583 rillig char buf[128];
893 1.583 rillig (void)snprintf(buf, sizeof(buf), "%s %d",
894 1.612 rillig op_name(op), (int)rn->u.value.u.integer);
895 1.583 rillig /* nonportable character comparison '%s' */
896 1.583 rillig warning(230, buf);
897 1.583 rillig return false;
898 1.583 rillig }
899 1.583 rillig if (rt == CHAR && is_out_of_char_range(ln)) {
900 1.583 rillig char buf[128];
901 1.583 rillig (void)snprintf(buf, sizeof(buf), "%d %s ?",
902 1.612 rillig (int)ln->u.value.u.integer, op_name(op));
903 1.583 rillig /* nonportable character comparison '%s' */
904 1.583 rillig warning(230, buf);
905 1.583 rillig return false;
906 1.583 rillig }
907 1.583 rillig return true;
908 1.583 rillig }
909 1.583 rillig
910 1.506 rillig static void
911 1.506 rillig check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
912 1.1 cgd {
913 1.506 rillig
914 1.536 rillig tspec_t lt = ln->tn_type->t_tspec;
915 1.536 rillig tspec_t rt = rn->tn_type->t_tspec;
916 1.1 cgd
917 1.506 rillig if (ln->tn_op != CON && rn->tn_op != CON)
918 1.506 rillig return;
919 1.1 cgd
920 1.506 rillig if (!is_integer(lt) || !is_integer(rt))
921 1.506 rillig return;
922 1.1 cgd
923 1.662 rillig if (any_query_enabled && !in_system_header) {
924 1.529 rillig if (lt == CHAR && rn->tn_op == CON &&
925 1.612 rillig !rn->u.value.v_char_constant) {
926 1.529 rillig /* comparison '%s' of 'char' with plain integer %d */
927 1.529 rillig query_message(14,
928 1.612 rillig op_name(op), (int)rn->u.value.u.integer);
929 1.529 rillig }
930 1.529 rillig if (rt == CHAR && ln->tn_op == CON &&
931 1.612 rillig !ln->u.value.v_char_constant) {
932 1.529 rillig /* comparison '%s' of 'char' with plain integer %d */
933 1.529 rillig query_message(14,
934 1.612 rillig op_name(op), (int)ln->u.value.u.integer);
935 1.529 rillig }
936 1.529 rillig }
937 1.529 rillig
938 1.583 rillig if (!check_nonportable_char_comparison(op, ln, lt, rn, rt))
939 1.583 rillig return;
940 1.1 cgd
941 1.506 rillig if (is_uinteger(lt) && !is_uinteger(rt) &&
942 1.612 rillig rn->tn_op == CON && rn->u.value.u.integer <= 0) {
943 1.612 rillig if (rn->u.value.u.integer < 0) {
944 1.506 rillig /* operator '%s' compares '%s' with '%s' */
945 1.506 rillig warning(162, op_name(op),
946 1.690 rillig expr_type_name(ln), "negative constant");
947 1.604 rillig } else if (op == LT || op == GE)
948 1.506 rillig /* operator '%s' compares '%s' with '%s' */
949 1.690 rillig warning(162, op_name(op), expr_type_name(ln), "0");
950 1.506 rillig return;
951 1.1 cgd }
952 1.506 rillig if (is_uinteger(rt) && !is_uinteger(lt) &&
953 1.612 rillig ln->tn_op == CON && ln->u.value.u.integer <= 0) {
954 1.612 rillig if (ln->u.value.u.integer < 0) {
955 1.506 rillig /* operator '%s' compares '%s' with '%s' */
956 1.506 rillig warning(162, op_name(op),
957 1.690 rillig "negative constant", expr_type_name(rn));
958 1.604 rillig } else if (op == GT || op == LE)
959 1.506 rillig /* operator '%s' compares '%s' with '%s' */
960 1.690 rillig warning(162, op_name(op), "0", expr_type_name(rn));
961 1.506 rillig return;
962 1.1 cgd }
963 1.506 rillig }
964 1.506 rillig
965 1.506 rillig static const tspec_t arith_rank[] = {
966 1.506 rillig LDOUBLE, DOUBLE, FLOAT,
967 1.506 rillig #ifdef INT128_SIZE
968 1.506 rillig UINT128, INT128,
969 1.506 rillig #endif
970 1.545 rillig ULLONG, LLONG,
971 1.506 rillig ULONG, LONG,
972 1.506 rillig UINT, INT,
973 1.506 rillig };
974 1.506 rillig
975 1.506 rillig /* Keep unsigned in traditional C */
976 1.506 rillig static tspec_t
977 1.506 rillig usual_arithmetic_conversion_trad(tspec_t lt, tspec_t rt)
978 1.506 rillig {
979 1.1 cgd
980 1.506 rillig size_t i;
981 1.506 rillig for (i = 0; arith_rank[i] != INT; i++)
982 1.506 rillig if (lt == arith_rank[i] || rt == arith_rank[i])
983 1.506 rillig break;
984 1.1 cgd
985 1.506 rillig tspec_t t = arith_rank[i];
986 1.506 rillig if (is_uinteger(lt) || is_uinteger(rt))
987 1.506 rillig if (is_integer(t) && !is_uinteger(t))
988 1.506 rillig return unsigned_type(t);
989 1.506 rillig return t;
990 1.506 rillig }
991 1.1 cgd
992 1.506 rillig static tspec_t
993 1.506 rillig usual_arithmetic_conversion_c90(tspec_t lt, tspec_t rt)
994 1.506 rillig {
995 1.1 cgd
996 1.506 rillig if (lt == rt)
997 1.506 rillig return lt;
998 1.1 cgd
999 1.506 rillig if (lt == LCOMPLEX || rt == LCOMPLEX)
1000 1.506 rillig return LCOMPLEX;
1001 1.506 rillig if (lt == DCOMPLEX || rt == DCOMPLEX)
1002 1.506 rillig return DCOMPLEX;
1003 1.506 rillig if (lt == FCOMPLEX || rt == FCOMPLEX)
1004 1.506 rillig return FCOMPLEX;
1005 1.506 rillig if (lt == LDOUBLE || rt == LDOUBLE)
1006 1.506 rillig return LDOUBLE;
1007 1.506 rillig if (lt == DOUBLE || rt == DOUBLE)
1008 1.506 rillig return DOUBLE;
1009 1.506 rillig if (lt == FLOAT || rt == FLOAT)
1010 1.506 rillig return FLOAT;
1011 1.1 cgd
1012 1.506 rillig if (size_in_bits(lt) > size_in_bits(rt))
1013 1.506 rillig return lt;
1014 1.506 rillig if (size_in_bits(lt) < size_in_bits(rt))
1015 1.506 rillig return rt;
1016 1.1 cgd
1017 1.506 rillig size_t i;
1018 1.506 rillig for (i = 3; arith_rank[i] != INT; i++)
1019 1.506 rillig if (arith_rank[i] == lt || arith_rank[i] == rt)
1020 1.506 rillig break;
1021 1.506 rillig if ((is_uinteger(lt) || is_uinteger(rt)) &&
1022 1.506 rillig !is_uinteger(arith_rank[i]))
1023 1.506 rillig i--;
1024 1.506 rillig return arith_rank[i];
1025 1.1 cgd }
1026 1.1 cgd
1027 1.506 rillig static tnode_t *
1028 1.506 rillig apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t)
1029 1.317 rillig {
1030 1.506 rillig type_t *ntp = expr_dup_type(tn->tn_type);
1031 1.506 rillig ntp->t_tspec = t;
1032 1.506 rillig if (tn->tn_op != CON) {
1033 1.506 rillig /* usual arithmetic conversion for '%s' from '%s' to '%s' */
1034 1.506 rillig query_message(4, op_name(op),
1035 1.690 rillig expr_type_name(tn), type_name(ntp));
1036 1.506 rillig }
1037 1.506 rillig return convert(op, 0, ntp, tn);
1038 1.317 rillig }
1039 1.317 rillig
1040 1.506 rillig /*
1041 1.506 rillig * Apply the "usual arithmetic conversions" (C99 6.3.1.8), which gives both
1042 1.506 rillig * operands the same type.
1043 1.506 rillig */
1044 1.506 rillig static void
1045 1.506 rillig balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1046 1.314 rillig {
1047 1.314 rillig
1048 1.506 rillig tspec_t lt = (*lnp)->tn_type->t_tspec;
1049 1.506 rillig tspec_t rt = (*rnp)->tn_type->t_tspec;
1050 1.506 rillig if (!is_arithmetic(lt) || !is_arithmetic(rt))
1051 1.506 rillig return;
1052 1.506 rillig
1053 1.506 rillig tspec_t t = allow_c90
1054 1.506 rillig ? usual_arithmetic_conversion_c90(lt, rt)
1055 1.506 rillig : usual_arithmetic_conversion_trad(lt, rt);
1056 1.314 rillig
1057 1.642 rillig if (modtab[op].m_comparison
1058 1.642 rillig && is_integer(lt) && (*lnp)->tn_op != CON
1059 1.642 rillig && is_floating(t) && (*rnp)->tn_op == CON)
1060 1.642 rillig /* comparing integer '%s' to floating point constant %Lg */
1061 1.690 rillig warning(379, expr_type_name(*lnp),
1062 1.642 rillig (*rnp)->u.value.u.floating);
1063 1.642 rillig
1064 1.506 rillig if (t != lt)
1065 1.506 rillig *lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
1066 1.506 rillig if (t != rt)
1067 1.506 rillig *rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
1068 1.626 rillig
1069 1.638 rillig if (is_integer(t)) {
1070 1.638 rillig unsigned lw = width_in_bits((*lnp)->tn_type);
1071 1.638 rillig unsigned rw = width_in_bits((*rnp)->tn_type);
1072 1.638 rillig if (lw < rw)
1073 1.638 rillig *lnp = convert(NOOP, 0, (*rnp)->tn_type, *lnp);
1074 1.638 rillig if (rw < lw)
1075 1.638 rillig *rnp = convert(NOOP, 0, (*lnp)->tn_type, *rnp);
1076 1.638 rillig }
1077 1.314 rillig }
1078 1.314 rillig
1079 1.506 rillig static tnode_t *
1080 1.673 rillig build_address(bool sys, tnode_t *tn)
1081 1.1 cgd {
1082 1.590 rillig /* eliminate '&*' */
1083 1.506 rillig if (tn->tn_op == INDIR &&
1084 1.612 rillig tn->u.ops.left->tn_type->t_tspec == PTR &&
1085 1.612 rillig tn->u.ops.left->tn_type->t_subt == tn->tn_type) {
1086 1.612 rillig return tn->u.ops.left;
1087 1.1 cgd }
1088 1.1 cgd
1089 1.565 rillig return build_op(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
1090 1.506 rillig tn, NULL);
1091 1.1 cgd }
1092 1.1 cgd
1093 1.618 rillig static uint64_t
1094 1.618 rillig fold_unsigned_integer(op_t op, uint64_t l, uint64_t r,
1095 1.618 rillig uint64_t max_value, bool *overflow)
1096 1.151 rillig {
1097 1.618 rillig switch (op) {
1098 1.615 rillig case COMPL:
1099 1.621 rillig return ~l & max_value;
1100 1.506 rillig case UPLUS:
1101 1.620 rillig return +l;
1102 1.506 rillig case UMINUS:
1103 1.621 rillig return -l & max_value;
1104 1.620 rillig case MULT:
1105 1.620 rillig *overflow = r > 0 && l > max_value / r;
1106 1.620 rillig return l * r;
1107 1.506 rillig case DIV:
1108 1.618 rillig if (r == 0) {
1109 1.506 rillig /* division by 0 */
1110 1.506 rillig error(139);
1111 1.618 rillig return max_value;
1112 1.620 rillig }
1113 1.620 rillig return l / r;
1114 1.506 rillig case MOD:
1115 1.618 rillig if (r == 0) {
1116 1.506 rillig /* modulus by 0 */
1117 1.506 rillig error(140);
1118 1.618 rillig return 0;
1119 1.620 rillig }
1120 1.620 rillig return l % r;
1121 1.619 rillig case PLUS:
1122 1.619 rillig *overflow = l > max_value - r;
1123 1.619 rillig return l + r;
1124 1.619 rillig case MINUS:
1125 1.619 rillig *overflow = l < r;
1126 1.619 rillig return l - r;
1127 1.506 rillig case SHL:
1128 1.628 rillig /* TODO: warn about out-of-bounds 'r'. */
1129 1.620 rillig /* TODO: warn about overflow. */
1130 1.618 rillig return l << (r & 63);
1131 1.506 rillig case SHR:
1132 1.628 rillig /* TODO: warn about out-of-bounds 'r'. */
1133 1.618 rillig return l >> (r & 63);
1134 1.618 rillig case LT:
1135 1.618 rillig return l < r ? 1 : 0;
1136 1.618 rillig case LE:
1137 1.618 rillig return l <= r ? 1 : 0;
1138 1.618 rillig case GT:
1139 1.618 rillig return l > r ? 1 : 0;
1140 1.618 rillig case GE:
1141 1.618 rillig return l >= r ? 1 : 0;
1142 1.618 rillig case EQ:
1143 1.618 rillig return l == r ? 1 : 0;
1144 1.618 rillig case NE:
1145 1.618 rillig return l != r ? 1 : 0;
1146 1.618 rillig case BITAND:
1147 1.618 rillig return l & r;
1148 1.618 rillig case BITXOR:
1149 1.618 rillig return l ^ r;
1150 1.618 rillig case BITOR:
1151 1.618 rillig return l | r;
1152 1.618 rillig default:
1153 1.680 rillig lint_assert(false);
1154 1.620 rillig /* NOTREACHED */
1155 1.618 rillig }
1156 1.618 rillig }
1157 1.618 rillig
1158 1.618 rillig static int64_t
1159 1.618 rillig fold_signed_integer(op_t op, int64_t l, int64_t r,
1160 1.618 rillig int64_t min_value, int64_t max_value, bool *overflow)
1161 1.618 rillig {
1162 1.618 rillig switch (op) {
1163 1.618 rillig case COMPL:
1164 1.618 rillig return ~l;
1165 1.618 rillig case UPLUS:
1166 1.620 rillig return +l;
1167 1.618 rillig case UMINUS:
1168 1.618 rillig *overflow = l == min_value;
1169 1.618 rillig return *overflow ? l : -l;
1170 1.618 rillig case MULT:;
1171 1.669 rillig uint64_t al = s64_abs(l);
1172 1.669 rillig uint64_t ar = s64_abs(r);
1173 1.618 rillig bool neg = (l >= 0) != (r >= 0);
1174 1.618 rillig uint64_t max_prod = (uint64_t)max_value + (neg ? 1 : 0);
1175 1.618 rillig if (al > 0 && ar > max_prod / al) {
1176 1.618 rillig *overflow = true;
1177 1.622 rillig return neg ? min_value : max_value;
1178 1.618 rillig }
1179 1.620 rillig return l * r;
1180 1.618 rillig case DIV:
1181 1.618 rillig if (r == 0) {
1182 1.618 rillig /* division by 0 */
1183 1.618 rillig error(139);
1184 1.618 rillig return max_value;
1185 1.618 rillig }
1186 1.618 rillig if (l == min_value && r == -1) {
1187 1.618 rillig *overflow = true;
1188 1.618 rillig return l;
1189 1.618 rillig }
1190 1.618 rillig return l / r;
1191 1.618 rillig case MOD:
1192 1.618 rillig if (r == 0) {
1193 1.618 rillig /* modulus by 0 */
1194 1.618 rillig error(140);
1195 1.618 rillig return 0;
1196 1.618 rillig }
1197 1.618 rillig if (l == min_value && r == -1) {
1198 1.618 rillig *overflow = true;
1199 1.618 rillig return 0;
1200 1.618 rillig }
1201 1.618 rillig return l % r;
1202 1.619 rillig case PLUS:
1203 1.619 rillig if (r > 0 && l > max_value - r) {
1204 1.618 rillig *overflow = true;
1205 1.619 rillig return max_value;
1206 1.619 rillig }
1207 1.619 rillig if (r < 0 && l < min_value - r) {
1208 1.618 rillig *overflow = true;
1209 1.619 rillig return min_value;
1210 1.619 rillig }
1211 1.619 rillig return l + r;
1212 1.619 rillig case MINUS:
1213 1.619 rillig if (r > 0 && l < min_value + r) {
1214 1.618 rillig *overflow = true;
1215 1.619 rillig return min_value;
1216 1.619 rillig }
1217 1.619 rillig if (r < 0 && l > max_value + r) {
1218 1.618 rillig *overflow = true;
1219 1.619 rillig return max_value;
1220 1.619 rillig }
1221 1.619 rillig return l - r;
1222 1.618 rillig case SHL:
1223 1.628 rillig /* TODO: warn about out-of-bounds 'r'. */
1224 1.620 rillig /* TODO: warn about overflow. */
1225 1.692 rillig return (int64_t)((uint64_t)l << (r & 63));
1226 1.621 rillig case SHR:
1227 1.628 rillig /* TODO: warn about out-of-bounds 'r'. */
1228 1.667 rillig return s64_shr(l, r & 63);
1229 1.506 rillig case LT:
1230 1.618 rillig return l < r ? 1 : 0;
1231 1.506 rillig case LE:
1232 1.618 rillig return l <= r ? 1 : 0;
1233 1.615 rillig case GT:
1234 1.618 rillig return l > r ? 1 : 0;
1235 1.506 rillig case GE:
1236 1.618 rillig return l >= r ? 1 : 0;
1237 1.506 rillig case EQ:
1238 1.618 rillig return l == r ? 1 : 0;
1239 1.506 rillig case NE:
1240 1.618 rillig return l != r ? 1 : 0;
1241 1.506 rillig case BITAND:
1242 1.618 rillig return l & r;
1243 1.506 rillig case BITXOR:
1244 1.618 rillig return l ^ r;
1245 1.506 rillig case BITOR:
1246 1.618 rillig return l | r;
1247 1.506 rillig default:
1248 1.680 rillig lint_assert(false);
1249 1.620 rillig /* NOTREACHED */
1250 1.506 rillig }
1251 1.618 rillig }
1252 1.618 rillig
1253 1.618 rillig static tnode_t *
1254 1.618 rillig fold_constant_integer(tnode_t *tn)
1255 1.618 rillig {
1256 1.618 rillig
1257 1.618 rillig lint_assert(has_operands(tn));
1258 1.618 rillig tspec_t t = tn->u.ops.left->tn_type->t_tspec;
1259 1.621 rillig int64_t l = tn->u.ops.left->u.value.u.integer;
1260 1.621 rillig int64_t r = is_binary(tn) ? tn->u.ops.right->u.value.u.integer : 0;
1261 1.621 rillig uint64_t mask = value_bits(size_in_bits(t));
1262 1.618 rillig
1263 1.621 rillig int64_t res;
1264 1.621 rillig bool overflow = false;
1265 1.621 rillig if (!is_integer(t) || is_uinteger(t)) {
1266 1.621 rillig uint64_t u_res = fold_unsigned_integer(tn->tn_op,
1267 1.621 rillig (uint64_t)l, (uint64_t)r, mask, &overflow);
1268 1.621 rillig if (u_res > mask)
1269 1.621 rillig overflow = true;
1270 1.621 rillig res = (int64_t)u_res;
1271 1.623 rillig if (overflow && hflag) {
1272 1.623 rillig char buf[128];
1273 1.623 rillig if (is_binary(tn)) {
1274 1.623 rillig snprintf(buf, sizeof(buf), "%ju %s %ju",
1275 1.623 rillig (uintmax_t)l, op_name(tn->tn_op),
1276 1.623 rillig (uintmax_t)r);
1277 1.623 rillig } else {
1278 1.623 rillig snprintf(buf, sizeof(buf), "%s%ju",
1279 1.623 rillig op_name(tn->tn_op), (uintmax_t)l);
1280 1.623 rillig }
1281 1.623 rillig /* '%s' overflows '%s' */
1282 1.690 rillig warning(141, buf, expr_type_name(tn));
1283 1.623 rillig }
1284 1.621 rillig } else {
1285 1.621 rillig int64_t max_value = (int64_t)(mask >> 1);
1286 1.621 rillig int64_t min_value = -max_value - 1;
1287 1.621 rillig res = fold_signed_integer(tn->tn_op,
1288 1.621 rillig l, r, min_value, max_value, &overflow);
1289 1.621 rillig if (res < min_value || res > max_value)
1290 1.621 rillig overflow = true;
1291 1.623 rillig if (overflow && hflag) {
1292 1.623 rillig char buf[128];
1293 1.623 rillig if (is_binary(tn)) {
1294 1.623 rillig snprintf(buf, sizeof(buf), "%jd %s %jd",
1295 1.623 rillig (intmax_t)l, op_name(tn->tn_op),
1296 1.623 rillig (intmax_t)r);
1297 1.623 rillig } else if (tn->tn_op == UMINUS && l < 0) {
1298 1.623 rillig snprintf(buf, sizeof(buf), "-(%jd)",
1299 1.623 rillig (intmax_t)l);
1300 1.623 rillig } else {
1301 1.623 rillig snprintf(buf, sizeof(buf), "%s%jd",
1302 1.623 rillig op_name(tn->tn_op), (intmax_t)l);
1303 1.623 rillig }
1304 1.623 rillig /* '%s' overflows '%s' */
1305 1.690 rillig warning(141, buf, expr_type_name(tn));
1306 1.623 rillig }
1307 1.506 rillig }
1308 1.506 rillig
1309 1.617 rillig val_t *v = xcalloc(1, sizeof(*v));
1310 1.617 rillig v->v_tspec = tn->tn_type->t_tspec;
1311 1.629 rillig v->u.integer = convert_integer(res, t, size_in_bits(t));
1312 1.151 rillig
1313 1.536 rillig tnode_t *cn = build_constant(tn->tn_type, v);
1314 1.612 rillig if (tn->u.ops.left->tn_system_dependent)
1315 1.506 rillig cn->tn_system_dependent = true;
1316 1.612 rillig if (is_binary(tn) && tn->u.ops.right->tn_system_dependent)
1317 1.506 rillig cn->tn_system_dependent = true;
1318 1.190 rillig
1319 1.506 rillig return cn;
1320 1.190 rillig }
1321 1.190 rillig
1322 1.506 rillig static tnode_t *
1323 1.506 rillig build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1324 1.375 rillig {
1325 1.440 rillig
1326 1.506 rillig lint_assert(rn->tn_op == NAME);
1327 1.612 rillig lint_assert(is_member(rn->u.sym));
1328 1.440 rillig
1329 1.566 rillig bool lvalue = op == ARROW || ln->tn_lvalue;
1330 1.506 rillig
1331 1.604 rillig if (op == POINT)
1332 1.673 rillig ln = build_address(sys, ln);
1333 1.604 rillig else if (ln->tn_type->t_tspec != PTR) {
1334 1.506 rillig lint_assert(!allow_c90);
1335 1.506 rillig lint_assert(is_integer(ln->tn_type->t_tspec));
1336 1.506 rillig ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
1337 1.506 rillig }
1338 1.506 rillig
1339 1.536 rillig tnode_t *ctn = build_integer_constant(PTRDIFF_TSPEC,
1340 1.612 rillig rn->u.sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
1341 1.506 rillig
1342 1.536 rillig type_t *ptr_tp = expr_derive_type(rn->tn_type, PTR);
1343 1.565 rillig tnode_t *ntn = build_op(PLUS, sys, ptr_tp, ln, ctn);
1344 1.506 rillig if (ln->tn_op == CON)
1345 1.589 rillig ntn = fold_constant_integer(ntn);
1346 1.506 rillig
1347 1.566 rillig op_t nop = rn->tn_type->t_bitfield ? FSEL : INDIR;
1348 1.566 rillig ntn = build_op(nop, sys, ntn->tn_type->t_subt, ntn, NULL);
1349 1.566 rillig if (!lvalue)
1350 1.506 rillig ntn->tn_lvalue = false;
1351 1.440 rillig
1352 1.506 rillig return ntn;
1353 1.375 rillig }
1354 1.375 rillig
1355 1.506 rillig /*
1356 1.506 rillig * Get the size in bytes of type tp->t_subt, as a constant expression of type
1357 1.506 rillig * ptrdiff_t as seen from the target platform.
1358 1.506 rillig */
1359 1.506 rillig static tnode_t *
1360 1.506 rillig subt_size_in_bytes(type_t *tp)
1361 1.375 rillig {
1362 1.506 rillig
1363 1.506 rillig lint_assert(tp->t_tspec == PTR);
1364 1.506 rillig tp = tp->t_subt;
1365 1.375 rillig
1366 1.536 rillig int elem = 1;
1367 1.506 rillig while (tp->t_tspec == ARRAY) {
1368 1.611 rillig elem *= tp->u.dimension;
1369 1.506 rillig tp = tp->t_subt;
1370 1.138 rillig }
1371 1.138 rillig
1372 1.536 rillig int elsz_in_bits = 0;
1373 1.506 rillig switch (tp->t_tspec) {
1374 1.506 rillig case FUNC:
1375 1.506 rillig /* pointer to function is not allowed here */
1376 1.506 rillig error(110);
1377 1.506 rillig break;
1378 1.506 rillig case VOID:
1379 1.506 rillig /* cannot do pointer arithmetic on operand of unknown size */
1380 1.506 rillig gnuism(136);
1381 1.506 rillig break;
1382 1.506 rillig case STRUCT:
1383 1.506 rillig case UNION:
1384 1.611 rillig if ((elsz_in_bits = (int)tp->u.sou->sou_size_in_bits) == 0)
1385 1.506 rillig /* cannot do pointer arithmetic on operand of ... */
1386 1.506 rillig error(136);
1387 1.506 rillig break;
1388 1.506 rillig case ENUM:
1389 1.604 rillig if (is_incomplete(tp))
1390 1.506 rillig /* cannot do pointer arithmetic on operand of ... */
1391 1.506 rillig warning(136);
1392 1.506 rillig /* FALLTHROUGH */
1393 1.506 rillig default:
1394 1.604 rillig if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0)
1395 1.506 rillig /* cannot do pointer arithmetic on operand of ... */
1396 1.506 rillig error(136);
1397 1.604 rillig else
1398 1.506 rillig lint_assert(elsz_in_bits != -1);
1399 1.506 rillig break;
1400 1.139 rillig }
1401 1.506 rillig
1402 1.604 rillig if (elem == 0 && elsz_in_bits != 0)
1403 1.506 rillig /* cannot do pointer arithmetic on operand of unknown size */
1404 1.506 rillig error(136);
1405 1.506 rillig
1406 1.506 rillig if (elsz_in_bits == 0)
1407 1.506 rillig elsz_in_bits = CHAR_SIZE;
1408 1.506 rillig
1409 1.506 rillig return build_integer_constant(PTRDIFF_TSPEC,
1410 1.506 rillig (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
1411 1.139 rillig }
1412 1.139 rillig
1413 1.506 rillig static tnode_t *
1414 1.506 rillig build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
1415 1.140 rillig {
1416 1.457 rillig
1417 1.506 rillig lint_assert(ln != NULL);
1418 1.536 rillig tnode_t *cn = ln->tn_type->t_tspec == PTR
1419 1.536 rillig ? subt_size_in_bytes(ln->tn_type)
1420 1.536 rillig : build_integer_constant(INT, 1);
1421 1.565 rillig return build_op(op, sys, ln->tn_type, ln, cn);
1422 1.140 rillig }
1423 1.140 rillig
1424 1.140 rillig static void
1425 1.506 rillig check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
1426 1.140 rillig {
1427 1.506 rillig
1428 1.686 rillig if (ln->tn_op != ADDR)
1429 1.686 rillig return;
1430 1.686 rillig ln = ln->u.ops.left;
1431 1.686 rillig if (ln->tn_op != NAME && ln->tn_op != STRING)
1432 1.506 rillig return;
1433 1.506 rillig
1434 1.686 rillig const type_t *ltp = ln->tn_type;
1435 1.536 rillig if (ltp->t_tspec != ARRAY || ltp->t_incomplete_array)
1436 1.506 rillig return;
1437 1.506 rillig
1438 1.506 rillig if (rn->tn_op != CVT || !rn->tn_type->t_is_enum)
1439 1.506 rillig return;
1440 1.612 rillig if (rn->u.ops.left->tn_op != LOAD)
1441 1.506 rillig return;
1442 1.506 rillig
1443 1.612 rillig const type_t *rtp = rn->u.ops.left->tn_type;
1444 1.611 rillig const sym_t *ec = rtp->u.enumer->en_first_enumerator;
1445 1.540 rillig const sym_t *max_ec = ec;
1446 1.506 rillig lint_assert(ec != NULL);
1447 1.506 rillig for (ec = ec->s_next; ec != NULL; ec = ec->s_next)
1448 1.506 rillig if (ec->u.s_enum_constant > max_ec->u.s_enum_constant)
1449 1.506 rillig max_ec = ec;
1450 1.506 rillig
1451 1.536 rillig int64_t max_enum_value = max_ec->u.s_enum_constant;
1452 1.506 rillig lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX);
1453 1.143 rillig
1454 1.611 rillig int max_array_index = ltp->u.dimension - 1;
1455 1.686 rillig size_t nonnull_dimension = ln->tn_op == STRING
1456 1.686 rillig ? str_len(ln)
1457 1.686 rillig : ln->u.sym->u.s_array_nonnull_dimension;
1458 1.686 rillig if (nonnull_dimension > 0)
1459 1.686 rillig max_array_index = (int)nonnull_dimension - 1;
1460 1.506 rillig if (max_enum_value == max_array_index)
1461 1.506 rillig return;
1462 1.143 rillig
1463 1.506 rillig if (max_enum_value == max_array_index + 1 &&
1464 1.506 rillig (strstr(max_ec->s_name, "MAX") != NULL ||
1465 1.506 rillig strstr(max_ec->s_name, "max") != NULL ||
1466 1.506 rillig strstr(max_ec->s_name, "NUM") != NULL ||
1467 1.657 rillig strstr(max_ec->s_name, "num") != NULL ||
1468 1.657 rillig strncmp(max_ec->s_name, "N_", 2) == 0))
1469 1.506 rillig return;
1470 1.502 rillig
1471 1.657 rillig /* maximum value %d for '%s' of type '%s' does not match maximum array index %d */
1472 1.657 rillig warning(348, (int)max_enum_value, max_ec->s_name, type_name(rtp),
1473 1.657 rillig max_array_index);
1474 1.506 rillig print_previous_declaration(max_ec);
1475 1.140 rillig }
1476 1.140 rillig
1477 1.506 rillig static tnode_t *
1478 1.506 rillig build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1479 1.182 rillig {
1480 1.506 rillig
1481 1.506 rillig if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
1482 1.506 rillig tnode_t *tmp = ln;
1483 1.506 rillig ln = rn;
1484 1.506 rillig rn = tmp;
1485 1.506 rillig /* pointer addition has integer on the left-hand side */
1486 1.506 rillig query_message(5);
1487 1.140 rillig }
1488 1.140 rillig
1489 1.506 rillig /* pointer +- integer */
1490 1.631 rillig tspec_t lt = ln->tn_type->t_tspec;
1491 1.631 rillig tspec_t rt = rn->tn_type->t_tspec;
1492 1.631 rillig if (lt == PTR && rt != PTR) {
1493 1.631 rillig lint_assert(is_integer(rt));
1494 1.270 rillig
1495 1.506 rillig check_ctype_macro_invocation(ln, rn);
1496 1.506 rillig check_enum_array_index(ln, rn);
1497 1.140 rillig
1498 1.506 rillig tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
1499 1.631 rillig tspec_t szt = elsz->tn_type->t_tspec;
1500 1.631 rillig if (rt != szt && rt != unsigned_type(szt))
1501 1.506 rillig rn = convert(NOOP, 0, elsz->tn_type, rn);
1502 1.140 rillig
1503 1.565 rillig tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
1504 1.506 rillig if (rn->tn_op == CON)
1505 1.589 rillig prod = fold_constant_integer(prod);
1506 1.392 rillig
1507 1.565 rillig return build_op(op, sys, ln->tn_type, ln, prod);
1508 1.140 rillig }
1509 1.178 rillig
1510 1.506 rillig /* pointer - pointer */
1511 1.631 rillig if (rt == PTR) {
1512 1.631 rillig lint_assert(lt == PTR);
1513 1.506 rillig lint_assert(op == MINUS);
1514 1.178 rillig
1515 1.506 rillig type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
1516 1.565 rillig tnode_t *raw_diff = build_op(op, sys, ptrdiff, ln, rn);
1517 1.506 rillig if (ln->tn_op == CON && rn->tn_op == CON)
1518 1.589 rillig raw_diff = fold_constant_integer(raw_diff);
1519 1.178 rillig
1520 1.506 rillig tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
1521 1.506 rillig balance(NOOP, &raw_diff, &elsz);
1522 1.140 rillig
1523 1.565 rillig return build_op(DIV, sys, ptrdiff, raw_diff, elsz);
1524 1.140 rillig }
1525 1.506 rillig
1526 1.565 rillig return build_op(op, sys, ln->tn_type, ln, rn);
1527 1.140 rillig }
1528 1.140 rillig
1529 1.506 rillig static tnode_t *
1530 1.506 rillig build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1531 1.206 rillig {
1532 1.206 rillig
1533 1.506 rillig if (!allow_c90 && rn->tn_type->t_tspec != INT)
1534 1.564 rillig // XXX: C1978 7.5 says: "Both [operators] perform the usual
1535 1.564 rillig // arithmetic conversions on their operands."
1536 1.564 rillig // TODO: Add a test to exercise this part of the code.
1537 1.506 rillig rn = convert(NOOP, 0, gettyp(INT), rn);
1538 1.565 rillig return build_op(op, sys, ln->tn_type, ln, rn);
1539 1.206 rillig }
1540 1.206 rillig
1541 1.140 rillig static bool
1542 1.506 rillig is_null_pointer(const tnode_t *tn)
1543 1.140 rillig {
1544 1.506 rillig tspec_t t = tn->tn_type->t_tspec;
1545 1.142 rillig
1546 1.564 rillig // TODO: Investigate how other pointers are stored, in particular,
1547 1.564 rillig // whether a pointer constant can have a non-zero value.
1548 1.564 rillig // If not, simplify the code below.
1549 1.584 rillig return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID)
1550 1.584 rillig || is_integer(t))
1551 1.612 rillig && (tn->tn_op == CON && tn->u.value.u.integer == 0);
1552 1.506 rillig }
1553 1.140 rillig
1554 1.506 rillig /* Return a type based on tp1, with added qualifiers from tp2. */
1555 1.506 rillig static type_t *
1556 1.506 rillig merge_qualifiers(type_t *tp1, const type_t *tp2)
1557 1.506 rillig {
1558 1.140 rillig
1559 1.506 rillig lint_assert(tp1->t_tspec == PTR);
1560 1.506 rillig lint_assert(tp2->t_tspec == PTR);
1561 1.140 rillig
1562 1.536 rillig bool c1 = tp1->t_subt->t_const;
1563 1.536 rillig bool c2 = tp2->t_subt->t_const;
1564 1.536 rillig bool v1 = tp1->t_subt->t_volatile;
1565 1.536 rillig bool v2 = tp2->t_subt->t_volatile;
1566 1.140 rillig
1567 1.506 rillig if (c1 == (c1 | c2) && v1 == (v1 | v2))
1568 1.506 rillig return tp1;
1569 1.140 rillig
1570 1.536 rillig type_t *nstp = expr_dup_type(tp1->t_subt);
1571 1.506 rillig nstp->t_const |= c2;
1572 1.506 rillig nstp->t_volatile |= v2;
1573 1.140 rillig
1574 1.536 rillig type_t *ntp = expr_dup_type(tp1);
1575 1.506 rillig ntp->t_subt = nstp;
1576 1.506 rillig return ntp;
1577 1.140 rillig }
1578 1.140 rillig
1579 1.506 rillig /* See C99 6.5.15 "Conditional operator". */
1580 1.506 rillig static tnode_t *
1581 1.506 rillig build_colon(bool sys, tnode_t *ln, tnode_t *rn)
1582 1.140 rillig {
1583 1.536 rillig tspec_t lt = ln->tn_type->t_tspec;
1584 1.536 rillig tspec_t rt = rn->tn_type->t_tspec;
1585 1.332 rillig
1586 1.536 rillig type_t *tp;
1587 1.604 rillig if (is_arithmetic(lt) && is_arithmetic(rt))
1588 1.506 rillig /* The operands were already balanced in build_binary. */
1589 1.506 rillig tp = ln->tn_type;
1590 1.604 rillig else if (lt == BOOL && rt == BOOL)
1591 1.506 rillig tp = ln->tn_type;
1592 1.604 rillig else if (lt == VOID || rt == VOID)
1593 1.506 rillig tp = gettyp(VOID);
1594 1.604 rillig else if (is_struct_or_union(lt)) {
1595 1.506 rillig lint_assert(is_struct_or_union(rt));
1596 1.611 rillig lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
1597 1.506 rillig if (is_incomplete(ln->tn_type)) {
1598 1.506 rillig /* unknown operand size, op '%s' */
1599 1.506 rillig error(138, op_name(COLON));
1600 1.506 rillig return NULL;
1601 1.140 rillig }
1602 1.506 rillig tp = ln->tn_type;
1603 1.506 rillig } else if (lt == PTR && is_integer(rt)) {
1604 1.506 rillig if (rt != PTRDIFF_TSPEC)
1605 1.506 rillig rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn);
1606 1.506 rillig tp = ln->tn_type;
1607 1.506 rillig } else if (rt == PTR && is_integer(lt)) {
1608 1.506 rillig if (lt != PTRDIFF_TSPEC)
1609 1.506 rillig ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln);
1610 1.506 rillig tp = rn->tn_type;
1611 1.604 rillig } else if (lt == PTR && is_null_pointer(rn))
1612 1.506 rillig tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1613 1.604 rillig else if (rt == PTR && is_null_pointer(ln))
1614 1.506 rillig tp = merge_qualifiers(rn->tn_type, ln->tn_type);
1615 1.604 rillig else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID)
1616 1.506 rillig tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1617 1.604 rillig else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID)
1618 1.506 rillig tp = merge_qualifiers(rn->tn_type, ln->tn_type);
1619 1.604 rillig else {
1620 1.506 rillig /*
1621 1.586 rillig * XXX For now we simply take the left type. This is probably
1622 1.586 rillig * wrong, if one type contains a function prototype and the
1623 1.586 rillig * other one, at the same place, only an old-style declaration.
1624 1.506 rillig */
1625 1.506 rillig tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1626 1.140 rillig }
1627 1.506 rillig
1628 1.565 rillig return build_op(COLON, sys, tp, ln, rn);
1629 1.140 rillig }
1630 1.140 rillig
1631 1.506 rillig /* TODO: check for varargs */
1632 1.150 rillig static bool
1633 1.506 rillig is_cast_redundant(const tnode_t *tn)
1634 1.1 cgd {
1635 1.612 rillig const type_t *ntp = tn->tn_type, *otp = tn->u.ops.left->tn_type;
1636 1.506 rillig tspec_t nt = ntp->t_tspec, ot = otp->t_tspec;
1637 1.506 rillig
1638 1.506 rillig if (nt == BOOL || ot == BOOL)
1639 1.506 rillig return nt == BOOL && ot == BOOL;
1640 1.506 rillig
1641 1.506 rillig if (is_integer(nt) && is_integer(ot)) {
1642 1.506 rillig unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp);
1643 1.506 rillig if (is_uinteger(nt) == is_uinteger(ot))
1644 1.584 rillig return nw >= ow;
1645 1.506 rillig return is_uinteger(ot) && nw > ow;
1646 1.1 cgd }
1647 1.1 cgd
1648 1.506 rillig if (is_complex(nt) || is_complex(ot))
1649 1.506 rillig return is_complex(nt) && is_complex(ot) &&
1650 1.584 rillig size_in_bits(nt) >= size_in_bits(ot);
1651 1.506 rillig
1652 1.506 rillig if (is_floating(nt) && is_floating(ot))
1653 1.506 rillig return size_in_bits(nt) >= size_in_bits(ot);
1654 1.506 rillig
1655 1.506 rillig if (nt == PTR && ot == PTR) {
1656 1.506 rillig if (!ntp->t_subt->t_const && otp->t_subt->t_const)
1657 1.141 rillig return false;
1658 1.506 rillig if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile)
1659 1.141 rillig return false;
1660 1.506 rillig
1661 1.506 rillig if (ntp->t_subt->t_tspec == VOID ||
1662 1.506 rillig otp->t_subt->t_tspec == VOID ||
1663 1.506 rillig types_compatible(ntp->t_subt, otp->t_subt,
1664 1.506 rillig false, false, NULL))
1665 1.506 rillig return true;
1666 1.1 cgd }
1667 1.1 cgd
1668 1.506 rillig return false;
1669 1.150 rillig }
1670 1.150 rillig
1671 1.523 rillig static bool
1672 1.523 rillig is_assignment(op_t op)
1673 1.523 rillig {
1674 1.523 rillig
1675 1.523 rillig return op == ASSIGN ||
1676 1.584 rillig op == MULASS ||
1677 1.584 rillig op == DIVASS ||
1678 1.584 rillig op == MODASS ||
1679 1.584 rillig op == ADDASS ||
1680 1.584 rillig op == SUBASS ||
1681 1.584 rillig op == SHLASS ||
1682 1.584 rillig op == SHRASS ||
1683 1.584 rillig op == ANDASS ||
1684 1.584 rillig op == XORASS ||
1685 1.584 rillig op == ORASS ||
1686 1.584 rillig op == RETURN ||
1687 1.584 rillig op == INIT;
1688 1.523 rillig }
1689 1.523 rillig
1690 1.506 rillig static tnode_t *
1691 1.506 rillig build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1692 1.150 rillig {
1693 1.150 rillig
1694 1.536 rillig tspec_t lt = ln->tn_type->t_tspec;
1695 1.536 rillig tspec_t rt = rn->tn_type->t_tspec;
1696 1.150 rillig
1697 1.661 rillig if (is_assignment(rn->tn_op))
1698 1.523 rillig /* chained assignment with '%s' and '%s' */
1699 1.523 rillig query_message(10, op_name(op), op_name(rn->tn_op));
1700 1.523 rillig
1701 1.506 rillig if ((op == ADDASS || op == SUBASS) && lt == PTR) {
1702 1.506 rillig lint_assert(is_integer(rt));
1703 1.536 rillig tnode_t *ctn = subt_size_in_bytes(ln->tn_type);
1704 1.506 rillig if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
1705 1.506 rillig rn = convert(NOOP, 0, ctn->tn_type, rn);
1706 1.565 rillig rn = build_op(MULT, sys, rn->tn_type, rn, ctn);
1707 1.612 rillig if (rn->u.ops.left->tn_op == CON)
1708 1.589 rillig rn = fold_constant_integer(rn);
1709 1.150 rillig }
1710 1.150 rillig
1711 1.506 rillig if ((op == ASSIGN || op == RETURN || op == INIT) &&
1712 1.506 rillig (lt == STRUCT || rt == STRUCT)) {
1713 1.506 rillig lint_assert(lt == rt);
1714 1.611 rillig lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
1715 1.506 rillig if (is_incomplete(ln->tn_type)) {
1716 1.604 rillig if (op == RETURN)
1717 1.506 rillig /* cannot return incomplete type */
1718 1.506 rillig error(212);
1719 1.604 rillig else
1720 1.506 rillig /* unknown operand size, op '%s' */
1721 1.506 rillig error(138, op_name(op));
1722 1.506 rillig return NULL;
1723 1.506 rillig }
1724 1.506 rillig }
1725 1.506 rillig
1726 1.549 rillig if (op == SHLASS && hflag && allow_trad && allow_c90
1727 1.550 rillig && portable_rank_cmp(lt, rt) < 0)
1728 1.688 rillig /* '%s' %s '%s' differs between traditional C and C90 */
1729 1.688 rillig warning(118, tspec_name(lt), "<<=", tspec_name(rt));
1730 1.549 rillig
1731 1.549 rillig if (op != SHLASS && op != SHRASS
1732 1.549 rillig && (op == ASSIGN || lt != PTR)
1733 1.549 rillig && (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) {
1734 1.549 rillig rn = convert(op, 0, ln->tn_type, rn);
1735 1.549 rillig rt = lt;
1736 1.506 rillig }
1737 1.506 rillig
1738 1.661 rillig if (lt == PTR && ln->tn_type->t_subt->t_tspec != VOID
1739 1.637 rillig && rt == PTR && rn->tn_type->t_subt->t_tspec == VOID
1740 1.637 rillig && !is_null_pointer(rn))
1741 1.636 rillig /* implicit narrowing conversion from void ... */
1742 1.636 rillig query_message(20, type_name(ln->tn_type));
1743 1.636 rillig
1744 1.662 rillig if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
1745 1.506 rillig types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
1746 1.506 rillig is_cast_redundant(rn)) {
1747 1.506 rillig /* redundant cast from '%s' to '%s' before assignment */
1748 1.690 rillig query_message(7, expr_type_name(rn->u.ops.left),
1749 1.612 rillig type_name(rn->tn_type));
1750 1.506 rillig }
1751 1.150 rillig
1752 1.565 rillig return build_op(op, sys, ln->tn_type, ln, rn);
1753 1.1 cgd }
1754 1.1 cgd
1755 1.506 rillig static tnode_t *
1756 1.506 rillig build_real_imag(op_t op, bool sys, tnode_t *ln)
1757 1.1 cgd {
1758 1.1 cgd
1759 1.506 rillig lint_assert(ln != NULL);
1760 1.506 rillig if (ln->tn_op == NAME) {
1761 1.506 rillig /*
1762 1.586 rillig * This may be too much, but it avoids wrong warnings. See
1763 1.586 rillig * d_c99_complex_split.c.
1764 1.506 rillig */
1765 1.612 rillig mark_as_used(ln->u.sym, false, false);
1766 1.612 rillig mark_as_set(ln->u.sym);
1767 1.1 cgd }
1768 1.1 cgd
1769 1.561 rillig tspec_t t;
1770 1.506 rillig switch (ln->tn_type->t_tspec) {
1771 1.506 rillig case LCOMPLEX:
1772 1.561 rillig t = LDOUBLE;
1773 1.506 rillig break;
1774 1.506 rillig case DCOMPLEX:
1775 1.561 rillig t = DOUBLE;
1776 1.506 rillig break;
1777 1.506 rillig case FCOMPLEX:
1778 1.561 rillig t = FLOAT;
1779 1.506 rillig break;
1780 1.506 rillig default:
1781 1.681 rillig /* '__%s__' is invalid for type '%s' */
1782 1.506 rillig error(276, op == REAL ? "real" : "imag",
1783 1.506 rillig type_name(ln->tn_type));
1784 1.506 rillig return NULL;
1785 1.1 cgd }
1786 1.536 rillig
1787 1.565 rillig tnode_t *ntn = build_op(op, sys, gettyp(t), ln, NULL);
1788 1.506 rillig ntn->tn_lvalue = true;
1789 1.506 rillig return ntn;
1790 1.1 cgd }
1791 1.1 cgd
1792 1.331 rillig static bool
1793 1.664 rillig is_confusing_precedence(op_t op, const tnode_t *operand, op_t *cop)
1794 1.331 rillig {
1795 1.664 rillig if (operand->tn_parenthesized)
1796 1.664 rillig return false;
1797 1.664 rillig op_t oop = operand->tn_op;
1798 1.336 rillig
1799 1.506 rillig if (op == SHL || op == SHR) {
1800 1.664 rillig if (oop == PLUS || oop == MINUS)
1801 1.664 rillig return *cop = oop, true;
1802 1.506 rillig return false;
1803 1.506 rillig }
1804 1.506 rillig
1805 1.506 rillig if (op == LOGOR) {
1806 1.664 rillig if (oop == LOGAND)
1807 1.664 rillig return *cop = oop, true;
1808 1.336 rillig return false;
1809 1.506 rillig }
1810 1.336 rillig
1811 1.506 rillig lint_assert(op == BITAND || op == BITXOR || op == BITOR);
1812 1.664 rillig if (oop != op
1813 1.664 rillig && (oop == PLUS || oop == MINUS || oop == BITAND || oop == BITXOR))
1814 1.664 rillig return *cop = oop, true;
1815 1.506 rillig return false;
1816 1.336 rillig }
1817 1.336 rillig
1818 1.506 rillig /*
1819 1.506 rillig * Print a warning if the given node has operands which should be
1820 1.506 rillig * parenthesized.
1821 1.506 rillig *
1822 1.506 rillig * XXX Does not work if an operand is a constant expression. Constant
1823 1.506 rillig * expressions are already folded.
1824 1.506 rillig */
1825 1.506 rillig static void
1826 1.506 rillig check_precedence_confusion(tnode_t *tn)
1827 1.336 rillig {
1828 1.506 rillig tnode_t *ln, *rn;
1829 1.336 rillig
1830 1.506 rillig if (!hflag)
1831 1.506 rillig return;
1832 1.331 rillig
1833 1.506 rillig debug_node(tn);
1834 1.331 rillig
1835 1.506 rillig lint_assert(is_binary(tn));
1836 1.612 rillig for (ln = tn->u.ops.left; ln->tn_op == CVT; ln = ln->u.ops.left)
1837 1.506 rillig continue;
1838 1.612 rillig for (rn = tn->u.ops.right; rn->tn_op == CVT; rn = rn->u.ops.left)
1839 1.506 rillig continue;
1840 1.331 rillig
1841 1.663 rillig op_t cop;
1842 1.664 rillig if (is_confusing_precedence(tn->tn_op, ln, &cop) ||
1843 1.664 rillig is_confusing_precedence(tn->tn_op, rn, &cop)) {
1844 1.663 rillig /* possible precedence confusion between '%s' and '%s' */
1845 1.663 rillig warning(169, op_name(tn->tn_op), op_name(cop));
1846 1.506 rillig }
1847 1.331 rillig }
1848 1.331 rillig
1849 1.506 rillig static tnode_t *
1850 1.589 rillig fold_constant_compare_zero(tnode_t *tn)
1851 1.384 rillig {
1852 1.384 rillig
1853 1.536 rillig val_t *v = xcalloc(1, sizeof(*v));
1854 1.506 rillig v->v_tspec = tn->tn_type->t_tspec;
1855 1.506 rillig lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
1856 1.384 rillig
1857 1.581 rillig lint_assert(has_operands(tn));
1858 1.612 rillig bool l = constant_is_nonzero(tn->u.ops.left);
1859 1.612 rillig bool r = is_binary(tn) && constant_is_nonzero(tn->u.ops.right);
1860 1.336 rillig
1861 1.506 rillig switch (tn->tn_op) {
1862 1.506 rillig case NOT:
1863 1.544 rillig v->u.integer = !l ? 1 : 0;
1864 1.506 rillig break;
1865 1.506 rillig case LOGAND:
1866 1.544 rillig v->u.integer = l && r ? 1 : 0;
1867 1.506 rillig break;
1868 1.506 rillig case LOGOR:
1869 1.544 rillig v->u.integer = l || r ? 1 : 0;
1870 1.506 rillig break;
1871 1.506 rillig default:
1872 1.680 rillig lint_assert(false);
1873 1.506 rillig }
1874 1.336 rillig
1875 1.506 rillig return build_constant(tn->tn_type, v);
1876 1.331 rillig }
1877 1.331 rillig
1878 1.527 rillig static long double
1879 1.527 rillig floating_error_value(tspec_t t, long double lv)
1880 1.384 rillig {
1881 1.509 rillig if (t == FLOAT)
1882 1.506 rillig return lv < 0 ? -FLT_MAX : FLT_MAX;
1883 1.509 rillig if (t == DOUBLE)
1884 1.506 rillig return lv < 0 ? -DBL_MAX : DBL_MAX;
1885 1.512 rillig /*
1886 1.586 rillig * When NetBSD is cross-built in MKLINT=yes mode on x86_64 for sparc64,
1887 1.586 rillig * tools/lint checks this code while building usr.bin/xlint. In that
1888 1.586 rillig * situation, lint uses the preprocessor for sparc64, in which the type
1889 1.586 rillig * 'long double' is IEEE-754-binary128, affecting the macro LDBL_MAX
1890 1.586 rillig * below. The type 'long double', as well as the strtold
1891 1.513 rillig * implementation, comes from the host platform x86_64 though, where
1892 1.513 rillig * 'long double' consumes 128 bits as well but only uses 80 of them.
1893 1.513 rillig * The exponent range of the two 'long double' types is the same, but
1894 1.513 rillig * the maximum finite value differs due to the extended precision on
1895 1.513 rillig * sparc64.
1896 1.512 rillig *
1897 1.586 rillig * To properly handle the data types of the target platform, lint would
1898 1.586 rillig * have to implement the floating-point types in a platform-independent
1899 1.586 rillig * way, which is not worth the effort, given how few programs
1900 1.586 rillig * practically use 'long double'.
1901 1.512 rillig */
1902 1.512 rillig /* LINTED 248: floating-point constant out of range */
1903 1.527 rillig long double max = LDBL_MAX;
1904 1.512 rillig return lv < 0 ? -max : max;
1905 1.506 rillig }
1906 1.384 rillig
1907 1.536 rillig static bool
1908 1.536 rillig is_floating_overflow(tspec_t t, long double val)
1909 1.536 rillig {
1910 1.536 rillig if (fpe != 0 || isfinite(val) == 0)
1911 1.536 rillig return true;
1912 1.536 rillig if (t == FLOAT && (val > FLT_MAX || val < -FLT_MAX))
1913 1.536 rillig return true;
1914 1.536 rillig if (t == DOUBLE && (val > DBL_MAX || val < -DBL_MAX))
1915 1.536 rillig return true;
1916 1.536 rillig return false;
1917 1.536 rillig }
1918 1.536 rillig
1919 1.506 rillig static tnode_t *
1920 1.589 rillig fold_constant_floating(tnode_t *tn)
1921 1.331 rillig {
1922 1.336 rillig
1923 1.506 rillig fpe = 0;
1924 1.536 rillig
1925 1.536 rillig tspec_t t = tn->tn_type->t_tspec;
1926 1.536 rillig
1927 1.536 rillig val_t *v = xcalloc(1, sizeof(*v));
1928 1.536 rillig v->v_tspec = t;
1929 1.384 rillig
1930 1.506 rillig lint_assert(is_floating(t));
1931 1.581 rillig lint_assert(has_operands(tn));
1932 1.612 rillig lint_assert(t == tn->u.ops.left->tn_type->t_tspec);
1933 1.612 rillig lint_assert(!is_binary(tn) || t == tn->u.ops.right->tn_type->t_tspec);
1934 1.331 rillig
1935 1.612 rillig long double lv = tn->u.ops.left->u.value.u.floating;
1936 1.612 rillig long double rv = is_binary(tn) ? tn->u.ops.right->u.value.u.floating
1937 1.612 rillig : 0.0;
1938 1.1 cgd
1939 1.506 rillig switch (tn->tn_op) {
1940 1.506 rillig case UPLUS:
1941 1.544 rillig v->u.floating = lv;
1942 1.369 rillig break;
1943 1.506 rillig case UMINUS:
1944 1.544 rillig v->u.floating = -lv;
1945 1.369 rillig break;
1946 1.506 rillig case MULT:
1947 1.544 rillig v->u.floating = lv * rv;
1948 1.369 rillig break;
1949 1.506 rillig case DIV:
1950 1.506 rillig if (rv == 0.0) {
1951 1.506 rillig /* division by 0 */
1952 1.506 rillig error(139);
1953 1.544 rillig v->u.floating = floating_error_value(t, lv);
1954 1.506 rillig } else {
1955 1.544 rillig v->u.floating = lv / rv;
1956 1.1 cgd }
1957 1.369 rillig break;
1958 1.506 rillig case PLUS:
1959 1.544 rillig v->u.floating = lv + rv;
1960 1.369 rillig break;
1961 1.506 rillig case MINUS:
1962 1.544 rillig v->u.floating = lv - rv;
1963 1.369 rillig break;
1964 1.506 rillig case LT:
1965 1.544 rillig v->u.integer = lv < rv ? 1 : 0;
1966 1.369 rillig break;
1967 1.506 rillig case LE:
1968 1.544 rillig v->u.integer = lv <= rv ? 1 : 0;
1969 1.369 rillig break;
1970 1.506 rillig case GE:
1971 1.544 rillig v->u.integer = lv >= rv ? 1 : 0;
1972 1.369 rillig break;
1973 1.506 rillig case GT:
1974 1.544 rillig v->u.integer = lv > rv ? 1 : 0;
1975 1.1 cgd break;
1976 1.506 rillig case EQ:
1977 1.544 rillig v->u.integer = lv == rv ? 1 : 0;
1978 1.1 cgd break;
1979 1.506 rillig case NE:
1980 1.544 rillig v->u.integer = lv != rv ? 1 : 0;
1981 1.1 cgd break;
1982 1.1 cgd default:
1983 1.680 rillig lint_assert(false);
1984 1.506 rillig }
1985 1.506 rillig
1986 1.564 rillig // XXX: Must not access u.floating after setting u.integer.
1987 1.544 rillig lint_assert(fpe != 0 || isnan(v->u.floating) == 0);
1988 1.508 rillig if (is_complex(v->v_tspec)) {
1989 1.508 rillig /*
1990 1.508 rillig * Don't warn, as lint doesn't model the imaginary part of
1991 1.508 rillig * complex numbers.
1992 1.508 rillig */
1993 1.508 rillig fpe = 0;
1994 1.544 rillig } else if (is_floating_overflow(t, v->u.floating)) {
1995 1.556 rillig /* operator '%s' produces floating point overflow */
1996 1.506 rillig warning(142, op_name(tn->tn_op));
1997 1.544 rillig v->u.floating = floating_error_value(t, v->u.floating);
1998 1.506 rillig fpe = 0;
1999 1.1 cgd }
2000 1.506 rillig
2001 1.506 rillig return build_constant(tn->tn_type, v);
2002 1.369 rillig }
2003 1.369 rillig
2004 1.579 rillig static void
2005 1.579 rillig use(const tnode_t *tn)
2006 1.579 rillig {
2007 1.579 rillig if (tn == NULL)
2008 1.579 rillig return;
2009 1.579 rillig switch (tn->tn_op) {
2010 1.579 rillig case NAME:
2011 1.612 rillig mark_as_used(tn->u.sym, false /* XXX */, false /* XXX */);
2012 1.579 rillig break;
2013 1.579 rillig case CON:
2014 1.579 rillig case STRING:
2015 1.579 rillig break;
2016 1.634 rillig case CALL:;
2017 1.612 rillig const function_call *call = tn->u.call;
2018 1.625 rillig for (size_t i = 0, n = call->args_len; i < n; i++)
2019 1.625 rillig use(call->args[i]);
2020 1.603 rillig break;
2021 1.579 rillig default:
2022 1.581 rillig lint_assert(has_operands(tn));
2023 1.612 rillig use(tn->u.ops.left);
2024 1.603 rillig if (is_binary(tn))
2025 1.612 rillig use(tn->u.ops.right);
2026 1.579 rillig }
2027 1.579 rillig }
2028 1.579 rillig
2029 1.369 rillig /*
2030 1.506 rillig * Create a tree node for a binary operator and its two operands. Also called
2031 1.506 rillig * for unary operators; in that case rn is NULL.
2032 1.506 rillig *
2033 1.506 rillig * Function calls, sizeof and casts are handled elsewhere.
2034 1.369 rillig */
2035 1.506 rillig tnode_t *
2036 1.506 rillig build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
2037 1.369 rillig {
2038 1.536 rillig const mod_t *mp = &modtab[op];
2039 1.369 rillig
2040 1.506 rillig /* If there was an error in one of the operands, return. */
2041 1.506 rillig if (ln == NULL || (mp->m_binary && rn == NULL))
2042 1.506 rillig return NULL;
2043 1.369 rillig
2044 1.506 rillig if (mp->m_value_context || mp->m_compares_with_zero)
2045 1.506 rillig ln = cconv(ln);
2046 1.506 rillig if (mp->m_binary && op != ARROW && op != POINT)
2047 1.506 rillig rn = cconv(rn);
2048 1.369 rillig
2049 1.506 rillig if (mp->m_comparison)
2050 1.506 rillig check_integer_comparison(op, ln, rn);
2051 1.369 rillig
2052 1.506 rillig if (mp->m_value_context || mp->m_compares_with_zero)
2053 1.506 rillig ln = promote(op, false, ln);
2054 1.506 rillig if (mp->m_binary && op != ARROW && op != POINT &&
2055 1.589 rillig op != ASSIGN && op != RETURN && op != INIT)
2056 1.506 rillig rn = promote(op, false, rn);
2057 1.369 rillig
2058 1.506 rillig if (mp->m_warn_if_left_unsigned_in_c90 &&
2059 1.612 rillig ln->tn_op == CON && ln->u.value.v_unsigned_since_c90) {
2060 1.578 rillig /* C90 treats constant as unsigned, op '%s' */
2061 1.560 rillig warning(218, op_name(op));
2062 1.612 rillig ln->u.value.v_unsigned_since_c90 = false;
2063 1.506 rillig }
2064 1.506 rillig if (mp->m_warn_if_right_unsigned_in_c90 &&
2065 1.612 rillig rn->tn_op == CON && rn->u.value.v_unsigned_since_c90) {
2066 1.578 rillig /* C90 treats constant as unsigned, op '%s' */
2067 1.560 rillig warning(218, op_name(op));
2068 1.612 rillig rn->u.value.v_unsigned_since_c90 = false;
2069 1.506 rillig }
2070 1.369 rillig
2071 1.506 rillig if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
2072 1.506 rillig balance(op, &ln, &rn);
2073 1.369 rillig
2074 1.683 rillig if (!typeok(op, NULL, 0, ln, rn))
2075 1.506 rillig return NULL;
2076 1.369 rillig
2077 1.536 rillig tnode_t *ntn;
2078 1.506 rillig switch (op) {
2079 1.506 rillig case POINT:
2080 1.506 rillig case ARROW:
2081 1.506 rillig ntn = build_struct_access(op, sys, ln, rn);
2082 1.506 rillig break;
2083 1.506 rillig case INCAFT:
2084 1.506 rillig case DECAFT:
2085 1.506 rillig case INCBEF:
2086 1.506 rillig case DECBEF:
2087 1.506 rillig ntn = build_prepost_incdec(op, sys, ln);
2088 1.506 rillig break;
2089 1.506 rillig case ADDR:
2090 1.673 rillig ntn = build_address(sys, ln);
2091 1.506 rillig break;
2092 1.506 rillig case INDIR:
2093 1.565 rillig ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
2094 1.506 rillig break;
2095 1.506 rillig case PLUS:
2096 1.506 rillig case MINUS:
2097 1.506 rillig ntn = build_plus_minus(op, sys, ln, rn);
2098 1.506 rillig break;
2099 1.506 rillig case SHL:
2100 1.506 rillig case SHR:
2101 1.506 rillig ntn = build_bit_shift(op, sys, ln, rn);
2102 1.506 rillig break;
2103 1.506 rillig case COLON:
2104 1.506 rillig ntn = build_colon(sys, ln, rn);
2105 1.506 rillig break;
2106 1.506 rillig case ASSIGN:
2107 1.506 rillig case MULASS:
2108 1.506 rillig case DIVASS:
2109 1.506 rillig case MODASS:
2110 1.506 rillig case ADDASS:
2111 1.506 rillig case SUBASS:
2112 1.506 rillig case SHLASS:
2113 1.506 rillig case SHRASS:
2114 1.506 rillig case ANDASS:
2115 1.506 rillig case XORASS:
2116 1.506 rillig case ORASS:
2117 1.506 rillig case RETURN:
2118 1.506 rillig case INIT:
2119 1.506 rillig ntn = build_assignment(op, sys, ln, rn);
2120 1.506 rillig break;
2121 1.506 rillig case COMMA:
2122 1.661 rillig /* comma operator with types '%s' and '%s' */
2123 1.661 rillig query_message(12,
2124 1.690 rillig expr_type_name(ln), expr_type_name(rn));
2125 1.528 rillig /* FALLTHROUGH */
2126 1.506 rillig case QUEST:
2127 1.565 rillig ntn = build_op(op, sys, rn->tn_type, ln, rn);
2128 1.506 rillig break;
2129 1.506 rillig case REAL:
2130 1.506 rillig case IMAG:
2131 1.506 rillig ntn = build_real_imag(op, sys, ln);
2132 1.506 rillig break;
2133 1.506 rillig default:
2134 1.536 rillig lint_assert(mp->m_binary == (rn != NULL));
2135 1.682 rillig if ((op == NOT || op == LOGAND || op == LOGOR)
2136 1.682 rillig && ln->tn_op == ASSIGN && ln->u.ops.right->tn_op == CON) {
2137 1.682 rillig /* constant assignment of type '%s' in operand ... */
2138 1.690 rillig warning(382, expr_type_name(ln), op_name(op),
2139 1.682 rillig is_nonzero_val(&ln->u.ops.right->u.value)
2140 1.682 rillig ? "true" : "false");
2141 1.682 rillig }
2142 1.682 rillig if ((op == LOGAND || op == LOGOR)
2143 1.682 rillig && rn->tn_op == ASSIGN && rn->u.ops.right->tn_op == CON) {
2144 1.682 rillig /* constant assignment of type '%s' in operand ... */
2145 1.690 rillig warning(382, expr_type_name(rn), op_name(op),
2146 1.682 rillig is_nonzero_val(&rn->u.ops.right->u.value)
2147 1.682 rillig ? "true" : "false");
2148 1.682 rillig }
2149 1.536 rillig type_t *rettp = mp->m_returns_bool
2150 1.506 rillig ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
2151 1.565 rillig ntn = build_op(op, sys, rettp, ln, rn);
2152 1.506 rillig break;
2153 1.506 rillig }
2154 1.1 cgd
2155 1.506 rillig if (ntn == NULL)
2156 1.506 rillig return NULL;
2157 1.1 cgd
2158 1.506 rillig if (mp->m_possible_precedence_confusion)
2159 1.506 rillig check_precedence_confusion(ntn);
2160 1.1 cgd
2161 1.589 rillig if (mp->m_fold_constant_operands && ln->tn_op == CON) {
2162 1.589 rillig if (!mp->m_binary || rn->tn_op == CON) {
2163 1.589 rillig if (mp->m_compares_with_zero)
2164 1.589 rillig ntn = fold_constant_compare_zero(ntn);
2165 1.589 rillig else if (is_floating(ntn->tn_type->t_tspec))
2166 1.589 rillig ntn = fold_constant_floating(ntn);
2167 1.589 rillig else
2168 1.589 rillig ntn = fold_constant_integer(ntn);
2169 1.589 rillig } else if (op == QUEST) {
2170 1.581 rillig lint_assert(has_operands(rn));
2171 1.612 rillig use(ln->u.value.u.integer != 0
2172 1.612 rillig ? rn->u.ops.right : rn->u.ops.left);
2173 1.612 rillig ntn = ln->u.value.u.integer != 0
2174 1.612 rillig ? rn->u.ops.left : rn->u.ops.right;
2175 1.506 rillig }
2176 1.1 cgd }
2177 1.1 cgd
2178 1.506 rillig return ntn;
2179 1.506 rillig }
2180 1.506 rillig
2181 1.506 rillig tnode_t *
2182 1.506 rillig build_unary(op_t op, bool sys, tnode_t *tn)
2183 1.506 rillig {
2184 1.506 rillig return build_binary(tn, op, sys, NULL);
2185 1.1 cgd }
2186 1.1 cgd
2187 1.536 rillig static bool
2188 1.536 rillig are_members_compatible(const sym_t *a, const sym_t *b)
2189 1.536 rillig {
2190 1.536 rillig if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
2191 1.536 rillig return false;
2192 1.536 rillig
2193 1.536 rillig const type_t *atp = a->s_type;
2194 1.536 rillig const type_t *btp = b->s_type;
2195 1.536 rillig bool w = false;
2196 1.536 rillig if (!types_compatible(atp, btp, false, false, &w) && !w)
2197 1.536 rillig return false;
2198 1.536 rillig if (a->s_bitfield != b->s_bitfield)
2199 1.536 rillig return false;
2200 1.536 rillig if (a->s_bitfield) {
2201 1.536 rillig if (atp->t_bit_field_width != btp->t_bit_field_width)
2202 1.536 rillig return false;
2203 1.536 rillig if (atp->t_bit_field_offset != btp->t_bit_field_offset)
2204 1.536 rillig return false;
2205 1.536 rillig }
2206 1.536 rillig return true;
2207 1.536 rillig }
2208 1.536 rillig
2209 1.1 cgd /*
2210 1.506 rillig * Return whether all struct/union members with the same name have the same
2211 1.506 rillig * type and offset.
2212 1.1 cgd */
2213 1.506 rillig static bool
2214 1.506 rillig all_members_compatible(const sym_t *msym)
2215 1.1 cgd {
2216 1.506 rillig for (const sym_t *csym = msym;
2217 1.584 rillig csym != NULL; csym = csym->s_symtab_next) {
2218 1.506 rillig if (!is_member(csym))
2219 1.506 rillig continue;
2220 1.506 rillig if (strcmp(msym->s_name, csym->s_name) != 0)
2221 1.506 rillig continue;
2222 1.506 rillig
2223 1.506 rillig for (const sym_t *sym = csym->s_symtab_next;
2224 1.584 rillig sym != NULL; sym = sym->s_symtab_next) {
2225 1.536 rillig if (is_member(sym)
2226 1.536 rillig && strcmp(csym->s_name, sym->s_name) == 0
2227 1.536 rillig && !are_members_compatible(csym, sym))
2228 1.506 rillig return false;
2229 1.1 cgd }
2230 1.1 cgd }
2231 1.506 rillig return true;
2232 1.1 cgd }
2233 1.1 cgd
2234 1.539 rillig sym_t *
2235 1.573 rillig find_member(const struct_or_union *sou, const char *name)
2236 1.537 rillig {
2237 1.573 rillig for (sym_t *mem = sou->sou_first_member;
2238 1.584 rillig mem != NULL; mem = mem->s_next) {
2239 1.572 rillig lint_assert(is_member(mem));
2240 1.573 rillig lint_assert(mem->u.s_member.sm_containing_type == sou);
2241 1.537 rillig if (strcmp(mem->s_name, name) == 0)
2242 1.537 rillig return mem;
2243 1.537 rillig }
2244 1.573 rillig
2245 1.573 rillig for (sym_t *mem = sou->sou_first_member;
2246 1.584 rillig mem != NULL; mem = mem->s_next) {
2247 1.573 rillig if (is_struct_or_union(mem->s_type->t_tspec)
2248 1.573 rillig && mem->s_name == unnamed) {
2249 1.573 rillig sym_t *nested_mem =
2250 1.611 rillig find_member(mem->s_type->u.sou, name);
2251 1.537 rillig if (nested_mem != NULL)
2252 1.537 rillig return nested_mem;
2253 1.537 rillig }
2254 1.537 rillig }
2255 1.537 rillig return NULL;
2256 1.537 rillig }
2257 1.537 rillig
2258 1.506 rillig /*
2259 1.570 rillig * Remove the member if it was unknown until now, which means
2260 1.570 rillig * that no defined struct or union has a member with the same name.
2261 1.570 rillig */
2262 1.570 rillig static void
2263 1.570 rillig remove_unknown_member(tnode_t *tn, sym_t *msym)
2264 1.570 rillig {
2265 1.570 rillig /* type '%s' does not have member '%s' */
2266 1.570 rillig error(101, type_name(tn->tn_type), msym->s_name);
2267 1.629 rillig symtab_remove_forever(msym);
2268 1.597 rillig msym->s_kind = SK_MEMBER;
2269 1.570 rillig msym->s_scl = STRUCT_MEMBER;
2270 1.570 rillig
2271 1.570 rillig struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
2272 1.570 rillig "struct_or_union");
2273 1.570 rillig sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
2274 1.570 rillig sou->sou_tag->s_name = unnamed;
2275 1.570 rillig
2276 1.570 rillig msym->u.s_member.sm_containing_type = sou;
2277 1.570 rillig /*
2278 1.586 rillig * The member sm_offset_in_bits is not needed here since this symbol
2279 1.586 rillig * can only be used for error reporting.
2280 1.570 rillig */
2281 1.570 rillig }
2282 1.570 rillig
2283 1.570 rillig /*
2284 1.574 rillig * Returns a symbol which has the same name as 'msym' and is a member of the
2285 1.574 rillig * struct or union specified by 'tn'.
2286 1.506 rillig */
2287 1.506 rillig static sym_t *
2288 1.506 rillig struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
2289 1.1 cgd {
2290 1.506 rillig
2291 1.537 rillig /* Determine the tag type of which msym is expected to be a member. */
2292 1.537 rillig const type_t *tp = NULL;
2293 1.536 rillig if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
2294 1.537 rillig tp = tn->tn_type;
2295 1.536 rillig if (op == ARROW && tn->tn_type->t_tspec == PTR
2296 1.536 rillig && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
2297 1.537 rillig tp = tn->tn_type->t_subt;
2298 1.611 rillig struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
2299 1.386 rillig
2300 1.543 rillig if (sou != NULL) {
2301 1.573 rillig sym_t *nested_mem = find_member(sou, msym->s_name);
2302 1.537 rillig if (nested_mem != NULL)
2303 1.537 rillig return nested_mem;
2304 1.537 rillig }
2305 1.537 rillig
2306 1.582 rillig if (msym->s_scl == NO_SCL) {
2307 1.571 rillig remove_unknown_member(tn, msym);
2308 1.571 rillig return msym;
2309 1.571 rillig }
2310 1.571 rillig
2311 1.506 rillig bool eq = all_members_compatible(msym);
2312 1.390 christos
2313 1.506 rillig /*
2314 1.586 rillig * Now handle the case in which the left operand refers really to a
2315 1.586 rillig * struct/union, but the right operand is not member of it.
2316 1.506 rillig */
2317 1.543 rillig if (sou != NULL) {
2318 1.604 rillig if (eq && !allow_c90)
2319 1.681 rillig /* invalid use of member '%s' */
2320 1.506 rillig warning(102, msym->s_name);
2321 1.604 rillig else
2322 1.681 rillig /* invalid use of member '%s' */
2323 1.506 rillig error(102, msym->s_name);
2324 1.506 rillig return msym;
2325 1.506 rillig }
2326 1.390 christos
2327 1.506 rillig if (eq) {
2328 1.506 rillig if (op == POINT) {
2329 1.604 rillig if (!allow_c90)
2330 1.506 rillig /* left operand of '.' must be struct ... */
2331 1.690 rillig warning(103, expr_type_name(tn));
2332 1.604 rillig else
2333 1.506 rillig /* left operand of '.' must be struct ... */
2334 1.690 rillig error(103, expr_type_name(tn));
2335 1.506 rillig } else {
2336 1.604 rillig if (!allow_c90 && tn->tn_type->t_tspec == PTR)
2337 1.506 rillig /* left operand of '->' must be pointer ... */
2338 1.690 rillig warning(104, expr_type_name(tn));
2339 1.604 rillig else
2340 1.506 rillig /* left operand of '->' must be pointer ... */
2341 1.690 rillig error(104, expr_type_name(tn));
2342 1.506 rillig }
2343 1.506 rillig } else {
2344 1.604 rillig if (!allow_c90)
2345 1.506 rillig /* non-unique member requires struct/union %s */
2346 1.506 rillig error(105, op == POINT ? "object" : "pointer");
2347 1.604 rillig else
2348 1.506 rillig /* unacceptable operand of '%s' */
2349 1.506 rillig error(111, op_name(op));
2350 1.506 rillig }
2351 1.389 rillig
2352 1.506 rillig return msym;
2353 1.506 rillig }
2354 1.386 rillig
2355 1.506 rillig tnode_t *
2356 1.506 rillig build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
2357 1.506 rillig {
2358 1.526 rillig sym_t *msym;
2359 1.386 rillig
2360 1.506 rillig if (ln == NULL)
2361 1.506 rillig return NULL;
2362 1.389 rillig
2363 1.604 rillig if (op == ARROW)
2364 1.506 rillig /* must do this before struct_or_union_member is called */
2365 1.506 rillig ln = cconv(ln);
2366 1.506 rillig msym = struct_or_union_member(ln, op, getsym(member));
2367 1.506 rillig return build_binary(ln, op, sys, build_name(msym, false));
2368 1.386 rillig }
2369 1.386 rillig
2370 1.1 cgd /*
2371 1.506 rillig * Perform class conversions.
2372 1.506 rillig *
2373 1.506 rillig * Arrays of type T are converted into pointers to type T.
2374 1.506 rillig * Functions are converted to pointers to functions.
2375 1.506 rillig * Lvalues are converted to rvalues.
2376 1.506 rillig *
2377 1.506 rillig * C99 6.3 "Conversions"
2378 1.506 rillig * C99 6.3.2 "Other operands"
2379 1.506 rillig * C99 6.3.2.1 "Lvalues, arrays, and function designators"
2380 1.1 cgd */
2381 1.506 rillig tnode_t *
2382 1.506 rillig cconv(tnode_t *tn)
2383 1.1 cgd {
2384 1.506 rillig if (tn->tn_type->t_tspec == ARRAY) {
2385 1.506 rillig if (!tn->tn_lvalue) {
2386 1.506 rillig /* XXX print correct operator */
2387 1.506 rillig /* %soperand of '%s' must be lvalue */
2388 1.506 rillig gnuism(114, "", op_name(ADDR));
2389 1.506 rillig }
2390 1.565 rillig tn = build_op(ADDR, tn->tn_sys,
2391 1.506 rillig expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
2392 1.506 rillig }
2393 1.1 cgd
2394 1.506 rillig if (tn->tn_type->t_tspec == FUNC)
2395 1.673 rillig tn = build_address(tn->tn_sys, tn);
2396 1.1 cgd
2397 1.506 rillig if (tn->tn_lvalue) {
2398 1.509 rillig type_t *tp = expr_dup_type(tn->tn_type);
2399 1.506 rillig /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
2400 1.506 rillig tp->t_const = tp->t_volatile = false;
2401 1.565 rillig tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
2402 1.1 cgd }
2403 1.1 cgd
2404 1.506 rillig return tn;
2405 1.1 cgd }
2406 1.1 cgd
2407 1.506 rillig const tnode_t *
2408 1.506 rillig before_conversion(const tnode_t *tn)
2409 1.496 rillig {
2410 1.506 rillig while (tn->tn_op == CVT && !tn->tn_cast)
2411 1.612 rillig tn = tn->u.ops.left;
2412 1.506 rillig return tn;
2413 1.496 rillig }
2414 1.496 rillig
2415 1.496 rillig /*
2416 1.578 rillig * Most errors required by C90 are reported in struct_or_union_member().
2417 1.506 rillig * Here we only check for totally wrong things.
2418 1.496 rillig */
2419 1.506 rillig static bool
2420 1.506 rillig typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
2421 1.496 rillig {
2422 1.506 rillig if (is_struct_or_union(lt))
2423 1.506 rillig return true;
2424 1.496 rillig
2425 1.506 rillig if (lt == FUNC || lt == VOID || ltp->t_bitfield)
2426 1.506 rillig goto wrong;
2427 1.496 rillig
2428 1.506 rillig /*
2429 1.506 rillig * Some C dialects from before C90 tolerated any lvalue on the
2430 1.586 rillig * left-hand side of the '.' operator, allowing things like 'char
2431 1.586 rillig * st[100]; st.st_mtime', assuming that the member 'st_mtime' only
2432 1.586 rillig * occurred in a single struct; see typeok_arrow.
2433 1.506 rillig */
2434 1.506 rillig if (ln->tn_lvalue)
2435 1.506 rillig return true;
2436 1.1 cgd
2437 1.506 rillig wrong:
2438 1.506 rillig /* With allow_c90 we already got an error */
2439 1.506 rillig if (!allow_c90)
2440 1.506 rillig /* unacceptable operand of '%s' */
2441 1.506 rillig error(111, op_name(POINT));
2442 1.1 cgd
2443 1.506 rillig return false;
2444 1.506 rillig }
2445 1.1 cgd
2446 1.506 rillig static bool
2447 1.506 rillig typeok_arrow(tspec_t lt)
2448 1.506 rillig {
2449 1.496 rillig /*
2450 1.506 rillig * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
2451 1.586 rillig * before '.', and that lvalue is then assumed to have the form of the
2452 1.586 rillig * structure of which the name of the right is a member. [...] Such
2453 1.586 rillig * constructions are non-portable.</quote>
2454 1.496 rillig */
2455 1.506 rillig if (lt == PTR || (!allow_c90 && is_integer(lt)))
2456 1.506 rillig return true;
2457 1.506 rillig
2458 1.506 rillig /* With allow_c90 we already got an error */
2459 1.506 rillig if (!allow_c90)
2460 1.506 rillig /* unacceptable operand of '%s' */
2461 1.506 rillig error(111, op_name(ARROW));
2462 1.506 rillig return false;
2463 1.1 cgd }
2464 1.1 cgd
2465 1.506 rillig static bool
2466 1.506 rillig typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
2467 1.488 rillig {
2468 1.506 rillig /* operand has scalar type (checked in typeok) */
2469 1.506 rillig if (!tn->tn_lvalue) {
2470 1.506 rillig if (tn->tn_op == CVT && tn->tn_cast &&
2471 1.612 rillig tn->u.ops.left->tn_op == LOAD)
2472 1.506 rillig /* a cast does not yield an lvalue */
2473 1.506 rillig error(163);
2474 1.506 rillig /* %soperand of '%s' must be lvalue */
2475 1.506 rillig error(114, "", op_name(op));
2476 1.506 rillig return false;
2477 1.509 rillig }
2478 1.604 rillig if (tp->t_const && allow_c90)
2479 1.509 rillig /* %soperand of '%s' must be modifiable lvalue */
2480 1.509 rillig warning(115, "", op_name(op));
2481 1.506 rillig return true;
2482 1.488 rillig }
2483 1.488 rillig
2484 1.506 rillig static bool
2485 1.560 rillig typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
2486 1.497 rillig {
2487 1.506 rillig if (t == ARRAY || t == FUNC) {
2488 1.506 rillig /* ok, a warning comes later (in build_address()) */
2489 1.506 rillig } else if (!tn->tn_lvalue) {
2490 1.506 rillig if (tn->tn_op == CVT && tn->tn_cast &&
2491 1.612 rillig tn->u.ops.left->tn_op == LOAD)
2492 1.506 rillig /* a cast does not yield an lvalue */
2493 1.506 rillig error(163);
2494 1.506 rillig /* %soperand of '%s' must be lvalue */
2495 1.560 rillig error(114, "", op_name(op));
2496 1.506 rillig return false;
2497 1.506 rillig } else if (is_scalar(t)) {
2498 1.506 rillig if (tp->t_bitfield) {
2499 1.506 rillig /* cannot take address of bit-field */
2500 1.506 rillig error(112);
2501 1.506 rillig return false;
2502 1.506 rillig }
2503 1.506 rillig } else if (t != STRUCT && t != UNION) {
2504 1.506 rillig /* unacceptable operand of '%s' */
2505 1.560 rillig error(111, op_name(op));
2506 1.506 rillig return false;
2507 1.506 rillig }
2508 1.612 rillig if (tn->tn_op == NAME && tn->u.sym->s_register) {
2509 1.506 rillig /* cannot take address of register '%s' */
2510 1.612 rillig error(113, tn->u.sym->s_name);
2511 1.506 rillig return false;
2512 1.506 rillig }
2513 1.506 rillig return true;
2514 1.497 rillig }
2515 1.497 rillig
2516 1.506 rillig static bool
2517 1.506 rillig typeok_indir(const type_t *tp, tspec_t t)
2518 1.497 rillig {
2519 1.497 rillig
2520 1.506 rillig if (t != PTR) {
2521 1.506 rillig /* cannot dereference non-pointer type '%s' */
2522 1.506 rillig error(96, type_name(tp));
2523 1.506 rillig return false;
2524 1.506 rillig }
2525 1.506 rillig return true;
2526 1.497 rillig }
2527 1.497 rillig
2528 1.1 cgd static void
2529 1.506 rillig warn_incompatible_types(op_t op,
2530 1.506 rillig const type_t *ltp, tspec_t lt,
2531 1.506 rillig const type_t *rtp, tspec_t rt)
2532 1.1 cgd {
2533 1.560 rillig bool binary = modtab[op].m_binary;
2534 1.1 cgd
2535 1.560 rillig if (lt == VOID || (binary && rt == VOID)) {
2536 1.681 rillig /* void type invalid in expression */
2537 1.506 rillig error(109);
2538 1.604 rillig } else if (op == ASSIGN)
2539 1.506 rillig /* cannot assign to '%s' from '%s' */
2540 1.506 rillig error(171, type_name(ltp), type_name(rtp));
2541 1.604 rillig else if (binary)
2542 1.506 rillig /* operands of '%s' have incompatible types '%s' and '%s' */
2543 1.560 rillig error(107, op_name(op), type_name(ltp), type_name(rtp));
2544 1.604 rillig else {
2545 1.524 rillig lint_assert(rt == NO_TSPEC);
2546 1.506 rillig /* operand of '%s' has invalid type '%s' */
2547 1.560 rillig error(108, op_name(op), type_name(ltp));
2548 1.506 rillig }
2549 1.1 cgd }
2550 1.1 cgd
2551 1.506 rillig static bool
2552 1.506 rillig typeok_plus(op_t op,
2553 1.506 rillig const type_t *ltp, tspec_t lt,
2554 1.506 rillig const type_t *rtp, tspec_t rt)
2555 1.471 rillig {
2556 1.506 rillig /* operands have scalar types (checked in typeok) */
2557 1.506 rillig if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
2558 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2559 1.506 rillig return false;
2560 1.506 rillig }
2561 1.506 rillig return true;
2562 1.471 rillig }
2563 1.471 rillig
2564 1.506 rillig static bool
2565 1.506 rillig typeok_minus(op_t op,
2566 1.506 rillig const type_t *ltp, tspec_t lt,
2567 1.506 rillig const type_t *rtp, tspec_t rt)
2568 1.1 cgd {
2569 1.506 rillig /* operands have scalar types (checked in typeok) */
2570 1.506 rillig if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
2571 1.506 rillig (lt != PTR && rt == PTR)) {
2572 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2573 1.506 rillig return false;
2574 1.506 rillig }
2575 1.509 rillig if (lt == PTR && rt == PTR &&
2576 1.509 rillig !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2577 1.681 rillig /* invalid pointer subtraction */
2578 1.509 rillig error(116);
2579 1.1 cgd }
2580 1.506 rillig return true;
2581 1.1 cgd }
2582 1.1 cgd
2583 1.506 rillig static void
2584 1.688 rillig typeok_shr(const tnode_t *ln, tspec_t lt,
2585 1.506 rillig const tnode_t *rn, tspec_t rt)
2586 1.344 rillig {
2587 1.536 rillig tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
2588 1.536 rillig tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
2589 1.367 rillig
2590 1.689 rillig if (is_uinteger(olt))
2591 1.689 rillig return;
2592 1.689 rillig
2593 1.689 rillig if (pflag) {
2594 1.506 rillig integer_constraints lc = ic_expr(ln);
2595 1.667 rillig if (lc.bclr >> 63 != 0)
2596 1.506 rillig return;
2597 1.367 rillig
2598 1.604 rillig if (ln->tn_op != CON)
2599 1.690 rillig /* bitwise '%s' on signed '%s' possibly nonportable */
2600 1.690 rillig warning(117, ">>", expr_type_name(ln));
2601 1.612 rillig else if (ln->u.value.u.integer < 0)
2602 1.690 rillig /* bitwise '%s' on signed '%s' nonportable */
2603 1.690 rillig warning(120, ">>", expr_type_name(ln));
2604 1.689 rillig return;
2605 1.364 rillig }
2606 1.689 rillig
2607 1.689 rillig /* The left operand would become unsigned in traditional C. */
2608 1.689 rillig if (allow_trad && allow_c90 && hflag && is_uinteger(ort)
2609 1.689 rillig && !(ln->tn_op == CON && ln->u.value.u.integer >= 0))
2610 1.689 rillig /* '%s' %s '%s' differs between traditional C and C90 */
2611 1.689 rillig warning(118, tspec_name(lt), ">>", tspec_name(rt));
2612 1.689 rillig
2613 1.689 rillig /*
2614 1.689 rillig * In traditional C, the left operand would be extended
2615 1.689 rillig * (possibly sign-extended) and then shifted.
2616 1.689 rillig */
2617 1.689 rillig if (allow_trad && allow_c90 && hflag && !is_uinteger(ort)
2618 1.689 rillig && portable_rank_cmp(lt, rt) < 0
2619 1.689 rillig && !(ln->tn_op == CON && ln->u.value.u.integer >= 0))
2620 1.689 rillig /* '%s' %s '%s' differs between traditional C and C90 */
2621 1.689 rillig warning(118, tspec_name(lt), ">>", tspec_name(rt));
2622 1.506 rillig }
2623 1.364 rillig
2624 1.506 rillig static void
2625 1.693 rillig typeok_shl_signed_to_msb(const tnode_t *ln, const tnode_t *rn)
2626 1.693 rillig {
2627 1.693 rillig integer_constraints lc = ic_expr(ln);
2628 1.693 rillig int64_t n;
2629 1.693 rillig unsigned lw = width_in_bits(ln->tn_type);
2630 1.693 rillig if (!is_uinteger(ln->tn_type->t_tspec)
2631 1.693 rillig && ln->tn_op != CON
2632 1.693 rillig && ((lc.smin == 0 && lc.smax != 0
2633 1.693 rillig && lc.smax != INT64_MAX && (lc.smax & (lc.smax + 1)) == 0)
2634 1.693 rillig || (lc.smin != INT64_MAX && lc.smin + 1 == -lc.smax))
2635 1.693 rillig && rn->tn_op == CON
2636 1.693 rillig && (n = rn->u.value.u.integer, 1 <= n && n <= lw)
2637 1.693 rillig && u64_width((uint64_t)lc.smax - (uint64_t)lc.smin) + n == lw)
2638 1.693 rillig /* bitwise '%s' on signed '%s' possibly nonportable */
2639 1.693 rillig warning(117, "<<", expr_type_name(ln));
2640 1.693 rillig }
2641 1.693 rillig
2642 1.693 rillig static void
2643 1.693 rillig typeok_shl(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2644 1.506 rillig {
2645 1.344 rillig /*
2646 1.689 rillig * Traditional C performs the usual arithmetic conversions on the
2647 1.689 rillig * operands, C90 and later don't.
2648 1.688 rillig */
2649 1.688 rillig if (hflag && allow_trad && allow_c90 && portable_rank_cmp(lt, rt) < 0)
2650 1.688 rillig /* '%s' %s '%s' differs between traditional C and C90 */
2651 1.688 rillig warning(118, tspec_name(lt), "<<", tspec_name(rt));
2652 1.693 rillig
2653 1.693 rillig typeok_shl_signed_to_msb(ln, rn);
2654 1.344 rillig }
2655 1.344 rillig
2656 1.1 cgd static void
2657 1.690 rillig typeok_shift(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2658 1.1 cgd {
2659 1.506 rillig if (rn->tn_op != CON)
2660 1.1 cgd return;
2661 1.1 cgd
2662 1.612 rillig if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
2663 1.506 rillig /* negative shift */
2664 1.506 rillig warning(121);
2665 1.612 rillig else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
2666 1.506 rillig /* shift amount %u equals bit-size of '%s' */
2667 1.690 rillig warning(267,
2668 1.690 rillig (unsigned)rn->u.value.u.integer, expr_type_name(ln));
2669 1.612 rillig else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
2670 1.506 rillig /* shift amount %llu is greater than bit-size %llu of '%s' */
2671 1.612 rillig warning(122, (unsigned long long)rn->u.value.u.integer,
2672 1.506 rillig (unsigned long long)size_in_bits(lt),
2673 1.690 rillig expr_type_name(ln));
2674 1.364 rillig }
2675 1.1 cgd }
2676 1.1 cgd
2677 1.434 rillig static bool
2678 1.506 rillig is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2679 1.434 rillig {
2680 1.506 rillig if (lt == PTR && is_null_pointer(rn))
2681 1.506 rillig return true;
2682 1.506 rillig if (rt == PTR && is_null_pointer(ln))
2683 1.444 rillig return true;
2684 1.434 rillig return false;
2685 1.434 rillig }
2686 1.434 rillig
2687 1.1 cgd static void
2688 1.560 rillig warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
2689 1.1 cgd {
2690 1.506 rillig lint_assert(ltp->t_tspec == PTR);
2691 1.506 rillig lint_assert(rtp->t_tspec == PTR);
2692 1.115 rillig
2693 1.509 rillig tspec_t lt = ltp->t_subt->t_tspec;
2694 1.509 rillig tspec_t rt = rtp->t_subt->t_tspec;
2695 1.1 cgd
2696 1.506 rillig if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
2697 1.604 rillig if (op == RETURN)
2698 1.681 rillig /* invalid structure pointer combination */
2699 1.506 rillig warning(244);
2700 1.604 rillig else {
2701 1.506 rillig /* incompatible structure pointers: '%s' '%s' '%s' */
2702 1.560 rillig warning(245, type_name(ltp),
2703 1.560 rillig op_name(op), type_name(rtp));
2704 1.1 cgd }
2705 1.506 rillig } else {
2706 1.604 rillig if (op == RETURN)
2707 1.681 rillig /* invalid combination of '%s' and '%s' */
2708 1.506 rillig warning(184, type_name(ltp), type_name(rtp));
2709 1.604 rillig else {
2710 1.681 rillig /* invalid combination of '%s' and '%s', op '%s' */
2711 1.506 rillig warning(124,
2712 1.560 rillig type_name(ltp), type_name(rtp), op_name(op));
2713 1.20 lukem }
2714 1.1 cgd }
2715 1.1 cgd }
2716 1.1 cgd
2717 1.1 cgd static void
2718 1.506 rillig check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
2719 1.1 cgd {
2720 1.509 rillig type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
2721 1.509 rillig tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
2722 1.20 lukem
2723 1.506 rillig if (lst == VOID || rst == VOID) {
2724 1.506 rillig /* TODO: C99 behaves like C90 here. */
2725 1.589 rillig if (!allow_trad && !allow_c99 &&
2726 1.506 rillig (lst == FUNC || rst == FUNC)) {
2727 1.509 rillig /* (void *)0 is already handled in typeok() */
2728 1.509 rillig const char *lsts, *rsts;
2729 1.506 rillig *(lst == FUNC ? &lsts : &rsts) = "function pointer";
2730 1.506 rillig *(lst == VOID ? &lsts : &rsts) = "'void *'";
2731 1.578 rillig /* C90 or later forbid comparison of %s with %s */
2732 1.506 rillig warning(274, lsts, rsts);
2733 1.506 rillig }
2734 1.1 cgd return;
2735 1.506 rillig }
2736 1.506 rillig
2737 1.506 rillig if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2738 1.560 rillig warn_incompatible_pointers(op, ltp, rtp);
2739 1.1 cgd return;
2740 1.506 rillig }
2741 1.1 cgd
2742 1.506 rillig if (lst == FUNC && rst == FUNC) {
2743 1.506 rillig /* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
2744 1.589 rillig if (!allow_trad && !allow_c99 && op != EQ && op != NE)
2745 1.578 rillig /* pointers to functions can only be compared ... */
2746 1.506 rillig warning(125);
2747 1.1 cgd }
2748 1.1 cgd }
2749 1.1 cgd
2750 1.247 rillig static bool
2751 1.506 rillig typeok_compare(op_t op,
2752 1.506 rillig const tnode_t *ln, const type_t *ltp, tspec_t lt,
2753 1.506 rillig const tnode_t *rn, const type_t *rtp, tspec_t rt)
2754 1.462 rillig {
2755 1.506 rillig if (lt == PTR && rt == PTR) {
2756 1.506 rillig check_pointer_comparison(op, ln, rn);
2757 1.506 rillig return true;
2758 1.506 rillig }
2759 1.506 rillig
2760 1.506 rillig if (lt != PTR && rt != PTR)
2761 1.506 rillig return true;
2762 1.506 rillig
2763 1.506 rillig if (!is_integer(lt) && !is_integer(rt)) {
2764 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2765 1.506 rillig return false;
2766 1.506 rillig }
2767 1.462 rillig
2768 1.509 rillig const char *lx = lt == PTR ? "pointer" : "integer";
2769 1.509 rillig const char *rx = rt == PTR ? "pointer" : "integer";
2770 1.681 rillig /* invalid combination of %s '%s' and %s '%s', op '%s' */
2771 1.506 rillig warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2772 1.506 rillig return true;
2773 1.462 rillig }
2774 1.462 rillig
2775 1.462 rillig static bool
2776 1.506 rillig typeok_quest(tspec_t lt, const tnode_t *rn)
2777 1.506 rillig {
2778 1.506 rillig if (!is_scalar(lt)) {
2779 1.557 rillig /* first operand of '?' must have scalar type */
2780 1.506 rillig error(170);
2781 1.506 rillig return false;
2782 1.506 rillig }
2783 1.506 rillig lint_assert(before_conversion(rn)->tn_op == COLON);
2784 1.506 rillig return true;
2785 1.506 rillig }
2786 1.506 rillig
2787 1.506 rillig static void
2788 1.560 rillig typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
2789 1.461 rillig {
2790 1.506 rillig type_t *lstp = ltp->t_subt;
2791 1.506 rillig type_t *rstp = rtp->t_subt;
2792 1.506 rillig tspec_t lst = lstp->t_tspec;
2793 1.506 rillig tspec_t rst = rstp->t_tspec;
2794 1.506 rillig
2795 1.506 rillig if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
2796 1.506 rillig /* (void *)0 is handled in typeok_colon */
2797 1.506 rillig /* TODO: C99 behaves like C90 here. */
2798 1.506 rillig if (!allow_trad && !allow_c99)
2799 1.578 rillig /* conversion of %s to %s requires a cast, op %s */
2800 1.506 rillig warning(305, "function pointer", "'void *'",
2801 1.560 rillig op_name(COLON));
2802 1.506 rillig return;
2803 1.506 rillig }
2804 1.461 rillig
2805 1.506 rillig if (pointer_types_are_compatible(lstp, rstp, true))
2806 1.506 rillig return;
2807 1.506 rillig if (!types_compatible(lstp, rstp, true, false, NULL))
2808 1.560 rillig warn_incompatible_pointers(COLON, ltp, rtp);
2809 1.461 rillig }
2810 1.461 rillig
2811 1.461 rillig static bool
2812 1.560 rillig typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
2813 1.506 rillig const tnode_t *rn, const type_t *rtp, tspec_t rt)
2814 1.247 rillig {
2815 1.462 rillig
2816 1.506 rillig if (is_arithmetic(lt) && is_arithmetic(rt))
2817 1.506 rillig return true;
2818 1.506 rillig if (lt == BOOL && rt == BOOL)
2819 1.506 rillig return true;
2820 1.463 rillig
2821 1.611 rillig if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
2822 1.506 rillig return true;
2823 1.611 rillig if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
2824 1.506 rillig return true;
2825 1.272 rillig
2826 1.506 rillig if (lt == PTR && is_null_pointer(rn))
2827 1.506 rillig return true;
2828 1.506 rillig if (rt == PTR && is_null_pointer(ln))
2829 1.506 rillig return true;
2830 1.274 rillig
2831 1.506 rillig if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
2832 1.506 rillig const char *lx = lt == PTR ? "pointer" : "integer";
2833 1.506 rillig const char *rx = rt == PTR ? "pointer" : "integer";
2834 1.681 rillig /* invalid combination of %s '%s' and %s '%s', op '%s' */
2835 1.506 rillig warning(123, lx, type_name(ltp),
2836 1.560 rillig rx, type_name(rtp), op_name(COLON));
2837 1.506 rillig return true;
2838 1.506 rillig }
2839 1.249 rillig
2840 1.506 rillig if (lt == VOID || rt == VOID) {
2841 1.506 rillig if (lt != VOID || rt != VOID)
2842 1.506 rillig /* incompatible types '%s' and '%s' in conditional */
2843 1.690 rillig warning(126, expr_type_name(ln), expr_type_name(rn));
2844 1.506 rillig return true;
2845 1.461 rillig }
2846 1.461 rillig
2847 1.506 rillig if (lt == PTR && rt == PTR) {
2848 1.560 rillig typeok_colon_pointer(ltp, rtp);
2849 1.460 rillig return true;
2850 1.506 rillig }
2851 1.460 rillig
2852 1.506 rillig /* incompatible types '%s' and '%s' in conditional */
2853 1.690 rillig error(126, expr_type_name(ln), expr_type_name(rn));
2854 1.506 rillig return false;
2855 1.247 rillig }
2856 1.247 rillig
2857 1.506 rillig static bool
2858 1.506 rillig has_constant_member(const type_t *tp)
2859 1.1 cgd {
2860 1.506 rillig lint_assert(is_struct_or_union(tp->t_tspec));
2861 1.506 rillig
2862 1.611 rillig for (sym_t *m = tp->u.sou->sou_first_member;
2863 1.584 rillig m != NULL; m = m->s_next) {
2864 1.506 rillig const type_t *mtp = m->s_type;
2865 1.506 rillig if (mtp->t_const)
2866 1.506 rillig return true;
2867 1.506 rillig if (is_struct_or_union(mtp->t_tspec) &&
2868 1.506 rillig has_constant_member(mtp))
2869 1.506 rillig return true;
2870 1.506 rillig }
2871 1.506 rillig return false;
2872 1.506 rillig }
2873 1.1 cgd
2874 1.506 rillig static bool
2875 1.506 rillig typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
2876 1.506 rillig {
2877 1.506 rillig if (op == RETURN || op == INIT || op == FARG)
2878 1.506 rillig return true;
2879 1.248 rillig
2880 1.506 rillig if (!ln->tn_lvalue) {
2881 1.506 rillig if (ln->tn_op == CVT && ln->tn_cast &&
2882 1.612 rillig ln->u.ops.left->tn_op == LOAD)
2883 1.506 rillig /* a cast does not yield an lvalue */
2884 1.506 rillig error(163);
2885 1.506 rillig /* %soperand of '%s' must be lvalue */
2886 1.506 rillig error(114, "left ", op_name(op));
2887 1.506 rillig return false;
2888 1.536 rillig } else if (ltp->t_const
2889 1.536 rillig || (is_struct_or_union(lt) && has_constant_member(ltp))) {
2890 1.506 rillig if (allow_c90)
2891 1.506 rillig /* %soperand of '%s' must be modifiable lvalue */
2892 1.506 rillig warning(115, "left ", op_name(op));
2893 1.1 cgd }
2894 1.506 rillig return true;
2895 1.506 rillig }
2896 1.20 lukem
2897 1.506 rillig static bool
2898 1.506 rillig typeok_scalar(op_t op, const mod_t *mp,
2899 1.506 rillig const type_t *ltp, tspec_t lt,
2900 1.506 rillig const type_t *rtp, tspec_t rt)
2901 1.506 rillig {
2902 1.506 rillig if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
2903 1.506 rillig return true;
2904 1.506 rillig if (mp->m_requires_integer) {
2905 1.506 rillig if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
2906 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2907 1.506 rillig return false;
2908 1.506 rillig }
2909 1.506 rillig } else if (mp->m_requires_integer_or_complex) {
2910 1.506 rillig if ((!is_integer(lt) && !is_complex(lt)) ||
2911 1.506 rillig (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
2912 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2913 1.506 rillig return false;
2914 1.506 rillig }
2915 1.506 rillig } else if (mp->m_requires_scalar) {
2916 1.506 rillig if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
2917 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2918 1.506 rillig return false;
2919 1.506 rillig }
2920 1.506 rillig } else if (mp->m_requires_arith) {
2921 1.506 rillig if (!is_arithmetic(lt) ||
2922 1.506 rillig (mp->m_binary && !is_arithmetic(rt))) {
2923 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
2924 1.506 rillig return false;
2925 1.506 rillig }
2926 1.1 cgd }
2927 1.506 rillig return true;
2928 1.1 cgd }
2929 1.1 cgd
2930 1.282 rillig static void
2931 1.506 rillig check_assign_void_pointer(op_t op, int arg,
2932 1.506 rillig tspec_t lt, tspec_t lst,
2933 1.506 rillig tspec_t rt, tspec_t rst)
2934 1.282 rillig {
2935 1.506 rillig
2936 1.506 rillig if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
2937 1.506 rillig return;
2938 1.506 rillig /* two pointers, at least one pointer to void */
2939 1.506 rillig
2940 1.506 rillig /* TODO: C99 behaves like C90 here. */
2941 1.589 rillig if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
2942 1.506 rillig return;
2943 1.506 rillig /* comb. of ptr to func and ptr to void */
2944 1.506 rillig
2945 1.509 rillig const char *lts, *rts;
2946 1.506 rillig *(lst == FUNC ? <s : &rts) = "function pointer";
2947 1.506 rillig *(lst == VOID ? <s : &rts) = "'void *'";
2948 1.282 rillig
2949 1.506 rillig switch (op) {
2950 1.506 rillig case INIT:
2951 1.506 rillig case RETURN:
2952 1.578 rillig /* conversion of %s to %s requires a cast */
2953 1.506 rillig warning(303, rts, lts);
2954 1.506 rillig break;
2955 1.506 rillig case FARG:
2956 1.578 rillig /* conversion of %s to %s requires a cast, arg #%d */
2957 1.506 rillig warning(304, rts, lts, arg);
2958 1.506 rillig break;
2959 1.282 rillig default:
2960 1.578 rillig /* conversion of %s to %s requires a cast, op %s */
2961 1.506 rillig warning(305, rts, lts, op_name(op));
2962 1.506 rillig break;
2963 1.282 rillig }
2964 1.282 rillig }
2965 1.282 rillig
2966 1.286 rillig static bool
2967 1.506 rillig is_direct_function_call(const tnode_t *tn, const char **out_name)
2968 1.286 rillig {
2969 1.506 rillig
2970 1.603 rillig if (tn->tn_op == CALL
2971 1.612 rillig && tn->u.call->func->tn_op == ADDR
2972 1.612 rillig && tn->u.call->func->u.ops.left->tn_op == NAME) {
2973 1.612 rillig *out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
2974 1.603 rillig return true;
2975 1.603 rillig }
2976 1.603 rillig return false;
2977 1.286 rillig }
2978 1.286 rillig
2979 1.506 rillig static bool
2980 1.506 rillig is_unconst_function(const char *name)
2981 1.285 rillig {
2982 1.506 rillig
2983 1.506 rillig return strcmp(name, "memchr") == 0 ||
2984 1.584 rillig strcmp(name, "strchr") == 0 ||
2985 1.584 rillig strcmp(name, "strpbrk") == 0 ||
2986 1.584 rillig strcmp(name, "strrchr") == 0 ||
2987 1.584 rillig strcmp(name, "strstr") == 0;
2988 1.285 rillig }
2989 1.285 rillig
2990 1.506 rillig static bool
2991 1.506 rillig is_const_char_pointer(const tnode_t *tn)
2992 1.285 rillig {
2993 1.506 rillig /*
2994 1.506 rillig * For traditional reasons, C99 6.4.5p5 defines that string literals
2995 1.586 rillig * have type 'char[]'. They are often implicitly converted to 'char
2996 1.586 rillig * *', for example when they are passed as function arguments.
2997 1.506 rillig *
2998 1.506 rillig * C99 6.4.5p6 further defines that modifying a string that is
2999 1.506 rillig * constructed from a string literal invokes undefined behavior.
3000 1.506 rillig *
3001 1.506 rillig * Out of these reasons, string literals are treated as 'effectively
3002 1.506 rillig * const' here.
3003 1.506 rillig */
3004 1.506 rillig if (tn->tn_op == CVT &&
3005 1.612 rillig tn->u.ops.left->tn_op == ADDR &&
3006 1.612 rillig tn->u.ops.left->u.ops.left->tn_op == STRING)
3007 1.506 rillig return true;
3008 1.506 rillig
3009 1.509 rillig const type_t *tp = before_conversion(tn)->tn_type;
3010 1.506 rillig return tp->t_tspec == PTR &&
3011 1.584 rillig tp->t_subt->t_tspec == CHAR &&
3012 1.584 rillig tp->t_subt->t_const;
3013 1.285 rillig }
3014 1.285 rillig
3015 1.506 rillig static bool
3016 1.506 rillig is_const_pointer(const tnode_t *tn)
3017 1.285 rillig {
3018 1.509 rillig const type_t *tp = before_conversion(tn)->tn_type;
3019 1.506 rillig return tp->t_tspec == PTR && tp->t_subt->t_const;
3020 1.285 rillig }
3021 1.285 rillig
3022 1.283 rillig static void
3023 1.506 rillig check_unconst_function(const type_t *lstp, const tnode_t *rn)
3024 1.283 rillig {
3025 1.506 rillig const char *function_name;
3026 1.506 rillig
3027 1.506 rillig if (lstp->t_tspec == CHAR && !lstp->t_const &&
3028 1.506 rillig is_direct_function_call(rn, &function_name) &&
3029 1.506 rillig is_unconst_function(function_name) &&
3030 1.625 rillig rn->u.call->args_len >= 1 &&
3031 1.625 rillig is_const_char_pointer(rn->u.call->args[0])) {
3032 1.506 rillig /* call to '%s' effectively discards 'const' from argument */
3033 1.506 rillig warning(346, function_name);
3034 1.506 rillig }
3035 1.283 rillig
3036 1.506 rillig if (!lstp->t_const &&
3037 1.506 rillig is_direct_function_call(rn, &function_name) &&
3038 1.506 rillig strcmp(function_name, "bsearch") == 0 &&
3039 1.625 rillig rn->u.call->args_len >= 2 &&
3040 1.625 rillig is_const_pointer(rn->u.call->args[1])) {
3041 1.506 rillig /* call to '%s' effectively discards 'const' from argument */
3042 1.506 rillig warning(346, function_name);
3043 1.283 rillig }
3044 1.283 rillig }
3045 1.283 rillig
3046 1.506 rillig static bool
3047 1.683 rillig check_assign_void_pointer_compat(op_t op, const function_call *call, int arg,
3048 1.660 rillig tspec_t lt,
3049 1.598 rillig const type_t *lstp, tspec_t lst,
3050 1.598 rillig const tnode_t *rn,
3051 1.598 rillig const type_t *rtp, tspec_t rt,
3052 1.598 rillig const type_t *rstp, tspec_t rst)
3053 1.1 cgd {
3054 1.506 rillig if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
3055 1.506 rillig types_compatible(lstp, rstp,
3056 1.506 rillig true, false, NULL))))
3057 1.506 rillig return false;
3058 1.1 cgd
3059 1.506 rillig /* compatible pointer types (qualifiers ignored) */
3060 1.659 rillig char qualifiers[32];
3061 1.659 rillig snprintf(qualifiers, sizeof(qualifiers), "%s%s",
3062 1.659 rillig !lstp->t_const && rstp->t_const ? " const" : "",
3063 1.659 rillig !lstp->t_volatile && rstp->t_volatile ? " volatile" : "");
3064 1.659 rillig if (allow_c90 && qualifiers[0] != '\0') {
3065 1.506 rillig switch (op) {
3066 1.506 rillig case INIT:
3067 1.506 rillig case RETURN:
3068 1.659 rillig /* '%s' discards '%s' from '%s' */
3069 1.659 rillig warning(182, op_name(op),
3070 1.659 rillig qualifiers + 1, type_name(rtp));
3071 1.506 rillig break;
3072 1.506 rillig case FARG:
3073 1.683 rillig /* passing '%s' as argument %d to '%s' discards '%s' */
3074 1.683 rillig warning(383, type_name(rtp), arg,
3075 1.683 rillig function_call_descr(call), qualifiers + 1);
3076 1.506 rillig break;
3077 1.506 rillig default:
3078 1.659 rillig /* operator '%s' discards '%s' from '%s' */
3079 1.506 rillig warning(128, op_name(op),
3080 1.659 rillig qualifiers + 1, type_name(rtp));
3081 1.506 rillig break;
3082 1.506 rillig }
3083 1.147 rillig }
3084 1.147 rillig
3085 1.506 rillig if (allow_c90)
3086 1.506 rillig check_unconst_function(lstp, rn);
3087 1.1 cgd
3088 1.506 rillig return true;
3089 1.1 cgd }
3090 1.1 cgd
3091 1.506 rillig static bool
3092 1.506 rillig check_assign_pointer_integer(op_t op, int arg,
3093 1.598 rillig const type_t *ltp, tspec_t lt,
3094 1.598 rillig const type_t *rtp, tspec_t rt)
3095 1.1 cgd {
3096 1.506 rillig
3097 1.506 rillig if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
3098 1.506 rillig return false;
3099 1.1 cgd
3100 1.536 rillig const char *lx = lt == PTR ? "pointer" : "integer";
3101 1.536 rillig const char *rx = rt == PTR ? "pointer" : "integer";
3102 1.1 cgd
3103 1.506 rillig switch (op) {
3104 1.506 rillig case INIT:
3105 1.506 rillig case RETURN:
3106 1.681 rillig /* invalid combination of %s '%s' and %s '%s' for '%s' */
3107 1.671 rillig warning(183,
3108 1.671 rillig lx, type_name(ltp), rx, type_name(rtp), op_name(op));
3109 1.506 rillig break;
3110 1.506 rillig case FARG:
3111 1.681 rillig /* invalid combination of %s '%s' and %s '%s', arg #%d */
3112 1.506 rillig warning(154,
3113 1.506 rillig lx, type_name(ltp), rx, type_name(rtp), arg);
3114 1.506 rillig break;
3115 1.506 rillig default:
3116 1.681 rillig /* invalid combination of %s '%s' and %s '%s', op '%s' */
3117 1.506 rillig warning(123,
3118 1.506 rillig lx, type_name(ltp), rx, type_name(rtp), op_name(op));
3119 1.506 rillig break;
3120 1.1 cgd }
3121 1.506 rillig return true;
3122 1.1 cgd }
3123 1.1 cgd
3124 1.506 rillig static bool
3125 1.506 rillig check_assign_pointer(op_t op, int arg,
3126 1.506 rillig const type_t *ltp, tspec_t lt,
3127 1.506 rillig const type_t *rtp, tspec_t rt)
3128 1.1 cgd {
3129 1.506 rillig if (!(lt == PTR && rt == PTR))
3130 1.506 rillig return false;
3131 1.1 cgd
3132 1.560 rillig if (op == FARG)
3133 1.506 rillig /* converting '%s' to incompatible '%s' for ... */
3134 1.506 rillig warning(153, type_name(rtp), type_name(ltp), arg);
3135 1.560 rillig else
3136 1.560 rillig warn_incompatible_pointers(op, ltp, rtp);
3137 1.506 rillig return true;
3138 1.506 rillig }
3139 1.1 cgd
3140 1.506 rillig static void
3141 1.506 rillig warn_assign(op_t op, int arg,
3142 1.506 rillig const type_t *ltp, tspec_t lt,
3143 1.506 rillig const type_t *rtp, tspec_t rt)
3144 1.506 rillig {
3145 1.506 rillig switch (op) {
3146 1.506 rillig case INIT:
3147 1.506 rillig /* cannot initialize '%s' from '%s' */
3148 1.506 rillig error(185, type_name(ltp), type_name(rtp));
3149 1.506 rillig break;
3150 1.506 rillig case RETURN:
3151 1.506 rillig /* function has return type '%s' but returns '%s' */
3152 1.506 rillig error(211, type_name(ltp), type_name(rtp));
3153 1.506 rillig break;
3154 1.506 rillig case FARG:
3155 1.506 rillig /* passing '%s' to incompatible '%s', arg #%d */
3156 1.506 rillig warning(155, type_name(rtp), type_name(ltp), arg);
3157 1.506 rillig break;
3158 1.506 rillig default:
3159 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
3160 1.506 rillig break;
3161 1.1 cgd }
3162 1.1 cgd }
3163 1.1 cgd
3164 1.506 rillig /*
3165 1.506 rillig * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
3166 1.506 rillig * and prints warnings/errors if necessary.
3167 1.506 rillig * Returns whether the types are (almost) compatible.
3168 1.506 rillig */
3169 1.506 rillig static bool
3170 1.683 rillig check_assign_types_compatible(op_t op, const function_call *call, int arg,
3171 1.506 rillig const tnode_t *ln, const tnode_t *rn)
3172 1.1 cgd {
3173 1.526 rillig tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
3174 1.526 rillig type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
3175 1.506 rillig
3176 1.506 rillig if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
3177 1.506 rillig lst = (lstp = ltp->t_subt)->t_tspec;
3178 1.506 rillig if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
3179 1.506 rillig rst = (rstp = rtp->t_subt)->t_tspec;
3180 1.506 rillig
3181 1.506 rillig if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
3182 1.506 rillig return true;
3183 1.20 lukem
3184 1.506 rillig if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
3185 1.506 rillig return true;
3186 1.1 cgd
3187 1.506 rillig if (is_struct_or_union(lt) && is_struct_or_union(rt))
3188 1.611 rillig return ltp->u.sou == rtp->u.sou;
3189 1.263 rillig
3190 1.533 rillig if (lt == PTR && is_null_pointer(rn)) {
3191 1.533 rillig if (is_integer(rn->tn_type->t_tspec))
3192 1.533 rillig /* implicit conversion from integer 0 to pointer ... */
3193 1.533 rillig query_message(15, type_name(ltp));
3194 1.506 rillig return true;
3195 1.533 rillig }
3196 1.263 rillig
3197 1.506 rillig check_assign_void_pointer(op, arg, lt, lst, rt, rst);
3198 1.263 rillig
3199 1.683 rillig if (check_assign_void_pointer_compat(op, call, arg,
3200 1.660 rillig lt, lstp, lst, rn, rtp, rt, rstp, rst))
3201 1.506 rillig return true;
3202 1.1 cgd
3203 1.506 rillig if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
3204 1.506 rillig return true;
3205 1.1 cgd
3206 1.506 rillig if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
3207 1.506 rillig return true;
3208 1.107 rillig
3209 1.506 rillig warn_assign(op, arg, ltp, lt, rtp, rt);
3210 1.149 rillig return false;
3211 1.1 cgd }
3212 1.1 cgd
3213 1.506 rillig static bool
3214 1.506 rillig has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
3215 1.1 cgd {
3216 1.506 rillig op_t op = tn->tn_op;
3217 1.1 cgd
3218 1.506 rillig if (modtab[op].m_has_side_effect)
3219 1.506 rillig return true;
3220 1.1 cgd
3221 1.506 rillig if (op == CVT && tn->tn_type->t_tspec == VOID)
3222 1.612 rillig return has_side_effect(tn->u.ops.left);
3223 1.1 cgd
3224 1.612 rillig /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
3225 1.506 rillig if (op == LOGAND || op == LOGOR)
3226 1.612 rillig return has_side_effect(tn->u.ops.right);
3227 1.1 cgd
3228 1.612 rillig /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
3229 1.506 rillig if (op == QUEST)
3230 1.612 rillig return has_side_effect(tn->u.ops.right);
3231 1.1 cgd
3232 1.506 rillig if (op == COLON || op == COMMA) {
3233 1.612 rillig return has_side_effect(tn->u.ops.left) ||
3234 1.612 rillig has_side_effect(tn->u.ops.right);
3235 1.506 rillig }
3236 1.1 cgd
3237 1.506 rillig return false;
3238 1.506 rillig }
3239 1.1 cgd
3240 1.506 rillig static bool
3241 1.506 rillig is_void_cast(const tnode_t *tn)
3242 1.506 rillig {
3243 1.1 cgd
3244 1.506 rillig return tn->tn_op == CVT && tn->tn_cast &&
3245 1.584 rillig tn->tn_type->t_tspec == VOID;
3246 1.1 cgd }
3247 1.1 cgd
3248 1.506 rillig static bool
3249 1.506 rillig is_local_symbol(const tnode_t *tn)
3250 1.1 cgd {
3251 1.1 cgd
3252 1.506 rillig return tn->tn_op == LOAD &&
3253 1.612 rillig tn->u.ops.left->tn_op == NAME &&
3254 1.612 rillig tn->u.ops.left->u.sym->s_scl == AUTO;
3255 1.506 rillig }
3256 1.1 cgd
3257 1.506 rillig static bool
3258 1.506 rillig is_int_constant_zero(const tnode_t *tn)
3259 1.506 rillig {
3260 1.1 cgd
3261 1.506 rillig return tn->tn_op == CON &&
3262 1.584 rillig tn->tn_type->t_tspec == INT &&
3263 1.612 rillig tn->u.value.u.integer == 0;
3264 1.1 cgd }
3265 1.1 cgd
3266 1.506 rillig static void
3267 1.506 rillig check_null_effect(const tnode_t *tn)
3268 1.46 christos {
3269 1.46 christos
3270 1.509 rillig if (hflag &&
3271 1.509 rillig !has_side_effect(tn) &&
3272 1.612 rillig !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
3273 1.612 rillig !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
3274 1.509 rillig /* expression has null effect */
3275 1.509 rillig warning(129);
3276 1.509 rillig }
3277 1.506 rillig }
3278 1.275 rillig
3279 1.506 rillig /*
3280 1.506 rillig * Check the types for specific operators and type combinations.
3281 1.506 rillig *
3282 1.506 rillig * At this point, the operands already conform to the type requirements of
3283 1.506 rillig * the operator, such as being integer, floating or scalar.
3284 1.506 rillig */
3285 1.506 rillig static bool
3286 1.683 rillig typeok_op(op_t op, const function_call *call, int arg,
3287 1.506 rillig const tnode_t *ln, const type_t *ltp, tspec_t lt,
3288 1.506 rillig const tnode_t *rn, const type_t *rtp, tspec_t rt)
3289 1.506 rillig {
3290 1.506 rillig switch (op) {
3291 1.506 rillig case ARROW:
3292 1.506 rillig return typeok_arrow(lt);
3293 1.506 rillig case POINT:
3294 1.506 rillig return typeok_point(ln, ltp, lt);
3295 1.506 rillig case INCBEF:
3296 1.506 rillig case DECBEF:
3297 1.506 rillig case INCAFT:
3298 1.506 rillig case DECAFT:
3299 1.506 rillig return typeok_incdec(op, ln, ltp);
3300 1.506 rillig case INDIR:
3301 1.506 rillig return typeok_indir(ltp, lt);
3302 1.506 rillig case ADDR:
3303 1.560 rillig return typeok_address(op, ln, ltp, lt);
3304 1.506 rillig case PLUS:
3305 1.506 rillig return typeok_plus(op, ltp, lt, rtp, rt);
3306 1.506 rillig case MINUS:
3307 1.506 rillig return typeok_minus(op, ltp, lt, rtp, rt);
3308 1.506 rillig case SHL:
3309 1.693 rillig typeok_shl(ln, lt, rn, rt);
3310 1.506 rillig goto shift;
3311 1.506 rillig case SHR:
3312 1.688 rillig typeok_shr(ln, lt, rn, rt);
3313 1.506 rillig shift:
3314 1.690 rillig typeok_shift(ln, lt, rn, rt);
3315 1.52 matt break;
3316 1.506 rillig case LT:
3317 1.506 rillig case LE:
3318 1.506 rillig case GT:
3319 1.506 rillig case GE:
3320 1.506 rillig compare:
3321 1.506 rillig return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
3322 1.506 rillig case EQ:
3323 1.506 rillig case NE:
3324 1.506 rillig if (is_typeok_eq(ln, lt, rn, rt))
3325 1.506 rillig break;
3326 1.506 rillig goto compare;
3327 1.506 rillig case QUEST:
3328 1.506 rillig return typeok_quest(lt, rn);
3329 1.506 rillig case COLON:
3330 1.560 rillig return typeok_colon(ln, ltp, lt, rn, rtp, rt);
3331 1.506 rillig case ASSIGN:
3332 1.506 rillig case INIT:
3333 1.506 rillig case FARG:
3334 1.506 rillig case RETURN:
3335 1.683 rillig if (!check_assign_types_compatible(op, call, arg, ln, rn))
3336 1.506 rillig return false;
3337 1.506 rillig goto assign;
3338 1.506 rillig case MULASS:
3339 1.506 rillig case DIVASS:
3340 1.506 rillig case MODASS:
3341 1.506 rillig goto assign;
3342 1.506 rillig case ADDASS:
3343 1.506 rillig case SUBASS:
3344 1.506 rillig if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
3345 1.506 rillig warn_incompatible_types(op, ltp, lt, rtp, rt);
3346 1.506 rillig return false;
3347 1.506 rillig }
3348 1.506 rillig goto assign;
3349 1.506 rillig case SHLASS:
3350 1.506 rillig goto assign;
3351 1.506 rillig case SHRASS:
3352 1.506 rillig if (pflag && !is_uinteger(lt) &&
3353 1.506 rillig !(!allow_c90 && is_uinteger(rt))) {
3354 1.690 rillig /* bitwise '%s' on signed '%s' possibly nonportable */
3355 1.690 rillig warning(117, op_name(op), expr_type_name(rn));
3356 1.506 rillig }
3357 1.506 rillig goto assign;
3358 1.506 rillig case ANDASS:
3359 1.506 rillig case XORASS:
3360 1.506 rillig case ORASS:
3361 1.506 rillig assign:
3362 1.506 rillig return typeok_assign(op, ln, ltp, lt);
3363 1.506 rillig case COMMA:
3364 1.506 rillig if (!modtab[ln->tn_op].m_has_side_effect)
3365 1.506 rillig check_null_effect(ln);
3366 1.46 christos break;
3367 1.506 rillig default:
3368 1.46 christos break;
3369 1.46 christos }
3370 1.506 rillig return true;
3371 1.46 christos }
3372 1.342 rillig
3373 1.506 rillig static void
3374 1.506 rillig check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
3375 1.1 cgd {
3376 1.20 lukem
3377 1.506 rillig if (!eflag)
3378 1.506 rillig return;
3379 1.1 cgd
3380 1.536 rillig /* Allow enum in array indices. */
3381 1.506 rillig if (op == PLUS &&
3382 1.506 rillig ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
3383 1.506 rillig (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
3384 1.506 rillig return;
3385 1.1 cgd }
3386 1.20 lukem
3387 1.556 rillig /* dubious operation '%s' on enum */
3388 1.506 rillig warning(241, op_name(op));
3389 1.1 cgd }
3390 1.1 cgd
3391 1.506 rillig static void
3392 1.506 rillig check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3393 1.1 cgd {
3394 1.509 rillig const mod_t *mp = &modtab[op];
3395 1.1 cgd
3396 1.611 rillig if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
3397 1.506 rillig switch (op) {
3398 1.506 rillig case INIT:
3399 1.506 rillig /* enum type mismatch between '%s' and '%s' in ... */
3400 1.506 rillig warning(210,
3401 1.506 rillig type_name(ln->tn_type), type_name(rn->tn_type));
3402 1.506 rillig break;
3403 1.506 rillig case FARG:
3404 1.506 rillig /* function expects '%s', passing '%s' for arg #%d */
3405 1.506 rillig warning(156,
3406 1.506 rillig type_name(ln->tn_type), type_name(rn->tn_type),
3407 1.506 rillig arg);
3408 1.506 rillig break;
3409 1.506 rillig case RETURN:
3410 1.506 rillig /* function has return type '%s' but returns '%s' */
3411 1.506 rillig warning(211,
3412 1.506 rillig type_name(ln->tn_type), type_name(rn->tn_type));
3413 1.506 rillig break;
3414 1.506 rillig default:
3415 1.506 rillig /* enum type mismatch: '%s' '%s' '%s' */
3416 1.690 rillig warning(130, expr_type_name(before_conversion(ln)),
3417 1.690 rillig op_name(op),
3418 1.690 rillig expr_type_name(before_conversion(rn)));
3419 1.506 rillig break;
3420 1.506 rillig }
3421 1.558 rillig } else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
3422 1.558 rillig /* operator '%s' assumes that '%s' is ordered */
3423 1.560 rillig warning(243, op_name(op), type_name(ln->tn_type));
3424 1.1 cgd }
3425 1.1 cgd
3426 1.506 rillig static void
3427 1.506 rillig check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3428 1.1 cgd {
3429 1.1 cgd
3430 1.506 rillig if (!eflag)
3431 1.506 rillig return;
3432 1.1 cgd
3433 1.506 rillig switch (op) {
3434 1.506 rillig case INIT:
3435 1.506 rillig /*
3436 1.506 rillig * Initialization with 0 is allowed. Otherwise, all implicit
3437 1.506 rillig * initializations would need to be warned upon as well.
3438 1.506 rillig */
3439 1.506 rillig if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
3440 1.506 rillig is_integer(rn->tn_type->t_tspec) &&
3441 1.612 rillig rn->u.value.u.integer == 0) {
3442 1.506 rillig return;
3443 1.1 cgd }
3444 1.506 rillig /* initialization of '%s' with '%s' */
3445 1.690 rillig warning(277, type_name(ln->tn_type), expr_type_name(rn));
3446 1.506 rillig break;
3447 1.506 rillig case FARG:
3448 1.506 rillig /* combination of '%s' and '%s', arg #%d */
3449 1.506 rillig warning(278,
3450 1.690 rillig type_name(ln->tn_type), expr_type_name(rn), arg);
3451 1.506 rillig break;
3452 1.506 rillig case RETURN:
3453 1.506 rillig /* combination of '%s' and '%s' in return */
3454 1.690 rillig warning(279, type_name(ln->tn_type), expr_type_name(rn));
3455 1.506 rillig break;
3456 1.506 rillig default:
3457 1.506 rillig /* combination of '%s' and '%s', op '%s' */
3458 1.690 rillig warning(242, type_name(ln->tn_type), expr_type_name(rn),
3459 1.506 rillig op_name(op));
3460 1.506 rillig break;
3461 1.1 cgd }
3462 1.1 cgd }
3463 1.1 cgd
3464 1.506 rillig static void
3465 1.506 rillig typeok_enum(op_t op, const mod_t *mp, int arg,
3466 1.506 rillig const tnode_t *ln, const type_t *ltp,
3467 1.506 rillig const tnode_t *rn, const type_t *rtp)
3468 1.473 rillig {
3469 1.506 rillig if (mp->m_bad_on_enum &&
3470 1.506 rillig (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
3471 1.506 rillig check_bad_enum_operation(op, ln, rn);
3472 1.506 rillig } else if (mp->m_valid_on_enum &&
3473 1.584 rillig (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
3474 1.506 rillig check_enum_type_mismatch(op, arg, ln, rn);
3475 1.506 rillig } else if (mp->m_valid_on_enum &&
3476 1.584 rillig (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
3477 1.506 rillig check_enum_int_mismatch(op, arg, ln, rn);
3478 1.473 rillig }
3479 1.473 rillig }
3480 1.473 rillig
3481 1.506 rillig /* Perform most type checks. Return whether the types are ok. */
3482 1.506 rillig bool
3483 1.683 rillig typeok(op_t op, const function_call *call, int arg,
3484 1.683 rillig const tnode_t *ln, const tnode_t *rn)
3485 1.1 cgd {
3486 1.1 cgd
3487 1.509 rillig const mod_t *mp = &modtab[op];
3488 1.1 cgd
3489 1.536 rillig type_t *ltp = ln->tn_type;
3490 1.536 rillig tspec_t lt = ltp->t_tspec;
3491 1.1 cgd
3492 1.536 rillig type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
3493 1.536 rillig tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
3494 1.1 cgd
3495 1.506 rillig if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
3496 1.506 rillig return false;
3497 1.506 rillig if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
3498 1.506 rillig return false;
3499 1.1 cgd
3500 1.683 rillig if (!typeok_op(op, call, arg, ln, ltp, lt, rn, rtp, rt))
3501 1.506 rillig return false;
3502 1.1 cgd
3503 1.506 rillig typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
3504 1.506 rillig return true;
3505 1.506 rillig }
3506 1.471 rillig
3507 1.506 rillig /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
3508 1.506 rillig static tspec_t
3509 1.506 rillig promote_trad(tspec_t t)
3510 1.506 rillig {
3511 1.1 cgd
3512 1.506 rillig if (t == UCHAR || t == USHORT)
3513 1.506 rillig return UINT;
3514 1.506 rillig if (t == CHAR || t == SCHAR || t == SHORT)
3515 1.506 rillig return INT;
3516 1.506 rillig if (t == FLOAT)
3517 1.506 rillig return DOUBLE;
3518 1.506 rillig if (t == ENUM)
3519 1.506 rillig return INT;
3520 1.506 rillig return t;
3521 1.1 cgd }
3522 1.1 cgd
3523 1.1 cgd /*
3524 1.506 rillig * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
3525 1.506 rillig * can represent all the values of the original type, the value is converted
3526 1.506 rillig * to an int; otherwise it is converted to an unsigned int", and that "All
3527 1.506 rillig * other types are unchanged by the integer promotions".
3528 1.1 cgd */
3529 1.506 rillig static tspec_t
3530 1.506 rillig promote_c90(const tnode_t *tn, tspec_t t, bool farg)
3531 1.1 cgd {
3532 1.506 rillig if (tn->tn_type->t_bitfield) {
3533 1.532 rillig unsigned int width = tn->tn_type->t_bit_field_width;
3534 1.532 rillig unsigned int int_width = size_in_bits(INT);
3535 1.532 rillig // XXX: What about _Bool bit-fields, since C99?
3536 1.532 rillig if (width < int_width)
3537 1.506 rillig return INT;
3538 1.532 rillig if (width == int_width)
3539 1.506 rillig return is_uinteger(t) ? UINT : INT;
3540 1.506 rillig return t;
3541 1.506 rillig }
3542 1.1 cgd
3543 1.506 rillig if (t == CHAR || t == SCHAR)
3544 1.506 rillig return INT;
3545 1.506 rillig if (t == UCHAR)
3546 1.506 rillig return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
3547 1.506 rillig if (t == SHORT)
3548 1.506 rillig return INT;
3549 1.506 rillig if (t == USHORT)
3550 1.506 rillig return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
3551 1.506 rillig if (t == ENUM)
3552 1.506 rillig return INT;
3553 1.506 rillig if (farg && t == FLOAT)
3554 1.506 rillig return DOUBLE;
3555 1.506 rillig return t;
3556 1.506 rillig }
3557 1.1 cgd
3558 1.506 rillig /*
3559 1.506 rillig * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
3560 1.506 rillig * integer types to either int or unsigned int.
3561 1.506 rillig *
3562 1.506 rillig * If allow_c90 is unset or the operand is a function argument with no type
3563 1.506 rillig * information (no prototype or variable # of args), converts float to double.
3564 1.506 rillig */
3565 1.506 rillig tnode_t *
3566 1.506 rillig promote(op_t op, bool farg, tnode_t *tn)
3567 1.506 rillig {
3568 1.506 rillig
3569 1.624 rillig const type_t *otp = tn->tn_type;
3570 1.624 rillig tspec_t ot = otp->t_tspec;
3571 1.506 rillig if (!is_arithmetic(ot))
3572 1.506 rillig return tn;
3573 1.506 rillig
3574 1.506 rillig tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
3575 1.506 rillig if (nt == ot)
3576 1.506 rillig return tn;
3577 1.506 rillig
3578 1.624 rillig type_t *ntp = expr_dup_type(gettyp(nt));
3579 1.506 rillig ntp->t_tspec = nt;
3580 1.624 rillig ntp->t_is_enum = otp->t_is_enum;
3581 1.624 rillig if (ntp->t_is_enum)
3582 1.624 rillig ntp->u.enumer = otp->u.enumer;
3583 1.506 rillig return convert(op, 0, ntp, tn);
3584 1.506 rillig }
3585 1.506 rillig
3586 1.506 rillig static void
3587 1.646 rillig check_lossy_floating_to_integer_conversion(
3588 1.646 rillig op_t op, int arg, const type_t *tp, const tnode_t *tn)
3589 1.646 rillig {
3590 1.646 rillig long double x = tn->u.value.u.floating;
3591 1.646 rillig integer_constraints ic = ic_any(tp);
3592 1.646 rillig if (is_uinteger(tp->t_tspec)
3593 1.646 rillig ? x >= ic.umin && x <= ic.umax && x == (uint64_t)x
3594 1.646 rillig : x >= ic.smin && x <= ic.smax && x == (int64_t)x)
3595 1.646 rillig return;
3596 1.646 rillig if (op == FARG)
3597 1.646 rillig /* lossy conversion of %Lg to '%s', arg #%d */
3598 1.646 rillig warning(380, x, type_name(tp), arg);
3599 1.646 rillig else
3600 1.646 rillig /* lossy conversion of %Lg to '%s' */
3601 1.646 rillig warning(381, x, type_name(tp));
3602 1.646 rillig }
3603 1.646 rillig
3604 1.646 rillig static void
3605 1.646 rillig convert_integer_from_floating(
3606 1.646 rillig op_t op, int arg, const type_t *tp, const tnode_t *tn)
3607 1.506 rillig {
3608 1.506 rillig
3609 1.506 rillig if (op == CVT)
3610 1.506 rillig /* cast from floating point '%s' to integer '%s' */
3611 1.506 rillig query_message(2, type_name(tn->tn_type), type_name(tp));
3612 1.506 rillig else
3613 1.506 rillig /* implicit conversion from floating point '%s' to ... */
3614 1.506 rillig query_message(1, type_name(tn->tn_type), type_name(tp));
3615 1.646 rillig if (tn->tn_op == CON)
3616 1.646 rillig check_lossy_floating_to_integer_conversion(op, arg, tp, tn);
3617 1.506 rillig }
3618 1.506 rillig
3619 1.506 rillig static bool
3620 1.506 rillig should_warn_about_prototype_conversion(tspec_t nt,
3621 1.506 rillig tspec_t ot, const tnode_t *ptn)
3622 1.506 rillig {
3623 1.506 rillig
3624 1.506 rillig if (nt == ot)
3625 1.506 rillig return false;
3626 1.1 cgd
3627 1.506 rillig if (nt == ENUM && ot == INT)
3628 1.506 rillig return false;
3629 1.1 cgd
3630 1.506 rillig if (is_floating(nt) != is_floating(ot) ||
3631 1.550 rillig portable_rank_cmp(nt, ot) != 0) {
3632 1.506 rillig /* representation and/or width change */
3633 1.506 rillig if (!is_integer(ot))
3634 1.506 rillig return true;
3635 1.506 rillig /*
3636 1.506 rillig * XXX: Investigate whether this rule makes sense; see
3637 1.506 rillig * tests/usr.bin/xlint/lint1/platform_long.c.
3638 1.506 rillig */
3639 1.550 rillig return portable_rank_cmp(ot, INT) > 0;
3640 1.1 cgd }
3641 1.1 cgd
3642 1.506 rillig if (!hflag)
3643 1.506 rillig return false;
3644 1.1 cgd
3645 1.506 rillig /*
3646 1.506 rillig * If the types differ only in sign and the argument has the same
3647 1.506 rillig * representation in both types, print no warning.
3648 1.506 rillig */
3649 1.506 rillig if (ptn->tn_op == CON && is_integer(nt) &&
3650 1.506 rillig signed_type(nt) == signed_type(ot) &&
3651 1.612 rillig !msb(ptn->u.value.u.integer, ot))
3652 1.506 rillig return false;
3653 1.1 cgd
3654 1.506 rillig return true;
3655 1.1 cgd }
3656 1.1 cgd
3657 1.1 cgd /*
3658 1.506 rillig * Warn if a prototype causes a type conversion that is different from what
3659 1.506 rillig * would happen to the same argument in the absence of a prototype. This
3660 1.506 rillig * check is intended for code that needs to stay compatible with pre-C90 C.
3661 1.506 rillig *
3662 1.681 rillig * Errors/warnings about invalid type combinations are already printed
3663 1.506 rillig * in check_assign_types_compatible().
3664 1.22 perry */
3665 1.506 rillig static void
3666 1.506 rillig check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
3667 1.506 rillig tnode_t *tn)
3668 1.506 rillig {
3669 1.506 rillig
3670 1.506 rillig if (!is_arithmetic(nt) || !is_arithmetic(ot))
3671 1.506 rillig return;
3672 1.506 rillig
3673 1.506 rillig /*
3674 1.586 rillig * If the type of the formal parameter is char/short, a warning would
3675 1.586 rillig * be useless, because functions declared the old style can't expect
3676 1.586 rillig * char/short arguments.
3677 1.506 rillig */
3678 1.506 rillig if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
3679 1.506 rillig nt == SHORT || nt == USHORT)
3680 1.506 rillig return;
3681 1.506 rillig
3682 1.509 rillig tnode_t *ptn = promote(NOOP, true, tn);
3683 1.506 rillig ot = ptn->tn_type->t_tspec;
3684 1.506 rillig
3685 1.506 rillig if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
3686 1.555 rillig /* argument %d is converted from '%s' to '%s' ... */
3687 1.690 rillig warning(259, arg, expr_type_name(tn), type_name(tp));
3688 1.506 rillig }
3689 1.506 rillig }
3690 1.506 rillig
3691 1.22 perry /*
3692 1.506 rillig * When converting a large integer type to a small integer type, in some
3693 1.506 rillig * cases the value of the actual expression is further restricted than the
3694 1.506 rillig * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
3695 1.1 cgd */
3696 1.506 rillig static bool
3697 1.506 rillig can_represent(const type_t *tp, const tnode_t *tn)
3698 1.1 cgd {
3699 1.506 rillig uint64_t nmask = value_bits(width_in_bits(tp));
3700 1.506 rillig if (!is_uinteger(tp->t_tspec))
3701 1.506 rillig nmask >>= 1;
3702 1.506 rillig
3703 1.506 rillig integer_constraints c = ic_expr(tn);
3704 1.506 rillig if ((~c.bclr & ~nmask) == 0)
3705 1.506 rillig return true;
3706 1.506 rillig
3707 1.521 rillig integer_constraints tpc = ic_any(tp);
3708 1.521 rillig if (is_uinteger(tp->t_tspec)
3709 1.521 rillig ? tpc.umin <= c.umin && tpc.umax >= c.umax
3710 1.521 rillig : tpc.smin <= c.smin && tpc.smax >= c.smax)
3711 1.521 rillig return true;
3712 1.521 rillig
3713 1.654 rillig debug_enter();
3714 1.654 rillig debug_step("type '%s' cannot represent:", type_name(tp));
3715 1.654 rillig debug_node(tn);
3716 1.654 rillig debug_leave();
3717 1.506 rillig return false;
3718 1.506 rillig }
3719 1.506 rillig
3720 1.577 rillig static bool
3721 1.577 rillig should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
3722 1.577 rillig const tnode_t *otn, tspec_t ot)
3723 1.577 rillig {
3724 1.577 rillig
3725 1.577 rillig // XXX: The portable_rank_cmp aims at portable mode, independent of the
3726 1.577 rillig // current platform, while can_represent acts on the actual type sizes
3727 1.577 rillig // from the current platform. This mix is inconsistent, but anything
3728 1.577 rillig // else would make the exact conditions too complicated to grasp.
3729 1.577 rillig if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
3730 1.577 rillig if (ot == LONG || ot == ULONG
3731 1.577 rillig || ot == LLONG || ot == ULLONG
3732 1.577 rillig #ifdef INT128_SIZE
3733 1.577 rillig || ot == INT128 || ot == UINT128
3734 1.577 rillig #endif
3735 1.577 rillig || aflag > 1)
3736 1.577 rillig return !can_represent(ntp, otn);
3737 1.577 rillig }
3738 1.577 rillig return false;
3739 1.577 rillig }
3740 1.577 rillig
3741 1.506 rillig static void
3742 1.506 rillig convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
3743 1.506 rillig type_t *tp, tnode_t *tn)
3744 1.506 rillig {
3745 1.1 cgd
3746 1.506 rillig if (tn->tn_op == CON)
3747 1.506 rillig return;
3748 1.1 cgd
3749 1.506 rillig if (op == CVT)
3750 1.506 rillig return;
3751 1.1 cgd
3752 1.506 rillig if (Pflag && pflag && aflag > 0 &&
3753 1.550 rillig portable_rank_cmp(nt, ot) > 0 &&
3754 1.506 rillig is_uinteger(nt) != is_uinteger(ot)) {
3755 1.604 rillig if (op == FARG)
3756 1.506 rillig /* conversion to '%s' may sign-extend ... */
3757 1.506 rillig warning(297, type_name(tp), arg);
3758 1.604 rillig else
3759 1.506 rillig /* conversion to '%s' may sign-extend ... */
3760 1.506 rillig warning(131, type_name(tp));
3761 1.506 rillig }
3762 1.506 rillig
3763 1.550 rillig if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
3764 1.506 rillig (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
3765 1.506 rillig tn->tn_op == SHL)) {
3766 1.506 rillig /* suggest cast from '%s' to '%s' on op '%s' to ... */
3767 1.506 rillig warning(324, type_name(gettyp(ot)), type_name(tp),
3768 1.506 rillig op_name(tn->tn_op));
3769 1.506 rillig }
3770 1.506 rillig
3771 1.577 rillig if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
3772 1.690 rillig if (op == FARG)
3773 1.506 rillig /* conversion from '%s' to '%s' may lose ... */
3774 1.690 rillig warning(298, expr_type_name(tn), type_name(tp), arg);
3775 1.690 rillig else
3776 1.506 rillig /* conversion from '%s' to '%s' may lose accuracy */
3777 1.690 rillig warning(132, expr_type_name(tn), type_name(tp));
3778 1.506 rillig }
3779 1.506 rillig
3780 1.661 rillig if (is_uinteger(nt) != is_uinteger(ot))
3781 1.506 rillig /* implicit conversion changes sign from '%s' to '%s' */
3782 1.690 rillig query_message(3, expr_type_name(tn), type_name(tp));
3783 1.506 rillig }
3784 1.506 rillig
3785 1.506 rillig static void
3786 1.506 rillig convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
3787 1.506 rillig {
3788 1.506 rillig
3789 1.506 rillig if (tn->tn_op == CON)
3790 1.506 rillig return;
3791 1.506 rillig if (op != CVT)
3792 1.514 rillig return; /* We already got an error. */
3793 1.550 rillig if (portable_rank_cmp(nt, PTR) >= 0)
3794 1.506 rillig return;
3795 1.506 rillig
3796 1.506 rillig if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
3797 1.506 rillig /* conversion of pointer to '%s' may lose bits */
3798 1.506 rillig warning(134, type_name(tp));
3799 1.506 rillig } else {
3800 1.506 rillig /* conversion of pointer to '%s' loses bits */
3801 1.506 rillig warning(133, type_name(tp));
3802 1.506 rillig }
3803 1.506 rillig }
3804 1.506 rillig
3805 1.506 rillig static bool
3806 1.506 rillig struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
3807 1.506 rillig {
3808 1.506 rillig
3809 1.611 rillig return struct_tp->u.sou->sou_first_member != NULL &&
3810 1.611 rillig types_compatible(struct_tp->u.sou->sou_first_member->s_type,
3811 1.584 rillig member_tp, true, false, NULL);
3812 1.506 rillig }
3813 1.506 rillig
3814 1.506 rillig static bool
3815 1.506 rillig is_byte_array(const type_t *tp)
3816 1.506 rillig {
3817 1.506 rillig
3818 1.506 rillig return tp->t_tspec == ARRAY &&
3819 1.584 rillig (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
3820 1.506 rillig }
3821 1.506 rillig
3822 1.506 rillig static bool
3823 1.519 rillig union_contains(const type_t *utp, const type_t *mtp)
3824 1.519 rillig {
3825 1.611 rillig for (const sym_t *mem = utp->u.sou->sou_first_member;
3826 1.584 rillig mem != NULL; mem = mem->s_next) {
3827 1.519 rillig if (types_compatible(mem->s_type, mtp, true, false, NULL))
3828 1.519 rillig return true;
3829 1.519 rillig }
3830 1.519 rillig return false;
3831 1.519 rillig }
3832 1.519 rillig
3833 1.519 rillig static bool
3834 1.506 rillig should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
3835 1.506 rillig const type_t *ostp, tspec_t ost)
3836 1.506 rillig {
3837 1.506 rillig
3838 1.506 rillig while (nst == ARRAY)
3839 1.506 rillig nstp = nstp->t_subt, nst = nstp->t_tspec;
3840 1.506 rillig while (ost == ARRAY)
3841 1.506 rillig ostp = ostp->t_subt, ost = ostp->t_tspec;
3842 1.506 rillig
3843 1.506 rillig if (nst == STRUCT && ost == STRUCT &&
3844 1.506 rillig (struct_starts_with(nstp, ostp) ||
3845 1.506 rillig struct_starts_with(ostp, nstp)))
3846 1.506 rillig return false;
3847 1.506 rillig
3848 1.506 rillig if (is_incomplete(nstp) || is_incomplete(ostp))
3849 1.506 rillig return false;
3850 1.506 rillig
3851 1.506 rillig if (nst == CHAR || nst == UCHAR)
3852 1.506 rillig return false; /* for the sake of traditional C code */
3853 1.506 rillig if (ost == CHAR || ost == UCHAR)
3854 1.506 rillig return false; /* for the sake of traditional C code */
3855 1.506 rillig
3856 1.506 rillig /* Allow cast between pointers to sockaddr variants. */
3857 1.506 rillig if (nst == STRUCT && ost == STRUCT) {
3858 1.611 rillig const sym_t *nmem = nstp->u.sou->sou_first_member;
3859 1.611 rillig const sym_t *omem = ostp->u.sou->sou_first_member;
3860 1.506 rillig while (nmem != NULL && omem != NULL &&
3861 1.584 rillig types_compatible(nmem->s_type, omem->s_type,
3862 1.584 rillig true, false, NULL))
3863 1.506 rillig nmem = nmem->s_next, omem = omem->s_next;
3864 1.506 rillig if (nmem != NULL && is_byte_array(nmem->s_type))
3865 1.506 rillig return false;
3866 1.506 rillig if (omem != NULL && is_byte_array(omem->s_type))
3867 1.506 rillig return false;
3868 1.506 rillig if (nmem == NULL && omem == NULL)
3869 1.506 rillig return false;
3870 1.506 rillig }
3871 1.506 rillig
3872 1.516 rillig if (nst == UNION || ost == UNION) {
3873 1.516 rillig const type_t *union_tp = nst == UNION ? nstp : ostp;
3874 1.516 rillig const type_t *other_tp = nst == UNION ? ostp : nstp;
3875 1.519 rillig if (union_contains(union_tp, other_tp))
3876 1.519 rillig return false;
3877 1.516 rillig }
3878 1.516 rillig
3879 1.554 rillig if (is_struct_or_union(nst) && is_struct_or_union(ost))
3880 1.611 rillig return nstp->u.sou != ostp->u.sou;
3881 1.554 rillig
3882 1.554 rillig enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
3883 1.554 rillig enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
3884 1.554 rillig if (rk1 != rk2 || rk1 == RK_NONE)
3885 1.506 rillig return true;
3886 1.506 rillig
3887 1.550 rillig return portable_rank_cmp(nst, ost) != 0;
3888 1.506 rillig }
3889 1.506 rillig
3890 1.506 rillig static void
3891 1.506 rillig convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
3892 1.506 rillig {
3893 1.509 rillig const type_t *nstp = ntp->t_subt;
3894 1.509 rillig const type_t *otp = tn->tn_type;
3895 1.509 rillig const type_t *ostp = otp->t_subt;
3896 1.509 rillig tspec_t nst = nstp->t_tspec;
3897 1.509 rillig tspec_t ost = ostp->t_tspec;
3898 1.506 rillig
3899 1.506 rillig if (nst == VOID || ost == VOID) {
3900 1.506 rillig /* TODO: C99 behaves like C90 here. */
3901 1.589 rillig if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
3902 1.509 rillig const char *nts, *ots;
3903 1.506 rillig /* null pointers are already handled in convert() */
3904 1.506 rillig *(nst == FUNC ? &nts : &ots) = "function pointer";
3905 1.506 rillig *(nst == VOID ? &nts : &ots) = "'void *'";
3906 1.578 rillig /* conversion of %s to %s requires a cast */
3907 1.506 rillig warning(303, ots, nts);
3908 1.506 rillig }
3909 1.506 rillig return;
3910 1.509 rillig }
3911 1.509 rillig if (nst == FUNC && ost == FUNC)
3912 1.506 rillig return;
3913 1.509 rillig if (nst == FUNC || ost == FUNC) {
3914 1.506 rillig /* converting '%s' to '%s' is questionable */
3915 1.506 rillig warning(229, type_name(otp), type_name(ntp));
3916 1.506 rillig return;
3917 1.506 rillig }
3918 1.506 rillig
3919 1.640 rillig if (hflag && alignment(nstp) > alignment(ostp) &&
3920 1.677 rillig !is_incomplete(ostp) && alignment(ostp) > 1 &&
3921 1.519 rillig !(nst == UNION && union_contains(nstp, ostp))) {
3922 1.506 rillig /* converting '%s' to '%s' increases alignment ... */
3923 1.506 rillig warning(135, type_name(otp), type_name(ntp),
3924 1.640 rillig alignment(ostp), alignment(nstp));
3925 1.506 rillig }
3926 1.506 rillig
3927 1.506 rillig if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
3928 1.652 rillig /* pointer cast from '%s' to unrelated '%s' */
3929 1.652 rillig warning(247, type_name(ostp), type_name(nstp));
3930 1.506 rillig }
3931 1.506 rillig }
3932 1.506 rillig
3933 1.506 rillig /*
3934 1.506 rillig * Insert a conversion operator, which converts the type of the node
3935 1.506 rillig * to another given type.
3936 1.506 rillig *
3937 1.506 rillig * Possible values for 'op':
3938 1.506 rillig * CVT a cast-expression
3939 1.506 rillig * binary integer promotion for one of the operands, or a usual
3940 1.506 rillig * arithmetic conversion
3941 1.506 rillig * binary plain or compound assignments to bit-fields
3942 1.574 rillig * FARG 'arg' is the number of the parameter (used for warnings)
3943 1.506 rillig * NOOP several other implicit conversions
3944 1.506 rillig * ...
3945 1.506 rillig */
3946 1.506 rillig tnode_t *
3947 1.506 rillig convert(op_t op, int arg, type_t *tp, tnode_t *tn)
3948 1.506 rillig {
3949 1.509 rillig tspec_t nt = tp->t_tspec;
3950 1.509 rillig tspec_t ot = tn->tn_type->t_tspec;
3951 1.506 rillig
3952 1.506 rillig if (allow_trad && allow_c90 && op == FARG)
3953 1.506 rillig check_prototype_conversion(arg, nt, ot, tp, tn);
3954 1.506 rillig
3955 1.506 rillig if (nt == BOOL) {
3956 1.506 rillig /* No further checks. */
3957 1.506 rillig
3958 1.506 rillig } else if (is_integer(nt)) {
3959 1.506 rillig if (ot == BOOL) {
3960 1.506 rillig /* No further checks. */
3961 1.536 rillig } else if (is_integer(ot))
3962 1.506 rillig convert_integer_from_integer(op, arg, nt, ot, tp, tn);
3963 1.536 rillig else if (is_floating(ot))
3964 1.646 rillig convert_integer_from_floating(op, arg, tp, tn);
3965 1.536 rillig else if (ot == PTR)
3966 1.506 rillig convert_integer_from_pointer(op, nt, tp, tn);
3967 1.506 rillig
3968 1.506 rillig } else if (is_floating(nt)) {
3969 1.633 rillig if (is_integer(ot) && op != CVT) {
3970 1.632 rillig /* implicit conversion from integer '%s' to ... */
3971 1.632 rillig query_message(19,
3972 1.632 rillig type_name(tn->tn_type), type_name(tp));
3973 1.632 rillig }
3974 1.506 rillig
3975 1.506 rillig } else if (nt == PTR) {
3976 1.506 rillig if (is_null_pointer(tn)) {
3977 1.506 rillig /* a null pointer may be assigned to any pointer. */
3978 1.536 rillig } else if (ot == PTR && op == CVT)
3979 1.506 rillig convert_pointer_from_pointer(tp, tn);
3980 1.506 rillig }
3981 1.506 rillig
3982 1.509 rillig tnode_t *ntn = expr_alloc_tnode();
3983 1.506 rillig ntn->tn_op = CVT;
3984 1.506 rillig ntn->tn_type = tp;
3985 1.506 rillig ntn->tn_cast = op == CVT;
3986 1.506 rillig ntn->tn_sys |= tn->tn_sys;
3987 1.612 rillig ntn->u.ops.right = NULL;
3988 1.506 rillig if (tn->tn_op != CON || nt == VOID) {
3989 1.612 rillig ntn->u.ops.left = tn;
3990 1.506 rillig } else {
3991 1.506 rillig ntn->tn_op = CON;
3992 1.612 rillig convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
3993 1.612 rillig &tn->u.value);
3994 1.506 rillig }
3995 1.506 rillig
3996 1.506 rillig return ntn;
3997 1.506 rillig }
3998 1.506 rillig
3999 1.506 rillig static void
4000 1.641 rillig convert_constant_from_floating(op_t op, int arg, const type_t *ntp,
4001 1.641 rillig tspec_t nt, val_t *nv, val_t *ov)
4002 1.506 rillig {
4003 1.527 rillig long double max = 0.0, min = 0.0;
4004 1.506 rillig
4005 1.506 rillig switch (nt) {
4006 1.506 rillig case CHAR:
4007 1.506 rillig max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
4008 1.506 rillig case UCHAR:
4009 1.506 rillig max = TARG_UCHAR_MAX; min = 0; break;
4010 1.506 rillig case SCHAR:
4011 1.506 rillig max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
4012 1.506 rillig case SHORT:
4013 1.506 rillig max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
4014 1.506 rillig case USHORT:
4015 1.506 rillig max = TARG_USHRT_MAX; min = 0; break;
4016 1.506 rillig case ENUM:
4017 1.506 rillig case INT:
4018 1.506 rillig max = TARG_INT_MAX; min = TARG_INT_MIN; break;
4019 1.506 rillig case UINT:
4020 1.506 rillig max = TARG_UINT_MAX; min = 0; break;
4021 1.506 rillig case LONG:
4022 1.506 rillig max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
4023 1.506 rillig case ULONG:
4024 1.506 rillig max = TARG_ULONG_MAX; min = 0; break;
4025 1.545 rillig case LLONG:
4026 1.545 rillig max = LLONG_MAX; min = LLONG_MIN; break;
4027 1.545 rillig case ULLONG:
4028 1.545 rillig max = ULLONG_MAX; min = 0; break;
4029 1.506 rillig case FLOAT:
4030 1.506 rillig case FCOMPLEX:
4031 1.506 rillig max = FLT_MAX; min = -FLT_MAX; break;
4032 1.506 rillig case DOUBLE:
4033 1.506 rillig case DCOMPLEX:
4034 1.506 rillig max = DBL_MAX; min = -DBL_MAX; break;
4035 1.506 rillig case LDOUBLE:
4036 1.506 rillig case LCOMPLEX:
4037 1.641 rillig /* LINTED 248; see floating_error_value. */
4038 1.506 rillig max = LDBL_MAX; min = -max; break;
4039 1.1 cgd default:
4040 1.680 rillig lint_assert(false);
4041 1.1 cgd }
4042 1.641 rillig if (ov->u.floating > max || ov->u.floating < min) {
4043 1.506 rillig lint_assert(nt != LDOUBLE);
4044 1.641 rillig const char *ot_name = type_name(gettyp(ov->v_tspec));
4045 1.641 rillig const char *nt_name = type_name(ntp);
4046 1.646 rillig if (is_integer(nt))
4047 1.646 rillig goto after_warning;
4048 1.641 rillig if (op == FARG)
4049 1.506 rillig /* conversion of '%s' to '%s' is out of range, ... */
4050 1.641 rillig warning(295, ot_name, nt_name, arg);
4051 1.641 rillig else
4052 1.506 rillig /* conversion of '%s' to '%s' is out of range */
4053 1.641 rillig warning(119, ot_name, nt_name);
4054 1.646 rillig after_warning:
4055 1.641 rillig ov->u.floating = ov->u.floating > 0 ? max : min;
4056 1.506 rillig }
4057 1.1 cgd
4058 1.536 rillig if (nt == FLOAT || nt == FCOMPLEX)
4059 1.641 rillig nv->u.floating = (float)ov->u.floating;
4060 1.536 rillig else if (nt == DOUBLE || nt == DCOMPLEX)
4061 1.641 rillig nv->u.floating = (double)ov->u.floating;
4062 1.536 rillig else if (nt == LDOUBLE || nt == LCOMPLEX)
4063 1.641 rillig nv->u.floating = ov->u.floating;
4064 1.536 rillig else
4065 1.641 rillig nv->u.integer = (int64_t)ov->u.floating;
4066 1.506 rillig }
4067 1.1 cgd
4068 1.506 rillig static bool
4069 1.506 rillig convert_constant_to_floating(tspec_t nt, val_t *nv,
4070 1.506 rillig tspec_t ot, const val_t *v)
4071 1.506 rillig {
4072 1.506 rillig if (nt == FLOAT) {
4073 1.544 rillig nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
4074 1.544 rillig (float)(uint64_t)v->u.integer : (float)v->u.integer;
4075 1.506 rillig } else if (nt == DOUBLE) {
4076 1.544 rillig nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
4077 1.544 rillig (double)(uint64_t)v->u.integer : (double)v->u.integer;
4078 1.506 rillig } else if (nt == LDOUBLE) {
4079 1.544 rillig nv->u.floating = (ot == PTR || is_uinteger(ot))
4080 1.544 rillig ? (long double)(uint64_t)v->u.integer
4081 1.544 rillig : (long double)v->u.integer;
4082 1.506 rillig } else
4083 1.506 rillig return false;
4084 1.506 rillig return true;
4085 1.506 rillig }
4086 1.1 cgd
4087 1.672 rillig static void
4088 1.672 rillig warn_constant_truncated(op_t op, const val_t *v)
4089 1.672 rillig {
4090 1.672 rillig char buf[256];
4091 1.672 rillig bool is_unsigned = is_uinteger(v->v_tspec);
4092 1.672 rillig int64_t val = v->u.integer;
4093 1.672 rillig unsigned long long abs_val = is_unsigned || val >= 0
4094 1.672 rillig ? (unsigned long long)val
4095 1.672 rillig : -(unsigned long long)val;
4096 1.672 rillig const char *sign = is_unsigned || val >= 0 ? "" : "-";
4097 1.672 rillig snprintf(buf, sizeof(buf), "%s%#llx", sign, abs_val);
4098 1.672 rillig /* constant %s truncated by conversion, op '%s' */
4099 1.672 rillig warning(306, buf, op_name(op));
4100 1.672 rillig }
4101 1.672 rillig
4102 1.506 rillig static void
4103 1.506 rillig convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
4104 1.506 rillig uint64_t xmask, op_t op)
4105 1.506 rillig {
4106 1.604 rillig if (nsz < osz && (v->u.integer & xmask) != 0)
4107 1.672 rillig warn_constant_truncated(op, v);
4108 1.1 cgd }
4109 1.1 cgd
4110 1.506 rillig static void
4111 1.506 rillig convert_constant_check_range_bitand(size_t nsz, size_t osz,
4112 1.506 rillig uint64_t xmask, const val_t *nv,
4113 1.506 rillig tspec_t ot, const val_t *v,
4114 1.506 rillig const type_t *tp, op_t op)
4115 1.1 cgd {
4116 1.506 rillig if (nsz > osz &&
4117 1.544 rillig (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
4118 1.544 rillig (nv->u.integer & xmask) != xmask) {
4119 1.685 rillig /* '%s' converts '%s' with its most significant bit being set to '%s' */
4120 1.685 rillig warning(309,
4121 1.685 rillig op_name(op), type_name(gettyp(ot)), type_name(tp));
4122 1.506 rillig } else if (nsz < osz &&
4123 1.584 rillig (v->u.integer & xmask) != xmask &&
4124 1.604 rillig (v->u.integer & xmask) != 0)
4125 1.672 rillig warn_constant_truncated(op, v);
4126 1.506 rillig }
4127 1.1 cgd
4128 1.506 rillig static void
4129 1.644 rillig convert_constant_check_range_signed(op_t op, int arg,
4130 1.644 rillig const type_t *ntp, int64_t ov)
4131 1.506 rillig {
4132 1.604 rillig if (op == ASSIGN)
4133 1.644 rillig /* assignment of negative constant %lld to unsigned ... */
4134 1.644 rillig warning(164, (long long)ov, type_name(ntp));
4135 1.604 rillig else if (op == INIT)
4136 1.644 rillig /* initialization of unsigned type '%s' with negative ... */
4137 1.644 rillig warning(221, type_name(ntp), (long long)ov);
4138 1.604 rillig else if (op == FARG)
4139 1.644 rillig /* conversion of negative constant %lld to unsigned ... */
4140 1.644 rillig warning(296, (long long)ov, type_name(ntp), arg);
4141 1.604 rillig else if (modtab[op].m_comparison) {
4142 1.506 rillig /* handled by check_integer_comparison() */
4143 1.604 rillig } else
4144 1.644 rillig /* conversion of negative constant %lld to unsigned ... */
4145 1.644 rillig warning(222, (long long)ov, type_name(ntp));
4146 1.506 rillig }
4147 1.1 cgd
4148 1.506 rillig /*
4149 1.509 rillig * Loss of significant bit(s). All truncated bits of unsigned types or all
4150 1.509 rillig * truncated bits plus the msb of the target for signed types are considered
4151 1.509 rillig * to be significant bits. Loss of significant bits means that at least one
4152 1.509 rillig * of the bits was set in an unsigned type or that at least one but not all
4153 1.509 rillig * of the bits was set in a signed type. Loss of significant bits means that
4154 1.509 rillig * it is not possible, also not with necessary casts, to convert back to the
4155 1.527 rillig * original type. An example for a necessary cast is:
4156 1.506 rillig * char c; int i; c = 128;
4157 1.506 rillig * i = c; ** yields -128 **
4158 1.506 rillig * i = (unsigned char)c; ** yields 128 **
4159 1.506 rillig */
4160 1.506 rillig static void
4161 1.536 rillig warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
4162 1.536 rillig tspec_t ot)
4163 1.506 rillig {
4164 1.536 rillig if (op == ASSIGN && tp->t_bitfield)
4165 1.506 rillig /* precision lost in bit-field assignment */
4166 1.506 rillig warning(166);
4167 1.536 rillig else if (op == ASSIGN)
4168 1.506 rillig /* constant truncated by assignment */
4169 1.506 rillig warning(165);
4170 1.536 rillig else if (op == INIT && tp->t_bitfield)
4171 1.506 rillig /* bit-field initializer does not fit */
4172 1.506 rillig warning(180);
4173 1.536 rillig else if (op == INIT)
4174 1.506 rillig /* initializer does not fit */
4175 1.506 rillig warning(178);
4176 1.536 rillig else if (op == CASE)
4177 1.674 rillig /* case label is converted from '%s' to '%s' */
4178 1.674 rillig warning(196, tspec_name(ot), type_name(tp));
4179 1.536 rillig else if (op == FARG)
4180 1.506 rillig /* conversion of '%s' to '%s' is out of range, arg #%d */
4181 1.536 rillig warning(295, type_name(gettyp(ot)), type_name(tp), arg);
4182 1.536 rillig else
4183 1.506 rillig /* conversion of '%s' to '%s' is out of range */
4184 1.536 rillig warning(119, type_name(gettyp(ot)), type_name(tp));
4185 1.506 rillig }
4186 1.1 cgd
4187 1.506 rillig static void
4188 1.536 rillig warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
4189 1.506 rillig tspec_t ot)
4190 1.506 rillig {
4191 1.536 rillig if (op == ASSIGN && tp->t_bitfield)
4192 1.506 rillig /* precision lost in bit-field assignment */
4193 1.506 rillig warning(166);
4194 1.536 rillig else if (op == INIT && tp->t_bitfield)
4195 1.506 rillig /* bit-field initializer out of range */
4196 1.506 rillig warning(11);
4197 1.536 rillig else if (op == CASE)
4198 1.674 rillig /* case label is converted from '%s' to '%s' */
4199 1.674 rillig warning(196, tspec_name(ot), type_name(tp));
4200 1.536 rillig else if (op == FARG)
4201 1.506 rillig /* conversion of '%s' to '%s' is out of range, arg #%d */
4202 1.509 rillig warning(295, type_name(gettyp(ot)), type_name(tp), arg);
4203 1.536 rillig else
4204 1.506 rillig /* conversion of '%s' to '%s' is out of range */
4205 1.509 rillig warning(119, type_name(gettyp(ot)), type_name(tp));
4206 1.1 cgd }
4207 1.1 cgd
4208 1.506 rillig static void
4209 1.506 rillig convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
4210 1.506 rillig op_t op, int arg, const val_t *v, val_t *nv)
4211 1.378 rillig {
4212 1.506 rillig unsigned int obitsz, nbitsz;
4213 1.506 rillig uint64_t xmask, xmsk1;
4214 1.506 rillig
4215 1.506 rillig obitsz = size_in_bits(ot);
4216 1.532 rillig nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
4217 1.506 rillig xmask = value_bits(nbitsz) ^ value_bits(obitsz);
4218 1.506 rillig xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
4219 1.506 rillig if (op == ORASS || op == BITOR || op == BITXOR) {
4220 1.506 rillig convert_constant_check_range_bitor(
4221 1.506 rillig nbitsz, obitsz, v, xmask, op);
4222 1.506 rillig } else if (op == ANDASS || op == BITAND) {
4223 1.506 rillig convert_constant_check_range_bitand(
4224 1.506 rillig nbitsz, obitsz, xmask, nv, ot, v, tp, op);
4225 1.589 rillig } else if (nt != PTR && is_uinteger(nt) &&
4226 1.684 rillig ot != PTR && !is_uinteger(ot) && v->u.integer < 0)
4227 1.684 rillig convert_constant_check_range_signed(op, arg, tp, v->u.integer);
4228 1.544 rillig else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
4229 1.584 rillig (v->u.integer & xmask) != 0 &&
4230 1.584 rillig (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
4231 1.536 rillig warn_constant_check_range_truncated(op, arg, tp, ot);
4232 1.544 rillig else if (nv->u.integer != v->u.integer)
4233 1.536 rillig warn_constant_check_range_loss(op, arg, tp, ot);
4234 1.378 rillig }
4235 1.378 rillig
4236 1.605 rillig /* Converts a typed constant to a constant of another type. */
4237 1.506 rillig void
4238 1.605 rillig convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
4239 1.506 rillig {
4240 1.506 rillig /*
4241 1.605 rillig * TODO: make 'ov' const; the name of this function does not suggest
4242 1.605 rillig * that it modifies 'ov'.
4243 1.506 rillig */
4244 1.605 rillig tspec_t ot = ov->v_tspec;
4245 1.605 rillig tspec_t nt = nv->v_tspec = ntp->t_tspec;
4246 1.509 rillig bool range_check = false;
4247 1.1 cgd
4248 1.506 rillig if (nt == BOOL) { /* C99 6.3.1.2 */
4249 1.506 rillig nv->v_unsigned_since_c90 = false;
4250 1.605 rillig nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
4251 1.506 rillig return;
4252 1.506 rillig }
4253 1.1 cgd
4254 1.536 rillig if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
4255 1.641 rillig convert_constant_from_floating(op, arg, ntp, nt, nv, ov);
4256 1.605 rillig else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
4257 1.506 rillig range_check = true; /* Check for lost precision. */
4258 1.605 rillig nv->u.integer = ov->u.integer;
4259 1.506 rillig }
4260 1.1 cgd
4261 1.605 rillig if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
4262 1.506 rillig (is_floating(nt) || (
4263 1.506 rillig (is_integer(nt) && !is_uinteger(nt) &&
4264 1.550 rillig portable_rank_cmp(nt, ot) > 0)))) {
4265 1.578 rillig /* C90 treats constant as unsigned */
4266 1.506 rillig warning(157);
4267 1.605 rillig ov->v_unsigned_since_c90 = false;
4268 1.1 cgd }
4269 1.1 cgd
4270 1.506 rillig if (is_integer(nt)) {
4271 1.605 rillig unsigned int size = ntp->t_bitfield
4272 1.605 rillig ? ntp->t_bit_field_width : size_in_bits(nt);
4273 1.605 rillig nv->u.integer = convert_integer(nv->u.integer, nt, size);
4274 1.1 cgd }
4275 1.1 cgd
4276 1.506 rillig if (range_check && op != CVT)
4277 1.605 rillig convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
4278 1.1 cgd }
4279 1.1 cgd
4280 1.1 cgd tnode_t *
4281 1.253 rillig build_sizeof(const type_t *tp)
4282 1.1 cgd {
4283 1.355 rillig unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
4284 1.318 rillig tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
4285 1.227 rillig tn->tn_system_dependent = true;
4286 1.415 rillig debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
4287 1.227 rillig return tn;
4288 1.62 christos }
4289 1.62 christos
4290 1.84 christos tnode_t *
4291 1.592 rillig build_offsetof(const type_t *tp, designation dn)
4292 1.84 christos {
4293 1.576 rillig unsigned int offset_in_bits = 0;
4294 1.392 rillig
4295 1.576 rillig if (!is_struct_or_union(tp->t_tspec)) {
4296 1.113 rillig /* unacceptable operand of '%s' */
4297 1.84 christos error(111, "offsetof");
4298 1.576 rillig goto proceed;
4299 1.576 rillig }
4300 1.592 rillig for (size_t i = 0; i < dn.dn_len; i++) {
4301 1.592 rillig const designator *dr = dn.dn_items + i;
4302 1.595 rillig if (dr->dr_kind == DK_SUBSCRIPT) {
4303 1.592 rillig if (tp->t_tspec != ARRAY)
4304 1.592 rillig goto proceed; /* silent error */
4305 1.592 rillig tp = tp->t_subt;
4306 1.667 rillig offset_in_bits += (unsigned)dr->dr_subscript
4307 1.592 rillig * type_size_in_bits(tp);
4308 1.592 rillig } else {
4309 1.592 rillig if (!is_struct_or_union(tp->t_tspec))
4310 1.592 rillig goto proceed; /* silent error */
4311 1.592 rillig const char *name = dr->dr_member->s_name;
4312 1.611 rillig sym_t *mem = find_member(tp->u.sou, name);
4313 1.592 rillig if (mem == NULL) {
4314 1.592 rillig /* type '%s' does not have member '%s' */
4315 1.592 rillig error(101, name, type_name(tp));
4316 1.592 rillig goto proceed;
4317 1.592 rillig }
4318 1.592 rillig tp = mem->s_type;
4319 1.592 rillig offset_in_bits += mem->u.s_member.sm_offset_in_bits;
4320 1.592 rillig }
4321 1.576 rillig }
4322 1.592 rillig free(dn.dn_items);
4323 1.89 rillig
4324 1.576 rillig proceed:;
4325 1.576 rillig unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
4326 1.536 rillig tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
4327 1.227 rillig tn->tn_system_dependent = true;
4328 1.227 rillig return tn;
4329 1.84 christos }
4330 1.84 christos
4331 1.355 rillig unsigned int
4332 1.253 rillig type_size_in_bits(const type_t *tp)
4333 1.62 christos {
4334 1.1 cgd
4335 1.509 rillig unsigned int elem = 1;
4336 1.509 rillig bool flex = false;
4337 1.412 rillig lint_assert(tp != NULL);
4338 1.1 cgd while (tp->t_tspec == ARRAY) {
4339 1.154 rillig flex = true; /* allow c99 flex arrays [] [0] */
4340 1.611 rillig elem *= tp->u.dimension;
4341 1.1 cgd tp = tp->t_subt;
4342 1.1 cgd }
4343 1.536 rillig if (elem == 0 && !flex) {
4344 1.536 rillig /* cannot take size/alignment of incomplete type */
4345 1.536 rillig error(143);
4346 1.536 rillig elem = 1;
4347 1.1 cgd }
4348 1.536 rillig
4349 1.536 rillig unsigned int elsz;
4350 1.1 cgd switch (tp->t_tspec) {
4351 1.542 rillig case VOID:
4352 1.542 rillig /* cannot take size/alignment of void */
4353 1.542 rillig error(146);
4354 1.542 rillig elsz = 1;
4355 1.542 rillig break;
4356 1.1 cgd case FUNC:
4357 1.414 rillig /* cannot take size/alignment of function type '%s' */
4358 1.414 rillig error(144, type_name(tp));
4359 1.1 cgd elsz = 1;
4360 1.1 cgd break;
4361 1.1 cgd case STRUCT:
4362 1.1 cgd case UNION:
4363 1.194 rillig if (is_incomplete(tp)) {
4364 1.108 rillig /* cannot take size/alignment of incomplete type */
4365 1.1 cgd error(143);
4366 1.1 cgd elsz = 1;
4367 1.604 rillig } else
4368 1.611 rillig elsz = tp->u.sou->sou_size_in_bits;
4369 1.1 cgd break;
4370 1.1 cgd case ENUM:
4371 1.194 rillig if (is_incomplete(tp)) {
4372 1.108 rillig /* cannot take size/alignment of incomplete type */
4373 1.1 cgd warning(143);
4374 1.1 cgd }
4375 1.1 cgd /* FALLTHROUGH */
4376 1.1 cgd default:
4377 1.604 rillig if (tp->t_bitfield)
4378 1.108 rillig /* cannot take size/alignment of bit-field */
4379 1.1 cgd error(145);
4380 1.542 rillig elsz = size_in_bits(tp->t_tspec);
4381 1.542 rillig lint_assert(elsz > 0);
4382 1.1 cgd break;
4383 1.1 cgd }
4384 1.1 cgd
4385 1.355 rillig return elem * elsz;
4386 1.1 cgd }
4387 1.1 cgd
4388 1.536 rillig /* C11 6.5.3.4, GCC */
4389 1.59 christos tnode_t *
4390 1.253 rillig build_alignof(const type_t *tp)
4391 1.59 christos {
4392 1.535 rillig if (tp->t_tspec == FUNC) {
4393 1.414 rillig /* cannot take size/alignment of function type '%s' */
4394 1.414 rillig error(144, type_name(tp));
4395 1.534 rillig return NULL;
4396 1.59 christos }
4397 1.535 rillig if (tp->t_tspec == VOID) {
4398 1.535 rillig /* cannot take size/alignment of void */
4399 1.535 rillig error(146);
4400 1.535 rillig return NULL;
4401 1.535 rillig }
4402 1.535 rillig if (is_incomplete(tp)) {
4403 1.535 rillig /* cannot take size/alignment of incomplete type */
4404 1.535 rillig error(143);
4405 1.535 rillig return NULL;
4406 1.535 rillig }
4407 1.535 rillig if (tp->t_bitfield) {
4408 1.535 rillig /* cannot take size/alignment of bit-field */
4409 1.535 rillig error(145);
4410 1.535 rillig return NULL;
4411 1.535 rillig }
4412 1.640 rillig return build_integer_constant(SIZEOF_TSPEC, (int64_t)alignment(tp));
4413 1.59 christos }
4414 1.59 christos
4415 1.499 rillig static tnode_t *
4416 1.594 rillig cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
4417 1.499 rillig {
4418 1.499 rillig
4419 1.499 rillig if (!allow_gcc) {
4420 1.499 rillig /* union cast is a GCC extension */
4421 1.499 rillig error(328);
4422 1.499 rillig return NULL;
4423 1.499 rillig }
4424 1.499 rillig
4425 1.611 rillig for (const sym_t *m = ntp->u.sou->sou_first_member;
4426 1.499 rillig m != NULL; m = m->s_next) {
4427 1.499 rillig if (types_compatible(m->s_type, otn->tn_type,
4428 1.499 rillig false, false, NULL)) {
4429 1.594 rillig tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
4430 1.499 rillig ntn->tn_cast = true;
4431 1.499 rillig return ntn;
4432 1.499 rillig }
4433 1.499 rillig }
4434 1.499 rillig
4435 1.499 rillig /* type '%s' is not a member of '%s' */
4436 1.499 rillig error(329, type_name(otn->tn_type), type_name(ntp));
4437 1.499 rillig return NULL;
4438 1.499 rillig }
4439 1.499 rillig
4440 1.639 rillig // In GCC mode, allow 'nullptr + offset' as a constant expression.
4441 1.639 rillig static tnode_t *
4442 1.639 rillig null_pointer_offset(tnode_t *tn)
4443 1.639 rillig {
4444 1.639 rillig uint64_t off = 0;
4445 1.639 rillig const tnode_t *n = tn;
4446 1.639 rillig while ((n->tn_op == PLUS || n->tn_op == MINUS)
4447 1.639 rillig && is_integer(n->u.ops.right->tn_type->t_tspec)) {
4448 1.639 rillig off += (uint64_t)n->u.ops.right->u.value.u.integer;
4449 1.639 rillig n = n->u.ops.left;
4450 1.639 rillig }
4451 1.639 rillig if (n->tn_type->t_tspec == PTR
4452 1.639 rillig && n->tn_op == ADDR
4453 1.639 rillig && n->u.ops.left->tn_op == INDIR
4454 1.639 rillig && n->u.ops.left->u.ops.left->tn_op == CON
4455 1.639 rillig && n->u.ops.left->u.ops.left->tn_type->t_tspec == PTR) {
4456 1.639 rillig off += (uint64_t)n->u.ops.left->u.ops.left->u.value.u.integer;
4457 1.639 rillig return build_integer_constant(SIZEOF_TSPEC, (int64_t)off);
4458 1.639 rillig }
4459 1.639 rillig return tn;
4460 1.639 rillig }
4461 1.639 rillig
4462 1.1 cgd tnode_t *
4463 1.594 rillig cast(tnode_t *tn, bool sys, type_t *tp)
4464 1.1 cgd {
4465 1.1 cgd
4466 1.1 cgd if (tn == NULL)
4467 1.95 rillig return NULL;
4468 1.1 cgd
4469 1.1 cgd tn = cconv(tn);
4470 1.1 cgd
4471 1.413 rillig lint_assert(tp != NULL);
4472 1.536 rillig tspec_t nt = tp->t_tspec;
4473 1.536 rillig tspec_t ot = tn->tn_type->t_tspec;
4474 1.1 cgd
4475 1.1 cgd if (nt == VOID) {
4476 1.1 cgd /*
4477 1.586 rillig * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
4478 1.586 rillig * cast to void. The only other allowed casts are from a
4479 1.429 rillig * scalar type to a scalar type.
4480 1.1 cgd */
4481 1.536 rillig } else if (nt == UNION)
4482 1.594 rillig return cast_to_union(tn, sys, tp);
4483 1.536 rillig else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
4484 1.328 rillig /* Casting to a struct is an undocumented GCC extension. */
4485 1.429 rillig if (!(allow_gcc && nt == STRUCT))
4486 1.319 rillig goto invalid_cast;
4487 1.536 rillig } else if (is_struct_or_union(ot))
4488 1.319 rillig goto invalid_cast;
4489 1.536 rillig else if (ot == VOID) {
4490 1.1 cgd /* improper cast of void expression */
4491 1.1 cgd error(148);
4492 1.95 rillig return NULL;
4493 1.144 rillig } else if (is_integer(nt) && is_scalar(ot)) {
4494 1.639 rillig tn = null_pointer_offset(tn);
4495 1.144 rillig } else if (is_floating(nt) && is_arithmetic(ot)) {
4496 1.1 cgd /* ok */
4497 1.144 rillig } else if (nt == PTR && is_integer(ot)) {
4498 1.1 cgd /* ok */
4499 1.1 cgd } else if (nt == PTR && ot == PTR) {
4500 1.7 jpo if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
4501 1.7 jpo if (hflag)
4502 1.196 rillig /* cast discards 'const' from type '%s' */
4503 1.196 rillig warning(275, type_name(tn->tn_type));
4504 1.7 jpo }
4505 1.319 rillig } else
4506 1.319 rillig goto invalid_cast;
4507 1.1 cgd
4508 1.662 rillig if (any_query_enabled
4509 1.662 rillig && types_compatible(tp, tn->tn_type, false, false, NULL))
4510 1.471 rillig /* no-op cast from '%s' to '%s' */
4511 1.690 rillig query_message(6, expr_type_name(tn), type_name(tp));
4512 1.471 rillig
4513 1.1 cgd tn = convert(CVT, 0, tp, tn);
4514 1.154 rillig tn->tn_cast = true;
4515 1.594 rillig tn->tn_sys = sys;
4516 1.1 cgd
4517 1.95 rillig return tn;
4518 1.319 rillig
4519 1.319 rillig invalid_cast:
4520 1.319 rillig /* invalid cast from '%s' to '%s' */
4521 1.690 rillig error(147, expr_type_name(tn), type_name(tp));
4522 1.319 rillig return NULL;
4523 1.1 cgd }
4524 1.1 cgd
4525 1.603 rillig void
4526 1.603 rillig add_function_argument(function_call *call, tnode_t *arg)
4527 1.1 cgd {
4528 1.1 cgd /*
4529 1.1 cgd * If there was a serious error in the expression for the argument,
4530 1.1 cgd * create a dummy argument so the positions of the remaining arguments
4531 1.1 cgd * will not change.
4532 1.1 cgd */
4533 1.1 cgd if (arg == NULL)
4534 1.318 rillig arg = build_integer_constant(INT, 0);
4535 1.1 cgd
4536 1.603 rillig if (call->args_len >= call->args_cap) {
4537 1.603 rillig call->args_cap += 8;
4538 1.603 rillig tnode_t **new_args = expr_zero_alloc(
4539 1.643 rillig call->args_cap * sizeof(*call->args), "tnode*[]");
4540 1.643 rillig if (call->args_len > 0)
4541 1.643 rillig memcpy(new_args, call->args,
4542 1.643 rillig call->args_len * sizeof(*call->args));
4543 1.603 rillig call->args = new_args;
4544 1.603 rillig }
4545 1.603 rillig call->args[call->args_len++] = arg;
4546 1.1 cgd }
4547 1.1 cgd
4548 1.1 cgd /*
4549 1.506 rillig * Compare the type of an argument with the corresponding type of a
4550 1.506 rillig * prototype parameter. If it is a valid combination, but both types
4551 1.506 rillig * are not the same, insert a conversion to convert the argument into
4552 1.506 rillig * the type of the parameter.
4553 1.1 cgd */
4554 1.506 rillig static tnode_t *
4555 1.683 rillig check_prototype_argument(const function_call *call, int arg,
4556 1.683 rillig type_t *tp, tnode_t *tn)
4557 1.1 cgd {
4558 1.526 rillig tnode_t *ln = xcalloc(1, sizeof(*ln));
4559 1.506 rillig ln->tn_type = expr_unqualified_type(tp);
4560 1.506 rillig ln->tn_lvalue = true;
4561 1.683 rillig if (typeok(FARG, call, arg, ln, tn)) {
4562 1.509 rillig bool dowarn;
4563 1.506 rillig if (!types_compatible(tp, tn->tn_type,
4564 1.506 rillig true, false, (dowarn = false, &dowarn)) || dowarn)
4565 1.683 rillig tn = convert(FARG, arg, tp, tn);
4566 1.1 cgd }
4567 1.506 rillig free(ln);
4568 1.506 rillig return tn;
4569 1.1 cgd }
4570 1.1 cgd
4571 1.1 cgd /*
4572 1.1 cgd * Check types of all function arguments and insert conversions,
4573 1.1 cgd * if necessary.
4574 1.1 cgd */
4575 1.625 rillig static void
4576 1.603 rillig check_function_arguments(const function_call *call)
4577 1.1 cgd {
4578 1.603 rillig type_t *ftp = call->func->tn_type->t_subt;
4579 1.603 rillig
4580 1.536 rillig /* get # of parameters in the prototype */
4581 1.536 rillig int npar = 0;
4582 1.611 rillig for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
4583 1.1 cgd npar++;
4584 1.1 cgd
4585 1.603 rillig int narg = (int)call->args_len;
4586 1.1 cgd
4587 1.611 rillig const sym_t *param = ftp->u.params;
4588 1.1 cgd if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
4589 1.459 rillig /* argument mismatch: %d %s passed, %d expected */
4590 1.575 rillig error(150, narg, narg != 1 ? "arguments" : "argument", npar);
4591 1.574 rillig param = NULL;
4592 1.1 cgd }
4593 1.20 lukem
4594 1.625 rillig for (int i = 0; i < narg; i++) {
4595 1.625 rillig tnode_t *arg = call->args[i];
4596 1.1 cgd
4597 1.106 rillig /* some things which are always not allowed */
4598 1.603 rillig tspec_t at = arg->tn_type->t_tspec;
4599 1.536 rillig if (at == VOID) {
4600 1.1 cgd /* void expressions may not be arguments, arg #%d */
4601 1.625 rillig error(151, i + 1);
4602 1.625 rillig return;
4603 1.574 rillig }
4604 1.628 rillig if (is_struct_or_union(at) && is_incomplete(arg->tn_type)) {
4605 1.1 cgd /* argument cannot have unknown size, arg #%d */
4606 1.625 rillig error(152, i + 1);
4607 1.625 rillig return;
4608 1.574 rillig }
4609 1.574 rillig if (is_integer(at) &&
4610 1.603 rillig arg->tn_type->t_is_enum &&
4611 1.603 rillig is_incomplete(arg->tn_type)) {
4612 1.1 cgd /* argument cannot have unknown size, arg #%d */
4613 1.625 rillig warning(152, i + 1);
4614 1.1 cgd }
4615 1.1 cgd
4616 1.603 rillig arg = cconv(arg);
4617 1.625 rillig call->args[i] = arg;
4618 1.1 cgd
4619 1.603 rillig arg = param != NULL
4620 1.683 rillig ? check_prototype_argument(call, i + 1, param->s_type, arg)
4621 1.603 rillig : promote(NOOP, true, arg);
4622 1.625 rillig call->args[i] = arg;
4623 1.1 cgd
4624 1.574 rillig if (param != NULL)
4625 1.574 rillig param = param->s_next;
4626 1.1 cgd }
4627 1.1 cgd }
4628 1.1 cgd
4629 1.687 rillig static bool
4630 1.687 rillig is_gcc_generic_atomic(const char *name)
4631 1.687 rillig {
4632 1.687 rillig // https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
4633 1.687 rillig return strcmp(name, "__atomic_load_n") == 0
4634 1.687 rillig || strcmp(name, "__atomic_exchange_n") == 0
4635 1.687 rillig || strcmp(name, "__atomic_add_fetch") == 0
4636 1.687 rillig || strcmp(name, "__atomic_sub_fetch") == 0
4637 1.687 rillig || strcmp(name, "__atomic_and_fetch") == 0
4638 1.687 rillig || strcmp(name, "__atomic_xor_fetch") == 0
4639 1.687 rillig || strcmp(name, "__atomic_or_fetch") == 0
4640 1.687 rillig || strcmp(name, "__atomic_nand_fetch") == 0
4641 1.687 rillig || strcmp(name, "__atomic_fetch_add") == 0
4642 1.687 rillig || strcmp(name, "__atomic_fetch_sub") == 0
4643 1.687 rillig || strcmp(name, "__atomic_fetch_and") == 0
4644 1.687 rillig || strcmp(name, "__atomic_fetch_xor") == 0
4645 1.687 rillig || strcmp(name, "__atomic_fetch_or") == 0
4646 1.687 rillig || strcmp(name, "__atomic_fetch_nand") == 0;
4647 1.687 rillig }
4648 1.687 rillig
4649 1.687 rillig static type_t *
4650 1.687 rillig return_type(const function_call *call)
4651 1.687 rillig {
4652 1.687 rillig const tnode_t *func = call->func;
4653 1.687 rillig if (allow_gcc
4654 1.687 rillig && func->tn_op == ADDR
4655 1.687 rillig && func->u.ops.left->tn_op == NAME
4656 1.687 rillig && is_gcc_generic_atomic(func->u.ops.left->u.sym->s_name)
4657 1.687 rillig && call->args_len > 0
4658 1.687 rillig && call->args[0]->tn_type->t_tspec == PTR)
4659 1.687 rillig return call->args[0]->tn_type->t_subt;
4660 1.687 rillig return func->tn_type->t_subt->t_subt;
4661 1.687 rillig }
4662 1.687 rillig
4663 1.506 rillig tnode_t *
4664 1.603 rillig build_function_call(tnode_t *func, bool sys, function_call *call)
4665 1.1 cgd {
4666 1.506 rillig
4667 1.506 rillig if (func == NULL)
4668 1.506 rillig return NULL;
4669 1.506 rillig
4670 1.603 rillig call->func = func;
4671 1.603 rillig check_ctype_function_call(call);
4672 1.506 rillig
4673 1.506 rillig func = cconv(func);
4674 1.603 rillig call->func = func;
4675 1.1 cgd
4676 1.506 rillig if (func->tn_type->t_tspec != PTR ||
4677 1.506 rillig func->tn_type->t_subt->t_tspec != FUNC) {
4678 1.506 rillig /* cannot call '%s', must be a function */
4679 1.690 rillig error(149, expr_type_name(func));
4680 1.506 rillig return NULL;
4681 1.1 cgd }
4682 1.506 rillig
4683 1.625 rillig check_function_arguments(call);
4684 1.506 rillig
4685 1.603 rillig tnode_t *ntn = expr_alloc_tnode();
4686 1.634 rillig ntn->tn_op = CALL;
4687 1.687 rillig ntn->tn_type = return_type(call);
4688 1.603 rillig ntn->tn_sys = sys;
4689 1.612 rillig ntn->u.call = call;
4690 1.603 rillig return ntn;
4691 1.1 cgd }
4692 1.1 cgd
4693 1.1 cgd /*
4694 1.1 cgd * Return the value of an integral constant expression.
4695 1.1 cgd * If the expression is not constant or its type is not an integer
4696 1.1 cgd * type, an error message is printed.
4697 1.1 cgd */
4698 1.1 cgd val_t *
4699 1.541 rillig integer_constant(tnode_t *tn, bool required)
4700 1.1 cgd {
4701 1.1 cgd
4702 1.1 cgd if (tn != NULL)
4703 1.1 cgd tn = cconv(tn);
4704 1.1 cgd if (tn != NULL)
4705 1.172 rillig tn = promote(NOOP, false, tn);
4706 1.1 cgd
4707 1.509 rillig val_t *v = xcalloc(1, sizeof(*v));
4708 1.1 cgd
4709 1.1 cgd if (tn == NULL) {
4710 1.548 rillig lint_assert(seen_error);
4711 1.325 rillig debug_step("constant node is null; returning 1 instead");
4712 1.1 cgd v->v_tspec = INT;
4713 1.544 rillig v->u.integer = 1;
4714 1.95 rillig return v;
4715 1.1 cgd }
4716 1.1 cgd
4717 1.1 cgd v->v_tspec = tn->tn_type->t_tspec;
4718 1.1 cgd
4719 1.1 cgd if (tn->tn_op == CON) {
4720 1.612 rillig lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
4721 1.612 rillig if (is_integer(tn->u.value.v_tspec)) {
4722 1.289 rillig v->v_unsigned_since_c90 =
4723 1.612 rillig tn->u.value.v_unsigned_since_c90;
4724 1.612 rillig v->u.integer = tn->u.value.u.integer;
4725 1.95 rillig return v;
4726 1.1 cgd }
4727 1.612 rillig v->u.integer = (int64_t)tn->u.value.u.floating;
4728 1.604 rillig } else
4729 1.544 rillig v->u.integer = 1;
4730 1.1 cgd
4731 1.31 christos if (required)
4732 1.113 rillig /* integral constant expression expected */
4733 1.31 christos error(55);
4734 1.31 christos else
4735 1.113 rillig /* variable array dimension is a C99/GCC extension */
4736 1.45 christos c99ism(318);
4737 1.1 cgd
4738 1.144 rillig if (!is_integer(v->v_tspec))
4739 1.1 cgd v->v_tspec = INT;
4740 1.1 cgd
4741 1.95 rillig return v;
4742 1.1 cgd }
4743 1.1 cgd
4744 1.1 cgd /*
4745 1.317 rillig * Perform some tests on expressions which can't be done in build_binary()
4746 1.317 rillig * and functions called by build_binary(). These tests must be done here
4747 1.317 rillig * because we need some information about the context in which the operations
4748 1.1 cgd * are performed.
4749 1.684 rillig * After all tests are performed, if free_expr is true, expr() frees the
4750 1.684 rillig * memory for the expression.
4751 1.1 cgd */
4752 1.1 cgd void
4753 1.684 rillig expr(tnode_t *tn, bool used, bool cond, bool free_expr, bool is_do_while,
4754 1.658 rillig const char *stmt_kind)
4755 1.1 cgd {
4756 1.20 lukem
4757 1.305 rillig if (tn == NULL) { /* in case of errors */
4758 1.257 rillig expr_free_all();
4759 1.1 cgd return;
4760 1.1 cgd }
4761 1.1 cgd
4762 1.218 rillig /* expr() is also called in global initializations */
4763 1.538 rillig if (dcs->d_kind != DLK_EXTERN && !is_do_while)
4764 1.658 rillig check_statement_reachable(stmt_kind);
4765 1.1 cgd
4766 1.684 rillig check_expr_misc(tn, used, cond, !cond, false, false, false);
4767 1.515 rillig if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
4768 1.433 rillig if (hflag && cond)
4769 1.1 cgd /* assignment in conditional context */
4770 1.1 cgd warning(159);
4771 1.1 cgd }
4772 1.164 rillig if (!modtab[tn->tn_op].m_has_side_effect) {
4773 1.1 cgd /*
4774 1.586 rillig * for left operands of COMMA this warning is already printed
4775 1.1 cgd */
4776 1.684 rillig if (tn->tn_op != COMMA && !used && !cond)
4777 1.100 rillig check_null_effect(tn);
4778 1.1 cgd }
4779 1.326 rillig debug_node(tn);
4780 1.1 cgd
4781 1.684 rillig if (free_expr)
4782 1.257 rillig expr_free_all();
4783 1.1 cgd }
4784 1.1 cgd
4785 1.613 rillig /* If the expression has the form '*(arr + idx)', check the array index. */
4786 1.197 rillig static void
4787 1.613 rillig check_array_index(const tnode_t *indir, bool taking_address)
4788 1.197 rillig {
4789 1.613 rillig const tnode_t *plus, *arr, *idx;
4790 1.506 rillig
4791 1.613 rillig if (indir->tn_op == INDIR
4792 1.613 rillig && (plus = indir->u.ops.left, plus->tn_op == PLUS)
4793 1.613 rillig && plus->u.ops.left->tn_op == ADDR
4794 1.613 rillig && (arr = plus->u.ops.left->u.ops.left, true)
4795 1.613 rillig && (arr->tn_op == STRING || arr->tn_op == NAME)
4796 1.613 rillig && arr->tn_type->t_tspec == ARRAY
4797 1.613 rillig && (idx = plus->u.ops.right, idx->tn_op == CON)
4798 1.613 rillig && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
4799 1.613 rillig goto proceed;
4800 1.613 rillig return;
4801 1.197 rillig
4802 1.613 rillig proceed:;
4803 1.613 rillig int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
4804 1.509 rillig if (elsz == 0)
4805 1.341 rillig return;
4806 1.506 rillig elsz /= CHAR_SIZE;
4807 1.506 rillig
4808 1.506 rillig /* Change the unit of the index from bytes to element size. */
4809 1.613 rillig int64_t con = is_uinteger(idx->tn_type->t_tspec)
4810 1.613 rillig ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
4811 1.613 rillig : idx->u.value.u.integer / elsz;
4812 1.506 rillig
4813 1.613 rillig int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
4814 1.341 rillig
4815 1.613 rillig if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
4816 1.630 rillig /* array subscript %jd cannot be negative */
4817 1.627 rillig warning(167, (intmax_t)con);
4818 1.604 rillig else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
4819 1.631 rillig /* array subscript %ju cannot be > %d */
4820 1.630 rillig warning(168, (uintmax_t)con, dim - 1);
4821 1.1 cgd }
4822 1.1 cgd
4823 1.370 rillig static void
4824 1.370 rillig check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
4825 1.370 rillig {
4826 1.370 rillig /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4827 1.370 rillig if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4828 1.370 rillig if (!szof)
4829 1.612 rillig mark_as_set(ln->u.sym);
4830 1.612 rillig mark_as_used(ln->u.sym, fcall, szof);
4831 1.370 rillig }
4832 1.613 rillig check_array_index(ln, true);
4833 1.370 rillig }
4834 1.370 rillig
4835 1.536 rillig /*
4836 1.536 rillig * If there is an asm statement in one of the compound statements around,
4837 1.536 rillig * there may be other side effects, so don't warn.
4838 1.536 rillig */
4839 1.536 rillig static bool
4840 1.536 rillig is_asm_around(void)
4841 1.536 rillig {
4842 1.538 rillig for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
4843 1.538 rillig if (dl->d_asm)
4844 1.536 rillig return true;
4845 1.536 rillig return false;
4846 1.536 rillig }
4847 1.536 rillig
4848 1.370 rillig static void
4849 1.370 rillig check_expr_side_effect(const tnode_t *ln, bool szof)
4850 1.1 cgd {
4851 1.370 rillig
4852 1.370 rillig /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4853 1.370 rillig if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4854 1.612 rillig scl_t sc = ln->u.sym->s_scl;
4855 1.370 rillig if (sc != EXTERN && sc != STATIC &&
4856 1.612 rillig !ln->u.sym->s_set && !szof && !is_asm_around()) {
4857 1.459 rillig /* '%s' may be used before set */
4858 1.612 rillig warning(158, ln->u.sym->s_name);
4859 1.612 rillig mark_as_set(ln->u.sym);
4860 1.370 rillig }
4861 1.612 rillig mark_as_used(ln->u.sym, false, false);
4862 1.370 rillig }
4863 1.370 rillig }
4864 1.1 cgd
4865 1.370 rillig static void
4866 1.370 rillig check_expr_assign(const tnode_t *ln, bool szof)
4867 1.370 rillig {
4868 1.370 rillig /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4869 1.370 rillig if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4870 1.612 rillig mark_as_set(ln->u.sym);
4871 1.612 rillig if (ln->u.sym->s_scl == EXTERN)
4872 1.612 rillig outusg(ln->u.sym);
4873 1.370 rillig }
4874 1.613 rillig check_array_index(ln, false);
4875 1.370 rillig }
4876 1.1 cgd
4877 1.370 rillig static void
4878 1.370 rillig check_expr_call(const tnode_t *tn, const tnode_t *ln,
4879 1.433 rillig bool szof, bool vctx, bool cond, bool retval_discarded)
4880 1.370 rillig {
4881 1.370 rillig lint_assert(ln->tn_op == ADDR);
4882 1.612 rillig lint_assert(ln->u.ops.left->tn_op == NAME);
4883 1.612 rillig if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
4884 1.433 rillig outcall(tn, vctx || cond, retval_discarded);
4885 1.635 rillig
4886 1.635 rillig const function_call *call = tn->u.call;
4887 1.635 rillig if (call->args_len == 4 || call->args_len == 5)
4888 1.635 rillig check_snprintb(call);
4889 1.370 rillig }
4890 1.1 cgd
4891 1.581 rillig static void
4892 1.603 rillig check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
4893 1.370 rillig {
4894 1.1 cgd switch (op) {
4895 1.169 rillig case ADDR:
4896 1.370 rillig check_expr_addr(ln, szof, fcall);
4897 1.1 cgd break;
4898 1.1 cgd case LOAD:
4899 1.613 rillig check_array_index(ln, false);
4900 1.1 cgd /* FALLTHROUGH */
4901 1.1 cgd case INCBEF:
4902 1.1 cgd case DECBEF:
4903 1.1 cgd case INCAFT:
4904 1.1 cgd case DECAFT:
4905 1.1 cgd case ADDASS:
4906 1.1 cgd case SUBASS:
4907 1.1 cgd case MULASS:
4908 1.1 cgd case DIVASS:
4909 1.1 cgd case MODASS:
4910 1.1 cgd case ANDASS:
4911 1.1 cgd case ORASS:
4912 1.1 cgd case XORASS:
4913 1.1 cgd case SHLASS:
4914 1.1 cgd case SHRASS:
4915 1.46 christos case REAL:
4916 1.46 christos case IMAG:
4917 1.370 rillig check_expr_side_effect(ln, szof);
4918 1.1 cgd break;
4919 1.1 cgd case ASSIGN:
4920 1.370 rillig check_expr_assign(ln, szof);
4921 1.1 cgd break;
4922 1.1 cgd case EQ:
4923 1.1 cgd if (hflag && eqwarn)
4924 1.113 rillig /* operator '==' found where '=' was expected */
4925 1.1 cgd warning(160);
4926 1.1 cgd break;
4927 1.443 rillig default:
4928 1.14 christos break;
4929 1.1 cgd }
4930 1.370 rillig }
4931 1.370 rillig
4932 1.440 rillig /*
4933 1.440 rillig * vctx ???
4934 1.440 rillig * cond whether the expression is a condition that
4935 1.440 rillig * will be compared with 0
4936 1.440 rillig * eqwarn whether the operator '==' might be a
4937 1.440 rillig * misspelled '='
4938 1.440 rillig * fcall whether the expression is a function call
4939 1.440 rillig * retval_discarded whether the return value of a function call
4940 1.440 rillig * is discarded; such calls will be analyzed by
4941 1.440 rillig * lint2 in messages 4, 8 and 9
4942 1.440 rillig * szof whether the expression is part of a sizeof
4943 1.440 rillig * expression, which means that its value is
4944 1.440 rillig * discarded since only the type is relevant
4945 1.440 rillig */
4946 1.370 rillig void
4947 1.433 rillig check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
4948 1.381 rillig bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4949 1.370 rillig {
4950 1.370 rillig
4951 1.370 rillig if (tn == NULL)
4952 1.370 rillig return;
4953 1.581 rillig op_t op = tn->tn_op;
4954 1.581 rillig if (op == NAME || op == CON || op == STRING)
4955 1.581 rillig return;
4956 1.634 rillig bool is_direct = op == CALL
4957 1.634 rillig && tn->u.call->func->tn_op == ADDR
4958 1.634 rillig && tn->u.call->func->u.ops.left->tn_op == NAME;
4959 1.634 rillig if (op == CALL) {
4960 1.612 rillig const function_call *call = tn->u.call;
4961 1.634 rillig if (is_direct)
4962 1.603 rillig check_expr_call(tn, call->func,
4963 1.603 rillig szof, vctx, cond, retval_discarded);
4964 1.603 rillig bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4965 1.634 rillig check_expr_misc(call->func, false, false, false, is_direct,
4966 1.603 rillig discard, szof);
4967 1.625 rillig for (size_t i = 0, n = call->args_len; i < n; i++)
4968 1.625 rillig check_expr_misc(call->args[i],
4969 1.647 rillig true, false, false, false, false, szof);
4970 1.603 rillig return;
4971 1.603 rillig }
4972 1.370 rillig
4973 1.581 rillig lint_assert(has_operands(tn));
4974 1.612 rillig tnode_t *ln = tn->u.ops.left;
4975 1.612 rillig tnode_t *rn = tn->u.ops.right;
4976 1.603 rillig check_expr_op(op, ln, szof, fcall, eqwarn);
4977 1.581 rillig
4978 1.536 rillig const mod_t *mp = &modtab[op];
4979 1.536 rillig bool cvctx = mp->m_value_context;
4980 1.536 rillig bool ccond = mp->m_compares_with_zero;
4981 1.536 rillig bool eq = mp->m_warn_if_operand_eq &&
4982 1.584 rillig !ln->tn_parenthesized &&
4983 1.584 rillig rn != NULL && !rn->tn_parenthesized;
4984 1.173 rillig
4985 1.1 cgd /*
4986 1.586 rillig * Values of operands of ':' are not used if the type of at least
4987 1.586 rillig * one of the operands (for GCC compatibility) is 'void'.
4988 1.586 rillig *
4989 1.1 cgd * XXX test/value context of QUEST should probably be used as
4990 1.586 rillig * context for both operands of COLON.
4991 1.1 cgd */
4992 1.1 cgd if (op == COLON && tn->tn_type->t_tspec == VOID)
4993 1.433 rillig cvctx = ccond = false;
4994 1.536 rillig bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4995 1.634 rillig check_expr_misc(ln, cvctx, ccond, eq, is_direct, discard, szof);
4996 1.1 cgd
4997 1.1 cgd switch (op) {
4998 1.1 cgd case LOGAND:
4999 1.1 cgd case LOGOR:
5000 1.173 rillig check_expr_misc(rn, false, true, eq, false, false, szof);
5001 1.1 cgd break;
5002 1.1 cgd case COLON:
5003 1.433 rillig check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
5004 1.19 mycroft break;
5005 1.19 mycroft case COMMA:
5006 1.441 rillig check_expr_misc(rn, vctx, cond, false, false, false, szof);
5007 1.1 cgd break;
5008 1.1 cgd default:
5009 1.1 cgd if (mp->m_binary)
5010 1.173 rillig check_expr_misc(rn, true, false, eq, false, false,
5011 1.173 rillig szof);
5012 1.1 cgd break;
5013 1.1 cgd }
5014 1.1 cgd }
5015 1.1 cgd
5016 1.1 cgd /*
5017 1.218 rillig * Return whether the expression can be used for static initialization.
5018 1.1 cgd *
5019 1.218 rillig * Constant initialization expressions must be constant or an address
5020 1.1 cgd * of a static object with an optional offset. In the first case,
5021 1.1 cgd * the result is returned in *offsp. In the second case, the static
5022 1.1 cgd * object is returned in *symp and the offset in *offsp.
5023 1.1 cgd *
5024 1.169 rillig * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
5025 1.1 cgd * CON. Type conversions are allowed if they do not change binary
5026 1.1 cgd * representation (including width).
5027 1.278 rillig *
5028 1.278 rillig * C99 6.6 "Constant expressions"
5029 1.278 rillig * C99 6.7.8p4 restricts initializers for static storage duration
5030 1.1 cgd */
5031 1.174 rillig bool
5032 1.253 rillig constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
5033 1.1 cgd {
5034 1.253 rillig const sym_t *sym;
5035 1.1 cgd ptrdiff_t offs1, offs2;
5036 1.526 rillig tspec_t t, ot;
5037 1.1 cgd
5038 1.1 cgd switch (tn->tn_op) {
5039 1.1 cgd case MINUS:
5040 1.612 rillig if (tn->u.ops.right->tn_op == CVT)
5041 1.612 rillig return constant_addr(tn->u.ops.right, symp, offsp);
5042 1.684 rillig if (tn->u.ops.right->tn_op != CON)
5043 1.174 rillig return false;
5044 1.1 cgd /* FALLTHROUGH */
5045 1.1 cgd case PLUS:
5046 1.1 cgd offs1 = offs2 = 0;
5047 1.612 rillig if (tn->u.ops.left->tn_op == CON) {
5048 1.612 rillig offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
5049 1.612 rillig if (!constant_addr(tn->u.ops.right, &sym, &offs2))
5050 1.174 rillig return false;
5051 1.612 rillig } else if (tn->u.ops.right->tn_op == CON) {
5052 1.612 rillig offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
5053 1.1 cgd if (tn->tn_op == MINUS)
5054 1.1 cgd offs2 = -offs2;
5055 1.612 rillig if (!constant_addr(tn->u.ops.left, &sym, &offs1))
5056 1.174 rillig return false;
5057 1.1 cgd } else {
5058 1.174 rillig return false;
5059 1.1 cgd }
5060 1.1 cgd *symp = sym;
5061 1.1 cgd *offsp = offs1 + offs2;
5062 1.215 rillig return true;
5063 1.169 rillig case ADDR:
5064 1.612 rillig if (tn->u.ops.left->tn_op == NAME) {
5065 1.612 rillig *symp = tn->u.ops.left->u.sym;
5066 1.1 cgd *offsp = 0;
5067 1.215 rillig return true;
5068 1.1 cgd }
5069 1.684 rillig *symp = NULL;
5070 1.684 rillig *offsp = 0;
5071 1.684 rillig return true;
5072 1.1 cgd case CVT:
5073 1.1 cgd t = tn->tn_type->t_tspec;
5074 1.612 rillig ot = tn->u.ops.left->tn_type->t_tspec;
5075 1.144 rillig if ((!is_integer(t) && t != PTR) ||
5076 1.144 rillig (!is_integer(ot) && ot != PTR)) {
5077 1.174 rillig return false;
5078 1.90 rillig }
5079 1.380 rillig #if 0
5080 1.586 rillig /*-
5081 1.27 christos * consider:
5082 1.97 rillig * struct foo {
5083 1.97 rillig * unsigned char a;
5084 1.97 rillig * } f = {
5085 1.353 rillig * (unsigned char)(unsigned long)
5086 1.353 rillig * (&(((struct foo *)0)->a))
5087 1.27 christos * };
5088 1.353 rillig * since psize(unsigned long) != psize(unsigned char),
5089 1.353 rillig * this fails.
5090 1.27 christos */
5091 1.27 christos else if (psize(t) != psize(ot))
5092 1.95 rillig return -1;
5093 1.27 christos #endif
5094 1.612 rillig return constant_addr(tn->u.ops.left, symp, offsp);
5095 1.1 cgd default:
5096 1.174 rillig return false;
5097 1.1 cgd }
5098 1.1 cgd }
5099 1.1 cgd
5100 1.351 rillig /* Append s2 to s1, then free s2. */
5101 1.600 rillig buffer *
5102 1.600 rillig cat_strings(buffer *s1, buffer *s2)
5103 1.1 cgd {
5104 1.1 cgd
5105 1.600 rillig if ((s1->data != NULL) != (s2->data != NULL)) {
5106 1.1 cgd /* cannot concatenate wide and regular string literals */
5107 1.1 cgd error(292);
5108 1.351 rillig return s1;
5109 1.1 cgd }
5110 1.1 cgd
5111 1.600 rillig if (s1->data != NULL) {
5112 1.601 rillig while (s1->len + s2->len + 1 > s1->cap)
5113 1.601 rillig s1->cap *= 2;
5114 1.601 rillig s1->data = xrealloc(s1->data, s1->cap);
5115 1.600 rillig memcpy(s1->data + s1->len, s2->data, s2->len + 1);
5116 1.600 rillig free(s2->data);
5117 1.599 rillig }
5118 1.600 rillig s1->len += s2->len;
5119 1.351 rillig free(s2);
5120 1.1 cgd
5121 1.351 rillig return s1;
5122 1.1 cgd }
5123 1.1 cgd
5124 1.403 rillig
5125 1.404 rillig typedef struct stmt_expr {
5126 1.490 rillig memory_pool se_mem;
5127 1.404 rillig sym_t *se_sym;
5128 1.404 rillig struct stmt_expr *se_enclosing;
5129 1.404 rillig } stmt_expr;
5130 1.404 rillig
5131 1.404 rillig static stmt_expr *stmt_exprs;
5132 1.404 rillig
5133 1.404 rillig void
5134 1.404 rillig begin_statement_expr(void)
5135 1.404 rillig {
5136 1.567 rillig debug_enter();
5137 1.567 rillig
5138 1.404 rillig stmt_expr *se = xmalloc(sizeof(*se));
5139 1.404 rillig se->se_mem = expr_save_memory();
5140 1.404 rillig se->se_sym = NULL;
5141 1.404 rillig se->se_enclosing = stmt_exprs;
5142 1.404 rillig stmt_exprs = se;
5143 1.404 rillig }
5144 1.404 rillig
5145 1.403 rillig void
5146 1.403 rillig do_statement_expr(tnode_t *tn)
5147 1.403 rillig {
5148 1.403 rillig block_level--;
5149 1.403 rillig mem_block_level--;
5150 1.418 rillig stmt_exprs->se_sym = tn != NULL
5151 1.418 rillig ? mktempsym(block_dup_type(tn->tn_type))
5152 1.418 rillig : NULL; /* after a syntax error */
5153 1.403 rillig mem_block_level++;
5154 1.403 rillig block_level++;
5155 1.556 rillig /* '({ ... })' is a GCC extension */
5156 1.403 rillig gnuism(320);
5157 1.403 rillig }
5158 1.403 rillig
5159 1.403 rillig tnode_t *
5160 1.403 rillig end_statement_expr(void)
5161 1.403 rillig {
5162 1.567 rillig tnode_t *tn;
5163 1.567 rillig
5164 1.404 rillig stmt_expr *se = stmt_exprs;
5165 1.567 rillig if (se->se_sym == NULL) {
5166 1.567 rillig tn = NULL; /* after a syntax error */
5167 1.567 rillig goto end;
5168 1.567 rillig }
5169 1.567 rillig
5170 1.567 rillig tn = build_name(se->se_sym, false);
5171 1.417 rillig (void)expr_save_memory(); /* leak */
5172 1.404 rillig expr_restore_memory(se->se_mem);
5173 1.404 rillig stmt_exprs = se->se_enclosing;
5174 1.404 rillig free(se);
5175 1.567 rillig
5176 1.567 rillig end:
5177 1.567 rillig debug_leave();
5178 1.403 rillig return tn;
5179 1.403 rillig }
5180 1.569 rillig
5181 1.569 rillig bool
5182 1.569 rillig in_statement_expr(void)
5183 1.569 rillig {
5184 1.569 rillig return stmt_exprs != NULL;
5185 1.569 rillig }
5186