tree.c revision 1.95 1 /* $NetBSD: tree.c,v 1.95 2020/12/29 11:35:11 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.95 2020/12/29 11:35:11 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 illptrc(mod_t *, type_t *, type_t *);
64 static void mrgqual(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 *bldamper(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 chkaidx(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 strmemb(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 = bldamper(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 && !ccflg)
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 = bldamper(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 * informations 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 strmemb().
758 * Here we only must check for totaly wrong things.
759 */
760 if (lt == FUNC || lt == VOID || ltp->t_isfield ||
761 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
762 /* Without tflag we got already an error */
763 if (tflag)
764 /* unacceptable operand of %s */
765 error(111, mp->m_name);
766 return 0;
767 }
768 /* Now we have an object we can create a pointer to */
769 break;
770 case ARROW:
771 if (lt != PTR && !(tflag && tspec_is_int(lt))) {
772 /* Without tflag we got already an error */
773 if (tflag)
774 /* unacceptabel operand of %s */
775 error(111, mp->m_name);
776 return 0;
777 }
778 break;
779 case INCAFT:
780 case DECAFT:
781 case INCBEF:
782 case DECBEF:
783 /* operands have scalar types (checked above) */
784 if (!ln->tn_lvalue) {
785 if (ln->tn_op == CVT && ln->tn_cast &&
786 ln->tn_left->tn_op == LOAD) {
787 /* a cast to non-ptr does not yield an lvalue */
788 if (ln->tn_type->t_tspec == PTR)
789 break;
790 error(163);
791 }
792 /* %soperand of %s must be lvalue */
793 error(114, "", mp->m_name);
794 return 0;
795 } else if (ltp->t_const) {
796 /* %soperand of %s must be modifiable lvalue */
797 if (!tflag)
798 warning(115, "", mp->m_name);
799 }
800 break;
801 case AMPER:
802 if (lt == ARRAY || lt == FUNC) {
803 /* ok, a warning comes later (in bldamper()) */
804 } else if (!ln->tn_lvalue) {
805 if (ln->tn_op == CVT && ln->tn_cast &&
806 ln->tn_left->tn_op == LOAD) {
807 /* a cast to non-ptr does not yield an lvalue */
808 if (ln->tn_type->t_tspec == PTR)
809 break;
810 error(163);
811 }
812 /* %soperand of %s must be lvalue */
813 error(114, "", mp->m_name);
814 return 0;
815 } else if (tspec_is_scalar(lt)) {
816 if (ltp->t_isfield) {
817 /* cannot take address of bit-field */
818 error(112);
819 return 0;
820 }
821 } else if (lt != STRUCT && lt != UNION) {
822 /* unacceptable operand of %s */
823 error(111, mp->m_name);
824 return 0;
825 }
826 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
827 /* cannot take address of register %s */
828 error(113, ln->tn_sym->s_name);
829 return 0;
830 }
831 break;
832 case STAR:
833 /* until now there were no type checks for this operator */
834 if (lt != PTR) {
835 /* cannot dereference non-pointer type */
836 error(96);
837 return 0;
838 }
839 break;
840 case PLUS:
841 /* operands have scalar types (checked above) */
842 if ((lt == PTR && !tspec_is_int(rt)) ||
843 (rt == PTR && !tspec_is_int(lt))) {
844 incompat(op, lt, rt);
845 return 0;
846 }
847 break;
848 case MINUS:
849 /* operands have scalar types (checked above) */
850 if (lt == PTR && (!tspec_is_int(rt) && rt != PTR)) {
851 incompat(op, lt, rt);
852 return 0;
853 } else if (rt == PTR && lt != PTR) {
854 incompat(op, lt, rt);
855 return 0;
856 }
857 if (lt == PTR && rt == PTR) {
858 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
859 /* illegal pointer subtraction */
860 error(116);
861 }
862 }
863 break;
864 case SHR:
865 /* operands have integer types (checked above) */
866 if (pflag && !tspec_is_uint(lt)) {
867 /*
868 * The left operand is signed. This means that
869 * the operation is (possibly) nonportable.
870 */
871 /* bitwise operation on signed value nonportable */
872 if (ln->tn_op != CON) {
873 /* possibly nonportable */
874 warning(117);
875 } else if (ln->tn_val->v_quad < 0) {
876 warning(120);
877 }
878 } else if (!tflag && !sflag &&
879 !tspec_is_uint(olt) && tspec_is_uint(ort)) {
880 /*
881 * The left operand would become unsigned in
882 * traditional C.
883 */
884 if (hflag &&
885 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
886 /* semantics of %s change in ANSI C; use ... */
887 warning(118, mp->m_name);
888 }
889 } else if (!tflag && !sflag &&
890 !tspec_is_uint(olt) && !tspec_is_uint(ort) &&
891 psize(lt) < psize(rt)) {
892 /*
893 * In traditional C the left operand would be extended,
894 * possibly with 1, and then shifted.
895 */
896 if (hflag &&
897 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
898 /* semantics of %s change in ANSI C; use ... */
899 warning(118, mp->m_name);
900 }
901 }
902 goto shift;
903 case SHL:
904 /*
905 * ANSI C does not perform balancing for shift operations,
906 * but traditional C does. If the width of the right operand
907 * is greather than the width of the left operand, than in
908 * traditional C the left operand would be extendet to the
909 * width of the right operand. For SHL this may result in
910 * different results.
911 */
912 if (psize(lt) < psize(rt)) {
913 /*
914 * XXX If both operands are constant make sure
915 * that there is really a differencs between
916 * ANSI C and traditional C.
917 */
918 if (hflag)
919 /* semantics of %s change in ANSI C; use ... */
920 warning(118, mp->m_name);
921 }
922 shift:
923 if (rn->tn_op == CON) {
924 if (!tspec_is_uint(rt) && rn->tn_val->v_quad < 0) {
925 /* negative shift */
926 warning(121);
927 } else if ((uint64_t)rn->tn_val->v_quad == (uint64_t)size(lt)) {
928 /* shift equal to size fo object */
929 warning(267);
930 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) {
931 /* shift greater than size of object */
932 warning(122);
933 }
934 }
935 break;
936 case EQ:
937 case NE:
938 /*
939 * Accept some things which are allowed with EQ and NE,
940 * but not with ordered comparisons.
941 */
942 if (lt == PTR && ((rt == PTR && rst == VOID) ||
943 tspec_is_int(rt))) {
944 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
945 break;
946 }
947 if (rt == PTR && ((lt == PTR && lst == VOID) ||
948 tspec_is_int(lt))) {
949 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
950 break;
951 }
952 /* FALLTHROUGH */
953 case LT:
954 case GT:
955 case LE:
956 case GE:
957 if ((lt == PTR || rt == PTR) && lt != rt) {
958 if (tspec_is_int(lt) || tspec_is_int(rt)) {
959 const char *lx = lt == PTR ?
960 "pointer" : "integer";
961 const char *rx = rt == PTR ?
962 "pointer" : "integer";
963 tyname(lbuf, sizeof(lbuf), ltp);
964 tyname(rbuf, sizeof(rbuf), rtp);
965 /* illegal comb. of pointer and int., op %s */
966 warning(123, lx, lbuf, rx, rbuf, mp->m_name);
967 } else {
968 incompat(op, lt, rt);
969 return 0;
970 }
971 } else if (lt == PTR && rt == PTR) {
972 ptrcmpok(op, ln, rn);
973 }
974 break;
975 case QUEST:
976 if (!tspec_is_scalar(lt)) {
977 /* first operand must have scalar type, op ? : */
978 error(170);
979 return 0;
980 }
981 while (rn->tn_op == CVT)
982 rn = rn->tn_left;
983 if (rn->tn_op != COLON)
984 LERROR("typeok()");
985 break;
986 case COLON:
987 if (tspec_is_arith(lt) && tspec_is_arith(rt))
988 break;
989
990 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
991 break;
992 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
993 break;
994
995 /* combination of any pointer and 0, 0L or (void *)0 is ok */
996 if (lt == PTR && ((rt == PTR && rst == VOID) ||
997 tspec_is_int(rt))) {
998 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
999 break;
1000 }
1001 if (rt == PTR && ((lt == PTR && lst == VOID) ||
1002 tspec_is_int(lt))) {
1003 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1004 break;
1005 }
1006
1007 if ((lt == PTR && tspec_is_int(rt)) ||
1008 (tspec_is_int(lt) && rt == PTR)) {
1009 const char *lx = lt == PTR ? "pointer" : "integer";
1010 const char *rx = rt == PTR ? "pointer" : "integer";
1011 tyname(lbuf, sizeof(lbuf), ltp);
1012 tyname(rbuf, sizeof(rbuf), rtp);
1013 /* illegal comb. of ptr. and int., op %s */
1014 warning(123, lx, lbuf, rx, rbuf, mp->m_name);
1015 break;
1016 }
1017
1018 if (lt == VOID || rt == VOID) {
1019 if (lt != VOID || rt != VOID)
1020 /* incompatible types in conditional */
1021 warning(126);
1022 break;
1023 }
1024
1025 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1026 (lst == FUNC && rst == VOID))) {
1027 /* (void *)0 handled above */
1028 if (sflag)
1029 /* ANSI C forbids conv. of %s to %s, op %s */
1030 warning(305, "function pointer", "'void *'",
1031 mp->m_name);
1032 break;
1033 }
1034
1035 if (rt == PTR && lt == PTR) {
1036 if (eqptrtype(lstp, rstp, 1))
1037 break;
1038 if (!eqtype(lstp, rstp, 1, 0, NULL))
1039 illptrc(mp, ltp, rtp);
1040 break;
1041 }
1042
1043 /* incompatible types in conditional */
1044 error(126);
1045 return 0;
1046
1047 case ASSIGN:
1048 case INIT:
1049 case FARG:
1050 case RETURN:
1051 if (!asgntypok(op, arg, ln, rn))
1052 return 0;
1053 goto assign;
1054 case MULASS:
1055 case DIVASS:
1056 case MODASS:
1057 goto assign;
1058 case ADDASS:
1059 case SUBASS:
1060 /* operands have scalar types (checked above) */
1061 if ((lt == PTR && !tspec_is_int(rt)) || rt == PTR) {
1062 incompat(op, lt, rt);
1063 return 0;
1064 }
1065 goto assign;
1066 case SHLASS:
1067 goto assign;
1068 case SHRASS:
1069 if (pflag && !tspec_is_uint(lt) &&
1070 !(tflag && tspec_is_uint(rt))) {
1071 /* bitwise operation on s.v. possibly nonportable */
1072 warning(117);
1073 }
1074 goto assign;
1075 case ANDASS:
1076 case XORASS:
1077 case ORASS:
1078 goto assign;
1079 assign:
1080 if (!ln->tn_lvalue) {
1081 if (ln->tn_op == CVT && ln->tn_cast &&
1082 ln->tn_left->tn_op == LOAD) {
1083 /* a cast to non-ptr does not yield an lvalue */
1084 if (ln->tn_type->t_tspec == PTR)
1085 break;
1086 error(163);
1087 }
1088 /* %soperand of %s must be lvalue */
1089 error(114, "left ", mp->m_name);
1090 return 0;
1091 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1092 conmemb(ltp))) {
1093 /* %soperand of %s must be modifiable lvalue */
1094 if (!tflag)
1095 warning(115, "left ", mp->m_name);
1096 }
1097 break;
1098 case COMMA:
1099 if (!modtab[ln->tn_op].m_sideeff)
1100 nulleff(ln);
1101 break;
1102 /* LINTED206: (enumeration values not handled in switch) */
1103 case CON:
1104 case CASE:
1105 case PUSH:
1106 case LOAD:
1107 case ICALL:
1108 case CVT:
1109 case CALL:
1110 case FSEL:
1111 case STRING:
1112 case NAME:
1113 case LOGOR:
1114 case LOGAND:
1115 case OR:
1116 case XOR:
1117 case AND:
1118 case MOD:
1119 case DIV:
1120 case MULT:
1121 case UMINUS:
1122 case UPLUS:
1123 case DEC:
1124 case INC:
1125 case COMPL:
1126 case NOT:
1127 case NOOP:
1128 case REAL:
1129 case IMAG:
1130 break;
1131 }
1132
1133 if (mp->m_bad_on_enum &&
1134 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1135 chkbeop(op, ln, rn);
1136 } else if (mp->m_valid_on_enum &&
1137 (ltp->t_isenum && rtp && rtp->t_isenum)) {
1138 chkeop2(op, arg, ln, rn);
1139 } else if (mp->m_valid_on_enum &&
1140 (ltp->t_isenum || (rtp && rtp->t_isenum))) {
1141 chkeop1(op, arg, ln, rn);
1142 }
1143
1144 return 1;
1145 }
1146
1147 static void
1148 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn)
1149 {
1150 type_t *ltp, *rtp;
1151 tspec_t lt, rt;
1152 const char *lts, *rts;
1153
1154 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1155 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1156
1157 if (lt == VOID || rt == VOID) {
1158 if (sflag && (lt == FUNC || rt == FUNC)) {
1159 /* (void *)0 already handled in typeok() */
1160 *(lt == FUNC ? <s : &rts) = "function pointer";
1161 *(lt == VOID ? <s : &rts) = "'void *'";
1162 /* ANSI C forbids comparison of %s with %s */
1163 warning(274, lts, rts);
1164 }
1165 return;
1166 }
1167
1168 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1169 illptrc(&modtab[op], ltp, rtp);
1170 return;
1171 }
1172
1173 if (lt == FUNC && rt == FUNC) {
1174 if (sflag && op != EQ && op != NE)
1175 /* ANSI C forbids ordered comp. of func ptr */
1176 warning(125);
1177 }
1178 }
1179
1180 /*
1181 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1182 * and prints warnings/errors if necessary.
1183 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1184 */
1185 static int
1186 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1187 {
1188 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1189 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1190 mod_t *mp;
1191 const char *lts, *rts;
1192 char lbuf[128], rbuf[128];
1193
1194 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1195 lst = (lstp = ltp->t_subt)->t_tspec;
1196 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1197 rst = (rstp = rtp->t_subt)->t_tspec;
1198 mp = &modtab[op];
1199
1200 if (tspec_is_arith(lt) && tspec_is_arith(rt))
1201 return 1;
1202
1203 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1204 /* both are struct or union */
1205 return ltp->t_str == rtp->t_str;
1206
1207 /* 0, 0L and (void *)0 may be assigned to any pointer */
1208 if (lt == PTR && ((rt == PTR && rst == VOID) || tspec_is_int(rt))) {
1209 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1210 return 1;
1211 }
1212
1213 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1214 /* two pointers, at least one pointer to void */
1215 if (sflag && (lst == FUNC || rst == FUNC)) {
1216 /* comb. of ptr to func and ptr to void */
1217 *(lst == FUNC ? <s : &rts) = "function pointer";
1218 *(lst == VOID ? <s : &rts) = "'void *'";
1219 switch (op) {
1220 case INIT:
1221 case RETURN:
1222 /* ANSI C forbids conversion of %s to %s */
1223 warning(303, rts, lts);
1224 break;
1225 case FARG:
1226 /* ANSI C forbids conv. of %s to %s, arg #%d */
1227 warning(304, rts, lts, arg);
1228 break;
1229 default:
1230 /* ANSI C forbids conv. of %s to %s, op %s */
1231 warning(305, rts, lts, mp->m_name);
1232 break;
1233 }
1234 }
1235 }
1236
1237 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1238 eqtype(lstp, rstp, 1, 0, NULL))) {
1239 /* compatible pointer types (qualifiers ignored) */
1240 if (!tflag &&
1241 ((!lstp->t_const && rstp->t_const) ||
1242 (!lstp->t_volatile && rstp->t_volatile))) {
1243 /* left side has not all qualifiers of right */
1244 tyname(lbuf, sizeof(lbuf), lstp);
1245 tyname(rbuf, sizeof(rbuf), rstp);
1246 switch (op) {
1247 case INIT:
1248 case RETURN:
1249 /* incompatible pointer types */
1250 warning(182, lbuf, rbuf);
1251 break;
1252 case FARG:
1253 /* argument has incompat. ptr. type, arg #%d */
1254 warning(153, arg, lbuf, rbuf);
1255 break;
1256 default:
1257 /* operands have incompat. ptr. types, op %s */
1258 warning(128, mp->m_name, lbuf, rbuf);
1259 break;
1260 }
1261 }
1262 return 1;
1263 }
1264
1265 if ((lt == PTR && tspec_is_int(rt)) ||
1266 (tspec_is_int(lt) && rt == PTR)) {
1267 const char *lx = lt == PTR ? "pointer" : "integer";
1268 const char *rx = rt == PTR ? "pointer" : "integer";
1269 tyname(lbuf, sizeof(lbuf), ltp);
1270 tyname(rbuf, sizeof(rbuf), rtp);
1271
1272 switch (op) {
1273 case INIT:
1274 case RETURN:
1275 /* illegal combination of pointer and integer */
1276 warning(183, lx, lbuf, rx, rbuf);
1277 break;
1278 case FARG:
1279 /* illegal comb. of ptr. and int., arg #%d */
1280 warning(154, lx, lbuf, rx, rbuf, arg);
1281 break;
1282 default:
1283 /* illegal comb. of ptr. and int., op %s */
1284 warning(123, lx, lbuf, rx, rbuf, mp->m_name);
1285 break;
1286 }
1287 return 1;
1288 }
1289
1290 if (lt == PTR && rt == PTR) {
1291 switch (op) {
1292 case INIT:
1293 case RETURN:
1294 illptrc(NULL, ltp, rtp);
1295 break;
1296 case FARG:
1297 /* argument has incompatible pointer type, arg #%d */
1298 warning(153, arg, tyname(lbuf, sizeof(lbuf), ltp),
1299 tyname(rbuf, sizeof(rbuf), rtp));
1300 break;
1301 default:
1302 illptrc(mp, ltp, rtp);
1303 break;
1304 }
1305 return 1;
1306 }
1307
1308 switch (op) {
1309 case INIT:
1310 /* initialisation type mismatch */
1311 error(185, tyname(lbuf, sizeof(lbuf), ltp),
1312 tyname(rbuf, sizeof(rbuf), rtp));
1313 break;
1314 case RETURN:
1315 /* return value type mismatch */
1316 error(211, tyname(lbuf, sizeof(lbuf), ltp),
1317 tyname(rbuf, sizeof(rbuf), rtp));
1318 break;
1319 case FARG:
1320 /* argument is incompatible with prototype, arg #%d */
1321 warning(155, arg);
1322 break;
1323 default:
1324 incompat(op, lt, rt);
1325 break;
1326 }
1327
1328 return 0;
1329 }
1330
1331 /*
1332 * Prints a warning if an operator, which should be senseless for an
1333 * enum type, is applied to an enum type.
1334 */
1335 static void
1336 chkbeop(op_t op, tnode_t *ln, tnode_t *rn)
1337 {
1338 mod_t *mp;
1339
1340 if (!eflag)
1341 return;
1342
1343 mp = &modtab[op];
1344
1345 if (!(ln->tn_type->t_isenum ||
1346 (mp->m_binary && rn->tn_type->t_isenum))) {
1347 return;
1348 }
1349
1350 /*
1351 * Enum as offset to a pointer is an exception (otherwise enums
1352 * could not be used as array indizes).
1353 */
1354 if (op == PLUS &&
1355 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1356 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1357 return;
1358 }
1359
1360 /* dubious operation on enum, op %s */
1361 warning(241, mp->m_name);
1362
1363 }
1364
1365 /*
1366 * Prints a warning if an operator is applied to two different enum types.
1367 */
1368 static void
1369 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1370 {
1371 mod_t *mp;
1372
1373 mp = &modtab[op];
1374
1375 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1376 switch (op) {
1377 case INIT:
1378 /* enum type mismatch in initialisation */
1379 warning(210);
1380 break;
1381 case FARG:
1382 /* enum type mismatch, arg #%d */
1383 warning(156, arg);
1384 break;
1385 case RETURN:
1386 /* return value type mismatch */
1387 warning(211);
1388 break;
1389 default:
1390 /* enum type mismatch, op %s */
1391 warning(130, mp->m_name);
1392 break;
1393 }
1394 } else if (Pflag && mp->m_comp && op != EQ && op != NE) {
1395 if (eflag)
1396 /* dubious comparisons of enums */
1397 warning(243, mp->m_name);
1398 }
1399 }
1400
1401 /*
1402 * Prints a warning if an operator has both enum end other integer
1403 * types.
1404 */
1405 static void
1406 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1407 {
1408 char lbuf[64], rbuf[64];
1409
1410 if (!eflag)
1411 return;
1412
1413 switch (op) {
1414 case INIT:
1415 /*
1416 * Initializations with 0 should be allowed. Otherwise,
1417 * we should complain about all uninitialized enums,
1418 * consequently.
1419 */
1420 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1421 tspec_is_int(rn->tn_type->t_tspec) &&
1422 rn->tn_val->v_quad == 0) {
1423 return;
1424 }
1425 /* initialisation of '%s' with '%s' */
1426 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1427 tyname(rbuf, sizeof(rbuf), rn->tn_type));
1428 break;
1429 case FARG:
1430 /* combination of '%s' and '%s', arg #%d */
1431 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1432 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg);
1433 break;
1434 case RETURN:
1435 /* combination of '%s' and '%s' in return */
1436 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1437 tyname(rbuf, sizeof(rbuf), rn->tn_type));
1438 break;
1439 default:
1440 /* combination of '%s' and %s, op %s */
1441 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1442 tyname(rbuf, sizeof(rbuf), rn->tn_type),
1443 modtab[op].m_name);
1444 break;
1445 }
1446 }
1447
1448 /*
1449 * Build and initialize a new node.
1450 */
1451 static tnode_t *
1452 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1453 {
1454 tnode_t *ntn;
1455 tspec_t t;
1456 #ifdef notyet
1457 size_t l;
1458 uint64_t rnum;
1459 #endif
1460
1461 ntn = getnode();
1462
1463 ntn->tn_op = op;
1464 ntn->tn_type = type;
1465 ntn->tn_left = ln;
1466 ntn->tn_right = rn;
1467
1468 switch (op) {
1469 #ifdef notyet
1470 case SHR:
1471 if (rn->tn_op != CON)
1472 break;
1473 rnum = rn->tn_val->v_quad;
1474 l = tsize(ln->tn_type) / CHAR_BIT;
1475 t = ln->tn_type->t_tspec;
1476 switch (l) {
1477 case 8:
1478 if (rnum >= 56)
1479 t = UCHAR;
1480 else if (rnum >= 48)
1481 t = USHORT;
1482 else if (rnum >= 32)
1483 t = UINT;
1484 break;
1485 case 4:
1486 if (rnum >= 24)
1487 t = UCHAR;
1488 else if (rnum >= 16)
1489 t = USHORT;
1490 break;
1491 case 2:
1492 if (rnum >= 8)
1493 t = UCHAR;
1494 break;
1495 default:
1496 break;
1497 }
1498 if (t != ln->tn_type->t_tspec)
1499 ntn->tn_type->t_tspec = t;
1500 break;
1501 #endif
1502 case STAR:
1503 case FSEL:
1504 if (ln->tn_type->t_tspec == PTR) {
1505 t = ln->tn_type->t_subt->t_tspec;
1506 if (t != FUNC && t != VOID)
1507 ntn->tn_lvalue = 1;
1508 } else {
1509 LERROR("mktnode()");
1510 }
1511 break;
1512 default:
1513 break;
1514 }
1515
1516 return ntn;
1517 }
1518
1519 /*
1520 * Performs usual conversion of operands to (unsigned) int.
1521 *
1522 * If tflag is set or the operand is a function argument with no
1523 * type information (no prototype or variable # of args), convert
1524 * float to double.
1525 */
1526 tnode_t *
1527 promote(op_t op, int farg, tnode_t *tn)
1528 {
1529 tspec_t t;
1530 type_t *ntp;
1531 u_int len;
1532
1533 t = tn->tn_type->t_tspec;
1534
1535 if (!tspec_is_arith(t))
1536 return tn;
1537
1538 if (!tflag) {
1539 /*
1540 * ANSI C requires that the result is always of type INT
1541 * if INT can represent all possible values of the previous
1542 * type.
1543 */
1544 if (tn->tn_type->t_isfield) {
1545 len = tn->tn_type->t_flen;
1546 if (size(INT) > len) {
1547 t = INT;
1548 } else {
1549 if (size(INT) != len)
1550 LERROR("promote()");
1551 if (tspec_is_uint(t)) {
1552 t = UINT;
1553 } else {
1554 t = INT;
1555 }
1556 }
1557 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1558 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1559 INT : UINT;
1560 } else if (t == SHORT || t == USHORT) {
1561 t = (size(SHORT) < size(INT) || t == SHORT) ?
1562 INT : UINT;
1563 } else if (t == ENUM) {
1564 t = INT;
1565 } else if (farg && t == FLOAT) {
1566 t = DOUBLE;
1567 }
1568 } else {
1569 /*
1570 * In traditional C, keep unsigned and promote FLOAT
1571 * to DOUBLE.
1572 */
1573 if (t == UCHAR || t == USHORT) {
1574 t = UINT;
1575 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1576 t = INT;
1577 } else if (t == FLOAT) {
1578 t = DOUBLE;
1579 } else if (t == ENUM) {
1580 t = INT;
1581 }
1582 }
1583
1584 if (t != tn->tn_type->t_tspec) {
1585 ntp = tduptyp(tn->tn_type);
1586 ntp->t_tspec = t;
1587 /*
1588 * Keep t_isenum so we are later able to check compatibility
1589 * of enum types.
1590 */
1591 tn = convert(op, 0, ntp, tn);
1592 }
1593
1594 return tn;
1595 }
1596
1597 /*
1598 * Insert conversions which are necessary to give both operands the same
1599 * type. This is done in different ways for traditional C and ANIS C.
1600 */
1601 static void
1602 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1603 {
1604 tspec_t lt, rt, t;
1605 int i, u;
1606 type_t *ntp;
1607 static tspec_t tl[] = {
1608 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1609 };
1610
1611 lt = (*lnp)->tn_type->t_tspec;
1612 rt = (*rnp)->tn_type->t_tspec;
1613
1614 if (!tspec_is_arith(lt) || !tspec_is_arith(rt))
1615 return;
1616
1617 if (!tflag) {
1618 if (lt == rt) {
1619 t = lt;
1620 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
1621 t = LCOMPLEX;
1622 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
1623 t = DCOMPLEX;
1624 } else if (lt == COMPLEX || rt == COMPLEX) {
1625 t = COMPLEX;
1626 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
1627 t = FCOMPLEX;
1628 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1629 t = LDOUBLE;
1630 } else if (lt == DOUBLE || rt == DOUBLE) {
1631 t = DOUBLE;
1632 } else if (lt == FLOAT || rt == FLOAT) {
1633 t = FLOAT;
1634 } else {
1635 /*
1636 * If type A has more bits than type B it should
1637 * be able to hold all possible values of type B.
1638 */
1639 if (size(lt) > size(rt)) {
1640 t = lt;
1641 } else if (size(lt) < size(rt)) {
1642 t = rt;
1643 } else {
1644 for (i = 3; tl[i] != INT; i++) {
1645 if (tl[i] == lt || tl[i] == rt)
1646 break;
1647 }
1648 if ((tspec_is_uint(lt) || tspec_is_uint(rt)) &&
1649 !tspec_is_uint(tl[i])) {
1650 i--;
1651 }
1652 t = tl[i];
1653 }
1654 }
1655 } else {
1656 /* Keep unsigned in traditional C */
1657 u = tspec_is_uint(lt) || tspec_is_uint(rt);
1658 for (i = 0; tl[i] != INT; i++) {
1659 if (lt == tl[i] || rt == tl[i])
1660 break;
1661 }
1662 t = tl[i];
1663 if (u && tspec_is_int(t) && !tspec_is_uint(t))
1664 t = utyp(t);
1665 }
1666
1667 if (t != lt) {
1668 ntp = tduptyp((*lnp)->tn_type);
1669 ntp->t_tspec = t;
1670 *lnp = convert(op, 0, ntp, *lnp);
1671 }
1672 if (t != rt) {
1673 ntp = tduptyp((*rnp)->tn_type);
1674 ntp->t_tspec = t;
1675 *rnp = convert(op, 0, ntp, *rnp);
1676 }
1677 }
1678
1679 /*
1680 * Insert a conversion operator, which converts the type of the node
1681 * to another given type.
1682 * If op is FARG, arg is the number of the argument (used for warnings).
1683 */
1684 tnode_t *
1685 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1686 {
1687 tnode_t *ntn;
1688 tspec_t nt, ot, ost = NOTSPEC;
1689
1690 nt = tp->t_tspec;
1691 if ((ot = tn->tn_type->t_tspec) == PTR)
1692 ost = tn->tn_type->t_subt->t_tspec;
1693
1694 if (!tflag && !sflag && op == FARG)
1695 ptconv(arg, nt, ot, tp, tn);
1696 if (tspec_is_int(nt) && tspec_is_int(ot)) {
1697 iiconv(op, arg, nt, ot, tp, tn);
1698 } else if (nt == PTR && ((ot == PTR && ost == VOID) ||
1699 tspec_is_int(ot)) && tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1700 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1701 } else if (tspec_is_int(nt) && ot == PTR) {
1702 piconv(op, nt, tp, tn);
1703 } else if (nt == PTR && ot == PTR) {
1704 ppconv(op, tn, tp);
1705 }
1706
1707 ntn = getnode();
1708 ntn->tn_op = CVT;
1709 ntn->tn_type = tp;
1710 ntn->tn_cast = op == CVT;
1711 ntn->tn_right = NULL;
1712 if (tn->tn_op != CON || nt == VOID) {
1713 ntn->tn_left = tn;
1714 } else {
1715 ntn->tn_op = CON;
1716 ntn->tn_val = tgetblk(sizeof (val_t));
1717 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1718 }
1719
1720 return ntn;
1721 }
1722
1723 /*
1724 * Print a warning if a prototype causes a type conversion that is
1725 * different from what would happen to the same argument in the
1726 * absence of a prototype.
1727 *
1728 * Errors/Warnings about illegal type combinations are already printed
1729 * in asgntypok().
1730 */
1731 static void
1732 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1733 {
1734 tnode_t *ptn;
1735 char buf[64];
1736
1737 if (!tspec_is_arith(nt) || !tspec_is_arith(ot))
1738 return;
1739
1740 /*
1741 * If the type of the formal parameter is char/short, a warning
1742 * would be useless, because functions declared the old style
1743 * can't expect char/short arguments.
1744 */
1745 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1746 return;
1747
1748 /* get default promotion */
1749 ptn = promote(NOOP, 1, tn);
1750 ot = ptn->tn_type->t_tspec;
1751
1752 /* return if types are the same with and without prototype */
1753 if (nt == ot || (nt == ENUM && ot == INT))
1754 return;
1755
1756 if (tspec_is_float(nt) != tspec_is_float(ot) ||
1757 psize(nt) != psize(ot)) {
1758 /* representation and/or width change */
1759 if (!tspec_is_int(ot) || psize(ot) > psize(INT)) {
1760 /* conversion to '%s' due to prototype, arg #%d */
1761 warning(259, tyname(buf, sizeof(buf), tp), arg);
1762 }
1763 } else if (hflag) {
1764 /*
1765 * they differ in sign or base type (char, short, int,
1766 * long, long long, float, double, long double)
1767 *
1768 * if they differ only in sign and the argument is a constant
1769 * and the msb of the argument is not set, print no warning
1770 */
1771 if (ptn->tn_op == CON && tspec_is_int(nt) &&
1772 styp(nt) == styp(ot) &&
1773 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1774 /* ok */
1775 } else {
1776 /* conversion to '%s' due to prototype, arg #%d */
1777 warning(259, tyname(buf, sizeof(buf), tp), arg);
1778 }
1779 }
1780 }
1781
1782 /*
1783 * Print warnings for conversions of integer types which my cause
1784 * 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 prtnode(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, basictyname(lt), basictyname(rt));
2227 return;
2228 default:
2229 error(e, basictyname(lt), basictyname(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 illptrc(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("illptrc()");
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 mrgqual(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("mrgqual()");
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 = bldamper(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 bldamper(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 mrgqual(&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 mrgqual(&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 mrgqual(&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 && !ccflg)
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 bldszof(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 bldoffsetof(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("bldszof()");
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 bldalof(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 chkreach();
3495
3496 chkmisc(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 && !ccflg)
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, scltoa(tn->tn_sym->s_scl));
3583 } else if (tn->tn_op == CON && tspec_is_float(tn->tn_type->t_tspec)) {
3584 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3585 } else if (tn->tn_op == CON && tspec_is_int(tn->tn_type->t_tspec)) {
3586 uq = tn->tn_val->v_quad;
3587 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3588 (long)uq & 0xffffffffl);
3589 } else if (tn->tn_op == CON) {
3590 if (tn->tn_type->t_tspec != PTR)
3591 LERROR("displexpr()");
3592 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3593 (u_long)tn->tn_val->v_quad);
3594 } else if (tn->tn_op == STRING) {
3595 if (tn->tn_strg->st_tspec == CHAR) {
3596 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3597 } else {
3598 char *s;
3599 size_t n;
3600 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3601 s = xmalloc(n);
3602 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3603 (void)printf("L\"%s\"", s);
3604 free(s);
3605 }
3606 (void)printf(" ");
3607 } else if (tn->tn_op == FSEL) {
3608 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3609 tn->tn_type->t_flen);
3610 }
3611 (void)printf("%s\n", ttos(tn->tn_type));
3612 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3613 return;
3614 displexpr(tn->tn_left, offs + 2);
3615 if (modtab[tn->tn_op].m_binary ||
3616 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3617 displexpr(tn->tn_right, offs + 2);
3618 }
3619 }
3620
3621 /*
3622 * Called by expr() to recursively perform some tests.
3623 */
3624 /* ARGSUSED */
3625 void
3626 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3627 int szof)
3628 {
3629 tnode_t *ln, *rn;
3630 mod_t *mp;
3631 int nrvdisc, cvctx, ctctx;
3632 op_t op;
3633 scl_t sc;
3634 dinfo_t *di;
3635
3636 if (tn == NULL)
3637 return;
3638
3639 ln = tn->tn_left;
3640 rn = tn->tn_right;
3641 mp = &modtab[op = tn->tn_op];
3642
3643 switch (op) {
3644 case AMPER:
3645 if (ln->tn_op == NAME && (reached || rchflg)) {
3646 if (!szof)
3647 mark_as_set(ln->tn_sym);
3648 mark_as_used(ln->tn_sym, fcall, szof);
3649 }
3650 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3651 /* check the range of array indices */
3652 chkaidx(ln->tn_left, 1);
3653 break;
3654 case LOAD:
3655 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3656 /* check the range of array indices */
3657 chkaidx(ln->tn_left, 0);
3658 /* FALLTHROUGH */
3659 case PUSH:
3660 case INCBEF:
3661 case DECBEF:
3662 case INCAFT:
3663 case DECAFT:
3664 case ADDASS:
3665 case SUBASS:
3666 case MULASS:
3667 case DIVASS:
3668 case MODASS:
3669 case ANDASS:
3670 case ORASS:
3671 case XORASS:
3672 case SHLASS:
3673 case SHRASS:
3674 case REAL:
3675 case IMAG:
3676 if (ln->tn_op == NAME && (reached || rchflg)) {
3677 sc = ln->tn_sym->s_scl;
3678 /*
3679 * Look if there was a asm statement in one of the
3680 * compound statements we are in. If not, we don't
3681 * print a warning.
3682 */
3683 for (di = dcs; di != NULL; di = di->d_nxt) {
3684 if (di->d_asm)
3685 break;
3686 }
3687 if (sc != EXTERN && sc != STATIC &&
3688 !ln->tn_sym->s_set && !szof && di == NULL) {
3689 /* %s may be used before set */
3690 warning(158, ln->tn_sym->s_name);
3691 mark_as_set(ln->tn_sym);
3692 }
3693 mark_as_used(ln->tn_sym, 0, 0);
3694 }
3695 break;
3696 case ASSIGN:
3697 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3698 mark_as_set(ln->tn_sym);
3699 if (ln->tn_sym->s_scl == EXTERN)
3700 outusg(ln->tn_sym);
3701 }
3702 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3703 /* check the range of array indices */
3704 chkaidx(ln->tn_left, 0);
3705 break;
3706 case CALL:
3707 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3708 LERROR("chkmisc(op=%s != %s || %s != %s)",
3709 getopname(ln->tn_op), getopname(AMPER),
3710 getopname(ln->tn_left->tn_op), getopname(NAME));
3711 if (!szof)
3712 outcall(tn, vctx || tctx, rvdisc);
3713 break;
3714 case EQ:
3715 /* equality operator "==" found where "=" was exp. */
3716 if (hflag && eqwarn)
3717 warning(160);
3718 break;
3719 case CON:
3720 case NAME:
3721 case STRING:
3722 return;
3723 /* LINTED206: (enumeration values not handled in switch) */
3724 case OR:
3725 case XOR:
3726 case NE:
3727 case GE:
3728 case GT:
3729 case LE:
3730 case LT:
3731 case SHR:
3732 case SHL:
3733 case MINUS:
3734 case PLUS:
3735 case MOD:
3736 case DIV:
3737 case MULT:
3738 case STAR:
3739 case UMINUS:
3740 case UPLUS:
3741 case DEC:
3742 case INC:
3743 case COMPL:
3744 case NOT:
3745 case POINT:
3746 case ARROW:
3747 case NOOP:
3748 case AND:
3749 case FARG:
3750 case CASE:
3751 case INIT:
3752 case RETURN:
3753 case ICALL:
3754 case CVT:
3755 case COMMA:
3756 case FSEL:
3757 case COLON:
3758 case QUEST:
3759 case LOGOR:
3760 case LOGAND:
3761 break;
3762 }
3763
3764 cvctx = mp->m_vctx;
3765 ctctx = mp->m_tctx;
3766 /*
3767 * values of operands of ':' are not used if the type of at least
3768 * one of the operands (for gcc compatibility) is void
3769 * XXX test/value context of QUEST should probably be used as
3770 * context for both operands of COLON
3771 */
3772 if (op == COLON && tn->tn_type->t_tspec == VOID)
3773 cvctx = ctctx = 0;
3774 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3775 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3776
3777 switch (op) {
3778 case PUSH:
3779 if (rn != NULL)
3780 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3781 break;
3782 case LOGAND:
3783 case LOGOR:
3784 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3785 break;
3786 case COLON:
3787 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3788 break;
3789 case COMMA:
3790 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3791 break;
3792 default:
3793 if (mp->m_binary)
3794 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3795 break;
3796 }
3797
3798 }
3799
3800 /*
3801 * Checks the range of array indices, if possible.
3802 * amper is set if only the address of the element is used. This
3803 * means that the index is allowd to refere to the first element
3804 * after the array.
3805 */
3806 static void
3807 chkaidx(tnode_t *tn, int amper)
3808 {
3809 int dim;
3810 tnode_t *ln, *rn;
3811 int elsz;
3812 int64_t con;
3813
3814 ln = tn->tn_left;
3815 rn = tn->tn_right;
3816
3817 /* We can only check constant indices. */
3818 if (rn->tn_op != CON)
3819 return;
3820
3821 /* Return if the left node does not stem from an array. */
3822 if (ln->tn_op != AMPER)
3823 return;
3824 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3825 return;
3826 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3827 return;
3828
3829 /*
3830 * For incomplete array types, we can print a warning only if
3831 * the index is negative.
3832 */
3833 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3834 return;
3835
3836 /* Get the size of one array element */
3837 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3838 return;
3839 elsz /= CHAR_BIT;
3840
3841 /* Change the unit of the index from bytes to element size. */
3842 if (tspec_is_uint(rn->tn_type->t_tspec)) {
3843 con = (uint64_t)rn->tn_val->v_quad / elsz;
3844 } else {
3845 con = rn->tn_val->v_quad / elsz;
3846 }
3847
3848 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3849
3850 if (!tspec_is_uint(rn->tn_type->t_tspec) && con < 0) {
3851 /* array subscript cannot be negative: %ld */
3852 warning(167, (long)con);
3853 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
3854 /* array subscript cannot be > %d: %ld */
3855 warning(168, dim - 1, (long)con);
3856 }
3857 }
3858
3859 /*
3860 * Check for ordered comparisons of unsigned values with 0.
3861 */
3862 static void
3863 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3864 {
3865 char buf[64];
3866 tspec_t lt, rt;
3867 mod_t *mp;
3868
3869 lt = ln->tn_type->t_tspec;
3870 rt = rn->tn_type->t_tspec;
3871 mp = &modtab[op];
3872
3873 if (ln->tn_op != CON && rn->tn_op != CON)
3874 return;
3875
3876 if (!tspec_is_int(lt) || !tspec_is_int(rt))
3877 return;
3878
3879 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3880 (rn->tn_val->v_quad < 0 ||
3881 rn->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) {
3882 /* nonportable character comparison, op %s */
3883 warning(230, mp->m_name);
3884 return;
3885 }
3886 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3887 (ln->tn_val->v_quad < 0 ||
3888 ln->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) {
3889 /* nonportable character comparison, op %s */
3890 warning(230, mp->m_name);
3891 return;
3892 }
3893 if (tspec_is_uint(lt) && !tspec_is_uint(rt) &&
3894 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3895 if (rn->tn_val->v_quad < 0) {
3896 /* comparison of %s with %s, op %s */
3897 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3898 "negative constant", mp->m_name);
3899 } else if (op == LT || op == GE || (hflag && op == LE)) {
3900 /* comparison of %s with %s, op %s */
3901 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3902 "0", mp->m_name);
3903 }
3904 return;
3905 }
3906 if (tspec_is_uint(rt) && !tspec_is_uint(lt) &&
3907 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3908 if (ln->tn_val->v_quad < 0) {
3909 /* comparison of %s with %s, op %s */
3910 warning(162, "negative constant",
3911 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name);
3912 } else if (op == GT || op == LE || (hflag && op == GE)) {
3913 /* comparison of %s with %s, op %s */
3914 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type),
3915 mp->m_name);
3916 }
3917 return;
3918 }
3919 }
3920
3921 /*
3922 * Takes an expression an returns 0 if this expression can be used
3923 * for static initialisation, otherwise -1.
3924 *
3925 * Constant initialisation expressions must be constant or an address
3926 * of a static object with an optional offset. In the first case,
3927 * the result is returned in *offsp. In the second case, the static
3928 * object is returned in *symp and the offset in *offsp.
3929 *
3930 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3931 * CON. Type conversions are allowed if they do not change binary
3932 * representation (including width).
3933 */
3934 int
3935 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3936 {
3937 sym_t *sym;
3938 ptrdiff_t offs1, offs2;
3939 tspec_t t, ot;
3940
3941 switch (tn->tn_op) {
3942 case MINUS:
3943 if (tn->tn_right->tn_op == CVT)
3944 return conaddr(tn->tn_right, symp, offsp);
3945 else if (tn->tn_right->tn_op != CON)
3946 return -1;
3947 /* FALLTHROUGH */
3948 case PLUS:
3949 offs1 = offs2 = 0;
3950 if (tn->tn_left->tn_op == CON) {
3951 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3952 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3953 return -1;
3954 } else if (tn->tn_right->tn_op == CON) {
3955 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3956 if (tn->tn_op == MINUS)
3957 offs2 = -offs2;
3958 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3959 return -1;
3960 } else {
3961 return -1;
3962 }
3963 *symp = sym;
3964 *offsp = offs1 + offs2;
3965 break;
3966 case AMPER:
3967 if (tn->tn_left->tn_op == NAME) {
3968 *symp = tn->tn_left->tn_sym;
3969 *offsp = 0;
3970 } else if (tn->tn_left->tn_op == STRING) {
3971 /*
3972 * If this would be the front end of a compiler we
3973 * would return a label instead of 0.
3974 */
3975 *offsp = 0;
3976 }
3977 break;
3978 case CVT:
3979 t = tn->tn_type->t_tspec;
3980 ot = tn->tn_left->tn_type->t_tspec;
3981 if ((!tspec_is_int(t) && t != PTR) ||
3982 (!tspec_is_int(ot) && ot != PTR)) {
3983 return -1;
3984 }
3985 #ifdef notdef
3986 /*
3987 * consider:
3988 * struct foo {
3989 * unsigned char a;
3990 * } f = {
3991 * (u_char)(u_long)(&(((struct foo *)0)->a))
3992 * };
3993 * since psize(u_long) != psize(u_char) this fails.
3994 */
3995 else if (psize(t) != psize(ot))
3996 return -1;
3997 #endif
3998 if (conaddr(tn->tn_left, symp, offsp) == -1)
3999 return -1;
4000 break;
4001 default:
4002 return -1;
4003 }
4004 return 0;
4005 }
4006
4007 /*
4008 * Concatenate two string constants.
4009 */
4010 strg_t *
4011 catstrg(strg_t *strg1, strg_t *strg2)
4012 {
4013 size_t len1, len2, len;
4014
4015 if (strg1->st_tspec != strg2->st_tspec) {
4016 /* cannot concatenate wide and regular string literals */
4017 error(292);
4018 return strg1;
4019 }
4020
4021 len1 = strg1->st_len;
4022 len2 = strg2->st_len + 1; /* + NUL */
4023 len = len1 + len2;
4024
4025 #define COPY(F) \
4026 do { \
4027 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \
4028 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \
4029 free(strg2->F); \
4030 } while (/*CONSTCOND*/0)
4031
4032 if (strg1->st_tspec == CHAR)
4033 COPY(st_cp);
4034 else
4035 COPY(st_wcp);
4036
4037 strg1->st_len = len - 1; /* - NUL */;
4038 free(strg2);
4039
4040 return strg1;
4041 }
4042
4043 /*
4044 * Print a warning if the given node has operands which should be
4045 * parenthesized.
4046 *
4047 * XXX Does not work if an operand is a constant expression. Constant
4048 * expressions are already folded.
4049 */
4050 static void
4051 precconf(tnode_t *tn)
4052 {
4053 tnode_t *ln, *rn;
4054 op_t lop, rop = NOOP;
4055 int lparn, rparn = 0;
4056 mod_t *mp;
4057 int dowarn;
4058
4059 if (!hflag)
4060 return;
4061
4062 mp = &modtab[tn->tn_op];
4063
4064 lparn = 0;
4065 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4066 lparn |= ln->tn_parn;
4067 lparn |= ln->tn_parn;
4068 lop = ln->tn_op;
4069
4070 if (mp->m_binary) {
4071 rparn = 0;
4072 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
4073 rparn |= rn->tn_parn;
4074 rparn |= rn->tn_parn;
4075 rop = rn->tn_op;
4076 }
4077
4078 dowarn = 0;
4079
4080 switch (tn->tn_op) {
4081 case SHL:
4082 case SHR:
4083 if (!lparn && (lop == PLUS || lop == MINUS)) {
4084 dowarn = 1;
4085 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
4086 dowarn = 1;
4087 }
4088 break;
4089 case LOGOR:
4090 if (!lparn && lop == LOGAND) {
4091 dowarn = 1;
4092 } else if (!rparn && rop == LOGAND) {
4093 dowarn = 1;
4094 }
4095 break;
4096 case AND:
4097 case XOR:
4098 case OR:
4099 if (!lparn && lop != tn->tn_op) {
4100 if (lop == PLUS || lop == MINUS) {
4101 dowarn = 1;
4102 } else if (lop == AND || lop == XOR) {
4103 dowarn = 1;
4104 }
4105 }
4106 if (!dowarn && !rparn && rop != tn->tn_op) {
4107 if (rop == PLUS || rop == MINUS) {
4108 dowarn = 1;
4109 } else if (rop == AND || rop == XOR) {
4110 dowarn = 1;
4111 }
4112 }
4113 break;
4114 /* LINTED206: (enumeration values not handled in switch) */
4115 case DECAFT:
4116 case XORASS:
4117 case SHLASS:
4118 case NOOP:
4119 case ARROW:
4120 case ORASS:
4121 case POINT:
4122 case NAME:
4123 case NOT:
4124 case COMPL:
4125 case CON:
4126 case INC:
4127 case STRING:
4128 case DEC:
4129 case INCBEF:
4130 case DECBEF:
4131 case INCAFT:
4132 case FSEL:
4133 case CALL:
4134 case COMMA:
4135 case CVT:
4136 case ICALL:
4137 case LOAD:
4138 case PUSH:
4139 case RETURN:
4140 case INIT:
4141 case CASE:
4142 case FARG:
4143 case SUBASS:
4144 case ADDASS:
4145 case MODASS:
4146 case DIVASS:
4147 case MULASS:
4148 case ASSIGN:
4149 case COLON:
4150 case QUEST:
4151 case LOGAND:
4152 case NE:
4153 case EQ:
4154 case GE:
4155 case GT:
4156 case LE:
4157 case LT:
4158 case MINUS:
4159 case PLUS:
4160 case MOD:
4161 case DIV:
4162 case MULT:
4163 case AMPER:
4164 case STAR:
4165 case UMINUS:
4166 case SHRASS:
4167 case UPLUS:
4168 case ANDASS:
4169 case REAL:
4170 case IMAG:
4171 break;
4172 }
4173
4174 if (dowarn) {
4175 /* precedence confusion possible: parenthesize! */
4176 warning(169);
4177 }
4178
4179 }
4180