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