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