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