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