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