tree.c revision 1.427 1 /* $NetBSD: tree.c,v 1.427 2022/04/15 21:50:07 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.427 2022/04/15 21:50:07 rillig Exp $");
41 #endif
42
43 #include <float.h>
44 #include <limits.h>
45 #include <math.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "lint1.h"
51 #include "cgram.h"
52
53 static tnode_t *build_integer_constant(tspec_t, int64_t);
54 static void check_pointer_comparison(op_t,
55 const tnode_t *, const tnode_t *);
56 static bool check_assign_types_compatible(op_t, int,
57 const tnode_t *, const tnode_t *);
58 static void check_bad_enum_operation(op_t,
59 const tnode_t *, const tnode_t *);
60 static void check_enum_type_mismatch(op_t, int,
61 const tnode_t *, const tnode_t *);
62 static void check_enum_int_mismatch(op_t, int,
63 const tnode_t *, const tnode_t *);
64 static tnode_t *new_tnode(op_t, bool, type_t *, tnode_t *, tnode_t *);
65 static void balance(op_t, tnode_t **, tnode_t **);
66 static void warn_incompatible_types(op_t, const type_t *, tspec_t,
67 const type_t *, tspec_t);
68 static void warn_incompatible_pointers(const mod_t *,
69 const type_t *, const type_t *);
70 static bool has_constant_member(const type_t *);
71 static void check_prototype_conversion(int, tspec_t, tspec_t, type_t *,
72 tnode_t *);
73 static void check_integer_conversion(op_t, int, tspec_t, tspec_t, type_t *,
74 tnode_t *);
75 static void check_pointer_integer_conversion(op_t, tspec_t, type_t *,
76 tnode_t *);
77 static void check_pointer_conversion(tnode_t *, type_t *);
78 static tnode_t *build_struct_access(op_t, bool, tnode_t *, tnode_t *);
79 static tnode_t *build_prepost_incdec(op_t, bool, tnode_t *);
80 static tnode_t *build_real_imag(op_t, bool, tnode_t *);
81 static tnode_t *build_address(bool, tnode_t *, bool);
82 static tnode_t *build_plus_minus(op_t, bool, tnode_t *, tnode_t *);
83 static tnode_t *build_bit_shift(op_t, bool, tnode_t *, tnode_t *);
84 static tnode_t *build_colon(bool, tnode_t *, tnode_t *);
85 static tnode_t *build_assignment(op_t, bool, tnode_t *, tnode_t *);
86 static tnode_t *plength(type_t *);
87 static tnode_t *fold(tnode_t *);
88 static tnode_t *fold_test(tnode_t *);
89 static tnode_t *fold_float(tnode_t *);
90 static tnode_t *check_function_arguments(type_t *, tnode_t *);
91 static tnode_t *check_prototype_argument(int, type_t *, tnode_t *);
92 static void check_null_effect(const tnode_t *);
93 static void check_array_index(tnode_t *, bool);
94 static void check_integer_comparison(op_t, tnode_t *, tnode_t *);
95 static void check_precedence_confusion(tnode_t *);
96
97 extern sig_atomic_t fpe;
98
99 static const char *
100 op_name(op_t op)
101 {
102 return modtab[op].m_name;
103 }
104
105 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
106 type_t *
107 block_derive_type(type_t *tp, tspec_t t)
108 {
109 type_t *tp2;
110
111 tp2 = block_zero_alloc(sizeof(*tp2));
112 tp2->t_tspec = t;
113 tp2->t_subt = tp;
114 return tp2;
115 }
116
117 /*
118 * Derive 'pointer to tp' or 'function returning tp'.
119 * The memory is freed at the end of the current expression.
120 */
121 type_t *
122 expr_derive_type(type_t *tp, tspec_t t)
123 {
124 type_t *tp2;
125
126 tp2 = expr_zero_alloc(sizeof(*tp2));
127 tp2->t_tspec = t;
128 tp2->t_subt = tp;
129 return tp2;
130 }
131
132 /*
133 * Create a node for a constant.
134 */
135 tnode_t *
136 build_constant(type_t *tp, val_t *v)
137 {
138 tnode_t *n;
139
140 n = expr_alloc_tnode();
141 n->tn_op = CON;
142 n->tn_type = tp;
143 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val));
144 n->tn_val->v_tspec = tp->t_tspec;
145 n->tn_val->v_unsigned_since_c90 = v->v_unsigned_since_c90;
146 n->tn_val->v_u = v->v_u;
147 free(v);
148 return n;
149 }
150
151 static tnode_t *
152 build_integer_constant(tspec_t t, int64_t q)
153 {
154 tnode_t *n;
155
156 n = expr_alloc_tnode();
157 n->tn_op = CON;
158 n->tn_type = gettyp(t);
159 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val));
160 n->tn_val->v_tspec = t;
161 n->tn_val->v_quad = q;
162 return n;
163 }
164
165 static void
166 fallback_symbol(sym_t *sym)
167 {
168
169 if (fallback_symbol_strict_bool(sym))
170 return;
171
172 if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
173 strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
174 /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
175 gnuism(316);
176 sym->s_type = block_derive_type(gettyp(CHAR), PTR);
177 sym->s_type->t_const = true;
178 return;
179 }
180
181 if (block_level > 0 && strcmp(sym->s_name, "__func__") == 0) {
182 if (!Sflag)
183 /* __func__ is a C99 feature */
184 warning(317);
185 sym->s_type = block_derive_type(gettyp(CHAR), PTR);
186 sym->s_type->t_const = true;
187 return;
188 }
189
190 /* '%s' undefined */
191 error(99, sym->s_name);
192 }
193
194 /*
195 * Functions that are predeclared by GCC or other compilers can be called
196 * with arbitrary arguments. Since lint usually runs after a successful
197 * compilation, it's the compiler's job to catch any errors.
198 */
199 bool
200 is_compiler_builtin(const char *name)
201 {
202 /* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */
203 if (gflag) {
204 if (strncmp(name, "__atomic_", 9) == 0 ||
205 strncmp(name, "__builtin_", 10) == 0 ||
206 strcmp(name, "alloca") == 0 ||
207 /* obsolete but still in use, as of 2021 */
208 strncmp(name, "__sync_", 7) == 0)
209 return true;
210 }
211
212 /* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */
213 if (strncmp(name, "_mm_", 4) == 0)
214 return true;
215
216 return false;
217 }
218
219 static bool
220 str_endswith(const char *haystack, const char *needle)
221 {
222 size_t hlen = strlen(haystack);
223 size_t nlen = strlen(needle);
224
225 return nlen <= hlen &&
226 memcmp(haystack + hlen - nlen, needle, nlen) == 0;
227 }
228
229 /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */
230 static bool
231 is_gcc_bool_builtin(const char *name)
232 {
233 return strncmp(name, "__builtin_", 10) == 0 &&
234 (str_endswith(name, "_overflow") ||
235 str_endswith(name, "_overflow_p"));
236 }
237
238 static void
239 build_name_call(sym_t *sym)
240 {
241
242 if (is_compiler_builtin(sym->s_name)) {
243 /*
244 * Do not warn about these, just assume that
245 * they are regular functions compatible with
246 * non-prototype calling conventions.
247 */
248 if (gflag && is_gcc_bool_builtin(sym->s_name))
249 sym->s_type = gettyp(BOOL);
250
251 } else if (Sflag) {
252 /* function '%s' implicitly declared to return int */
253 error(215, sym->s_name);
254 } else if (sflag) {
255 /* function '%s' implicitly declared to return int */
256 warning(215, sym->s_name);
257 }
258
259 /* XXX if tflag is set, the symbol should be exported to level 0 */
260 sym->s_type = block_derive_type(sym->s_type, FUNC);
261 }
262
263 /* Create a node for a name (symbol table entry). */
264 tnode_t *
265 build_name(sym_t *sym, bool is_funcname)
266 {
267 tnode_t *n;
268
269 if (sym->s_scl == NOSCL) {
270 sym->s_scl = EXTERN;
271 sym->s_def = DECL;
272 if (is_funcname)
273 build_name_call(sym);
274 else
275 fallback_symbol(sym);
276 }
277
278 lint_assert(sym->s_kind == FVFT || sym->s_kind == FMEMBER);
279
280 n = expr_alloc_tnode();
281 n->tn_type = sym->s_type;
282 if (sym->s_scl == BOOL_CONST) {
283 n->tn_op = CON;
284 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val));
285 n->tn_val->v_tspec = BOOL;
286 n->tn_val->v_quad = sym->u.s_bool_constant ? 1 : 0;
287 } else if (sym->s_scl == ENUM_CONST) {
288 n->tn_op = CON;
289 n->tn_val = expr_zero_alloc(sizeof(*n->tn_val));
290 n->tn_val->v_tspec = INT; /* ENUM is in n->tn_type */
291 n->tn_val->v_quad = sym->u.s_enum_constant;
292 } else {
293 n->tn_op = NAME;
294 n->tn_sym = sym;
295 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
296 n->tn_lvalue = true;
297 }
298
299 return n;
300 }
301
302 tnode_t *
303 build_string(strg_t *strg)
304 {
305 size_t len;
306 tnode_t *n;
307 type_t *tp;
308
309 len = strg->st_len;
310
311 n = expr_alloc_tnode();
312
313 tp = expr_zero_alloc(sizeof(*tp));
314 tp->t_tspec = ARRAY;
315 tp->t_subt = gettyp(strg->st_char ? CHAR : WCHAR);
316 tp->t_dim = (int)(len + 1);
317
318 n->tn_op = STRING;
319 n->tn_type = tp;
320 n->tn_lvalue = true;
321
322 n->tn_string = expr_zero_alloc(sizeof(*n->tn_string));
323 n->tn_string->st_char = strg->st_char;
324 n->tn_string->st_len = len;
325
326 size_t chsize = strg->st_char ? sizeof(char) : sizeof(wchar_t);
327 size_t size = (len + 1) * chsize;
328 n->tn_string->st_mem = expr_zero_alloc(size);
329 (void)memcpy(n->tn_string->st_mem, strg->st_mem, size);
330 free(strg->st_mem);
331 free(strg);
332
333 return n;
334 }
335
336 /*
337 * Returns a symbol which has the same name as the msym argument and is a
338 * member of the struct or union specified by the tn argument.
339 */
340 static sym_t *
341 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
342 {
343 struct_or_union *str;
344 type_t *tp;
345 sym_t *sym, *csym;
346 bool eq;
347 tspec_t t;
348
349 /*
350 * Remove the member if it was unknown until now, which means
351 * that no defined struct or union has a member with the same name.
352 */
353 if (msym->s_scl == NOSCL) {
354 /* type '%s' does not have member '%s' */
355 error(101, type_name(tn->tn_type), msym->s_name);
356 rmsym(msym);
357 msym->s_kind = FMEMBER;
358 msym->s_scl = MOS;
359
360 struct_or_union *sou = expr_zero_alloc(sizeof(*sou));
361 sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag));
362 sou->sou_tag->s_name = unnamed;
363
364 msym->u.s_member.sm_sou_type = sou;
365 /*
366 * The member sm_offset_in_bits is not needed here since this
367 * symbol can only be used for error reporting.
368 */
369 return msym;
370 }
371
372 /* Set str to the tag of which msym is expected to be a member. */
373 str = NULL;
374 t = (tp = tn->tn_type)->t_tspec;
375 if (op == POINT) {
376 if (t == STRUCT || t == UNION)
377 str = tp->t_str;
378 } else if (op == ARROW && t == PTR) {
379 t = (tp = tp->t_subt)->t_tspec;
380 if (t == STRUCT || t == UNION)
381 str = tp->t_str;
382 }
383
384 /*
385 * If this struct/union has a member with the name of msym, return it.
386 */
387 if (str != NULL) {
388 for (sym = msym; sym != NULL; sym = sym->s_symtab_next) {
389 if (!is_member(sym))
390 continue;
391 if (sym->u.s_member.sm_sou_type != str)
392 continue;
393 if (strcmp(sym->s_name, msym->s_name) != 0)
394 continue;
395 return sym;
396 }
397 }
398
399 /*
400 * Set eq to false if there are struct/union members with the same
401 * name and different types and/or offsets.
402 */
403 eq = true;
404 for (csym = msym; csym != NULL; csym = csym->s_symtab_next) {
405 if (csym->s_scl != MOS && csym->s_scl != MOU)
406 continue;
407 if (strcmp(msym->s_name, csym->s_name) != 0)
408 continue;
409 for (sym = csym->s_symtab_next; sym != NULL;
410 sym = sym->s_symtab_next) {
411 bool w;
412
413 if (sym->s_scl != MOS && sym->s_scl != MOU)
414 continue;
415 if (strcmp(csym->s_name, sym->s_name) != 0)
416 continue;
417 if (csym->u.s_member.sm_offset_in_bits !=
418 sym->u.s_member.sm_offset_in_bits) {
419 eq = false;
420 break;
421 }
422 w = false;
423 eq = eqtype(csym->s_type, sym->s_type,
424 false, false, &w) && !w;
425 if (!eq)
426 break;
427 if (csym->s_bitfield != sym->s_bitfield) {
428 eq = false;
429 break;
430 }
431 if (csym->s_bitfield) {
432 type_t *tp1, *tp2;
433
434 tp1 = csym->s_type;
435 tp2 = sym->s_type;
436 if (tp1->t_flen != tp2->t_flen) {
437 eq = false;
438 break;
439 }
440 if (tp1->t_foffs != tp2->t_foffs) {
441 eq = false;
442 break;
443 }
444 }
445 }
446 if (!eq)
447 break;
448 }
449
450 /*
451 * Now handle the case in which the left operand refers really
452 * to a struct/union, but the right operand is not member of it.
453 */
454 if (str != NULL) {
455 if (eq && tflag) {
456 /* illegal member use: %s */
457 warning(102, msym->s_name);
458 } else {
459 /* illegal member use: %s */
460 error(102, msym->s_name);
461 }
462 return msym;
463 }
464
465 /*
466 * Now the left operand of ARROW does not point to a struct/union
467 * or the left operand of POINT is no struct/union.
468 */
469 if (eq) {
470 if (op == POINT) {
471 if (tflag) {
472 /* left operand of '.' must be struct ... */
473 warning(103, type_name(tn->tn_type));
474 } else {
475 /* left operand of '.' must be struct ... */
476 error(103, type_name(tn->tn_type));
477 }
478 } else {
479 if (tflag && tn->tn_type->t_tspec == PTR) {
480 /* left operand of '->' must be pointer ... */
481 warning(104, type_name(tn->tn_type));
482 } else {
483 /* left operand of '->' must be pointer ... */
484 error(104, type_name(tn->tn_type));
485 }
486 }
487 } else {
488 if (tflag) {
489 /* non-unique member requires struct/union %s */
490 error(105, op == POINT ? "object" : "pointer");
491 } else {
492 /* unacceptable operand of '%s' */
493 error(111, op_name(op));
494 }
495 }
496
497 return msym;
498 }
499
500 tnode_t *
501 build_generic_selection(const tnode_t *expr,
502 struct generic_association *sel)
503 {
504 tnode_t *default_result = NULL;
505
506 for (; sel != NULL; sel = sel->ga_prev) {
507 if (expr != NULL &&
508 eqtype(sel->ga_arg, expr->tn_type, false, false, NULL))
509 return sel->ga_result;
510 else if (sel->ga_arg == NULL)
511 default_result = sel->ga_result;
512 }
513 return default_result;
514 }
515
516 /*
517 * Create a tree node for a binary operator and its two operands. Also called
518 * for unary operators; in that case rn is NULL.
519 *
520 * Function calls, sizeof and casts are handled elsewhere.
521 */
522 tnode_t *
523 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
524 {
525 const mod_t *mp;
526 tnode_t *ntn;
527 type_t *rettp;
528
529 mp = &modtab[op];
530
531 /* If there was an error in one of the operands, return. */
532 if (ln == NULL || (mp->m_binary && rn == NULL))
533 return NULL;
534
535 /*
536 * Apply class conversions to the left operand, but only if its
537 * value is needed or it is compared with null.
538 */
539 if (mp->m_left_value_context || mp->m_left_test_context)
540 ln = cconv(ln);
541 /*
542 * The right operand is almost always in a test or value context,
543 * except if it is a struct or union member.
544 */
545 if (mp->m_binary && op != ARROW && op != POINT)
546 rn = cconv(rn);
547
548 /*
549 * Print some warnings for comparisons of unsigned values with
550 * constants lower than or equal to null. This must be done
551 * before promote() because otherwise unsigned char and unsigned
552 * short would be promoted to int. Also types are tested to be
553 * CHAR, which would also become int.
554 */
555 if (mp->m_comparison)
556 check_integer_comparison(op, ln, rn);
557
558 /*
559 * Promote the left operand if it is in a test or value context
560 */
561 if (mp->m_left_value_context || mp->m_left_test_context)
562 ln = promote(op, false, ln);
563 /*
564 * Promote the right operand, but only if it is no struct or
565 * union member, or if it is not to be assigned to the left operand
566 */
567 if (mp->m_binary && op != ARROW && op != POINT &&
568 op != ASSIGN && op != RETURN && op != INIT) {
569 rn = promote(op, false, rn);
570 }
571
572 /*
573 * If the result of the operation is different for signed or
574 * unsigned operands and one of the operands is signed only in
575 * ANSI C, print a warning.
576 */
577 if (mp->m_warn_if_left_unsigned_in_c90 &&
578 ln->tn_op == CON && ln->tn_val->v_unsigned_since_c90) {
579 /* ANSI C treats constant as unsigned, op %s */
580 warning(218, mp->m_name);
581 ln->tn_val->v_unsigned_since_c90 = false;
582 }
583 if (mp->m_warn_if_right_unsigned_in_c90 &&
584 rn->tn_op == CON && rn->tn_val->v_unsigned_since_c90) {
585 /* ANSI C treats constant as unsigned, op %s */
586 warning(218, mp->m_name);
587 rn->tn_val->v_unsigned_since_c90 = false;
588 }
589
590 /* Make sure both operands are of the same type */
591 if (mp->m_balance_operands || (tflag && (op == SHL || op == SHR)))
592 balance(op, &ln, &rn);
593
594 /*
595 * Check types for compatibility with the operation and mutual
596 * compatibility. Return if there are serious problems.
597 */
598 if (!typeok(op, 0, ln, rn))
599 return NULL;
600
601 /* And now create the node. */
602 switch (op) {
603 case POINT:
604 case ARROW:
605 ntn = build_struct_access(op, sys, ln, rn);
606 break;
607 case INCAFT:
608 case DECAFT:
609 case INCBEF:
610 case DECBEF:
611 ntn = build_prepost_incdec(op, sys, ln);
612 break;
613 case ADDR:
614 ntn = build_address(sys, ln, false);
615 break;
616 case INDIR:
617 ntn = new_tnode(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
618 break;
619 case PLUS:
620 case MINUS:
621 ntn = build_plus_minus(op, sys, ln, rn);
622 break;
623 case SHL:
624 case SHR:
625 ntn = build_bit_shift(op, sys, ln, rn);
626 break;
627 case COLON:
628 ntn = build_colon(sys, ln, rn);
629 break;
630 case ASSIGN:
631 case MULASS:
632 case DIVASS:
633 case MODASS:
634 case ADDASS:
635 case SUBASS:
636 case SHLASS:
637 case SHRASS:
638 case ANDASS:
639 case XORASS:
640 case ORASS:
641 case RETURN:
642 case INIT:
643 ntn = build_assignment(op, sys, ln, rn);
644 break;
645 case COMMA:
646 case QUEST:
647 ntn = new_tnode(op, sys, rn->tn_type, ln, rn);
648 break;
649 case REAL:
650 case IMAG:
651 ntn = build_real_imag(op, sys, ln);
652 break;
653 default:
654 rettp = mp->m_returns_bool
655 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
656 lint_assert(mp->m_binary || rn == NULL);
657 ntn = new_tnode(op, sys, rettp, ln, rn);
658 break;
659 }
660
661 /* Return if an error occurred. */
662 if (ntn == NULL)
663 return NULL;
664
665 /* Print a warning if precedence confusion is possible */
666 if (mp->m_possible_precedence_confusion)
667 check_precedence_confusion(ntn);
668
669 /*
670 * Print a warning if one of the operands is in a context where
671 * it is compared with null and if this operand is a constant.
672 */
673 if (mp->m_left_test_context) {
674 if (ln->tn_op == CON ||
675 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
676 if (hflag && !constcond_flag &&
677 !ln->tn_system_dependent)
678 /* constant in conditional context */
679 warning(161);
680 }
681 }
682
683 /* Fold if the operator requires it */
684 if (mp->m_fold_constant_operands) {
685 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
686 if (mp->m_left_test_context) {
687 ntn = fold_test(ntn);
688 } else if (is_floating(ntn->tn_type->t_tspec)) {
689 ntn = fold_float(ntn);
690 } else {
691 ntn = fold(ntn);
692 }
693 } else if (op == QUEST && ln->tn_op == CON) {
694 ntn = ln->tn_val->v_quad != 0
695 ? rn->tn_left : rn->tn_right;
696 }
697 }
698
699 return ntn;
700 }
701
702 tnode_t *
703 build_unary(op_t op, bool sys, tnode_t *tn)
704 {
705 return build_binary(tn, op, sys, NULL);
706 }
707
708 tnode_t *
709 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
710 {
711 sym_t *msym;
712
713 if (ln == NULL)
714 return NULL;
715
716 if (op == ARROW) {
717 /* must do this before struct_or_union_member is called */
718 ln = cconv(ln);
719 }
720 msym = struct_or_union_member(ln, op, getsym(member));
721 return build_binary(ln, op, sys, build_name(msym, false));
722 }
723
724 /*
725 * Perform class conversions.
726 *
727 * Arrays of type T are converted into pointers to type T.
728 * Functions are converted to pointers to functions.
729 * Lvalues are converted to rvalues.
730 *
731 * C99 6.3 "Conversions"
732 * C99 6.3.2 "Other operands"
733 * C99 6.3.2.1 "Lvalues, arrays, and function designators"
734 */
735 tnode_t *
736 cconv(tnode_t *tn)
737 {
738 type_t *tp;
739
740 /*
741 * Array-lvalue (array of type T) is converted into rvalue
742 * (pointer to type T)
743 */
744 if (tn->tn_type->t_tspec == ARRAY) {
745 if (!tn->tn_lvalue) {
746 /* XXX print correct operator */
747 /* %soperand of '%s' must be lvalue */
748 gnuism(114, "", op_name(ADDR));
749 }
750 tn = new_tnode(ADDR, tn->tn_sys,
751 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
752 }
753
754 /*
755 * Expression of type function (function with return value of type T)
756 * in rvalue-expression (pointer to function with return value
757 * of type T)
758 */
759 if (tn->tn_type->t_tspec == FUNC)
760 tn = build_address(tn->tn_sys, tn, true);
761
762 /* lvalue to rvalue */
763 if (tn->tn_lvalue) {
764 tp = expr_dup_type(tn->tn_type);
765 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
766 tp->t_const = tp->t_volatile = false;
767 tn = new_tnode(LOAD, tn->tn_sys, tp, tn, NULL);
768 }
769
770 return tn;
771 }
772
773 const tnode_t *
774 before_conversion(const tnode_t *tn)
775 {
776 while (tn->tn_op == CVT && !tn->tn_cast)
777 tn = tn->tn_left;
778 return tn;
779 }
780
781 static bool
782 is_null_pointer(const tnode_t *tn)
783 {
784 tspec_t t = tn->tn_type->t_tspec;
785
786 return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID) ||
787 is_integer(t))
788 && (tn->tn_op == CON && tn->tn_val->v_quad == 0);
789 }
790
791 /*
792 * Most errors required by ANSI C are reported in struct_or_union_member().
793 * Here we only check for totally wrong things.
794 */
795 static bool
796 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
797 {
798 if (lt == FUNC || lt == VOID || ltp->t_bitfield ||
799 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
800 /* Without tflag we got already an error */
801 if (tflag)
802 /* unacceptable operand of '%s' */
803 error(111, op_name(POINT));
804 return false;
805 }
806 return true;
807 }
808
809 static bool
810 typeok_arrow(tspec_t lt)
811 {
812 if (lt == PTR || (tflag && is_integer(lt)))
813 return true;
814
815 /* Without tflag we got already an error */
816 if (tflag)
817 /* unacceptable operand of '%s' */
818 error(111, op_name(ARROW));
819 return false;
820 }
821
822 static bool
823 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
824 {
825 /* operand has scalar type (checked in typeok) */
826 if (!tn->tn_lvalue) {
827 if (tn->tn_op == CVT && tn->tn_cast &&
828 tn->tn_left->tn_op == LOAD) {
829 /* a cast does not yield an lvalue */
830 error(163);
831 }
832 /* %soperand of '%s' must be lvalue */
833 error(114, "", op_name(op));
834 return false;
835 } else if (tp->t_const) {
836 if (!tflag)
837 /* %soperand of '%s' must be modifiable lvalue */
838 warning(115, "", op_name(op));
839 }
840 return true;
841 }
842
843 static bool
844 typeok_address(const mod_t *mp,
845 const tnode_t *tn, const type_t *tp, tspec_t t)
846 {
847 if (t == ARRAY || t == FUNC) {
848 /* ok, a warning comes later (in build_address()) */
849 } else if (!tn->tn_lvalue) {
850 if (tn->tn_op == CVT && tn->tn_cast &&
851 tn->tn_left->tn_op == LOAD) {
852 /* a cast does not yield an lvalue */
853 error(163);
854 }
855 /* %soperand of '%s' must be lvalue */
856 error(114, "", mp->m_name);
857 return false;
858 } else if (is_scalar(t)) {
859 if (tp->t_bitfield) {
860 /* cannot take address of bit-field */
861 error(112);
862 return false;
863 }
864 } else if (t != STRUCT && t != UNION) {
865 /* unacceptable operand of '%s' */
866 error(111, mp->m_name);
867 return false;
868 }
869 if (tn->tn_op == NAME && tn->tn_sym->s_register) {
870 /* cannot take address of register %s */
871 error(113, tn->tn_sym->s_name);
872 return false;
873 }
874 return true;
875 }
876
877 static bool
878 typeok_indir(tspec_t t)
879 {
880 /* until now there were no type checks for this operator */
881 if (t != PTR) {
882 /* cannot dereference non-pointer type */
883 error(96);
884 return false;
885 }
886 return true;
887 }
888
889 static bool
890 typeok_plus(op_t op,
891 const type_t *ltp, tspec_t lt,
892 const type_t *rtp, tspec_t rt)
893 {
894 /* operands have scalar types (checked above) */
895 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
896 warn_incompatible_types(op, ltp, lt, rtp, rt);
897 return false;
898 }
899 return true;
900 }
901
902 static bool
903 typeok_minus(op_t op,
904 const type_t *ltp, tspec_t lt,
905 const type_t *rtp, tspec_t rt)
906 {
907 /* operands have scalar types (checked above) */
908 if (lt == PTR && (!is_integer(rt) && rt != PTR)) {
909 warn_incompatible_types(op, ltp, lt, rtp, rt);
910 return false;
911 } else if (rt == PTR && lt != PTR) {
912 warn_incompatible_types(op, ltp, lt, rtp, rt);
913 return false;
914 }
915 if (lt == PTR && rt == PTR) {
916 if (!eqtype(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
917 /* illegal pointer subtraction */
918 error(116);
919 }
920 }
921 return true;
922 }
923
924 static void
925 typeok_shr(const mod_t *mp,
926 const tnode_t *ln, tspec_t lt,
927 const tnode_t *rn, tspec_t rt)
928 {
929 tspec_t olt, ort;
930
931 olt = before_conversion(ln)->tn_type->t_tspec;
932 ort = before_conversion(rn)->tn_type->t_tspec;
933
934 /* operands have integer types (checked above) */
935 if (pflag && !is_uinteger(olt)) {
936 /*
937 * The left operand is signed. This means that
938 * the operation is (possibly) nonportable.
939 */
940 if (ln->tn_op != CON) {
941 /* bitwise '%s' on signed value possibly nonportable */
942 warning(117, mp->m_name);
943 } else if (ln->tn_val->v_quad < 0) {
944 /* bitwise '%s' on signed value nonportable */
945 warning(120, mp->m_name);
946 }
947 } else if (!tflag && !sflag && !is_uinteger(olt) && is_uinteger(ort)) {
948 /*
949 * The left operand would become unsigned in
950 * traditional C.
951 */
952 if (hflag && !Sflag &&
953 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
954 /* semantics of '%s' change in ANSI C; use ... */
955 warning(118, mp->m_name);
956 }
957 } else if (!tflag && !sflag && !is_uinteger(olt) && !is_uinteger(ort) &&
958 portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
959 /*
960 * In traditional C the left operand would be extended,
961 * possibly with 1, and then shifted.
962 */
963 if (hflag && !Sflag &&
964 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
965 /* semantics of '%s' change in ANSI C; use ... */
966 warning(118, mp->m_name);
967 }
968 }
969 }
970
971 static void
972 typeok_shl(const mod_t *mp, tspec_t lt, tspec_t rt)
973 {
974 /*
975 * C90 does not perform balancing for shift operations,
976 * but traditional C does. If the width of the right operand
977 * is greater than the width of the left operand, then in
978 * traditional C the left operand would be extended to the
979 * width of the right operand. For SHL this may result in
980 * different results.
981 */
982 if (portable_size_in_bits(lt) < portable_size_in_bits(rt)) {
983 /*
984 * XXX If both operands are constant, make sure
985 * that there is really a difference between
986 * ANSI C and traditional C.
987 */
988 if (hflag && !Sflag)
989 /* semantics of '%s' change in ANSI C; use ... */
990 warning(118, mp->m_name);
991 }
992 }
993
994 static void
995 typeok_shift(tspec_t lt, const tnode_t *rn, tspec_t rt)
996 {
997 if (rn->tn_op != CON)
998 return;
999
1000 if (!is_uinteger(rt) && rn->tn_val->v_quad < 0) {
1001 /* negative shift */
1002 warning(121);
1003 } else if ((uint64_t)rn->tn_val->v_quad ==
1004 (uint64_t)size_in_bits(lt)) {
1005 /* shift equal to size of object */
1006 warning(267);
1007 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size_in_bits(lt)) {
1008 /* shift amount %llu is greater than bit-size %llu of '%s' */
1009 warning(122, (unsigned long long)rn->tn_val->v_quad,
1010 (unsigned long long)size_in_bits(lt),
1011 tspec_name(lt));
1012 }
1013 }
1014
1015 static bool
1016 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
1017 {
1018 if (lt == PTR && is_null_pointer(rn))
1019 return true;
1020 if (rt == PTR && is_null_pointer(ln))
1021 return true;
1022 return false;
1023 }
1024
1025 static bool
1026 typeok_compare(op_t op,
1027 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1028 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1029 {
1030 const char *lx, *rx;
1031
1032 if (lt == PTR && rt == PTR) {
1033 check_pointer_comparison(op, ln, rn);
1034 return true;
1035 }
1036
1037 if (lt != PTR && rt != PTR)
1038 return true;
1039
1040 if (!is_integer(lt) && !is_integer(rt)) {
1041 warn_incompatible_types(op, ltp, lt, rtp, rt);
1042 return false;
1043 }
1044
1045 lx = lt == PTR ? "pointer" : "integer";
1046 rx = rt == PTR ? "pointer" : "integer";
1047 /* illegal combination of %s '%s' and %s '%s', op '%s' */
1048 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
1049 return true;
1050 }
1051
1052 static bool
1053 typeok_quest(tspec_t lt, const tnode_t *rn)
1054 {
1055 if (!is_scalar(lt)) {
1056 /* first operand must have scalar type, op ? : */
1057 error(170);
1058 return false;
1059 }
1060 lint_assert(before_conversion(rn)->tn_op == COLON);
1061 return true;
1062 }
1063
1064 static void
1065 typeok_colon_pointer(const mod_t *mp, const type_t *ltp, const type_t *rtp)
1066 {
1067 type_t *lstp = ltp->t_subt;
1068 type_t *rstp = rtp->t_subt;
1069 tspec_t lst = lstp->t_tspec;
1070 tspec_t rst = rstp->t_tspec;
1071
1072 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
1073 /* (void *)0 handled above */
1074 if (sflag)
1075 /* ANSI C forbids conversion of %s to %s, op %s */
1076 warning(305, "function pointer", "'void *'",
1077 mp->m_name);
1078 return;
1079 }
1080
1081 if (eqptrtype(lstp, rstp, true))
1082 return;
1083 if (!eqtype(lstp, rstp, true, false, NULL))
1084 warn_incompatible_pointers(mp, ltp, rtp);
1085 }
1086
1087 static bool
1088 typeok_colon(const mod_t *mp,
1089 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1090 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1091 {
1092
1093 if (is_arithmetic(lt) && is_arithmetic(rt))
1094 return true;
1095 if (lt == BOOL && rt == BOOL)
1096 return true;
1097
1098 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
1099 return true;
1100 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
1101 return true;
1102
1103 if (lt == PTR && is_null_pointer(rn))
1104 return true;
1105 if (rt == PTR && is_null_pointer(ln))
1106 return true;
1107
1108 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
1109 const char *lx = lt == PTR ? "pointer" : "integer";
1110 const char *rx = rt == PTR ? "pointer" : "integer";
1111 /* illegal combination of %s '%s' and %s '%s', op '%s' */
1112 warning(123, lx, type_name(ltp),
1113 rx, type_name(rtp), mp->m_name);
1114 return true;
1115 }
1116
1117 if (lt == VOID || rt == VOID) {
1118 if (lt != VOID || rt != VOID)
1119 /* incompatible types '%s' and '%s' in conditional */
1120 warning(126, type_name(ltp), type_name(rtp));
1121 return true;
1122 }
1123
1124 if (lt == PTR && rt == PTR) {
1125 typeok_colon_pointer(mp, ltp, rtp);
1126 return true;
1127 }
1128
1129 /* incompatible types '%s' and '%s' in conditional */
1130 error(126, type_name(ltp), type_name(rtp));
1131 return false;
1132 }
1133
1134 static bool
1135 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
1136 {
1137 if (op == RETURN || op == INIT || op == FARG)
1138 return true;
1139
1140 if (!ln->tn_lvalue) {
1141 if (ln->tn_op == CVT && ln->tn_cast &&
1142 ln->tn_left->tn_op == LOAD) {
1143 /* a cast does not yield an lvalue */
1144 error(163);
1145 }
1146 /* %soperand of '%s' must be lvalue */
1147 error(114, "left ", op_name(op));
1148 return false;
1149 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1150 has_constant_member(ltp))) {
1151 if (!tflag)
1152 /* %soperand of '%s' must be modifiable lvalue */
1153 warning(115, "left ", op_name(op));
1154 }
1155 return true;
1156 }
1157
1158 /* Check the types using the information from modtab[]. */
1159 static bool
1160 typeok_scalar(op_t op, const mod_t *mp,
1161 const type_t *ltp, tspec_t lt,
1162 const type_t *rtp, tspec_t rt)
1163 {
1164 if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
1165 return true;
1166 if (mp->m_requires_integer) {
1167 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
1168 warn_incompatible_types(op, ltp, lt, rtp, rt);
1169 return false;
1170 }
1171 } else if (mp->m_requires_integer_or_complex) {
1172 if ((!is_integer(lt) && !is_complex(lt)) ||
1173 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
1174 warn_incompatible_types(op, ltp, lt, rtp, rt);
1175 return false;
1176 }
1177 } else if (mp->m_requires_scalar) {
1178 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
1179 warn_incompatible_types(op, ltp, lt, rtp, rt);
1180 return false;
1181 }
1182 } else if (mp->m_requires_arith) {
1183 if (!is_arithmetic(lt) ||
1184 (mp->m_binary && !is_arithmetic(rt))) {
1185 warn_incompatible_types(op, ltp, lt, rtp, rt);
1186 return false;
1187 }
1188 }
1189 return true;
1190 }
1191
1192 /*
1193 * Check the types for specific operators and type combinations.
1194 *
1195 * At this point, the operands already conform to the type requirements of
1196 * the operator, such as being integer, floating or scalar.
1197 */
1198 static bool
1199 typeok_op(op_t op, const mod_t *mp, int arg,
1200 const tnode_t *ln, const type_t *ltp, tspec_t lt,
1201 const tnode_t *rn, const type_t *rtp, tspec_t rt)
1202 {
1203 switch (op) {
1204 case ARROW:
1205 return typeok_arrow(lt);
1206 case POINT:
1207 return typeok_point(ln, ltp, lt);
1208 case INCBEF:
1209 case DECBEF:
1210 case INCAFT:
1211 case DECAFT:
1212 return typeok_incdec(op, ln, ltp);
1213 case INDIR:
1214 return typeok_indir(lt);
1215 case ADDR:
1216 return typeok_address(mp, ln, ltp, lt);
1217 case PLUS:
1218 return typeok_plus(op, ltp, lt, rtp, rt);
1219 case MINUS:
1220 return typeok_minus(op, ltp, lt, rtp, rt);
1221 case SHL:
1222 typeok_shl(mp, lt, rt);
1223 goto shift;
1224 case SHR:
1225 typeok_shr(mp, ln, lt, rn, rt);
1226 shift:
1227 typeok_shift(lt, rn, rt);
1228 break;
1229 case LT:
1230 case LE:
1231 case GT:
1232 case GE:
1233 compare:
1234 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
1235 case EQ:
1236 case NE:
1237 if (is_typeok_eq(ln, lt, rn, rt))
1238 break;
1239 goto compare;
1240 case QUEST:
1241 return typeok_quest(lt, rn);
1242 case COLON:
1243 return typeok_colon(mp, ln, ltp, lt, rn, rtp, rt);
1244 case ASSIGN:
1245 case INIT:
1246 case FARG:
1247 case RETURN:
1248 if (!check_assign_types_compatible(op, arg, ln, rn))
1249 return false;
1250 goto assign;
1251 case MULASS:
1252 case DIVASS:
1253 case MODASS:
1254 goto assign;
1255 case ADDASS:
1256 case SUBASS:
1257 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
1258 warn_incompatible_types(op, ltp, lt, rtp, rt);
1259 return false;
1260 }
1261 goto assign;
1262 case SHLASS:
1263 goto assign;
1264 case SHRASS:
1265 if (pflag && !is_uinteger(lt) && !(tflag && is_uinteger(rt))) {
1266 /* bitwise '%s' on signed value possibly nonportable */
1267 warning(117, mp->m_name);
1268 }
1269 goto assign;
1270 case ANDASS:
1271 case XORASS:
1272 case ORASS:
1273 assign:
1274 return typeok_assign(op, ln, ltp, lt);
1275 case COMMA:
1276 if (!modtab[ln->tn_op].m_has_side_effect)
1277 check_null_effect(ln);
1278 break;
1279 default:
1280 break;
1281 }
1282 return true;
1283 }
1284
1285 static void
1286 typeok_enum(op_t op, const mod_t *mp, int arg,
1287 const tnode_t *ln, const type_t *ltp,
1288 const tnode_t *rn, const type_t *rtp)
1289 {
1290 if (mp->m_bad_on_enum &&
1291 (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
1292 check_bad_enum_operation(op, ln, rn);
1293 } else if (mp->m_valid_on_enum &&
1294 (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
1295 check_enum_type_mismatch(op, arg, ln, rn);
1296 } else if (mp->m_valid_on_enum &&
1297 (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
1298 check_enum_int_mismatch(op, arg, ln, rn);
1299 }
1300 }
1301
1302 /* Perform most type checks. Return whether the types are ok. */
1303 bool
1304 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1305 {
1306 const mod_t *mp;
1307 tspec_t lt, rt;
1308 type_t *ltp, *rtp;
1309
1310 mp = &modtab[op];
1311
1312 lint_assert((ltp = ln->tn_type) != NULL);
1313 lt = ltp->t_tspec;
1314
1315 if (mp->m_binary) {
1316 lint_assert((rtp = rn->tn_type) != NULL);
1317 rt = rtp->t_tspec;
1318 } else {
1319 rtp = NULL;
1320 rt = NOTSPEC;
1321 }
1322
1323 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
1324 return false;
1325 if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
1326 return false;
1327
1328 if (!typeok_op(op, mp, arg, ln, ltp, lt, rn, rtp, rt))
1329 return false;
1330
1331 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
1332 return true;
1333 }
1334
1335 static void
1336 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
1337 {
1338 type_t *ltp, *rtp;
1339 tspec_t lst, rst;
1340 const char *lsts, *rsts;
1341
1342 lst = (ltp = ln->tn_type)->t_subt->t_tspec;
1343 rst = (rtp = rn->tn_type)->t_subt->t_tspec;
1344
1345 if (lst == VOID || rst == VOID) {
1346 if (sflag && (lst == FUNC || rst == FUNC)) {
1347 /* (void *)0 already handled in typeok() */
1348 *(lst == FUNC ? &lsts : &rsts) = "function pointer";
1349 *(lst == VOID ? &lsts : &rsts) = "'void *'";
1350 /* ANSI C forbids comparison of %s with %s */
1351 warning(274, lsts, rsts);
1352 }
1353 return;
1354 }
1355
1356 if (!eqtype(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
1357 warn_incompatible_pointers(&modtab[op], ltp, rtp);
1358 return;
1359 }
1360
1361 if (lst == FUNC && rst == FUNC) {
1362 if (sflag && op != EQ && op != NE)
1363 /* ANSI C forbids ordered comparisons of ... */
1364 warning(125);
1365 }
1366 }
1367
1368 static bool
1369 is_direct_function_call(const tnode_t *tn, const char **out_name)
1370 {
1371
1372 if (!(tn->tn_op == CALL &&
1373 tn->tn_left->tn_op == ADDR &&
1374 tn->tn_left->tn_left->tn_op == NAME))
1375 return false;
1376
1377 *out_name = tn->tn_left->tn_left->tn_sym->s_name;
1378 return true;
1379 }
1380
1381 static bool
1382 is_unconst_function(const char *name)
1383 {
1384
1385 return strcmp(name, "memchr") == 0 ||
1386 strcmp(name, "strchr") == 0 ||
1387 strcmp(name, "strpbrk") == 0 ||
1388 strcmp(name, "strrchr") == 0 ||
1389 strcmp(name, "strstr") == 0;
1390 }
1391
1392 static bool
1393 is_const_char_pointer(const tnode_t *tn)
1394 {
1395 const type_t *tp;
1396
1397 /*
1398 * For traditional reasons, C99 6.4.5p5 defines that string literals
1399 * have type 'char[]'. They are often implicitly converted to
1400 * 'char *', for example when they are passed as function arguments.
1401 *
1402 * C99 6.4.5p6 further defines that modifying a string that is
1403 * constructed from a string literal invokes undefined behavior.
1404 *
1405 * Out of these reasons, string literals are treated as 'effectively
1406 * const' here.
1407 */
1408 if (tn->tn_op == CVT &&
1409 tn->tn_left->tn_op == ADDR &&
1410 tn->tn_left->tn_left->tn_op == STRING)
1411 return true;
1412
1413 tp = before_conversion(tn)->tn_type;
1414 return tp->t_tspec == PTR &&
1415 tp->t_subt->t_tspec == CHAR &&
1416 tp->t_subt->t_const;
1417 }
1418
1419 static bool
1420 is_const_pointer(const tnode_t *tn)
1421 {
1422 const type_t *tp;
1423
1424 tp = before_conversion(tn)->tn_type;
1425 return tp->t_tspec == PTR && tp->t_subt->t_const;
1426 }
1427
1428 static bool
1429 is_first_arg_const_char_pointer(const tnode_t *tn)
1430 {
1431 const tnode_t *an;
1432
1433 an = tn->tn_right;
1434 if (an == NULL)
1435 return false;
1436
1437 while (an->tn_right != NULL)
1438 an = an->tn_right;
1439 return is_const_char_pointer(an->tn_left);
1440 }
1441
1442 static bool
1443 is_second_arg_const_pointer(const tnode_t *tn)
1444 {
1445 const tnode_t *an;
1446
1447 an = tn->tn_right;
1448 if (an == NULL || an->tn_right == NULL)
1449 return false;
1450
1451 while (an->tn_right->tn_right != NULL)
1452 an = an->tn_right;
1453 return is_const_pointer(an->tn_left);
1454 }
1455
1456 static void
1457 check_unconst_function(const type_t *lstp, const tnode_t *rn)
1458 {
1459 const char *function_name;
1460
1461 if (lstp->t_tspec == CHAR && !lstp->t_const &&
1462 is_direct_function_call(rn, &function_name) &&
1463 is_unconst_function(function_name) &&
1464 is_first_arg_const_char_pointer(rn)) {
1465 /* call to '%s' effectively discards 'const' from argument */
1466 warning(346, function_name);
1467 }
1468
1469 if (!lstp->t_const &&
1470 is_direct_function_call(rn, &function_name) &&
1471 strcmp(function_name, "bsearch") == 0 &&
1472 is_second_arg_const_pointer(rn)) {
1473 /* call to '%s' effectively discards 'const' from argument */
1474 warning(346, function_name);
1475 }
1476 }
1477
1478 static void
1479 check_assign_void_pointer(op_t op, int arg,
1480 tspec_t lt, tspec_t lst,
1481 tspec_t rt, tspec_t rst)
1482 {
1483 const char *lts, *rts;
1484
1485 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
1486 return;
1487 /* two pointers, at least one pointer to void */
1488
1489 if (!(sflag && (lst == FUNC || rst == FUNC)))
1490 return;
1491 /* comb. of ptr to func and ptr to void */
1492
1493 *(lst == FUNC ? <s : &rts) = "function pointer";
1494 *(lst == VOID ? <s : &rts) = "'void *'";
1495
1496 switch (op) {
1497 case INIT:
1498 case RETURN:
1499 /* ANSI C forbids conversion of %s to %s */
1500 warning(303, rts, lts);
1501 break;
1502 case FARG:
1503 /* ANSI C forbids conversion of %s to %s, arg #%d */
1504 warning(304, rts, lts, arg);
1505 break;
1506 default:
1507 /* ANSI C forbids conversion of %s to %s, op %s */
1508 warning(305, rts, lts, op_name(op));
1509 break;
1510 }
1511 }
1512
1513 static bool
1514 check_assign_void_pointer_compat(op_t op, int arg,
1515 const type_t *const ltp, tspec_t const lt,
1516 const type_t *const lstp, tspec_t const lst,
1517 const tnode_t *const rn,
1518 const type_t *const rtp, tspec_t const rt,
1519 const type_t *const rstp, tspec_t const rst)
1520 {
1521 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1522 eqtype(lstp, rstp, true, false, NULL))))
1523 return false;
1524
1525 /* compatible pointer types (qualifiers ignored) */
1526 if (!tflag &&
1527 ((!lstp->t_const && rstp->t_const) ||
1528 (!lstp->t_volatile && rstp->t_volatile))) {
1529 /* left side has not all qualifiers of right */
1530 switch (op) {
1531 case INIT:
1532 case RETURN:
1533 /* incompatible pointer types (%s != %s) */
1534 warning(182, type_name(lstp), type_name(rstp));
1535 break;
1536 case FARG:
1537 /* converting '%s' to incompatible '%s' ... */
1538 warning(153,
1539 type_name(rtp), type_name(ltp), arg);
1540 break;
1541 default:
1542 /* operands have incompatible pointer type... */
1543 warning(128, op_name(op),
1544 type_name(lstp), type_name(rstp));
1545 break;
1546 }
1547 }
1548
1549 if (!tflag)
1550 check_unconst_function(lstp, rn);
1551
1552 return true;
1553 }
1554
1555 static bool
1556 check_assign_pointer_integer(op_t op, int arg,
1557 const type_t *const ltp, tspec_t const lt,
1558 const type_t *const rtp, tspec_t const rt)
1559 {
1560 const char *lx, *rx;
1561
1562 if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
1563 return false;
1564
1565 lx = lt == PTR ? "pointer" : "integer";
1566 rx = rt == PTR ? "pointer" : "integer";
1567
1568 switch (op) {
1569 case INIT:
1570 case RETURN:
1571 /* illegal combination of %s (%s) and %s (%s) */
1572 warning(183, lx, type_name(ltp), rx, type_name(rtp));
1573 break;
1574 case FARG:
1575 /* illegal combination of %s (%s) and %s (%s), arg #%d */
1576 warning(154,
1577 lx, type_name(ltp), rx, type_name(rtp), arg);
1578 break;
1579 default:
1580 /* illegal combination of %s '%s' and %s '%s', op '%s' */
1581 warning(123,
1582 lx, type_name(ltp), rx, type_name(rtp), op_name(op));
1583 break;
1584 }
1585 return true;
1586 }
1587
1588 static bool
1589 check_assign_pointer(op_t op, int arg,
1590 const type_t *ltp, tspec_t lt,
1591 const type_t *rtp, tspec_t rt)
1592 {
1593 if (!(lt == PTR && rt == PTR))
1594 return false;
1595
1596 switch (op) {
1597 case RETURN:
1598 warn_incompatible_pointers(NULL, ltp, rtp);
1599 break;
1600 case FARG:
1601 /* converting '%s' to incompatible '%s' for ... */
1602 warning(153, type_name(rtp), type_name(ltp), arg);
1603 break;
1604 default:
1605 warn_incompatible_pointers(&modtab[op], ltp, rtp);
1606 break;
1607 }
1608 return true;
1609 }
1610
1611 static void
1612 warn_assign(op_t op, int arg,
1613 const type_t *ltp, tspec_t lt,
1614 const type_t *rtp, tspec_t rt)
1615 {
1616 switch (op) {
1617 case INIT:
1618 /* cannot initialize '%s' from '%s' */
1619 error(185, type_name(ltp), type_name(rtp));
1620 break;
1621 case RETURN:
1622 /* return value type mismatch (%s) and (%s) */
1623 error(211, type_name(ltp), type_name(rtp));
1624 break;
1625 case FARG:
1626 /* passing '%s' to incompatible '%s', arg #%d */
1627 warning(155, type_name(rtp), type_name(ltp), arg);
1628 break;
1629 default:
1630 warn_incompatible_types(op, ltp, lt, rtp, rt);
1631 break;
1632 }
1633 }
1634
1635 /*
1636 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1637 * and prints warnings/errors if necessary.
1638 * Returns whether the types are (almost) compatible.
1639 */
1640 static bool
1641 check_assign_types_compatible(op_t op, int arg,
1642 const tnode_t *ln, const tnode_t *rn)
1643 {
1644 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1645 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1646
1647 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1648 lst = (lstp = ltp->t_subt)->t_tspec;
1649 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1650 rst = (rstp = rtp->t_subt)->t_tspec;
1651
1652 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
1653 return true;
1654
1655 if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
1656 return true;
1657
1658 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1659 /* both are struct or union */
1660 return ltp->t_str == rtp->t_str;
1661
1662 /* a null pointer may be assigned to any pointer */
1663 if (lt == PTR && is_null_pointer(rn))
1664 return true;
1665
1666 check_assign_void_pointer(op, arg, lt, lst, rt, rst);
1667
1668 if (check_assign_void_pointer_compat(op, arg,
1669 ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
1670 return true;
1671
1672 if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
1673 return true;
1674
1675 if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
1676 return true;
1677
1678 warn_assign(op, arg, ltp, lt, rtp, rt);
1679 return false;
1680 }
1681
1682 /* Prints a warning if a strange operator is used on an enum type. */
1683 static void
1684 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
1685 {
1686
1687 if (!eflag)
1688 return;
1689
1690 /*
1691 * Enum as offset to a pointer is an exception (otherwise enums
1692 * could not be used as array indices).
1693 */
1694 if (op == PLUS &&
1695 ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
1696 (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
1697 return;
1698 }
1699
1700 /* dubious operation on enum, op %s */
1701 warning(241, op_name(op));
1702 }
1703
1704 /*
1705 * Prints a warning if an operator is applied to two different enum types.
1706 */
1707 static void
1708 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1709 {
1710 const mod_t *mp;
1711
1712 mp = &modtab[op];
1713
1714 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1715 switch (op) {
1716 case INIT:
1717 /* enum type mismatch between '%s' and '%s' in ... */
1718 warning(210,
1719 type_name(ln->tn_type), type_name(rn->tn_type));
1720 break;
1721 case FARG:
1722 /* enum type mismatch, arg #%d (%s != %s) */
1723 warning(156, arg,
1724 type_name(ln->tn_type), type_name(rn->tn_type));
1725 break;
1726 case RETURN:
1727 /* return value type mismatch (%s) and (%s) */
1728 warning(211,
1729 type_name(ln->tn_type), type_name(rn->tn_type));
1730 break;
1731 default:
1732 /* enum type mismatch: '%s' '%s' '%s' */
1733 warning(130, type_name(ln->tn_type), mp->m_name,
1734 type_name(rn->tn_type));
1735 break;
1736 }
1737 } else if (Pflag && mp->m_comparison && op != EQ && op != NE) {
1738 if (eflag)
1739 /* dubious comparison of enums, op %s */
1740 warning(243, mp->m_name);
1741 }
1742 }
1743
1744 /* Prints a warning if the operands mix between enum and integer. */
1745 static void
1746 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
1747 {
1748
1749 if (!eflag)
1750 return;
1751
1752 switch (op) {
1753 case INIT:
1754 /*
1755 * Initialization with 0 is allowed. Otherwise, all implicit
1756 * initializations would need to be warned upon as well.
1757 */
1758 if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
1759 is_integer(rn->tn_type->t_tspec) &&
1760 rn->tn_val->v_quad == 0) {
1761 return;
1762 }
1763 /* initialization of '%s' with '%s' */
1764 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
1765 break;
1766 case FARG:
1767 /* combination of '%s' and '%s', arg #%d */
1768 warning(278,
1769 type_name(ln->tn_type), type_name(rn->tn_type), arg);
1770 break;
1771 case RETURN:
1772 /* combination of '%s' and '%s' in return */
1773 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
1774 break;
1775 default:
1776 /* combination of '%s' and '%s', op %s */
1777 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
1778 op_name(op));
1779 break;
1780 }
1781 }
1782
1783 static void
1784 check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
1785 {
1786 int max_array_index;
1787 int64_t max_enum_value;
1788 const struct sym *ec, *max_ec;
1789 const type_t *lt, *rt;
1790
1791 if (ln->tn_op != ADDR || ln->tn_left->tn_op != NAME)
1792 return;
1793
1794 lt = ln->tn_left->tn_type;
1795 if (lt->t_tspec != ARRAY || lt->t_incomplete_array)
1796 return;
1797
1798 if (rn->tn_op != CVT || !rn->tn_type->t_is_enum)
1799 return;
1800 if (rn->tn_left->tn_op != LOAD)
1801 return;
1802
1803 rt = rn->tn_left->tn_type;
1804 ec = rt->t_enum->en_first_enumerator;
1805 max_ec = ec;
1806 lint_assert(ec != NULL);
1807 for (ec = ec->s_next; ec != NULL; ec = ec->s_next)
1808 if (ec->u.s_enum_constant > max_ec->u.s_enum_constant)
1809 max_ec = ec;
1810
1811 max_enum_value = max_ec->u.s_enum_constant;
1812 lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX);
1813
1814 max_array_index = lt->t_dim - 1;
1815 if (max_enum_value == max_array_index)
1816 return;
1817
1818 /*
1819 * If the largest enum constant is named '*_NUM_*', it is typically
1820 * not part of the allowed enum values but a marker for the number
1821 * of actual enum values.
1822 */
1823 if (max_enum_value == max_array_index + 1 &&
1824 (strstr(max_ec->s_name, "NUM") != NULL ||
1825 strstr(max_ec->s_name, "num") != NULL))
1826 return;
1827
1828 /* maximum value %d of '%s' does not match maximum array index %d */
1829 warning(348, (int)max_enum_value, type_name(rt), max_array_index);
1830 print_previous_declaration(-1, max_ec);
1831 }
1832
1833 /*
1834 * Build and initialize a new node.
1835 */
1836 static tnode_t *
1837 new_tnode(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
1838 {
1839 tnode_t *ntn;
1840 tspec_t t;
1841 #if 0 /* not yet */
1842 size_t l;
1843 uint64_t rnum;
1844 #endif
1845
1846 ntn = expr_alloc_tnode();
1847
1848 ntn->tn_op = op;
1849 ntn->tn_type = type;
1850 ntn->tn_sys = sys;
1851 ntn->tn_left = ln;
1852 ntn->tn_right = rn;
1853
1854 switch (op) {
1855 #if 0 /* not yet */
1856 case SHR:
1857 if (rn->tn_op != CON)
1858 break;
1859 rnum = rn->tn_val->v_quad;
1860 l = type_size_in_bits(ln->tn_type) / CHAR_SIZE;
1861 t = ln->tn_type->t_tspec;
1862 switch (l) {
1863 case 8:
1864 if (rnum >= 56)
1865 t = UCHAR;
1866 else if (rnum >= 48)
1867 t = USHORT;
1868 else if (rnum >= 32)
1869 t = UINT;
1870 break;
1871 case 4:
1872 if (rnum >= 24)
1873 t = UCHAR;
1874 else if (rnum >= 16)
1875 t = USHORT;
1876 break;
1877 case 2:
1878 if (rnum >= 8)
1879 t = UCHAR;
1880 break;
1881 default:
1882 break;
1883 }
1884 if (t != ln->tn_type->t_tspec)
1885 ntn->tn_type->t_tspec = t;
1886 break;
1887 #endif
1888 case INDIR:
1889 case FSEL:
1890 lint_assert(ln->tn_type->t_tspec == PTR);
1891 t = ln->tn_type->t_subt->t_tspec;
1892 if (t != FUNC && t != VOID)
1893 ntn->tn_lvalue = true;
1894 break;
1895 default:
1896 break;
1897 }
1898
1899 return ntn;
1900 }
1901
1902 /*
1903 * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
1904 * integer types to either int or unsigned int.
1905 *
1906 * If tflag is set or the operand is a function argument with no type
1907 * information (no prototype or variable # of args), converts float to double.
1908 */
1909 tnode_t *
1910 promote(op_t op, bool farg, tnode_t *tn)
1911 {
1912 tspec_t t;
1913 type_t *ntp;
1914 unsigned int len;
1915
1916 t = tn->tn_type->t_tspec;
1917
1918 if (!is_arithmetic(t))
1919 return tn;
1920
1921 if (!tflag) {
1922 /*
1923 * C99 6.3.1.1p2 requires for types with lower rank than int
1924 * that "If an int can represent all the values of the
1925 * original type, the value is converted to an int; otherwise
1926 * it is converted to an unsigned int", and that "All other
1927 * types are unchanged by the integer promotions".
1928 */
1929 if (tn->tn_type->t_bitfield) {
1930 len = tn->tn_type->t_flen;
1931 if (len < size_in_bits(INT)) {
1932 t = INT;
1933 } else if (len == size_in_bits(INT)) {
1934 t = is_uinteger(t) ? UINT : INT;
1935 }
1936 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1937 t = (size_in_bits(CHAR) < size_in_bits(INT)
1938 || t != UCHAR) ? INT : UINT;
1939 } else if (t == SHORT || t == USHORT) {
1940 t = (size_in_bits(SHORT) < size_in_bits(INT)
1941 || t == SHORT) ? INT : UINT;
1942 } else if (t == ENUM) {
1943 t = INT;
1944 } else if (farg && t == FLOAT) {
1945 t = DOUBLE;
1946 }
1947 } else {
1948 /*
1949 * In traditional C, keep unsigned and promote FLOAT
1950 * to DOUBLE.
1951 */
1952 if (t == UCHAR || t == USHORT) {
1953 t = UINT;
1954 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1955 t = INT;
1956 } else if (t == FLOAT) {
1957 t = DOUBLE;
1958 } else if (t == ENUM) {
1959 t = INT;
1960 }
1961 }
1962
1963 if (t != tn->tn_type->t_tspec) {
1964 ntp = expr_dup_type(tn->tn_type);
1965 ntp->t_tspec = t;
1966 /*
1967 * Keep t_is_enum even though t_tspec gets converted from
1968 * ENUM to INT, so we are later able to check compatibility
1969 * of enum types.
1970 */
1971 tn = convert(op, 0, ntp, tn);
1972 }
1973
1974 return tn;
1975 }
1976
1977 /*
1978 * Apply the "usual arithmetic conversions" (C99 6.3.1.8).
1979 *
1980 * This gives both operands the same type.
1981 * This is done in different ways for traditional C and C90.
1982 */
1983 static void
1984 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1985 {
1986 tspec_t lt, rt, t;
1987 int i;
1988 bool u;
1989 type_t *ntp;
1990 static const tspec_t tl[] = {
1991 LDOUBLE, DOUBLE, FLOAT,
1992 #ifdef INT128_SIZE
1993 UINT128, INT128,
1994 #endif
1995 UQUAD, QUAD,
1996 ULONG, LONG,
1997 UINT, INT,
1998 };
1999
2000 lt = (*lnp)->tn_type->t_tspec;
2001 rt = (*rnp)->tn_type->t_tspec;
2002
2003 if (!is_arithmetic(lt) || !is_arithmetic(rt))
2004 return;
2005
2006 if (!tflag) {
2007 if (lt == rt) {
2008 t = lt;
2009 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
2010 t = LCOMPLEX;
2011 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
2012 t = DCOMPLEX;
2013 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
2014 t = FCOMPLEX;
2015 } else if (lt == LDOUBLE || rt == LDOUBLE) {
2016 t = LDOUBLE;
2017 } else if (lt == DOUBLE || rt == DOUBLE) {
2018 t = DOUBLE;
2019 } else if (lt == FLOAT || rt == FLOAT) {
2020 t = FLOAT;
2021 } else {
2022 /*
2023 * If type A has more bits than type B it should
2024 * be able to hold all possible values of type B.
2025 */
2026 if (size_in_bits(lt) > size_in_bits(rt)) {
2027 t = lt;
2028 } else if (size_in_bits(lt) < size_in_bits(rt)) {
2029 t = rt;
2030 } else {
2031 for (i = 3; tl[i] != INT; i++) {
2032 if (tl[i] == lt || tl[i] == rt)
2033 break;
2034 }
2035 if ((is_uinteger(lt) || is_uinteger(rt)) &&
2036 !is_uinteger(tl[i])) {
2037 i--;
2038 }
2039 t = tl[i];
2040 }
2041 }
2042 } else {
2043 /* Keep unsigned in traditional C */
2044 u = is_uinteger(lt) || is_uinteger(rt);
2045 for (i = 0; tl[i] != INT; i++) {
2046 if (lt == tl[i] || rt == tl[i])
2047 break;
2048 }
2049 t = tl[i];
2050 if (u && is_integer(t) && !is_uinteger(t))
2051 t = unsigned_type(t);
2052 }
2053
2054 if (t != lt) {
2055 ntp = expr_dup_type((*lnp)->tn_type);
2056 ntp->t_tspec = t;
2057 *lnp = convert(op, 0, ntp, *lnp);
2058 }
2059 if (t != rt) {
2060 ntp = expr_dup_type((*rnp)->tn_type);
2061 ntp->t_tspec = t;
2062 *rnp = convert(op, 0, ntp, *rnp);
2063 }
2064 }
2065
2066 /*
2067 * Insert a conversion operator, which converts the type of the node
2068 * to another given type.
2069 * If op is FARG, arg is the number of the argument (used for warnings).
2070 */
2071 tnode_t *
2072 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
2073 {
2074 tnode_t *ntn;
2075 tspec_t nt, ot;
2076
2077 nt = tp->t_tspec;
2078 ot = tn->tn_type->t_tspec;
2079
2080 if (!tflag && !sflag && !Sflag && 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 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3593 * But this seems really questionable.
3594 */
3595 } else if (nt == UNION) {
3596 sym_t *m;
3597 struct_or_union *str = tp->t_str;
3598 if (!gflag) {
3599 /* union cast is a GCC extension */
3600 error(328);
3601 return NULL;
3602 }
3603 for (m = str->sou_first_member; m != NULL; m = m->s_next) {
3604 if (eqtype(m->s_type, tn->tn_type,
3605 false, false, NULL)) {
3606 tn = expr_alloc_tnode();
3607 tn->tn_op = CVT;
3608 tn->tn_type = tp;
3609 tn->tn_cast = true;
3610 tn->tn_right = NULL;
3611 return tn;
3612 }
3613 }
3614 /* type '%s' is not a member of '%s' */
3615 error(329, type_name(tn->tn_type), type_name(tp));
3616 return NULL;
3617 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3618 /* Casting to a struct is an undocumented GCC extension. */
3619 if (!(gflag && nt == STRUCT))
3620 goto invalid_cast;
3621 } else if (ot == STRUCT || ot == UNION) {
3622 goto invalid_cast;
3623 } else if (ot == VOID) {
3624 /* improper cast of void expression */
3625 error(148);
3626 return NULL;
3627 } else if (is_integer(nt) && is_scalar(ot)) {
3628 /* ok */
3629 } else if (is_floating(nt) && is_arithmetic(ot)) {
3630 /* ok */
3631 } else if (nt == PTR && is_integer(ot)) {
3632 /* ok */
3633 } else if (nt == PTR && ot == PTR) {
3634 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3635 if (hflag)
3636 /* cast discards 'const' from type '%s' */
3637 warning(275, type_name(tn->tn_type));
3638 }
3639 } else
3640 goto invalid_cast;
3641
3642 tn = convert(CVT, 0, tp, tn);
3643 tn->tn_cast = true;
3644
3645 return tn;
3646
3647 invalid_cast:
3648 /* invalid cast from '%s' to '%s' */
3649 error(147, type_name(tn->tn_type), type_name(tp));
3650 return NULL;
3651 }
3652
3653 /*
3654 * Create the node for a function argument.
3655 * All necessary conversions and type checks are done in
3656 * build_function_call because build_function_argument has no
3657 * information about expected argument types.
3658 */
3659 tnode_t *
3660 build_function_argument(tnode_t *args, tnode_t *arg)
3661 {
3662 tnode_t *ntn;
3663
3664 /*
3665 * If there was a serious error in the expression for the argument,
3666 * create a dummy argument so the positions of the remaining arguments
3667 * will not change.
3668 */
3669 if (arg == NULL)
3670 arg = build_integer_constant(INT, 0);
3671
3672 ntn = new_tnode(PUSH, arg->tn_sys, arg->tn_type, arg, args);
3673
3674 return ntn;
3675 }
3676
3677 /*
3678 * Create the node for a function call. Also check types of
3679 * function arguments and insert conversions, if necessary.
3680 */
3681 tnode_t *
3682 build_function_call(tnode_t *func, bool sys, tnode_t *args)
3683 {
3684 tnode_t *ntn;
3685 op_t fcop;
3686
3687 if (func == NULL)
3688 return NULL;
3689
3690 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3691 fcop = CALL;
3692 } else {
3693 fcop = ICALL;
3694 }
3695
3696 check_ctype_function_call(func, args);
3697
3698 /*
3699 * after cconv() func will always be a pointer to a function
3700 * if it is a valid function designator.
3701 */
3702 func = cconv(func);
3703
3704 if (func->tn_type->t_tspec != PTR ||
3705 func->tn_type->t_subt->t_tspec != FUNC) {
3706 /* illegal function (type %s) */
3707 error(149, type_name(func->tn_type));
3708 return NULL;
3709 }
3710
3711 args = check_function_arguments(func->tn_type->t_subt, args);
3712
3713 ntn = new_tnode(fcop, sys, func->tn_type->t_subt->t_subt, func, args);
3714
3715 return ntn;
3716 }
3717
3718 /*
3719 * Check types of all function arguments and insert conversions,
3720 * if necessary.
3721 */
3722 static tnode_t *
3723 check_function_arguments(type_t *ftp, tnode_t *args)
3724 {
3725 tnode_t *arg;
3726 sym_t *asym;
3727 tspec_t at;
3728 int narg, npar, n, i;
3729
3730 /* get # of args in the prototype */
3731 npar = 0;
3732 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3733 npar++;
3734
3735 /* get # of args in function call */
3736 narg = 0;
3737 for (arg = args; arg != NULL; arg = arg->tn_right)
3738 narg++;
3739
3740 asym = ftp->t_args;
3741 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3742 /* argument mismatch: %d arg%s passed, %d expected */
3743 error(150, narg, narg > 1 ? "s" : "", npar);
3744 asym = NULL;
3745 }
3746
3747 for (n = 1; n <= narg; n++) {
3748
3749 /*
3750 * The rightmost argument is at the top of the argument
3751 * subtree.
3752 */
3753 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3754 continue;
3755
3756 /* some things which are always not allowed */
3757 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3758 /* void expressions may not be arguments, arg #%d */
3759 error(151, n);
3760 return NULL;
3761 } else if ((at == STRUCT || at == UNION) &&
3762 is_incomplete(arg->tn_left->tn_type)) {
3763 /* argument cannot have unknown size, arg #%d */
3764 error(152, n);
3765 return NULL;
3766 } else if (is_integer(at) &&
3767 arg->tn_left->tn_type->t_is_enum &&
3768 is_incomplete(arg->tn_left->tn_type)) {
3769 /* argument cannot have unknown size, arg #%d */
3770 warning(152, n);
3771 }
3772
3773 /* class conversions (arg in value context) */
3774 arg->tn_left = cconv(arg->tn_left);
3775
3776 if (asym != NULL) {
3777 arg->tn_left = check_prototype_argument(
3778 n, asym->s_type, arg->tn_left);
3779 } else {
3780 arg->tn_left = promote(NOOP, true, arg->tn_left);
3781 }
3782 arg->tn_type = arg->tn_left->tn_type;
3783
3784 if (asym != NULL)
3785 asym = asym->s_next;
3786 }
3787
3788 return args;
3789 }
3790
3791 /*
3792 * Compare the type of an argument with the corresponding type of a
3793 * prototype parameter. If it is a valid combination, but both types
3794 * are not the same, insert a conversion to convert the argument into
3795 * the type of the parameter.
3796 */
3797 static tnode_t *
3798 check_prototype_argument(
3799 int n, /* pos of arg */
3800 type_t *tp, /* expected type (from prototype) */
3801 tnode_t *tn) /* argument */
3802 {
3803 tnode_t *ln;
3804 bool dowarn;
3805
3806 ln = xcalloc(1, sizeof(*ln));
3807 ln->tn_type = expr_unqualified_type(tp);
3808 ln->tn_lvalue = true;
3809 if (typeok(FARG, n, ln, tn)) {
3810 if (!eqtype(tp, tn->tn_type,
3811 true, false, (dowarn = false, &dowarn)) || dowarn)
3812 tn = convert(FARG, n, tp, tn);
3813 }
3814 free(ln);
3815 return tn;
3816 }
3817
3818 /*
3819 * Return the value of an integral constant expression.
3820 * If the expression is not constant or its type is not an integer
3821 * type, an error message is printed.
3822 */
3823 val_t *
3824 constant(tnode_t *tn, bool required)
3825 {
3826 val_t *v;
3827
3828 if (tn != NULL)
3829 tn = cconv(tn);
3830 if (tn != NULL)
3831 tn = promote(NOOP, false, tn);
3832
3833 v = xcalloc(1, sizeof(*v));
3834
3835 if (tn == NULL) {
3836 lint_assert(nerr != 0);
3837 debug_step("constant node is null; returning 1 instead");
3838 v->v_tspec = INT;
3839 v->v_quad = 1;
3840 return v;
3841 }
3842
3843 v->v_tspec = tn->tn_type->t_tspec;
3844
3845 if (tn->tn_op == CON) {
3846 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3847 if (is_integer(tn->tn_val->v_tspec)) {
3848 v->v_unsigned_since_c90 =
3849 tn->tn_val->v_unsigned_since_c90;
3850 v->v_quad = tn->tn_val->v_quad;
3851 return v;
3852 }
3853 v->v_quad = tn->tn_val->v_ldbl;
3854 } else {
3855 v->v_quad = 1;
3856 }
3857
3858 if (required)
3859 /* integral constant expression expected */
3860 error(55);
3861 else
3862 /* variable array dimension is a C99/GCC extension */
3863 c99ism(318);
3864
3865 if (!is_integer(v->v_tspec))
3866 v->v_tspec = INT;
3867
3868 return v;
3869 }
3870
3871 static bool
3872 is_constcond_false(const tnode_t *tn, tspec_t t)
3873 {
3874 return (t == BOOL || t == INT) &&
3875 tn->tn_op == CON && tn->tn_val->v_quad == 0;
3876 }
3877
3878 /*
3879 * Perform some tests on expressions which can't be done in build_binary()
3880 * and functions called by build_binary(). These tests must be done here
3881 * because we need some information about the context in which the operations
3882 * are performed.
3883 * After all tests are performed and dofreeblk is true, expr() frees the
3884 * memory which is used for the expression.
3885 */
3886 void
3887 expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool is_do_while)
3888 {
3889
3890 if (tn == NULL) { /* in case of errors */
3891 expr_free_all();
3892 return;
3893 }
3894
3895 /* expr() is also called in global initializations */
3896 if (dcs->d_kind != DK_EXTERN && !is_do_while)
3897 check_statement_reachable();
3898
3899 check_expr_misc(tn, vctx, tctx, !tctx, false, false, false);
3900 if (tn->tn_op == ASSIGN) {
3901 if (hflag && tctx)
3902 /* assignment in conditional context */
3903 warning(159);
3904 } else if (tn->tn_op == CON) {
3905 if (hflag && tctx && !constcond_flag &&
3906 !tn->tn_system_dependent &&
3907 !(is_do_while &&
3908 is_constcond_false(tn, tn->tn_type->t_tspec)))
3909 /* constant in conditional context */
3910 warning(161);
3911 }
3912 if (!modtab[tn->tn_op].m_has_side_effect) {
3913 /*
3914 * for left operands of COMMA this warning is already
3915 * printed
3916 */
3917 if (tn->tn_op != COMMA && !vctx && !tctx)
3918 check_null_effect(tn);
3919 }
3920 debug_node(tn);
3921
3922 /* free the tree memory */
3923 if (dofreeblk)
3924 expr_free_all();
3925 }
3926
3927 static bool
3928 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
3929 {
3930 op_t op = tn->tn_op;
3931
3932 if (modtab[op].m_has_side_effect)
3933 return true;
3934
3935 if (op == CVT && tn->tn_type->t_tspec == VOID)
3936 return has_side_effect(tn->tn_left);
3937
3938 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3939 if (op == LOGAND || op == LOGOR)
3940 return has_side_effect(tn->tn_right);
3941
3942 /* XXX: Why not has_side_effect(tn->tn_left) as well? */
3943 if (op == QUEST)
3944 return has_side_effect(tn->tn_right);
3945
3946 if (op == COLON || op == COMMA) {
3947 return has_side_effect(tn->tn_left) ||
3948 has_side_effect(tn->tn_right);
3949 }
3950
3951 return false;
3952 }
3953
3954 static bool
3955 is_void_cast(const tnode_t *tn)
3956 {
3957
3958 return tn->tn_op == CVT && tn->tn_cast &&
3959 tn->tn_type->t_tspec == VOID;
3960 }
3961
3962 static bool
3963 is_local_symbol(const tnode_t *tn)
3964 {
3965
3966 return tn->tn_op == LOAD &&
3967 tn->tn_left->tn_op == NAME &&
3968 tn->tn_left->tn_sym->s_scl == AUTO;
3969 }
3970
3971 static bool
3972 is_int_constant_zero(const tnode_t *tn)
3973 {
3974
3975 return tn->tn_op == CON &&
3976 tn->tn_type->t_tspec == INT &&
3977 tn->tn_val->v_quad == 0;
3978 }
3979
3980 static void
3981 check_null_effect(const tnode_t *tn)
3982 {
3983
3984 if (!hflag)
3985 return;
3986 if (has_side_effect(tn))
3987 return;
3988 if (is_void_cast(tn) && is_local_symbol(tn->tn_left))
3989 return;
3990 if (is_void_cast(tn) && is_int_constant_zero(tn->tn_left))
3991 return;
3992
3993 /* expression has null effect */
3994 warning(129);
3995 }
3996
3997 static void
3998 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
3999 {
4000 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4001 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4002 if (!szof)
4003 mark_as_set(ln->tn_sym);
4004 mark_as_used(ln->tn_sym, fcall, szof);
4005 }
4006 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4007 /* check the range of array indices */
4008 check_array_index(ln->tn_left, true);
4009 }
4010
4011 static void
4012 check_expr_load(const tnode_t *ln)
4013 {
4014 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4015 /* check the range of array indices */
4016 check_array_index(ln->tn_left, false);
4017 }
4018
4019 static void
4020 check_expr_side_effect(const tnode_t *ln, bool szof)
4021 {
4022 scl_t sc;
4023 dinfo_t *di;
4024
4025 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4026 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4027 sc = ln->tn_sym->s_scl;
4028 /*
4029 * Look if there was a asm statement in one of the
4030 * compound statements we are in. If not, we don't
4031 * print a warning.
4032 */
4033 for (di = dcs; di != NULL; di = di->d_enclosing) {
4034 if (di->d_asm)
4035 break;
4036 }
4037 if (sc != EXTERN && sc != STATIC &&
4038 !ln->tn_sym->s_set && !szof && di == NULL) {
4039 /* %s may be used before set */
4040 warning(158, ln->tn_sym->s_name);
4041 mark_as_set(ln->tn_sym);
4042 }
4043 mark_as_used(ln->tn_sym, false, false);
4044 }
4045 }
4046
4047 static void
4048 check_expr_assign(const tnode_t *ln, bool szof)
4049 {
4050 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4051 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4052 mark_as_set(ln->tn_sym);
4053 if (ln->tn_sym->s_scl == EXTERN)
4054 outusg(ln->tn_sym);
4055 }
4056 if (ln->tn_op == INDIR && ln->tn_left->tn_op == PLUS)
4057 /* check the range of array indices */
4058 check_array_index(ln->tn_left, false);
4059 }
4060
4061 static void
4062 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4063 bool szof, bool vctx, bool tctx, bool retval_discarded)
4064 {
4065 lint_assert(ln->tn_op == ADDR);
4066 lint_assert(ln->tn_left->tn_op == NAME);
4067 if (!szof &&
4068 !is_compiler_builtin(ln->tn_left->tn_sym->s_name))
4069 outcall(tn, vctx || tctx, retval_discarded);
4070 }
4071
4072 static bool
4073 check_expr_op(const tnode_t *tn, op_t op, const tnode_t *ln,
4074 bool szof, bool fcall, bool vctx, bool tctx,
4075 bool retval_discarded, bool eqwarn)
4076 {
4077 switch (op) {
4078 case ADDR:
4079 check_expr_addr(ln, szof, fcall);
4080 break;
4081 case LOAD:
4082 check_expr_load(ln);
4083 /* FALLTHROUGH */
4084 case PUSH:
4085 case INCBEF:
4086 case DECBEF:
4087 case INCAFT:
4088 case DECAFT:
4089 case ADDASS:
4090 case SUBASS:
4091 case MULASS:
4092 case DIVASS:
4093 case MODASS:
4094 case ANDASS:
4095 case ORASS:
4096 case XORASS:
4097 case SHLASS:
4098 case SHRASS:
4099 case REAL:
4100 case IMAG:
4101 check_expr_side_effect(ln, szof);
4102 break;
4103 case ASSIGN:
4104 check_expr_assign(ln, szof);
4105 break;
4106 case CALL:
4107 check_expr_call(tn, ln, szof, vctx, tctx, retval_discarded);
4108 break;
4109 case EQ:
4110 if (hflag && eqwarn)
4111 /* operator '==' found where '=' was expected */
4112 warning(160);
4113 break;
4114 case CON:
4115 case NAME:
4116 case STRING:
4117 return false;
4118 /* LINTED206: (enumeration values not handled in switch) */
4119 case BITOR:
4120 case BITXOR:
4121 case NE:
4122 case GE:
4123 case GT:
4124 case LE:
4125 case LT:
4126 case SHR:
4127 case SHL:
4128 case MINUS:
4129 case PLUS:
4130 case MOD:
4131 case DIV:
4132 case MULT:
4133 case INDIR:
4134 case UMINUS:
4135 case UPLUS:
4136 case DEC:
4137 case INC:
4138 case COMPL:
4139 case NOT:
4140 case POINT:
4141 case ARROW:
4142 case NOOP:
4143 case BITAND:
4144 case FARG:
4145 case CASE:
4146 case INIT:
4147 case RETURN:
4148 case ICALL:
4149 case CVT:
4150 case COMMA:
4151 case FSEL:
4152 case COLON:
4153 case QUEST:
4154 case LOGOR:
4155 case LOGAND:
4156 break;
4157 }
4158 return true;
4159 }
4160
4161 void
4162 check_expr_misc(const tnode_t *tn, bool vctx, bool tctx,
4163 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4164 {
4165 tnode_t *ln, *rn;
4166 const mod_t *mp;
4167 op_t op;
4168 bool cvctx, ctctx, eq, discard;
4169
4170 if (tn == NULL)
4171 return;
4172
4173 ln = tn->tn_left;
4174 rn = tn->tn_right;
4175 mp = &modtab[op = tn->tn_op];
4176
4177 if (!check_expr_op(tn, op, ln,
4178 szof, fcall, vctx, tctx, retval_discarded, eqwarn))
4179 return;
4180
4181 cvctx = mp->m_left_value_context;
4182 ctctx = mp->m_left_test_context;
4183 eq = mp->m_warn_if_operand_eq &&
4184 !ln->tn_parenthesized &&
4185 rn != NULL && !rn->tn_parenthesized;
4186
4187 /*
4188 * values of operands of ':' are not used if the type of at least
4189 * one of the operands (for gcc compatibility) is void
4190 * XXX test/value context of QUEST should probably be used as
4191 * context for both operands of COLON
4192 */
4193 if (op == COLON && tn->tn_type->t_tspec == VOID)
4194 cvctx = ctctx = false;
4195 discard = op == CVT && tn->tn_type->t_tspec == VOID;
4196 check_expr_misc(ln, cvctx, ctctx, eq, op == CALL, discard, szof);
4197
4198 switch (op) {
4199 case PUSH:
4200 if (rn != NULL)
4201 check_expr_misc(rn, false, false, eq, false, false,
4202 szof);
4203 break;
4204 case LOGAND:
4205 case LOGOR:
4206 check_expr_misc(rn, false, true, eq, false, false, szof);
4207 break;
4208 case COLON:
4209 check_expr_misc(rn, cvctx, ctctx, eq, false, false, szof);
4210 break;
4211 case COMMA:
4212 check_expr_misc(rn, vctx, tctx, eq, false, false, szof);
4213 break;
4214 default:
4215 if (mp->m_binary)
4216 check_expr_misc(rn, true, false, eq, false, false,
4217 szof);
4218 break;
4219 }
4220 }
4221
4222 /*
4223 * Checks the range of array indices, if possible.
4224 * amper is set if only the address of the element is used. This
4225 * means that the index is allowed to refer to the first element
4226 * after the array.
4227 */
4228 static void
4229 check_array_index(tnode_t *tn, bool amper)
4230 {
4231 int dim;
4232 tnode_t *ln, *rn;
4233 int elsz;
4234 int64_t con;
4235
4236 ln = tn->tn_left;
4237 rn = tn->tn_right;
4238
4239 /* We can only check constant indices. */
4240 if (rn->tn_op != CON)
4241 return;
4242
4243 /* Return if the left node does not stem from an array. */
4244 if (ln->tn_op != ADDR)
4245 return;
4246 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
4247 return;
4248 if (ln->tn_left->tn_type->t_tspec != ARRAY)
4249 return;
4250
4251 /*
4252 * For incomplete array types, we can print a warning only if
4253 * the index is negative.
4254 */
4255 if (is_incomplete(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
4256 return;
4257
4258 /* Get the size of one array element */
4259 if ((elsz = length_in_bits(ln->tn_type->t_subt, NULL)) == 0)
4260 return;
4261 elsz /= CHAR_SIZE;
4262
4263 /* Change the unit of the index from bytes to element size. */
4264 if (is_uinteger(rn->tn_type->t_tspec)) {
4265 con = (uint64_t)rn->tn_val->v_quad / elsz;
4266 } else {
4267 con = rn->tn_val->v_quad / elsz;
4268 }
4269
4270 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
4271
4272 if (!is_uinteger(rn->tn_type->t_tspec) && con < 0) {
4273 /* array subscript cannot be negative: %ld */
4274 warning(167, (long)con);
4275 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
4276 /* array subscript cannot be > %d: %ld */
4277 warning(168, dim - 1, (long)con);
4278 }
4279 }
4280
4281 static bool
4282 is_out_of_char_range(const tnode_t *tn)
4283 {
4284 return tn->tn_op == CON &&
4285 !(0 <= tn->tn_val->v_quad &&
4286 tn->tn_val->v_quad < 1 << (CHAR_SIZE - 1));
4287 }
4288
4289 /*
4290 * Check for ordered comparisons of unsigned values with 0.
4291 */
4292 static void
4293 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
4294 {
4295 tspec_t lt, rt;
4296
4297 lt = ln->tn_type->t_tspec;
4298 rt = rn->tn_type->t_tspec;
4299
4300 if (ln->tn_op != CON && rn->tn_op != CON)
4301 return;
4302
4303 if (!is_integer(lt) || !is_integer(rt))
4304 return;
4305
4306 if (hflag || pflag) {
4307 if (lt == CHAR && is_out_of_char_range(rn)) {
4308 /* nonportable character comparison '%s %d' */
4309 warning(230, op_name(op), (int)rn->tn_val->v_quad);
4310 return;
4311 }
4312 if (rt == CHAR && is_out_of_char_range(ln)) {
4313 /* nonportable character comparison '%s %d' */
4314 warning(230, op_name(op), (int)ln->tn_val->v_quad);
4315 return;
4316 }
4317 }
4318
4319 if (is_uinteger(lt) && !is_uinteger(rt) &&
4320 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
4321 if (rn->tn_val->v_quad < 0) {
4322 /* comparison of %s with %s, op %s */
4323 warning(162, type_name(ln->tn_type),
4324 "negative constant", op_name(op));
4325 } else if (op == LT || op == GE) {
4326 /* comparison of %s with %s, op %s */
4327 warning(162, type_name(ln->tn_type), "0", op_name(op));
4328 }
4329 return;
4330 }
4331 if (is_uinteger(rt) && !is_uinteger(lt) &&
4332 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
4333 if (ln->tn_val->v_quad < 0) {
4334 /* comparison of %s with %s, op %s */
4335 warning(162, "negative constant",
4336 type_name(rn->tn_type), op_name(op));
4337 } else if (op == GT || op == LE) {
4338 /* comparison of %s with %s, op %s */
4339 warning(162, "0", type_name(rn->tn_type), op_name(op));
4340 }
4341 return;
4342 }
4343 }
4344
4345 /*
4346 * Return whether the expression can be used for static initialization.
4347 *
4348 * Constant initialization expressions must be constant or an address
4349 * of a static object with an optional offset. In the first case,
4350 * the result is returned in *offsp. In the second case, the static
4351 * object is returned in *symp and the offset in *offsp.
4352 *
4353 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4354 * CON. Type conversions are allowed if they do not change binary
4355 * representation (including width).
4356 *
4357 * C99 6.6 "Constant expressions"
4358 * C99 6.7.8p4 restricts initializers for static storage duration
4359 */
4360 bool
4361 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4362 {
4363 const sym_t *sym;
4364 ptrdiff_t offs1, offs2;
4365 tspec_t t, ot;
4366
4367 switch (tn->tn_op) {
4368 case MINUS:
4369 if (tn->tn_right->tn_op == CVT)
4370 return constant_addr(tn->tn_right, symp, offsp);
4371 else if (tn->tn_right->tn_op != CON)
4372 return false;
4373 /* FALLTHROUGH */
4374 case PLUS:
4375 offs1 = offs2 = 0;
4376 if (tn->tn_left->tn_op == CON) {
4377 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
4378 if (!constant_addr(tn->tn_right, &sym, &offs2))
4379 return false;
4380 } else if (tn->tn_right->tn_op == CON) {
4381 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
4382 if (tn->tn_op == MINUS)
4383 offs2 = -offs2;
4384 if (!constant_addr(tn->tn_left, &sym, &offs1))
4385 return false;
4386 } else {
4387 return false;
4388 }
4389 *symp = sym;
4390 *offsp = offs1 + offs2;
4391 return true;
4392 case ADDR:
4393 if (tn->tn_left->tn_op == NAME) {
4394 *symp = tn->tn_left->tn_sym;
4395 *offsp = 0;
4396 return true;
4397 } else {
4398 /*
4399 * If this would be the front end of a compiler we
4400 * would return a label instead of 0, at least if
4401 * 'tn->tn_left->tn_op == STRING'.
4402 */
4403 *symp = NULL;
4404 *offsp = 0;
4405 return true;
4406 }
4407 case CVT:
4408 t = tn->tn_type->t_tspec;
4409 ot = tn->tn_left->tn_type->t_tspec;
4410 if ((!is_integer(t) && t != PTR) ||
4411 (!is_integer(ot) && ot != PTR)) {
4412 return false;
4413 }
4414 #if 0
4415 /*
4416 * consider:
4417 * struct foo {
4418 * unsigned char a;
4419 * } f = {
4420 * (unsigned char)(unsigned long)
4421 * (&(((struct foo *)0)->a))
4422 * };
4423 * since psize(unsigned long) != psize(unsigned char),
4424 * this fails.
4425 */
4426 else if (psize(t) != psize(ot))
4427 return -1;
4428 #endif
4429 return constant_addr(tn->tn_left, symp, offsp);
4430 default:
4431 return false;
4432 }
4433 }
4434
4435 /* Append s2 to s1, then free s2. */
4436 strg_t *
4437 cat_strings(strg_t *s1, strg_t *s2)
4438 {
4439
4440 if (s1->st_char != s2->st_char) {
4441 /* cannot concatenate wide and regular string literals */
4442 error(292);
4443 return s1;
4444 }
4445
4446 size_t len1 = s1->st_len;
4447 size_t len2 = s2->st_len;
4448 size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
4449 size_t size1 = len1 * chsize;
4450 size_t size2 = (len2 + 1) * chsize;
4451 s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
4452 memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
4453 free(s2->st_mem);
4454
4455 s1->st_len = len1 + len2;
4456 free(s2);
4457
4458 return s1;
4459 }
4460
4461 static bool
4462 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
4463 {
4464
4465 if (op == SHL || op == SHR) {
4466 if (!lparen && (lop == PLUS || lop == MINUS))
4467 return true;
4468 if (!rparen && (rop == PLUS || rop == MINUS))
4469 return true;
4470 return false;
4471 }
4472
4473 if (op == LOGOR) {
4474 if (!lparen && lop == LOGAND)
4475 return true;
4476 if (!rparen && rop == LOGAND)
4477 return true;
4478 return false;
4479 }
4480
4481 lint_assert(op == BITAND || op == BITXOR || op == BITOR);
4482 if (!lparen && lop != op) {
4483 if (lop == PLUS || lop == MINUS)
4484 return true;
4485 if (lop == BITAND || lop == BITXOR)
4486 return true;
4487 }
4488 if (!rparen && rop != op) {
4489 if (rop == PLUS || rop == MINUS)
4490 return true;
4491 if (rop == BITAND || rop == BITXOR)
4492 return true;
4493 }
4494 return false;
4495 }
4496
4497 /*
4498 * Print a warning if the given node has operands which should be
4499 * parenthesized.
4500 *
4501 * XXX Does not work if an operand is a constant expression. Constant
4502 * expressions are already folded.
4503 */
4504 static void
4505 check_precedence_confusion(tnode_t *tn)
4506 {
4507 tnode_t *ln, *rn;
4508
4509 if (!hflag)
4510 return;
4511
4512 debug_node(tn);
4513
4514 lint_assert(is_binary(tn));
4515 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4516 continue;
4517 for (rn = tn->tn_right; rn->tn_op == CVT; rn = rn->tn_left)
4518 continue;
4519
4520 if (is_confusing_precedence(tn->tn_op,
4521 ln->tn_op, ln->tn_parenthesized,
4522 rn->tn_op, rn->tn_parenthesized)) {
4523 /* precedence confusion possible: parenthesize! */
4524 warning(169);
4525 }
4526 }
4527
4528 typedef struct stmt_expr {
4529 struct memory_block *se_mem;
4530 sym_t *se_sym;
4531 struct stmt_expr *se_enclosing;
4532 } stmt_expr;
4533
4534 static stmt_expr *stmt_exprs;
4535
4536 void
4537 begin_statement_expr(void)
4538 {
4539 stmt_expr *se = xmalloc(sizeof(*se));
4540 se->se_mem = expr_save_memory();
4541 se->se_sym = NULL;
4542 se->se_enclosing = stmt_exprs;
4543 stmt_exprs = se;
4544 }
4545
4546 void
4547 do_statement_expr(tnode_t *tn)
4548 {
4549 block_level--;
4550 mem_block_level--;
4551 stmt_exprs->se_sym = tn != NULL
4552 ? mktempsym(block_dup_type(tn->tn_type))
4553 : NULL; /* after a syntax error */
4554 mem_block_level++;
4555 block_level++;
4556 /* ({ }) is a GCC extension */
4557 gnuism(320);
4558 }
4559
4560 tnode_t *
4561 end_statement_expr(void)
4562 {
4563 stmt_expr *se = stmt_exprs;
4564 if (se->se_sym == NULL)
4565 return NULL; /* after a syntax error */
4566 tnode_t *tn = build_name(se->se_sym, false);
4567 (void)expr_save_memory(); /* leak */
4568 expr_restore_memory(se->se_mem);
4569 stmt_exprs = se->se_enclosing;
4570 free(se);
4571 return tn;
4572 }
4573