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