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