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