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