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