tree.c revision 1.364 1 /* $NetBSD: tree.c,v 1.364 2021/08/31 19:17:45 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.364 2021/08/31 19:17:45 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 error(215, sym->s_name);
240 } else if (sflag) {
241 /* function '%s' implicitly declared to ... */
242 warning(215, sym->s_name);
243 }
244 /*
245 * XXX if tflag is set the symbol should be
246 * exported to level 0
247 */
248 sym->s_type = derive_type(sym->s_type, FUNC);
249 } else {
250 fallback_symbol(sym);
251 }
252 }
253
254 lint_assert(sym->s_kind == FVFT || sym->s_kind == FMEMBER);
255
256 n = expr_zalloc_tnode();
257 n->tn_type = sym->s_type;
258 if (sym->s_scl != CTCONST) {
259 n->tn_op = NAME;
260 n->tn_sym = sym;
261 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
262 n->tn_lvalue = true;
263 } else {
264 n->tn_op = CON;
265 n->tn_val = expr_zalloc(sizeof(*n->tn_val));
266 *n->tn_val = sym->s_value;
267 }
268
269 return n;
270 }
271
272 tnode_t *
273 build_string(strg_t *strg)
274 {
275 size_t len;
276 tnode_t *n;
277
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 * int128_t, 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 bool
1984 should_warn_about_prototype_conversion(
1985 int arg,
1986 const type_t *tp, tspec_t nt,
1987 const tnode_t *tn, tspec_t ot,
1988 const tnode_t *ptn)
1989 {
1990
1991 if (is_floating(nt) != is_floating(ot) ||
1992 portable_size_in_bits(nt) != portable_size_in_bits(ot)) {
1993 /* representation and/or width change */
1994 if (!is_integer(ot))
1995 return true;
1996 return portable_size_in_bits(ot) > portable_size_in_bits(INT);
1997 }
1998
1999 if (!hflag)
2000 return false;
2001
2002 /*
2003 * If the types differ only in sign and the argument has the same
2004 * representation in both types, print no warning.
2005 */
2006 if (ptn->tn_op == CON && is_integer(nt) &&
2007 signed_type(nt) == signed_type(ot) &&
2008 !msb(ptn->tn_val->v_quad, ot))
2009 return false;
2010
2011 return true;
2012 }
2013
2014 /*
2015 * Print a warning if a prototype causes a type conversion that is
2016 * different from what would happen to the same argument in the
2017 * absence of a prototype.
2018 *
2019 * Errors/warnings about illegal type combinations are already printed
2020 * in check_assign_types_compatible().
2021 */
2022 static void
2023 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
2024 tnode_t *tn)
2025 {
2026 tnode_t *ptn;
2027
2028 if (!is_arithmetic(nt) || !is_arithmetic(ot))
2029 return;
2030
2031 /*
2032 * If the type of the formal parameter is char/short, a warning
2033 * would be useless, because functions declared the old style
2034 * can't expect char/short arguments.
2035 */
2036 /* XXX: what about SCHAR? */
2037 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
2038 return;
2039
2040 /* get default promotion */
2041 ptn = promote(NOOP, true, tn);
2042 ot = ptn->tn_type->t_tspec;
2043
2044 /* return if types are the same with and without prototype */
2045 if (nt == ot || (nt == ENUM && ot == INT))
2046 return;
2047
2048 if (should_warn_about_prototype_conversion(
2049 arg, tp, nt, tn, ot, ptn)) {
2050 /* argument #%d is converted from '%s' to '%s' ... */
2051 warning(259, arg, type_name(tn->tn_type), type_name(tp));
2052 }
2053 }
2054
2055 /*
2056 * Print warnings for conversions of integer types which may cause problems.
2057 */
2058 static void
2059 check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
2060 tnode_t *tn)
2061 {
2062
2063 if (tn->tn_op == CON)
2064 return;
2065
2066 if (op == CVT)
2067 return;
2068
2069 if (Sflag && nt == BOOL)
2070 return; /* See C99 6.3.1.2 */
2071
2072 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
2073 is_uinteger(nt) != is_uinteger(ot)) {
2074 if (aflag > 0 && pflag) {
2075 if (op == FARG) {
2076 /* conversion to '%s' may sign-extend ... */
2077 warning(297, type_name(tp), arg);
2078 } else {
2079 /* conversion to '%s' may sign-extend ... */
2080 warning(131, type_name(tp));
2081 }
2082 }
2083 }
2084
2085 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot)) {
2086 switch (tn->tn_op) {
2087 case PLUS:
2088 case MINUS:
2089 case MULT:
2090 case SHL:
2091 /* suggest cast from '%s' to '%s' on op %s to ... */
2092 warning(324, type_name(gettyp(ot)), type_name(tp),
2093 op_name(tn->tn_op));
2094 break;
2095 default:
2096 break;
2097 }
2098 }
2099
2100 if (portable_size_in_bits(nt) < portable_size_in_bits(ot) &&
2101 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
2102 aflag > 1)) {
2103 /* conversion from '%s' may lose accuracy */
2104 if (aflag > 0) {
2105 if (op == FARG) {
2106 /* conv. from '%s' to '%s' may lose ... */
2107 warning(298,
2108 type_name(tn->tn_type), type_name(tp), arg);
2109 } else {
2110 /* conv. from '%s' to '%s' may lose accuracy */
2111 warning(132,
2112 type_name(tn->tn_type), type_name(tp));
2113 }
2114 }
2115 }
2116 }
2117
2118 /*
2119 * Print warnings for dubious conversions of pointer to integer.
2120 */
2121 static void
2122 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
2123 {
2124
2125 if (tn->tn_op == CON)
2126 return;
2127 if (op != CVT)
2128 return; /* We got already an error. */
2129 if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR))
2130 return;
2131
2132 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
2133 /* conversion of pointer to '%s' may lose bits */
2134 warning(134, type_name(tp));
2135 } else {
2136 /* conversion of pointer to '%s' loses bits */
2137 warning(133, type_name(tp));
2138 }
2139 }
2140
2141 static bool
2142 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
2143 const type_t *ostp, tspec_t ost)
2144 {
2145 /*
2146 * Casting a pointer to 'struct S' to a pointer to another struct that
2147 * has 'struct S' as its first member is ok, see msg_247.c, 'struct
2148 * counter'.
2149 */
2150 if (nst == STRUCT && ost == STRUCT &&
2151 nstp->t_str->sou_first_member != NULL &&
2152 nstp->t_str->sou_first_member->s_type == ostp)
2153 return false;
2154
2155 if (is_incomplete(nstp) || is_incomplete(ostp))
2156 return false;
2157
2158 if ((nst == STRUCT || nst == UNION) && nstp->t_str != ostp->t_str)
2159 return true;
2160
2161 if (nst == CHAR || nst == UCHAR)
2162 return false; /* for the sake of traditional C code */
2163 if (ost == CHAR || ost == UCHAR)
2164 return false; /* for the sake of traditional C code */
2165
2166 return portable_size_in_bits(nst) != portable_size_in_bits(ost);
2167 }
2168
2169 /*
2170 * Warn about questionable pointer conversions.
2171 */
2172 static void
2173 check_pointer_conversion(tnode_t *tn, type_t *ntp)
2174 {
2175 const type_t *nstp, *otp, *ostp;
2176 tspec_t nst, ost;
2177 const char *nts, *ots;
2178
2179 nstp = ntp->t_subt;
2180 otp = tn->tn_type;
2181 ostp = otp->t_subt;
2182 nst = nstp->t_tspec;
2183 ost = ostp->t_tspec;
2184
2185 if (nst == VOID || ost == VOID) {
2186 if (sflag && (nst == FUNC || ost == FUNC)) {
2187 /* null pointers are already handled in convert() */
2188 *(nst == FUNC ? &nts : &ots) = "function pointer";
2189 *(nst == VOID ? &nts : &ots) = "'void *'";
2190 /* ANSI C forbids conversion of %s to %s */
2191 warning(303, ots, nts);
2192 }
2193 return;
2194 } else if (nst == FUNC && ost == FUNC) {
2195 return;
2196 } else if (nst == FUNC || ost == FUNC) {
2197 /* converting '%s' to '%s' is questionable */
2198 warning(229, type_name(otp), type_name(ntp));
2199 return;
2200 }
2201
2202 if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
2203 ost != CHAR && ost != UCHAR &&
2204 !is_incomplete(ostp)) {
2205 /* converting '%s' to '%s' may cause alignment problem */
2206 warning(135, type_name(otp), type_name(ntp));
2207 }
2208
2209 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
2210 /* pointer cast from '%s' to '%s' may be troublesome */
2211 warning(247, type_name(otp), type_name(ntp));
2212 }
2213 }
2214
2215 static void
2216 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
2217 tspec_t nt, val_t *v, val_t *nv)
2218 {
2219 ldbl_t max = 0.0, min = 0.0;
2220
2221 switch (nt) {
2222 case CHAR:
2223 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
2224 case UCHAR:
2225 max = TARG_UCHAR_MAX; min = 0; break;
2226 case SCHAR:
2227 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
2228 case SHORT:
2229 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
2230 case USHORT:
2231 max = TARG_USHRT_MAX; min = 0; break;
2232 case ENUM:
2233 case INT:
2234 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
2235 case UINT:
2236 max = TARG_UINT_MAX; min = 0; break;
2237 case LONG:
2238 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
2239 case ULONG:
2240 max = TARG_ULONG_MAX; min = 0; break;
2241 case QUAD:
2242 max = QUAD_MAX; min = QUAD_MIN; break;
2243 case UQUAD:
2244 max = UQUAD_MAX; min = 0; break;
2245 case FLOAT:
2246 case FCOMPLEX:
2247 max = FLT_MAX; min = -FLT_MAX; break;
2248 case DOUBLE:
2249 case DCOMPLEX:
2250 max = DBL_MAX; min = -DBL_MAX; break;
2251 case PTR:
2252 /* Got already an error because of float --> ptr */
2253 case LDOUBLE:
2254 case LCOMPLEX:
2255 max = LDBL_MAX; min = -LDBL_MAX; break;
2256 default:
2257 lint_assert(/*CONSTCOND*/false);
2258 }
2259 if (v->v_ldbl > max || v->v_ldbl < min) {
2260 lint_assert(nt != LDOUBLE);
2261 if (op == FARG) {
2262 /* conv. of '%s' to '%s' is out of range, ... */
2263 warning(295,
2264 type_name(gettyp(ot)), type_name(tp), arg);
2265 } else {
2266 /* conversion of '%s' to '%s' is out of range */
2267 warning(119,
2268 type_name(gettyp(ot)), type_name(tp));
2269 }
2270 v->v_ldbl = v->v_ldbl > 0 ? max : min;
2271 }
2272
2273 if (nt == FLOAT) {
2274 nv->v_ldbl = (float)v->v_ldbl;
2275 } else if (nt == DOUBLE) {
2276 nv->v_ldbl = (double)v->v_ldbl;
2277 } else if (nt == LDOUBLE) {
2278 nv->v_ldbl = v->v_ldbl;
2279 } else {
2280 nv->v_quad = (int64_t)v->v_ldbl;
2281 }
2282 }
2283
2284 static bool
2285 convert_constant_to_floating(tspec_t nt, val_t *nv,
2286 tspec_t ot, const val_t *v)
2287 {
2288 if (nt == FLOAT) {
2289 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2290 (float)(uint64_t)v->v_quad : (float)v->v_quad;
2291 } else if (nt == DOUBLE) {
2292 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2293 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2294 } else if (nt == LDOUBLE) {
2295 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2296 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2297 } else
2298 return false;
2299 return true;
2300 }
2301
2302 /*
2303 * Print a warning if bits which were set are lost due to the conversion.
2304 * This can happen with operator ORASS only.
2305 */
2306 static void
2307 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
2308 uint64_t xmask, op_t op)
2309 {
2310 if (nsz < osz && (v->v_quad & xmask) != 0) {
2311 /* constant truncated by conv., op %s */
2312 warning(306, op_name(op));
2313 }
2314 }
2315
2316 /*
2317 * Print a warning if additional bits are not all 1
2318 * and the most significant bit of the old value is 1,
2319 * or if at least one (but not all) removed bit was 0.
2320 */
2321 static void
2322 convert_constant_check_range_bitand(size_t nsz, size_t osz,
2323 uint64_t xmask, const val_t *nv,
2324 tspec_t ot, const val_t *v,
2325 const type_t *tp, op_t op)
2326 {
2327 if (nsz > osz &&
2328 (nv->v_quad & bit(osz - 1)) != 0 &&
2329 (nv->v_quad & xmask) != xmask) {
2330 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
2331 warning(309, type_name(gettyp(ot)),
2332 type_name(tp), op_name(op));
2333 } else if (nsz < osz &&
2334 (v->v_quad & xmask) != xmask &&
2335 (v->v_quad & xmask) != 0) {
2336 /* constant truncated by conversion, op %s */
2337 warning(306, op_name(op));
2338 }
2339 }
2340
2341 static void
2342 convert_constant_check_range_signed(op_t op, int arg)
2343 {
2344 if (op == ASSIGN) {
2345 /* assignment of negative constant to unsigned type */
2346 warning(164);
2347 } else if (op == INIT) {
2348 /* initialization of unsigned with negative constant */
2349 warning(221);
2350 } else if (op == FARG) {
2351 /* conversion of negative constant to unsigned type, ... */
2352 warning(296, arg);
2353 } else if (modtab[op].m_comparison) {
2354 /* handled by check_integer_comparison() */
2355 } else {
2356 /* conversion of negative constant to unsigned type */
2357 warning(222);
2358 }
2359 }
2360
2361 /*
2362 * Loss of significant bit(s). All truncated bits
2363 * of unsigned types or all truncated bits plus the
2364 * msb of the target for signed types are considered
2365 * to be significant bits. Loss of significant bits
2366 * means that at least one of the bits was set in an
2367 * unsigned type or that at least one but not all of
2368 * the bits was set in a signed type.
2369 * Loss of significant bits means that it is not
2370 * possible, also not with necessary casts, to convert
2371 * back to the original type. A example for a
2372 * necessary cast is:
2373 * char c; int i; c = 128;
2374 * i = c; ** yields -128 **
2375 * i = (unsigned char)c; ** yields 128 **
2376 */
2377 static void
2378 convert_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
2379 tspec_t ot)
2380 {
2381 if (op == ASSIGN && tp->t_bitfield) {
2382 /* precision lost in bit-field assignment */
2383 warning(166);
2384 } else if (op == ASSIGN) {
2385 /* constant truncated by assignment */
2386 warning(165);
2387 } else if (op == INIT && tp->t_bitfield) {
2388 /* bit-field initializer does not fit */
2389 warning(180);
2390 } else if (op == INIT) {
2391 /* initializer does not fit */
2392 warning(178);
2393 } else if (op == CASE) {
2394 /* case label affected by conversion */
2395 warning(196);
2396 } else if (op == FARG) {
2397 /* conversion of '%s' to '%s' is out of range, arg #%d */
2398 warning(295,
2399 type_name(gettyp(ot)), type_name(tp), arg);
2400 } else {
2401 /* conversion of '%s' to '%s' is out of range */
2402 warning(119,
2403 type_name(gettyp(ot)), type_name(tp));
2404 }
2405 }
2406
2407 static void
2408 convert_constant_check_range_loss(op_t op, int arg, const type_t *tp,
2409 tspec_t ot)
2410 {
2411 if (op == ASSIGN && tp->t_bitfield) {
2412 /* precision lost in bit-field assignment */
2413 warning(166);
2414 } else if (op == INIT && tp->t_bitfield) {
2415 /* bit-field initializer out of range */
2416 warning(11);
2417 } else if (op == CASE) {
2418 /* case label affected by conversion */
2419 warning(196);
2420 } else if (op == FARG) {
2421 /* conversion of '%s' to '%s' is out of range, arg #%d */
2422 warning(295,
2423 type_name(gettyp(ot)), type_name(tp), arg);
2424 } else {
2425 /* conversion of '%s' to '%s' is out of range */
2426 warning(119,
2427 type_name(gettyp(ot)), type_name(tp));
2428 }
2429 }
2430
2431 static void
2432 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
2433 op_t op, int arg, const val_t *v, val_t *nv)
2434 {
2435 int osz, nsz;
2436 int64_t xmask, xmsk1;
2437
2438 osz = size_in_bits(ot);
2439 nsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
2440 xmask = value_bits(nsz) ^ value_bits(osz);
2441 xmsk1 = value_bits(nsz) ^ value_bits(osz - 1);
2442 /*
2443 * For bitwise operations we are not interested in the
2444 * value, but in the bits itself.
2445 */
2446 if (op == ORASS || op == BITOR || op == BITXOR) {
2447 convert_constant_check_range_bitor(nsz, osz, v, xmask, op);
2448 } else if (op == ANDASS || op == BITAND) {
2449 convert_constant_check_range_bitand(nsz, osz, xmask, nv, ot,
2450 v, tp, op);
2451 } else if ((nt != PTR && is_uinteger(nt)) &&
2452 (ot != PTR && !is_uinteger(ot)) &&
2453 v->v_quad < 0) {
2454 convert_constant_check_range_signed(op, arg);
2455 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2456 (v->v_quad & xmask) != 0 &&
2457 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2458 convert_constant_check_range_truncated(op, arg, tp, ot);
2459 } else if (nv->v_quad != v->v_quad) {
2460 convert_constant_check_range_loss(op, arg, tp, ot);
2461 }
2462 }
2463
2464 /*
2465 * Converts a typed constant to a constant of another type.
2466 *
2467 * op operator which requires conversion
2468 * arg if op is FARG, # of argument
2469 * tp type in which to convert the constant
2470 * nv new constant
2471 * v old constant
2472 */
2473 void
2474 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v)
2475 {
2476 tspec_t ot, nt;
2477 int sz;
2478 bool range_check;
2479
2480 /*
2481 * TODO: make 'v' const; the name of this function does not suggest
2482 * that it modifies 'v'.
2483 */
2484 ot = v->v_tspec;
2485 nt = nv->v_tspec = tp->t_tspec;
2486 range_check = false;
2487
2488 if (nt == BOOL) { /* C99 6.3.1.2 */
2489 nv->v_unsigned_since_c90 = false;
2490 nv->v_quad = is_nonzero_val(v) ? 1 : 0;
2491 return;
2492 }
2493
2494 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
2495 convert_constant_floating(op, arg, ot, tp, nt, v, nv);
2496 } else if (!convert_constant_to_floating(nt, nv, ot, v)) {
2497 range_check = true; /* Check for lost precision. */
2498 nv->v_quad = v->v_quad;
2499 }
2500
2501 if ((v->v_unsigned_since_c90 && is_floating(nt)) ||
2502 (v->v_unsigned_since_c90 && (is_integer(nt) && !is_uinteger(nt) &&
2503 portable_size_in_bits(nt) >
2504 portable_size_in_bits(ot)))) {
2505 /* ANSI C treats constant as unsigned */
2506 warning(157);
2507 v->v_unsigned_since_c90 = false;
2508 }
2509
2510 if (is_integer(nt)) {
2511 sz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
2512 nv->v_quad = convert_integer(nv->v_quad, nt, sz);
2513 }
2514
2515 if (range_check && op != CVT)
2516 convert_constant_check_range(ot, tp, nt, op, arg, v, nv);
2517 }
2518
2519 /*
2520 * Called if incompatible types were detected.
2521 * Prints a appropriate warning.
2522 */
2523 static void
2524 warn_incompatible_types(op_t op,
2525 const type_t *ltp, tspec_t lt,
2526 const type_t *rtp, tspec_t rt)
2527 {
2528 const mod_t *mp;
2529
2530 mp = &modtab[op];
2531
2532 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2533 /* void type illegal in expression */
2534 error(109);
2535 } else if (op == ASSIGN) {
2536 if ((lt == STRUCT || lt == UNION) &&
2537 (rt == STRUCT || rt == UNION)) {
2538 /* assignment of different structures (%s != %s) */
2539 error(240, tspec_name(lt), tspec_name(rt));
2540 } else {
2541 /* cannot assign to '%s' from '%s' */
2542 error(171, type_name(ltp), type_name(rtp));
2543 }
2544 } else if (mp->m_binary) {
2545 /* operands of '%s' have incompatible types (%s != %s) */
2546 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2547 } else {
2548 lint_assert(rt == NOTSPEC);
2549 /* operand of '%s' has invalid type (%s) */
2550 error(108, mp->m_name, tspec_name(lt));
2551 }
2552 }
2553
2554 /*
2555 * Called if incompatible pointer types are detected.
2556 * Print an appropriate warning.
2557 */
2558 static void
2559 warn_incompatible_pointers(const mod_t *mp,
2560 const type_t *ltp, const type_t *rtp)
2561 {
2562 tspec_t lt, rt;
2563
2564 lint_assert(ltp->t_tspec == PTR);
2565 lint_assert(rtp->t_tspec == PTR);
2566
2567 lt = ltp->t_subt->t_tspec;
2568 rt = rtp->t_subt->t_tspec;
2569
2570 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2571 if (mp == NULL) {
2572 /* illegal structure pointer combination */
2573 warning(244);
2574 } else {
2575 /* incompatible structure pointers: '%s' '%s' '%s' */
2576 warning(245, type_name(ltp), mp->m_name, type_name(rtp));
2577 }
2578 } else {
2579 if (mp == NULL) {
2580 /* illegal combination of '%s' and '%s' */
2581 warning(184, type_name(ltp), type_name(rtp));
2582 } else {
2583 /* illegal combination of '%s' and '%s', op '%s' */
2584 warning(124,
2585 type_name(ltp), type_name(rtp), mp->m_name);
2586 }
2587 }
2588 }
2589
2590 /* Return a type based on tp1, with added qualifiers from tp2. */
2591 static type_t *
2592 merge_qualifiers(type_t *tp1, const type_t *tp2)
2593 {
2594 type_t *ntp, *nstp;
2595
2596 lint_assert(tp1->t_tspec == PTR);
2597 lint_assert(tp2->t_tspec == PTR);
2598
2599 bool c1 = tp1->t_subt->t_const;
2600 bool c2 = tp2->t_subt->t_const;
2601 bool v1 = tp1->t_subt->t_volatile;
2602 bool v2 = tp2->t_subt->t_volatile;
2603
2604 if (c1 == (c1 | c2) && v1 == (v1 | v2))
2605 return tp1;
2606
2607 nstp = expr_dup_type(tp1->t_subt);
2608 nstp->t_const |= c2;
2609 nstp->t_volatile |= v2;
2610
2611 ntp = expr_dup_type(tp1);
2612 ntp->t_subt = nstp;
2613 return ntp;
2614 }
2615
2616 /*
2617 * Returns true if the given structure or union has a constant member
2618 * (maybe recursively).
2619 */
2620 static bool
2621 has_constant_member(const type_t *tp)
2622 {
2623 sym_t *m;
2624
2625 lint_assert(is_struct_or_union(tp->t_tspec));
2626
2627 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
2628 const type_t *mtp = m->s_type;
2629 if (mtp->t_const)
2630 return true;
2631 if (is_struct_or_union(mtp->t_tspec) &&
2632 has_constant_member(mtp))
2633 return true;
2634 }
2635 return false;
2636 }
2637
2638 /*
2639 * Create a new node for one of the operators POINT and ARROW.
2640 */
2641 static tnode_t *
2642 build_struct_access(op_t op, tnode_t *ln, tnode_t *rn)
2643 {
2644 tnode_t *ntn, *ctn;
2645 bool nolval;
2646
2647 lint_assert(rn->tn_op == NAME);
2648 lint_assert(rn->tn_sym->s_value.v_tspec == INT);
2649 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
2650
2651 /*
2652 * Remember if the left operand is an lvalue (structure members
2653 * are lvalues if and only if the structure itself is an lvalue).
2654 */
2655 nolval = op == POINT && !ln->tn_lvalue;
2656
2657 if (op == POINT) {
2658 ln = build_address(ln, true);
2659 } else if (ln->tn_type->t_tspec != PTR) {
2660 lint_assert(tflag);
2661 lint_assert(is_integer(ln->tn_type->t_tspec));
2662 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
2663 }
2664
2665 ctn = build_integer_constant(PTRDIFF_TSPEC,
2666 rn->tn_sym->s_value.v_quad / CHAR_SIZE);
2667
2668 ntn = new_tnode(PLUS, expr_derive_type(rn->tn_type, PTR), ln, ctn);
2669 if (ln->tn_op == CON)
2670 ntn = fold(ntn);
2671
2672 if (rn->tn_type->t_bitfield) {
2673 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2674 } else {
2675 ntn = new_tnode(INDIR, ntn->tn_type->t_subt, ntn, NULL);
2676 }
2677
2678 if (nolval)
2679 ntn->tn_lvalue = false;
2680
2681 return ntn;
2682 }
2683
2684 /*
2685 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2686 */
2687 static tnode_t *
2688 build_prepost_incdec(op_t op, tnode_t *ln)
2689 {
2690 tnode_t *cn, *ntn;
2691
2692 lint_assert(ln != NULL);
2693
2694 if (ln->tn_type->t_tspec == PTR) {
2695 cn = plength(ln->tn_type);
2696 } else {
2697 cn = build_integer_constant(INT, (int64_t)1);
2698 }
2699 ntn = new_tnode(op, ln->tn_type, ln, cn);
2700
2701 return ntn;
2702 }
2703
2704 /*
2705 * Create a node for REAL, IMAG
2706 */
2707 static tnode_t *
2708 build_real_imag(op_t op, tnode_t *ln)
2709 {
2710 tnode_t *cn, *ntn;
2711
2712 lint_assert(ln != NULL);
2713
2714 if (ln->tn_op == NAME) {
2715 /*
2716 * This may be too much, but it avoids wrong warnings.
2717 * See d_c99_complex_split.c.
2718 */
2719 mark_as_used(ln->tn_sym, false, false);
2720 mark_as_set(ln->tn_sym);
2721 }
2722
2723 switch (ln->tn_type->t_tspec) {
2724 case LCOMPLEX:
2725 /* XXX: integer and LDOUBLE don't match. */
2726 cn = build_integer_constant(LDOUBLE, (int64_t)1);
2727 break;
2728 case DCOMPLEX:
2729 /* XXX: integer and DOUBLE don't match. */
2730 cn = build_integer_constant(DOUBLE, (int64_t)1);
2731 break;
2732 case FCOMPLEX:
2733 /* XXX: integer and FLOAT don't match. */
2734 cn = build_integer_constant(FLOAT, (int64_t)1);
2735 break;
2736 default:
2737 /* __%s__ is illegal for type %s */
2738 error(276, op == REAL ? "real" : "imag",
2739 type_name(ln->tn_type));
2740 return NULL;
2741 }
2742 ntn = new_tnode(op, cn->tn_type, ln, cn);
2743 ntn->tn_lvalue = true;
2744
2745 return ntn;
2746 }
2747
2748 /*
2749 * Create a tree node for the unary & operator
2750 */
2751 static tnode_t *
2752 build_address(tnode_t *tn, bool noign)
2753 {
2754 tspec_t t;
2755
2756 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2757 if (tflag)
2758 /* '&' before array or function: ignored */
2759 warning(127);
2760 return tn;
2761 }
2762
2763 /* eliminate &* */
2764 if (tn->tn_op == INDIR &&
2765 tn->tn_left->tn_type->t_tspec == PTR &&
2766 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2767 return tn->tn_left;
2768 }
2769
2770 return new_tnode(ADDR, expr_derive_type(tn->tn_type, PTR), tn, NULL);
2771 }
2772
2773 /*
2774 * Create a node for operators PLUS and MINUS.
2775 */
2776 static tnode_t *
2777 build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn)
2778 {
2779 tnode_t *ntn, *ctn;
2780 type_t *tp;
2781
2782 /* If pointer and integer, then pointer to the lhs. */
2783 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
2784 ntn = ln;
2785 ln = rn;
2786 rn = ntn;
2787 }
2788
2789 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2790 lint_assert(is_integer(rn->tn_type->t_tspec));
2791
2792 check_ctype_macro_invocation(ln, rn);
2793
2794 ctn = plength(ln->tn_type);
2795 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2796 rn = convert(NOOP, 0, ctn->tn_type, rn);
2797 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2798 if (rn->tn_left->tn_op == CON)
2799 rn = fold(rn);
2800 ntn = new_tnode(op, ln->tn_type, ln, rn);
2801
2802 } else if (rn->tn_type->t_tspec == PTR) {
2803
2804 lint_assert(ln->tn_type->t_tspec == PTR);
2805 lint_assert(op == MINUS);
2806 tp = gettyp(PTRDIFF_TSPEC);
2807 ntn = new_tnode(op, tp, ln, rn);
2808 if (ln->tn_op == CON && rn->tn_op == CON)
2809 ntn = fold(ntn);
2810 ctn = plength(ln->tn_type);
2811 balance(NOOP, &ntn, &ctn);
2812 ntn = new_tnode(DIV, tp, ntn, ctn);
2813
2814 } else {
2815
2816 ntn = new_tnode(op, ln->tn_type, ln, rn);
2817
2818 }
2819 return ntn;
2820 }
2821
2822 /*
2823 * Create a node for operators SHL and SHR.
2824 */
2825 static tnode_t *
2826 build_bit_shift(op_t op, tnode_t *ln, tnode_t *rn)
2827 {
2828 tspec_t t;
2829 tnode_t *ntn;
2830
2831 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2832 rn = convert(CVT, 0, gettyp(INT), rn);
2833 ntn = new_tnode(op, ln->tn_type, ln, rn);
2834 return ntn;
2835 }
2836
2837 /*
2838 * Create a node for COLON.
2839 */
2840 static tnode_t *
2841 build_colon(tnode_t *ln, tnode_t *rn)
2842 {
2843 tspec_t lt, rt, pdt;
2844 type_t *tp;
2845 tnode_t *ntn;
2846
2847 lt = ln->tn_type->t_tspec;
2848 rt = rn->tn_type->t_tspec;
2849 pdt = PTRDIFF_TSPEC;
2850
2851 /*
2852 * Arithmetic types are balanced, all other type combinations
2853 * still need to be handled.
2854 */
2855 if (is_arithmetic(lt) && is_arithmetic(rt)) {
2856 tp = ln->tn_type;
2857 } else if (lt == BOOL && rt == BOOL) {
2858 tp = ln->tn_type;
2859 } else if (lt == VOID || rt == VOID) {
2860 tp = gettyp(VOID);
2861 } else if (lt == STRUCT || lt == UNION) {
2862 /* Both types must be identical. */
2863 lint_assert(rt == STRUCT || rt == UNION);
2864 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2865 if (is_incomplete(ln->tn_type)) {
2866 /* unknown operand size, op %s */
2867 error(138, op_name(COLON));
2868 return NULL;
2869 }
2870 tp = ln->tn_type;
2871 } else if (lt == PTR && is_integer(rt)) {
2872 if (rt != pdt) {
2873 rn = convert(NOOP, 0, gettyp(pdt), rn);
2874 rt = pdt;
2875 }
2876 tp = ln->tn_type;
2877 } else if (rt == PTR && is_integer(lt)) {
2878 if (lt != pdt) {
2879 ln = convert(NOOP, 0, gettyp(pdt), ln);
2880 lt = pdt;
2881 }
2882 tp = rn->tn_type;
2883 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2884 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
2885 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2886 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
2887 } else {
2888 /*
2889 * XXX For now we simply take the left type. This is
2890 * probably wrong, if one type contains a function prototype
2891 * and the other one, at the same place, only an old style
2892 * declaration.
2893 */
2894 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
2895 }
2896
2897 ntn = new_tnode(COLON, tp, ln, rn);
2898
2899 return ntn;
2900 }
2901
2902 /*
2903 * Create a node for an assignment operator (both = and op= ).
2904 */
2905 static tnode_t *
2906 build_assignment(op_t op, tnode_t *ln, tnode_t *rn)
2907 {
2908 tspec_t lt, rt;
2909 tnode_t *ntn, *ctn;
2910
2911 lint_assert(ln != NULL);
2912 lint_assert(rn != NULL);
2913
2914 lt = ln->tn_type->t_tspec;
2915 rt = rn->tn_type->t_tspec;
2916
2917 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2918 lint_assert(is_integer(rt));
2919 ctn = plength(ln->tn_type);
2920 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2921 rn = convert(NOOP, 0, ctn->tn_type, rn);
2922 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2923 if (rn->tn_left->tn_op == CON)
2924 rn = fold(rn);
2925 }
2926
2927 if ((op == ASSIGN || op == RETURN || op == INIT) &&
2928 (lt == STRUCT || rt == STRUCT)) {
2929 lint_assert(lt == rt);
2930 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2931 if (is_incomplete(ln->tn_type)) {
2932 if (op == RETURN) {
2933 /* cannot return incomplete type */
2934 error(212);
2935 } else {
2936 /* unknown operand size, op %s */
2937 error(138, op_name(op));
2938 }
2939 return NULL;
2940 }
2941 }
2942
2943 if (op == SHLASS) {
2944 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
2945 if (hflag)
2946 /* semantics of '%s' change in ANSI C; ... */
2947 warning(118, "<<=");
2948 }
2949 } else if (op != SHRASS) {
2950 if (op == ASSIGN || lt != PTR) {
2951 if (lt != rt ||
2952 (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
2953 rn = convert(op, 0, ln->tn_type, rn);
2954 rt = lt;
2955 }
2956 }
2957 }
2958
2959 ntn = new_tnode(op, ln->tn_type, ln, rn);
2960
2961 return ntn;
2962 }
2963
2964 /*
2965 * Get length of type tp->t_subt, as a constant expression of type ptrdiff_t
2966 * as seen from the target platform.
2967 */
2968 static tnode_t *
2969 plength(type_t *tp)
2970 {
2971 int elem, elsz_in_bits;
2972
2973 lint_assert(tp->t_tspec == PTR);
2974 tp = tp->t_subt;
2975
2976 elem = 1;
2977 elsz_in_bits = 0;
2978
2979 while (tp->t_tspec == ARRAY) {
2980 elem *= tp->t_dim;
2981 tp = tp->t_subt;
2982 }
2983
2984 switch (tp->t_tspec) {
2985 case FUNC:
2986 /* pointer to function is not allowed here */
2987 error(110);
2988 break;
2989 case VOID:
2990 /* cannot do pointer arithmetic on operand of unknown size */
2991 gnuism(136);
2992 break;
2993 case STRUCT:
2994 case UNION:
2995 if ((elsz_in_bits = tp->t_str->sou_size_in_bits) == 0)
2996 /* cannot do pointer arithmetic on operand of ... */
2997 error(136);
2998 break;
2999 case ENUM:
3000 if (is_incomplete(tp)) {
3001 /* cannot do pointer arithmetic on operand of ... */
3002 warning(136);
3003 }
3004 /* FALLTHROUGH */
3005 default:
3006 if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0) {
3007 /* cannot do pointer arithmetic on operand of ... */
3008 error(136);
3009 } else {
3010 lint_assert(elsz_in_bits != -1);
3011 }
3012 break;
3013 }
3014
3015 if (elem == 0 && elsz_in_bits != 0) {
3016 /* cannot do pointer arithmetic on operand of unknown size */
3017 error(136);
3018 }
3019
3020 if (elsz_in_bits == 0)
3021 elsz_in_bits = CHAR_SIZE;
3022
3023 return build_integer_constant(PTRDIFF_TSPEC,
3024 (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
3025 }
3026
3027 /*
3028 * XXX
3029 * Note: There appear to be a number of bugs in detecting overflow in
3030 * this function. An audit and a set of proper regression tests are needed.
3031 * --Perry Metzger, Nov. 16, 2001
3032 */
3033 /*
3034 * Do only as much as necessary to compute constant expressions.
3035 * Called only if the operator allows folding and all operands are constants.
3036 */
3037 static tnode_t *
3038 fold(tnode_t *tn)
3039 {
3040 val_t *v;
3041 tspec_t t;
3042 bool utyp, ovfl;
3043 int64_t sl, sr = 0, q = 0, mask;
3044 uint64_t ul, ur = 0;
3045 tnode_t *cn;
3046
3047 v = xcalloc(1, sizeof(*v));
3048 v->v_tspec = tn->tn_type->t_tspec;
3049
3050 t = tn->tn_left->tn_type->t_tspec;
3051 utyp = !is_integer(t) || is_uinteger(t);
3052 ul = sl = tn->tn_left->tn_val->v_quad;
3053 if (is_binary(tn))
3054 ur = sr = tn->tn_right->tn_val->v_quad;
3055
3056 mask = value_bits(size_in_bits(t));
3057 ovfl = false;
3058
3059 switch (tn->tn_op) {
3060 case UPLUS:
3061 q = sl;
3062 break;
3063 case UMINUS:
3064 q = -sl;
3065 if (sl != 0 && msb(q, t) == msb(sl, t))
3066 ovfl = true;
3067 break;
3068 case COMPL:
3069 q = ~sl;
3070 break;
3071 case MULT:
3072 if (utyp) {
3073 q = ul * ur;
3074 if (q != (q & mask))
3075 ovfl = true;
3076 else if ((ul != 0) && ((q / ul) != ur))
3077 ovfl = true;
3078 } else {
3079 q = sl * sr;
3080 if (msb(q, t) != (msb(sl, t) ^ msb(sr, t)))
3081 ovfl = true;
3082 }
3083 break;
3084 case DIV:
3085 if (sr == 0) {
3086 /* division by 0 */
3087 error(139);
3088 q = utyp ? UQUAD_MAX : QUAD_MAX;
3089 } else {
3090 q = utyp ? (int64_t)(ul / ur) : sl / sr;
3091 }
3092 break;
3093 case MOD:
3094 if (sr == 0) {
3095 /* modulus by 0 */
3096 error(140);
3097 q = 0;
3098 } else {
3099 q = utyp ? (int64_t)(ul % ur) : sl % sr;
3100 }
3101 break;
3102 case PLUS:
3103 q = utyp ? (int64_t)(ul + ur) : sl + sr;
3104 if (msb(sl, t) && msb(sr, t) && !msb(q, t))
3105 ovfl = true;
3106 if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(q, t))
3107 ovfl = true;
3108 break;
3109 case MINUS:
3110 q = utyp ? (int64_t)(ul - ur) : sl - sr;
3111 if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(q, t))
3112 ovfl = true;
3113 if (!msb(sl, t) && msb(sr, t) && msb(q, t))
3114 ovfl = true;
3115 break;
3116 case SHL:
3117 q = utyp ? (int64_t)(ul << sr) : sl << sr;
3118 break;
3119 case SHR:
3120 /*
3121 * The sign must be explicitly extended because
3122 * shifts of signed values are implementation dependent.
3123 */
3124 q = ul >> sr;
3125 q = convert_integer(q, t, size_in_bits(t) - (int)sr);
3126 break;
3127 case LT:
3128 q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
3129 break;
3130 case LE:
3131 q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
3132 break;
3133 case GE:
3134 q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
3135 break;
3136 case GT:
3137 q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
3138 break;
3139 case EQ:
3140 q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
3141 break;
3142 case NE:
3143 q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
3144 break;
3145 case BITAND:
3146 q = utyp ? (int64_t)(ul & ur) : sl & sr;
3147 break;
3148 case BITXOR:
3149 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
3150 break;
3151 case BITOR:
3152 q = utyp ? (int64_t)(ul | ur) : sl | sr;
3153 break;
3154 default:
3155 lint_assert(/*CONSTCOND*/false);
3156 }
3157
3158 /* XXX does not work for quads. */
3159 if (ovfl ||
3160 ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
3161 if (hflag)
3162 /* integer overflow detected, op '%s' */
3163 warning(141, op_name(tn->tn_op));
3164 }
3165
3166 v->v_quad = convert_integer(q, t, 0);
3167
3168 cn = build_constant(tn->tn_type, v);
3169 if (tn->tn_left->tn_system_dependent)
3170 cn->tn_system_dependent = true;
3171 if (is_binary(tn) && tn->tn_right->tn_system_dependent)
3172 cn->tn_system_dependent = true;
3173
3174 return cn;
3175 }
3176
3177 /*
3178 * Fold constant nodes, as much as is needed for comparing the value with 0
3179 * (test context, for controlling expressions).
3180 */
3181 static tnode_t *
3182 fold_test(tnode_t *tn)
3183 {
3184 bool l, r;
3185 val_t *v;
3186
3187 v = xcalloc(1, sizeof(*v));
3188 v->v_tspec = tn->tn_type->t_tspec;
3189 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
3190
3191 l = constant_is_nonzero(tn->tn_left);
3192 r = is_binary(tn) && constant_is_nonzero(tn->tn_right);
3193
3194 switch (tn->tn_op) {
3195 case NOT:
3196 if (hflag && !constcond_flag)
3197 /* constant argument to '!' */
3198 warning(239);
3199 v->v_quad = !l ? 1 : 0;
3200 break;
3201 case LOGAND:
3202 v->v_quad = l && r ? 1 : 0;
3203 break;
3204 case LOGOR:
3205 v->v_quad = l || r ? 1 : 0;
3206 break;
3207 default:
3208 lint_assert(/*CONSTCOND*/false);
3209 }
3210
3211 return build_constant(tn->tn_type, v);
3212 }
3213
3214 /*
3215 * Fold constant nodes having operands with floating point type.
3216 */
3217 static tnode_t *
3218 fold_float(tnode_t *tn)
3219 {
3220 val_t *v;
3221 tspec_t t;
3222 ldbl_t lv, rv = 0;
3223
3224 fpe = 0;
3225 v = xcalloc(1, sizeof(*v));
3226 v->v_tspec = t = tn->tn_type->t_tspec;
3227
3228 lint_assert(is_floating(t));
3229 lint_assert(t == tn->tn_left->tn_type->t_tspec);
3230 lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec);
3231
3232 lv = tn->tn_left->tn_val->v_ldbl;
3233 if (is_binary(tn))
3234 rv = tn->tn_right->tn_val->v_ldbl;
3235
3236 switch (tn->tn_op) {
3237 case UPLUS:
3238 v->v_ldbl = lv;
3239 break;
3240 case UMINUS:
3241 v->v_ldbl = -lv;
3242 break;
3243 case MULT:
3244 v->v_ldbl = lv * rv;
3245 break;
3246 case DIV:
3247 if (rv == 0.0) {
3248 /* division by 0 */
3249 error(139);
3250 if (t == FLOAT) {
3251 v->v_ldbl = lv < 0 ? -FLT_MAX : FLT_MAX;
3252 } else if (t == DOUBLE) {
3253 v->v_ldbl = lv < 0 ? -DBL_MAX : DBL_MAX;
3254 } else {
3255 v->v_ldbl = lv < 0 ? -LDBL_MAX : LDBL_MAX;
3256 }
3257 } else {
3258 v->v_ldbl = lv / rv;
3259 }
3260 break;
3261 case PLUS:
3262 v->v_ldbl = lv + rv;
3263 break;
3264 case MINUS:
3265 v->v_ldbl = lv - rv;
3266 break;
3267 case LT:
3268 v->v_quad = lv < rv ? 1 : 0;
3269 break;
3270 case LE:
3271 v->v_quad = lv <= rv ? 1 : 0;
3272 break;
3273 case GE:
3274 v->v_quad = lv >= rv ? 1 : 0;
3275 break;
3276 case GT:
3277 v->v_quad = lv > rv ? 1 : 0;
3278 break;
3279 case EQ:
3280 v->v_quad = lv == rv ? 1 : 0;
3281 break;
3282 case NE:
3283 v->v_quad = lv != rv ? 1 : 0;
3284 break;
3285 default:
3286 lint_assert(/*CONSTCOND*/false);
3287 }
3288
3289 lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0);
3290 if (fpe != 0 || finite((double)v->v_ldbl) == 0 ||
3291 (t == FLOAT &&
3292 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3293 (t == DOUBLE &&
3294 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3295 /* floating point overflow detected, op %s */
3296 warning(142, op_name(tn->tn_op));
3297 if (t == FLOAT) {
3298 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3299 } else if (t == DOUBLE) {
3300 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3301 } else {
3302 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX : LDBL_MAX;
3303 }
3304 fpe = 0;
3305 }
3306
3307 return build_constant(tn->tn_type, v);
3308 }
3309
3310
3311 /*
3312 * Create a constant node for sizeof.
3313 */
3314 tnode_t *
3315 build_sizeof(const type_t *tp)
3316 {
3317 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3318 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
3319 tn->tn_system_dependent = true;
3320 return tn;
3321 }
3322
3323 /*
3324 * Create a constant node for offsetof.
3325 */
3326 tnode_t *
3327 build_offsetof(const type_t *tp, const sym_t *sym)
3328 {
3329 tspec_t t = tp->t_tspec;
3330 if (t != STRUCT && t != UNION)
3331 /* unacceptable operand of '%s' */
3332 error(111, "offsetof");
3333
3334 // XXX: wrong size, no checking for sym fixme
3335 unsigned int offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3336 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
3337 tn->tn_system_dependent = true;
3338 return tn;
3339 }
3340
3341 unsigned int
3342 type_size_in_bits(const type_t *tp)
3343 {
3344 unsigned int elem, elsz;
3345 bool flex;
3346
3347 elem = 1;
3348 flex = false;
3349 while (tp->t_tspec == ARRAY) {
3350 flex = true; /* allow c99 flex arrays [] [0] */
3351 elem *= tp->t_dim;
3352 tp = tp->t_subt;
3353 }
3354 if (elem == 0) {
3355 if (!flex) {
3356 /* cannot take size/alignment of incomplete type */
3357 error(143);
3358 elem = 1;
3359 }
3360 }
3361 switch (tp->t_tspec) {
3362 case FUNC:
3363 /* cannot take size/alignment of function */
3364 error(144);
3365 elsz = 1;
3366 break;
3367 case STRUCT:
3368 case UNION:
3369 if (is_incomplete(tp)) {
3370 /* cannot take size/alignment of incomplete type */
3371 error(143);
3372 elsz = 1;
3373 } else {
3374 elsz = tp->t_str->sou_size_in_bits;
3375 }
3376 break;
3377 case ENUM:
3378 if (is_incomplete(tp)) {
3379 /* cannot take size/alignment of incomplete type */
3380 warning(143);
3381 }
3382 /* FALLTHROUGH */
3383 default:
3384 if (tp->t_bitfield) {
3385 /* cannot take size/alignment of bit-field */
3386 error(145);
3387 }
3388 if (tp->t_tspec == VOID) {
3389 /* cannot take size/alignment of void */
3390 error(146);
3391 elsz = 1;
3392 } else {
3393 elsz = size_in_bits(tp->t_tspec);
3394 lint_assert(elsz > 0);
3395 }
3396 break;
3397 }
3398
3399 return elem * elsz;
3400 }
3401
3402 tnode_t *
3403 build_alignof(const type_t *tp)
3404 {
3405 switch (tp->t_tspec) {
3406 case ARRAY:
3407 break;
3408
3409 case FUNC:
3410 /* cannot take size/alignment of function */
3411 error(144);
3412 return 0;
3413
3414 case STRUCT:
3415 case UNION:
3416 if (is_incomplete(tp)) {
3417 /* cannot take size/alignment of incomplete type */
3418 error(143);
3419 return 0;
3420 }
3421 break;
3422 case ENUM:
3423 break;
3424 default:
3425 if (tp->t_bitfield) {
3426 /* cannot take size/alignment of bit-field */
3427 error(145);
3428 return 0;
3429 }
3430 if (tp->t_tspec == VOID) {
3431 /* cannot take size/alignment of void */
3432 error(146);
3433 return 0;
3434 }
3435 break;
3436 }
3437
3438 return build_integer_constant(SIZEOF_TSPEC,
3439 (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
3440 }
3441
3442 /*
3443 * Type casts.
3444 */
3445 tnode_t *
3446 cast(tnode_t *tn, type_t *tp)
3447 {
3448 tspec_t nt, ot;
3449
3450 if (tn == NULL)
3451 return NULL;
3452
3453 /*
3454 * XXX: checking for tp == NULL is only a quick fix for PR 22119.
3455 * The proper fix needs to be investigated properly.
3456 * See d_pr_22119.c for how to get here.
3457 */
3458 if (tp == NULL)
3459 return NULL;
3460
3461 tn = cconv(tn);
3462
3463 nt = tp->t_tspec;
3464 ot = tn->tn_type->t_tspec;
3465
3466 if (nt == VOID) {
3467 /*
3468 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3469 * But this seems really questionable.
3470 */
3471 } else if (nt == UNION) {
3472 sym_t *m;
3473 struct_or_union *str = tp->t_str;
3474 if (!gflag) {
3475 /* union cast is a GCC extension */
3476 error(328);
3477 return NULL;
3478 }
3479 for (m = str->sou_first_member; m != NULL; m = m->s_next) {
3480 if (eqtype(m->s_type, tn->tn_type,
3481 false, false, NULL)) {
3482 tn = expr_zalloc_tnode();
3483 tn->tn_op = CVT;
3484 tn->tn_type = tp;
3485 tn->tn_cast = true;
3486 tn->tn_right = NULL;
3487 return tn;
3488 }
3489 }
3490 /* type '%s' is not a member of '%s' */
3491 error(329, type_name(tn->tn_type), type_name(tp));
3492 return NULL;
3493 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3494 /* Casting to a struct is an undocumented GCC extension. */
3495 if (!(gflag && nt == STRUCT))
3496 goto invalid_cast;
3497 } else if (ot == STRUCT || ot == UNION) {
3498 goto invalid_cast;
3499 } else if (ot == VOID) {
3500 /* improper cast of void expression */
3501 error(148);
3502 return NULL;
3503 } else if (is_integer(nt) && is_scalar(ot)) {
3504 /* ok */
3505 } else if (is_floating(nt) && is_arithmetic(ot)) {
3506 /* ok */
3507 } else if (nt == PTR && is_integer(ot)) {
3508 /* ok */
3509 } else if (nt == PTR && ot == PTR) {
3510 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3511 if (hflag)
3512 /* cast discards 'const' from type '%s' */
3513 warning(275, type_name(tn->tn_type));
3514 }
3515 } else
3516 goto invalid_cast;
3517
3518 tn = convert(CVT, 0, tp, tn);
3519 tn->tn_cast = true;
3520
3521 return tn;
3522
3523 invalid_cast:
3524 /* invalid cast from '%s' to '%s' */
3525 error(147, type_name(tn->tn_type), type_name(tp));
3526 return NULL;
3527 }
3528
3529 /*
3530 * Create the node for a function argument.
3531 * All necessary conversions and type checks are done in
3532 * build_function_call because build_function_argument has no
3533 * information about expected argument types.
3534 */
3535 tnode_t *
3536 build_function_argument(tnode_t *args, tnode_t *arg)
3537 {
3538 tnode_t *ntn;
3539
3540 /*
3541 * If there was a serious error in the expression for the argument,
3542 * create a dummy argument so the positions of the remaining arguments
3543 * will not change.
3544 */
3545 if (arg == NULL)
3546 arg = build_integer_constant(INT, 0);
3547
3548 ntn = new_tnode(PUSH, arg->tn_type, arg, args);
3549
3550 return ntn;
3551 }
3552
3553 /*
3554 * Create the node for a function call. Also check types of
3555 * function arguments and insert conversions, if necessary.
3556 */
3557 tnode_t *
3558 build_function_call(tnode_t *func, tnode_t *args)
3559 {
3560 tnode_t *ntn;
3561 op_t fcop;
3562
3563 if (func == NULL)
3564 return NULL;
3565
3566 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3567 fcop = CALL;
3568 } else {
3569 fcop = ICALL;
3570 }
3571
3572 check_ctype_function_call(func, args);
3573
3574 /*
3575 * after cconv() func will always be a pointer to a function
3576 * if it is a valid function designator.
3577 */
3578 func = cconv(func);
3579
3580 if (func->tn_type->t_tspec != PTR ||
3581 func->tn_type->t_subt->t_tspec != FUNC) {
3582 /* illegal function (type %s) */
3583 error(149, type_name(func->tn_type));
3584 return NULL;
3585 }
3586
3587 args = check_function_arguments(func->tn_type->t_subt, args);
3588
3589 ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3590
3591 return ntn;
3592 }
3593
3594 /*
3595 * Check types of all function arguments and insert conversions,
3596 * if necessary.
3597 */
3598 static tnode_t *
3599 check_function_arguments(type_t *ftp, tnode_t *args)
3600 {
3601 tnode_t *arg;
3602 sym_t *asym;
3603 tspec_t at;
3604 int narg, npar, n, i;
3605
3606 /* get # of args in the prototype */
3607 npar = 0;
3608 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3609 npar++;
3610
3611 /* get # of args in function call */
3612 narg = 0;
3613 for (arg = args; arg != NULL; arg = arg->tn_right)
3614 narg++;
3615
3616 asym = ftp->t_args;
3617 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3618 /* argument mismatch: %d arg%s passed, %d expected */
3619 error(150, narg, narg > 1 ? "s" : "", npar);
3620 asym = NULL;
3621 }
3622
3623 for (n = 1; n <= narg; n++) {
3624
3625 /*
3626 * The rightmost argument is at the top of the argument
3627 * subtree.
3628 */
3629 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3630 continue;
3631
3632 /* some things which are always not allowed */
3633 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3634 /* void expressions may not be arguments, arg #%d */
3635 error(151, n);
3636 return NULL;
3637 } else if ((at == STRUCT || at == UNION) &&
3638 is_incomplete(arg->tn_left->tn_type)) {
3639 /* argument cannot have unknown size, arg #%d */
3640 error(152, n);
3641 return NULL;
3642 } else if (is_integer(at) &&
3643 arg->tn_left->tn_type->t_is_enum &&
3644 is_incomplete(arg->tn_left->tn_type)) {
3645 /* argument cannot have unknown size, arg #%d */
3646 warning(152, n);
3647 }
3648
3649 /* class conversions (arg in value context) */
3650 arg->tn_left = cconv(arg->tn_left);
3651
3652 if (asym != NULL) {
3653 arg->tn_left = check_prototype_argument(
3654 n, asym->s_type, arg->tn_left);
3655 } else {
3656 arg->tn_left = promote(NOOP, true, arg->tn_left);
3657 }
3658 arg->tn_type = arg->tn_left->tn_type;
3659
3660 if (asym != NULL)
3661 asym = asym->s_next;
3662 }
3663
3664 return args;
3665 }
3666
3667 /*
3668 * Compare the type of an argument with the corresponding type of a
3669 * prototype parameter. If it is a valid combination, but both types
3670 * are not the same, insert a conversion to convert the argument into
3671 * the type of the parameter.
3672 */
3673 static tnode_t *
3674 check_prototype_argument(
3675 int n, /* pos of arg */
3676 type_t *tp, /* expected type (from prototype) */
3677 tnode_t *tn) /* argument */
3678 {
3679 tnode_t *ln;
3680 bool dowarn;
3681
3682 ln = xcalloc(1, sizeof(*ln));
3683 ln->tn_type = expr_unqualified_type(tp);
3684 ln->tn_lvalue = true;
3685 if (typeok(FARG, n, ln, tn)) {
3686 if (!eqtype(tp, tn->tn_type,
3687 true, false, (dowarn = false, &dowarn)) || dowarn)
3688 tn = convert(FARG, n, tp, tn);
3689 }
3690 free(ln);
3691 return tn;
3692 }
3693
3694 /*
3695 * Return the value of an integral constant expression.
3696 * If the expression is not constant or its type is not an integer
3697 * type, an error message is printed.
3698 */
3699 val_t *
3700 constant(tnode_t *tn, bool required)
3701 {
3702 val_t *v;
3703
3704 if (tn != NULL)
3705 tn = cconv(tn);
3706 if (tn != NULL)
3707 tn = promote(NOOP, false, tn);
3708
3709 v = xcalloc(1, sizeof(*v));
3710
3711 if (tn == NULL) {
3712 lint_assert(nerr != 0);
3713 debug_step("constant node is null; returning 1 instead");
3714 v->v_tspec = INT;
3715 v->v_quad = 1;
3716 return v;
3717 }
3718
3719 v->v_tspec = tn->tn_type->t_tspec;
3720
3721 if (tn->tn_op == CON) {
3722 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3723 if (is_integer(tn->tn_val->v_tspec)) {
3724 v->v_unsigned_since_c90 =
3725 tn->tn_val->v_unsigned_since_c90;
3726 v->v_quad = tn->tn_val->v_quad;
3727 return v;
3728 }
3729 v->v_quad = tn->tn_val->v_ldbl;
3730 } else {
3731 v->v_quad = 1;
3732 }
3733
3734 if (required)
3735 /* integral constant expression expected */
3736 error(55);
3737 else
3738 /* variable array dimension is a C99/GCC extension */
3739 c99ism(318);
3740
3741 if (!is_integer(v->v_tspec))
3742 v->v_tspec = INT;
3743
3744 return v;
3745 }
3746
3747 static bool
3748 is_constcond_false(const tnode_t *tn, tspec_t t)
3749 {
3750 return (t == BOOL || t == INT) &&
3751 tn->tn_op == CON && tn->tn_val->v_quad == 0;
3752 }
3753
3754 /*
3755 * Perform some tests on expressions which can't be done in build_binary()
3756 * and functions called by build_binary(). These tests must be done here
3757 * because we need some information about the context in which the operations
3758 * are performed.
3759 * After all tests are performed and dofreeblk is true, expr() frees the
3760 * memory which is used for the expression.
3761 */
3762 void
3763 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool is_do_while)
3764 {
3765
3766 if (tn == NULL) { /* in case of errors */
3767 expr_free_all();
3768 return;
3769 }
3770
3771 /* expr() is also called in global initializations */
3772 if (dcs->d_ctx != EXTERN && !is_do_while)
3773 check_statement_reachable();
3774
3775 check_expr_misc(tn, vctx, tctx, !tctx, false, false, false);
3776 if (tn->tn_op == ASSIGN) {
3777 if (hflag && tctx)
3778 /* assignment in conditional context */
3779 warning(159);
3780 } else if (tn->tn_op == CON) {
3781 if (hflag && tctx && !constcond_flag &&
3782 !tn->tn_system_dependent &&
3783 !(is_do_while &&
3784 is_constcond_false(tn, tn->tn_type->t_tspec)))
3785 /* constant in conditional context */
3786 warning(161);
3787 }
3788 if (!modtab[tn->tn_op].m_has_side_effect) {
3789 /*
3790 * for left operands of COMMA this warning is already
3791 * printed
3792 */
3793 if (tn->tn_op != COMMA && !vctx && !tctx)
3794 check_null_effect(tn);
3795 }
3796 debug_node(tn);
3797
3798 /* free the tree memory */
3799 if (dofreeblk)
3800 expr_free_all();
3801 }
3802
3803 static bool
3804 has_side_effect(const tnode_t *tn) // NOLINT(misc-no-recursion)
3805 {
3806 op_t op = tn->tn_op;
3807
3808 if (modtab[op].m_has_side_effect)
3809 return true;
3810
3811 if (op == CVT && tn->tn_type->t_tspec == VOID)
3812 return has_side_effect(tn->tn_left);
3813
3814 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3815 if (op == LOGAND || op == LOGOR)
3816 return has_side_effect(tn->tn_right);
3817
3818 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3819 if (op == QUEST)
3820 return has_side_effect(tn->tn_right);
3821
3822 if (op == COLON || op == COMMA) {
3823 return has_side_effect(tn->tn_left) ||
3824 has_side_effect(tn->tn_right);
3825 }
3826
3827 return false;
3828 }
3829
3830 static bool
3831 is_void_cast(const tnode_t *tn)
3832 {
3833
3834 return tn->tn_op == CVT && tn->tn_cast &&
3835 tn->tn_type->t_tspec == VOID;
3836 }
3837
3838 static bool
3839 is_local_symbol(const tnode_t *tn)
3840 {
3841
3842 return tn->tn_op == LOAD &&
3843 tn->tn_left->tn_op == NAME &&
3844 tn->tn_left->tn_sym->s_scl == AUTO;
3845 }
3846
3847 static bool
3848 is_int_constant_zero(const tnode_t *tn)
3849 {
3850
3851 return tn->tn_op == CON &&
3852 tn->tn_type->t_tspec == INT &&
3853 tn->tn_val->v_quad == 0;
3854 }
3855
3856 static void
3857 check_null_effect(const tnode_t *tn)
3858 {
3859
3860 if (!hflag)
3861 return;
3862 if (has_side_effect(tn))
3863 return;
3864 if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
3865 return;
3866 if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
3867 return;
3868
3869 /* expression has null effect */
3870 warning(129);
3871 }
3872
3873 /* ARGSUSED */
3874 void
3875 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
3876 bool eqwarn, bool fcall, bool rvdisc, bool szof)
3877 {
3878 tnode_t *ln, *rn;
3879 const mod_t *mp;
3880 op_t op;
3881 scl_t sc;
3882 dinfo_t *di;
3883
3884 if (tn == NULL)
3885 return;
3886
3887 ln = tn->tn_left;
3888 rn = tn->tn_right;
3889 mp = &modtab[op = tn->tn_op];
3890
3891 switch (op) {
3892 case ADDR:
3893 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
3894 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
3895 if (!szof)
3896 mark_as_set(ln->tn_sym);
3897 mark_as_used(ln->tn_sym, fcall, szof);
3898 }
3899 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
3900 /* check the range of array indices */
3901 check_array_index(ln->tn_left, true);
3902 break;
3903 case LOAD:
3904 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
3905 /* check the range of array indices */
3906 check_array_index(ln->tn_left, false);
3907 /* FALLTHROUGH */
3908 case PUSH:
3909 case INCBEF:
3910 case DECBEF:
3911 case INCAFT:
3912 case DECAFT:
3913 case ADDASS:
3914 case SUBASS:
3915 case MULASS:
3916 case DIVASS:
3917 case MODASS:
3918 case ANDASS:
3919 case ORASS:
3920 case XORASS:
3921 case SHLASS:
3922 case SHRASS:
3923 case REAL:
3924 case IMAG:
3925 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
3926 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
3927 sc = ln->tn_sym->s_scl;
3928 /*
3929 * Look if there was a asm statement in one of the
3930 * compound statements we are in. If not, we don't
3931 * print a warning.
3932 */
3933 for (di = dcs; di != NULL; di = di->d_next) {
3934 if (di->d_asm)
3935 break;
3936 }
3937 if (sc != EXTERN && sc != STATIC &&
3938 !ln->tn_sym->s_set && !szof && di == NULL) {
3939 /* %s may be used before set */
3940 warning(158, ln->tn_sym->s_name);
3941 mark_as_set(ln->tn_sym);
3942 }
3943 mark_as_used(ln->tn_sym, false, false);
3944 }
3945 break;
3946 case ASSIGN:
3947 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
3948 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
3949 mark_as_set(ln->tn_sym);
3950 if (ln->tn_sym->s_scl == EXTERN)
3951 outusg(ln->tn_sym);
3952 }
3953 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
3954 /* check the range of array indices */
3955 check_array_index(ln->tn_left, false);
3956 break;
3957 case CALL:
3958 lint_assert(ln->tn_op == ADDR);
3959 lint_assert(ln->tn_left->tn_op == NAME);
3960 if (!szof &&
3961 !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
3962 outcall(tn, vctx || tctx, rvdisc);
3963 break;
3964 case EQ:
3965 if (hflag && eqwarn)
3966 /* operator '==' found where '=' was expected */
3967 warning(160);
3968 break;
3969 case CON:
3970 case NAME:
3971 case STRING:
3972 return;
3973 /* LINTED206: (enumeration values not handled in switch) */
3974 case BITOR:
3975 case BITXOR:
3976 case NE:
3977 case GE:
3978 case GT:
3979 case LE:
3980 case LT:
3981 case SHR:
3982 case SHL:
3983 case MINUS:
3984 case PLUS:
3985 case MOD:
3986 case DIV:
3987 case MULT:
3988 case INDIR:
3989 case UMINUS:
3990 case UPLUS:
3991 case DEC:
3992 case INC:
3993 case COMPL:
3994 case NOT:
3995 case POINT:
3996 case ARROW:
3997 case NOOP:
3998 case BITAND:
3999 case FARG:
4000 case CASE:
4001 case INIT:
4002 case RETURN:
4003 case ICALL:
4004 case CVT:
4005 case COMMA:
4006 case FSEL:
4007 case COLON:
4008 case QUEST:
4009 case LOGOR:
4010 case LOGAND:
4011 break;
4012 }
4013
4014 bool cvctx = mp->m_left_value_context;
4015 bool ctctx = mp->m_left_test_context;
4016 bool eq = mp->m_warn_if_operand_eq &&
4017 !ln->tn_parenthesized &&
4018 rn != NULL && !rn->tn_parenthesized;
4019
4020 /*
4021 * values of operands of ':' are not used if the type of at least
4022 * one of the operands (for gcc compatibility) is void
4023 * XXX test/value context of QUEST should probably be used as
4024 * context for both operands of COLON
4025 */
4026 if (op == COLON && tn->tn_type->t_tspec == VOID)
4027 cvctx = ctctx = false;
4028 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4029 check_expr_misc(ln, cvctx, ctctx, eq, op == CALL, discard, szof);
4030
4031 switch (op) {
4032 case PUSH:
4033 if (rn != NULL)
4034 check_expr_misc(rn, false, false, eq, false, false,
4035 szof);
4036 break;
4037 case LOGAND:
4038 case LOGOR:
4039 check_expr_misc(rn, false, true, eq, false, false, szof);
4040 break;
4041 case COLON:
4042 check_expr_misc(rn, cvctx, ctctx, eq, false, false, szof);
4043 break;
4044 case COMMA:
4045 check_expr_misc(rn, vctx, tctx, eq, false, false, szof);
4046 break;
4047 default:
4048 if (mp->m_binary)
4049 check_expr_misc(rn, true, false, eq, false, false,
4050 szof);
4051 break;
4052 }
4053 }
4054
4055 /*
4056 * Checks the range of array indices, if possible.
4057 * amper is set if only the address of the element is used. This
4058 * means that the index is allowed to refer to the first element
4059 * after the array.
4060 */
4061 static void
4062 check_array_index(tnode_t *tn, bool amper)
4063 {
4064 int dim;
4065 tnode_t *ln, *rn;
4066 int elsz;
4067 int64_t con;
4068
4069 ln = tn->tn_left;
4070 rn = tn->tn_right;
4071
4072 /* We can only check constant indices. */
4073 if (rn->tn_op != CON)
4074 return;
4075
4076 /* Return if the left node does not stem from an array. */
4077 if (ln->tn_op != ADDR)
4078 return;
4079 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4080 return;
4081 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4082 return;
4083
4084 /*
4085 * For incomplete array types, we can print a warning only if
4086 * the index is negative.
4087 */
4088 if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4089 return;
4090
4091 /* Get the size of one array element */
4092 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
4093 return;
4094 elsz /= CHAR_SIZE;
4095
4096 /* Change the unit of the index from bytes to element size. */
4097 if (is_uinteger(rn->tn_type->t_tspec)) {
4098 con = (uint64_t)rn->tn_val->v_quad / elsz;
4099 } else {
4100 con = rn->tn_val->v_quad / elsz;
4101 }
4102
4103 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4104
4105 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4106 /* array subscript cannot be negative: %ld */
4107 warning(167, (long)con);
4108 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4109 /* array subscript cannot be > %d: %ld */
4110 warning(168, dim - 1, (long)con);
4111 }
4112 }
4113
4114 static bool
4115 is_out_of_char_range(const tnode_t *tn)
4116 {
4117 return tn->tn_op == CON &&
4118 !(0 <= tn->tn_val->v_quad &&
4119 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
4120 }
4121
4122 /*
4123 * Check for ordered comparisons of unsigned values with 0.
4124 */
4125 static void
4126 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4127 {
4128 tspec_t lt, rt;
4129
4130 lt = ln->tn_type->t_tspec;
4131 rt = rn->tn_type->t_tspec;
4132
4133 if (ln->tn_op != CON && rn->tn_op != CON)
4134 return;
4135
4136 if (!is_integer(lt) || !is_integer(rt))
4137 return;
4138
4139 if ((hflag || pflag) && ((lt == CHAR && is_out_of_char_range(rn)) ||
4140 (rt == CHAR && is_out_of_char_range(ln)))) {
4141 /* nonportable character comparison, op %s */
4142 warning(230, op_name(op));
4143 return;
4144 }
4145 if (is_uinteger(lt) && !is_uinteger(rt) &&
4146 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4147 if (rn->tn_val->v_quad < 0) {
4148 /* comparison of %s with %s, op %s */
4149 warning(162, type_name(ln->tn_type),
4150 "negative constant", op_name(op));
4151 } else if (op == LT || op == GE || (hflag && op == LE)) {
4152 /* comparison of %s with %s, op %s */
4153 warning(162, type_name(ln->tn_type), "0", op_name(op));
4154 }
4155 return;
4156 }
4157 if (is_uinteger(rt) && !is_uinteger(lt) &&
4158 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4159 if (ln->tn_val->v_quad < 0) {
4160 /* comparison of %s with %s, op %s */
4161 warning(162, "negative constant",
4162 type_name(rn->tn_type), op_name(op));
4163 } else if (op == GT || op == LE || (hflag && op == GE)) {
4164 /* comparison of %s with %s, op %s */
4165 warning(162, "0", type_name(rn->tn_type), op_name(op));
4166 }
4167 return;
4168 }
4169 }
4170
4171 /*
4172 * Return whether the expression can be used for static initialization.
4173 *
4174 * Constant initialization expressions must be constant or an address
4175 * of a static object with an optional offset. In the first case,
4176 * the result is returned in *offsp. In the second case, the static
4177 * object is returned in *symp and the offset in *offsp.
4178 *
4179 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4180 * CON. Type conversions are allowed if they do not change binary
4181 * representation (including width).
4182 *
4183 * C99 6.6 "Constant expressions"
4184 * C99 6.7.8p4 restricts initializers for static storage duration
4185 */
4186 bool
4187 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4188 {
4189 const sym_t *sym;
4190 ptrdiff_t offs1, offs2;
4191 tspec_t t, ot;
4192
4193 switch (tn->tn_op) {
4194 case MINUS:
4195 if (tn->tn_right->tn_op == CVT)
4196 return constant_addr(tn->tn_right, symp, offsp);
4197 else if (tn->tn_right->tn_op != CON)
4198 return false;
4199 /* FALLTHROUGH */
4200 case PLUS:
4201 offs1 = offs2 = 0;
4202 if (tn->tn_left->tn_op == CON) {
4203 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4204 if (!constant_addr(tn->tn_right, &sym, &offs2))
4205 return false;
4206 } else if (tn->tn_right->tn_op == CON) {
4207 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4208 if (tn->tn_op == MINUS)
4209 offs2 = -offs2;
4210 if (!constant_addr(tn->tn_left, &sym, &offs1))
4211 return false;
4212 } else {
4213 return false;
4214 }
4215 *symp = sym;
4216 *offsp = offs1 + offs2;
4217 return true;
4218 case ADDR:
4219 if (tn->tn_left->tn_op == NAME) {
4220 *symp = tn->tn_left->tn_sym;
4221 *offsp = 0;
4222 return true;
4223 } else {
4224 /*
4225 * If this would be the front end of a compiler we
4226 * would return a label instead of 0, at least if
4227 * 'tn->tn_left->tn_op == STRING'.
4228 */
4229 *symp = NULL;
4230 *offsp = 0;
4231 return true;
4232 }
4233 case CVT:
4234 t = tn->tn_type->t_tspec;
4235 ot = tn->tn_left->tn_type->t_tspec;
4236 if ((!is_integer(t) && t != PTR) ||
4237 (!is_integer(ot) && ot != PTR)) {
4238 return false;
4239 }
4240 #ifdef notdef
4241 /*
4242 * consider:
4243 * struct foo {
4244 * unsigned char a;
4245 * } f = {
4246 * (unsigned char)(unsigned long)
4247 * (&(((struct foo *)0)->a))
4248 * };
4249 * since psize(unsigned long) != psize(unsigned char),
4250 * this fails.
4251 */
4252 else if (psize(t) != psize(ot))
4253 return -1;
4254 #endif
4255 return constant_addr(tn->tn_left, symp, offsp);
4256 default:
4257 return false;
4258 }
4259 }
4260
4261 /* Append s2 to s1, then free s2. */
4262 strg_t *
4263 cat_strings(strg_t *s1, strg_t *s2)
4264 {
4265 size_t len1, len2, sz;
4266
4267 if (s1->st_tspec != s2->st_tspec) {
4268 /* cannot concatenate wide and regular string literals */
4269 error(292);
4270 return s1;
4271 }
4272
4273 len1 = s1->st_len;
4274 len2 = s2->st_len;
4275
4276 if (s1->st_tspec == CHAR) {
4277 sz = sizeof(*s1->st_cp);
4278 s1->st_cp = xrealloc(s1->st_cp, (len1 + len2 + 1) * sz);
4279 memcpy(s1->st_cp + len1, s2->st_cp, (len2 + 1) * sz);
4280 free(s2->st_cp);
4281 } else {
4282 sz = sizeof(*s1->st_wcp);
4283 s1->st_wcp = xrealloc(s1->st_wcp, (len1 + len2 + 1) * sz);
4284 memcpy(s1->st_wcp + len1, s2->st_wcp, (len2 + 1) * sz);
4285 free(s2->st_wcp);
4286 }
4287
4288 s1->st_len = len1 + len2;
4289 free(s2);
4290
4291 return s1;
4292 }
4293
4294 static bool
4295 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4296 {
4297
4298 if (op == SHL || op == SHR) {
4299 if (!lparen && (lop == PLUS || lop == MINUS))
4300 return true;
4301 if (!rparen && (rop == PLUS || rop == MINUS))
4302 return true;
4303 return false;
4304 }
4305
4306 if (op == LOGOR) {
4307 if (!lparen && lop == LOGAND)
4308 return true;
4309 if (!rparen && rop == LOGAND)
4310 return true;
4311 return false;
4312 }
4313
4314 lint_assert(op == BITAND || op == BITXOR || op == BITOR);
4315 if (!lparen && lop != op) {
4316 if (lop == PLUS || lop == MINUS)
4317 return true;
4318 if (lop == BITAND || lop == BITXOR)
4319 return true;
4320 }
4321 if (!rparen && rop != op) {
4322 if (rop == PLUS || rop == MINUS)
4323 return true;
4324 if (rop == BITAND || rop == BITXOR)
4325 return true;
4326 }
4327 return false;
4328 }
4329
4330 /*
4331 * Print a warning if the given node has operands which should be
4332 * parenthesized.
4333 *
4334 * XXX Does not work if an operand is a constant expression. Constant
4335 * expressions are already folded.
4336 */
4337 static void
4338 check_precedence_confusion(tnode_t *tn)
4339 {
4340 tnode_t *ln, *rn;
4341
4342 if (!hflag)
4343 return;
4344
4345 debug_node(tn);
4346
4347 lint_assert(is_binary(tn));
4348 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4349 continue;
4350 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4351 continue;
4352
4353 if (is_confusing_precedence(tn->tn_op,
4354 ln->tn_op, ln->tn_parenthesized,
4355 rn->tn_op, rn->tn_parenthesized)) {
4356 /* precedence confusion possible: parenthesize! */
4357 warning(169);
4358 }
4359 }
4360