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