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