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