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