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