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