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