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