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