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