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