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