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