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