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