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