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