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