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