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