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