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