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