tree.c revision 1.40 1 /* $NetBSD: tree.c,v 1.40 2005/01/02 17:59:47 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.40 2005/01/02 17:59:47 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->t_isenum)) {
1188 chkeop2(op, arg, ln, rn);
1189 } else if (mp->m_enumop && (ltp->t_isenum || 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 (styp(nt) != SHORT || !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 } else if (hflag) {
1752 /*
1753 * they differ in sign or base type (char, short, int,
1754 * long, long long, float, double, long double)
1755 *
1756 * if they differ only in sign and the argument is a constant
1757 * and the msb of the argument is not set, print no warning
1758 */
1759 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1760 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1761 /* ok */
1762 } else {
1763 /* conversion to '%s' due to prototype, arg #%d */
1764 warning(259, tyname(buf, sizeof(buf), tp), arg);
1765 }
1766 }
1767 }
1768
1769 /*
1770 * Print warnings for conversions of integer types which my cause
1771 * problems.
1772 */
1773 /* ARGSUSED */
1774 static void
1775 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1776 {
1777 char lbuf[64], rbuf[64];
1778 if (tn->tn_op == CON)
1779 return;
1780
1781 if (op == CVT)
1782 return;
1783
1784 #if 0
1785 if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1786 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1787 if (aflag && pflag) {
1788 if (op == FARG) {
1789 warning(297, tyname(lbuf, sizeof(lbuf), tp),
1790 arg);
1791 } else {
1792 warning(131, tyname(lbuf, sizeof(lbuf), tp));
1793 }
1794 }
1795 }
1796 #endif
1797
1798 if (psize(nt) < psize(ot) &&
1799 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1800 aflag > 1)) {
1801 /* conversion from '%s' may lose accuracy */
1802 if (aflag) {
1803 if (op == FARG) {
1804 warning(298,
1805 tyname(rbuf, sizeof(rbuf), tn->tn_type),
1806 tyname(lbuf, sizeof(lbuf), tp),
1807 arg);
1808 } else {
1809 warning(132,
1810 tyname(rbuf, sizeof(rbuf), tn->tn_type),
1811 tyname(lbuf, sizeof(lbuf), tp));
1812 }
1813 }
1814 }
1815 }
1816
1817 /*
1818 * Print warnings for dubious conversions of pointer to integer.
1819 */
1820 static void
1821 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
1822 {
1823 char buf[64];
1824
1825 if (tn->tn_op == CON)
1826 return;
1827
1828 if (op != CVT) {
1829 /* We got already an error. */
1830 return;
1831 }
1832
1833 if (psize(nt) < psize(PTR)) {
1834 if (pflag && size(nt) >= size(PTR)) {
1835 /* conv. of pointer to %s may lose bits */
1836 warning(134, tyname(buf, sizeof(buf), tp));
1837 } else {
1838 /* conv. of pointer to %s loses bits */
1839 warning(133, tyname(buf, sizeof(buf), tp));
1840 }
1841 }
1842 }
1843
1844 /*
1845 * Print warnings for questionable pointer conversions.
1846 */
1847 static void
1848 ppconv(op_t op, tnode_t *tn, type_t *tp)
1849 {
1850 tspec_t nt, ot;
1851 const char *nts, *ots;
1852
1853 /*
1854 * We got already an error (pointers of different types
1855 * without a cast) or we will not get a warning.
1856 */
1857 if (op != CVT)
1858 return;
1859
1860 nt = tp->t_subt->t_tspec;
1861 ot = tn->tn_type->t_subt->t_tspec;
1862
1863 if (nt == VOID || ot == VOID) {
1864 if (sflag && (nt == FUNC || ot == FUNC)) {
1865 /* (void *)0 already handled in convert() */
1866 *(nt == FUNC ? &nts : &ots) = "function pointer";
1867 *(nt == VOID ? &nts : &ots) = "'void *'";
1868 /* ANSI C forbids conversion of %s to %s */
1869 warning(303, ots, nts);
1870 }
1871 return;
1872 } else if (nt == FUNC && ot == FUNC) {
1873 return;
1874 } else if (nt == FUNC || ot == FUNC) {
1875 /* questionable conversion of function pointer */
1876 warning(229);
1877 return;
1878 }
1879
1880 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1881 if (hflag)
1882 /* possible pointer alignment problem */
1883 warning(135);
1884 }
1885 if (((nt == STRUCT || nt == UNION) &&
1886 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1887 psize(nt) != psize(ot)) {
1888 if (cflag) {
1889 /* pointer casts may be troublesome */
1890 warning(247);
1891 }
1892 }
1893 }
1894
1895 /*
1896 * Converts a typed constant in a constant of another type.
1897 *
1898 * op operator which requires conversion
1899 * arg if op is FARG, # of argument
1900 * tp type in which to convert the constant
1901 * nv new constant
1902 * v old constant
1903 */
1904 void
1905 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
1906 {
1907 char lbuf[64], rbuf[64];
1908 tspec_t ot, nt;
1909 ldbl_t max = 0.0, min = 0.0;
1910 int sz, rchk;
1911 int64_t xmask, xmsk1;
1912 int osz, nsz;
1913
1914 ot = v->v_tspec;
1915 nt = nv->v_tspec = tp->t_tspec;
1916 rchk = 0;
1917
1918 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1919 switch (nt) {
1920 case BOOL:
1921 max = 1; min = 0; break;
1922 case CHAR:
1923 max = CHAR_MAX; min = CHAR_MIN; break;
1924 case UCHAR:
1925 max = UCHAR_MAX; min = 0; break;
1926 case SCHAR:
1927 max = SCHAR_MAX; min = SCHAR_MIN; break;
1928 case SHORT:
1929 max = SHRT_MAX; min = SHRT_MIN; break;
1930 case USHORT:
1931 max = USHRT_MAX; min = 0; break;
1932 case ENUM:
1933 case INT:
1934 max = INT_MAX; min = INT_MIN; break;
1935 case UINT:
1936 max = (u_int)UINT_MAX; min = 0; break;
1937 case LONG:
1938 max = LONG_MAX; min = LONG_MIN; break;
1939 case ULONG:
1940 max = (u_long)ULONG_MAX; min = 0; break;
1941 case QUAD:
1942 max = QUAD_MAX; min = QUAD_MIN; break;
1943 case UQUAD:
1944 max = (uint64_t)UQUAD_MAX; min = 0; break;
1945 case FLOAT:
1946 max = FLT_MAX; min = -FLT_MAX; break;
1947 case DOUBLE:
1948 max = DBL_MAX; min = -DBL_MAX; break;
1949 case PTR:
1950 /* Got already an error because of float --> ptr */
1951 case LDOUBLE:
1952 max = LDBL_MAX; min = -LDBL_MAX; break;
1953 default:
1954 LERROR("cvtcon()");
1955 }
1956 if (v->v_ldbl > max || v->v_ldbl < min) {
1957 if (nt == LDOUBLE)
1958 LERROR("cvtcon()");
1959 if (op == FARG) {
1960 /* conv. of %s to %s is out of rng., arg #%d */
1961 warning(295, tyname(lbuf, sizeof(lbuf),
1962 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
1963 arg);
1964 } else {
1965 /* conversion of %s to %s is out of range */
1966 warning(119, tyname(lbuf, sizeof(lbuf),
1967 gettyp(ot)),
1968 tyname(rbuf, sizeof(rbuf), tp));
1969 }
1970 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1971 }
1972 if (nt == FLOAT) {
1973 nv->v_ldbl = (float)v->v_ldbl;
1974 } else if (nt == DOUBLE) {
1975 nv->v_ldbl = (double)v->v_ldbl;
1976 } else if (nt == LDOUBLE) {
1977 nv->v_ldbl = v->v_ldbl;
1978 } else {
1979 nv->v_quad = (nt == PTR || isutyp(nt)) ?
1980 (uint64_t)v->v_ldbl : (int64_t)v->v_ldbl;
1981 }
1982 } else {
1983 if (nt == FLOAT) {
1984 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1985 (float)(uint64_t)v->v_quad : (float)v->v_quad;
1986 } else if (nt == DOUBLE) {
1987 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1988 (double)(uint64_t)v->v_quad : (double)v->v_quad;
1989 } else if (nt == LDOUBLE) {
1990 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
1991 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
1992 } else {
1993 rchk = 1; /* Check for lost precision. */
1994 nv->v_quad = v->v_quad;
1995 }
1996 }
1997
1998 if (v->v_ansiu && isftyp(nt)) {
1999 /* ANSI C treats constant as unsigned */
2000 warning(157);
2001 v->v_ansiu = 0;
2002 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
2003 psize(nt) > psize(ot))) {
2004 /* ANSI C treats constant as unsigned */
2005 warning(157);
2006 v->v_ansiu = 0;
2007 }
2008
2009 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
2010 sz = tp->t_isfield ? tp->t_flen : size(nt);
2011 nv->v_quad = xsign(nv->v_quad, nt, sz);
2012 }
2013
2014 if (rchk && op != CVT) {
2015 osz = size(ot);
2016 nsz = tp->t_isfield ? tp->t_flen : size(nt);
2017 xmask = qlmasks[nsz] ^ qlmasks[osz];
2018 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2019 /*
2020 * For bitwise operations we are not interested in the
2021 * value, but in the bits itself.
2022 */
2023 if (op == ORASS || op == OR || op == XOR) {
2024 /*
2025 * Print a warning if bits which were set are
2026 * lost due to the conversion.
2027 * This can happen with operator ORASS only.
2028 */
2029 if (nsz < osz && (v->v_quad & xmask) != 0) {
2030 /* constant truncated by conv., op %s */
2031 warning(306, modtab[op].m_name);
2032 }
2033 } else if (op == ANDASS || op == AND) {
2034 /*
2035 * Print a warning if additional bits are not all 1
2036 * and the most significant bit of the old value is 1,
2037 * or if at least one (but not all) removed bit was 0.
2038 */
2039 if (nsz > osz &&
2040 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2041 (nv->v_quad & xmask) != xmask) {
2042 /*
2043 * extra bits set to 0 in conversion
2044 * of '%s' to '%s', op %s
2045 */
2046 warning(309, tyname(lbuf, sizeof(lbuf),
2047 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2048 modtab[op].m_name);
2049 } else if (nsz < osz &&
2050 (v->v_quad & xmask) != xmask &&
2051 (v->v_quad & xmask) != 0) {
2052 /* const. truncated by conv., op %s */
2053 warning(306, modtab[op].m_name);
2054 }
2055 } else if ((nt != PTR && isutyp(nt)) &&
2056 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2057 if (op == ASSIGN) {
2058 /* assignment of negative constant to ... */
2059 warning(164);
2060 } else if (op == INIT) {
2061 /* initialisation of unsigned with neg. ... */
2062 warning(221);
2063 } else if (op == FARG) {
2064 /* conversion of neg. const. to ..., arg #%d */
2065 warning(296, arg);
2066 } else if (modtab[op].m_comp) {
2067 /* we get this warning already in chkcomp() */
2068 } else {
2069 /* conversion of negative constant to ... */
2070 warning(222);
2071 }
2072 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2073 (v->v_quad & xmask) != 0 &&
2074 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2075 /*
2076 * Loss of significant bit(s). All truncated bits
2077 * of unsigned types or all truncated bits plus the
2078 * msb of the target for signed types are considered
2079 * to be significant bits. Loss of significant bits
2080 * means that at least on of the bits was set in an
2081 * unsigned type or that at least one, but not all of
2082 * the bits was set in an signed type.
2083 * Loss of significant bits means that it is not
2084 * possible, also not with necessary casts, to convert
2085 * back to the original type. A example for a
2086 * necessary cast is:
2087 * char c; int i; c = 128;
2088 * i = c; ** yields -128 **
2089 * i = (unsigned char)c; ** yields 128 **
2090 */
2091 if (op == ASSIGN && tp->t_isfield) {
2092 /* precision lost in bit-field assignment */
2093 warning(166);
2094 } else if (op == ASSIGN) {
2095 /* constant truncated by assignment */
2096 warning(165);
2097 } else if (op == INIT && tp->t_isfield) {
2098 /* bit-field initializer does not fit */
2099 warning(180);
2100 } else if (op == INIT) {
2101 /* initializer does not fit */
2102 warning(178);
2103 } else if (op == CASE) {
2104 /* case label affected by conversion */
2105 warning(196);
2106 } else if (op == FARG) {
2107 /* conv. of %s to %s is out of rng., arg #%d */
2108 warning(295, tyname(lbuf, sizeof(lbuf),
2109 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2110 arg);
2111 } else {
2112 /* conversion of %s to %s is out of range */
2113 warning(119, tyname(lbuf, sizeof(lbuf),
2114 gettyp(ot)),
2115 tyname(rbuf, sizeof(rbuf), tp));
2116 }
2117 } else if (nv->v_quad != v->v_quad) {
2118 if (op == ASSIGN && tp->t_isfield) {
2119 /* precision lost in bit-field assignment */
2120 warning(166);
2121 } else if (op == INIT && tp->t_isfield) {
2122 /* bit-field initializer out of range */
2123 warning(11);
2124 } else if (op == CASE) {
2125 /* case label affected by conversion */
2126 warning(196);
2127 } else if (op == FARG) {
2128 /* conv. of %s to %s is out of rng., arg #%d */
2129 warning(295, tyname(lbuf, sizeof(lbuf),
2130 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2131 arg);
2132 } else {
2133 /* conversion of %s to %s is out of range */
2134 warning(119, tyname(lbuf, sizeof(lbuf),
2135 gettyp(ot)),
2136 tyname(rbuf, sizeof(rbuf), tp));
2137 }
2138 }
2139 }
2140 }
2141
2142 /*
2143 * Called if incompatible types were detected.
2144 * Prints a appropriate warning.
2145 */
2146 static void
2147 incompat(op_t op, tspec_t lt, tspec_t rt)
2148 {
2149 mod_t *mp;
2150
2151 mp = &modtab[op];
2152
2153 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2154 /* void type illegal in expression */
2155 error(109);
2156 } else if (op == ASSIGN) {
2157 if ((lt == STRUCT || lt == UNION) &&
2158 (rt == STRUCT || rt == UNION)) {
2159 /* assignment of different structures */
2160 error(240);
2161 } else {
2162 /* assignment type mismatch */
2163 error(171);
2164 }
2165 } else if (mp->m_binary) {
2166 /* operands of %s have incompatible types */
2167 error(107, mp->m_name);
2168 } else {
2169 /* operand of %s has incompatible type */
2170 error(108, mp->m_name);
2171 }
2172 }
2173
2174 /*
2175 * Called if incompatible pointer types are detected.
2176 * Print an appropriate warning.
2177 */
2178 static void
2179 illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
2180 {
2181 tspec_t lt, rt;
2182
2183 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2184 LERROR("illptrc()");
2185
2186 lt = ltp->t_subt->t_tspec;
2187 rt = rtp->t_subt->t_tspec;
2188
2189 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2190 if (mp == NULL) {
2191 /* illegal structure pointer combination */
2192 warning(244);
2193 } else {
2194 /* illegal structure pointer combination, op %s */
2195 warning(245, mp->m_name);
2196 }
2197 } else {
2198 if (mp == NULL) {
2199 /* illegal pointer combination */
2200 warning(184);
2201 } else {
2202 /* illegal pointer combination, op %s */
2203 warning(124, mp->m_name);
2204 }
2205 }
2206 }
2207
2208 /*
2209 * Make sure type (*tpp)->t_subt has at least the qualifiers
2210 * of tp1->t_subt and tp2->t_subt.
2211 */
2212 static void
2213 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
2214 {
2215
2216 if ((*tpp)->t_tspec != PTR ||
2217 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2218 LERROR("mrgqual()");
2219 }
2220
2221 if ((*tpp)->t_subt->t_const ==
2222 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2223 (*tpp)->t_subt->t_volatile ==
2224 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2225 return;
2226 }
2227
2228 *tpp = tduptyp(*tpp);
2229 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2230 (*tpp)->t_subt->t_const =
2231 tp1->t_subt->t_const | tp2->t_subt->t_const;
2232 (*tpp)->t_subt->t_volatile =
2233 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2234 }
2235
2236 /*
2237 * Returns 1 if the given structure or union has a constant member
2238 * (maybe recursively).
2239 */
2240 static int
2241 conmemb(type_t *tp)
2242 {
2243 sym_t *m;
2244 tspec_t t;
2245
2246 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2247 LERROR("conmemb()");
2248 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2249 tp = m->s_type;
2250 if (tp->t_const)
2251 return (1);
2252 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2253 if (conmemb(m->s_type))
2254 return (1);
2255 }
2256 }
2257 return (0);
2258 }
2259
2260 const char *
2261 basictyname(tspec_t t)
2262 {
2263 switch (t) {
2264 case BOOL: return "_Bool";
2265 case CHAR: return "char";
2266 case UCHAR: return "unsigned char";
2267 case SCHAR: return "signed char";
2268 case SHORT: return "short";
2269 case USHORT: return "unsigned short";
2270 case INT: return "int";
2271 case UINT: return "unsigned int";
2272 case LONG: return "long";
2273 case ULONG: return "unsigned long";
2274 case QUAD: return "long long";
2275 case UQUAD: return "unsigned long long";
2276 case FLOAT: return "float";
2277 case DOUBLE: return "double";
2278 case LDOUBLE: return "long double";
2279 case PTR: return "pointer";
2280 case ENUM: return "enum";
2281 case STRUCT: return "struct";
2282 case UNION: return "union";
2283 case FUNC: return "function";
2284 case ARRAY: return "array";
2285 default:
2286 LERROR("basictyname()");
2287 return NULL;
2288 }
2289 }
2290
2291 const char *
2292 tyname(char *buf, size_t bufsiz, type_t *tp)
2293 {
2294 tspec_t t;
2295 const char *s;
2296 char lbuf[64];
2297
2298 if ((t = tp->t_tspec) == INT && tp->t_isenum)
2299 t = ENUM;
2300
2301 s = basictyname(t);
2302
2303
2304 switch (t) {
2305 case BOOL:
2306 case CHAR:
2307 case UCHAR:
2308 case SCHAR:
2309 case SHORT:
2310 case USHORT:
2311 case INT:
2312 case UINT:
2313 case LONG:
2314 case ULONG:
2315 case QUAD:
2316 case UQUAD:
2317 case FLOAT:
2318 case DOUBLE:
2319 case LDOUBLE:
2320 case FUNC:
2321 (void)snprintf(buf, bufsiz, "%s", s);
2322 break;
2323 case PTR:
2324 (void)snprintf(buf, bufsiz, "%s to %s", s,
2325 tyname(lbuf, sizeof(lbuf), tp->t_subt));
2326 break;
2327 case ENUM:
2328 (void)snprintf(buf, bufsiz, "%s %s", s,
2329 tp->t_enum->etag->s_name);
2330 break;
2331 case STRUCT:
2332 case UNION:
2333 (void)snprintf(buf, bufsiz, "%s %s", s,
2334 tp->t_str->stag->s_name);
2335 break;
2336 case ARRAY:
2337 (void)snprintf(buf, bufsiz, "%s of %s[%d]", s,
2338 tyname(lbuf, sizeof(lbuf), tp->t_subt), tp->t_dim);
2339 break;
2340 default:
2341 LERROR("tyname()");
2342 }
2343 return (buf);
2344 }
2345
2346 /*
2347 * Create a new node for one of the operators POINT and ARROW.
2348 */
2349 static tnode_t *
2350 bldstr(op_t op, tnode_t *ln, tnode_t *rn)
2351 {
2352 tnode_t *ntn, *ctn;
2353 int nolval;
2354
2355 if (rn->tn_op != NAME)
2356 LERROR("bldstr()");
2357 if (rn->tn_sym->s_value.v_tspec != INT)
2358 LERROR("bldstr()");
2359 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2360 LERROR("bldstr()");
2361
2362 /*
2363 * Remember if the left operand is an lvalue (structure members
2364 * are lvalues if and only if the structure itself is an lvalue).
2365 */
2366 nolval = op == POINT && !ln->tn_lvalue;
2367
2368 if (op == POINT) {
2369 ln = bldamper(ln, 1);
2370 } else if (ln->tn_type->t_tspec != PTR) {
2371 if (!tflag || !isityp(ln->tn_type->t_tspec))
2372 LERROR("bldstr()");
2373 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2374 }
2375
2376 #if PTRDIFF_IS_LONG
2377 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2378 #else
2379 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2380 #endif
2381
2382 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2383 if (ln->tn_op == CON)
2384 ntn = fold(ntn);
2385
2386 if (rn->tn_type->t_isfield) {
2387 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2388 } else {
2389 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2390 }
2391
2392 if (nolval)
2393 ntn->tn_lvalue = 0;
2394
2395 return (ntn);
2396 }
2397
2398 /*
2399 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2400 */
2401 static tnode_t *
2402 bldincdec(op_t op, tnode_t *ln)
2403 {
2404 tnode_t *cn, *ntn;
2405
2406 if (ln == NULL)
2407 LERROR("bldincdec()");
2408
2409 if (ln->tn_type->t_tspec == PTR) {
2410 cn = plength(ln->tn_type);
2411 } else {
2412 cn = getinode(INT, (int64_t)1);
2413 }
2414 ntn = mktnode(op, ln->tn_type, ln, cn);
2415
2416 return (ntn);
2417 }
2418
2419 /*
2420 * Create a tree node for the & operator
2421 */
2422 static tnode_t *
2423 bldamper(tnode_t *tn, int noign)
2424 {
2425 tnode_t *ntn;
2426 tspec_t t;
2427
2428 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2429 /* & before array or function: ignored */
2430 if (tflag)
2431 warning(127);
2432 return (tn);
2433 }
2434
2435 /* eliminate &* */
2436 if (tn->tn_op == STAR &&
2437 tn->tn_left->tn_type->t_tspec == PTR &&
2438 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2439 return (tn->tn_left);
2440 }
2441
2442 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2443
2444 return (ntn);
2445 }
2446
2447 /*
2448 * Create a node for operators PLUS and MINUS.
2449 */
2450 static tnode_t *
2451 bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
2452 {
2453 tnode_t *ntn, *ctn;
2454 type_t *tp;
2455
2456 /* If pointer and integer, then pointer to the lhs. */
2457 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2458 ntn = ln;
2459 ln = rn;
2460 rn = ntn;
2461 }
2462
2463 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2464
2465 if (!isityp(rn->tn_type->t_tspec))
2466 LERROR("bldplmi()");
2467
2468 ctn = plength(ln->tn_type);
2469 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2470 rn = convert(NOOP, 0, ctn->tn_type, rn);
2471 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2472 if (rn->tn_left->tn_op == CON)
2473 rn = fold(rn);
2474 ntn = mktnode(op, ln->tn_type, ln, rn);
2475
2476 } else if (rn->tn_type->t_tspec == PTR) {
2477
2478 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2479 LERROR("bldplmi()");
2480 #if PTRDIFF_IS_LONG
2481 tp = gettyp(LONG);
2482 #else
2483 tp = gettyp(INT);
2484 #endif
2485 ntn = mktnode(op, tp, ln, rn);
2486 if (ln->tn_op == CON && rn->tn_op == CON)
2487 ntn = fold(ntn);
2488 ctn = plength(ln->tn_type);
2489 balance(NOOP, &ntn, &ctn);
2490 ntn = mktnode(DIV, tp, ntn, ctn);
2491
2492 } else {
2493
2494 ntn = mktnode(op, ln->tn_type, ln, rn);
2495
2496 }
2497 return (ntn);
2498 }
2499
2500 /*
2501 * Create a node for operators SHL and SHR.
2502 */
2503 static tnode_t *
2504 bldshft(op_t op, tnode_t *ln, tnode_t *rn)
2505 {
2506 tspec_t t;
2507 tnode_t *ntn;
2508
2509 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2510 rn = convert(CVT, 0, gettyp(INT), rn);
2511 ntn = mktnode(op, ln->tn_type, ln, rn);
2512 return (ntn);
2513 }
2514
2515 /*
2516 * Create a node for COLON.
2517 */
2518 static tnode_t *
2519 bldcol(tnode_t *ln, tnode_t *rn)
2520 {
2521 tspec_t lt, rt, pdt;
2522 type_t *rtp;
2523 tnode_t *ntn;
2524
2525 lt = ln->tn_type->t_tspec;
2526 rt = rn->tn_type->t_tspec;
2527 #if PTRDIFF_IS_LONG
2528 pdt = LONG;
2529 #else
2530 pdt = INT;
2531 #endif
2532
2533 /*
2534 * Arithmetic types are balanced, all other type combinations
2535 * still need to be handled.
2536 */
2537 if (isatyp(lt) && isatyp(rt)) {
2538 rtp = ln->tn_type;
2539 } else if (lt == VOID || rt == VOID) {
2540 rtp = gettyp(VOID);
2541 } else if (lt == STRUCT || lt == UNION) {
2542 /* Both types must be identical. */
2543 if (rt != STRUCT && rt != UNION)
2544 LERROR("bldcol()");
2545 if (ln->tn_type->t_str != rn->tn_type->t_str)
2546 LERROR("bldcol()");
2547 if (incompl(ln->tn_type)) {
2548 /* unknown operand size, op %s */
2549 error(138, modtab[COLON].m_name);
2550 return (NULL);
2551 }
2552 rtp = ln->tn_type;
2553 } else if (lt == PTR && isityp(rt)) {
2554 if (rt != pdt) {
2555 rn = convert(NOOP, 0, gettyp(pdt), rn);
2556 rt = pdt;
2557 }
2558 rtp = ln->tn_type;
2559 } else if (rt == PTR && isityp(lt)) {
2560 if (lt != pdt) {
2561 ln = convert(NOOP, 0, gettyp(pdt), ln);
2562 lt = pdt;
2563 }
2564 rtp = rn->tn_type;
2565 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2566 if (rt != PTR)
2567 LERROR("bldcol()");
2568 rtp = ln->tn_type;
2569 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2570 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2571 if (lt != PTR)
2572 LERROR("bldcol()");
2573 rtp = rn->tn_type;
2574 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2575 } else {
2576 if (lt != PTR || rt != PTR)
2577 LERROR("bldcol()");
2578 /*
2579 * XXX For now we simply take the left type. This is
2580 * probably wrong, if one type contains a functionprototype
2581 * and the other one, at the same place, only an old style
2582 * declaration.
2583 */
2584 rtp = ln->tn_type;
2585 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2586 }
2587
2588 ntn = mktnode(COLON, rtp, ln, rn);
2589
2590 return (ntn);
2591 }
2592
2593 /*
2594 * Create a node for an assignment operator (both = and op= ).
2595 */
2596 static tnode_t *
2597 bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
2598 {
2599 tspec_t lt, rt;
2600 tnode_t *ntn, *ctn;
2601
2602 if (ln == NULL || rn == NULL)
2603 LERROR("bldasgn()");
2604
2605 lt = ln->tn_type->t_tspec;
2606 rt = rn->tn_type->t_tspec;
2607
2608 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2609 if (!isityp(rt))
2610 LERROR("bldasgn()");
2611 ctn = plength(ln->tn_type);
2612 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2613 rn = convert(NOOP, 0, ctn->tn_type, rn);
2614 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2615 if (rn->tn_left->tn_op == CON)
2616 rn = fold(rn);
2617 }
2618
2619 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2620 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2621 LERROR("bldasgn()");
2622 if (incompl(ln->tn_type)) {
2623 if (op == RETURN) {
2624 /* cannot return incomplete type */
2625 error(212);
2626 } else {
2627 /* unknown operand size, op %s */
2628 error(138, modtab[op].m_name);
2629 }
2630 return (NULL);
2631 }
2632 }
2633
2634 if (op == SHLASS) {
2635 if (psize(lt) < psize(rt)) {
2636 if (hflag)
2637 /* semantics of %s change in ANSI C; use ... */
2638 warning(118, "<<=");
2639 }
2640 } else if (op != SHRASS) {
2641 if (op == ASSIGN || lt != PTR) {
2642 if (lt != rt ||
2643 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2644 rn = convert(op, 0, ln->tn_type, rn);
2645 rt = lt;
2646 }
2647 }
2648 }
2649
2650 ntn = mktnode(op, ln->tn_type, ln, rn);
2651
2652 return (ntn);
2653 }
2654
2655 /*
2656 * Get length of type tp->t_subt.
2657 */
2658 static tnode_t *
2659 plength(type_t *tp)
2660 {
2661 int elem, elsz;
2662 tspec_t st;
2663
2664 if (tp->t_tspec != PTR)
2665 LERROR("plength()");
2666 tp = tp->t_subt;
2667
2668 elem = 1;
2669 elsz = 0;
2670
2671 while (tp->t_tspec == ARRAY) {
2672 elem *= tp->t_dim;
2673 tp = tp->t_subt;
2674 }
2675
2676 switch (tp->t_tspec) {
2677 case FUNC:
2678 /* pointer to function is not allowed here */
2679 error(110);
2680 break;
2681 case VOID:
2682 /* cannot do pointer arithmetic on operand of ... */
2683 (void)gnuism(136);
2684 break;
2685 case STRUCT:
2686 case UNION:
2687 if ((elsz = tp->t_str->size) == 0)
2688 /* cannot do pointer arithmetic on operand of ... */
2689 error(136);
2690 break;
2691 case ENUM:
2692 if (incompl(tp)) {
2693 /* cannot do pointer arithmetic on operand of ... */
2694 warning(136);
2695 }
2696 /* FALLTHROUGH */
2697 default:
2698 if ((elsz = size(tp->t_tspec)) == 0) {
2699 /* cannot do pointer arithmetic on operand of ... */
2700 error(136);
2701 } else if (elsz == -1) {
2702 LERROR("plength()");
2703 }
2704 break;
2705 }
2706
2707 if (elem == 0 && elsz != 0) {
2708 /* cannot do pointer arithmetic on operand of ... */
2709 error(136);
2710 }
2711
2712 if (elsz == 0)
2713 elsz = CHAR_BIT;
2714
2715 #if PTRDIFF_IS_LONG
2716 st = LONG;
2717 #else
2718 st = INT;
2719 #endif
2720
2721 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
2722 }
2723
2724 /*
2725 * XXX
2726 * Note: There appear to be a number of bugs in detecting overflow in
2727 * this function. An audit and a set of proper regression tests are needed.
2728 * --Perry Metzger, Nov. 16, 2001
2729 */
2730 /*
2731 * Do only as much as necessary to compute constant expressions.
2732 * Called only if the operator allows folding and (both) operands
2733 * are constants.
2734 */
2735 static tnode_t *
2736 fold(tnode_t *tn)
2737 {
2738 val_t *v;
2739 tspec_t t;
2740 int utyp, ovfl;
2741 int64_t sl, sr = 0, q = 0, mask;
2742 uint64_t ul, ur = 0;
2743 tnode_t *cn;
2744
2745 v = xcalloc(1, sizeof (val_t));
2746 v->v_tspec = t = tn->tn_type->t_tspec;
2747
2748 utyp = t == PTR || isutyp(t);
2749 ul = sl = tn->tn_left->tn_val->v_quad;
2750 if (modtab[tn->tn_op].m_binary)
2751 ur = sr = tn->tn_right->tn_val->v_quad;
2752
2753 mask = qlmasks[size(t)];
2754 ovfl = 0;
2755
2756 switch (tn->tn_op) {
2757 case UPLUS:
2758 q = sl;
2759 break;
2760 case UMINUS:
2761 q = -sl;
2762 if (msb(q, t, -1) == msb(sl, t, -1))
2763 ovfl = 1;
2764 break;
2765 case COMPL:
2766 q = ~sl;
2767 break;
2768 case MULT:
2769 if (utyp) {
2770 q = ul * ur;
2771 if (q != (q & mask))
2772 ovfl = 1;
2773 else if ((ul != 0) && ((q / ul) != ur))
2774 ovfl = 1;
2775 } else {
2776 q = sl * sr;
2777 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2778 ovfl = 1;
2779 }
2780 break;
2781 case DIV:
2782 if (sr == 0) {
2783 /* division by 0 */
2784 error(139);
2785 q = utyp ? UQUAD_MAX : QUAD_MAX;
2786 } else {
2787 q = utyp ? ul / ur : sl / sr;
2788 }
2789 break;
2790 case MOD:
2791 if (sr == 0) {
2792 /* modulus by 0 */
2793 error(140);
2794 q = 0;
2795 } else {
2796 q = utyp ? ul % ur : sl % sr;
2797 }
2798 break;
2799 case PLUS:
2800 q = utyp ? ul + ur : sl + sr;
2801 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2802 if (msb(q, t, -1) == 0)
2803 ovfl = 1;
2804 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2805 if (msb(q, t, -1) != 0)
2806 ovfl = 1;
2807 }
2808 break;
2809 case MINUS:
2810 q = utyp ? ul - ur : sl - sr;
2811 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2812 if (msb(q, t, -1) == 0)
2813 ovfl = 1;
2814 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2815 if (msb(q, t, -1) != 0)
2816 ovfl = 1;
2817 }
2818 break;
2819 case SHL:
2820 q = utyp ? ul << sr : sl << sr;
2821 break;
2822 case SHR:
2823 /*
2824 * The sign must be explicitly extended because
2825 * shifts of signed values are implementation dependent.
2826 */
2827 q = ul >> sr;
2828 q = xsign(q, t, size(t) - (int)sr);
2829 break;
2830 case LT:
2831 q = utyp ? ul < ur : sl < sr;
2832 break;
2833 case LE:
2834 q = utyp ? ul <= ur : sl <= sr;
2835 break;
2836 case GE:
2837 q = utyp ? ul >= ur : sl >= sr;
2838 break;
2839 case GT:
2840 q = utyp ? ul > ur : sl > sr;
2841 break;
2842 case EQ:
2843 q = utyp ? ul == ur : sl == sr;
2844 break;
2845 case NE:
2846 q = utyp ? ul != ur : sl != sr;
2847 break;
2848 case AND:
2849 q = utyp ? ul & ur : sl & sr;
2850 break;
2851 case XOR:
2852 q = utyp ? ul ^ ur : sl ^ sr;
2853 break;
2854 case OR:
2855 q = utyp ? ul | ur : sl | sr;
2856 break;
2857 default:
2858 LERROR("fold()");
2859 }
2860
2861 /* XXX does not work for quads. */
2862 if (ovfl || ((q | mask) != ~(uint64_t)0 && (q & ~mask) != 0)) {
2863 if (hflag)
2864 /* integer overflow detected, op %s */
2865 warning(141, modtab[tn->tn_op].m_name);
2866 }
2867
2868 v->v_quad = xsign(q, t, -1);
2869
2870 cn = getcnode(tn->tn_type, v);
2871
2872 return (cn);
2873 }
2874
2875 /*
2876 * Same for operators whose operands are compared with 0 (test context).
2877 */
2878 static tnode_t *
2879 foldtst(tnode_t *tn)
2880 {
2881 int l, r = 0;
2882 val_t *v;
2883
2884 v = xcalloc(1, sizeof (val_t));
2885 v->v_tspec = tn->tn_type->t_tspec;
2886 if (tn->tn_type->t_tspec != INT)
2887 LERROR("foldtst()");
2888
2889 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2890 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2891 } else {
2892 l = tn->tn_left->tn_val->v_quad != 0;
2893 }
2894
2895 if (modtab[tn->tn_op].m_binary) {
2896 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2897 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2898 } else {
2899 r = tn->tn_right->tn_val->v_quad != 0;
2900 }
2901 }
2902
2903 switch (tn->tn_op) {
2904 case NOT:
2905 if (hflag)
2906 /* constant argument to NOT */
2907 warning(239);
2908 v->v_quad = !l;
2909 break;
2910 case LOGAND:
2911 v->v_quad = l && r;
2912 break;
2913 case LOGOR:
2914 v->v_quad = l || r;
2915 break;
2916 default:
2917 LERROR("foldtst()");
2918 }
2919
2920 return (getcnode(tn->tn_type, v));
2921 }
2922
2923 /*
2924 * Same for operands with floating point type.
2925 */
2926 static tnode_t *
2927 foldflt(tnode_t *tn)
2928 {
2929 val_t *v;
2930 tspec_t t;
2931 ldbl_t l, r = 0;
2932
2933 v = xcalloc(1, sizeof (val_t));
2934 v->v_tspec = t = tn->tn_type->t_tspec;
2935
2936 if (!isftyp(t))
2937 LERROR("foldflt()");
2938
2939 if (t != tn->tn_left->tn_type->t_tspec)
2940 LERROR("foldflt()");
2941 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2942 LERROR("foldflt()");
2943
2944 l = tn->tn_left->tn_val->v_ldbl;
2945 if (modtab[tn->tn_op].m_binary)
2946 r = tn->tn_right->tn_val->v_ldbl;
2947
2948 switch (tn->tn_op) {
2949 case UPLUS:
2950 v->v_ldbl = l;
2951 break;
2952 case UMINUS:
2953 v->v_ldbl = -l;
2954 break;
2955 case MULT:
2956 v->v_ldbl = l * r;
2957 break;
2958 case DIV:
2959 if (r == 0.0) {
2960 /* division by 0 */
2961 error(139);
2962 if (t == FLOAT) {
2963 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2964 } else if (t == DOUBLE) {
2965 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2966 } else {
2967 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2968 }
2969 } else {
2970 v->v_ldbl = l / r;
2971 }
2972 break;
2973 case PLUS:
2974 v->v_ldbl = l + r;
2975 break;
2976 case MINUS:
2977 v->v_ldbl = l - r;
2978 break;
2979 case LT:
2980 v->v_quad = l < r;
2981 break;
2982 case LE:
2983 v->v_quad = l <= r;
2984 break;
2985 case GE:
2986 v->v_quad = l >= r;
2987 break;
2988 case GT:
2989 v->v_quad = l > r;
2990 break;
2991 case EQ:
2992 v->v_quad = l == r;
2993 break;
2994 case NE:
2995 v->v_quad = l != r;
2996 break;
2997 default:
2998 LERROR("foldflt()");
2999 }
3000
3001 if (isnan((double)v->v_ldbl))
3002 LERROR("foldflt()");
3003 if (!finite((double)v->v_ldbl) ||
3004 (t == FLOAT &&
3005 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3006 (t == DOUBLE &&
3007 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3008 /* floating point overflow detected, op %s */
3009 warning(142, modtab[tn->tn_op].m_name);
3010 if (t == FLOAT) {
3011 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3012 } else if (t == DOUBLE) {
3013 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3014 } else {
3015 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3016 }
3017 }
3018
3019 return (getcnode(tn->tn_type, v));
3020 }
3021
3022 /*
3023 * Create a constant node for sizeof.
3024 */
3025 tnode_t *
3026 bldszof(type_t *tp)
3027 {
3028 int elem, elsz;
3029 tspec_t st;
3030
3031 elem = 1;
3032 while (tp->t_tspec == ARRAY) {
3033 elem *= tp->t_dim;
3034 tp = tp->t_subt;
3035 }
3036 if (elem == 0) {
3037 /* cannot take size of incomplete type */
3038 error(143);
3039 elem = 1;
3040 }
3041 switch (tp->t_tspec) {
3042 case FUNC:
3043 /* cannot take size of function */
3044 error(144);
3045 elsz = 1;
3046 break;
3047 case STRUCT:
3048 case UNION:
3049 if (incompl(tp)) {
3050 /* cannot take size of incomplete type */
3051 error(143);
3052 elsz = 1;
3053 } else {
3054 elsz = tp->t_str->size;
3055 }
3056 break;
3057 case ENUM:
3058 if (incompl(tp)) {
3059 /* cannot take size of incomplete type */
3060 warning(143);
3061 }
3062 /* FALLTHROUGH */
3063 default:
3064 if (tp->t_isfield) {
3065 /* cannot take size of bit-field */
3066 error(145);
3067 }
3068 if (tp->t_tspec == VOID) {
3069 /* cannot take size of void */
3070 error(146);
3071 elsz = 1;
3072 } else {
3073 elsz = size(tp->t_tspec);
3074 if (elsz <= 0)
3075 LERROR("bldszof()");
3076 }
3077 break;
3078 }
3079
3080 #if SIZEOF_IS_ULONG
3081 st = ULONG;
3082 #else
3083 st = UINT;
3084 #endif
3085
3086 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
3087 }
3088
3089 /*
3090 * Type casts.
3091 */
3092 tnode_t *
3093 cast(tnode_t *tn, type_t *tp)
3094 {
3095 tspec_t nt, ot;
3096
3097 if (tn == NULL)
3098 return (NULL);
3099
3100 tn = cconv(tn);
3101
3102 nt = tp->t_tspec;
3103 ot = tn->tn_type->t_tspec;
3104
3105 if (nt == VOID) {
3106 /*
3107 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3108 * But this seams really questionable.
3109 */
3110 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3111 /* invalid cast expression */
3112 error(147);
3113 return (NULL);
3114 } else if (ot == STRUCT || ot == UNION) {
3115 /* invalid cast expression */
3116 error(147);
3117 return (NULL);
3118 } else if (ot == VOID) {
3119 /* improper cast of void expression */
3120 error(148);
3121 return (NULL);
3122 } else if (isityp(nt) && issclt(ot)) {
3123 /* ok */
3124 } else if (isftyp(nt) && isatyp(ot)) {
3125 /* ok */
3126 } else if (nt == PTR && isityp(ot)) {
3127 /* ok */
3128 } else if (nt == PTR && ot == PTR) {
3129 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3130 if (hflag)
3131 /* cast discards 'const' from ... */
3132 warning(275);
3133 }
3134 } else {
3135 /* invalid cast expression */
3136 error(147);
3137 return (NULL);
3138 }
3139
3140 tn = convert(CVT, 0, tp, tn);
3141 tn->tn_cast = 1;
3142
3143 return (tn);
3144 }
3145
3146 /*
3147 * Create the node for a function argument.
3148 * All necessary conversions and type checks are done in funccall(), because
3149 * in funcarg() we have no information about expected argument types.
3150 */
3151 tnode_t *
3152 funcarg(tnode_t *args, tnode_t *arg)
3153 {
3154 tnode_t *ntn;
3155
3156 /*
3157 * If there was a serious error in the expression for the argument,
3158 * create a dummy argument so the positions of the remaining arguments
3159 * will not change.
3160 */
3161 if (arg == NULL)
3162 arg = getinode(INT, (int64_t)0);
3163
3164 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3165
3166 return (ntn);
3167 }
3168
3169 /*
3170 * Create the node for a function call. Also check types of
3171 * function arguments and insert conversions, if necessary.
3172 */
3173 tnode_t *
3174 funccall(tnode_t *func, tnode_t *args)
3175 {
3176 tnode_t *ntn;
3177 op_t fcop;
3178
3179 if (func == NULL)
3180 return (NULL);
3181
3182 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3183 fcop = CALL;
3184 } else {
3185 fcop = ICALL;
3186 }
3187
3188 /*
3189 * after cconv() func will always be a pointer to a function
3190 * if it is a valid function designator.
3191 */
3192 func = cconv(func);
3193
3194 if (func->tn_type->t_tspec != PTR ||
3195 func->tn_type->t_subt->t_tspec != FUNC) {
3196 /* illegal function */
3197 error(149);
3198 return (NULL);
3199 }
3200
3201 args = chkfarg(func->tn_type->t_subt, args);
3202
3203 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3204
3205 return (ntn);
3206 }
3207
3208 /*
3209 * Check types of all function arguments and insert conversions,
3210 * if necessary.
3211 */
3212 static tnode_t *
3213 chkfarg(type_t *ftp, tnode_t *args)
3214 {
3215 tnode_t *arg;
3216 sym_t *asym;
3217 tspec_t at;
3218 int narg, npar, n, i;
3219
3220 /* get # of args in the prototype */
3221 npar = 0;
3222 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3223 npar++;
3224
3225 /* get # of args in function call */
3226 narg = 0;
3227 for (arg = args; arg != NULL; arg = arg->tn_right)
3228 narg++;
3229
3230 asym = ftp->t_args;
3231 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3232 /* argument mismatch: %d arg%s passed, %d expected */
3233 error(150, narg, narg > 1 ? "s" : "", npar);
3234 asym = NULL;
3235 }
3236
3237 for (n = 1; n <= narg; n++) {
3238
3239 /*
3240 * The rightmost argument is at the top of the argument
3241 * subtree.
3242 */
3243 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3244 continue;
3245
3246 /* some things which are always not allowd */
3247 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3248 /* void expressions may not be arguments, arg #%d */
3249 error(151, n);
3250 return (NULL);
3251 } else if ((at == STRUCT || at == UNION) &&
3252 incompl(arg->tn_left->tn_type)) {
3253 /* argument cannot have unknown size, arg #%d */
3254 error(152, n);
3255 return (NULL);
3256 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3257 incompl(arg->tn_left->tn_type)) {
3258 /* argument cannot have unknown size, arg #%d */
3259 warning(152, n);
3260 }
3261
3262 /* class conversions (arg in value context) */
3263 arg->tn_left = cconv(arg->tn_left);
3264
3265 if (asym != NULL) {
3266 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3267 } else {
3268 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3269 }
3270 arg->tn_type = arg->tn_left->tn_type;
3271
3272 if (asym != NULL)
3273 asym = asym->s_nxt;
3274 }
3275
3276 return (args);
3277 }
3278
3279 /*
3280 * Compare the type of an argument with the corresponding type of a
3281 * prototype parameter. If it is a valid combination, but both types
3282 * are not the same, insert a conversion to convert the argument into
3283 * the type of the parameter.
3284 */
3285 static tnode_t *
3286 parg( int n, /* pos of arg */
3287 type_t *tp, /* expected type (from prototype) */
3288 tnode_t *tn) /* argument */
3289 {
3290 tnode_t *ln;
3291 int warn;
3292
3293 ln = xcalloc(1, sizeof (tnode_t));
3294 ln->tn_type = tduptyp(tp);
3295 ln->tn_type->t_const = 0;
3296 ln->tn_lvalue = 1;
3297 if (typeok(FARG, n, ln, tn)) {
3298 if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
3299 tn = convert(FARG, n, tp, tn);
3300 }
3301 free(ln);
3302 return (tn);
3303 }
3304
3305 /*
3306 * Return the value of an integral constant expression.
3307 * If the expression is not constant or its type is not an integer
3308 * type, an error message is printed.
3309 */
3310 val_t *
3311 constant(tnode_t *tn, int required)
3312 {
3313 val_t *v;
3314
3315 if (tn != NULL)
3316 tn = cconv(tn);
3317 if (tn != NULL)
3318 tn = promote(NOOP, 0, tn);
3319
3320 v = xcalloc(1, sizeof (val_t));
3321
3322 if (tn == NULL) {
3323 if (nerr == 0)
3324 LERROR("constant()");
3325 v->v_tspec = INT;
3326 v->v_quad = 1;
3327 return (v);
3328 }
3329
3330 v->v_tspec = tn->tn_type->t_tspec;
3331
3332 if (tn->tn_op == CON) {
3333 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3334 LERROR("constant()");
3335 if (isityp(tn->tn_val->v_tspec)) {
3336 v->v_ansiu = tn->tn_val->v_ansiu;
3337 v->v_quad = tn->tn_val->v_quad;
3338 return (v);
3339 }
3340 v->v_quad = tn->tn_val->v_ldbl;
3341 } else {
3342 v->v_quad = 1;
3343 }
3344
3345 /* integral constant expression expected */
3346 if (required)
3347 error(55);
3348 else
3349 gnuism(318);
3350
3351 if (!isityp(v->v_tspec))
3352 v->v_tspec = INT;
3353
3354 return (v);
3355 }
3356
3357 /*
3358 * Perform some tests on expressions which can't be done in build() and
3359 * functions called by build(). These tests must be done here because
3360 * we need some information about the context in which the operations
3361 * are performed.
3362 * After all tests are performed, expr() frees the memory which is used
3363 * for the expression.
3364 */
3365 void
3366 expr(tnode_t *tn, int vctx, int tctx, int freeblk)
3367 {
3368
3369 if (tn == NULL && nerr == 0)
3370 LERROR("expr()");
3371
3372 if (tn == NULL) {
3373 tfreeblk();
3374 return;
3375 }
3376
3377 /* expr() is also called in global initialisations */
3378 if (dcs->d_ctx != EXTERN)
3379 chkreach();
3380
3381 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3382 if (tn->tn_op == ASSIGN) {
3383 if (hflag && tctx)
3384 /* assignment in conditional context */
3385 warning(159);
3386 } else if (tn->tn_op == CON) {
3387 if (hflag && tctx && !ccflg)
3388 /* constant in conditional context */
3389 warning(161);
3390 }
3391 if (!modtab[tn->tn_op].m_sideeff) {
3392 /*
3393 * for left operands of COMMA this warning is already
3394 * printed
3395 */
3396 if (tn->tn_op != COMMA && !vctx && !tctx)
3397 nulleff(tn);
3398 }
3399 if (dflag)
3400 displexpr(tn, 0);
3401
3402 /* free the tree memory */
3403 if (freeblk)
3404 tfreeblk();
3405 }
3406
3407 static void
3408 nulleff(tnode_t *tn)
3409 {
3410
3411 if (!hflag)
3412 return;
3413
3414 while (!modtab[tn->tn_op].m_sideeff) {
3415 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3416 tn = tn->tn_left;
3417 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3418 /*
3419 * && and || have a side effect if the right operand
3420 * has a side effect.
3421 */
3422 tn = tn->tn_right;
3423 } else if (tn->tn_op == QUEST) {
3424 /*
3425 * ? has a side effect if at least one of its right
3426 * operands has a side effect
3427 */
3428 tn = tn->tn_right;
3429 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3430 /*
3431 * : has a side effect if at least one of its operands
3432 * has a side effect
3433 */
3434 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3435 tn = tn->tn_left;
3436 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3437 tn = tn->tn_right;
3438 } else {
3439 break;
3440 }
3441 } else {
3442 break;
3443 }
3444 }
3445 if (!modtab[tn->tn_op].m_sideeff)
3446 /* expression has null effect */
3447 warning(129);
3448 }
3449
3450 /*
3451 * Dump an expression to stdout
3452 * only used for debugging
3453 */
3454 static void
3455 displexpr(tnode_t *tn, int offs)
3456 {
3457 uint64_t uq;
3458
3459 if (tn == NULL) {
3460 (void)printf("%*s%s\n", offs, "", "NULL");
3461 return;
3462 }
3463 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3464
3465 if (tn->tn_op == NAME) {
3466 (void)printf("%s: %s ",
3467 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3468 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3469 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3470 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3471 uq = tn->tn_val->v_quad;
3472 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3473 (long)uq & 0xffffffffl);
3474 } else if (tn->tn_op == CON) {
3475 if (tn->tn_type->t_tspec != PTR)
3476 LERROR("displexpr()");
3477 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3478 (u_long)tn->tn_val->v_quad);
3479 } else if (tn->tn_op == STRING) {
3480 if (tn->tn_strg->st_tspec == CHAR) {
3481 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3482 } else {
3483 char *s;
3484 size_t n;
3485 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3486 s = xmalloc(n);
3487 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3488 (void)printf("L\"%s\"", s);
3489 free(s);
3490 }
3491 (void)printf(" ");
3492 } else if (tn->tn_op == FSEL) {
3493 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3494 tn->tn_type->t_flen);
3495 }
3496 (void)printf("%s\n", ttos(tn->tn_type));
3497 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3498 return;
3499 displexpr(tn->tn_left, offs + 2);
3500 if (modtab[tn->tn_op].m_binary ||
3501 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3502 displexpr(tn->tn_right, offs + 2);
3503 }
3504 }
3505
3506 /*
3507 * Called by expr() to recursively perform some tests.
3508 */
3509 /* ARGSUSED */
3510 void
3511 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3512 int szof)
3513 {
3514 tnode_t *ln, *rn;
3515 mod_t *mp;
3516 int nrvdisc, cvctx, ctctx;
3517 op_t op;
3518 scl_t sc;
3519 dinfo_t *di;
3520
3521 if (tn == NULL)
3522 return;
3523
3524 ln = tn->tn_left;
3525 rn = tn->tn_right;
3526 mp = &modtab[op = tn->tn_op];
3527
3528 switch (op) {
3529 case AMPER:
3530 if (ln->tn_op == NAME && (reached || rchflg)) {
3531 if (!szof)
3532 setsflg(ln->tn_sym);
3533 setuflg(ln->tn_sym, fcall, szof);
3534 }
3535 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3536 /* check the range of array indices */
3537 chkaidx(ln->tn_left, 1);
3538 break;
3539 case LOAD:
3540 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3541 /* check the range of array indices */
3542 chkaidx(ln->tn_left, 0);
3543 /* FALLTHROUGH */
3544 case PUSH:
3545 case INCBEF:
3546 case DECBEF:
3547 case INCAFT:
3548 case DECAFT:
3549 case ADDASS:
3550 case SUBASS:
3551 case MULASS:
3552 case DIVASS:
3553 case MODASS:
3554 case ANDASS:
3555 case ORASS:
3556 case XORASS:
3557 case SHLASS:
3558 case SHRASS:
3559 if (ln->tn_op == NAME && (reached || rchflg)) {
3560 sc = ln->tn_sym->s_scl;
3561 /*
3562 * Look if there was a asm statement in one of the
3563 * compound statements we are in. If not, we don't
3564 * print a warning.
3565 */
3566 for (di = dcs; di != NULL; di = di->d_nxt) {
3567 if (di->d_asm)
3568 break;
3569 }
3570 if (sc != EXTERN && sc != STATIC &&
3571 !ln->tn_sym->s_set && !szof && di == NULL) {
3572 /* %s may be used before set */
3573 warning(158, ln->tn_sym->s_name);
3574 setsflg(ln->tn_sym);
3575 }
3576 setuflg(ln->tn_sym, 0, 0);
3577 }
3578 break;
3579 case ASSIGN:
3580 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3581 setsflg(ln->tn_sym);
3582 if (ln->tn_sym->s_scl == EXTERN)
3583 outusg(ln->tn_sym);
3584 }
3585 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3586 /* check the range of array indices */
3587 chkaidx(ln->tn_left, 0);
3588 break;
3589 case CALL:
3590 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3591 LERROR("chkmisc()");
3592 if (!szof)
3593 outcall(tn, vctx || tctx, rvdisc);
3594 break;
3595 case EQ:
3596 /* equality operator "==" found where "=" was exp. */
3597 if (hflag && eqwarn)
3598 warning(160);
3599 break;
3600 case CON:
3601 case NAME:
3602 case STRING:
3603 return;
3604 /* LINTED (enumeration values not handled in switch) */
3605 case OR:
3606 case XOR:
3607 case NE:
3608 case GE:
3609 case GT:
3610 case LE:
3611 case LT:
3612 case SHR:
3613 case SHL:
3614 case MINUS:
3615 case PLUS:
3616 case MOD:
3617 case DIV:
3618 case MULT:
3619 case STAR:
3620 case UMINUS:
3621 case UPLUS:
3622 case DEC:
3623 case INC:
3624 case COMPL:
3625 case NOT:
3626 case POINT:
3627 case ARROW:
3628 case NOOP:
3629 case AND:
3630 case FARG:
3631 case CASE:
3632 case INIT:
3633 case RETURN:
3634 case ICALL:
3635 case CVT:
3636 case COMMA:
3637 case FSEL:
3638 case COLON:
3639 case QUEST:
3640 case LOGOR:
3641 case LOGAND:
3642 break;
3643 }
3644
3645 cvctx = mp->m_vctx;
3646 ctctx = mp->m_tctx;
3647 /*
3648 * values of operands of ':' are not used if the type of at least
3649 * one of the operands (for gcc compatibility) is void
3650 * XXX test/value context of QUEST should probably be used as
3651 * context for both operands of COLON
3652 */
3653 if (op == COLON && tn->tn_type->t_tspec == VOID)
3654 cvctx = ctctx = 0;
3655 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3656 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3657
3658 switch (op) {
3659 case PUSH:
3660 if (rn != NULL)
3661 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3662 break;
3663 case LOGAND:
3664 case LOGOR:
3665 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3666 break;
3667 case COLON:
3668 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3669 break;
3670 case COMMA:
3671 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3672 break;
3673 default:
3674 if (mp->m_binary)
3675 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3676 break;
3677 }
3678
3679 }
3680
3681 /*
3682 * Checks the range of array indices, if possible.
3683 * amper is set if only the address of the element is used. This
3684 * means that the index is allowd to refere to the first element
3685 * after the array.
3686 */
3687 static void
3688 chkaidx(tnode_t *tn, int amper)
3689 {
3690 int dim;
3691 tnode_t *ln, *rn;
3692 int elsz;
3693 int64_t con;
3694
3695 ln = tn->tn_left;
3696 rn = tn->tn_right;
3697
3698 /* We can only check constant indices. */
3699 if (rn->tn_op != CON)
3700 return;
3701
3702 /* Return if the left node does not stem from an array. */
3703 if (ln->tn_op != AMPER)
3704 return;
3705 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3706 return;
3707 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3708 return;
3709
3710 /*
3711 * For incomplete array types, we can print a warning only if
3712 * the index is negative.
3713 */
3714 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3715 return;
3716
3717 /* Get the size of one array element */
3718 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3719 return;
3720 elsz /= CHAR_BIT;
3721
3722 /* Change the unit of the index from bytes to element size. */
3723 if (isutyp(rn->tn_type->t_tspec)) {
3724 con = (uint64_t)rn->tn_val->v_quad / elsz;
3725 } else {
3726 con = rn->tn_val->v_quad / elsz;
3727 }
3728
3729 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3730
3731 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3732 /* array subscript cannot be negative: %ld */
3733 warning(167, (long)con);
3734 } else if (dim > 0 && (uint64_t)con >= dim) {
3735 /* array subscript cannot be > %d: %ld */
3736 warning(168, dim - 1, (long)con);
3737 }
3738 }
3739
3740 /*
3741 * Check for ordered comparisons of unsigned values with 0.
3742 */
3743 static void
3744 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3745 {
3746 char buf[64];
3747 tspec_t lt, rt;
3748 mod_t *mp;
3749
3750 lt = ln->tn_type->t_tspec;
3751 rt = rn->tn_type->t_tspec;
3752 mp = &modtab[op];
3753
3754 if (ln->tn_op != CON && rn->tn_op != CON)
3755 return;
3756
3757 if (!isityp(lt) || !isityp(rt))
3758 return;
3759
3760 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3761 (rn->tn_val->v_quad < 0 ||
3762 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3763 /* nonportable character comparison, op %s */
3764 warning(230, mp->m_name);
3765 return;
3766 }
3767 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3768 (ln->tn_val->v_quad < 0 ||
3769 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3770 /* nonportable character comparison, op %s */
3771 warning(230, mp->m_name);
3772 return;
3773 }
3774 if (isutyp(lt) && !isutyp(rt) &&
3775 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3776 if (rn->tn_val->v_quad < 0) {
3777 /* comparison of %s with %s, op %s */
3778 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3779 "negative constant", mp->m_name);
3780 } else if (op == LT || op == GE || (hflag && op == LE)) {
3781 /* comparison of %s with %s, op %s */
3782 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3783 "0", mp->m_name);
3784 }
3785 return;
3786 }
3787 if (isutyp(rt) && !isutyp(lt) &&
3788 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3789 if (ln->tn_val->v_quad < 0) {
3790 /* comparison of %s with %s, op %s */
3791 warning(162, "negative constant",
3792 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name);
3793 } else if (op == GT || op == LE || (hflag && op == GE)) {
3794 /* comparison of %s with %s, op %s */
3795 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type),
3796 mp->m_name);
3797 }
3798 return;
3799 }
3800 }
3801
3802 /*
3803 * Takes an expression an returns 0 if this expression can be used
3804 * for static initialisation, otherwise -1.
3805 *
3806 * Constant initialisation expressions must be constant or an address
3807 * of a static object with an optional offset. In the first case,
3808 * the result is returned in *offsp. In the second case, the static
3809 * object is returned in *symp and the offset in *offsp.
3810 *
3811 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3812 * CON. Type conversions are allowed if they do not change binary
3813 * representation (including width).
3814 */
3815 int
3816 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3817 {
3818 sym_t *sym;
3819 ptrdiff_t offs1, offs2;
3820 tspec_t t, ot;
3821
3822 switch (tn->tn_op) {
3823 case MINUS:
3824 if (tn->tn_right->tn_op == CVT)
3825 return conaddr(tn->tn_right, symp, offsp);
3826 else if (tn->tn_right->tn_op != CON)
3827 return (-1);
3828 /* FALLTHROUGH */
3829 case PLUS:
3830 offs1 = offs2 = 0;
3831 if (tn->tn_left->tn_op == CON) {
3832 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3833 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3834 return (-1);
3835 } else if (tn->tn_right->tn_op == CON) {
3836 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3837 if (tn->tn_op == MINUS)
3838 offs2 = -offs2;
3839 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3840 return (-1);
3841 } else {
3842 return (-1);
3843 }
3844 *symp = sym;
3845 *offsp = offs1 + offs2;
3846 break;
3847 case AMPER:
3848 if (tn->tn_left->tn_op == NAME) {
3849 *symp = tn->tn_left->tn_sym;
3850 *offsp = 0;
3851 } else if (tn->tn_left->tn_op == STRING) {
3852 /*
3853 * If this would be the front end of a compiler we
3854 * would return a label instead of 0.
3855 */
3856 *offsp = 0;
3857 }
3858 break;
3859 case CVT:
3860 t = tn->tn_type->t_tspec;
3861 ot = tn->tn_left->tn_type->t_tspec;
3862 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR))
3863 return (-1);
3864 #ifdef notdef
3865 /*
3866 * consider:
3867 * struct foo {
3868 * unsigned char a;
3869 * } f = {
3870 * (u_char)(u_long)(&(((struct foo *)0)->a))
3871 * };
3872 * since psize(u_long) != psize(u_char) this fails.
3873 */
3874 else if (psize(t) != psize(ot))
3875 return (-1);
3876 #endif
3877 if (conaddr(tn->tn_left, symp, offsp) == -1)
3878 return (-1);
3879 break;
3880 default:
3881 return (-1);
3882 }
3883 return (0);
3884 }
3885
3886 /*
3887 * Concatenate two string constants.
3888 */
3889 strg_t *
3890 catstrg(strg_t *strg1, strg_t *strg2)
3891 {
3892 size_t len1, len2, len;
3893
3894 if (strg1->st_tspec != strg2->st_tspec) {
3895 /* cannot concatenate wide and regular string literals */
3896 error(292);
3897 return (strg1);
3898 }
3899
3900 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3901
3902 if (strg1->st_tspec == CHAR) {
3903 strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
3904 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3905 free(strg2->st_cp);
3906 } else {
3907 strg1->st_wcp = xrealloc(strg1->st_wcp,
3908 (len + 1) * sizeof (wchar_t));
3909 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3910 (len2 + 1) * sizeof (wchar_t));
3911 free(strg2->st_wcp);
3912 }
3913 free(strg2);
3914
3915 return (strg1);
3916 }
3917
3918 /*
3919 * Print a warning if the given node has operands which should be
3920 * parenthesized.
3921 *
3922 * XXX Does not work if an operand is a constant expression. Constant
3923 * expressions are already folded.
3924 */
3925 static void
3926 precconf(tnode_t *tn)
3927 {
3928 tnode_t *ln, *rn;
3929 op_t lop, rop = NOOP;
3930 int lparn, rparn = 0;
3931 mod_t *mp;
3932 int warn;
3933
3934 if (!hflag)
3935 return;
3936
3937 mp = &modtab[tn->tn_op];
3938
3939 lparn = 0;
3940 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
3941 lparn |= ln->tn_parn;
3942 lparn |= ln->tn_parn;
3943 lop = ln->tn_op;
3944
3945 if (mp->m_binary) {
3946 rparn = 0;
3947 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
3948 rparn |= rn->tn_parn;
3949 rparn |= rn->tn_parn;
3950 rop = rn->tn_op;
3951 }
3952
3953 warn = 0;
3954
3955 switch (tn->tn_op) {
3956 case SHL:
3957 case SHR:
3958 if (!lparn && (lop == PLUS || lop == MINUS)) {
3959 warn = 1;
3960 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
3961 warn = 1;
3962 }
3963 break;
3964 case LOGOR:
3965 if (!lparn && lop == LOGAND) {
3966 warn = 1;
3967 } else if (!rparn && rop == LOGAND) {
3968 warn = 1;
3969 }
3970 break;
3971 case AND:
3972 case XOR:
3973 case OR:
3974 if (!lparn && lop != tn->tn_op) {
3975 if (lop == PLUS || lop == MINUS) {
3976 warn = 1;
3977 } else if (lop == AND || lop == XOR) {
3978 warn = 1;
3979 }
3980 }
3981 if (!warn && !rparn && rop != tn->tn_op) {
3982 if (rop == PLUS || rop == MINUS) {
3983 warn = 1;
3984 } else if (rop == AND || rop == XOR) {
3985 warn = 1;
3986 }
3987 }
3988 break;
3989 /* LINTED (enumeration values not handled in switch) */
3990 case DECAFT:
3991 case XORASS:
3992 case SHLASS:
3993 case NOOP:
3994 case ARROW:
3995 case ORASS:
3996 case POINT:
3997 case NAME:
3998 case NOT:
3999 case COMPL:
4000 case CON:
4001 case INC:
4002 case STRING:
4003 case DEC:
4004 case INCBEF:
4005 case DECBEF:
4006 case INCAFT:
4007 case FSEL:
4008 case CALL:
4009 case COMMA:
4010 case CVT:
4011 case ICALL:
4012 case LOAD:
4013 case PUSH:
4014 case RETURN:
4015 case INIT:
4016 case CASE:
4017 case FARG:
4018 case SUBASS:
4019 case ADDASS:
4020 case MODASS:
4021 case DIVASS:
4022 case MULASS:
4023 case ASSIGN:
4024 case COLON:
4025 case QUEST:
4026 case LOGAND:
4027 case NE:
4028 case EQ:
4029 case GE:
4030 case GT:
4031 case LE:
4032 case LT:
4033 case MINUS:
4034 case PLUS:
4035 case MOD:
4036 case DIV:
4037 case MULT:
4038 case AMPER:
4039 case STAR:
4040 case UMINUS:
4041 case SHRASS:
4042 case UPLUS:
4043 case ANDASS:
4044 break;
4045 }
4046
4047 if (warn) {
4048 /* precedence confusion possible: parenthesize! */
4049 warning(169);
4050 }
4051
4052 }
4053