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