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