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