Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / xlint / lint1 / tree.c
blob3324cb54fda5da8ff187cfecef3f9fe489ea3925
1 /* $NetBSD: tree.c,v 1.61 2009/10/02 19:02:16 christos Exp $ */
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
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.
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.
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(lint)
40 __RCSID("$NetBSD: tree.c,v 1.61 2009/10/02 19:02:16 christos Exp $");
41 #endif
43 #include <stdlib.h>
44 #include <string.h>
45 #include <float.h>
46 #include <limits.h>
47 #include <math.h>
48 #include <signal.h>
50 #include "lint1.h"
51 #include "cgram.h"
52 #include "externs1.h"
54 /* Various flags for each operator. */
55 static mod_t modtab[NOPS];
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 *);
93 extern sig_atomic_t fpe;
96 * Initialize mods of operators.
98 void
99 initmtab(void)
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, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, NULL } }
219 int i;
221 for (i = 0; imods[i].op != NOOP; i++)
222 STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m);
226 * Increase degree of reference.
227 * This is most often used to change type "T" in type "pointer to T".
229 type_t *
230 incref(type_t *tp, tspec_t t)
232 type_t *tp2;
234 tp2 = getblk(sizeof (type_t));
235 tp2->t_tspec = t;
236 tp2->t_subt = tp;
237 return (tp2);
241 * same for use in expressions
243 type_t *
244 tincref(type_t *tp, tspec_t t)
246 type_t *tp2;
248 tp2 = tgetblk(sizeof (type_t));
249 tp2->t_tspec = t;
250 tp2->t_subt = tp;
251 return (tp2);
255 * Create a node for a constant.
257 tnode_t *
258 getcnode(type_t *tp, val_t *v)
260 tnode_t *n;
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);
274 * Create a node for a integer constant.
276 static tnode_t *
277 getinode(tspec_t t, int64_t q)
279 tnode_t *n;
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);
291 * Create a node for a name (symbol table entry).
292 * ntok is the token which follows the name.
294 tnode_t *
295 getnnode(sym_t *sym, int ntok)
297 tnode_t *n;
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);
308 * XXX if tflag is set the symbol should be
309 * exported to level 0
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;
329 if (fixtype) {
330 sym->s_type = incref(gettyp(CHAR), PTR);
331 sym->s_type->t_const = 1;
337 if (sym->s_kind != FVFT && sym->s_kind != FMOS)
338 LERROR("getnnode()");
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;
353 return (n);
357 * Create a node for a string.
359 tnode_t *
360 getsnode(strg_t *strg)
362 size_t len;
363 tnode_t *n;
365 len = strg->st_len;
367 n = getnode();
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;
374 n->tn_strg = tgetblk(sizeof (strg_t));
375 n->tn_strg->st_tspec = strg->st_tspec;
376 n->tn_strg->st_len = len;
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);
388 free(strg);
390 return (n);
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.
397 sym_t *
398 strmemb(tnode_t *tn, op_t op, sym_t *msym)
400 str_t *str;
401 type_t *tp;
402 sym_t *sym, *csym;
403 int eq;
404 tspec_t t;
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).
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);
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;
436 * If this struct/union has a member with the name of msym, return
437 * return this it.
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);
452 * Set eq to 0 if there are struct/union members with the same name
453 * and different types and/or offsets.
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;
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;
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;
480 if (csym->s_field) {
481 type_t *tp1, *tp2;
483 tp1 = csym->s_type;
484 tp2 = sym->s_type;
485 if (tp1->t_flen != tp2->t_flen) {
486 eq = 0;
487 break;
489 if (tp1->t_foffs != tp2->t_foffs) {
490 eq = 0;
491 break;
495 if (!eq)
496 break;
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.
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);
510 return (msym);
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.
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);
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);
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);
543 return (msym);
547 * Create a tree node. Called for most operands except function calls,
548 * sizeof and casts.
550 * op operator
551 * ln left operand
552 * rn if not NULL, right operand
554 tnode_t *
555 build(op_t op, tnode_t *ln, tnode_t *rn)
557 mod_t *mp;
558 tnode_t *ntn;
559 type_t *rtp;
561 mp = &modtab[op];
563 /* If there was an error in one of the operands, return. */
564 if (ln == NULL || (mp->m_binary && rn == NULL))
565 return (NULL);
568 * Apply class conversions to the left operand, but only if its
569 * value is needed or it is compaired with null.
571 if (mp->m_vctx || mp->m_tctx)
572 ln = cconv(ln);
574 * The right operand is almost always in a test or value context,
575 * except if it is a struct or union member.
577 if (mp->m_binary && op != ARROW && op != POINT)
578 rn = cconv(rn);
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.
587 if (mp->m_comp)
588 chkcomp(op, ln, rn);
591 * Promote the left operand if it is in a test or value context
593 if (mp->m_vctx || mp->m_tctx)
594 ln = promote(op, 0, ln);
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
599 if (mp->m_binary && op != ARROW && op != POINT &&
600 op != ASSIGN && op != RETURN) {
601 rn = promote(op, 0, rn);
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.
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;
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;
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);
625 * Check types for compatibility with the operation and mutual
626 * compatibility. Return if there are serios problems.
628 if (!typeok(op, 0, ln, rn))
629 return (NULL);
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;
690 /* Return if an error occurred. */
691 if (ntn == NULL)
692 return (NULL);
694 /* Print a warning if precedence confusion is possible */
695 if (mp->m_tpconf)
696 precconf(ntn);
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.
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);
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);
721 } else if (op == QUEST && ln->tn_op == CON) {
722 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
726 return (ntn);
730 * Perform class conversions.
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.
736 tnode_t *
737 cconv(tnode_t *tn)
739 type_t *tp;
742 * Array-lvalue (array of type T) is converted into rvalue
743 * (pointer to type T)
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);
751 tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
752 tn, NULL);
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)
760 if (tn->tn_type->t_tspec == FUNC)
761 tn = bldamper(tn, 1);
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);
770 return (tn);
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.
778 * If the types are ok, typeok() returns 1, otherwise 0.
781 typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
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;
789 mp = &modtab[op];
791 if ((ltp = ln->tn_type) == NULL)
792 LERROR("typeok()");
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;
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);
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);
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);
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);
830 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
832 * For these operations we need the types before promotion
833 * and balancing.
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;
843 switch (op) {
844 case POINT:
846 * Most errors required by ANSI C are reported in strmemb().
847 * Here we only must check for totaly wrong things.
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);
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);
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);
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);
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);
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);
910 } else if (lt != STRUCT && lt != UNION) {
911 /* unacceptable operand of %s */
912 error(111, mp->m_name);
913 return (0);
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);
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);
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);
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);
945 if (lt == PTR && rt == PTR) {
946 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
947 /* illegal pointer subtraction */
948 error(116);
951 break;
952 case SHR:
953 /* operands have integer types (checked above) */
954 if (pflag && !isutyp(lt)) {
956 * The left operand is signed. This means that
957 * the operation is (possibly) nonportable.
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);
966 } else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
968 * The left operand would become unsigned in
969 * traditional C.
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);
976 } else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
977 psize(lt) < psize(rt)) {
979 * In traditional C the left operand would be extended,
980 * possibly with 1, and then shifted.
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);
988 goto shift;
989 case SHL:
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.
998 if (psize(lt) < psize(rt)) {
1000 * XXX If both operands are constant make sure
1001 * that there is really a differencs between
1002 * ANSI C and traditional C.
1004 if (hflag)
1005 /* semantics of %s change in ANSI C; use ... */
1006 warning(118, mp->m_name);
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 == (uint64_t)size(lt)) {
1014 /* shift equal to size fo object */
1015 warning(267);
1016 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) {
1017 /* shift greater than size of object */
1018 warning(122);
1021 break;
1022 case EQ:
1023 case NE:
1025 * Accept some things which are allowed with EQ and NE,
1026 * but not with ordered comparisons.
1028 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1029 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1030 break;
1032 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1033 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1034 break;
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);
1049 } else if (lt == PTR && rt == PTR) {
1050 ptrcmpok(op, ln, rn);
1052 break;
1053 case QUEST:
1054 if (!issclt(lt)) {
1055 /* first operand must have scalar type, op ? : */
1056 error(170);
1057 return (0);
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:
1066 if (isatyp(lt) && isatyp(rt))
1067 break;
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;
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;
1079 if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
1080 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1081 break;
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;
1090 if (lt == VOID || rt == VOID) {
1091 if (lt != VOID || rt != VOID)
1092 /* incompatible types in conditional */
1093 warning(126);
1094 break;
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;
1107 if (rt == PTR && lt == PTR) {
1108 if (eqptrtype(lstp, rstp, 1))
1109 break;
1110 if (!eqtype(lstp, rstp, 1, 0, NULL))
1111 illptrc(mp, ltp, rtp);
1112 break;
1115 /* incompatible types in conditional */
1116 error(126);
1117 return (0);
1119 case ASSIGN:
1120 case INIT:
1121 case FARG:
1122 case RETURN:
1123 if (!asgntypok(op, arg, ln, rn))
1124 return (0);
1125 goto assign;
1126 case MULASS:
1127 case DIVASS:
1128 case MODASS:
1129 goto assign;
1130 case ADDASS:
1131 case SUBASS:
1132 /* operands have scalar types (checked above) */
1133 if ((lt == PTR && !isityp(rt)) || rt == PTR) {
1134 incompat(op, lt, rt);
1135 return (0);
1137 goto assign;
1138 case SHLASS:
1139 goto assign;
1140 case SHRASS:
1141 if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
1142 /* bitwise operation on s.v. possibly nonportabel */
1143 warning(117);
1145 goto assign;
1146 case ANDASS:
1147 case XORASS:
1148 case ORASS:
1149 goto assign;
1150 assign:
1151 if (!ln->tn_lvalue) {
1152 if (ln->tn_op == CVT && ln->tn_cast &&
1153 ln->tn_left->tn_op == LOAD) {
1154 /* a cast to non-ptr does not yield an lvalue */
1155 if (ln->tn_type->t_tspec == PTR)
1156 break;
1157 error(163);
1159 /* %soperand of %s must be lvalue */
1160 error(114, "left ", mp->m_name);
1161 return (0);
1162 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1163 conmemb(ltp))) {
1164 /* %soperand of %s must be modifiable lvalue */
1165 if (!tflag)
1166 warning(115, "left ", mp->m_name);
1168 break;
1169 case COMMA:
1170 if (!modtab[ln->tn_op].m_sideeff)
1171 nulleff(ln);
1172 break;
1173 /* LINTED (enumeration values not handled in switch) */
1174 case CON:
1175 case CASE:
1176 case PUSH:
1177 case LOAD:
1178 case ICALL:
1179 case CVT:
1180 case CALL:
1181 case FSEL:
1182 case STRING:
1183 case NAME:
1184 case LOGOR:
1185 case LOGAND:
1186 case OR:
1187 case XOR:
1188 case AND:
1189 case MOD:
1190 case DIV:
1191 case MULT:
1192 case UMINUS:
1193 case UPLUS:
1194 case DEC:
1195 case INC:
1196 case COMPL:
1197 case NOT:
1198 case NOOP:
1199 case REAL:
1200 case IMAG:
1201 break;
1204 if (mp->m_badeop &&
1205 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1206 chkbeop(op, ln, rn);
1207 } else if (mp->m_enumop && (ltp->t_isenum && rtp && rtp->t_isenum)) {
1208 chkeop2(op, arg, ln, rn);
1209 } else if (mp->m_enumop && (ltp->t_isenum || (rtp && rtp->t_isenum))) {
1210 chkeop1(op, arg, ln, rn);
1213 return (1);
1216 static void
1217 ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn)
1219 type_t *ltp, *rtp;
1220 tspec_t lt, rt;
1221 const char *lts, *rts;
1223 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1224 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1226 if (lt == VOID || rt == VOID) {
1227 if (sflag && (lt == FUNC || rt == FUNC)) {
1228 /* (void *)0 already handled in typeok() */
1229 *(lt == FUNC ? &lts : &rts) = "function pointer";
1230 *(lt == VOID ? &lts : &rts) = "'void *'";
1231 /* ANSI C forbids comparison of %s with %s */
1232 warning(274, lts, rts);
1234 return;
1237 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1238 illptrc(&modtab[op], ltp, rtp);
1239 return;
1242 if (lt == FUNC && rt == FUNC) {
1243 if (sflag && op != EQ && op != NE)
1244 /* ANSI C forbids ordered comp. of func ptr */
1245 warning(125);
1250 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1251 * and prints warnings/errors if necessary.
1252 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1254 static int
1255 asgntypok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1257 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1258 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1259 mod_t *mp;
1260 const char *lts, *rts;
1261 char lbuf[128], rbuf[128];
1263 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1264 lst = (lstp = ltp->t_subt)->t_tspec;
1265 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1266 rst = (rstp = rtp->t_subt)->t_tspec;
1267 mp = &modtab[op];
1269 if (isatyp(lt) && isatyp(rt))
1270 return (1);
1272 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1273 /* both are struct or union */
1274 return (ltp->t_str == rtp->t_str);
1276 /* 0, 0L and (void *)0 may be assigned to any pointer */
1277 if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
1278 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1279 return (1);
1282 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1283 /* two pointers, at least one pointer to void */
1284 if (sflag && (lst == FUNC || rst == FUNC)) {
1285 /* comb. of ptr to func and ptr to void */
1286 *(lst == FUNC ? &lts : &rts) = "function pointer";
1287 *(lst == VOID ? &lts : &rts) = "'void *'";
1288 switch (op) {
1289 case INIT:
1290 case RETURN:
1291 /* ANSI C forbids conversion of %s to %s */
1292 warning(303, rts, lts);
1293 break;
1294 case FARG:
1295 /* ANSI C forbids conv. of %s to %s, arg #%d */
1296 warning(304, rts, lts, arg);
1297 break;
1298 default:
1299 /* ANSI C forbids conv. of %s to %s, op %s */
1300 warning(305, rts, lts, mp->m_name);
1301 break;
1306 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1307 eqtype(lstp, rstp, 1, 0, NULL))) {
1308 /* compatible pointer types (qualifiers ignored) */
1309 if (!tflag &&
1310 ((!lstp->t_const && rstp->t_const) ||
1311 (!lstp->t_volatile && rstp->t_volatile))) {
1312 /* left side has not all qualifiers of right */
1313 tyname(lbuf, sizeof(lbuf), lstp);
1314 tyname(rbuf, sizeof(rbuf), rstp);
1315 switch (op) {
1316 case INIT:
1317 case RETURN:
1318 /* incompatible pointer types */
1319 warning(182, lbuf, rbuf);
1320 break;
1321 case FARG:
1322 /* argument has incompat. ptr. type, arg #%d */
1323 warning(153, arg, lbuf, rbuf);
1324 break;
1325 default:
1326 /* operands have incompat. ptr. types, op %s */
1327 warning(128, mp->m_name, lbuf, rbuf);
1328 break;
1331 return (1);
1334 if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
1335 switch (op) {
1336 case INIT:
1337 case RETURN:
1338 /* illegal combination of pointer and integer */
1339 warning(183);
1340 break;
1341 case FARG:
1342 /* illegal comb. of ptr. and int., arg #%d */
1343 warning(154, arg);
1344 break;
1345 default:
1346 /* illegal comb. of ptr. and int., op %s */
1347 warning(123, mp->m_name);
1348 break;
1350 return (1);
1353 if (lt == PTR && rt == PTR) {
1354 switch (op) {
1355 case INIT:
1356 case RETURN:
1357 illptrc(NULL, ltp, rtp);
1358 break;
1359 case FARG:
1360 /* argument has incompatible pointer type, arg #%d */
1361 warning(153, arg, tyname(lbuf, sizeof(lbuf), ltp),
1362 tyname(rbuf, sizeof(rbuf), rtp));
1363 break;
1364 default:
1365 illptrc(mp, ltp, rtp);
1366 break;
1368 return (1);
1371 switch (op) {
1372 case INIT:
1373 /* initialisation type mismatch */
1374 error(185);
1375 break;
1376 case RETURN:
1377 /* return value type mismatch */
1378 error(211);
1379 break;
1380 case FARG:
1381 /* argument is incompatible with prototype, arg #%d */
1382 warning(155, arg);
1383 break;
1384 default:
1385 incompat(op, lt, rt);
1386 break;
1389 return (0);
1393 * Prints a warning if an operator, which should be senseless for an
1394 * enum type, is applied to an enum type.
1396 static void
1397 chkbeop(op_t op, tnode_t *ln, tnode_t *rn)
1399 mod_t *mp;
1401 if (!eflag)
1402 return;
1404 mp = &modtab[op];
1406 if (!(ln->tn_type->t_isenum ||
1407 (mp->m_binary && rn->tn_type->t_isenum))) {
1408 return;
1412 * Enum as offset to a pointer is an exception (otherwise enums
1413 * could not be used as array indizes).
1415 if (op == PLUS &&
1416 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1417 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1418 return;
1421 /* dubious operation on enum, op %s */
1422 warning(241, mp->m_name);
1427 * Prints a warning if an operator is applied to two different enum types.
1429 static void
1430 chkeop2(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1432 mod_t *mp;
1434 mp = &modtab[op];
1436 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1437 switch (op) {
1438 case INIT:
1439 /* enum type mismatch in initialisation */
1440 warning(210);
1441 break;
1442 case FARG:
1443 /* enum type mismatch, arg #%d */
1444 warning(156, arg);
1445 break;
1446 case RETURN:
1447 /* return value type mismatch */
1448 warning(211);
1449 break;
1450 default:
1451 /* enum type mismatch, op %s */
1452 warning(130, mp->m_name);
1453 break;
1455 } else if (Pflag && mp->m_comp && op != EQ && op != NE) {
1456 if (eflag)
1457 /* dubious comparisons of enums */
1458 warning(243, mp->m_name);
1463 * Prints a warning if an operator has both enum end other integer
1464 * types.
1466 static void
1467 chkeop1(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1469 char lbuf[64], rbuf[64];
1471 if (!eflag)
1472 return;
1474 switch (op) {
1475 case INIT:
1477 * Initializations with 0 should be allowed. Otherwise,
1478 * we should complain about all uninitialized enums,
1479 * consequently.
1481 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1482 isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) {
1483 return;
1485 /* initialisation of '%s' with '%s' */
1486 warning(277, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1487 tyname(rbuf, sizeof(rbuf), rn->tn_type));
1488 break;
1489 case FARG:
1490 /* combination of '%s' and '%s', arg #%d */
1491 warning(278, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1492 tyname(rbuf, sizeof(rbuf), rn->tn_type), arg);
1493 break;
1494 case RETURN:
1495 /* combination of '%s' and '%s' in return */
1496 warning(279, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1497 tyname(rbuf, sizeof(rbuf), rn->tn_type));
1498 break;
1499 default:
1500 /* combination of '%s' and %s, op %s */
1501 warning(242, tyname(lbuf, sizeof(lbuf), ln->tn_type),
1502 tyname(rbuf, sizeof(rbuf), rn->tn_type),
1503 modtab[op].m_name);
1504 break;
1509 * Build and initialize a new node.
1511 static tnode_t *
1512 mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1514 tnode_t *ntn;
1515 tspec_t t;
1517 ntn = getnode();
1519 ntn->tn_op = op;
1520 ntn->tn_type = type;
1521 ntn->tn_left = ln;
1522 ntn->tn_right = rn;
1524 if (op == STAR || op == FSEL) {
1525 if (ln->tn_type->t_tspec == PTR) {
1526 t = ln->tn_type->t_subt->t_tspec;
1527 if (t != FUNC && t != VOID)
1528 ntn->tn_lvalue = 1;
1529 } else {
1530 LERROR("mktnode()");
1534 return (ntn);
1538 * Performs usual conversion of operands to (unsigned) int.
1540 * If tflag is set or the operand is a function argument with no
1541 * type information (no prototype or variable # of args), convert
1542 * float to double.
1544 tnode_t *
1545 promote(op_t op, int farg, tnode_t *tn)
1547 tspec_t t;
1548 type_t *ntp;
1549 u_int len;
1551 t = tn->tn_type->t_tspec;
1553 if (!isatyp(t))
1554 return (tn);
1556 if (!tflag) {
1558 * ANSI C requires that the result is always of type INT
1559 * if INT can represent all possible values of the previous
1560 * type.
1562 if (tn->tn_type->t_isfield) {
1563 len = tn->tn_type->t_flen;
1564 if (size(INT) > len) {
1565 t = INT;
1566 } else {
1567 if (size(INT) != len)
1568 LERROR("promote()");
1569 if (isutyp(t)) {
1570 t = UINT;
1571 } else {
1572 t = INT;
1575 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1576 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1577 INT : UINT;
1578 } else if (t == SHORT || t == USHORT) {
1579 t = (size(SHORT) < size(INT) || t == SHORT) ?
1580 INT : UINT;
1581 } else if (t == ENUM) {
1582 t = INT;
1583 } else if (farg && t == FLOAT) {
1584 t = DOUBLE;
1586 } else {
1588 * In traditional C, keep unsigned and promote FLOAT
1589 * to DOUBLE.
1591 if (t == UCHAR || t == USHORT) {
1592 t = UINT;
1593 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1594 t = INT;
1595 } else if (t == FLOAT) {
1596 t = DOUBLE;
1597 } else if (t == ENUM) {
1598 t = INT;
1602 if (t != tn->tn_type->t_tspec) {
1603 ntp = tduptyp(tn->tn_type);
1604 ntp->t_tspec = t;
1606 * Keep t_isenum so we are later able to check compatibility
1607 * of enum types.
1609 tn = convert(op, 0, ntp, tn);
1612 return (tn);
1616 * Insert conversions which are necessary to give both operands the same
1617 * type. This is done in different ways for traditional C and ANIS C.
1619 static void
1620 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1622 tspec_t lt, rt, t;
1623 int i, u;
1624 type_t *ntp;
1625 static tspec_t tl[] = {
1626 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1629 lt = (*lnp)->tn_type->t_tspec;
1630 rt = (*rnp)->tn_type->t_tspec;
1632 if (!isatyp(lt) || !isatyp(rt))
1633 return;
1635 if (!tflag) {
1636 if (lt == rt) {
1637 t = lt;
1638 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
1639 t = LCOMPLEX;
1640 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
1641 t = DCOMPLEX;
1642 } else if (lt == COMPLEX || rt == COMPLEX) {
1643 t = COMPLEX;
1644 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
1645 t = FCOMPLEX;
1646 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1647 t = LDOUBLE;
1648 } else if (lt == DOUBLE || rt == DOUBLE) {
1649 t = DOUBLE;
1650 } else if (lt == FLOAT || rt == FLOAT) {
1651 t = FLOAT;
1652 } else {
1654 * If type A has more bits than type B it should
1655 * be able to hold all possible values of type B.
1657 if (size(lt) > size(rt)) {
1658 t = lt;
1659 } else if (size(lt) < size(rt)) {
1660 t = rt;
1661 } else {
1662 for (i = 3; tl[i] != INT; i++) {
1663 if (tl[i] == lt || tl[i] == rt)
1664 break;
1666 if ((isutyp(lt) || isutyp(rt)) &&
1667 !isutyp(tl[i])) {
1668 i--;
1670 t = tl[i];
1673 } else {
1674 /* Keep unsigned in traditional C */
1675 u = isutyp(lt) || isutyp(rt);
1676 for (i = 0; tl[i] != INT; i++) {
1677 if (lt == tl[i] || rt == tl[i])
1678 break;
1680 t = tl[i];
1681 if (u && isityp(t) && !isutyp(t))
1682 t = utyp(t);
1685 if (t != lt) {
1686 ntp = tduptyp((*lnp)->tn_type);
1687 ntp->t_tspec = t;
1688 *lnp = convert(op, 0, ntp, *lnp);
1690 if (t != rt) {
1691 ntp = tduptyp((*rnp)->tn_type);
1692 ntp->t_tspec = t;
1693 *rnp = convert(op, 0, ntp, *rnp);
1698 * Insert a conversion operator, which converts the type of the node
1699 * to another given type.
1700 * If op is FARG, arg is the number of the argument (used for warnings).
1702 tnode_t *
1703 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1705 tnode_t *ntn;
1706 tspec_t nt, ot, ost = NOTSPEC;
1708 if (tn->tn_lvalue)
1709 LERROR("convert()");
1711 nt = tp->t_tspec;
1712 if ((ot = tn->tn_type->t_tspec) == PTR)
1713 ost = tn->tn_type->t_subt->t_tspec;
1715 if (!tflag && !sflag && op == FARG)
1716 ptconv(arg, nt, ot, tp, tn);
1717 if (isityp(nt) && isityp(ot)) {
1718 iiconv(op, arg, nt, ot, tp, tn);
1719 } else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
1720 tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1721 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1722 } else if (isityp(nt) && ot == PTR) {
1723 piconv(op, nt, tp, tn);
1724 } else if (nt == PTR && ot == PTR) {
1725 ppconv(op, tn, tp);
1728 ntn = getnode();
1729 ntn->tn_op = CVT;
1730 ntn->tn_type = tp;
1731 ntn->tn_cast = op == CVT;
1732 if (tn->tn_op != CON || nt == VOID) {
1733 ntn->tn_left = tn;
1734 } else {
1735 ntn->tn_op = CON;
1736 ntn->tn_val = tgetblk(sizeof (val_t));
1737 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1740 return (ntn);
1744 * Print a warning if a prototype causes a type conversion that is
1745 * different from what would happen to the same argument in the
1746 * absence of a prototype.
1748 * Errors/Warnings about illegal type combinations are already printed
1749 * in asgntypok().
1751 static void
1752 ptconv(int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1754 tnode_t *ptn;
1755 char buf[64];
1757 if (!isatyp(nt) || !isatyp(ot))
1758 return;
1761 * If the type of the formal parameter is char/short, a warning
1762 * would be useless, because functions declared the old style
1763 * can't expect char/short arguments.
1765 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1766 return;
1768 /* get default promotion */
1769 ptn = promote(NOOP, 1, tn);
1770 ot = ptn->tn_type->t_tspec;
1772 /* return if types are the same with and without prototype */
1773 if (nt == ot || (nt == ENUM && ot == INT))
1774 return;
1776 if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
1777 /* representation and/or width change */
1778 if (!isityp(ot) || psize(ot) > psize(INT)) {
1779 /* conversion to '%s' due to prototype, arg #%d */
1780 warning(259, tyname(buf, sizeof(buf), tp), arg);
1782 } else if (hflag) {
1784 * they differ in sign or base type (char, short, int,
1785 * long, long long, float, double, long double)
1787 * if they differ only in sign and the argument is a constant
1788 * and the msb of the argument is not set, print no warning
1790 if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
1791 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1792 /* ok */
1793 } else {
1794 /* conversion to '%s' due to prototype, arg #%d */
1795 warning(259, tyname(buf, sizeof(buf), tp), arg);
1801 * Print warnings for conversions of integer types which my cause
1802 * problems.
1804 /* ARGSUSED */
1805 static void
1806 iiconv(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
1808 char lbuf[64], rbuf[64], opbuf[16];
1809 if (tn->tn_op == CON)
1810 return;
1812 if (op == CVT)
1813 return;
1815 if (Pflag && psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
1816 /* conversion to %s may sign-extend incorrectly (, arg #%d) */
1817 if (aflag && pflag) {
1818 if (op == FARG) {
1819 warning(297, tyname(lbuf, sizeof(lbuf), tp),
1820 arg);
1821 } else {
1822 warning(131, tyname(lbuf, sizeof(lbuf), tp));
1827 if (Pflag && psize(nt) > psize(ot)) {
1828 switch (tn->tn_op) {
1829 case PLUS:
1830 case MINUS:
1831 case MULT:
1832 case SHL:
1833 warning(324,
1834 tyname(rbuf, sizeof(rbuf), gettyp(ot)),
1835 tyname(lbuf, sizeof(lbuf), tp),
1836 prtnode(opbuf, sizeof(opbuf), tn));
1837 break;
1838 default:
1839 break;
1843 if (psize(nt) < psize(ot) &&
1844 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1845 aflag > 1)) {
1846 /* conversion from '%s' may lose accuracy */
1847 if (aflag) {
1848 if (op == FARG) {
1849 warning(298,
1850 tyname(rbuf, sizeof(rbuf), tn->tn_type),
1851 tyname(lbuf, sizeof(lbuf), tp),
1852 arg);
1853 } else {
1854 warning(132,
1855 tyname(rbuf, sizeof(rbuf), tn->tn_type),
1856 tyname(lbuf, sizeof(lbuf), tp));
1863 * Print warnings for dubious conversions of pointer to integer.
1865 static void
1866 piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
1868 char buf[64];
1870 if (tn->tn_op == CON)
1871 return;
1873 if (op != CVT) {
1874 /* We got already an error. */
1875 return;
1878 if (psize(nt) < psize(PTR)) {
1879 if (pflag && size(nt) >= size(PTR)) {
1880 /* conv. of pointer to %s may lose bits */
1881 warning(134, tyname(buf, sizeof(buf), tp));
1882 } else {
1883 /* conv. of pointer to %s loses bits */
1884 warning(133, tyname(buf, sizeof(buf), tp));
1890 * Print warnings for questionable pointer conversions.
1892 static void
1893 ppconv(op_t op, tnode_t *tn, type_t *tp)
1895 tspec_t nt, ot;
1896 const char *nts, *ots;
1899 * We got already an error (pointers of different types
1900 * without a cast) or we will not get a warning.
1902 if (op != CVT)
1903 return;
1905 nt = tp->t_subt->t_tspec;
1906 ot = tn->tn_type->t_subt->t_tspec;
1908 if (nt == VOID || ot == VOID) {
1909 if (sflag && (nt == FUNC || ot == FUNC)) {
1910 /* (void *)0 already handled in convert() */
1911 *(nt == FUNC ? &nts : &ots) = "function pointer";
1912 *(nt == VOID ? &nts : &ots) = "'void *'";
1913 /* ANSI C forbids conversion of %s to %s */
1914 warning(303, ots, nts);
1916 return;
1917 } else if (nt == FUNC && ot == FUNC) {
1918 return;
1919 } else if (nt == FUNC || ot == FUNC) {
1920 /* questionable conversion of function pointer */
1921 warning(229);
1922 return;
1925 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1926 if (hflag)
1927 /* possible pointer alignment problem */
1928 warning(135);
1930 if (((nt == STRUCT || nt == UNION) &&
1931 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1932 psize(nt) != psize(ot)) {
1933 if (cflag) {
1934 /* pointer casts may be troublesome */
1935 warning(247);
1941 * Converts a typed constant in a constant of another type.
1943 * op operator which requires conversion
1944 * arg if op is FARG, # of argument
1945 * tp type in which to convert the constant
1946 * nv new constant
1947 * v old constant
1949 void
1950 cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
1952 char lbuf[64], rbuf[64];
1953 tspec_t ot, nt;
1954 ldbl_t max = 0.0, min = 0.0;
1955 int sz, rchk;
1956 int64_t xmask, xmsk1;
1957 int osz, nsz;
1959 ot = v->v_tspec;
1960 nt = nv->v_tspec = tp->t_tspec;
1961 rchk = 0;
1963 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1964 switch (nt) {
1965 case BOOL:
1966 max = 1; min = 0; break;
1967 case CHAR:
1968 max = CHAR_MAX; min = CHAR_MIN; break;
1969 case UCHAR:
1970 max = UCHAR_MAX; min = 0; break;
1971 case SCHAR:
1972 max = SCHAR_MAX; min = SCHAR_MIN; break;
1973 case SHORT:
1974 max = SHRT_MAX; min = SHRT_MIN; break;
1975 case USHORT:
1976 max = USHRT_MAX; min = 0; break;
1977 case ENUM:
1978 case INT:
1979 max = INT_MAX; min = INT_MIN; break;
1980 case UINT:
1981 max = (u_int)UINT_MAX; min = 0; break;
1982 case LONG:
1983 max = LONG_MAX; min = LONG_MIN; break;
1984 case ULONG:
1985 max = (u_long)ULONG_MAX; min = 0; break;
1986 case QUAD:
1987 max = QUAD_MAX; min = QUAD_MIN; break;
1988 case UQUAD:
1989 max = (uint64_t)UQUAD_MAX; min = 0; break;
1990 case FLOAT:
1991 case FCOMPLEX:
1992 max = FLT_MAX; min = -FLT_MAX; break;
1993 case DOUBLE:
1994 case DCOMPLEX:
1995 max = DBL_MAX; min = -DBL_MAX; break;
1996 case PTR:
1997 /* Got already an error because of float --> ptr */
1998 case LDOUBLE:
1999 case LCOMPLEX:
2000 max = LDBL_MAX; min = -LDBL_MAX; break;
2001 default:
2002 LERROR("cvtcon()");
2004 if (v->v_ldbl > max || v->v_ldbl < min) {
2005 if (nt == LDOUBLE)
2006 LERROR("cvtcon()");
2007 if (op == FARG) {
2008 /* conv. of %s to %s is out of rng., arg #%d */
2009 warning(295, tyname(lbuf, sizeof(lbuf),
2010 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2011 arg);
2012 } else {
2013 /* conversion of %s to %s is out of range */
2014 warning(119, tyname(lbuf, sizeof(lbuf),
2015 gettyp(ot)),
2016 tyname(rbuf, sizeof(rbuf), tp));
2018 v->v_ldbl = v->v_ldbl > 0 ? max : min;
2020 if (nt == FLOAT) {
2021 nv->v_ldbl = (float)v->v_ldbl;
2022 } else if (nt == DOUBLE) {
2023 nv->v_ldbl = (double)v->v_ldbl;
2024 } else if (nt == LDOUBLE) {
2025 nv->v_ldbl = v->v_ldbl;
2026 } else {
2027 nv->v_quad = (nt == PTR || isutyp(nt)) ?
2028 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl;
2030 } else {
2031 if (nt == FLOAT) {
2032 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
2033 (float)(uint64_t)v->v_quad : (float)v->v_quad;
2034 } else if (nt == DOUBLE) {
2035 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
2036 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2037 } else if (nt == LDOUBLE) {
2038 nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
2039 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2040 } else {
2041 rchk = 1; /* Check for lost precision. */
2042 nv->v_quad = v->v_quad;
2046 if (v->v_ansiu && isftyp(nt)) {
2047 /* ANSI C treats constant as unsigned */
2048 warning(157);
2049 v->v_ansiu = 0;
2050 } else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
2051 psize(nt) > psize(ot))) {
2052 /* ANSI C treats constant as unsigned */
2053 warning(157);
2054 v->v_ansiu = 0;
2057 if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
2058 sz = tp->t_isfield ? tp->t_flen : size(nt);
2059 nv->v_quad = xsign(nv->v_quad, nt, sz);
2062 if (rchk && op != CVT) {
2063 osz = size(ot);
2064 nsz = tp->t_isfield ? tp->t_flen : size(nt);
2065 xmask = qlmasks[nsz] ^ qlmasks[osz];
2066 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2068 * For bitwise operations we are not interested in the
2069 * value, but in the bits itself.
2071 if (op == ORASS || op == OR || op == XOR) {
2073 * Print a warning if bits which were set are
2074 * lost due to the conversion.
2075 * This can happen with operator ORASS only.
2077 if (nsz < osz && (v->v_quad & xmask) != 0) {
2078 /* constant truncated by conv., op %s */
2079 warning(306, modtab[op].m_name);
2081 } else if (op == ANDASS || op == AND) {
2083 * Print a warning if additional bits are not all 1
2084 * and the most significant bit of the old value is 1,
2085 * or if at least one (but not all) removed bit was 0.
2087 if (nsz > osz &&
2088 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2089 (nv->v_quad & xmask) != xmask) {
2091 * extra bits set to 0 in conversion
2092 * of '%s' to '%s', op %s
2094 warning(309, tyname(lbuf, sizeof(lbuf),
2095 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2096 modtab[op].m_name);
2097 } else if (nsz < osz &&
2098 (v->v_quad & xmask) != xmask &&
2099 (v->v_quad & xmask) != 0) {
2100 /* const. truncated by conv., op %s */
2101 warning(306, modtab[op].m_name);
2103 } else if ((nt != PTR && isutyp(nt)) &&
2104 (ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
2105 if (op == ASSIGN) {
2106 /* assignment of negative constant to ... */
2107 warning(164);
2108 } else if (op == INIT) {
2109 /* initialisation of unsigned with neg. ... */
2110 warning(221);
2111 } else if (op == FARG) {
2112 /* conversion of neg. const. to ..., arg #%d */
2113 warning(296, arg);
2114 } else if (modtab[op].m_comp) {
2115 /* we get this warning already in chkcomp() */
2116 } else {
2117 /* conversion of negative constant to ... */
2118 warning(222);
2120 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2121 (v->v_quad & xmask) != 0 &&
2122 (isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
2124 * Loss of significant bit(s). All truncated bits
2125 * of unsigned types or all truncated bits plus the
2126 * msb of the target for signed types are considered
2127 * to be significant bits. Loss of significant bits
2128 * means that at least on of the bits was set in an
2129 * unsigned type or that at least one, but not all of
2130 * the bits was set in an signed type.
2131 * Loss of significant bits means that it is not
2132 * possible, also not with necessary casts, to convert
2133 * back to the original type. A example for a
2134 * necessary cast is:
2135 * char c; int i; c = 128;
2136 * i = c; ** yields -128 **
2137 * i = (unsigned char)c; ** yields 128 **
2139 if (op == ASSIGN && tp->t_isfield) {
2140 /* precision lost in bit-field assignment */
2141 warning(166);
2142 } else if (op == ASSIGN) {
2143 /* constant truncated by assignment */
2144 warning(165);
2145 } else if (op == INIT && tp->t_isfield) {
2146 /* bit-field initializer does not fit */
2147 warning(180);
2148 } else if (op == INIT) {
2149 /* initializer does not fit */
2150 warning(178);
2151 } else if (op == CASE) {
2152 /* case label affected by conversion */
2153 warning(196);
2154 } else if (op == FARG) {
2155 /* conv. of %s to %s is out of rng., arg #%d */
2156 warning(295, tyname(lbuf, sizeof(lbuf),
2157 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2158 arg);
2159 } else {
2160 /* conversion of %s to %s is out of range */
2161 warning(119, tyname(lbuf, sizeof(lbuf),
2162 gettyp(ot)),
2163 tyname(rbuf, sizeof(rbuf), tp));
2165 } else if (nv->v_quad != v->v_quad) {
2166 if (op == ASSIGN && tp->t_isfield) {
2167 /* precision lost in bit-field assignment */
2168 warning(166);
2169 } else if (op == INIT && tp->t_isfield) {
2170 /* bit-field initializer out of range */
2171 warning(11);
2172 } else if (op == CASE) {
2173 /* case label affected by conversion */
2174 warning(196);
2175 } else if (op == FARG) {
2176 /* conv. of %s to %s is out of rng., arg #%d */
2177 warning(295, tyname(lbuf, sizeof(lbuf),
2178 gettyp(ot)), tyname(rbuf, sizeof(rbuf), tp),
2179 arg);
2180 } else {
2181 /* conversion of %s to %s is out of range */
2182 warning(119, tyname(lbuf, sizeof(lbuf),
2183 gettyp(ot)),
2184 tyname(rbuf, sizeof(rbuf), tp));
2191 * Called if incompatible types were detected.
2192 * Prints a appropriate warning.
2194 static void
2195 incompat(op_t op, tspec_t lt, tspec_t rt)
2197 mod_t *mp;
2198 int e = 0;
2200 mp = &modtab[op];
2202 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2203 /* void type illegal in expression */
2204 e = 109;
2205 } else if (op == ASSIGN) {
2206 if ((lt == STRUCT || lt == UNION) &&
2207 (rt == STRUCT || rt == UNION)) {
2208 /* assignment of different structures */
2209 e = 240;
2210 } else {
2211 /* assignment type mismatch */
2212 e = 171;
2214 } else if (mp->m_binary) {
2215 /* operands of %s have incompatible types */
2216 e = 107;
2217 } else {
2218 /* operand of %s has incompatible type */
2219 e = 108;
2221 switch (e) {
2222 case 0:
2223 return;
2224 case 109:
2225 error(e);
2226 return;
2227 case 108:
2228 case 107:
2229 error(e, mp->m_name, basictyname(lt), basictyname(rt));
2230 return;
2231 default:
2232 error(e, basictyname(lt), basictyname(rt));
2233 return;
2238 * Called if incompatible pointer types are detected.
2239 * Print an appropriate warning.
2241 static void
2242 illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
2244 tspec_t lt, rt;
2246 if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
2247 LERROR("illptrc()");
2249 lt = ltp->t_subt->t_tspec;
2250 rt = rtp->t_subt->t_tspec;
2252 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2253 if (mp == NULL) {
2254 /* illegal structure pointer combination */
2255 warning(244);
2256 } else {
2257 /* illegal structure pointer combination, op %s */
2258 warning(245, mp->m_name);
2260 } else {
2261 if (mp == NULL) {
2262 /* illegal pointer combination */
2263 warning(184);
2264 } else {
2265 /* illegal pointer combination, op %s */
2266 warning(124, mp->m_name);
2272 * Make sure type (*tpp)->t_subt has at least the qualifiers
2273 * of tp1->t_subt and tp2->t_subt.
2275 static void
2276 mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
2279 if ((*tpp)->t_tspec != PTR ||
2280 tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
2281 LERROR("mrgqual()");
2284 if ((*tpp)->t_subt->t_const ==
2285 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2286 (*tpp)->t_subt->t_volatile ==
2287 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2288 return;
2291 *tpp = tduptyp(*tpp);
2292 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2293 (*tpp)->t_subt->t_const =
2294 tp1->t_subt->t_const | tp2->t_subt->t_const;
2295 (*tpp)->t_subt->t_volatile =
2296 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2300 * Returns 1 if the given structure or union has a constant member
2301 * (maybe recursively).
2303 static int
2304 conmemb(type_t *tp)
2306 sym_t *m;
2307 tspec_t t;
2309 if ((t = tp->t_tspec) != STRUCT && t != UNION)
2310 LERROR("conmemb()");
2311 for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
2312 tp = m->s_type;
2313 if (tp->t_const)
2314 return (1);
2315 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2316 if (conmemb(m->s_type))
2317 return (1);
2320 return (0);
2324 * Create a new node for one of the operators POINT and ARROW.
2326 static tnode_t *
2327 bldstr(op_t op, tnode_t *ln, tnode_t *rn)
2329 tnode_t *ntn, *ctn;
2330 int nolval;
2332 if (rn->tn_op != NAME)
2333 LERROR("bldstr()");
2334 if (rn->tn_sym->s_value.v_tspec != INT)
2335 LERROR("bldstr()");
2336 if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
2337 LERROR("bldstr()");
2340 * Remember if the left operand is an lvalue (structure members
2341 * are lvalues if and only if the structure itself is an lvalue).
2343 nolval = op == POINT && !ln->tn_lvalue;
2345 if (op == POINT) {
2346 ln = bldamper(ln, 1);
2347 } else if (ln->tn_type->t_tspec != PTR) {
2348 if (!tflag || !isityp(ln->tn_type->t_tspec))
2349 LERROR("bldstr()");
2350 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2353 #if PTRDIFF_IS_LONG
2354 ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2355 #else
2356 ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2357 #endif
2359 ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2360 if (ln->tn_op == CON)
2361 ntn = fold(ntn);
2363 if (rn->tn_type->t_isfield) {
2364 ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2365 } else {
2366 ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2369 if (nolval)
2370 ntn->tn_lvalue = 0;
2372 return (ntn);
2376 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2378 static tnode_t *
2379 bldincdec(op_t op, tnode_t *ln)
2381 tnode_t *cn, *ntn;
2383 if (ln == NULL)
2384 LERROR("bldincdec()");
2386 if (ln->tn_type->t_tspec == PTR) {
2387 cn = plength(ln->tn_type);
2388 } else {
2389 cn = getinode(INT, (int64_t)1);
2391 ntn = mktnode(op, ln->tn_type, ln, cn);
2393 return (ntn);
2397 * Create a node for REAL, IMAG
2399 static tnode_t *
2400 bldri(op_t op, tnode_t *ln)
2402 tnode_t *cn, *ntn;
2403 char buf[64];
2405 if (ln == NULL)
2406 LERROR("bldincdec()");
2408 switch (ln->tn_type->t_tspec) {
2409 case LCOMPLEX:
2410 cn = getinode(LDOUBLE, (int64_t)1);
2411 break;
2412 case DCOMPLEX:
2413 cn = getinode(DOUBLE, (int64_t)1);
2414 break;
2415 case FCOMPLEX:
2416 cn = getinode(FLOAT, (int64_t)1);
2417 break;
2418 default:
2419 error(276, op == REAL ? "real" : "imag",
2420 tyname(buf, sizeof(buf), ln->tn_type));
2421 return NULL;
2423 ntn = mktnode(op, cn->tn_type, ln, cn);
2425 return (ntn);
2428 * Create a tree node for the & operator
2430 static tnode_t *
2431 bldamper(tnode_t *tn, int noign)
2433 tnode_t *ntn;
2434 tspec_t t;
2436 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2437 /* & before array or function: ignored */
2438 if (tflag)
2439 warning(127);
2440 return (tn);
2443 /* eliminate &* */
2444 if (tn->tn_op == STAR &&
2445 tn->tn_left->tn_type->t_tspec == PTR &&
2446 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2447 return (tn->tn_left);
2450 ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2452 return (ntn);
2456 * Create a node for operators PLUS and MINUS.
2458 static tnode_t *
2459 bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
2461 tnode_t *ntn, *ctn;
2462 type_t *tp;
2464 /* If pointer and integer, then pointer to the lhs. */
2465 if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
2466 ntn = ln;
2467 ln = rn;
2468 rn = ntn;
2471 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2473 if (!isityp(rn->tn_type->t_tspec))
2474 LERROR("bldplmi()");
2476 ctn = plength(ln->tn_type);
2477 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2478 rn = convert(NOOP, 0, ctn->tn_type, rn);
2479 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2480 if (rn->tn_left->tn_op == CON)
2481 rn = fold(rn);
2482 ntn = mktnode(op, ln->tn_type, ln, rn);
2484 } else if (rn->tn_type->t_tspec == PTR) {
2486 if (ln->tn_type->t_tspec != PTR || op != MINUS)
2487 LERROR("bldplmi()");
2488 #if PTRDIFF_IS_LONG
2489 tp = gettyp(LONG);
2490 #else
2491 tp = gettyp(INT);
2492 #endif
2493 ntn = mktnode(op, tp, ln, rn);
2494 if (ln->tn_op == CON && rn->tn_op == CON)
2495 ntn = fold(ntn);
2496 ctn = plength(ln->tn_type);
2497 balance(NOOP, &ntn, &ctn);
2498 ntn = mktnode(DIV, tp, ntn, ctn);
2500 } else {
2502 ntn = mktnode(op, ln->tn_type, ln, rn);
2505 return (ntn);
2509 * Create a node for operators SHL and SHR.
2511 static tnode_t *
2512 bldshft(op_t op, tnode_t *ln, tnode_t *rn)
2514 tspec_t t;
2515 tnode_t *ntn;
2517 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2518 rn = convert(CVT, 0, gettyp(INT), rn);
2519 ntn = mktnode(op, ln->tn_type, ln, rn);
2520 return (ntn);
2524 * Create a node for COLON.
2526 static tnode_t *
2527 bldcol(tnode_t *ln, tnode_t *rn)
2529 tspec_t lt, rt, pdt;
2530 type_t *rtp;
2531 tnode_t *ntn;
2533 lt = ln->tn_type->t_tspec;
2534 rt = rn->tn_type->t_tspec;
2535 #if PTRDIFF_IS_LONG
2536 pdt = LONG;
2537 #else
2538 pdt = INT;
2539 #endif
2542 * Arithmetic types are balanced, all other type combinations
2543 * still need to be handled.
2545 if (isatyp(lt) && isatyp(rt)) {
2546 rtp = ln->tn_type;
2547 } else if (lt == VOID || rt == VOID) {
2548 rtp = gettyp(VOID);
2549 } else if (lt == STRUCT || lt == UNION) {
2550 /* Both types must be identical. */
2551 if (rt != STRUCT && rt != UNION)
2552 LERROR("bldcol()");
2553 if (ln->tn_type->t_str != rn->tn_type->t_str)
2554 LERROR("bldcol()");
2555 if (incompl(ln->tn_type)) {
2556 /* unknown operand size, op %s */
2557 error(138, modtab[COLON].m_name);
2558 return (NULL);
2560 rtp = ln->tn_type;
2561 } else if (lt == PTR && isityp(rt)) {
2562 if (rt != pdt) {
2563 rn = convert(NOOP, 0, gettyp(pdt), rn);
2564 rt = pdt;
2566 rtp = ln->tn_type;
2567 } else if (rt == PTR && isityp(lt)) {
2568 if (lt != pdt) {
2569 ln = convert(NOOP, 0, gettyp(pdt), ln);
2570 lt = pdt;
2572 rtp = rn->tn_type;
2573 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2574 if (rt != PTR)
2575 LERROR("bldcol()");
2576 rtp = ln->tn_type;
2577 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2578 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2579 if (lt != PTR)
2580 LERROR("bldcol()");
2581 rtp = rn->tn_type;
2582 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2583 } else {
2584 if (lt != PTR || rt != PTR)
2585 LERROR("bldcol()");
2587 * XXX For now we simply take the left type. This is
2588 * probably wrong, if one type contains a functionprototype
2589 * and the other one, at the same place, only an old style
2590 * declaration.
2592 rtp = ln->tn_type;
2593 mrgqual(&rtp, ln->tn_type, rn->tn_type);
2596 ntn = mktnode(COLON, rtp, ln, rn);
2598 return (ntn);
2602 * Create a node for an assignment operator (both = and op= ).
2604 static tnode_t *
2605 bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
2607 tspec_t lt, rt;
2608 tnode_t *ntn, *ctn;
2610 if (ln == NULL || rn == NULL)
2611 LERROR("bldasgn()");
2613 lt = ln->tn_type->t_tspec;
2614 rt = rn->tn_type->t_tspec;
2616 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2617 if (!isityp(rt))
2618 LERROR("bldasgn()");
2619 ctn = plength(ln->tn_type);
2620 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2621 rn = convert(NOOP, 0, ctn->tn_type, rn);
2622 rn = mktnode(MULT, rn->tn_type, rn, ctn);
2623 if (rn->tn_left->tn_op == CON)
2624 rn = fold(rn);
2627 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2628 if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
2629 LERROR("bldasgn()");
2630 if (incompl(ln->tn_type)) {
2631 if (op == RETURN) {
2632 /* cannot return incomplete type */
2633 error(212);
2634 } else {
2635 /* unknown operand size, op %s */
2636 error(138, modtab[op].m_name);
2638 return (NULL);
2642 if (op == SHLASS) {
2643 if (psize(lt) < psize(rt)) {
2644 if (hflag)
2645 /* semantics of %s change in ANSI C; use ... */
2646 warning(118, "<<=");
2648 } else if (op != SHRASS) {
2649 if (op == ASSIGN || lt != PTR) {
2650 if (lt != rt ||
2651 (ln->tn_type->t_isfield && rn->tn_op == CON)) {
2652 rn = convert(op, 0, ln->tn_type, rn);
2653 rt = lt;
2658 ntn = mktnode(op, ln->tn_type, ln, rn);
2660 return (ntn);
2664 * Get length of type tp->t_subt.
2666 static tnode_t *
2667 plength(type_t *tp)
2669 int elem, elsz;
2670 tspec_t st;
2672 if (tp->t_tspec != PTR)
2673 LERROR("plength()");
2674 tp = tp->t_subt;
2676 elem = 1;
2677 elsz = 0;
2679 while (tp->t_tspec == ARRAY) {
2680 elem *= tp->t_dim;
2681 tp = tp->t_subt;
2684 switch (tp->t_tspec) {
2685 case FUNC:
2686 /* pointer to function is not allowed here */
2687 error(110);
2688 break;
2689 case VOID:
2690 /* cannot do pointer arithmetic on operand of ... */
2691 (void)gnuism(136);
2692 break;
2693 case STRUCT:
2694 case UNION:
2695 if ((elsz = tp->t_str->size) == 0)
2696 /* cannot do pointer arithmetic on operand of ... */
2697 error(136);
2698 break;
2699 case ENUM:
2700 if (incompl(tp)) {
2701 /* cannot do pointer arithmetic on operand of ... */
2702 warning(136);
2704 /* FALLTHROUGH */
2705 default:
2706 if ((elsz = size(tp->t_tspec)) == 0) {
2707 /* cannot do pointer arithmetic on operand of ... */
2708 error(136);
2709 } else if (elsz == -1) {
2710 LERROR("plength()");
2712 break;
2715 if (elem == 0 && elsz != 0) {
2716 /* cannot do pointer arithmetic on operand of ... */
2717 error(136);
2720 if (elsz == 0)
2721 elsz = CHAR_BIT;
2723 #if PTRDIFF_IS_LONG
2724 st = LONG;
2725 #else
2726 st = INT;
2727 #endif
2729 return (getinode(st, (int64_t)(elem * elsz / CHAR_BIT)));
2733 * XXX
2734 * Note: There appear to be a number of bugs in detecting overflow in
2735 * this function. An audit and a set of proper regression tests are needed.
2736 * --Perry Metzger, Nov. 16, 2001
2739 * Do only as much as necessary to compute constant expressions.
2740 * Called only if the operator allows folding and (both) operands
2741 * are constants.
2743 static tnode_t *
2744 fold(tnode_t *tn)
2746 val_t *v;
2747 tspec_t t;
2748 int utyp, ovfl;
2749 int64_t sl, sr = 0, q = 0, mask;
2750 uint64_t ul, ur = 0;
2751 tnode_t *cn;
2753 v = xcalloc(1, sizeof (val_t));
2754 v->v_tspec = t = tn->tn_type->t_tspec;
2756 utyp = t == PTR || isutyp(t);
2757 ul = sl = tn->tn_left->tn_val->v_quad;
2758 if (modtab[tn->tn_op].m_binary)
2759 ur = sr = tn->tn_right->tn_val->v_quad;
2761 mask = qlmasks[size(t)];
2762 ovfl = 0;
2764 switch (tn->tn_op) {
2765 case UPLUS:
2766 q = sl;
2767 break;
2768 case UMINUS:
2769 q = -sl;
2770 if (msb(q, t, -1) == msb(sl, t, -1))
2771 ovfl = 1;
2772 break;
2773 case COMPL:
2774 q = ~sl;
2775 break;
2776 case MULT:
2777 if (utyp) {
2778 q = ul * ur;
2779 if (q != (q & mask))
2780 ovfl = 1;
2781 else if ((ul != 0) && ((q / ul) != ur))
2782 ovfl = 1;
2783 } else {
2784 q = sl * sr;
2785 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2786 ovfl = 1;
2788 break;
2789 case DIV:
2790 if (sr == 0) {
2791 /* division by 0 */
2792 error(139);
2793 q = utyp ? UQUAD_MAX : QUAD_MAX;
2794 } else {
2795 q = utyp ? (int64_t)(ul / ur) : sl / sr;
2797 break;
2798 case MOD:
2799 if (sr == 0) {
2800 /* modulus by 0 */
2801 error(140);
2802 q = 0;
2803 } else {
2804 q = utyp ? (int64_t)(ul % ur) : sl % sr;
2806 break;
2807 case PLUS:
2808 q = utyp ? (int64_t)(ul + ur) : sl + sr;
2809 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2810 if (msb(q, t, -1) == 0)
2811 ovfl = 1;
2812 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2813 if (msb(q, t, -1) != 0)
2814 ovfl = 1;
2816 break;
2817 case MINUS:
2818 q = utyp ? (int64_t)(ul - ur) : sl - sr;
2819 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2820 if (msb(q, t, -1) == 0)
2821 ovfl = 1;
2822 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2823 if (msb(q, t, -1) != 0)
2824 ovfl = 1;
2826 break;
2827 case SHL:
2828 q = utyp ? (int64_t)(ul << sr) : sl << sr;
2829 break;
2830 case SHR:
2832 * The sign must be explicitly extended because
2833 * shifts of signed values are implementation dependent.
2835 q = ul >> sr;
2836 q = xsign(q, t, size(t) - (int)sr);
2837 break;
2838 case LT:
2839 q = utyp ? ul < ur : sl < sr;
2840 break;
2841 case LE:
2842 q = utyp ? ul <= ur : sl <= sr;
2843 break;
2844 case GE:
2845 q = utyp ? ul >= ur : sl >= sr;
2846 break;
2847 case GT:
2848 q = utyp ? ul > ur : sl > sr;
2849 break;
2850 case EQ:
2851 q = utyp ? ul == ur : sl == sr;
2852 break;
2853 case NE:
2854 q = utyp ? ul != ur : sl != sr;
2855 break;
2856 case AND:
2857 q = utyp ? (int64_t)(ul & ur) : sl & sr;
2858 break;
2859 case XOR:
2860 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
2861 break;
2862 case OR:
2863 q = utyp ? (int64_t)(ul | ur) : sl | sr;
2864 break;
2865 default:
2866 LERROR("fold()");
2869 /* XXX does not work for quads. */
2870 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 &&
2871 (q & ~mask) != 0)) {
2872 if (hflag)
2873 /* integer overflow detected, op %s */
2874 warning(141, modtab[tn->tn_op].m_name);
2877 v->v_quad = xsign(q, t, -1);
2879 cn = getcnode(tn->tn_type, v);
2881 return (cn);
2885 * Same for operators whose operands are compared with 0 (test context).
2887 static tnode_t *
2888 foldtst(tnode_t *tn)
2890 int l, r = 0;
2891 val_t *v;
2893 v = xcalloc(1, sizeof (val_t));
2894 v->v_tspec = tn->tn_type->t_tspec;
2895 if (tn->tn_type->t_tspec != INT)
2896 LERROR("foldtst()");
2898 if (isftyp(tn->tn_left->tn_type->t_tspec)) {
2899 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2900 } else {
2901 l = tn->tn_left->tn_val->v_quad != 0;
2904 if (modtab[tn->tn_op].m_binary) {
2905 if (isftyp(tn->tn_right->tn_type->t_tspec)) {
2906 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2907 } else {
2908 r = tn->tn_right->tn_val->v_quad != 0;
2912 switch (tn->tn_op) {
2913 case NOT:
2914 if (hflag)
2915 /* constant argument to NOT */
2916 warning(239);
2917 v->v_quad = !l;
2918 break;
2919 case LOGAND:
2920 v->v_quad = l && r;
2921 break;
2922 case LOGOR:
2923 v->v_quad = l || r;
2924 break;
2925 default:
2926 LERROR("foldtst()");
2929 return (getcnode(tn->tn_type, v));
2933 * Same for operands with floating point type.
2935 static tnode_t *
2936 foldflt(tnode_t *tn)
2938 val_t *v;
2939 tspec_t t;
2940 ldbl_t l, r = 0;
2942 fpe = 0;
2943 v = xcalloc(1, sizeof (val_t));
2944 v->v_tspec = t = tn->tn_type->t_tspec;
2946 if (!isftyp(t))
2947 LERROR("foldflt()");
2949 if (t != tn->tn_left->tn_type->t_tspec)
2950 LERROR("foldflt()");
2951 if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
2952 LERROR("foldflt()");
2954 l = tn->tn_left->tn_val->v_ldbl;
2955 if (modtab[tn->tn_op].m_binary)
2956 r = tn->tn_right->tn_val->v_ldbl;
2958 switch (tn->tn_op) {
2959 case UPLUS:
2960 v->v_ldbl = l;
2961 break;
2962 case UMINUS:
2963 v->v_ldbl = -l;
2964 break;
2965 case MULT:
2966 v->v_ldbl = l * r;
2967 break;
2968 case DIV:
2969 if (r == 0.0) {
2970 /* division by 0 */
2971 error(139);
2972 if (t == FLOAT) {
2973 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2974 } else if (t == DOUBLE) {
2975 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2976 } else {
2977 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2979 } else {
2980 v->v_ldbl = l / r;
2982 break;
2983 case PLUS:
2984 v->v_ldbl = l + r;
2985 break;
2986 case MINUS:
2987 v->v_ldbl = l - r;
2988 break;
2989 case LT:
2990 v->v_quad = l < r;
2991 break;
2992 case LE:
2993 v->v_quad = l <= r;
2994 break;
2995 case GE:
2996 v->v_quad = l >= r;
2997 break;
2998 case GT:
2999 v->v_quad = l > r;
3000 break;
3001 case EQ:
3002 v->v_quad = l == r;
3003 break;
3004 case NE:
3005 v->v_quad = l != r;
3006 break;
3007 default:
3008 LERROR("foldflt()");
3011 if (!fpe && isnan((double)v->v_ldbl))
3012 LERROR("foldflt()");
3013 if (fpe || !finite((double)v->v_ldbl) ||
3014 (t == FLOAT &&
3015 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
3016 (t == DOUBLE &&
3017 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3018 /* floating point overflow detected, op %s */
3019 warning(142, modtab[tn->tn_op].m_name);
3020 if (t == FLOAT) {
3021 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3022 } else if (t == DOUBLE) {
3023 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3024 } else {
3025 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3027 fpe = 0;
3030 return (getcnode(tn->tn_type, v));
3035 * Create a constant node for sizeof.
3037 tnode_t *
3038 bldszof(type_t *tp)
3040 tspec_t st;
3041 #if SIZEOF_IS_ULONG
3042 st = ULONG;
3043 #else
3044 st = UINT;
3045 #endif
3046 return getinode(st, tsize(tp) / CHAR_BIT);
3049 int64_t
3050 tsize(type_t *tp)
3052 int elem, elsz;
3054 elem = 1;
3055 while (tp->t_tspec == ARRAY) {
3056 elem *= tp->t_dim;
3057 tp = tp->t_subt;
3059 if (elem == 0) {
3060 /* cannot take size of incomplete type */
3061 error(143);
3062 elem = 1;
3064 switch (tp->t_tspec) {
3065 case FUNC:
3066 /* cannot take size of function */
3067 error(144);
3068 elsz = 1;
3069 break;
3070 case STRUCT:
3071 case UNION:
3072 if (incompl(tp)) {
3073 /* cannot take size of incomplete type */
3074 error(143);
3075 elsz = 1;
3076 } else {
3077 elsz = tp->t_str->size;
3079 break;
3080 case ENUM:
3081 if (incompl(tp)) {
3082 /* cannot take size of incomplete type */
3083 warning(143);
3085 /* FALLTHROUGH */
3086 default:
3087 if (tp->t_isfield) {
3088 /* cannot take size of bit-field */
3089 error(145);
3091 if (tp->t_tspec == VOID) {
3092 /* cannot take size of void */
3093 error(146);
3094 elsz = 1;
3095 } else {
3096 elsz = size(tp->t_tspec);
3097 if (elsz <= 0)
3098 LERROR("bldszof()");
3100 break;
3103 return (int64_t)(elem * elsz);
3108 tnode_t *
3109 bldalof(type_t *tp)
3111 tspec_t st;
3113 switch (tp->t_tspec) {
3114 case ARRAY:
3115 break;
3117 case FUNC:
3118 /* cannot take align of function */
3119 error(144);
3120 return 0;
3122 case STRUCT:
3123 case UNION:
3124 if (incompl(tp)) {
3125 /* cannot take align of incomplete type */
3126 error(143);
3127 return 0;
3129 break;
3130 case ENUM:
3131 break;
3132 default:
3133 if (tp->t_isfield) {
3134 /* cannot take align of bit-field */
3135 error(145);
3136 return 0;
3138 if (tp->t_tspec == VOID) {
3139 /* cannot take alignsize of void */
3140 error(146);
3141 return 0;
3143 break;
3146 #if SIZEOF_IS_ULONG
3147 st = ULONG;
3148 #else
3149 st = UINT;
3150 #endif
3152 return getinode(st, (int64_t)getbound(tp));
3156 * Type casts.
3158 tnode_t *
3159 cast(tnode_t *tn, type_t *tp)
3161 tspec_t nt, ot;
3163 if (tn == NULL)
3164 return (NULL);
3166 tn = cconv(tn);
3168 nt = tp->t_tspec;
3169 ot = tn->tn_type->t_tspec;
3171 if (nt == VOID) {
3173 * XXX ANSI C requires scalar types or void (Plauger&Brodie).
3174 * But this seams really questionable.
3176 } else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
3177 /* invalid cast expression */
3178 error(147);
3179 return (NULL);
3180 } else if (ot == STRUCT || ot == UNION) {
3181 /* invalid cast expression */
3182 error(147);
3183 return (NULL);
3184 } else if (ot == VOID) {
3185 /* improper cast of void expression */
3186 error(148);
3187 return (NULL);
3188 } else if (isityp(nt) && issclt(ot)) {
3189 /* ok */
3190 } else if (isftyp(nt) && isatyp(ot)) {
3191 /* ok */
3192 } else if (nt == PTR && isityp(ot)) {
3193 /* ok */
3194 } else if (nt == PTR && ot == PTR) {
3195 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3196 if (hflag)
3197 /* cast discards 'const' from ... */
3198 warning(275);
3200 } else {
3201 /* invalid cast expression */
3202 error(147);
3203 return (NULL);
3206 tn = convert(CVT, 0, tp, tn);
3207 tn->tn_cast = 1;
3209 return (tn);
3213 * Create the node for a function argument.
3214 * All necessary conversions and type checks are done in funccall(), because
3215 * in funcarg() we have no information about expected argument types.
3217 tnode_t *
3218 funcarg(tnode_t *args, tnode_t *arg)
3220 tnode_t *ntn;
3223 * If there was a serious error in the expression for the argument,
3224 * create a dummy argument so the positions of the remaining arguments
3225 * will not change.
3227 if (arg == NULL)
3228 arg = getinode(INT, (int64_t)0);
3230 ntn = mktnode(PUSH, arg->tn_type, arg, args);
3232 return (ntn);
3236 * Create the node for a function call. Also check types of
3237 * function arguments and insert conversions, if necessary.
3239 tnode_t *
3240 funccall(tnode_t *func, tnode_t *args)
3242 tnode_t *ntn;
3243 op_t fcop;
3245 if (func == NULL)
3246 return (NULL);
3248 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3249 fcop = CALL;
3250 } else {
3251 fcop = ICALL;
3255 * after cconv() func will always be a pointer to a function
3256 * if it is a valid function designator.
3258 func = cconv(func);
3260 if (func->tn_type->t_tspec != PTR ||
3261 func->tn_type->t_subt->t_tspec != FUNC) {
3262 /* illegal function */
3263 error(149);
3264 return (NULL);
3267 args = chkfarg(func->tn_type->t_subt, args);
3269 ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3271 return (ntn);
3275 * Check types of all function arguments and insert conversions,
3276 * if necessary.
3278 static tnode_t *
3279 chkfarg(type_t *ftp, tnode_t *args)
3281 tnode_t *arg;
3282 sym_t *asym;
3283 tspec_t at;
3284 int narg, npar, n, i;
3286 /* get # of args in the prototype */
3287 npar = 0;
3288 for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
3289 npar++;
3291 /* get # of args in function call */
3292 narg = 0;
3293 for (arg = args; arg != NULL; arg = arg->tn_right)
3294 narg++;
3296 asym = ftp->t_args;
3297 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3298 /* argument mismatch: %d arg%s passed, %d expected */
3299 error(150, narg, narg > 1 ? "s" : "", npar);
3300 asym = NULL;
3303 for (n = 1; n <= narg; n++) {
3306 * The rightmost argument is at the top of the argument
3307 * subtree.
3309 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3310 continue;
3312 /* some things which are always not allowd */
3313 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3314 /* void expressions may not be arguments, arg #%d */
3315 error(151, n);
3316 return (NULL);
3317 } else if ((at == STRUCT || at == UNION) &&
3318 incompl(arg->tn_left->tn_type)) {
3319 /* argument cannot have unknown size, arg #%d */
3320 error(152, n);
3321 return (NULL);
3322 } else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
3323 incompl(arg->tn_left->tn_type)) {
3324 /* argument cannot have unknown size, arg #%d */
3325 warning(152, n);
3328 /* class conversions (arg in value context) */
3329 arg->tn_left = cconv(arg->tn_left);
3331 if (asym != NULL) {
3332 arg->tn_left = parg(n, asym->s_type, arg->tn_left);
3333 } else {
3334 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3336 arg->tn_type = arg->tn_left->tn_type;
3338 if (asym != NULL)
3339 asym = asym->s_nxt;
3342 return (args);
3346 * Compare the type of an argument with the corresponding type of a
3347 * prototype parameter. If it is a valid combination, but both types
3348 * are not the same, insert a conversion to convert the argument into
3349 * the type of the parameter.
3351 static tnode_t *
3352 parg( int n, /* pos of arg */
3353 type_t *tp, /* expected type (from prototype) */
3354 tnode_t *tn) /* argument */
3356 tnode_t *ln;
3357 int dowarn;
3359 ln = xcalloc(1, sizeof (tnode_t));
3360 ln->tn_type = tduptyp(tp);
3361 ln->tn_type->t_const = 0;
3362 ln->tn_lvalue = 1;
3363 if (typeok(FARG, n, ln, tn)) {
3364 if (!eqtype(tp, tn->tn_type, 1, 0, (dowarn = 0, &dowarn)) || dowarn)
3365 tn = convert(FARG, n, tp, tn);
3367 free(ln);
3368 return (tn);
3372 * Return the value of an integral constant expression.
3373 * If the expression is not constant or its type is not an integer
3374 * type, an error message is printed.
3376 val_t *
3377 constant(tnode_t *tn, int required)
3379 val_t *v;
3381 if (tn != NULL)
3382 tn = cconv(tn);
3383 if (tn != NULL)
3384 tn = promote(NOOP, 0, tn);
3386 v = xcalloc(1, sizeof (val_t));
3388 if (tn == NULL) {
3389 if (nerr == 0)
3390 LERROR("constant()");
3391 v->v_tspec = INT;
3392 v->v_quad = 1;
3393 return (v);
3396 v->v_tspec = tn->tn_type->t_tspec;
3398 if (tn->tn_op == CON) {
3399 if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
3400 LERROR("constant()");
3401 if (isityp(tn->tn_val->v_tspec)) {
3402 v->v_ansiu = tn->tn_val->v_ansiu;
3403 v->v_quad = tn->tn_val->v_quad;
3404 return (v);
3406 v->v_quad = tn->tn_val->v_ldbl;
3407 } else {
3408 v->v_quad = 1;
3411 /* integral constant expression expected */
3412 if (required)
3413 error(55);
3414 else
3415 c99ism(318);
3417 if (!isityp(v->v_tspec))
3418 v->v_tspec = INT;
3420 return (v);
3424 * Perform some tests on expressions which can't be done in build() and
3425 * functions called by build(). These tests must be done here because
3426 * we need some information about the context in which the operations
3427 * are performed.
3428 * After all tests are performed, expr() frees the memory which is used
3429 * for the expression.
3431 void
3432 expr(tnode_t *tn, int vctx, int tctx, int dofreeblk)
3435 if (tn == NULL && nerr == 0)
3436 LERROR("expr()");
3438 if (tn == NULL) {
3439 tfreeblk();
3440 return;
3443 /* expr() is also called in global initialisations */
3444 if (dcs->d_ctx != EXTERN)
3445 chkreach();
3447 chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
3448 if (tn->tn_op == ASSIGN) {
3449 if (hflag && tctx)
3450 /* assignment in conditional context */
3451 warning(159);
3452 } else if (tn->tn_op == CON) {
3453 if (hflag && tctx && !ccflg)
3454 /* constant in conditional context */
3455 warning(161);
3457 if (!modtab[tn->tn_op].m_sideeff) {
3459 * for left operands of COMMA this warning is already
3460 * printed
3462 if (tn->tn_op != COMMA && !vctx && !tctx)
3463 nulleff(tn);
3465 if (dflag)
3466 displexpr(tn, 0);
3468 /* free the tree memory */
3469 if (dofreeblk)
3470 tfreeblk();
3473 static void
3474 nulleff(tnode_t *tn)
3477 if (!hflag)
3478 return;
3480 while (!modtab[tn->tn_op].m_sideeff) {
3481 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3482 tn = tn->tn_left;
3483 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3485 * && and || have a side effect if the right operand
3486 * has a side effect.
3488 tn = tn->tn_right;
3489 } else if (tn->tn_op == QUEST) {
3491 * ? has a side effect if at least one of its right
3492 * operands has a side effect
3494 tn = tn->tn_right;
3495 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3497 * : has a side effect if at least one of its operands
3498 * has a side effect
3500 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3501 tn = tn->tn_left;
3502 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3503 tn = tn->tn_right;
3504 } else {
3505 break;
3507 } else {
3508 break;
3511 if (!modtab[tn->tn_op].m_sideeff)
3512 /* expression has null effect */
3513 warning(129);
3517 * Dump an expression to stdout
3518 * only used for debugging
3520 static void
3521 displexpr(tnode_t *tn, int offs)
3523 uint64_t uq;
3525 if (tn == NULL) {
3526 (void)printf("%*s%s\n", offs, "", "NULL");
3527 return;
3529 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3531 if (tn->tn_op == NAME) {
3532 (void)printf("%s: %s ",
3533 tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
3534 } else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
3535 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3536 } else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
3537 uq = tn->tn_val->v_quad;
3538 (void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
3539 (long)uq & 0xffffffffl);
3540 } else if (tn->tn_op == CON) {
3541 if (tn->tn_type->t_tspec != PTR)
3542 LERROR("displexpr()");
3543 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3544 (u_long)tn->tn_val->v_quad);
3545 } else if (tn->tn_op == STRING) {
3546 if (tn->tn_strg->st_tspec == CHAR) {
3547 (void)printf("\"%s\"", tn->tn_strg->st_cp);
3548 } else {
3549 char *s;
3550 size_t n;
3551 n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
3552 s = xmalloc(n);
3553 (void)wcstombs(s, tn->tn_strg->st_wcp, n);
3554 (void)printf("L\"%s\"", s);
3555 free(s);
3557 (void)printf(" ");
3558 } else if (tn->tn_op == FSEL) {
3559 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3560 tn->tn_type->t_flen);
3562 (void)printf("%s\n", ttos(tn->tn_type));
3563 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3564 return;
3565 displexpr(tn->tn_left, offs + 2);
3566 if (modtab[tn->tn_op].m_binary ||
3567 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3568 displexpr(tn->tn_right, offs + 2);
3573 * Called by expr() to recursively perform some tests.
3575 /* ARGSUSED */
3576 void
3577 chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3578 int szof)
3580 tnode_t *ln, *rn;
3581 mod_t *mp;
3582 int nrvdisc, cvctx, ctctx;
3583 op_t op;
3584 scl_t sc;
3585 dinfo_t *di;
3587 if (tn == NULL)
3588 return;
3590 ln = tn->tn_left;
3591 rn = tn->tn_right;
3592 mp = &modtab[op = tn->tn_op];
3594 switch (op) {
3595 case AMPER:
3596 if (ln->tn_op == NAME && (reached || rchflg)) {
3597 if (!szof)
3598 setsflg(ln->tn_sym);
3599 setuflg(ln->tn_sym, fcall, szof);
3601 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3602 /* check the range of array indices */
3603 chkaidx(ln->tn_left, 1);
3604 break;
3605 case LOAD:
3606 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3607 /* check the range of array indices */
3608 chkaidx(ln->tn_left, 0);
3609 /* FALLTHROUGH */
3610 case PUSH:
3611 case INCBEF:
3612 case DECBEF:
3613 case INCAFT:
3614 case DECAFT:
3615 case ADDASS:
3616 case SUBASS:
3617 case MULASS:
3618 case DIVASS:
3619 case MODASS:
3620 case ANDASS:
3621 case ORASS:
3622 case XORASS:
3623 case SHLASS:
3624 case SHRASS:
3625 case REAL:
3626 case IMAG:
3627 if (ln->tn_op == NAME && (reached || rchflg)) {
3628 sc = ln->tn_sym->s_scl;
3630 * Look if there was a asm statement in one of the
3631 * compound statements we are in. If not, we don't
3632 * print a warning.
3634 for (di = dcs; di != NULL; di = di->d_nxt) {
3635 if (di->d_asm)
3636 break;
3638 if (sc != EXTERN && sc != STATIC &&
3639 !ln->tn_sym->s_set && !szof && di == NULL) {
3640 /* %s may be used before set */
3641 warning(158, ln->tn_sym->s_name);
3642 setsflg(ln->tn_sym);
3644 setuflg(ln->tn_sym, 0, 0);
3646 break;
3647 case ASSIGN:
3648 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3649 setsflg(ln->tn_sym);
3650 if (ln->tn_sym->s_scl == EXTERN)
3651 outusg(ln->tn_sym);
3653 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3654 /* check the range of array indices */
3655 chkaidx(ln->tn_left, 0);
3656 break;
3657 case CALL:
3658 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3659 LERROR("chkmisc()");
3660 if (!szof)
3661 outcall(tn, vctx || tctx, rvdisc);
3662 break;
3663 case EQ:
3664 /* equality operator "==" found where "=" was exp. */
3665 if (hflag && eqwarn)
3666 warning(160);
3667 break;
3668 case CON:
3669 case NAME:
3670 case STRING:
3671 return;
3672 /* LINTED (enumeration values not handled in switch) */
3673 case OR:
3674 case XOR:
3675 case NE:
3676 case GE:
3677 case GT:
3678 case LE:
3679 case LT:
3680 case SHR:
3681 case SHL:
3682 case MINUS:
3683 case PLUS:
3684 case MOD:
3685 case DIV:
3686 case MULT:
3687 case STAR:
3688 case UMINUS:
3689 case UPLUS:
3690 case DEC:
3691 case INC:
3692 case COMPL:
3693 case NOT:
3694 case POINT:
3695 case ARROW:
3696 case NOOP:
3697 case AND:
3698 case FARG:
3699 case CASE:
3700 case INIT:
3701 case RETURN:
3702 case ICALL:
3703 case CVT:
3704 case COMMA:
3705 case FSEL:
3706 case COLON:
3707 case QUEST:
3708 case LOGOR:
3709 case LOGAND:
3710 break;
3713 cvctx = mp->m_vctx;
3714 ctctx = mp->m_tctx;
3716 * values of operands of ':' are not used if the type of at least
3717 * one of the operands (for gcc compatibility) is void
3718 * XXX test/value context of QUEST should probably be used as
3719 * context for both operands of COLON
3721 if (op == COLON && tn->tn_type->t_tspec == VOID)
3722 cvctx = ctctx = 0;
3723 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3724 chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3726 switch (op) {
3727 case PUSH:
3728 if (rn != NULL)
3729 chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3730 break;
3731 case LOGAND:
3732 case LOGOR:
3733 chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3734 break;
3735 case COLON:
3736 chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3737 break;
3738 case COMMA:
3739 chkmisc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3740 break;
3741 default:
3742 if (mp->m_binary)
3743 chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3744 break;
3750 * Checks the range of array indices, if possible.
3751 * amper is set if only the address of the element is used. This
3752 * means that the index is allowd to refere to the first element
3753 * after the array.
3755 static void
3756 chkaidx(tnode_t *tn, int amper)
3758 int dim;
3759 tnode_t *ln, *rn;
3760 int elsz;
3761 int64_t con;
3763 ln = tn->tn_left;
3764 rn = tn->tn_right;
3766 /* We can only check constant indices. */
3767 if (rn->tn_op != CON)
3768 return;
3770 /* Return if the left node does not stem from an array. */
3771 if (ln->tn_op != AMPER)
3772 return;
3773 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3774 return;
3775 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3776 return;
3779 * For incomplete array types, we can print a warning only if
3780 * the index is negative.
3782 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3783 return;
3785 /* Get the size of one array element */
3786 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3787 return;
3788 elsz /= CHAR_BIT;
3790 /* Change the unit of the index from bytes to element size. */
3791 if (isutyp(rn->tn_type->t_tspec)) {
3792 con = (uint64_t)rn->tn_val->v_quad / elsz;
3793 } else {
3794 con = rn->tn_val->v_quad / elsz;
3797 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3799 if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
3800 /* array subscript cannot be negative: %ld */
3801 warning(167, (long)con);
3802 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
3803 /* array subscript cannot be > %d: %ld */
3804 warning(168, dim - 1, (long)con);
3809 * Check for ordered comparisons of unsigned values with 0.
3811 static void
3812 chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
3814 char buf[64];
3815 tspec_t lt, rt;
3816 mod_t *mp;
3818 lt = ln->tn_type->t_tspec;
3819 rt = rn->tn_type->t_tspec;
3820 mp = &modtab[op];
3822 if (ln->tn_op != CON && rn->tn_op != CON)
3823 return;
3825 if (!isityp(lt) || !isityp(rt))
3826 return;
3828 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3829 (rn->tn_val->v_quad < 0 ||
3830 rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3831 /* nonportable character comparison, op %s */
3832 warning(230, mp->m_name);
3833 return;
3835 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3836 (ln->tn_val->v_quad < 0 ||
3837 ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
3838 /* nonportable character comparison, op %s */
3839 warning(230, mp->m_name);
3840 return;
3842 if (isutyp(lt) && !isutyp(rt) &&
3843 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3844 if (rn->tn_val->v_quad < 0) {
3845 /* comparison of %s with %s, op %s */
3846 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3847 "negative constant", mp->m_name);
3848 } else if (op == LT || op == GE || (hflag && op == LE)) {
3849 /* comparison of %s with %s, op %s */
3850 warning(162, tyname(buf, sizeof(buf), ln->tn_type),
3851 "0", mp->m_name);
3853 return;
3855 if (isutyp(rt) && !isutyp(lt) &&
3856 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3857 if (ln->tn_val->v_quad < 0) {
3858 /* comparison of %s with %s, op %s */
3859 warning(162, "negative constant",
3860 tyname(buf, sizeof(buf), rn->tn_type), mp->m_name);
3861 } else if (op == GT || op == LE || (hflag && op == GE)) {
3862 /* comparison of %s with %s, op %s */
3863 warning(162, "0", tyname(buf, sizeof(buf), rn->tn_type),
3864 mp->m_name);
3866 return;
3871 * Takes an expression an returns 0 if this expression can be used
3872 * for static initialisation, otherwise -1.
3874 * Constant initialisation expressions must be constant or an address
3875 * of a static object with an optional offset. In the first case,
3876 * the result is returned in *offsp. In the second case, the static
3877 * object is returned in *symp and the offset in *offsp.
3879 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3880 * CON. Type conversions are allowed if they do not change binary
3881 * representation (including width).
3884 conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3886 sym_t *sym;
3887 ptrdiff_t offs1, offs2;
3888 tspec_t t, ot;
3890 switch (tn->tn_op) {
3891 case MINUS:
3892 if (tn->tn_right->tn_op == CVT)
3893 return conaddr(tn->tn_right, symp, offsp);
3894 else if (tn->tn_right->tn_op != CON)
3895 return (-1);
3896 /* FALLTHROUGH */
3897 case PLUS:
3898 offs1 = offs2 = 0;
3899 if (tn->tn_left->tn_op == CON) {
3900 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3901 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3902 return (-1);
3903 } else if (tn->tn_right->tn_op == CON) {
3904 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3905 if (tn->tn_op == MINUS)
3906 offs2 = -offs2;
3907 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3908 return (-1);
3909 } else {
3910 return (-1);
3912 *symp = sym;
3913 *offsp = offs1 + offs2;
3914 break;
3915 case AMPER:
3916 if (tn->tn_left->tn_op == NAME) {
3917 *symp = tn->tn_left->tn_sym;
3918 *offsp = 0;
3919 } else if (tn->tn_left->tn_op == STRING) {
3921 * If this would be the front end of a compiler we
3922 * would return a label instead of 0.
3924 *offsp = 0;
3926 break;
3927 case CVT:
3928 t = tn->tn_type->t_tspec;
3929 ot = tn->tn_left->tn_type->t_tspec;
3930 if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR))
3931 return (-1);
3932 #ifdef notdef
3934 * consider:
3935 * struct foo {
3936 * unsigned char a;
3937 * } f = {
3938 * (u_char)(u_long)(&(((struct foo *)0)->a))
3939 * };
3940 * since psize(u_long) != psize(u_char) this fails.
3942 else if (psize(t) != psize(ot))
3943 return (-1);
3944 #endif
3945 if (conaddr(tn->tn_left, symp, offsp) == -1)
3946 return (-1);
3947 break;
3948 default:
3949 return (-1);
3951 return (0);
3955 * Concatenate two string constants.
3957 strg_t *
3958 catstrg(strg_t *strg1, strg_t *strg2)
3960 size_t len1, len2, len;
3962 if (strg1->st_tspec != strg2->st_tspec) {
3963 /* cannot concatenate wide and regular string literals */
3964 error(292);
3965 return (strg1);
3968 len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
3970 if (strg1->st_tspec == CHAR) {
3971 strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
3972 (void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
3973 free(strg2->st_cp);
3974 } else {
3975 strg1->st_wcp = xrealloc(strg1->st_wcp,
3976 (len + 1) * sizeof (wchar_t));
3977 (void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
3978 (len2 + 1) * sizeof (wchar_t));
3979 free(strg2->st_wcp);
3981 free(strg2);
3983 return (strg1);
3987 * Print a warning if the given node has operands which should be
3988 * parenthesized.
3990 * XXX Does not work if an operand is a constant expression. Constant
3991 * expressions are already folded.
3993 static void
3994 precconf(tnode_t *tn)
3996 tnode_t *ln, *rn;
3997 op_t lop, rop = NOOP;
3998 int lparn, rparn = 0;
3999 mod_t *mp;
4000 int dowarn;
4002 if (!hflag)
4003 return;
4005 mp = &modtab[tn->tn_op];
4007 lparn = 0;
4008 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4009 lparn |= ln->tn_parn;
4010 lparn |= ln->tn_parn;
4011 lop = ln->tn_op;
4013 if (mp->m_binary) {
4014 rparn = 0;
4015 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
4016 rparn |= rn->tn_parn;
4017 rparn |= rn->tn_parn;
4018 rop = rn->tn_op;
4021 dowarn = 0;
4023 switch (tn->tn_op) {
4024 case SHL:
4025 case SHR:
4026 if (!lparn && (lop == PLUS || lop == MINUS)) {
4027 dowarn = 1;
4028 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
4029 dowarn = 1;
4031 break;
4032 case LOGOR:
4033 if (!lparn && lop == LOGAND) {
4034 dowarn = 1;
4035 } else if (!rparn && rop == LOGAND) {
4036 dowarn = 1;
4038 break;
4039 case AND:
4040 case XOR:
4041 case OR:
4042 if (!lparn && lop != tn->tn_op) {
4043 if (lop == PLUS || lop == MINUS) {
4044 dowarn = 1;
4045 } else if (lop == AND || lop == XOR) {
4046 dowarn = 1;
4049 if (!dowarn && !rparn && rop != tn->tn_op) {
4050 if (rop == PLUS || rop == MINUS) {
4051 dowarn = 1;
4052 } else if (rop == AND || rop == XOR) {
4053 dowarn = 1;
4056 break;
4057 /* LINTED (enumeration values not handled in switch) */
4058 case DECAFT:
4059 case XORASS:
4060 case SHLASS:
4061 case NOOP:
4062 case ARROW:
4063 case ORASS:
4064 case POINT:
4065 case NAME:
4066 case NOT:
4067 case COMPL:
4068 case CON:
4069 case INC:
4070 case STRING:
4071 case DEC:
4072 case INCBEF:
4073 case DECBEF:
4074 case INCAFT:
4075 case FSEL:
4076 case CALL:
4077 case COMMA:
4078 case CVT:
4079 case ICALL:
4080 case LOAD:
4081 case PUSH:
4082 case RETURN:
4083 case INIT:
4084 case CASE:
4085 case FARG:
4086 case SUBASS:
4087 case ADDASS:
4088 case MODASS:
4089 case DIVASS:
4090 case MULASS:
4091 case ASSIGN:
4092 case COLON:
4093 case QUEST:
4094 case LOGAND:
4095 case NE:
4096 case EQ:
4097 case GE:
4098 case GT:
4099 case LE:
4100 case LT:
4101 case MINUS:
4102 case PLUS:
4103 case MOD:
4104 case DIV:
4105 case MULT:
4106 case AMPER:
4107 case STAR:
4108 case UMINUS:
4109 case SHRASS:
4110 case UPLUS:
4111 case ANDASS:
4112 case REAL:
4113 case IMAG:
4114 break;
4117 if (dowarn) {
4118 /* precedence confusion possible: parenthesize! */
4119 warning(169);