tree.c revision 1.422 1 /* $NetBSD: tree.c,v 1.422 2022/04/09 15:43:41 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.422 2022/04/09 15:43:41 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_test(tnode_t *);
89 static tnode_t *fold_float(tnode_t *);
90 static tnode_t *check_function_arguments(type_t *, tnode_t *);
91 static tnode_t *check_prototype_argument(int, type_t *, tnode_t *);
92 static void check_null_effect(const tnode_t *);
93 static void check_array_index(tnode_t *, bool);
94 static void check_integer_comparison(op_t, tnode_t *, tnode_t *);
95 static void check_precedence_confusion(tnode_t *);
96
97 extern sig_atomic_t fpe;
98
99 static const char *
100 op_name(op_t op)
101 {
102 return modtab[op].m_name;
103 }
104
105 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
106 type_t *
107 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 (!Sflag)
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 (gflag) {
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 (gflag && is_gcc_bool_builtin(sym->s_name))
249 sym->s_type = gettyp(BOOL);
250
251 } else if (Sflag) {
252 /* function '%s' implicitly declared to return int */
253 error(215, sym->s_name);
254 } else if (sflag) {
255 /* function '%s' implicitly declared to return int */
256 warning(215, sym->s_name);
257 }
258
259 /* XXX if tflag is set, the symbol should be exported to level 0 */
260 sym->s_type = 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 (sym->s_scl != MOS && sym->s_scl != MOU)
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 && tflag) {
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 (tflag) {
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 (tflag && 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 (tflag) {
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 null.
538 */
539 if (mp->m_left_value_context || mp->m_left_test_context)
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. Also types are 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 /*
559 * Promote the left operand if it is in a test or value context
560 */
561 if (mp->m_left_value_context || mp->m_left_test_context)
562 ln = promote(op, false, ln);
563 /*
564 * Promote the right operand, but only if it is no struct or
565 * union member, or if it is not to be assigned to the left operand
566 */
567 if (mp->m_binary && op != ARROW && op != POINT &&
568 op != ASSIGN && op != RETURN && op != INIT) {
569 rn = promote(op, false, rn);
570 }
571
572 /*
573 * If the result of the operation is different for signed or
574 * unsigned operands and one of the operands is signed only in
575 * ANSI C, print a warning.
576 */
577 if (mp->m_warn_if_left_unsigned_in_c90 &&
578 ln->tn_op == CON && ln->tn_val->v_unsigned_since_c90) {
579 /* ANSI C treats constant as unsigned, op %s */
580 warning(218, mp->m_name);
581 ln->tn_val->v_unsigned_since_c90 = false;
582 }
583 if (mp->m_warn_if_right_unsigned_in_c90 &&
584 rn->tn_op == CON && rn->tn_val->v_unsigned_since_c90) {
585 /* ANSI C treats constant as unsigned, op %s */
586 warning(218, mp->m_name);
587 rn->tn_val->v_unsigned_since_c90 = false;
588 }
589
590 /* Make sure both operands are of the same type */
591 if (mp->m_balance_operands || (tflag && (op == SHL || op == SHR)))
592 balance(op, &ln, &rn);
593
594 /*
595 * Check types for compatibility with the operation and mutual
596 * compatibility. Return if there are serious problems.
597 */
598 if (!typeok(op, 0, ln, rn))
599 return NULL;
600
601 /* And now create the node. */
602 switch (op) {
603 case POINT:
604 case ARROW:
605 ntn = build_struct_access(op, sys, ln, rn);
606 break;
607 case INCAFT:
608 case DECAFT:
609 case INCBEF:
610 case DECBEF:
611 ntn = build_prepost_incdec(op, sys, ln);
612 break;
613 case ADDR:
614 ntn = build_address(sys, ln, false);
615 break;
616 case INDIR:
617 ntn = new_tnode(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
618 break;
619 case PLUS:
620 case MINUS:
621 ntn = build_plus_minus(op, sys, ln, rn);
622 break;
623 case SHL:
624 case SHR:
625 ntn = build_bit_shift(op, sys, ln, rn);
626 break;
627 case COLON:
628 ntn = build_colon(sys, ln, rn);
629 break;
630 case ASSIGN:
631 case MULASS:
632 case DIVASS:
633 case MODASS:
634 case ADDASS:
635 case SUBASS:
636 case SHLASS:
637 case SHRASS:
638 case ANDASS:
639 case XORASS:
640 case ORASS:
641 case RETURN:
642 case INIT:
643 ntn = build_assignment(op, sys, ln, rn);
644 break;
645 case COMMA:
646 case QUEST:
647 ntn = new_tnode(op, sys, rn->tn_type, ln, rn);
648 break;
649 case REAL:
650 case IMAG:
651 ntn = build_real_imag(op, sys, ln);
652 break;
653 default:
654 rettp = mp->m_returns_bool
655 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
656 lint_assert(mp->m_binary || rn == NULL);
657 ntn = new_tnode(op, sys, rettp, ln, rn);
658 break;
659 }
660
661 /* Return if an error occurred. */
662 if (ntn == NULL)
663 return NULL;
664
665 /* Print a warning if precedence confusion is possible */
666 if (mp->m_possible_precedence_confusion)
667 check_precedence_confusion(ntn);
668
669 /*
670 * Print a warning if one of the operands is in a context where
671 * it is compared with null and if this operand is a constant.
672 */
673 if (mp->m_left_test_context) {
674 if (ln->tn_op == CON ||
675 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
676 if (hflag && !constcond_flag &&
677 !ln->tn_system_dependent)
678 /* constant in conditional context */
679 warning(161);
680 }
681 }
682
683 /* Fold if the operator requires it */
684 if (mp->m_fold_constant_operands) {
685 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
686 if (mp->m_left_test_context) {
687 ntn = fold_test(ntn);
688 } else if (is_floating(ntn->tn_type->t_tspec)) {
689 ntn = fold_float(ntn);
690 } else {
691 ntn = fold(ntn);
692 }
693 } else if (op == QUEST && ln->tn_op == CON) {
694 ntn = ln->tn_val->v_quad != 0
695 ? rn->tn_left : rn->tn_right;
696 }
697 }
698
699 return ntn;
700 }
701
702 tnode_t *
703 build_unary(op_t op, bool sys, tnode_t *tn)
704 {
705 return build_binary(tn, op, sys, NULL);
706 }
707
708 tnode_t *
709 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
710 {
711 sym_t *msym;
712
713 if (ln == NULL)
714 return NULL;
715
716 if (op == ARROW) {
717 /* must do this before struct_or_union_member is called */
718 ln = cconv(ln);
719 }
720 msym = struct_or_union_member(ln, op, getsym(member));
721 return build_binary(ln, op, sys, build_name(msym, false));
722 }
723
724 /*
725 * Perform class conversions.
726 *
727 * Arrays of type T are converted into pointers to type T.
728 * Functions are converted to pointers to functions.
729 * Lvalues are converted to rvalues.
730 *
731 * C99 6.3 "Conversions"
732 * C99 6.3.2 "Other operands"
733 * C99 6.3.2.1 "Lvalues, arrays, and function designators"
734 */
735 tnode_t *
736 cconv(tnode_t *tn)
737 {
738 type_t *tp;
739
740 /*
741 * Array-lvalue (array of type T) is converted into rvalue
742 * (pointer to type T)
743 */
744 if (tn->tn_type->t_tspec == ARRAY) {
745 if (!tn->tn_lvalue) {
746 /* XXX print correct operator */
747 /* %soperand of '%s' must be lvalue */
748 gnuism(114, "", op_name(ADDR));
749 }
750 tn = new_tnode(ADDR, tn->tn_sys,
751 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
752 }
753
754 /*
755 * Expression of type function (function with return value of type T)
756 * in rvalue-expression (pointer to function with return value
757 * of type T)
758 */
759 if (tn->tn_type->t_tspec == FUNC)
760 tn = build_address(tn->tn_sys, tn, true);
761
762 /* lvalue to rvalue */
763 if (tn->tn_lvalue) {
764 tp = expr_dup_type(tn->tn_type);
765 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
766 tp->t_const = tp->t_volatile = false;
767 tn = new_tnode(LOAD, tn->tn_sys, tp, tn, NULL);
768 }
769
770 return tn;
771 }
772
773 const tnode_t *
774 before_conversion(const tnode_t *tn)
775 {
776 while (tn->tn_op == CVT && !tn->tn_cast)
777 tn = tn->tn_left;
778 return tn;
779 }
780
781 static bool
782 is_null_pointer(const tnode_t *tn)
783 {
784 tspec_t t = tn->tn_type->t_tspec;
785
786 return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID) ||
787 is_integer(t))
788 && (tn->tn_op == CON && tn->tn_val->v_quad == 0);
789 }
790
791 /*
792 * Most errors required by ANSI C are reported in struct_or_union_member().
793 * Here we only check for totally wrong things.
794 */
795 static bool
796 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
797 {
798 if (lt == FUNC || lt == VOID || ltp->t_bitfield ||
799 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
800 /* Without tflag we got already an error */
801 if (tflag)
802 /* unacceptable operand of '%s' */
803 error(111, op_name(POINT));
804 return false;
805 }
806 return true;
807 }
808
809 static bool
810 typeok_arrow(tspec_t lt)
811 {
812 if (lt == PTR || (tflag && is_integer(lt)))
813 return true;
814
815 /* Without tflag we got already an error */
816 if (tflag)
817 /* unacceptable operand of '%s' */
818 error(111, op_name(ARROW));
819 return false;
820 }
821
822 static bool
823 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
824 {
825 /* operand has scalar type (checked in typeok) */
826 if (!tn->tn_lvalue) {
827 if (tn->tn_op == CVT && tn->tn_cast &&
828 tn->tn_left->tn_op == LOAD) {
829 /* a cast does not yield an lvalue */
830 error(163);
831 }
832 /* %soperand of '%s' must be lvalue */
833 error(114, "", op_name(op));
834 return false;
835 } else if (tp->t_const) {
836 if (!tflag)
837 /* %soperand of '%s' must be modifiable lvalue */
838 warning(115, "", op_name(op));
839 }
840 return true;
841 }
842
843 static bool
844 typeok_address(const mod_t *mp,
845 const tnode_t *tn, const type_t *tp, tspec_t t)
846 {
847 if (t == ARRAY || t == FUNC) {
848 /* ok, a warning comes later (in build_address()) */
849 } else if (!tn->tn_lvalue) {
850 if (tn->tn_op == CVT && tn->tn_cast &&
851 tn->tn_left->tn_op == LOAD) {
852 /* a cast does not yield an lvalue */
853 error(163);
854 }
855 /* %soperand of '%s' must be lvalue */
856 error(114, "", mp->m_name);
857 return false;
858 } else if (is_scalar(t)) {
859 if (tp->t_bitfield) {
860 /* cannot take address of bit-field */
861 error(112);
862 return false;
863 }
864 } else if (t != STRUCT && t != UNION) {
865 /* unacceptable operand of '%s' */
866 error(111, mp->m_name);
867 return false;
868 }
869 if (tn->tn_op == NAME && tn->tn_sym->s_register) {
870 /* cannot take address of register %s */
871 error(113, tn->tn_sym->s_name);
872 return false;
873 }
874 return true;
875 }
876
877 static bool
878 typeok_indir(tspec_t t)
879 {
880 /* until now there were no type checks for this operator */
881 if (t != PTR) {
882 /* cannot dereference non-pointer type */
883 error(96);
884 return false;
885 }
886 return true;
887 }
888
889 static bool
890 typeok_plus(op_t op,
891 const type_t *ltp, tspec_t lt,
892 const type_t *rtp, tspec_t rt)
893 {
894 /* operands have scalar types (checked above) */
895 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
896 warn_incompatible_types(op, ltp, lt, rtp, rt);
897 return false;
898 }
899 return true;
900 }
901
902 static bool
903 typeok_minus(op_t op,
904 const type_t *ltp, tspec_t lt,
905 const type_t *rtp, tspec_t rt)
906 {
907 /* operands have scalar types (checked above) */
908 if (lt == PTR && (!is_integer(rt) && rt != PTR)) {
909 warn_incompatible_types(op, ltp, lt, rtp, rt);
910 return false;
911 } else if (rt == PTR && lt != PTR) {
912 warn_incompatible_types(op, ltp, lt, rtp, rt);
913 return false;
914 }
915 if (lt == PTR && rt == PTR) {
916 if (!eqtype(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
917 /* illegal pointer subtraction */
918 error(116);
919 }
920 }
921 return true;
922 }
923
924 static void
925 typeok_shr(const mod_t *mp,
926 const tnode_t *ln, tspec_t lt,
927 const tnode_t *rn, tspec_t rt)
928 {
929 tspec_t olt, ort;
930
931 olt = before_conversion(ln)->tn_type->t_tspec;
932 ort = before_conversion(rn)->tn_type->t_tspec;
933
934 /* operands have integer types (checked above) */
935 if (pflag && !is_uinteger(olt)) {
936 /*
937 * The left operand is signed. This means that
938 * the operation is (possibly) nonportable.
939 */
940 if (ln->tn_op != CON) {
941 /* bitwise '%s' on signed value possibly nonportable */
942 warning(117, mp->m_name);
943 } else if (ln->tn_val->v_quad < 0) {
944 /* bitwise '%s' on signed value nonportable */
945 warning(120, mp->m_name);
946 }
947 } else if (!tflag && !sflag && !is_uinteger(olt) && is_uinteger(ort)) {
948 /*
949 * The left operand would become unsigned in
950 * traditional C.
951 */
952 if (hflag && !Sflag &&
953 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
954 /* semantics of '%s' change in ANSI C; use ... */
955 warning(118, mp->m_name);
956 }
957 } else if (!tflag && !sflag && !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 with 1, and then shifted.
962 */
963 if (hflag && !Sflag &&
964 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
965 /* semantics of '%s' change in ANSI C; use ... */
966 warning(118, mp->m_name);
967 }
968 }
969 }
970
971 static void
972 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt)
973 {
974 /*
975 * C90 does not perform balancing for shift operations,
976 * but traditional C does. If the width of the right operand
977 * is greater than the width of the left operand, then in
978 * traditional C the left operand would be extended to the
979 * width of the right operand. For SHL this may result in
980 * different results.
981 */
982 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
983 /*
984 * XXX If both operands are constant, make sure
985 * that there is really a difference between
986 * ANSI C and traditional C.
987 */
988 if (hflag && !Sflag)
989 /* semantics of '%s' change in ANSI C; use ... */
990 warning(118, mp->m_name);
991 }
992 }
993
994 static void
995 typeok_shift(tspec_t lt, const tnode_t *rn, tspec_t rt)
996 {
997 if (rn->tn_op != CON)
998 return;
999
1000 if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) {
1001 /* negative shift */
1002 warning(121);
1003 } else if ((uint64_t)rn->tn_val->v_quad ==
1004 (uint64_t)size_in_bits(lt)) {
1005 /* shift equal to size of object */
1006 warning(267);
1007 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size_in_bits(lt)) {
1008 /* shift amount %llu is greater than bit-size %llu of '%s' */
1009 warning(122, (unsigned long long)rn->tn_val->v_quad,
1010 (unsigned long long)size_in_bits(lt),
1011 tspec_name(lt));
1012 }
1013 }
1014
1015 static bool
1016 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
1017 {
1018 if (lt == PTR && is_null_pointer(rn))
1019 return true;
1020 if (rt == PTR && is_null_pointer(ln))
1021 return true;
1022 return false;
1023 }
1024
1025 static bool
1026 typeok_compare(op_t op,
1027 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1028 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1029 {
1030 const char *lx, *rx;
1031
1032 if (lt == PTR && rt == PTR) {
1033 check_pointer_comparison(op, ln, rn);
1034 return true;
1035 }
1036
1037 if (lt != PTR && rt != PTR)
1038 return true;
1039
1040 if (!is_integer(lt) && !is_integer(rt)) {
1041 warn_incompatible_types(op, ltp, lt, rtp, rt);
1042 return false;
1043 }
1044
1045 lx = lt == PTR ? "pointer" : "integer";
1046 rx = rt == PTR ? "pointer" : "integer";
1047 /* illegal combination of %s '%s' and %s '%s', op '%s' */
1048 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
1049 return true;
1050 }
1051
1052 static bool
1053 typeok_quest(tspec_t lt, const tnode_t *rn)
1054 {
1055 if (!is_scalar(lt)) {
1056 /* first operand must have scalar type, op ? : */
1057 error(170);
1058 return false;
1059 }
1060 lint_assert(before_conversion(rn)->tn_op == COLON);
1061 return true;
1062 }
1063
1064 static void
1065 typeok_colon_pointer(const mod_t *mp, const type_t *ltp, const type_t *rtp)
1066 {
1067 type_t *lstp = ltp->t_subt;
1068 type_t *rstp = rtp->t_subt;
1069 tspec_t lst = lstp->t_tspec;
1070 tspec_t rst = rstp->t_tspec;
1071
1072 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
1073 /* (void *)0 handled above */
1074 if (sflag)
1075 /* ANSI C forbids conversion of %s to %s, op %s */
1076 warning(305, "function pointer", "'void *'",
1077 mp->m_name);
1078 return;
1079 }
1080
1081 if (eqptrtype(lstp, rstp, true))
1082 return;
1083 if (!eqtype(lstp, rstp, true, false, NULL))
1084 warn_incompatible_pointers(mp, ltp, rtp);
1085 }
1086
1087 static bool
1088 typeok_colon(const mod_t *mp,
1089 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1090 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1091 {
1092
1093 if (is_arithmetic(lt) && is_arithmetic(rt))
1094 return true;
1095 if (lt == BOOL && rt == BOOL)
1096 return true;
1097
1098 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1099 return true;
1100 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1101 return true;
1102
1103 if (lt == PTR && is_null_pointer(rn))
1104 return true;
1105 if (rt == PTR && is_null_pointer(ln))
1106 return true;
1107
1108 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
1109 const char *lx = lt == PTR ? "pointer" : "integer";
1110 const char *rx = rt == PTR ? "pointer" : "integer";
1111 /* illegal combination of %s '%s' and %s '%s', op '%s' */
1112 warning(123, lx, type_name(ltp),
1113 rx, type_name(rtp), mp->m_name);
1114 return true;
1115 }
1116
1117 if (lt == VOID || rt == VOID) {
1118 if (lt != VOID || rt != VOID)
1119 /* incompatible types '%s' and '%s' in conditional */
1120 warning(126, type_name(ltp), type_name(rtp));
1121 return true;
1122 }
1123
1124 if (lt == PTR && rt == PTR) {
1125 typeok_colon_pointer(mp, ltp, rtp);
1126 return true;
1127 }
1128
1129 /* incompatible types '%s' and '%s' in conditional */
1130 error(126, type_name(ltp), type_name(rtp));
1131 return false;
1132 }
1133
1134 static bool
1135 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
1136 {
1137 if (op == RETURN || op == INIT || op == FARG)
1138 return true;
1139
1140 if (!ln->tn_lvalue) {
1141 if (ln->tn_op == CVT && ln->tn_cast &&
1142 ln->tn_left->tn_op == LOAD) {
1143 /* a cast does not yield an lvalue */
1144 error(163);
1145 }
1146 /* %soperand of '%s' must be lvalue */
1147 error(114, "left ", op_name(op));
1148 return false;
1149 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1150 has_constant_member(ltp))) {
1151 if (!tflag)
1152 /* %soperand of '%s' must be modifiable lvalue */
1153 warning(115, "left ", op_name(op));
1154 }
1155 return true;
1156 }
1157
1158 /* Check the types using the information from modtab[]. */
1159 static bool
1160 typeok_scalar(op_t op, const mod_t *mp,
1161 const type_t *ltp, tspec_t lt,
1162 const type_t *rtp, tspec_t rt)
1163 {
1164 if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
1165 return true;
1166 if (mp->m_requires_integer) {
1167 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
1168 warn_incompatible_types(op, ltp, lt, rtp, rt);
1169 return false;
1170 }
1171 } else if (mp->m_requires_integer_or_complex) {
1172 if ((!is_integer(lt) && !is_complex(lt)) ||
1173 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
1174 warn_incompatible_types(op, ltp, lt, rtp, rt);
1175 return false;
1176 }
1177 } else if (mp->m_requires_scalar) {
1178 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
1179 warn_incompatible_types(op, ltp, lt, rtp, rt);
1180 return false;
1181 }
1182 } else if (mp->m_requires_arith) {
1183 if (!is_arithmetic(lt) ||
1184 (mp->m_binary && !is_arithmetic(rt))) {
1185 warn_incompatible_types(op, ltp, lt, rtp, rt);
1186 return false;
1187 }
1188 }
1189 return true;
1190 }
1191
1192 /*
1193 * Check the types for specific operators and type combinations.
1194 *
1195 * At this point, the operands already conform to the type requirements of
1196 * the operator, such as being integer, floating or scalar.
1197 */
1198 static bool
1199 typeok_op(op_t op, const mod_t *mp, int arg,
1200 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1201 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1202 {
1203 switch (op) {
1204 case ARROW:
1205 return typeok_arrow(lt);
1206 case POINT:
1207 return typeok_point(ln, ltp, lt);
1208 case INCBEF:
1209 case DECBEF:
1210 case INCAFT:
1211 case DECAFT:
1212 return typeok_incdec(op, ln, ltp);
1213 case INDIR:
1214 return typeok_indir(lt);
1215 case ADDR:
1216 return typeok_address(mp, ln, ltp, lt);
1217 case PLUS:
1218 return typeok_plus(op, ltp, lt, rtp, rt);
1219 case MINUS:
1220 return typeok_minus(op, ltp, lt, rtp, rt);
1221 case SHL:
1222 typeok_shl(mp, lt, rt);
1223 goto shift;
1224 case SHR:
1225 typeok_shr(mp, ln, lt, rn, rt);
1226 shift:
1227 typeok_shift(lt, rn, rt);
1228 break;
1229 case LT:
1230 case LE:
1231 case GT:
1232 case GE:
1233 compare:
1234 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
1235 case EQ:
1236 case NE:
1237 if (is_typeok_eq(ln, lt, rn, rt))
1238 break;
1239 goto compare;
1240 case QUEST:
1241 return typeok_quest(lt, rn);
1242 case COLON:
1243 return typeok_colon(mp, ln, ltp, lt, rn, rtp, rt);
1244 case ASSIGN:
1245 case INIT:
1246 case FARG:
1247 case RETURN:
1248 if (!check_assign_types_compatible(op, arg, ln, rn))
1249 return false;
1250 goto assign;
1251 case MULASS:
1252 case DIVASS:
1253 case MODASS:
1254 goto assign;
1255 case ADDASS:
1256 case SUBASS:
1257 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
1258 warn_incompatible_types(op, ltp, lt, rtp, rt);
1259 return false;
1260 }
1261 goto assign;
1262 case SHLASS:
1263 goto assign;
1264 case SHRASS:
1265 if (pflag && !is_uinteger(lt) && !(tflag && 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 (!tflag &&
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 (!tflag)
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 tflag is set 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 (!tflag) {
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 (!tflag) {
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 (!tflag && !sflag && op == FARG)
2081 check_prototype_conversion(arg, nt, ot, tp, tn);
2082 if (is_integer(nt) && is_integer(ot)) {
2083 check_integer_conversion(op, arg, nt, ot, tp, tn);
2084 } else if (nt == PTR && is_null_pointer(tn)) {
2085 /* a null pointer may be assigned to any pointer. */
2086 } else if (is_integer(nt) && nt != BOOL && ot == PTR) {
2087 check_pointer_integer_conversion(op, nt, tp, tn);
2088 } else if (nt == PTR && ot == PTR && op == CVT) {
2089 check_pointer_conversion(tn, tp);
2090 }
2091
2092 ntn = expr_alloc_tnode();
2093 ntn->tn_op = CVT;
2094 ntn->tn_type = tp;
2095 ntn->tn_cast = op == CVT;
2096 ntn->tn_sys |= tn->tn_sys;
2097 ntn->tn_right = NULL;
2098 if (tn->tn_op != CON || nt == VOID) {
2099 ntn->tn_left = tn;
2100 } else {
2101 ntn->tn_op = CON;
2102 ntn->tn_val = expr_zero_alloc(sizeof(*ntn->tn_val));
2103 convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
2104 tn->tn_val);
2105 }
2106
2107 return ntn;
2108 }
2109
2110 static bool
2111 should_warn_about_prototype_conversion(tspec_t nt,
2112 tspec_t ot, const tnode_t *ptn)
2113 {
2114
2115 if (nt == ot)
2116 return false;
2117
2118 if (nt == ENUM && ot == INT)
2119 return false;
2120
2121 if (is_floating(nt) != is_floating(ot) ||
2122 portable_size_in_bits(nt) != portable_size_in_bits(ot)) {
2123 /* representation and/or width change */
2124 if (!is_integer(ot))
2125 return true;
2126 /*
2127 * XXX: Investigate whether this rule makes sense; see
2128 * tests/usr.bin/xlint/lint1/platform_long.c.
2129 */
2130 return portable_size_in_bits(ot) > portable_size_in_bits(INT);
2131 }
2132
2133 if (!hflag)
2134 return false;
2135
2136 /*
2137 * If the types differ only in sign and the argument has the same
2138 * representation in both types, print no warning.
2139 */
2140 if (ptn->tn_op == CON && is_integer(nt) &&
2141 signed_type(nt) == signed_type(ot) &&
2142 !msb(ptn->tn_val->v_quad, ot))
2143 return false;
2144
2145 return true;
2146 }
2147
2148 /*
2149 * Warn if a prototype causes a type conversion that is different from what
2150 * would happen to the same argument in the absence of a prototype. This
2151 * check is intended for code that needs to stay compatible with pre-C90 C.
2152 *
2153 * Errors/warnings about illegal type combinations are already printed
2154 * in check_assign_types_compatible().
2155 */
2156 static void
2157 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
2158 tnode_t *tn)
2159 {
2160 tnode_t *ptn;
2161
2162 if (!is_arithmetic(nt) || !is_arithmetic(ot))
2163 return;
2164
2165 /*
2166 * If the type of the formal parameter is char/short, a warning
2167 * would be useless, because functions declared the old style
2168 * can't expect char/short arguments.
2169 */
2170 if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
2171 nt == SHORT || nt == USHORT)
2172 return;
2173
2174 /* apply the default promotion */
2175 ptn = promote(NOOP, true, tn);
2176 ot = ptn->tn_type->t_tspec;
2177
2178 if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
2179 /* argument #%d is converted from '%s' to '%s' ... */
2180 warning(259, arg, type_name(tn->tn_type), type_name(tp));
2181 }
2182 }
2183
2184 /*
2185 * Print warnings for conversions of integer types which may cause problems.
2186 */
2187 static void
2188 check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
2189 tnode_t *tn)
2190 {
2191
2192 if (tn->tn_op == CON)
2193 return;
2194
2195 if (op == CVT)
2196 return;
2197
2198 if (Sflag && nt == BOOL)
2199 return; /* See C99 6.3.1.2 */
2200
2201 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
2202 is_uinteger(nt) != is_uinteger(ot)) {
2203 if (aflag > 0 && pflag) {
2204 if (op == FARG) {
2205 /* conversion to '%s' may sign-extend ... */
2206 warning(297, type_name(tp), arg);
2207 } else {
2208 /* conversion to '%s' may sign-extend ... */
2209 warning(131, type_name(tp));
2210 }
2211 }
2212 }
2213
2214 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot)) {
2215 switch (tn->tn_op) {
2216 case PLUS:
2217 case MINUS:
2218 case MULT:
2219 case SHL:
2220 /* suggest cast from '%s' to '%s' on op %s to ... */
2221 warning(324, type_name(gettyp(ot)), type_name(tp),
2222 op_name(tn->tn_op));
2223 break;
2224 default:
2225 break;
2226 }
2227 }
2228
2229 if (portable_size_in_bits(nt) < portable_size_in_bits(ot) &&
2230 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
2231 aflag > 1)) {
2232 /* conversion from '%s' may lose accuracy */
2233 if (aflag > 0) {
2234 if (op == FARG) {
2235 /* conversion from '%s' to '%s' may ... */
2236 warning(298,
2237 type_name(tn->tn_type), type_name(tp), arg);
2238 } else {
2239 /* conversion from '%s' to '%s' may ... */
2240 warning(132,
2241 type_name(tn->tn_type), type_name(tp));
2242 }
2243 }
2244 }
2245 }
2246
2247 /*
2248 * Print warnings for dubious conversions of pointer to integer.
2249 */
2250 static void
2251 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
2252 {
2253
2254 if (tn->tn_op == CON)
2255 return;
2256 if (op != CVT)
2257 return; /* We got already an error. */
2258 if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR))
2259 return;
2260
2261 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
2262 /* conversion of pointer to '%s' may lose bits */
2263 warning(134, type_name(tp));
2264 } else {
2265 /* conversion of pointer to '%s' loses bits */
2266 warning(133, type_name(tp));
2267 }
2268 }
2269
2270 static bool
2271 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
2272 const type_t *ostp, tspec_t ost)
2273 {
2274 /*
2275 * Casting a pointer to 'struct S' to a pointer to another struct that
2276 * has 'struct S' as its first member is ok, see msg_247.c, 'struct
2277 * counter'.
2278 */
2279 if (nst == STRUCT && ost == STRUCT &&
2280 nstp->t_str->sou_first_member != NULL &&
2281 nstp->t_str->sou_first_member->s_type == ostp)
2282 return false;
2283
2284 if (is_incomplete(nstp) || is_incomplete(ostp))
2285 return false;
2286
2287 if ((nst == STRUCT || nst == UNION) && nstp->t_str != ostp->t_str)
2288 return true;
2289
2290 if (nst == CHAR || nst == UCHAR)
2291 return false; /* for the sake of traditional C code */
2292 if (ost == CHAR || ost == UCHAR)
2293 return false; /* for the sake of traditional C code */
2294
2295 return portable_size_in_bits(nst) != portable_size_in_bits(ost);
2296 }
2297
2298 /*
2299 * Warn about questionable pointer conversions.
2300 */
2301 static void
2302 check_pointer_conversion(tnode_t *tn, type_t *ntp)
2303 {
2304 const type_t *nstp, *otp, *ostp;
2305 tspec_t nst, ost;
2306 const char *nts, *ots;
2307
2308 nstp = ntp->t_subt;
2309 otp = tn->tn_type;
2310 ostp = otp->t_subt;
2311 nst = nstp->t_tspec;
2312 ost = ostp->t_tspec;
2313
2314 if (nst == VOID || ost == VOID) {
2315 if (sflag && (nst == FUNC || ost == FUNC)) {
2316 /* null pointers are already handled in convert() */
2317 *(nst == FUNC ? &nts : &ots) = "function pointer";
2318 *(nst == VOID ? &nts : &ots) = "'void *'";
2319 /* ANSI C forbids conversion of %s to %s */
2320 warning(303, ots, nts);
2321 }
2322 return;
2323 } else if (nst == FUNC && ost == FUNC) {
2324 return;
2325 } else if (nst == FUNC || ost == FUNC) {
2326 /* converting '%s' to '%s' is questionable */
2327 warning(229, type_name(otp), type_name(ntp));
2328 return;
2329 }
2330
2331 if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
2332 ost != CHAR && ost != UCHAR &&
2333 !is_incomplete(ostp)) {
2334 /* converting '%s' to '%s' may cause alignment problem */
2335 warning(135, type_name(otp), type_name(ntp));
2336 }
2337
2338 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
2339 /* pointer cast from '%s' to '%s' may be troublesome */
2340 warning(247, type_name(otp), type_name(ntp));
2341 }
2342 }
2343
2344 static void
2345 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
2346 tspec_t nt, val_t *v, val_t *nv)
2347 {
2348 ldbl_t max = 0.0, min = 0.0;
2349
2350 switch (nt) {
2351 case CHAR:
2352 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
2353 case UCHAR:
2354 max = TARG_UCHAR_MAX; min = 0; break;
2355 case SCHAR:
2356 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
2357 case SHORT:
2358 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
2359 case USHORT:
2360 max = TARG_USHRT_MAX; min = 0; break;
2361 case ENUM:
2362 case INT:
2363 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
2364 case UINT:
2365 max = TARG_UINT_MAX; min = 0; break;
2366 case LONG:
2367 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
2368 case ULONG:
2369 max = TARG_ULONG_MAX; min = 0; break;
2370 case QUAD:
2371 max = QUAD_MAX; min = QUAD_MIN; break;
2372 case UQUAD:
2373 max = UQUAD_MAX; min = 0; break;
2374 case FLOAT:
2375 case FCOMPLEX:
2376 max = FLT_MAX; min = -FLT_MAX; break;
2377 case DOUBLE:
2378 case DCOMPLEX:
2379 max = DBL_MAX; min = -DBL_MAX; break;
2380 case PTR:
2381 /* Got already an error because of float --> ptr */
2382 case LDOUBLE:
2383 case LCOMPLEX:
2384 /* LINTED 248 */
2385 max = LDBL_MAX; min = -max; break;
2386 default:
2387 lint_assert(/*CONSTCOND*/false);
2388 }
2389 if (v->v_ldbl > max || v->v_ldbl < min) {
2390 lint_assert(nt != LDOUBLE);
2391 if (op == FARG) {
2392 /* conversion of '%s' to '%s' is out of range, ... */
2393 warning(295,
2394 type_name(gettyp(ot)), type_name(tp), arg);
2395 } else {
2396 /* conversion of '%s' to '%s' is out of range */
2397 warning(119,
2398 type_name(gettyp(ot)), type_name(tp));
2399 }
2400 v->v_ldbl = v->v_ldbl > 0 ? max : min;
2401 }
2402
2403 if (nt == FLOAT) {
2404 nv->v_ldbl = (float)v->v_ldbl;
2405 } else if (nt == DOUBLE) {
2406 nv->v_ldbl = (double)v->v_ldbl;
2407 } else if (nt == LDOUBLE) {
2408 nv->v_ldbl = v->v_ldbl;
2409 } else {
2410 nv->v_quad = (int64_t)v->v_ldbl;
2411 }
2412 }
2413
2414 static bool
2415 convert_constant_to_floating(tspec_t nt, val_t *nv,
2416 tspec_t ot, const val_t *v)
2417 {
2418 if (nt == FLOAT) {
2419 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2420 (float)(uint64_t)v->v_quad : (float)v->v_quad;
2421 } else if (nt == DOUBLE) {
2422 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2423 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2424 } else if (nt == LDOUBLE) {
2425 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2426 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2427 } else
2428 return false;
2429 return true;
2430 }
2431
2432 /*
2433 * Print a warning if bits which were set are lost due to the conversion.
2434 * This can happen with operator ORASS only.
2435 */
2436 static void
2437 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
2438 uint64_t xmask, op_t op)
2439 {
2440 if (nsz < osz && (v->v_quad & xmask) != 0) {
2441 /* constant truncated by conversion, op %s */
2442 warning(306, op_name(op));
2443 }
2444 }
2445
2446 /*
2447 * Print a warning if additional bits are not all 1
2448 * and the most significant bit of the old value is 1,
2449 * or if at least one (but not all) removed bit was 0.
2450 */
2451 static void
2452 convert_constant_check_range_bitand(size_t nsz, size_t osz,
2453 uint64_t xmask, const val_t *nv,
2454 tspec_t ot, const val_t *v,
2455 const type_t *tp, op_t op)
2456 {
2457 if (nsz > osz &&
2458 (nv->v_quad & bit((unsigned int)(osz - 1))) != 0 &&
2459 (nv->v_quad & xmask) != xmask) {
2460 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
2461 warning(309, type_name(gettyp(ot)),
2462 type_name(tp), op_name(op));
2463 } else if (nsz < osz &&
2464 (v->v_quad & xmask) != xmask &&
2465 (v->v_quad & xmask) != 0) {
2466 /* constant truncated by conversion, op %s */
2467 warning(306, op_name(op));
2468 }
2469 }
2470
2471 static void
2472 convert_constant_check_range_signed(op_t op, int arg)
2473 {
2474 if (op == ASSIGN) {
2475 /* assignment of negative constant to unsigned type */
2476 warning(164);
2477 } else if (op == INIT) {
2478 /* initialization of unsigned with negative constant */
2479 warning(221);
2480 } else if (op == FARG) {
2481 /* conversion of negative constant to unsigned type, ... */
2482 warning(296, arg);
2483 } else if (modtab[op].m_comparison) {
2484 /* handled by check_integer_comparison() */
2485 } else {
2486 /* conversion of negative constant to unsigned type */
2487 warning(222);
2488 }
2489 }
2490
2491 /*
2492 * Loss of significant bit(s). All truncated bits
2493 * of unsigned types or all truncated bits plus the
2494 * msb of the target for signed types are considered
2495 * to be significant bits. Loss of significant bits
2496 * means that at least one of the bits was set in an
2497 * unsigned type or that at least one but not all of
2498 * the bits was set in a signed type.
2499 * Loss of significant bits means that it is not
2500 * possible, also not with necessary casts, to convert
2501 * back to the original type. A example for a
2502 * necessary cast is:
2503 * char c; int i; c = 128;
2504 * i = c; ** yields -128 **
2505 * i = (unsigned char)c; ** yields 128 **
2506 */
2507 static void
2508 convert_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
2509 tspec_t ot)
2510 {
2511 if (op == ASSIGN && tp->t_bitfield) {
2512 /* precision lost in bit-field assignment */
2513 warning(166);
2514 } else if (op == ASSIGN) {
2515 /* constant truncated by assignment */
2516 warning(165);
2517 } else if (op == INIT && tp->t_bitfield) {
2518 /* bit-field initializer does not fit */
2519 warning(180);
2520 } else if (op == INIT) {
2521 /* initializer does not fit */
2522 warning(178);
2523 } else if (op == CASE) {
2524 /* case label affected by conversion */
2525 warning(196);
2526 } else if (op == FARG) {
2527 /* conversion of '%s' to '%s' is out of range, arg #%d */
2528 warning(295,
2529 type_name(gettyp(ot)), type_name(tp), arg);
2530 } else {
2531 /* conversion of '%s' to '%s' is out of range */
2532 warning(119,
2533 type_name(gettyp(ot)), type_name(tp));
2534 }
2535 }
2536
2537 static void
2538 convert_constant_check_range_loss(op_t op, int arg, const type_t *tp,
2539 tspec_t ot)
2540 {
2541 if (op == ASSIGN && tp->t_bitfield) {
2542 /* precision lost in bit-field assignment */
2543 warning(166);
2544 } else if (op == INIT && tp->t_bitfield) {
2545 /* bit-field initializer out of range */
2546 warning(11);
2547 } else if (op == CASE) {
2548 /* case label affected by conversion */
2549 warning(196);
2550 } else if (op == FARG) {
2551 /* conversion of '%s' to '%s' is out of range, arg #%d */
2552 warning(295,
2553 type_name(gettyp(ot)), type_name(tp), arg);
2554 } else {
2555 /* conversion of '%s' to '%s' is out of range */
2556 warning(119,
2557 type_name(gettyp(ot)), type_name(tp));
2558 }
2559 }
2560
2561 static void
2562 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
2563 op_t op, int arg, const val_t *v, val_t *nv)
2564 {
2565 unsigned int osz, nsz;
2566 uint64_t xmask, xmsk1;
2567
2568 osz = size_in_bits(ot);
2569 nsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
2570 xmask = value_bits(nsz) ^ value_bits(osz);
2571 xmsk1 = value_bits(nsz) ^ value_bits(osz - 1);
2572 /*
2573 * For bitwise operations we are not interested in the
2574 * value, but in the bits itself.
2575 */
2576 if (op == ORASS || op == BITOR || op == BITXOR) {
2577 convert_constant_check_range_bitor(nsz, osz, v, xmask, op);
2578 } else if (op == ANDASS || op == BITAND) {
2579 convert_constant_check_range_bitand(nsz, osz, xmask, nv, ot,
2580 v, tp, op);
2581 } else if ((nt != PTR && is_uinteger(nt)) &&
2582 (ot != PTR && !is_uinteger(ot)) &&
2583 v->v_quad < 0) {
2584 convert_constant_check_range_signed(op, arg);
2585 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2586 (v->v_quad & xmask) != 0 &&
2587 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2588 convert_constant_check_range_truncated(op, arg, tp, ot);
2589 } else if (nv->v_quad != v->v_quad) {
2590 convert_constant_check_range_loss(op, arg, tp, ot);
2591 }
2592 }
2593
2594 /*
2595 * Converts a typed constant to a constant of another type.
2596 *
2597 * op operator which requires conversion
2598 * arg if op is FARG, # of argument
2599 * tp type in which to convert the constant
2600 * nv new constant
2601 * v old constant
2602 */
2603 void
2604 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v)
2605 {
2606 tspec_t ot, nt;
2607 unsigned int sz;
2608 bool range_check;
2609
2610 /*
2611 * TODO: make 'v' const; the name of this function does not suggest
2612 * that it modifies 'v'.
2613 */
2614 ot = v->v_tspec;
2615 nt = nv->v_tspec = tp->t_tspec;
2616 range_check = false;
2617
2618 if (nt == BOOL) { /* C99 6.3.1.2 */
2619 nv->v_unsigned_since_c90 = false;
2620 nv->v_quad = is_nonzero_val(v) ? 1 : 0;
2621 return;
2622 }
2623
2624 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
2625 convert_constant_floating(op, arg, ot, tp, nt, v, nv);
2626 } else if (!convert_constant_to_floating(nt, nv, ot, v)) {
2627 range_check = true; /* Check for lost precision. */
2628 nv->v_quad = v->v_quad;
2629 }
2630
2631 if ((v->v_unsigned_since_c90 && is_floating(nt)) ||
2632 (v->v_unsigned_since_c90 && (is_integer(nt) && !is_uinteger(nt) &&
2633 portable_size_in_bits(nt) >
2634 portable_size_in_bits(ot)))) {
2635 /* ANSI C treats constant as unsigned */
2636 warning(157);
2637 v->v_unsigned_since_c90 = false;
2638 }
2639
2640 if (is_integer(nt)) {
2641 sz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
2642 nv->v_quad = convert_integer(nv->v_quad, nt, sz);
2643 }
2644
2645 if (range_check && op != CVT)
2646 convert_constant_check_range(ot, tp, nt, op, arg, v, nv);
2647 }
2648
2649 /*
2650 * Called if incompatible types were detected.
2651 * Prints a appropriate warning.
2652 */
2653 static void
2654 warn_incompatible_types(op_t op,
2655 const type_t *ltp, tspec_t lt,
2656 const type_t *rtp, tspec_t rt)
2657 {
2658 const mod_t *mp;
2659
2660 mp = &modtab[op];
2661
2662 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2663 /* void type illegal in expression */
2664 error(109);
2665 } else if (op == ASSIGN) {
2666 if ((lt == STRUCT || lt == UNION) &&
2667 (rt == STRUCT || rt == UNION)) {
2668 /* assignment of different structures (%s != %s) */
2669 error(240, tspec_name(lt), tspec_name(rt));
2670 } else {
2671 /* cannot assign to '%s' from '%s' */
2672 error(171, type_name(ltp), type_name(rtp));
2673 }
2674 } else if (mp->m_binary) {
2675 /* operands of '%s' have incompatible types (%s != %s) */
2676 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2677 } else {
2678 lint_assert(rt == NOTSPEC);
2679 /* operand of '%s' has invalid type (%s) */
2680 error(108, mp->m_name, tspec_name(lt));
2681 }
2682 }
2683
2684 /*
2685 * Called if incompatible pointer types are detected.
2686 * Print an appropriate warning.
2687 */
2688 static void
2689 warn_incompatible_pointers(const mod_t *mp,
2690 const type_t *ltp, const type_t *rtp)
2691 {
2692 tspec_t lt, rt;
2693
2694 lint_assert(ltp->t_tspec == PTR);
2695 lint_assert(rtp->t_tspec == PTR);
2696
2697 lt = ltp->t_subt->t_tspec;
2698 rt = rtp->t_subt->t_tspec;
2699
2700 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2701 if (mp == NULL) {
2702 /* illegal structure pointer combination */
2703 warning(244);
2704 } else {
2705 /* incompatible structure pointers: '%s' '%s' '%s' */
2706 warning(245, type_name(ltp), mp->m_name, type_name(rtp));
2707 }
2708 } else {
2709 if (mp == NULL) {
2710 /* illegal combination of '%s' and '%s' */
2711 warning(184, type_name(ltp), type_name(rtp));
2712 } else {
2713 /* illegal combination of '%s' and '%s', op '%s' */
2714 warning(124,
2715 type_name(ltp), type_name(rtp), mp->m_name);
2716 }
2717 }
2718 }
2719
2720 /* Return a type based on tp1, with added qualifiers from tp2. */
2721 static type_t *
2722 merge_qualifiers(type_t *tp1, const type_t *tp2)
2723 {
2724 type_t *ntp, *nstp;
2725 bool c1, c2, v1, v2;
2726
2727 lint_assert(tp1->t_tspec == PTR);
2728 lint_assert(tp2->t_tspec == PTR);
2729
2730 c1 = tp1->t_subt->t_const;
2731 c2 = tp2->t_subt->t_const;
2732 v1 = tp1->t_subt->t_volatile;
2733 v2 = tp2->t_subt->t_volatile;
2734
2735 if (c1 == (c1 | c2) && v1 == (v1 | v2))
2736 return tp1;
2737
2738 nstp = expr_dup_type(tp1->t_subt);
2739 nstp->t_const |= c2;
2740 nstp->t_volatile |= v2;
2741
2742 ntp = expr_dup_type(tp1);
2743 ntp->t_subt = nstp;
2744 return ntp;
2745 }
2746
2747 /*
2748 * Returns true if the given structure or union has a constant member
2749 * (maybe recursively).
2750 */
2751 static bool
2752 has_constant_member(const type_t *tp)
2753 {
2754 sym_t *m;
2755
2756 lint_assert(is_struct_or_union(tp->t_tspec));
2757
2758 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
2759 const type_t *mtp = m->s_type;
2760 if (mtp->t_const)
2761 return true;
2762 if (is_struct_or_union(mtp->t_tspec) &&
2763 has_constant_member(mtp))
2764 return true;
2765 }
2766 return false;
2767 }
2768
2769 /*
2770 * Create a new node for one of the operators POINT and ARROW.
2771 */
2772 static tnode_t *
2773 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
2774 {
2775 tnode_t *ntn, *ctn;
2776 bool nolval;
2777
2778 lint_assert(rn->tn_op == NAME);
2779 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
2780
2781 /*
2782 * Remember if the left operand is an lvalue (structure members
2783 * are lvalues if and only if the structure itself is an lvalue).
2784 */
2785 nolval = op == POINT && !ln->tn_lvalue;
2786
2787 if (op == POINT) {
2788 ln = build_address(sys, ln, true);
2789 } else if (ln->tn_type->t_tspec != PTR) {
2790 lint_assert(tflag);
2791 lint_assert(is_integer(ln->tn_type->t_tspec));
2792 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
2793 }
2794
2795 ctn = build_integer_constant(PTRDIFF_TSPEC,
2796 rn->tn_sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
2797
2798 ntn = new_tnode(PLUS, sys, expr_derive_type(rn->tn_type, PTR),
2799 ln, ctn);
2800 if (ln->tn_op == CON)
2801 ntn = fold(ntn);
2802
2803 if (rn->tn_type->t_bitfield) {
2804 ntn = new_tnode(FSEL, sys, ntn->tn_type->t_subt, ntn, NULL);
2805 } else {
2806 ntn = new_tnode(INDIR, sys, ntn->tn_type->t_subt, ntn, NULL);
2807 }
2808
2809 if (nolval)
2810 ntn->tn_lvalue = false;
2811
2812 return ntn;
2813 }
2814
2815 /*
2816 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2817 */
2818 static tnode_t *
2819 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
2820 {
2821 tnode_t *cn, *ntn;
2822
2823 lint_assert(ln != NULL);
2824
2825 if (ln->tn_type->t_tspec == PTR) {
2826 cn = plength(ln->tn_type);
2827 } else {
2828 cn = build_integer_constant(INT, (int64_t)1);
2829 }
2830 ntn = new_tnode(op, sys, ln->tn_type, ln, cn);
2831
2832 return ntn;
2833 }
2834
2835 /*
2836 * Create a node for REAL, IMAG
2837 */
2838 static tnode_t *
2839 build_real_imag(op_t op, bool sys, tnode_t *ln)
2840 {
2841 tnode_t *cn, *ntn;
2842
2843 lint_assert(ln != NULL);
2844
2845 if (ln->tn_op == NAME) {
2846 /*
2847 * This may be too much, but it avoids wrong warnings.
2848 * See d_c99_complex_split.c.
2849 */
2850 mark_as_used(ln->tn_sym, false, false);
2851 mark_as_set(ln->tn_sym);
2852 }
2853
2854 switch (ln->tn_type->t_tspec) {
2855 case LCOMPLEX:
2856 /* XXX: integer and LDOUBLE don't match. */
2857 cn = build_integer_constant(LDOUBLE, (int64_t)1);
2858 break;
2859 case DCOMPLEX:
2860 /* XXX: integer and DOUBLE don't match. */
2861 cn = build_integer_constant(DOUBLE, (int64_t)1);
2862 break;
2863 case FCOMPLEX:
2864 /* XXX: integer and FLOAT don't match. */
2865 cn = build_integer_constant(FLOAT, (int64_t)1);
2866 break;
2867 default:
2868 /* __%s__ is illegal for type %s */
2869 error(276, op == REAL ? "real" : "imag",
2870 type_name(ln->tn_type));
2871 return NULL;
2872 }
2873 ntn = new_tnode(op, sys, cn->tn_type, ln, cn);
2874 ntn->tn_lvalue = true;
2875
2876 return ntn;
2877 }
2878
2879 /*
2880 * Create a tree node for the unary & operator
2881 */
2882 static tnode_t *
2883 build_address(bool sys, tnode_t *tn, bool noign)
2884 {
2885 tspec_t t;
2886
2887 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2888 if (tflag)
2889 /* '&' before array or function: ignored */
2890 warning(127);
2891 return tn;
2892 }
2893
2894 /* eliminate &* */
2895 if (tn->tn_op == INDIR &&
2896 tn->tn_left->tn_type->t_tspec == PTR &&
2897 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2898 return tn->tn_left;
2899 }
2900
2901 return new_tnode(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
2902 tn, NULL);
2903 }
2904
2905 /*
2906 * Create a node for operators PLUS and MINUS.
2907 */
2908 static tnode_t *
2909 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
2910 {
2911 tnode_t *ntn, *ctn;
2912 type_t *tp;
2913
2914 /* If pointer and integer, then pointer to the lhs. */
2915 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
2916 ntn = ln;
2917 ln = rn;
2918 rn = ntn;
2919 }
2920
2921 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2922 lint_assert(is_integer(rn->tn_type->t_tspec));
2923
2924 check_ctype_macro_invocation(ln, rn);
2925 check_enum_array_index(ln, rn);
2926
2927 ctn = plength(ln->tn_type);
2928 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2929 rn = convert(NOOP, 0, ctn->tn_type, rn);
2930 rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn);
2931 if (rn->tn_left->tn_op == CON)
2932 rn = fold(rn);
2933 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
2934
2935 } else if (rn->tn_type->t_tspec == PTR) {
2936
2937 lint_assert(ln->tn_type->t_tspec == PTR);
2938 lint_assert(op == MINUS);
2939 tp = gettyp(PTRDIFF_TSPEC);
2940 ntn = new_tnode(op, sys, tp, ln, rn);
2941 if (ln->tn_op == CON && rn->tn_op == CON)
2942 ntn = fold(ntn);
2943 ctn = plength(ln->tn_type);
2944 balance(NOOP, &ntn, &ctn);
2945 ntn = new_tnode(DIV, sys, tp, ntn, ctn);
2946
2947 } else {
2948
2949 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
2950
2951 }
2952 return ntn;
2953 }
2954
2955 /*
2956 * Create a node for operators SHL and SHR.
2957 */
2958 static tnode_t *
2959 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
2960 {
2961 tspec_t t;
2962 tnode_t *ntn;
2963
2964 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2965 rn = convert(CVT, 0, gettyp(INT), rn);
2966 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
2967 return ntn;
2968 }
2969
2970 /*
2971 * Create a node for COLON.
2972 */
2973 static tnode_t *
2974 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
2975 {
2976 tspec_t lt, rt, pdt;
2977 type_t *tp;
2978 tnode_t *ntn;
2979
2980 lt = ln->tn_type->t_tspec;
2981 rt = rn->tn_type->t_tspec;
2982 pdt = PTRDIFF_TSPEC;
2983
2984 /*
2985 * Arithmetic types are balanced, all other type combinations
2986 * still need to be handled.
2987 */
2988 if (is_arithmetic(lt) && is_arithmetic(rt)) {
2989 tp = ln->tn_type;
2990 } else if (lt == BOOL && rt == BOOL) {
2991 tp = ln->tn_type;
2992 } else if (lt == VOID || rt == VOID) {
2993 tp = gettyp(VOID);
2994 } else if (lt == STRUCT || lt == UNION) {
2995 /* Both types must be identical. */
2996 lint_assert(rt == STRUCT || rt == UNION);
2997 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2998 if (is_incomplete(ln->tn_type)) {
2999 /* unknown operand size, op %s */
3000 error(138, op_name(COLON));
3001 return NULL;
3002 }
3003 tp = ln->tn_type;
3004 } else if (lt == PTR && is_integer(rt)) {
3005 if (rt != pdt) {
3006 rn = convert(NOOP, 0, gettyp(pdt), rn);
3007 rt = pdt;
3008 }
3009 tp = ln->tn_type;
3010 } else if (rt == PTR && is_integer(lt)) {
3011 if (lt != pdt) {
3012 ln = convert(NOOP, 0, gettyp(pdt), ln);
3013 lt = pdt;
3014 }
3015 tp = rn->tn_type;
3016 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
3017 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
3018 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
3019 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
3020 } else {
3021 /*
3022 * XXX For now we simply take the left type. This is
3023 * probably wrong, if one type contains a function prototype
3024 * and the other one, at the same place, only an old style
3025 * declaration.
3026 */
3027 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
3028 }
3029
3030 ntn = new_tnode(COLON, sys, tp, ln, rn);
3031
3032 return ntn;
3033 }
3034
3035 /*
3036 * Create a node for an assignment operator (both = and op= ).
3037 */
3038 static tnode_t *
3039 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
3040 {
3041 tspec_t lt, rt;
3042 tnode_t *ntn, *ctn;
3043
3044 lint_assert(ln != NULL);
3045 lint_assert(rn != NULL);
3046
3047 lt = ln->tn_type->t_tspec;
3048 rt = rn->tn_type->t_tspec;
3049
3050 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
3051 lint_assert(is_integer(rt));
3052 ctn = plength(ln->tn_type);
3053 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
3054 rn = convert(NOOP, 0, ctn->tn_type, rn);
3055 rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn);
3056 if (rn->tn_left->tn_op == CON)
3057 rn = fold(rn);
3058 }
3059
3060 if ((op == ASSIGN || op == RETURN || op == INIT) &&
3061 (lt == STRUCT || rt == STRUCT)) {
3062 lint_assert(lt == rt);
3063 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
3064 if (is_incomplete(ln->tn_type)) {
3065 if (op == RETURN) {
3066 /* cannot return incomplete type */
3067 error(212);
3068 } else {
3069 /* unknown operand size, op %s */
3070 error(138, op_name(op));
3071 }
3072 return NULL;
3073 }
3074 }
3075
3076 if (op == SHLASS) {
3077 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
3078 if (hflag)
3079 /* semantics of '%s' change in ANSI C; ... */
3080 warning(118, "<<=");
3081 }
3082 } else if (op != SHRASS) {
3083 if (op == ASSIGN || lt != PTR) {
3084 if (lt != rt ||
3085 (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
3086 rn = convert(op, 0, ln->tn_type, rn);
3087 rt = lt;
3088 }
3089 }
3090 }
3091
3092 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
3093
3094 return ntn;
3095 }
3096
3097 /*
3098 * Get length_in_bits of type tp->t_subt, as a constant expression of type
3099 * ptrdiff_t as seen from the target platform.
3100 */
3101 static tnode_t *
3102 plength(type_t *tp)
3103 {
3104 int elem, elsz_in_bits;
3105
3106 lint_assert(tp->t_tspec == PTR);
3107 tp = tp->t_subt;
3108
3109 elem = 1;
3110 elsz_in_bits = 0;
3111
3112 while (tp->t_tspec == ARRAY) {
3113 elem *= tp->t_dim;
3114 tp = tp->t_subt;
3115 }
3116
3117 switch (tp->t_tspec) {
3118 case FUNC:
3119 /* pointer to function is not allowed here */
3120 error(110);
3121 break;
3122 case VOID:
3123 /* cannot do pointer arithmetic on operand of unknown size */
3124 gnuism(136);
3125 break;
3126 case STRUCT:
3127 case UNION:
3128 if ((elsz_in_bits = tp->t_str->sou_size_in_bits) == 0)
3129 /* cannot do pointer arithmetic on operand of ... */
3130 error(136);
3131 break;
3132 case ENUM:
3133 if (is_incomplete(tp)) {
3134 /* cannot do pointer arithmetic on operand of ... */
3135 warning(136);
3136 }
3137 /* FALLTHROUGH */
3138 default:
3139 if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0) {
3140 /* cannot do pointer arithmetic on operand of ... */
3141 error(136);
3142 } else {
3143 lint_assert(elsz_in_bits != -1);
3144 }
3145 break;
3146 }
3147
3148 if (elem == 0 && elsz_in_bits != 0) {
3149 /* cannot do pointer arithmetic on operand of unknown size */
3150 error(136);
3151 }
3152
3153 if (elsz_in_bits == 0)
3154 elsz_in_bits = CHAR_SIZE;
3155
3156 return build_integer_constant(PTRDIFF_TSPEC,
3157 (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
3158 }
3159
3160 /*
3161 * XXX
3162 * Note: There appear to be a number of bugs in detecting overflow in
3163 * this function. An audit and a set of proper regression tests are needed.
3164 * --Perry Metzger, Nov. 16, 2001
3165 */
3166 /*
3167 * Do only as much as necessary to compute constant expressions.
3168 * Called only if the operator allows folding and all operands are constants.
3169 */
3170 static tnode_t *
3171 fold(tnode_t *tn)
3172 {
3173 val_t *v;
3174 tspec_t t;
3175 bool utyp, ovfl;
3176 int64_t sl, sr = 0, q = 0, mask;
3177 uint64_t ul, ur = 0;
3178 tnode_t *cn;
3179
3180 v = xcalloc(1, sizeof(*v));
3181 v->v_tspec = tn->tn_type->t_tspec;
3182
3183 t = tn->tn_left->tn_type->t_tspec;
3184 utyp = !is_integer(t) || is_uinteger(t);
3185 ul = sl = tn->tn_left->tn_val->v_quad;
3186 if (is_binary(tn))
3187 ur = sr = tn->tn_right->tn_val->v_quad;
3188
3189 mask = value_bits(size_in_bits(t));
3190 ovfl = false;
3191
3192 switch (tn->tn_op) {
3193 case UPLUS:
3194 q = sl;
3195 break;
3196 case UMINUS:
3197 q = -sl;
3198 if (sl != 0 && msb(q, t) == msb(sl, t))
3199 ovfl = true;
3200 break;
3201 case COMPL:
3202 q = ~sl;
3203 break;
3204 case MULT:
3205 if (utyp) {
3206 q = ul * ur;
3207 if (q != (q & mask))
3208 ovfl = true;
3209 else if ((ul != 0) && ((q / ul) != ur))
3210 ovfl = true;
3211 } else {
3212 q = sl * sr;
3213 if (msb(q, t) != (msb(sl, t) ^ msb(sr, t)))
3214 ovfl = true;
3215 }
3216 break;
3217 case DIV:
3218 if (sr == 0) {
3219 /* division by 0 */
3220 error(139);
3221 q = utyp ? -1 : INT64_MAX;
3222 } else {
3223 q = utyp ? (int64_t)(ul / ur) : sl / sr;
3224 }
3225 break;
3226 case MOD:
3227 if (sr == 0) {
3228 /* modulus by 0 */
3229 error(140);
3230 q = 0;
3231 } else {
3232 q = utyp ? (int64_t)(ul % ur) : sl % sr;
3233 }
3234 break;
3235 case PLUS:
3236 q = utyp ? (int64_t)(ul + ur) : sl + sr;
3237 if (msb(sl, t) && msb(sr, t) && !msb(q, t))
3238 ovfl = true;
3239 if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(q, t))
3240 ovfl = true;
3241 break;
3242 case MINUS:
3243 q = utyp ? (int64_t)(ul - ur) : sl - sr;
3244 if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(q, t))
3245 ovfl = true;
3246 if (!msb(sl, t) && msb(sr, t) && msb(q, t))
3247 ovfl = true;
3248 break;
3249 case SHL:
3250 q = utyp ? (int64_t)(ul << sr) : sl << sr;
3251 break;
3252 case SHR:
3253 /*
3254 * The sign must be explicitly extended because
3255 * shifts of signed values are implementation dependent.
3256 */
3257 q = ul >> sr;
3258 q = convert_integer(q, t, size_in_bits(t) - (int)sr);
3259 break;
3260 case LT:
3261 q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
3262 break;
3263 case LE:
3264 q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
3265 break;
3266 case GE:
3267 q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
3268 break;
3269 case GT:
3270 q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
3271 break;
3272 case EQ:
3273 q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
3274 break;
3275 case NE:
3276 q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
3277 break;
3278 case BITAND:
3279 q = utyp ? (int64_t)(ul & ur) : sl & sr;
3280 break;
3281 case BITXOR:
3282 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
3283 break;
3284 case BITOR:
3285 q = utyp ? (int64_t)(ul | ur) : sl | sr;
3286 break;
3287 default:
3288 lint_assert(/*CONSTCOND*/false);
3289 }
3290
3291 /* XXX does not work for quads. */
3292 if (ovfl ||
3293 ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
3294 if (hflag)
3295 /* integer overflow detected, op '%s' */
3296 warning(141, op_name(tn->tn_op));
3297 }
3298
3299 v->v_quad = convert_integer(q, t, 0);
3300
3301 cn = build_constant(tn->tn_type, v);
3302 if (tn->tn_left->tn_system_dependent)
3303 cn->tn_system_dependent = true;
3304 if (is_binary(tn) && tn->tn_right->tn_system_dependent)
3305 cn->tn_system_dependent = true;
3306
3307 return cn;
3308 }
3309
3310 /*
3311 * Fold constant nodes, as much as is needed for comparing the value with 0
3312 * (test context, for controlling expressions).
3313 */
3314 static tnode_t *
3315 fold_test(tnode_t *tn)
3316 {
3317 bool l, r;
3318 val_t *v;
3319
3320 v = xcalloc(1, sizeof(*v));
3321 v->v_tspec = tn->tn_type->t_tspec;
3322 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
3323
3324 l = constant_is_nonzero(tn->tn_left);
3325 r = is_binary(tn) && constant_is_nonzero(tn->tn_right);
3326
3327 switch (tn->tn_op) {
3328 case NOT:
3329 if (hflag && !constcond_flag)
3330 /* constant argument to '!' */
3331 warning(239);
3332 v->v_quad = !l ? 1 : 0;
3333 break;
3334 case LOGAND:
3335 v->v_quad = l && r ? 1 : 0;
3336 break;
3337 case LOGOR:
3338 v->v_quad = l || r ? 1 : 0;
3339 break;
3340 default:
3341 lint_assert(/*CONSTCOND*/false);
3342 }
3343
3344 return build_constant(tn->tn_type, v);
3345 }
3346
3347 static ldbl_t
3348 floating_error_value(tspec_t t, ldbl_t lv)
3349 {
3350 if (t == FLOAT) {
3351 return lv < 0 ? -FLT_MAX : FLT_MAX;
3352 } else if (t == DOUBLE) {
3353 return lv < 0 ? -DBL_MAX : DBL_MAX;
3354 } else {
3355 /* LINTED 248: floating-point constant out of range */
3356 ldbl_t max = LDBL_MAX;
3357 return lv < 0 ? -max : max;
3358 }
3359 }
3360
3361 /*
3362 * Fold constant nodes having operands with floating point type.
3363 */
3364 static tnode_t *
3365 fold_float(tnode_t *tn)
3366 {
3367 val_t *v;
3368 tspec_t t;
3369 ldbl_t lv, rv = 0;
3370
3371 fpe = 0;
3372 v = xcalloc(1, sizeof(*v));
3373 v->v_tspec = t = tn->tn_type->t_tspec;
3374
3375 lint_assert(is_floating(t));
3376 lint_assert(t == tn->tn_left->tn_type->t_tspec);
3377 lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec);
3378
3379 lv = tn->tn_left->tn_val->v_ldbl;
3380 if (is_binary(tn))
3381 rv = tn->tn_right->tn_val->v_ldbl;
3382
3383 switch (tn->tn_op) {
3384 case UPLUS:
3385 v->v_ldbl = lv;
3386 break;
3387 case UMINUS:
3388 v->v_ldbl = -lv;
3389 break;
3390 case MULT:
3391 v->v_ldbl = lv * rv;
3392 break;
3393 case DIV:
3394 if (rv == 0.0) {
3395 /* division by 0 */
3396 error(139);
3397 v->v_ldbl = floating_error_value(t, lv);
3398 } else {
3399 v->v_ldbl = lv / rv;
3400 }
3401 break;
3402 case PLUS:
3403 v->v_ldbl = lv + rv;
3404 break;
3405 case MINUS:
3406 v->v_ldbl = lv - rv;
3407 break;
3408 case LT:
3409 v->v_quad = lv < rv ? 1 : 0;
3410 break;
3411 case LE:
3412 v->v_quad = lv <= rv ? 1 : 0;
3413 break;
3414 case GE:
3415 v->v_quad = lv >= rv ? 1 : 0;
3416 break;
3417 case GT:
3418 v->v_quad = lv > rv ? 1 : 0;
3419 break;
3420 case EQ:
3421 v->v_quad = lv == rv ? 1 : 0;
3422 break;
3423 case NE:
3424 v->v_quad = lv != rv ? 1 : 0;
3425 break;
3426 default:
3427 lint_assert(/*CONSTCOND*/false);
3428 }
3429
3430 lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0);
3431 if (fpe != 0 || isfinite((double)v->v_ldbl) == 0 ||
3432 (t == FLOAT &&
3433 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3434 (t == DOUBLE &&
3435 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3436 /* floating point overflow detected, op %s */
3437 warning(142, op_name(tn->tn_op));
3438 v->v_ldbl = floating_error_value(t, v->v_ldbl);
3439 fpe = 0;
3440 }
3441
3442 return build_constant(tn->tn_type, v);
3443 }
3444
3445
3446 /*
3447 * Create a constant node for sizeof.
3448 */
3449 tnode_t *
3450 build_sizeof(const type_t *tp)
3451 {
3452 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3453 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
3454 tn->tn_system_dependent = true;
3455 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
3456 return tn;
3457 }
3458
3459 /*
3460 * Create a constant node for offsetof.
3461 */
3462 /* ARGSUSED */ /* See implementation comments. */
3463 tnode_t *
3464 build_offsetof(const type_t *tp, const sym_t *sym)
3465 {
3466 unsigned int offset_in_bytes;
3467 tnode_t *tn;
3468
3469 if (!is_struct_or_union(tp->t_tspec))
3470 /* unacceptable operand of '%s' */
3471 error(111, "offsetof");
3472
3473 /* XXX: wrong size, no checking for sym fixme */
3474 offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3475 tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
3476 tn->tn_system_dependent = true;
3477 return tn;
3478 }
3479
3480 unsigned int
3481 type_size_in_bits(const type_t *tp)
3482 {
3483 unsigned int elem, elsz;
3484 bool flex;
3485
3486 elem = 1;
3487 flex = false;
3488 lint_assert(tp != NULL);
3489 while (tp->t_tspec == ARRAY) {
3490 flex = true; /* allow c99 flex arrays [] [0] */
3491 elem *= tp->t_dim;
3492 tp = tp->t_subt;
3493 }
3494 if (elem == 0) {
3495 if (!flex) {
3496 /* cannot take size/alignment of incomplete type */
3497 error(143);
3498 elem = 1;
3499 }
3500 }
3501 switch (tp->t_tspec) {
3502 case FUNC:
3503 /* cannot take size/alignment of function type '%s' */
3504 error(144, type_name(tp));
3505 elsz = 1;
3506 break;
3507 case STRUCT:
3508 case UNION:
3509 if (is_incomplete(tp)) {
3510 /* cannot take size/alignment of incomplete type */
3511 error(143);
3512 elsz = 1;
3513 } else {
3514 elsz = tp->t_str->sou_size_in_bits;
3515 }
3516 break;
3517 case ENUM:
3518 if (is_incomplete(tp)) {
3519 /* cannot take size/alignment of incomplete type */
3520 warning(143);
3521 }
3522 /* FALLTHROUGH */
3523 default:
3524 if (tp->t_bitfield) {
3525 /* cannot take size/alignment of bit-field */
3526 error(145);
3527 }
3528 if (tp->t_tspec == VOID) {
3529 /* cannot take size/alignment of void */
3530 error(146);
3531 elsz = 1;
3532 } else {
3533 elsz = size_in_bits(tp->t_tspec);
3534 lint_assert(elsz > 0);
3535 }
3536 break;
3537 }
3538
3539 return elem * elsz;
3540 }
3541
3542 tnode_t *
3543 build_alignof(const type_t *tp)
3544 {
3545 switch (tp->t_tspec) {
3546 case ARRAY:
3547 break;
3548
3549 case FUNC:
3550 /* cannot take size/alignment of function type '%s' */
3551 error(144, type_name(tp));
3552 return 0;
3553
3554 case STRUCT:
3555 case UNION:
3556 if (is_incomplete(tp)) {
3557 /* cannot take size/alignment of incomplete type */
3558 error(143);
3559 return 0;
3560 }
3561 break;
3562 case ENUM:
3563 break;
3564 default:
3565 if (tp->t_bitfield) {
3566 /* cannot take size/alignment of bit-field */
3567 error(145);
3568 return 0;
3569 }
3570 if (tp->t_tspec == VOID) {
3571 /* cannot take size/alignment of void */
3572 error(146);
3573 return 0;
3574 }
3575 break;
3576 }
3577
3578 return build_integer_constant(SIZEOF_TSPEC,
3579 (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
3580 }
3581
3582 /*
3583 * Type casts.
3584 */
3585 tnode_t *
3586 cast(tnode_t *tn, type_t *tp)
3587 {
3588 tspec_t nt, ot;
3589
3590 if (tn == NULL)
3591 return NULL;
3592
3593 tn = cconv(tn);
3594
3595 lint_assert(tp != NULL);
3596 nt = tp->t_tspec;
3597 ot = tn->tn_type->t_tspec;
3598
3599 if (nt == VOID) {
3600 /*
3601 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3602 * But this seems really questionable.
3603 */
3604 } else if (nt == UNION) {
3605 sym_t *m;
3606 struct_or_union *str = tp->t_str;
3607 if (!gflag) {
3608 /* union cast is a GCC extension */
3609 error(328);
3610 return NULL;
3611 }
3612 for (m = str->sou_first_member; m != NULL; m = m->s_next) {
3613 if (eqtype(m->s_type, tn->tn_type,
3614 false, false, NULL)) {
3615 tn = expr_alloc_tnode();
3616 tn->tn_op = CVT;
3617 tn->tn_type = tp;
3618 tn->tn_cast = true;
3619 tn->tn_right = NULL;
3620 return tn;
3621 }
3622 }
3623 /* type '%s' is not a member of '%s' */
3624 error(329, type_name(tn->tn_type), type_name(tp));
3625 return NULL;
3626 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3627 /* Casting to a struct is an undocumented GCC extension. */
3628 if (!(gflag && nt == STRUCT))
3629 goto invalid_cast;
3630 } else if (ot == STRUCT || ot == UNION) {
3631 goto invalid_cast;
3632 } else if (ot == VOID) {
3633 /* improper cast of void expression */
3634 error(148);
3635 return NULL;
3636 } else if (is_integer(nt) && is_scalar(ot)) {
3637 /* ok */
3638 } else if (is_floating(nt) && is_arithmetic(ot)) {
3639 /* ok */
3640 } else if (nt == PTR && is_integer(ot)) {
3641 /* ok */
3642 } else if (nt == PTR && ot == PTR) {
3643 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3644 if (hflag)
3645 /* cast discards 'const' from type '%s' */
3646 warning(275, type_name(tn->tn_type));
3647 }
3648 } else
3649 goto invalid_cast;
3650
3651 tn = convert(CVT, 0, tp, tn);
3652 tn->tn_cast = true;
3653
3654 return tn;
3655
3656 invalid_cast:
3657 /* invalid cast from '%s' to '%s' */
3658 error(147, type_name(tn->tn_type), type_name(tp));
3659 return NULL;
3660 }
3661
3662 /*
3663 * Create the node for a function argument.
3664 * All necessary conversions and type checks are done in
3665 * build_function_call because build_function_argument has no
3666 * information about expected argument types.
3667 */
3668 tnode_t *
3669 build_function_argument(tnode_t *args, tnode_t *arg)
3670 {
3671 tnode_t *ntn;
3672
3673 /*
3674 * If there was a serious error in the expression for the argument,
3675 * create a dummy argument so the positions of the remaining arguments
3676 * will not change.
3677 */
3678 if (arg == NULL)
3679 arg = build_integer_constant(INT, 0);
3680
3681 ntn = new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args);
3682
3683 return ntn;
3684 }
3685
3686 /*
3687 * Create the node for a function call. Also check types of
3688 * function arguments and insert conversions, if necessary.
3689 */
3690 tnode_t *
3691 build_function_call(tnode_t *func, bool sys, tnode_t *args)
3692 {
3693 tnode_t *ntn;
3694 op_t fcop;
3695
3696 if (func == NULL)
3697 return NULL;
3698
3699 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3700 fcop = CALL;
3701 } else {
3702 fcop = ICALL;
3703 }
3704
3705 check_ctype_function_call(func, args);
3706
3707 /*
3708 * after cconv() func will always be a pointer to a function
3709 * if it is a valid function designator.
3710 */
3711 func = cconv(func);
3712
3713 if (func->tn_type->t_tspec != PTR ||
3714 func->tn_type->t_subt->t_tspec != FUNC) {
3715 /* illegal function (type %s) */
3716 error(149, type_name(func->tn_type));
3717 return NULL;
3718 }
3719
3720 args = check_function_arguments(func->tn_type->t_subt, args);
3721
3722 ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
3723
3724 return ntn;
3725 }
3726
3727 /*
3728 * Check types of all function arguments and insert conversions,
3729 * if necessary.
3730 */
3731 static tnode_t *
3732 check_function_arguments(type_t *ftp, tnode_t *args)
3733 {
3734 tnode_t *arg;
3735 sym_t *asym;
3736 tspec_t at;
3737 int narg, npar, n, i;
3738
3739 /* get # of args in the prototype */
3740 npar = 0;
3741 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3742 npar++;
3743
3744 /* get # of args in function call */
3745 narg = 0;
3746 for (arg = args; arg != NULL; arg = arg->tn_right)
3747 narg++;
3748
3749 asym = ftp->t_args;
3750 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3751 /* argument mismatch: %d arg%s passed, %d expected */
3752 error(150, narg, narg > 1 ? "s" : "", npar);
3753 asym = NULL;
3754 }
3755
3756 for (n = 1; n <= narg; n++) {
3757
3758 /*
3759 * The rightmost argument is at the top of the argument
3760 * subtree.
3761 */
3762 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3763 continue;
3764
3765 /* some things which are always not allowed */
3766 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3767 /* void expressions may not be arguments, arg #%d */
3768 error(151, n);
3769 return NULL;
3770 } else if ((at == STRUCT || at == UNION) &&
3771 is_incomplete(arg->tn_left->tn_type)) {
3772 /* argument cannot have unknown size, arg #%d */
3773 error(152, n);
3774 return NULL;
3775 } else if (is_integer(at) &&
3776 arg->tn_left->tn_type->t_is_enum &&
3777 is_incomplete(arg->tn_left->tn_type)) {
3778 /* argument cannot have unknown size, arg #%d */
3779 warning(152, n);
3780 }
3781
3782 /* class conversions (arg in value context) */
3783 arg->tn_left = cconv(arg->tn_left);
3784
3785 if (asym != NULL) {
3786 arg->tn_left = check_prototype_argument(
3787 n, asym->s_type, arg->tn_left);
3788 } else {
3789 arg->tn_left = promote(NOOP, true, arg->tn_left);
3790 }
3791 arg->tn_type = arg->tn_left->tn_type;
3792
3793 if (asym != NULL)
3794 asym = asym->s_next;
3795 }
3796
3797 return args;
3798 }
3799
3800 /*
3801 * Compare the type of an argument with the corresponding type of a
3802 * prototype parameter. If it is a valid combination, but both types
3803 * are not the same, insert a conversion to convert the argument into
3804 * the type of the parameter.
3805 */
3806 static tnode_t *
3807 check_prototype_argument(
3808 int n, /* pos of arg */
3809 type_t *tp, /* expected type (from prototype) */
3810 tnode_t *tn) /* argument */
3811 {
3812 tnode_t *ln;
3813 bool dowarn;
3814
3815 ln = xcalloc(1, sizeof(*ln));
3816 ln->tn_type = expr_unqualified_type(tp);
3817 ln->tn_lvalue = true;
3818 if (typeok(FARG, n, ln, tn)) {
3819 if (!eqtype(tp, tn->tn_type,
3820 true, false, (dowarn = false, &dowarn)) || dowarn)
3821 tn = convert(FARG, n, tp, tn);
3822 }
3823 free(ln);
3824 return tn;
3825 }
3826
3827 /*
3828 * Return the value of an integral constant expression.
3829 * If the expression is not constant or its type is not an integer
3830 * type, an error message is printed.
3831 */
3832 val_t *
3833 constant(tnode_t *tn, bool required)
3834 {
3835 val_t *v;
3836
3837 if (tn != NULL)
3838 tn = cconv(tn);
3839 if (tn != NULL)
3840 tn = promote(NOOP, false, tn);
3841
3842 v = xcalloc(1, sizeof(*v));
3843
3844 if (tn == NULL) {
3845 lint_assert(nerr != 0);
3846 debug_step("constant node is null; returning 1 instead");
3847 v->v_tspec = INT;
3848 v->v_quad = 1;
3849 return v;
3850 }
3851
3852 v->v_tspec = tn->tn_type->t_tspec;
3853
3854 if (tn->tn_op == CON) {
3855 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3856 if (is_integer(tn->tn_val->v_tspec)) {
3857 v->v_unsigned_since_c90 =
3858 tn->tn_val->v_unsigned_since_c90;
3859 v->v_quad = tn->tn_val->v_quad;
3860 return v;
3861 }
3862 v->v_quad = tn->tn_val->v_ldbl;
3863 } else {
3864 v->v_quad = 1;
3865 }
3866
3867 if (required)
3868 /* integral constant expression expected */
3869 error(55);
3870 else
3871 /* variable array dimension is a C99/GCC extension */
3872 c99ism(318);
3873
3874 if (!is_integer(v->v_tspec))
3875 v->v_tspec = INT;
3876
3877 return v;
3878 }
3879
3880 static bool
3881 is_constcond_false(const tnode_t *tn, tspec_t t)
3882 {
3883 return (t == BOOL || t == INT) &&
3884 tn->tn_op == CON && tn->tn_val->v_quad == 0;
3885 }
3886
3887 /*
3888 * Perform some tests on expressions which can't be done in build_binary()
3889 * and functions called by build_binary(). These tests must be done here
3890 * because we need some information about the context in which the operations
3891 * are performed.
3892 * After all tests are performed and dofreeblk is true, expr() frees the
3893 * memory which is used for the expression.
3894 */
3895 void
3896 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool is_do_while)
3897 {
3898
3899 if (tn == NULL) { /* in case of errors */
3900 expr_free_all();
3901 return;
3902 }
3903
3904 /* expr() is also called in global initializations */
3905 if (dcs->d_ctx != EXTERN && !is_do_while)
3906 check_statement_reachable();
3907
3908 check_expr_misc(tn, vctx, tctx, !tctx, false, false, false);
3909 if (tn->tn_op == ASSIGN) {
3910 if (hflag && tctx)
3911 /* assignment in conditional context */
3912 warning(159);
3913 } else if (tn->tn_op == CON) {
3914 if (hflag && tctx && !constcond_flag &&
3915 !tn->tn_system_dependent &&
3916 !(is_do_while &&
3917 is_constcond_false(tn, tn->tn_type->t_tspec)))
3918 /* constant in conditional context */
3919 warning(161);
3920 }
3921 if (!modtab[tn->tn_op].m_has_side_effect) {
3922 /*
3923 * for left operands of COMMA this warning is already
3924 * printed
3925 */
3926 if (tn->tn_op != COMMA && !vctx && !tctx)
3927 check_null_effect(tn);
3928 }
3929 debug_node(tn);
3930
3931 /* free the tree memory */
3932 if (dofreeblk)
3933 expr_free_all();
3934 }
3935
3936 static bool
3937 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
3938 {
3939 op_t op = tn->tn_op;
3940
3941 if (modtab[op].m_has_side_effect)
3942 return true;
3943
3944 if (op == CVT && tn->tn_type->t_tspec == VOID)
3945 return has_side_effect(tn->tn_left);
3946
3947 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3948 if (op == LOGAND || op == LOGOR)
3949 return has_side_effect(tn->tn_right);
3950
3951 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3952 if (op == QUEST)
3953 return has_side_effect(tn->tn_right);
3954
3955 if (op == COLON || op == COMMA) {
3956 return has_side_effect(tn->tn_left) ||
3957 has_side_effect(tn->tn_right);
3958 }
3959
3960 return false;
3961 }
3962
3963 static bool
3964 is_void_cast(const tnode_t *tn)
3965 {
3966
3967 return tn->tn_op == CVT && tn->tn_cast &&
3968 tn->tn_type->t_tspec == VOID;
3969 }
3970
3971 static bool
3972 is_local_symbol(const tnode_t *tn)
3973 {
3974
3975 return tn->tn_op == LOAD &&
3976 tn->tn_left->tn_op == NAME &&
3977 tn->tn_left->tn_sym->s_scl == AUTO;
3978 }
3979
3980 static bool
3981 is_int_constant_zero(const tnode_t *tn)
3982 {
3983
3984 return tn->tn_op == CON &&
3985 tn->tn_type->t_tspec == INT &&
3986 tn->tn_val->v_quad == 0;
3987 }
3988
3989 static void
3990 check_null_effect(const tnode_t *tn)
3991 {
3992
3993 if (!hflag)
3994 return;
3995 if (has_side_effect(tn))
3996 return;
3997 if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
3998 return;
3999 if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
4000 return;
4001
4002 /* expression has null effect */
4003 warning(129);
4004 }
4005
4006 static void
4007 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
4008 {
4009 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4010 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4011 if (!szof)
4012 mark_as_set(ln->tn_sym);
4013 mark_as_used(ln->tn_sym, fcall, szof);
4014 }
4015 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4016 /* check the range of array indices */
4017 check_array_index(ln->tn_left, true);
4018 }
4019
4020 static void
4021 check_expr_load(const tnode_t *ln)
4022 {
4023 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4024 /* check the range of array indices */
4025 check_array_index(ln->tn_left, false);
4026 }
4027
4028 static void
4029 check_expr_side_effect(const tnode_t *ln, bool szof)
4030 {
4031 scl_t sc;
4032 dinfo_t *di;
4033
4034 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4035 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4036 sc = ln->tn_sym->s_scl;
4037 /*
4038 * Look if there was a asm statement in one of the
4039 * compound statements we are in. If not, we don't
4040 * print a warning.
4041 */
4042 for (di = dcs; di != NULL; di = di->d_enclosing) {
4043 if (di->d_asm)
4044 break;
4045 }
4046 if (sc != EXTERN && sc != STATIC &&
4047 !ln->tn_sym->s_set && !szof && di == NULL) {
4048 /* %s may be used before set */
4049 warning(158, ln->tn_sym->s_name);
4050 mark_as_set(ln->tn_sym);
4051 }
4052 mark_as_used(ln->tn_sym, false, false);
4053 }
4054 }
4055
4056 static void
4057 check_expr_assign(const tnode_t *ln, bool szof)
4058 {
4059 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4060 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4061 mark_as_set(ln->tn_sym);
4062 if (ln->tn_sym->s_scl == EXTERN)
4063 outusg(ln->tn_sym);
4064 }
4065 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4066 /* check the range of array indices */
4067 check_array_index(ln->tn_left, false);
4068 }
4069
4070 static void
4071 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4072 bool szof, bool vctx, bool tctx, bool retval_discarded)
4073 {
4074 lint_assert(ln->tn_op == ADDR);
4075 lint_assert(ln->tn_left->tn_op == NAME);
4076 if (!szof &&
4077 !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
4078 outcall(tn, vctx || tctx, retval_discarded);
4079 }
4080
4081 static bool
4082 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
4083 bool szof, bool fcall, bool vctx, bool tctx,
4084 bool retval_discarded, bool eqwarn)
4085 {
4086 switch (op) {
4087 case ADDR:
4088 check_expr_addr(ln, szof, fcall);
4089 break;
4090 case LOAD:
4091 check_expr_load(ln);
4092 /* FALLTHROUGH */
4093 case PUSH:
4094 case INCBEF:
4095 case DECBEF:
4096 case INCAFT:
4097 case DECAFT:
4098 case ADDASS:
4099 case SUBASS:
4100 case MULASS:
4101 case DIVASS:
4102 case MODASS:
4103 case ANDASS:
4104 case ORASS:
4105 case XORASS:
4106 case SHLASS:
4107 case SHRASS:
4108 case REAL:
4109 case IMAG:
4110 check_expr_side_effect(ln, szof);
4111 break;
4112 case ASSIGN:
4113 check_expr_assign(ln, szof);
4114 break;
4115 case CALL:
4116 check_expr_call(tn, ln, szof, vctx, tctx, retval_discarded);
4117 break;
4118 case EQ:
4119 if (hflag && eqwarn)
4120 /* operator '==' found where '=' was expected */
4121 warning(160);
4122 break;
4123 case CON:
4124 case NAME:
4125 case STRING:
4126 return false;
4127 /* LINTED206: (enumeration values not handled in switch) */
4128 case BITOR:
4129 case BITXOR:
4130 case NE:
4131 case GE:
4132 case GT:
4133 case LE:
4134 case LT:
4135 case SHR:
4136 case SHL:
4137 case MINUS:
4138 case PLUS:
4139 case MOD:
4140 case DIV:
4141 case MULT:
4142 case INDIR:
4143 case UMINUS:
4144 case UPLUS:
4145 case DEC:
4146 case INC:
4147 case COMPL:
4148 case NOT:
4149 case POINT:
4150 case ARROW:
4151 case NOOP:
4152 case BITAND:
4153 case FARG:
4154 case CASE:
4155 case INIT:
4156 case RETURN:
4157 case ICALL:
4158 case CVT:
4159 case COMMA:
4160 case FSEL:
4161 case COLON:
4162 case QUEST:
4163 case LOGOR:
4164 case LOGAND:
4165 break;
4166 }
4167 return true;
4168 }
4169
4170 void
4171 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
4172 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4173 {
4174 tnode_t *ln, *rn;
4175 const mod_t *mp;
4176 op_t op;
4177 bool cvctx, ctctx, eq, discard;
4178
4179 if (tn == NULL)
4180 return;
4181
4182 ln = tn->tn_left;
4183 rn = tn->tn_right;
4184 mp = &modtab[op = tn->tn_op];
4185
4186 if (!check_expr_op(tn, op, ln,
4187 szof, fcall, vctx, tctx, retval_discarded, eqwarn))
4188 return;
4189
4190 cvctx = mp->m_left_value_context;
4191 ctctx = mp->m_left_test_context;
4192 eq = mp->m_warn_if_operand_eq &&
4193 !ln->tn_parenthesized &&
4194 rn != NULL && !rn->tn_parenthesized;
4195
4196 /*
4197 * values of operands of ':' are not used if the type of at least
4198 * one of the operands (for gcc compatibility) is void
4199 * XXX test/value context of QUEST should probably be used as
4200 * context for both operands of COLON
4201 */
4202 if (op == COLON && tn->tn_type->t_tspec == VOID)
4203 cvctx = ctctx = false;
4204 discard = op == CVT && tn->tn_type->t_tspec == VOID;
4205 check_expr_misc(ln, cvctx, ctctx, eq, op == CALL, discard, szof);
4206
4207 switch (op) {
4208 case PUSH:
4209 if (rn != NULL)
4210 check_expr_misc(rn, false, false, eq, false, false,
4211 szof);
4212 break;
4213 case LOGAND:
4214 case LOGOR:
4215 check_expr_misc(rn, false, true, eq, false, false, szof);
4216 break;
4217 case COLON:
4218 check_expr_misc(rn, cvctx, ctctx, eq, false, false, szof);
4219 break;
4220 case COMMA:
4221 check_expr_misc(rn, vctx, tctx, eq, false, false, szof);
4222 break;
4223 default:
4224 if (mp->m_binary)
4225 check_expr_misc(rn, true, false, eq, false, false,
4226 szof);
4227 break;
4228 }
4229 }
4230
4231 /*
4232 * Checks the range of array indices, if possible.
4233 * amper is set if only the address of the element is used. This
4234 * means that the index is allowed to refer to the first element
4235 * after the array.
4236 */
4237 static void
4238 check_array_index(tnode_t *tn, bool amper)
4239 {
4240 int dim;
4241 tnode_t *ln, *rn;
4242 int elsz;
4243 int64_t con;
4244
4245 ln = tn->tn_left;
4246 rn = tn->tn_right;
4247
4248 /* We can only check constant indices. */
4249 if (rn->tn_op != CON)
4250 return;
4251
4252 /* Return if the left node does not stem from an array. */
4253 if (ln->tn_op != ADDR)
4254 return;
4255 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4256 return;
4257 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4258 return;
4259
4260 /*
4261 * For incomplete array types, we can print a warning only if
4262 * the index is negative.
4263 */
4264 if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4265 return;
4266
4267 /* Get the size of one array element */
4268 if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0)
4269 return;
4270 elsz /= CHAR_SIZE;
4271
4272 /* Change the unit of the index from bytes to element size. */
4273 if (is_uinteger(rn->tn_type->t_tspec)) {
4274 con = (uint64_t)rn->tn_val->v_quad / elsz;
4275 } else {
4276 con = rn->tn_val->v_quad / elsz;
4277 }
4278
4279 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4280
4281 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4282 /* array subscript cannot be negative: %ld */
4283 warning(167, (long)con);
4284 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4285 /* array subscript cannot be > %d: %ld */
4286 warning(168, dim - 1, (long)con);
4287 }
4288 }
4289
4290 static bool
4291 is_out_of_char_range(const tnode_t *tn)
4292 {
4293 return tn->tn_op == CON &&
4294 !(0 <= tn->tn_val->v_quad &&
4295 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
4296 }
4297
4298 /*
4299 * Check for ordered comparisons of unsigned values with 0.
4300 */
4301 static void
4302 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4303 {
4304 tspec_t lt, rt;
4305
4306 lt = ln->tn_type->t_tspec;
4307 rt = rn->tn_type->t_tspec;
4308
4309 if (ln->tn_op != CON && rn->tn_op != CON)
4310 return;
4311
4312 if (!is_integer(lt) || !is_integer(rt))
4313 return;
4314
4315 if (hflag || pflag) {
4316 if (lt == CHAR && is_out_of_char_range(rn)) {
4317 /* nonportable character comparison '%s %d' */
4318 warning(230, op_name(op), (int)rn->tn_val->v_quad);
4319 return;
4320 }
4321 if (rt == CHAR && is_out_of_char_range(ln)) {
4322 /* nonportable character comparison '%s %d' */
4323 warning(230, op_name(op), (int)ln->tn_val->v_quad);
4324 return;
4325 }
4326 }
4327
4328 if (is_uinteger(lt) && !is_uinteger(rt) &&
4329 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4330 if (rn->tn_val->v_quad < 0) {
4331 /* comparison of %s with %s, op %s */
4332 warning(162, type_name(ln->tn_type),
4333 "negative constant", op_name(op));
4334 } else if (op == LT || op == GE) {
4335 /* comparison of %s with %s, op %s */
4336 warning(162, type_name(ln->tn_type), "0", op_name(op));
4337 }
4338 return;
4339 }
4340 if (is_uinteger(rt) && !is_uinteger(lt) &&
4341 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4342 if (ln->tn_val->v_quad < 0) {
4343 /* comparison of %s with %s, op %s */
4344 warning(162, "negative constant",
4345 type_name(rn->tn_type), op_name(op));
4346 } else if (op == GT || op == LE) {
4347 /* comparison of %s with %s, op %s */
4348 warning(162, "0", type_name(rn->tn_type), op_name(op));
4349 }
4350 return;
4351 }
4352 }
4353
4354 /*
4355 * Return whether the expression can be used for static initialization.
4356 *
4357 * Constant initialization expressions must be constant or an address
4358 * of a static object with an optional offset. In the first case,
4359 * the result is returned in *offsp. In the second case, the static
4360 * object is returned in *symp and the offset in *offsp.
4361 *
4362 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4363 * CON. Type conversions are allowed if they do not change binary
4364 * representation (including width).
4365 *
4366 * C99 6.6 "Constant expressions"
4367 * C99 6.7.8p4 restricts initializers for static storage duration
4368 */
4369 bool
4370 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4371 {
4372 const sym_t *sym;
4373 ptrdiff_t offs1, offs2;
4374 tspec_t t, ot;
4375
4376 switch (tn->tn_op) {
4377 case MINUS:
4378 if (tn->tn_right->tn_op == CVT)
4379 return constant_addr(tn->tn_right, symp, offsp);
4380 else if (tn->tn_right->tn_op != CON)
4381 return false;
4382 /* FALLTHROUGH */
4383 case PLUS:
4384 offs1 = offs2 = 0;
4385 if (tn->tn_left->tn_op == CON) {
4386 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4387 if (!constant_addr(tn->tn_right, &sym, &offs2))
4388 return false;
4389 } else if (tn->tn_right->tn_op == CON) {
4390 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4391 if (tn->tn_op == MINUS)
4392 offs2 = -offs2;
4393 if (!constant_addr(tn->tn_left, &sym, &offs1))
4394 return false;
4395 } else {
4396 return false;
4397 }
4398 *symp = sym;
4399 *offsp = offs1 + offs2;
4400 return true;
4401 case ADDR:
4402 if (tn->tn_left->tn_op == NAME) {
4403 *symp = tn->tn_left->tn_sym;
4404 *offsp = 0;
4405 return true;
4406 } else {
4407 /*
4408 * If this would be the front end of a compiler we
4409 * would return a label instead of 0, at least if
4410 * 'tn->tn_left->tn_op == STRING'.
4411 */
4412 *symp = NULL;
4413 *offsp = 0;
4414 return true;
4415 }
4416 case CVT:
4417 t = tn->tn_type->t_tspec;
4418 ot = tn->tn_left->tn_type->t_tspec;
4419 if ((!is_integer(t) && t != PTR) ||
4420 (!is_integer(ot) && ot != PTR)) {
4421 return false;
4422 }
4423 #if 0
4424 /*
4425 * consider:
4426 * struct foo {
4427 * unsigned char a;
4428 * } f = {
4429 * (unsigned char)(unsigned long)
4430 * (&(((struct foo *)0)->a))
4431 * };
4432 * since psize(unsigned long) != psize(unsigned char),
4433 * this fails.
4434 */
4435 else if (psize(t) != psize(ot))
4436 return -1;
4437 #endif
4438 return constant_addr(tn->tn_left, symp, offsp);
4439 default:
4440 return false;
4441 }
4442 }
4443
4444 /* Append s2 to s1, then free s2. */
4445 strg_t *
4446 cat_strings(strg_t *s1, strg_t *s2)
4447 {
4448
4449 if (s1->st_char != s2->st_char) {
4450 /* cannot concatenate wide and regular string literals */
4451 error(292);
4452 return s1;
4453 }
4454
4455 size_t len1 = s1->st_len;
4456 size_t len2 = s2->st_len;
4457 size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
4458 size_t size1 = len1 * chsize;
4459 size_t size2 = (len2 + 1) * chsize;
4460 s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
4461 memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
4462 free(s2->st_mem);
4463
4464 s1->st_len = len1 + len2;
4465 free(s2);
4466
4467 return s1;
4468 }
4469
4470 static bool
4471 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4472 {
4473
4474 if (op == SHL || op == SHR) {
4475 if (!lparen && (lop == PLUS || lop == MINUS))
4476 return true;
4477 if (!rparen && (rop == PLUS || rop == MINUS))
4478 return true;
4479 return false;
4480 }
4481
4482 if (op == LOGOR) {
4483 if (!lparen && lop == LOGAND)
4484 return true;
4485 if (!rparen && rop == LOGAND)
4486 return true;
4487 return false;
4488 }
4489
4490 lint_assert(op == BITAND || op == BITXOR || op == BITOR);
4491 if (!lparen && lop != op) {
4492 if (lop == PLUS || lop == MINUS)
4493 return true;
4494 if (lop == BITAND || lop == BITXOR)
4495 return true;
4496 }
4497 if (!rparen && rop != op) {
4498 if (rop == PLUS || rop == MINUS)
4499 return true;
4500 if (rop == BITAND || rop == BITXOR)
4501 return true;
4502 }
4503 return false;
4504 }
4505
4506 /*
4507 * Print a warning if the given node has operands which should be
4508 * parenthesized.
4509 *
4510 * XXX Does not work if an operand is a constant expression. Constant
4511 * expressions are already folded.
4512 */
4513 static void
4514 check_precedence_confusion(tnode_t *tn)
4515 {
4516 tnode_t *ln, *rn;
4517
4518 if (!hflag)
4519 return;
4520
4521 debug_node(tn);
4522
4523 lint_assert(is_binary(tn));
4524 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4525 continue;
4526 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4527 continue;
4528
4529 if (is_confusing_precedence(tn->tn_op,
4530 ln->tn_op, ln->tn_parenthesized,
4531 rn->tn_op, rn->tn_parenthesized)) {
4532 /* precedence confusion possible: parenthesize! */
4533 warning(169);
4534 }
4535 }
4536
4537 typedef struct stmt_expr {
4538 struct memory_block *se_mem;
4539 sym_t *se_sym;
4540 struct stmt_expr *se_enclosing;
4541 } stmt_expr;
4542
4543 static stmt_expr *stmt_exprs;
4544
4545 void
4546 begin_statement_expr(void)
4547 {
4548 stmt_expr *se = xmalloc(sizeof(*se));
4549 se->se_mem = expr_save_memory();
4550 se->se_sym = NULL;
4551 se->se_enclosing = stmt_exprs;
4552 stmt_exprs = se;
4553 }
4554
4555 void
4556 do_statement_expr(tnode_t *tn)
4557 {
4558 block_level--;
4559 mem_block_level--;
4560 stmt_exprs->se_sym = tn != NULL
4561 ? mktempsym(block_dup_type(tn->tn_type))
4562 : NULL; /* after a syntax error */
4563 mem_block_level++;
4564 block_level++;
4565 /* ({ }) is a GCC extension */
4566 gnuism(320);
4567 }
4568
4569 tnode_t *
4570 end_statement_expr(void)
4571 {
4572 stmt_expr *se = stmt_exprs;
4573 if (se->se_sym == NULL)
4574 return NULL; /* after a syntax error */
4575 tnode_t *tn = build_name(se->se_sym, false);
4576 (void)expr_save_memory(); /* leak */
4577 expr_restore_memory(se->se_mem);
4578 stmt_exprs = se->se_enclosing;
4579 free(se);
4580 return tn;
4581 }
4582