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