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