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