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