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