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