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