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