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