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