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