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