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