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