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