tree.c revision 1.426 1 /* $NetBSD: tree.c,v 1.426 2022/04/15 17:08:21 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.426 2022/04/15 17:08:21 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 (!is_member(sym))
390 continue;
391 if (sym->u.s_member.sm_sou_type != str)
392 continue;
393 if (strcmp(sym->s_name, msym->s_name) != 0)
394 continue;
395 return sym;
396 }
397 }
398
399 /*
400 * Set eq to false if there are struct/union members with the same
401 * name and different types and/or offsets.
402 */
403 eq = true;
404 for (csym = msym; csym != NULL; csym = csym->s_symtab_next) {
405 if (csym->s_scl != MOS && csym->s_scl != MOU)
406 continue;
407 if (strcmp(msym->s_name, csym->s_name) != 0)
408 continue;
409 for (sym = csym->s_symtab_next; sym != NULL;
410 sym = sym->s_symtab_next) {
411 bool w;
412
413 if (sym->s_scl != MOS && sym->s_scl != MOU)
414 continue;
415 if (strcmp(csym->s_name, sym->s_name) != 0)
416 continue;
417 if (csym->u.s_member.sm_offset_in_bits !=
418 sym->u.s_member.sm_offset_in_bits) {
419 eq = false;
420 break;
421 }
422 w = false;
423 eq = eqtype(csym->s_type, sym->s_type,
424 false, false, &w) && !w;
425 if (!eq)
426 break;
427 if (csym->s_bitfield != sym->s_bitfield) {
428 eq = false;
429 break;
430 }
431 if (csym->s_bitfield) {
432 type_t *tp1, *tp2;
433
434 tp1 = csym->s_type;
435 tp2 = sym->s_type;
436 if (tp1->t_flen != tp2->t_flen) {
437 eq = false;
438 break;
439 }
440 if (tp1->t_foffs != tp2->t_foffs) {
441 eq = false;
442 break;
443 }
444 }
445 }
446 if (!eq)
447 break;
448 }
449
450 /*
451 * Now handle the case in which the left operand refers really
452 * to a struct/union, but the right operand is not member of it.
453 */
454 if (str != NULL) {
455 if (eq && 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 && pflag && aflag > 0 &&
2202 portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
2203 is_uinteger(nt) != is_uinteger(ot)) {
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 if (Pflag && portable_size_in_bits(nt) > portable_size_in_bits(ot) &&
2214 (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
2215 tn->tn_op == SHL)) {
2216 /* suggest cast from '%s' to '%s' on op %s to ... */
2217 warning(324, type_name(gettyp(ot)), type_name(tp),
2218 op_name(tn->tn_op));
2219 }
2220
2221 if (aflag > 0 &&
2222 portable_size_in_bits(nt) < portable_size_in_bits(ot) &&
2223 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
2224 aflag > 1)) {
2225 if (op == FARG) {
2226 /* conversion from '%s' to '%s' may lose ... */
2227 warning(298,
2228 type_name(tn->tn_type), type_name(tp), arg);
2229 } else {
2230 /* conversion from '%s' to '%s' may lose accuracy */
2231 warning(132,
2232 type_name(tn->tn_type), type_name(tp));
2233 }
2234 }
2235 }
2236
2237 /*
2238 * Print warnings for dubious conversions of pointer to integer.
2239 */
2240 static void
2241 check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
2242 {
2243
2244 if (tn->tn_op == CON)
2245 return;
2246 if (op != CVT)
2247 return; /* We got already an error. */
2248 if (portable_size_in_bits(nt) >= portable_size_in_bits(PTR))
2249 return;
2250
2251 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
2252 /* conversion of pointer to '%s' may lose bits */
2253 warning(134, type_name(tp));
2254 } else {
2255 /* conversion of pointer to '%s' loses bits */
2256 warning(133, type_name(tp));
2257 }
2258 }
2259
2260 static bool
2261 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
2262 const type_t *ostp, tspec_t ost)
2263 {
2264 /*
2265 * Casting a pointer to 'struct S' to a pointer to another struct that
2266 * has 'struct S' as its first member is ok, see msg_247.c, 'struct
2267 * counter'.
2268 */
2269 if (nst == STRUCT && ost == STRUCT &&
2270 nstp->t_str->sou_first_member != NULL &&
2271 nstp->t_str->sou_first_member->s_type == ostp)
2272 return false;
2273
2274 if (is_incomplete(nstp) || is_incomplete(ostp))
2275 return false;
2276
2277 if ((nst == STRUCT || nst == UNION) && nstp->t_str != ostp->t_str)
2278 return true;
2279
2280 if (nst == CHAR || nst == UCHAR)
2281 return false; /* for the sake of traditional C code */
2282 if (ost == CHAR || ost == UCHAR)
2283 return false; /* for the sake of traditional C code */
2284
2285 return portable_size_in_bits(nst) != portable_size_in_bits(ost);
2286 }
2287
2288 /*
2289 * Warn about questionable pointer conversions.
2290 */
2291 static void
2292 check_pointer_conversion(tnode_t *tn, type_t *ntp)
2293 {
2294 const type_t *nstp, *otp, *ostp;
2295 tspec_t nst, ost;
2296 const char *nts, *ots;
2297
2298 nstp = ntp->t_subt;
2299 otp = tn->tn_type;
2300 ostp = otp->t_subt;
2301 nst = nstp->t_tspec;
2302 ost = ostp->t_tspec;
2303
2304 if (nst == VOID || ost == VOID) {
2305 if (sflag && (nst == FUNC || ost == FUNC)) {
2306 /* null pointers are already handled in convert() */
2307 *(nst == FUNC ? &nts : &ots) = "function pointer";
2308 *(nst == VOID ? &nts : &ots) = "'void *'";
2309 /* ANSI C forbids conversion of %s to %s */
2310 warning(303, ots, nts);
2311 }
2312 return;
2313 } else if (nst == FUNC && ost == FUNC) {
2314 return;
2315 } else if (nst == FUNC || ost == FUNC) {
2316 /* converting '%s' to '%s' is questionable */
2317 warning(229, type_name(otp), type_name(ntp));
2318 return;
2319 }
2320
2321 if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
2322 ost != CHAR && ost != UCHAR &&
2323 !is_incomplete(ostp)) {
2324 /* converting '%s' to '%s' may cause alignment problem */
2325 warning(135, type_name(otp), type_name(ntp));
2326 }
2327
2328 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
2329 /* pointer cast from '%s' to '%s' may be troublesome */
2330 warning(247, type_name(otp), type_name(ntp));
2331 }
2332 }
2333
2334 static void
2335 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
2336 tspec_t nt, val_t *v, val_t *nv)
2337 {
2338 ldbl_t max = 0.0, min = 0.0;
2339
2340 switch (nt) {
2341 case CHAR:
2342 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
2343 case UCHAR:
2344 max = TARG_UCHAR_MAX; min = 0; break;
2345 case SCHAR:
2346 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
2347 case SHORT:
2348 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
2349 case USHORT:
2350 max = TARG_USHRT_MAX; min = 0; break;
2351 case ENUM:
2352 case INT:
2353 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
2354 case UINT:
2355 max = TARG_UINT_MAX; min = 0; break;
2356 case LONG:
2357 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
2358 case ULONG:
2359 max = TARG_ULONG_MAX; min = 0; break;
2360 case QUAD:
2361 max = QUAD_MAX; min = QUAD_MIN; break;
2362 case UQUAD:
2363 max = UQUAD_MAX; min = 0; break;
2364 case FLOAT:
2365 case FCOMPLEX:
2366 max = FLT_MAX; min = -FLT_MAX; break;
2367 case DOUBLE:
2368 case DCOMPLEX:
2369 max = DBL_MAX; min = -DBL_MAX; break;
2370 case PTR:
2371 /* Got already an error because of float --> ptr */
2372 case LDOUBLE:
2373 case LCOMPLEX:
2374 /* LINTED 248 */
2375 max = LDBL_MAX; min = -max; break;
2376 default:
2377 lint_assert(/*CONSTCOND*/false);
2378 }
2379 if (v->v_ldbl > max || v->v_ldbl < min) {
2380 lint_assert(nt != LDOUBLE);
2381 if (op == FARG) {
2382 /* conversion of '%s' to '%s' is out of range, ... */
2383 warning(295,
2384 type_name(gettyp(ot)), type_name(tp), arg);
2385 } else {
2386 /* conversion of '%s' to '%s' is out of range */
2387 warning(119,
2388 type_name(gettyp(ot)), type_name(tp));
2389 }
2390 v->v_ldbl = v->v_ldbl > 0 ? max : min;
2391 }
2392
2393 if (nt == FLOAT) {
2394 nv->v_ldbl = (float)v->v_ldbl;
2395 } else if (nt == DOUBLE) {
2396 nv->v_ldbl = (double)v->v_ldbl;
2397 } else if (nt == LDOUBLE) {
2398 nv->v_ldbl = v->v_ldbl;
2399 } else {
2400 nv->v_quad = (int64_t)v->v_ldbl;
2401 }
2402 }
2403
2404 static bool
2405 convert_constant_to_floating(tspec_t nt, val_t *nv,
2406 tspec_t ot, const val_t *v)
2407 {
2408 if (nt == FLOAT) {
2409 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2410 (float)(uint64_t)v->v_quad : (float)v->v_quad;
2411 } else if (nt == DOUBLE) {
2412 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2413 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2414 } else if (nt == LDOUBLE) {
2415 nv->v_ldbl = (ot == PTR || is_uinteger(ot)) ?
2416 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2417 } else
2418 return false;
2419 return true;
2420 }
2421
2422 /*
2423 * Print a warning if bits which were set are lost due to the conversion.
2424 * This can happen with operator ORASS only.
2425 */
2426 static void
2427 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
2428 uint64_t xmask, op_t op)
2429 {
2430 if (nsz < osz && (v->v_quad & xmask) != 0) {
2431 /* constant truncated by conversion, op %s */
2432 warning(306, op_name(op));
2433 }
2434 }
2435
2436 /*
2437 * Print a warning if additional bits are not all 1
2438 * and the most significant bit of the old value is 1,
2439 * or if at least one (but not all) removed bit was 0.
2440 */
2441 static void
2442 convert_constant_check_range_bitand(size_t nsz, size_t osz,
2443 uint64_t xmask, const val_t *nv,
2444 tspec_t ot, const val_t *v,
2445 const type_t *tp, op_t op)
2446 {
2447 if (nsz > osz &&
2448 (nv->v_quad & bit((unsigned int)(osz - 1))) != 0 &&
2449 (nv->v_quad & xmask) != xmask) {
2450 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
2451 warning(309, type_name(gettyp(ot)),
2452 type_name(tp), op_name(op));
2453 } else if (nsz < osz &&
2454 (v->v_quad & xmask) != xmask &&
2455 (v->v_quad & xmask) != 0) {
2456 /* constant truncated by conversion, op %s */
2457 warning(306, op_name(op));
2458 }
2459 }
2460
2461 static void
2462 convert_constant_check_range_signed(op_t op, int arg)
2463 {
2464 if (op == ASSIGN) {
2465 /* assignment of negative constant to unsigned type */
2466 warning(164);
2467 } else if (op == INIT) {
2468 /* initialization of unsigned with negative constant */
2469 warning(221);
2470 } else if (op == FARG) {
2471 /* conversion of negative constant to unsigned type, ... */
2472 warning(296, arg);
2473 } else if (modtab[op].m_comparison) {
2474 /* handled by check_integer_comparison() */
2475 } else {
2476 /* conversion of negative constant to unsigned type */
2477 warning(222);
2478 }
2479 }
2480
2481 /*
2482 * Loss of significant bit(s). All truncated bits
2483 * of unsigned types or all truncated bits plus the
2484 * msb of the target for signed types are considered
2485 * to be significant bits. Loss of significant bits
2486 * means that at least one of the bits was set in an
2487 * unsigned type or that at least one but not all of
2488 * the bits was set in a signed type.
2489 * Loss of significant bits means that it is not
2490 * possible, also not with necessary casts, to convert
2491 * back to the original type. A example for a
2492 * necessary cast is:
2493 * char c; int i; c = 128;
2494 * i = c; ** yields -128 **
2495 * i = (unsigned char)c; ** yields 128 **
2496 */
2497 static void
2498 convert_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
2499 tspec_t ot)
2500 {
2501 if (op == ASSIGN && tp->t_bitfield) {
2502 /* precision lost in bit-field assignment */
2503 warning(166);
2504 } else if (op == ASSIGN) {
2505 /* constant truncated by assignment */
2506 warning(165);
2507 } else if (op == INIT && tp->t_bitfield) {
2508 /* bit-field initializer does not fit */
2509 warning(180);
2510 } else if (op == INIT) {
2511 /* initializer does not fit */
2512 warning(178);
2513 } else if (op == CASE) {
2514 /* case label affected by conversion */
2515 warning(196);
2516 } else if (op == FARG) {
2517 /* conversion of '%s' to '%s' is out of range, arg #%d */
2518 warning(295,
2519 type_name(gettyp(ot)), type_name(tp), arg);
2520 } else {
2521 /* conversion of '%s' to '%s' is out of range */
2522 warning(119,
2523 type_name(gettyp(ot)), type_name(tp));
2524 }
2525 }
2526
2527 static void
2528 convert_constant_check_range_loss(op_t op, int arg, const type_t *tp,
2529 tspec_t ot)
2530 {
2531 if (op == ASSIGN && tp->t_bitfield) {
2532 /* precision lost in bit-field assignment */
2533 warning(166);
2534 } else if (op == INIT && tp->t_bitfield) {
2535 /* bit-field initializer out of range */
2536 warning(11);
2537 } else if (op == CASE) {
2538 /* case label affected by conversion */
2539 warning(196);
2540 } else if (op == FARG) {
2541 /* conversion of '%s' to '%s' is out of range, arg #%d */
2542 warning(295,
2543 type_name(gettyp(ot)), type_name(tp), arg);
2544 } else {
2545 /* conversion of '%s' to '%s' is out of range */
2546 warning(119,
2547 type_name(gettyp(ot)), type_name(tp));
2548 }
2549 }
2550
2551 static void
2552 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
2553 op_t op, int arg, const val_t *v, val_t *nv)
2554 {
2555 unsigned int osz, nsz;
2556 uint64_t xmask, xmsk1;
2557
2558 osz = size_in_bits(ot);
2559 nsz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
2560 xmask = value_bits(nsz) ^ value_bits(osz);
2561 xmsk1 = value_bits(nsz) ^ value_bits(osz - 1);
2562 /*
2563 * For bitwise operations we are not interested in the
2564 * value, but in the bits itself.
2565 */
2566 if (op == ORASS || op == BITOR || op == BITXOR) {
2567 convert_constant_check_range_bitor(nsz, osz, v, xmask, op);
2568 } else if (op == ANDASS || op == BITAND) {
2569 convert_constant_check_range_bitand(nsz, osz, xmask, nv, ot,
2570 v, tp, op);
2571 } else if ((nt != PTR && is_uinteger(nt)) &&
2572 (ot != PTR && !is_uinteger(ot)) &&
2573 v->v_quad < 0) {
2574 convert_constant_check_range_signed(op, arg);
2575 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2576 (v->v_quad & xmask) != 0 &&
2577 (is_uinteger(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2578 convert_constant_check_range_truncated(op, arg, tp, ot);
2579 } else if (nv->v_quad != v->v_quad) {
2580 convert_constant_check_range_loss(op, arg, tp, ot);
2581 }
2582 }
2583
2584 /*
2585 * Converts a typed constant to a constant of another type.
2586 *
2587 * op operator which requires conversion
2588 * arg if op is FARG, # of argument
2589 * tp type in which to convert the constant
2590 * nv new constant
2591 * v old constant
2592 */
2593 void
2594 convert_constant(op_t op, int arg, const type_t *tp, val_t *nv, val_t *v)
2595 {
2596 tspec_t ot, nt;
2597 unsigned int sz;
2598 bool range_check;
2599
2600 /*
2601 * TODO: make 'v' const; the name of this function does not suggest
2602 * that it modifies 'v'.
2603 */
2604 ot = v->v_tspec;
2605 nt = nv->v_tspec = tp->t_tspec;
2606 range_check = false;
2607
2608 if (nt == BOOL) { /* C99 6.3.1.2 */
2609 nv->v_unsigned_since_c90 = false;
2610 nv->v_quad = is_nonzero_val(v) ? 1 : 0;
2611 return;
2612 }
2613
2614 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
2615 convert_constant_floating(op, arg, ot, tp, nt, v, nv);
2616 } else if (!convert_constant_to_floating(nt, nv, ot, v)) {
2617 range_check = true; /* Check for lost precision. */
2618 nv->v_quad = v->v_quad;
2619 }
2620
2621 if ((v->v_unsigned_since_c90 && is_floating(nt)) ||
2622 (v->v_unsigned_since_c90 && (is_integer(nt) && !is_uinteger(nt) &&
2623 portable_size_in_bits(nt) >
2624 portable_size_in_bits(ot)))) {
2625 /* ANSI C treats constant as unsigned */
2626 warning(157);
2627 v->v_unsigned_since_c90 = false;
2628 }
2629
2630 if (is_integer(nt)) {
2631 sz = tp->t_bitfield ? tp->t_flen : size_in_bits(nt);
2632 nv->v_quad = convert_integer(nv->v_quad, nt, sz);
2633 }
2634
2635 if (range_check && op != CVT)
2636 convert_constant_check_range(ot, tp, nt, op, arg, v, nv);
2637 }
2638
2639 /*
2640 * Called if incompatible types were detected.
2641 * Prints a appropriate warning.
2642 */
2643 static void
2644 warn_incompatible_types(op_t op,
2645 const type_t *ltp, tspec_t lt,
2646 const type_t *rtp, tspec_t rt)
2647 {
2648 const mod_t *mp;
2649
2650 mp = &modtab[op];
2651
2652 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2653 /* void type illegal in expression */
2654 error(109);
2655 } else if (op == ASSIGN) {
2656 if ((lt == STRUCT || lt == UNION) &&
2657 (rt == STRUCT || rt == UNION)) {
2658 /* assignment of different structures (%s != %s) */
2659 error(240, tspec_name(lt), tspec_name(rt));
2660 } else {
2661 /* cannot assign to '%s' from '%s' */
2662 error(171, type_name(ltp), type_name(rtp));
2663 }
2664 } else if (mp->m_binary) {
2665 /* operands of '%s' have incompatible types (%s != %s) */
2666 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2667 } else {
2668 lint_assert(rt == NOTSPEC);
2669 /* operand of '%s' has invalid type (%s) */
2670 error(108, mp->m_name, tspec_name(lt));
2671 }
2672 }
2673
2674 /*
2675 * Called if incompatible pointer types are detected.
2676 * Print an appropriate warning.
2677 */
2678 static void
2679 warn_incompatible_pointers(const mod_t *mp,
2680 const type_t *ltp, const type_t *rtp)
2681 {
2682 tspec_t lt, rt;
2683
2684 lint_assert(ltp->t_tspec == PTR);
2685 lint_assert(rtp->t_tspec == PTR);
2686
2687 lt = ltp->t_subt->t_tspec;
2688 rt = rtp->t_subt->t_tspec;
2689
2690 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2691 if (mp == NULL) {
2692 /* illegal structure pointer combination */
2693 warning(244);
2694 } else {
2695 /* incompatible structure pointers: '%s' '%s' '%s' */
2696 warning(245, type_name(ltp), mp->m_name, type_name(rtp));
2697 }
2698 } else {
2699 if (mp == NULL) {
2700 /* illegal combination of '%s' and '%s' */
2701 warning(184, type_name(ltp), type_name(rtp));
2702 } else {
2703 /* illegal combination of '%s' and '%s', op '%s' */
2704 warning(124,
2705 type_name(ltp), type_name(rtp), mp->m_name);
2706 }
2707 }
2708 }
2709
2710 /* Return a type based on tp1, with added qualifiers from tp2. */
2711 static type_t *
2712 merge_qualifiers(type_t *tp1, const type_t *tp2)
2713 {
2714 type_t *ntp, *nstp;
2715 bool c1, c2, v1, v2;
2716
2717 lint_assert(tp1->t_tspec == PTR);
2718 lint_assert(tp2->t_tspec == PTR);
2719
2720 c1 = tp1->t_subt->t_const;
2721 c2 = tp2->t_subt->t_const;
2722 v1 = tp1->t_subt->t_volatile;
2723 v2 = tp2->t_subt->t_volatile;
2724
2725 if (c1 == (c1 | c2) && v1 == (v1 | v2))
2726 return tp1;
2727
2728 nstp = expr_dup_type(tp1->t_subt);
2729 nstp->t_const |= c2;
2730 nstp->t_volatile |= v2;
2731
2732 ntp = expr_dup_type(tp1);
2733 ntp->t_subt = nstp;
2734 return ntp;
2735 }
2736
2737 /*
2738 * Returns true if the given structure or union has a constant member
2739 * (maybe recursively).
2740 */
2741 static bool
2742 has_constant_member(const type_t *tp)
2743 {
2744 sym_t *m;
2745
2746 lint_assert(is_struct_or_union(tp->t_tspec));
2747
2748 for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
2749 const type_t *mtp = m->s_type;
2750 if (mtp->t_const)
2751 return true;
2752 if (is_struct_or_union(mtp->t_tspec) &&
2753 has_constant_member(mtp))
2754 return true;
2755 }
2756 return false;
2757 }
2758
2759 /*
2760 * Create a new node for one of the operators POINT and ARROW.
2761 */
2762 static tnode_t *
2763 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
2764 {
2765 tnode_t *ntn, *ctn;
2766 bool nolval;
2767
2768 lint_assert(rn->tn_op == NAME);
2769 lint_assert(is_member(rn->tn_sym));
2770
2771 /*
2772 * Remember if the left operand is an lvalue (structure members
2773 * are lvalues if and only if the structure itself is an lvalue).
2774 */
2775 nolval = op == POINT && !ln->tn_lvalue;
2776
2777 if (op == POINT) {
2778 ln = build_address(sys, ln, true);
2779 } else if (ln->tn_type->t_tspec != PTR) {
2780 lint_assert(tflag);
2781 lint_assert(is_integer(ln->tn_type->t_tspec));
2782 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
2783 }
2784
2785 ctn = build_integer_constant(PTRDIFF_TSPEC,
2786 rn->tn_sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
2787
2788 ntn = new_tnode(PLUS, sys, expr_derive_type(rn->tn_type, PTR),
2789 ln, ctn);
2790 if (ln->tn_op == CON)
2791 ntn = fold(ntn);
2792
2793 if (rn->tn_type->t_bitfield) {
2794 ntn = new_tnode(FSEL, sys, ntn->tn_type->t_subt, ntn, NULL);
2795 } else {
2796 ntn = new_tnode(INDIR, sys, ntn->tn_type->t_subt, ntn, NULL);
2797 }
2798
2799 if (nolval)
2800 ntn->tn_lvalue = false;
2801
2802 return ntn;
2803 }
2804
2805 /*
2806 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2807 */
2808 static tnode_t *
2809 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
2810 {
2811 tnode_t *cn, *ntn;
2812
2813 lint_assert(ln != NULL);
2814
2815 if (ln->tn_type->t_tspec == PTR) {
2816 cn = plength(ln->tn_type);
2817 } else {
2818 cn = build_integer_constant(INT, (int64_t)1);
2819 }
2820 ntn = new_tnode(op, sys, ln->tn_type, ln, cn);
2821
2822 return ntn;
2823 }
2824
2825 /*
2826 * Create a node for REAL, IMAG
2827 */
2828 static tnode_t *
2829 build_real_imag(op_t op, bool sys, tnode_t *ln)
2830 {
2831 tnode_t *cn, *ntn;
2832
2833 lint_assert(ln != NULL);
2834
2835 if (ln->tn_op == NAME) {
2836 /*
2837 * This may be too much, but it avoids wrong warnings.
2838 * See d_c99_complex_split.c.
2839 */
2840 mark_as_used(ln->tn_sym, false, false);
2841 mark_as_set(ln->tn_sym);
2842 }
2843
2844 switch (ln->tn_type->t_tspec) {
2845 case LCOMPLEX:
2846 /* XXX: integer and LDOUBLE don't match. */
2847 cn = build_integer_constant(LDOUBLE, (int64_t)1);
2848 break;
2849 case DCOMPLEX:
2850 /* XXX: integer and DOUBLE don't match. */
2851 cn = build_integer_constant(DOUBLE, (int64_t)1);
2852 break;
2853 case FCOMPLEX:
2854 /* XXX: integer and FLOAT don't match. */
2855 cn = build_integer_constant(FLOAT, (int64_t)1);
2856 break;
2857 default:
2858 /* __%s__ is illegal for type %s */
2859 error(276, op == REAL ? "real" : "imag",
2860 type_name(ln->tn_type));
2861 return NULL;
2862 }
2863 ntn = new_tnode(op, sys, cn->tn_type, ln, cn);
2864 ntn->tn_lvalue = true;
2865
2866 return ntn;
2867 }
2868
2869 /*
2870 * Create a tree node for the unary & operator
2871 */
2872 static tnode_t *
2873 build_address(bool sys, tnode_t *tn, bool noign)
2874 {
2875 tspec_t t;
2876
2877 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2878 if (tflag)
2879 /* '&' before array or function: ignored */
2880 warning(127);
2881 return tn;
2882 }
2883
2884 /* eliminate &* */
2885 if (tn->tn_op == INDIR &&
2886 tn->tn_left->tn_type->t_tspec == PTR &&
2887 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2888 return tn->tn_left;
2889 }
2890
2891 return new_tnode(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
2892 tn, NULL);
2893 }
2894
2895 /*
2896 * Create a node for operators PLUS and MINUS.
2897 */
2898 static tnode_t *
2899 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
2900 {
2901 tnode_t *ntn, *ctn;
2902 type_t *tp;
2903
2904 /* If pointer and integer, then pointer to the lhs. */
2905 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
2906 ntn = ln;
2907 ln = rn;
2908 rn = ntn;
2909 }
2910
2911 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2912 lint_assert(is_integer(rn->tn_type->t_tspec));
2913
2914 check_ctype_macro_invocation(ln, rn);
2915 check_enum_array_index(ln, rn);
2916
2917 ctn = plength(ln->tn_type);
2918 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2919 rn = convert(NOOP, 0, ctn->tn_type, rn);
2920 rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn);
2921 if (rn->tn_left->tn_op == CON)
2922 rn = fold(rn);
2923 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
2924
2925 } else if (rn->tn_type->t_tspec == PTR) {
2926
2927 lint_assert(ln->tn_type->t_tspec == PTR);
2928 lint_assert(op == MINUS);
2929 tp = gettyp(PTRDIFF_TSPEC);
2930 ntn = new_tnode(op, sys, tp, ln, rn);
2931 if (ln->tn_op == CON && rn->tn_op == CON)
2932 ntn = fold(ntn);
2933 ctn = plength(ln->tn_type);
2934 balance(NOOP, &ntn, &ctn);
2935 ntn = new_tnode(DIV, sys, tp, ntn, ctn);
2936
2937 } else {
2938
2939 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
2940
2941 }
2942 return ntn;
2943 }
2944
2945 /*
2946 * Create a node for operators SHL and SHR.
2947 */
2948 static tnode_t *
2949 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
2950 {
2951 tspec_t t;
2952 tnode_t *ntn;
2953
2954 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2955 rn = convert(CVT, 0, gettyp(INT), rn);
2956 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
2957 return ntn;
2958 }
2959
2960 /*
2961 * Create a node for COLON.
2962 */
2963 static tnode_t *
2964 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
2965 {
2966 tspec_t lt, rt, pdt;
2967 type_t *tp;
2968 tnode_t *ntn;
2969
2970 lt = ln->tn_type->t_tspec;
2971 rt = rn->tn_type->t_tspec;
2972 pdt = PTRDIFF_TSPEC;
2973
2974 /*
2975 * Arithmetic types are balanced, all other type combinations
2976 * still need to be handled.
2977 */
2978 if (is_arithmetic(lt) && is_arithmetic(rt)) {
2979 tp = ln->tn_type;
2980 } else if (lt == BOOL && rt == BOOL) {
2981 tp = ln->tn_type;
2982 } else if (lt == VOID || rt == VOID) {
2983 tp = gettyp(VOID);
2984 } else if (lt == STRUCT || lt == UNION) {
2985 /* Both types must be identical. */
2986 lint_assert(rt == STRUCT || rt == UNION);
2987 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2988 if (is_incomplete(ln->tn_type)) {
2989 /* unknown operand size, op %s */
2990 error(138, op_name(COLON));
2991 return NULL;
2992 }
2993 tp = ln->tn_type;
2994 } else if (lt == PTR && is_integer(rt)) {
2995 if (rt != pdt) {
2996 rn = convert(NOOP, 0, gettyp(pdt), rn);
2997 rt = pdt;
2998 }
2999 tp = ln->tn_type;
3000 } else if (rt == PTR && is_integer(lt)) {
3001 if (lt != pdt) {
3002 ln = convert(NOOP, 0, gettyp(pdt), ln);
3003 lt = pdt;
3004 }
3005 tp = rn->tn_type;
3006 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
3007 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
3008 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
3009 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
3010 } else {
3011 /*
3012 * XXX For now we simply take the left type. This is
3013 * probably wrong, if one type contains a function prototype
3014 * and the other one, at the same place, only an old style
3015 * declaration.
3016 */
3017 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
3018 }
3019
3020 ntn = new_tnode(COLON, sys, tp, ln, rn);
3021
3022 return ntn;
3023 }
3024
3025 /*
3026 * Create a node for an assignment operator (both = and op= ).
3027 */
3028 static tnode_t *
3029 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
3030 {
3031 tspec_t lt, rt;
3032 tnode_t *ntn, *ctn;
3033
3034 lint_assert(ln != NULL);
3035 lint_assert(rn != NULL);
3036
3037 lt = ln->tn_type->t_tspec;
3038 rt = rn->tn_type->t_tspec;
3039
3040 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
3041 lint_assert(is_integer(rt));
3042 ctn = plength(ln->tn_type);
3043 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
3044 rn = convert(NOOP, 0, ctn->tn_type, rn);
3045 rn = new_tnode(MULT, sys, rn->tn_type, rn, ctn);
3046 if (rn->tn_left->tn_op == CON)
3047 rn = fold(rn);
3048 }
3049
3050 if ((op == ASSIGN || op == RETURN || op == INIT) &&
3051 (lt == STRUCT || rt == STRUCT)) {
3052 lint_assert(lt == rt);
3053 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
3054 if (is_incomplete(ln->tn_type)) {
3055 if (op == RETURN) {
3056 /* cannot return incomplete type */
3057 error(212);
3058 } else {
3059 /* unknown operand size, op %s */
3060 error(138, op_name(op));
3061 }
3062 return NULL;
3063 }
3064 }
3065
3066 if (op == SHLASS) {
3067 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
3068 if (hflag)
3069 /* semantics of '%s' change in ANSI C; ... */
3070 warning(118, "<<=");
3071 }
3072 } else if (op != SHRASS) {
3073 if (op == ASSIGN || lt != PTR) {
3074 if (lt != rt ||
3075 (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
3076 rn = convert(op, 0, ln->tn_type, rn);
3077 rt = lt;
3078 }
3079 }
3080 }
3081
3082 ntn = new_tnode(op, sys, ln->tn_type, ln, rn);
3083
3084 return ntn;
3085 }
3086
3087 /*
3088 * Get length_in_bits of type tp->t_subt, as a constant expression of type
3089 * ptrdiff_t as seen from the target platform.
3090 */
3091 static tnode_t *
3092 plength(type_t *tp)
3093 {
3094 int elem, elsz_in_bits;
3095
3096 lint_assert(tp->t_tspec == PTR);
3097 tp = tp->t_subt;
3098
3099 elem = 1;
3100 elsz_in_bits = 0;
3101
3102 while (tp->t_tspec == ARRAY) {
3103 elem *= tp->t_dim;
3104 tp = tp->t_subt;
3105 }
3106
3107 switch (tp->t_tspec) {
3108 case FUNC:
3109 /* pointer to function is not allowed here */
3110 error(110);
3111 break;
3112 case VOID:
3113 /* cannot do pointer arithmetic on operand of unknown size */
3114 gnuism(136);
3115 break;
3116 case STRUCT:
3117 case UNION:
3118 if ((elsz_in_bits = tp->t_str->sou_size_in_bits) == 0)
3119 /* cannot do pointer arithmetic on operand of ... */
3120 error(136);
3121 break;
3122 case ENUM:
3123 if (is_incomplete(tp)) {
3124 /* cannot do pointer arithmetic on operand of ... */
3125 warning(136);
3126 }
3127 /* FALLTHROUGH */
3128 default:
3129 if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0) {
3130 /* cannot do pointer arithmetic on operand of ... */
3131 error(136);
3132 } else {
3133 lint_assert(elsz_in_bits != -1);
3134 }
3135 break;
3136 }
3137
3138 if (elem == 0 && elsz_in_bits != 0) {
3139 /* cannot do pointer arithmetic on operand of unknown size */
3140 error(136);
3141 }
3142
3143 if (elsz_in_bits == 0)
3144 elsz_in_bits = CHAR_SIZE;
3145
3146 return build_integer_constant(PTRDIFF_TSPEC,
3147 (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
3148 }
3149
3150 /*
3151 * XXX
3152 * Note: There appear to be a number of bugs in detecting overflow in
3153 * this function. An audit and a set of proper regression tests are needed.
3154 * --Perry Metzger, Nov. 16, 2001
3155 */
3156 /*
3157 * Do only as much as necessary to compute constant expressions.
3158 * Called only if the operator allows folding and all operands are constants.
3159 */
3160 static tnode_t *
3161 fold(tnode_t *tn)
3162 {
3163 val_t *v;
3164 tspec_t t;
3165 bool utyp, ovfl;
3166 int64_t sl, sr = 0, q = 0, mask;
3167 uint64_t ul, ur = 0;
3168 tnode_t *cn;
3169
3170 v = xcalloc(1, sizeof(*v));
3171 v->v_tspec = tn->tn_type->t_tspec;
3172
3173 t = tn->tn_left->tn_type->t_tspec;
3174 utyp = !is_integer(t) || is_uinteger(t);
3175 ul = sl = tn->tn_left->tn_val->v_quad;
3176 if (is_binary(tn))
3177 ur = sr = tn->tn_right->tn_val->v_quad;
3178
3179 mask = value_bits(size_in_bits(t));
3180 ovfl = false;
3181
3182 switch (tn->tn_op) {
3183 case UPLUS:
3184 q = sl;
3185 break;
3186 case UMINUS:
3187 q = -sl;
3188 if (sl != 0 && msb(q, t) == msb(sl, t))
3189 ovfl = true;
3190 break;
3191 case COMPL:
3192 q = ~sl;
3193 break;
3194 case MULT:
3195 if (utyp) {
3196 q = ul * ur;
3197 if (q != (q & mask))
3198 ovfl = true;
3199 else if ((ul != 0) && ((q / ul) != ur))
3200 ovfl = true;
3201 } else {
3202 q = sl * sr;
3203 if (msb(q, t) != (msb(sl, t) ^ msb(sr, t)))
3204 ovfl = true;
3205 }
3206 break;
3207 case DIV:
3208 if (sr == 0) {
3209 /* division by 0 */
3210 error(139);
3211 q = utyp ? -1 : INT64_MAX;
3212 } else {
3213 q = utyp ? (int64_t)(ul / ur) : sl / sr;
3214 }
3215 break;
3216 case MOD:
3217 if (sr == 0) {
3218 /* modulus by 0 */
3219 error(140);
3220 q = 0;
3221 } else {
3222 q = utyp ? (int64_t)(ul % ur) : sl % sr;
3223 }
3224 break;
3225 case PLUS:
3226 q = utyp ? (int64_t)(ul + ur) : sl + sr;
3227 if (msb(sl, t) && msb(sr, t) && !msb(q, t))
3228 ovfl = true;
3229 if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(q, t))
3230 ovfl = true;
3231 break;
3232 case MINUS:
3233 q = utyp ? (int64_t)(ul - ur) : sl - sr;
3234 if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(q, t))
3235 ovfl = true;
3236 if (!msb(sl, t) && msb(sr, t) && msb(q, t))
3237 ovfl = true;
3238 break;
3239 case SHL:
3240 q = utyp ? (int64_t)(ul << sr) : sl << sr;
3241 break;
3242 case SHR:
3243 /*
3244 * The sign must be explicitly extended because
3245 * shifts of signed values are implementation dependent.
3246 */
3247 q = ul >> sr;
3248 q = convert_integer(q, t, size_in_bits(t) - (int)sr);
3249 break;
3250 case LT:
3251 q = (utyp ? ul < ur : sl < sr) ? 1 : 0;
3252 break;
3253 case LE:
3254 q = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
3255 break;
3256 case GE:
3257 q = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
3258 break;
3259 case GT:
3260 q = (utyp ? ul > ur : sl > sr) ? 1 : 0;
3261 break;
3262 case EQ:
3263 q = (utyp ? ul == ur : sl == sr) ? 1 : 0;
3264 break;
3265 case NE:
3266 q = (utyp ? ul != ur : sl != sr) ? 1 : 0;
3267 break;
3268 case BITAND:
3269 q = utyp ? (int64_t)(ul & ur) : sl & sr;
3270 break;
3271 case BITXOR:
3272 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
3273 break;
3274 case BITOR:
3275 q = utyp ? (int64_t)(ul | ur) : sl | sr;
3276 break;
3277 default:
3278 lint_assert(/*CONSTCOND*/false);
3279 }
3280
3281 /* XXX does not work for quads. */
3282 if (ovfl ||
3283 ((uint64_t)(q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
3284 if (hflag)
3285 /* integer overflow detected, op '%s' */
3286 warning(141, op_name(tn->tn_op));
3287 }
3288
3289 v->v_quad = convert_integer(q, t, 0);
3290
3291 cn = build_constant(tn->tn_type, v);
3292 if (tn->tn_left->tn_system_dependent)
3293 cn->tn_system_dependent = true;
3294 if (is_binary(tn) && tn->tn_right->tn_system_dependent)
3295 cn->tn_system_dependent = true;
3296
3297 return cn;
3298 }
3299
3300 /*
3301 * Fold constant nodes, as much as is needed for comparing the value with 0
3302 * (test context, for controlling expressions).
3303 */
3304 static tnode_t *
3305 fold_test(tnode_t *tn)
3306 {
3307 bool l, r;
3308 val_t *v;
3309
3310 v = xcalloc(1, sizeof(*v));
3311 v->v_tspec = tn->tn_type->t_tspec;
3312 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
3313
3314 l = constant_is_nonzero(tn->tn_left);
3315 r = is_binary(tn) && constant_is_nonzero(tn->tn_right);
3316
3317 switch (tn->tn_op) {
3318 case NOT:
3319 if (hflag && !constcond_flag)
3320 /* constant argument to '!' */
3321 warning(239);
3322 v->v_quad = !l ? 1 : 0;
3323 break;
3324 case LOGAND:
3325 v->v_quad = l && r ? 1 : 0;
3326 break;
3327 case LOGOR:
3328 v->v_quad = l || r ? 1 : 0;
3329 break;
3330 default:
3331 lint_assert(/*CONSTCOND*/false);
3332 }
3333
3334 return build_constant(tn->tn_type, v);
3335 }
3336
3337 static ldbl_t
3338 floating_error_value(tspec_t t, ldbl_t lv)
3339 {
3340 if (t == FLOAT) {
3341 return lv < 0 ? -FLT_MAX : FLT_MAX;
3342 } else if (t == DOUBLE) {
3343 return lv < 0 ? -DBL_MAX : DBL_MAX;
3344 } else {
3345 /* LINTED 248: floating-point constant out of range */
3346 ldbl_t max = LDBL_MAX;
3347 return lv < 0 ? -max : max;
3348 }
3349 }
3350
3351 /*
3352 * Fold constant nodes having operands with floating point type.
3353 */
3354 static tnode_t *
3355 fold_float(tnode_t *tn)
3356 {
3357 val_t *v;
3358 tspec_t t;
3359 ldbl_t lv, rv = 0;
3360
3361 fpe = 0;
3362 v = xcalloc(1, sizeof(*v));
3363 v->v_tspec = t = tn->tn_type->t_tspec;
3364
3365 lint_assert(is_floating(t));
3366 lint_assert(t == tn->tn_left->tn_type->t_tspec);
3367 lint_assert(!is_binary(tn) || t == tn->tn_right->tn_type->t_tspec);
3368
3369 lv = tn->tn_left->tn_val->v_ldbl;
3370 if (is_binary(tn))
3371 rv = tn->tn_right->tn_val->v_ldbl;
3372
3373 switch (tn->tn_op) {
3374 case UPLUS:
3375 v->v_ldbl = lv;
3376 break;
3377 case UMINUS:
3378 v->v_ldbl = -lv;
3379 break;
3380 case MULT:
3381 v->v_ldbl = lv * rv;
3382 break;
3383 case DIV:
3384 if (rv == 0.0) {
3385 /* division by 0 */
3386 error(139);
3387 v->v_ldbl = floating_error_value(t, lv);
3388 } else {
3389 v->v_ldbl = lv / rv;
3390 }
3391 break;
3392 case PLUS:
3393 v->v_ldbl = lv + rv;
3394 break;
3395 case MINUS:
3396 v->v_ldbl = lv - rv;
3397 break;
3398 case LT:
3399 v->v_quad = lv < rv ? 1 : 0;
3400 break;
3401 case LE:
3402 v->v_quad = lv <= rv ? 1 : 0;
3403 break;
3404 case GE:
3405 v->v_quad = lv >= rv ? 1 : 0;
3406 break;
3407 case GT:
3408 v->v_quad = lv > rv ? 1 : 0;
3409 break;
3410 case EQ:
3411 v->v_quad = lv == rv ? 1 : 0;
3412 break;
3413 case NE:
3414 v->v_quad = lv != rv ? 1 : 0;
3415 break;
3416 default:
3417 lint_assert(/*CONSTCOND*/false);
3418 }
3419
3420 lint_assert(fpe != 0 || isnan((double)v->v_ldbl) == 0);
3421 if (fpe != 0 || isfinite((double)v->v_ldbl) == 0 ||
3422 (t == FLOAT &&
3423 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3424 (t == DOUBLE &&
3425 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3426 /* floating point overflow detected, op %s */
3427 warning(142, op_name(tn->tn_op));
3428 v->v_ldbl = floating_error_value(t, v->v_ldbl);
3429 fpe = 0;
3430 }
3431
3432 return build_constant(tn->tn_type, v);
3433 }
3434
3435
3436 /*
3437 * Create a constant node for sizeof.
3438 */
3439 tnode_t *
3440 build_sizeof(const type_t *tp)
3441 {
3442 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3443 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
3444 tn->tn_system_dependent = true;
3445 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
3446 return tn;
3447 }
3448
3449 /*
3450 * Create a constant node for offsetof.
3451 */
3452 /* ARGSUSED */ /* See implementation comments. */
3453 tnode_t *
3454 build_offsetof(const type_t *tp, const sym_t *sym)
3455 {
3456 unsigned int offset_in_bytes;
3457 tnode_t *tn;
3458
3459 if (!is_struct_or_union(tp->t_tspec))
3460 /* unacceptable operand of '%s' */
3461 error(111, "offsetof");
3462
3463 /* XXX: wrong size, no checking for sym fixme */
3464 offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3465 tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
3466 tn->tn_system_dependent = true;
3467 return tn;
3468 }
3469
3470 unsigned int
3471 type_size_in_bits(const type_t *tp)
3472 {
3473 unsigned int elem, elsz;
3474 bool flex;
3475
3476 elem = 1;
3477 flex = false;
3478 lint_assert(tp != NULL);
3479 while (tp->t_tspec == ARRAY) {
3480 flex = true; /* allow c99 flex arrays [] [0] */
3481 elem *= tp->t_dim;
3482 tp = tp->t_subt;
3483 }
3484 if (elem == 0) {
3485 if (!flex) {
3486 /* cannot take size/alignment of incomplete type */
3487 error(143);
3488 elem = 1;
3489 }
3490 }
3491 switch (tp->t_tspec) {
3492 case FUNC:
3493 /* cannot take size/alignment of function type '%s' */
3494 error(144, type_name(tp));
3495 elsz = 1;
3496 break;
3497 case STRUCT:
3498 case UNION:
3499 if (is_incomplete(tp)) {
3500 /* cannot take size/alignment of incomplete type */
3501 error(143);
3502 elsz = 1;
3503 } else {
3504 elsz = tp->t_str->sou_size_in_bits;
3505 }
3506 break;
3507 case ENUM:
3508 if (is_incomplete(tp)) {
3509 /* cannot take size/alignment of incomplete type */
3510 warning(143);
3511 }
3512 /* FALLTHROUGH */
3513 default:
3514 if (tp->t_bitfield) {
3515 /* cannot take size/alignment of bit-field */
3516 error(145);
3517 }
3518 if (tp->t_tspec == VOID) {
3519 /* cannot take size/alignment of void */
3520 error(146);
3521 elsz = 1;
3522 } else {
3523 elsz = size_in_bits(tp->t_tspec);
3524 lint_assert(elsz > 0);
3525 }
3526 break;
3527 }
3528
3529 return elem * elsz;
3530 }
3531
3532 tnode_t *
3533 build_alignof(const type_t *tp)
3534 {
3535 switch (tp->t_tspec) {
3536 case ARRAY:
3537 break;
3538
3539 case FUNC:
3540 /* cannot take size/alignment of function type '%s' */
3541 error(144, type_name(tp));
3542 return 0;
3543
3544 case STRUCT:
3545 case UNION:
3546 if (is_incomplete(tp)) {
3547 /* cannot take size/alignment of incomplete type */
3548 error(143);
3549 return 0;
3550 }
3551 break;
3552 case ENUM:
3553 break;
3554 default:
3555 if (tp->t_bitfield) {
3556 /* cannot take size/alignment of bit-field */
3557 error(145);
3558 return 0;
3559 }
3560 if (tp->t_tspec == VOID) {
3561 /* cannot take size/alignment of void */
3562 error(146);
3563 return 0;
3564 }
3565 break;
3566 }
3567
3568 return build_integer_constant(SIZEOF_TSPEC,
3569 (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
3570 }
3571
3572 /*
3573 * Type casts.
3574 */
3575 tnode_t *
3576 cast(tnode_t *tn, type_t *tp)
3577 {
3578 tspec_t nt, ot;
3579
3580 if (tn == NULL)
3581 return NULL;
3582
3583 tn = cconv(tn);
3584
3585 lint_assert(tp != NULL);
3586 nt = tp->t_tspec;
3587 ot = tn->tn_type->t_tspec;
3588
3589 if (nt == VOID) {
3590 /*
3591 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3592 * But this seems really questionable.
3593 */
3594 } else if (nt == UNION) {
3595 sym_t *m;
3596 struct_or_union *str = tp->t_str;
3597 if (!gflag) {
3598 /* union cast is a GCC extension */
3599 error(328);
3600 return NULL;
3601 }
3602 for (m = str->sou_first_member; m != NULL; m = m->s_next) {
3603 if (eqtype(m->s_type, tn->tn_type,
3604 false, false, NULL)) {
3605 tn = expr_alloc_tnode();
3606 tn->tn_op = CVT;
3607 tn->tn_type = tp;
3608 tn->tn_cast = true;
3609 tn->tn_right = NULL;
3610 return tn;
3611 }
3612 }
3613 /* type '%s' is not a member of '%s' */
3614 error(329, type_name(tn->tn_type), type_name(tp));
3615 return NULL;
3616 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3617 /* Casting to a struct is an undocumented GCC extension. */
3618 if (!(gflag && nt == STRUCT))
3619 goto invalid_cast;
3620 } else if (ot == STRUCT || ot == UNION) {
3621 goto invalid_cast;
3622 } else if (ot == VOID) {
3623 /* improper cast of void expression */
3624 error(148);
3625 return NULL;
3626 } else if (is_integer(nt) && is_scalar(ot)) {
3627 /* ok */
3628 } else if (is_floating(nt) && is_arithmetic(ot)) {
3629 /* ok */
3630 } else if (nt == PTR && is_integer(ot)) {
3631 /* ok */
3632 } else if (nt == PTR && ot == PTR) {
3633 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3634 if (hflag)
3635 /* cast discards 'const' from type '%s' */
3636 warning(275, type_name(tn->tn_type));
3637 }
3638 } else
3639 goto invalid_cast;
3640
3641 tn = convert(CVT, 0, tp, tn);
3642 tn->tn_cast = true;
3643
3644 return tn;
3645
3646 invalid_cast:
3647 /* invalid cast from '%s' to '%s' */
3648 error(147, type_name(tn->tn_type), type_name(tp));
3649 return NULL;
3650 }
3651
3652 /*
3653 * Create the node for a function argument.
3654 * All necessary conversions and type checks are done in
3655 * build_function_call because build_function_argument has no
3656 * information about expected argument types.
3657 */
3658 tnode_t *
3659 build_function_argument(tnode_t *args, tnode_t *arg)
3660 {
3661 tnode_t *ntn;
3662
3663 /*
3664 * If there was a serious error in the expression for the argument,
3665 * create a dummy argument so the positions of the remaining arguments
3666 * will not change.
3667 */
3668 if (arg == NULL)
3669 arg = build_integer_constant(INT, 0);
3670
3671 ntn = new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args);
3672
3673 return ntn;
3674 }
3675
3676 /*
3677 * Create the node for a function call. Also check types of
3678 * function arguments and insert conversions, if necessary.
3679 */
3680 tnode_t *
3681 build_function_call(tnode_t *func, bool sys, tnode_t *args)
3682 {
3683 tnode_t *ntn;
3684 op_t fcop;
3685
3686 if (func == NULL)
3687 return NULL;
3688
3689 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3690 fcop = CALL;
3691 } else {
3692 fcop = ICALL;
3693 }
3694
3695 check_ctype_function_call(func, args);
3696
3697 /*
3698 * after cconv() func will always be a pointer to a function
3699 * if it is a valid function designator.
3700 */
3701 func = cconv(func);
3702
3703 if (func->tn_type->t_tspec != PTR ||
3704 func->tn_type->t_subt->t_tspec != FUNC) {
3705 /* illegal function (type %s) */
3706 error(149, type_name(func->tn_type));
3707 return NULL;
3708 }
3709
3710 args = check_function_arguments(func->tn_type->t_subt, args);
3711
3712 ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
3713
3714 return ntn;
3715 }
3716
3717 /*
3718 * Check types of all function arguments and insert conversions,
3719 * if necessary.
3720 */
3721 static tnode_t *
3722 check_function_arguments(type_t *ftp, tnode_t *args)
3723 {
3724 tnode_t *arg;
3725 sym_t *asym;
3726 tspec_t at;
3727 int narg, npar, n, i;
3728
3729 /* get # of args in the prototype */
3730 npar = 0;
3731 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3732 npar++;
3733
3734 /* get # of args in function call */
3735 narg = 0;
3736 for (arg = args; arg != NULL; arg = arg->tn_right)
3737 narg++;
3738
3739 asym = ftp->t_args;
3740 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3741 /* argument mismatch: %d arg%s passed, %d expected */
3742 error(150, narg, narg > 1 ? "s" : "", npar);
3743 asym = NULL;
3744 }
3745
3746 for (n = 1; n <= narg; n++) {
3747
3748 /*
3749 * The rightmost argument is at the top of the argument
3750 * subtree.
3751 */
3752 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3753 continue;
3754
3755 /* some things which are always not allowed */
3756 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3757 /* void expressions may not be arguments, arg #%d */
3758 error(151, n);
3759 return NULL;
3760 } else if ((at == STRUCT || at == UNION) &&
3761 is_incomplete(arg->tn_left->tn_type)) {
3762 /* argument cannot have unknown size, arg #%d */
3763 error(152, n);
3764 return NULL;
3765 } else if (is_integer(at) &&
3766 arg->tn_left->tn_type->t_is_enum &&
3767 is_incomplete(arg->tn_left->tn_type)) {
3768 /* argument cannot have unknown size, arg #%d */
3769 warning(152, n);
3770 }
3771
3772 /* class conversions (arg in value context) */
3773 arg->tn_left = cconv(arg->tn_left);
3774
3775 if (asym != NULL) {
3776 arg->tn_left = check_prototype_argument(
3777 n, asym->s_type, arg->tn_left);
3778 } else {
3779 arg->tn_left = promote(NOOP, true, arg->tn_left);
3780 }
3781 arg->tn_type = arg->tn_left->tn_type;
3782
3783 if (asym != NULL)
3784 asym = asym->s_next;
3785 }
3786
3787 return args;
3788 }
3789
3790 /*
3791 * Compare the type of an argument with the corresponding type of a
3792 * prototype parameter. If it is a valid combination, but both types
3793 * are not the same, insert a conversion to convert the argument into
3794 * the type of the parameter.
3795 */
3796 static tnode_t *
3797 check_prototype_argument(
3798 int n, /* pos of arg */
3799 type_t *tp, /* expected type (from prototype) */
3800 tnode_t *tn) /* argument */
3801 {
3802 tnode_t *ln;
3803 bool dowarn;
3804
3805 ln = xcalloc(1, sizeof(*ln));
3806 ln->tn_type = expr_unqualified_type(tp);
3807 ln->tn_lvalue = true;
3808 if (typeok(FARG, n, ln, tn)) {
3809 if (!eqtype(tp, tn->tn_type,
3810 true, false, (dowarn = false, &dowarn)) || dowarn)
3811 tn = convert(FARG, n, tp, tn);
3812 }
3813 free(ln);
3814 return tn;
3815 }
3816
3817 /*
3818 * Return the value of an integral constant expression.
3819 * If the expression is not constant or its type is not an integer
3820 * type, an error message is printed.
3821 */
3822 val_t *
3823 constant(tnode_t *tn, bool required)
3824 {
3825 val_t *v;
3826
3827 if (tn != NULL)
3828 tn = cconv(tn);
3829 if (tn != NULL)
3830 tn = promote(NOOP, false, tn);
3831
3832 v = xcalloc(1, sizeof(*v));
3833
3834 if (tn == NULL) {
3835 lint_assert(nerr != 0);
3836 debug_step("constant node is null; returning 1 instead");
3837 v->v_tspec = INT;
3838 v->v_quad = 1;
3839 return v;
3840 }
3841
3842 v->v_tspec = tn->tn_type->t_tspec;
3843
3844 if (tn->tn_op == CON) {
3845 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3846 if (is_integer(tn->tn_val->v_tspec)) {
3847 v->v_unsigned_since_c90 =
3848 tn->tn_val->v_unsigned_since_c90;
3849 v->v_quad = tn->tn_val->v_quad;
3850 return v;
3851 }
3852 v->v_quad = tn->tn_val->v_ldbl;
3853 } else {
3854 v->v_quad = 1;
3855 }
3856
3857 if (required)
3858 /* integral constant expression expected */
3859 error(55);
3860 else
3861 /* variable array dimension is a C99/GCC extension */
3862 c99ism(318);
3863
3864 if (!is_integer(v->v_tspec))
3865 v->v_tspec = INT;
3866
3867 return v;
3868 }
3869
3870 static bool
3871 is_constcond_false(const tnode_t *tn, tspec_t t)
3872 {
3873 return (t == BOOL || t == INT) &&
3874 tn->tn_op == CON && tn->tn_val->v_quad == 0;
3875 }
3876
3877 /*
3878 * Perform some tests on expressions which can't be done in build_binary()
3879 * and functions called by build_binary(). These tests must be done here
3880 * because we need some information about the context in which the operations
3881 * are performed.
3882 * After all tests are performed and dofreeblk is true, expr() frees the
3883 * memory which is used for the expression.
3884 */
3885 void
3886 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool is_do_while)
3887 {
3888
3889 if (tn == NULL) { /* in case of errors */
3890 expr_free_all();
3891 return;
3892 }
3893
3894 /* expr() is also called in global initializations */
3895 if (dcs->d_kind != DK_EXTERN && !is_do_while)
3896 check_statement_reachable();
3897
3898 check_expr_misc(tn, vctx, tctx, !tctx, false, false, false);
3899 if (tn->tn_op == ASSIGN) {
3900 if (hflag && tctx)
3901 /* assignment in conditional context */
3902 warning(159);
3903 } else if (tn->tn_op == CON) {
3904 if (hflag && tctx && !constcond_flag &&
3905 !tn->tn_system_dependent &&
3906 !(is_do_while &&
3907 is_constcond_false(tn, tn->tn_type->t_tspec)))
3908 /* constant in conditional context */
3909 warning(161);
3910 }
3911 if (!modtab[tn->tn_op].m_has_side_effect) {
3912 /*
3913 * for left operands of COMMA this warning is already
3914 * printed
3915 */
3916 if (tn->tn_op != COMMA && !vctx && !tctx)
3917 check_null_effect(tn);
3918 }
3919 debug_node(tn);
3920
3921 /* free the tree memory */
3922 if (dofreeblk)
3923 expr_free_all();
3924 }
3925
3926 static bool
3927 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
3928 {
3929 op_t op = tn->tn_op;
3930
3931 if (modtab[op].m_has_side_effect)
3932 return true;
3933
3934 if (op == CVT && tn->tn_type->t_tspec == VOID)
3935 return has_side_effect(tn->tn_left);
3936
3937 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3938 if (op == LOGAND || op == LOGOR)
3939 return has_side_effect(tn->tn_right);
3940
3941 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3942 if (op == QUEST)
3943 return has_side_effect(tn->tn_right);
3944
3945 if (op == COLON || op == COMMA) {
3946 return has_side_effect(tn->tn_left) ||
3947 has_side_effect(tn->tn_right);
3948 }
3949
3950 return false;
3951 }
3952
3953 static bool
3954 is_void_cast(const tnode_t *tn)
3955 {
3956
3957 return tn->tn_op == CVT && tn->tn_cast &&
3958 tn->tn_type->t_tspec == VOID;
3959 }
3960
3961 static bool
3962 is_local_symbol(const tnode_t *tn)
3963 {
3964
3965 return tn->tn_op == LOAD &&
3966 tn->tn_left->tn_op == NAME &&
3967 tn->tn_left->tn_sym->s_scl == AUTO;
3968 }
3969
3970 static bool
3971 is_int_constant_zero(const tnode_t *tn)
3972 {
3973
3974 return tn->tn_op == CON &&
3975 tn->tn_type->t_tspec == INT &&
3976 tn->tn_val->v_quad == 0;
3977 }
3978
3979 static void
3980 check_null_effect(const tnode_t *tn)
3981 {
3982
3983 if (!hflag)
3984 return;
3985 if (has_side_effect(tn))
3986 return;
3987 if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
3988 return;
3989 if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
3990 return;
3991
3992 /* expression has null effect */
3993 warning(129);
3994 }
3995
3996 static void
3997 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
3998 {
3999 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4000 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4001 if (!szof)
4002 mark_as_set(ln->tn_sym);
4003 mark_as_used(ln->tn_sym, fcall, szof);
4004 }
4005 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4006 /* check the range of array indices */
4007 check_array_index(ln->tn_left, true);
4008 }
4009
4010 static void
4011 check_expr_load(const tnode_t *ln)
4012 {
4013 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4014 /* check the range of array indices */
4015 check_array_index(ln->tn_left, false);
4016 }
4017
4018 static void
4019 check_expr_side_effect(const tnode_t *ln, bool szof)
4020 {
4021 scl_t sc;
4022 dinfo_t *di;
4023
4024 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4025 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4026 sc = ln->tn_sym->s_scl;
4027 /*
4028 * Look if there was a asm statement in one of the
4029 * compound statements we are in. If not, we don't
4030 * print a warning.
4031 */
4032 for (di = dcs; di != NULL; di = di->d_enclosing) {
4033 if (di->d_asm)
4034 break;
4035 }
4036 if (sc != EXTERN && sc != STATIC &&
4037 !ln->tn_sym->s_set && !szof && di == NULL) {
4038 /* %s may be used before set */
4039 warning(158, ln->tn_sym->s_name);
4040 mark_as_set(ln->tn_sym);
4041 }
4042 mark_as_used(ln->tn_sym, false, false);
4043 }
4044 }
4045
4046 static void
4047 check_expr_assign(const tnode_t *ln, bool szof)
4048 {
4049 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4050 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4051 mark_as_set(ln->tn_sym);
4052 if (ln->tn_sym->s_scl == EXTERN)
4053 outusg(ln->tn_sym);
4054 }
4055 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4056 /* check the range of array indices */
4057 check_array_index(ln->tn_left, false);
4058 }
4059
4060 static void
4061 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4062 bool szof, bool vctx, bool tctx, bool retval_discarded)
4063 {
4064 lint_assert(ln->tn_op == ADDR);
4065 lint_assert(ln->tn_left->tn_op == NAME);
4066 if (!szof &&
4067 !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
4068 outcall(tn, vctx || tctx, retval_discarded);
4069 }
4070
4071 static bool
4072 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
4073 bool szof, bool fcall, bool vctx, bool tctx,
4074 bool retval_discarded, bool eqwarn)
4075 {
4076 switch (op) {
4077 case ADDR:
4078 check_expr_addr(ln, szof, fcall);
4079 break;
4080 case LOAD:
4081 check_expr_load(ln);
4082 /* FALLTHROUGH */
4083 case PUSH:
4084 case INCBEF:
4085 case DECBEF:
4086 case INCAFT:
4087 case DECAFT:
4088 case ADDASS:
4089 case SUBASS:
4090 case MULASS:
4091 case DIVASS:
4092 case MODASS:
4093 case ANDASS:
4094 case ORASS:
4095 case XORASS:
4096 case SHLASS:
4097 case SHRASS:
4098 case REAL:
4099 case IMAG:
4100 check_expr_side_effect(ln, szof);
4101 break;
4102 case ASSIGN:
4103 check_expr_assign(ln, szof);
4104 break;
4105 case CALL:
4106 check_expr_call(tn, ln, szof, vctx, tctx, retval_discarded);
4107 break;
4108 case EQ:
4109 if (hflag && eqwarn)
4110 /* operator '==' found where '=' was expected */
4111 warning(160);
4112 break;
4113 case CON:
4114 case NAME:
4115 case STRING:
4116 return false;
4117 /* LINTED206: (enumeration values not handled in switch) */
4118 case BITOR:
4119 case BITXOR:
4120 case NE:
4121 case GE:
4122 case GT:
4123 case LE:
4124 case LT:
4125 case SHR:
4126 case SHL:
4127 case MINUS:
4128 case PLUS:
4129 case MOD:
4130 case DIV:
4131 case MULT:
4132 case INDIR:
4133 case UMINUS:
4134 case UPLUS:
4135 case DEC:
4136 case INC:
4137 case COMPL:
4138 case NOT:
4139 case POINT:
4140 case ARROW:
4141 case NOOP:
4142 case BITAND:
4143 case FARG:
4144 case CASE:
4145 case INIT:
4146 case RETURN:
4147 case ICALL:
4148 case CVT:
4149 case COMMA:
4150 case FSEL:
4151 case COLON:
4152 case QUEST:
4153 case LOGOR:
4154 case LOGAND:
4155 break;
4156 }
4157 return true;
4158 }
4159
4160 void
4161 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
4162 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4163 {
4164 tnode_t *ln, *rn;
4165 const mod_t *mp;
4166 op_t op;
4167 bool cvctx, ctctx, eq, discard;
4168
4169 if (tn == NULL)
4170 return;
4171
4172 ln = tn->tn_left;
4173 rn = tn->tn_right;
4174 mp = &modtab[op = tn->tn_op];
4175
4176 if (!check_expr_op(tn, op, ln,
4177 szof, fcall, vctx, tctx, retval_discarded, eqwarn))
4178 return;
4179
4180 cvctx = mp->m_left_value_context;
4181 ctctx = mp->m_left_test_context;
4182 eq = mp->m_warn_if_operand_eq &&
4183 !ln->tn_parenthesized &&
4184 rn != NULL && !rn->tn_parenthesized;
4185
4186 /*
4187 * values of operands of ':' are not used if the type of at least
4188 * one of the operands (for gcc compatibility) is void
4189 * XXX test/value context of QUEST should probably be used as
4190 * context for both operands of COLON
4191 */
4192 if (op == COLON && tn->tn_type->t_tspec == VOID)
4193 cvctx = ctctx = false;
4194 discard = op == CVT && tn->tn_type->t_tspec == VOID;
4195 check_expr_misc(ln, cvctx, ctctx, eq, op == CALL, discard, szof);
4196
4197 switch (op) {
4198 case PUSH:
4199 if (rn != NULL)
4200 check_expr_misc(rn, false, false, eq, false, false,
4201 szof);
4202 break;
4203 case LOGAND:
4204 case LOGOR:
4205 check_expr_misc(rn, false, true, eq, false, false, szof);
4206 break;
4207 case COLON:
4208 check_expr_misc(rn, cvctx, ctctx, eq, false, false, szof);
4209 break;
4210 case COMMA:
4211 check_expr_misc(rn, vctx, tctx, eq, false, false, szof);
4212 break;
4213 default:
4214 if (mp->m_binary)
4215 check_expr_misc(rn, true, false, eq, false, false,
4216 szof);
4217 break;
4218 }
4219 }
4220
4221 /*
4222 * Checks the range of array indices, if possible.
4223 * amper is set if only the address of the element is used. This
4224 * means that the index is allowed to refer to the first element
4225 * after the array.
4226 */
4227 static void
4228 check_array_index(tnode_t *tn, bool amper)
4229 {
4230 int dim;
4231 tnode_t *ln, *rn;
4232 int elsz;
4233 int64_t con;
4234
4235 ln = tn->tn_left;
4236 rn = tn->tn_right;
4237
4238 /* We can only check constant indices. */
4239 if (rn->tn_op != CON)
4240 return;
4241
4242 /* Return if the left node does not stem from an array. */
4243 if (ln->tn_op != ADDR)
4244 return;
4245 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4246 return;
4247 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4248 return;
4249
4250 /*
4251 * For incomplete array types, we can print a warning only if
4252 * the index is negative.
4253 */
4254 if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4255 return;
4256
4257 /* Get the size of one array element */
4258 if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0)
4259 return;
4260 elsz /= CHAR_SIZE;
4261
4262 /* Change the unit of the index from bytes to element size. */
4263 if (is_uinteger(rn->tn_type->t_tspec)) {
4264 con = (uint64_t)rn->tn_val->v_quad / elsz;
4265 } else {
4266 con = rn->tn_val->v_quad / elsz;
4267 }
4268
4269 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4270
4271 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4272 /* array subscript cannot be negative: %ld */
4273 warning(167, (long)con);
4274 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4275 /* array subscript cannot be > %d: %ld */
4276 warning(168, dim - 1, (long)con);
4277 }
4278 }
4279
4280 static bool
4281 is_out_of_char_range(const tnode_t *tn)
4282 {
4283 return tn->tn_op == CON &&
4284 !(0 <= tn->tn_val->v_quad &&
4285 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
4286 }
4287
4288 /*
4289 * Check for ordered comparisons of unsigned values with 0.
4290 */
4291 static void
4292 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4293 {
4294 tspec_t lt, rt;
4295
4296 lt = ln->tn_type->t_tspec;
4297 rt = rn->tn_type->t_tspec;
4298
4299 if (ln->tn_op != CON && rn->tn_op != CON)
4300 return;
4301
4302 if (!is_integer(lt) || !is_integer(rt))
4303 return;
4304
4305 if (hflag || pflag) {
4306 if (lt == CHAR && is_out_of_char_range(rn)) {
4307 /* nonportable character comparison '%s %d' */
4308 warning(230, op_name(op), (int)rn->tn_val->v_quad);
4309 return;
4310 }
4311 if (rt == CHAR && is_out_of_char_range(ln)) {
4312 /* nonportable character comparison '%s %d' */
4313 warning(230, op_name(op), (int)ln->tn_val->v_quad);
4314 return;
4315 }
4316 }
4317
4318 if (is_uinteger(lt) && !is_uinteger(rt) &&
4319 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4320 if (rn->tn_val->v_quad < 0) {
4321 /* comparison of %s with %s, op %s */
4322 warning(162, type_name(ln->tn_type),
4323 "negative constant", op_name(op));
4324 } else if (op == LT || op == GE) {
4325 /* comparison of %s with %s, op %s */
4326 warning(162, type_name(ln->tn_type), "0", op_name(op));
4327 }
4328 return;
4329 }
4330 if (is_uinteger(rt) && !is_uinteger(lt) &&
4331 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4332 if (ln->tn_val->v_quad < 0) {
4333 /* comparison of %s with %s, op %s */
4334 warning(162, "negative constant",
4335 type_name(rn->tn_type), op_name(op));
4336 } else if (op == GT || op == LE) {
4337 /* comparison of %s with %s, op %s */
4338 warning(162, "0", type_name(rn->tn_type), op_name(op));
4339 }
4340 return;
4341 }
4342 }
4343
4344 /*
4345 * Return whether the expression can be used for static initialization.
4346 *
4347 * Constant initialization expressions must be constant or an address
4348 * of a static object with an optional offset. In the first case,
4349 * the result is returned in *offsp. In the second case, the static
4350 * object is returned in *symp and the offset in *offsp.
4351 *
4352 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4353 * CON. Type conversions are allowed if they do not change binary
4354 * representation (including width).
4355 *
4356 * C99 6.6 "Constant expressions"
4357 * C99 6.7.8p4 restricts initializers for static storage duration
4358 */
4359 bool
4360 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4361 {
4362 const sym_t *sym;
4363 ptrdiff_t offs1, offs2;
4364 tspec_t t, ot;
4365
4366 switch (tn->tn_op) {
4367 case MINUS:
4368 if (tn->tn_right->tn_op == CVT)
4369 return constant_addr(tn->tn_right, symp, offsp);
4370 else if (tn->tn_right->tn_op != CON)
4371 return false;
4372 /* FALLTHROUGH */
4373 case PLUS:
4374 offs1 = offs2 = 0;
4375 if (tn->tn_left->tn_op == CON) {
4376 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4377 if (!constant_addr(tn->tn_right, &sym, &offs2))
4378 return false;
4379 } else if (tn->tn_right->tn_op == CON) {
4380 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4381 if (tn->tn_op == MINUS)
4382 offs2 = -offs2;
4383 if (!constant_addr(tn->tn_left, &sym, &offs1))
4384 return false;
4385 } else {
4386 return false;
4387 }
4388 *symp = sym;
4389 *offsp = offs1 + offs2;
4390 return true;
4391 case ADDR:
4392 if (tn->tn_left->tn_op == NAME) {
4393 *symp = tn->tn_left->tn_sym;
4394 *offsp = 0;
4395 return true;
4396 } else {
4397 /*
4398 * If this would be the front end of a compiler we
4399 * would return a label instead of 0, at least if
4400 * 'tn->tn_left->tn_op == STRING'.
4401 */
4402 *symp = NULL;
4403 *offsp = 0;
4404 return true;
4405 }
4406 case CVT:
4407 t = tn->tn_type->t_tspec;
4408 ot = tn->tn_left->tn_type->t_tspec;
4409 if ((!is_integer(t) && t != PTR) ||
4410 (!is_integer(ot) && ot != PTR)) {
4411 return false;
4412 }
4413 #if 0
4414 /*
4415 * consider:
4416 * struct foo {
4417 * unsigned char a;
4418 * } f = {
4419 * (unsigned char)(unsigned long)
4420 * (&(((struct foo *)0)->a))
4421 * };
4422 * since psize(unsigned long) != psize(unsigned char),
4423 * this fails.
4424 */
4425 else if (psize(t) != psize(ot))
4426 return -1;
4427 #endif
4428 return constant_addr(tn->tn_left, symp, offsp);
4429 default:
4430 return false;
4431 }
4432 }
4433
4434 /* Append s2 to s1, then free s2. */
4435 strg_t *
4436 cat_strings(strg_t *s1, strg_t *s2)
4437 {
4438
4439 if (s1->st_char != s2->st_char) {
4440 /* cannot concatenate wide and regular string literals */
4441 error(292);
4442 return s1;
4443 }
4444
4445 size_t len1 = s1->st_len;
4446 size_t len2 = s2->st_len;
4447 size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
4448 size_t size1 = len1 * chsize;
4449 size_t size2 = (len2 + 1) * chsize;
4450 s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
4451 memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
4452 free(s2->st_mem);
4453
4454 s1->st_len = len1 + len2;
4455 free(s2);
4456
4457 return s1;
4458 }
4459
4460 static bool
4461 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4462 {
4463
4464 if (op == SHL || op == SHR) {
4465 if (!lparen && (lop == PLUS || lop == MINUS))
4466 return true;
4467 if (!rparen && (rop == PLUS || rop == MINUS))
4468 return true;
4469 return false;
4470 }
4471
4472 if (op == LOGOR) {
4473 if (!lparen && lop == LOGAND)
4474 return true;
4475 if (!rparen && rop == LOGAND)
4476 return true;
4477 return false;
4478 }
4479
4480 lint_assert(op == BITAND || op == BITXOR || op == BITOR);
4481 if (!lparen && lop != op) {
4482 if (lop == PLUS || lop == MINUS)
4483 return true;
4484 if (lop == BITAND || lop == BITXOR)
4485 return true;
4486 }
4487 if (!rparen && rop != op) {
4488 if (rop == PLUS || rop == MINUS)
4489 return true;
4490 if (rop == BITAND || rop == BITXOR)
4491 return true;
4492 }
4493 return false;
4494 }
4495
4496 /*
4497 * Print a warning if the given node has operands which should be
4498 * parenthesized.
4499 *
4500 * XXX Does not work if an operand is a constant expression. Constant
4501 * expressions are already folded.
4502 */
4503 static void
4504 check_precedence_confusion(tnode_t *tn)
4505 {
4506 tnode_t *ln, *rn;
4507
4508 if (!hflag)
4509 return;
4510
4511 debug_node(tn);
4512
4513 lint_assert(is_binary(tn));
4514 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4515 continue;
4516 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4517 continue;
4518
4519 if (is_confusing_precedence(tn->tn_op,
4520 ln->tn_op, ln->tn_parenthesized,
4521 rn->tn_op, rn->tn_parenthesized)) {
4522 /* precedence confusion possible: parenthesize! */
4523 warning(169);
4524 }
4525 }
4526
4527 typedef struct stmt_expr {
4528 struct memory_block *se_mem;
4529 sym_t *se_sym;
4530 struct stmt_expr *se_enclosing;
4531 } stmt_expr;
4532
4533 static stmt_expr *stmt_exprs;
4534
4535 void
4536 begin_statement_expr(void)
4537 {
4538 stmt_expr *se = xmalloc(sizeof(*se));
4539 se->se_mem = expr_save_memory();
4540 se->se_sym = NULL;
4541 se->se_enclosing = stmt_exprs;
4542 stmt_exprs = se;
4543 }
4544
4545 void
4546 do_statement_expr(tnode_t *tn)
4547 {
4548 block_level--;
4549 mem_block_level--;
4550 stmt_exprs->se_sym = tn != NULL
4551 ? mktempsym(block_dup_type(tn->tn_type))
4552 : NULL; /* after a syntax error */
4553 mem_block_level++;
4554 block_level++;
4555 /* ({ }) is a GCC extension */
4556 gnuism(320);
4557 }
4558
4559 tnode_t *
4560 end_statement_expr(void)
4561 {
4562 stmt_expr *se = stmt_exprs;
4563 if (se->se_sym == NULL)
4564 return NULL; /* after a syntax error */
4565 tnode_t *tn = build_name(se->se_sym, false);
4566 (void)expr_save_memory(); /* leak */
4567 expr_restore_memory(se->se_mem);
4568 stmt_exprs = se->se_enclosing;
4569 free(se);
4570 return tn;
4571 }
4572