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