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