2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 /* $Id: ch3.c,v 1.30 1994/06/27 07:58:29 ceriel Exp $ */
6 /* S E M A N T I C A N A L Y S I S -- C H A P T E R 3.3 */
10 #include "nobitfield.h"
12 #include <flt_arith.h>
22 #include "file_info.h"
24 extern char options[];
25 extern char *symbol2str();
26 extern struct type *qualifier_type();
28 /* Most expression-handling routines have a pointer to a
29 (struct type *) as first parameter. The object under the pointer
30 gets updated in the process.
33 ch3sel(expp, oper, idf)
37 /* The selector idf is applied to *expp; oper may be '.' or
40 register struct expr *exp;
41 register struct type *tp;
42 register struct sdef *sd;
48 if (tp->tp_fund == POINTER &&
49 ( tp->tp_up->tp_fund == STRUCT ||
50 tp->tp_up->tp_fund == UNION)) /* normal case */
52 else { /* constructions like "12->selector" and
55 switch (tp->tp_fund) {
60 /* An error is given in idf2sdef() */
61 ch3cast(expp, CAST, pa_type);
62 sd = idf2sdef(idf, tp);
66 expr_error(exp, "-> applied to %s",
67 symbol2str(tp->tp_fund));
69 exp->ex_type = error_type;
73 } else { /* oper == '.' */
77 switch (tp->tp_fund) {
78 case POINTER: /* for int *p; p->next = ... */
84 /* warning will be given by idf2sdef() */
87 if (!is_anon_idf(idf))
88 expr_error(exp, "selector %s applied to %s",
89 idf->id_text, symbol2str(tp->tp_fund));
91 exp->ex_type = error_type;
94 sd = idf2sdef(idf, tp);
96 /* there are 3 cases in which the selection can be
97 performed compile-time:
98 I: n.sel (n either an identifier or a constant)
99 II: (e.s1).s2 (transformed into (e.(s1+s2)))
100 III: (e->s1).s2 (transformed into (e->(s1+s2)))
101 The code performing these conversions is
104 if (exp->ex_class == Value) {
105 /* It is an object we know the address of; so
106 we can calculate the address of the
109 exp->VL_VALUE += sd->sd_offset;
110 exp->ex_type = sd->sd_type;
111 exp->ex_lvalue = exp->ex_type->tp_fund != ARRAY;
112 if (exp->ex_type == error_type) {
113 exp->ex_flags |= EX_ERROR;
117 if (exp->ex_class == Oper) {
118 struct oper *op = &(exp->ex_object.ex_oper);
120 if (op->op_oper == '.' || op->op_oper == ARROW) {
121 ASSERT(is_cp_cst(op->op_right));
122 op->op_right->VL_VALUE += sd->sd_offset;
123 exp->ex_type = sd->sd_type;
124 exp->ex_lvalue = exp->ex_type->tp_fund != ARRAY;
125 if (exp->ex_type == error_type) {
126 exp->ex_flags |= EX_ERROR;
130 exp = new_oper(sd->sd_type, exp, '.',
131 intexpr(sd->sd_offset, INT));
132 exp->ex_lvalue = sd->sd_type->tp_fund != ARRAY;
133 if (!exp->OP_LEFT->ex_lvalue)
134 exp->ex_flags |= EX_ILVALUE;
138 else { /* oper == ARROW */
139 if (is_ld_cst(exp)) {
140 exp->VL_VALUE += sd->sd_offset;
141 exp->ex_type = sd->sd_type;
144 exp = new_oper(sd->sd_type,
145 exp, oper, intexpr(sd->sd_offset, INT));
147 exp->ex_lvalue = (sd->sd_type->tp_fund != ARRAY);
148 exp->ex_flags &= ~EX_ILVALUE;
150 if ((sd->sd_type->tp_typequal & TQ_CONST)
151 || (tp->tp_typequal & TQ_CONST))
152 exp->ex_flags |= EX_READONLY;
153 if ((sd->sd_type->tp_typequal & TQ_VOLATILE)
154 || (tp->tp_typequal & TQ_VOLATILE))
155 exp->ex_flags |= EX_VOLATILE;
156 if (oper == '.' && exp->ex_flags & EX_READONLY) {
157 exp->ex_type = qualifier_type(exp->ex_type, TQ_CONST);
159 if (exp->ex_flags & EX_VOLATILE) {
160 exp->ex_type = qualifier_type(exp->ex_type, TQ_VOLATILE);
168 /* The monadic prefix/postfix incr/decr operator oper is
171 ch3asgn(expp, oper, intexpr((arith)1, INT));
174 ch3cast(expp, oper, tp)
175 register struct expr **expp;
176 register struct type *tp;
178 /* The expression *expp is cast to type tp; the cast is
179 caused by the operator oper. If the cast has
180 to be passed on to run time, its left operand will be an
181 expression of class Type.
183 register struct type *oldtp;
184 register struct expr *exp = *expp;
185 int qual_lev, ascompat = 0;
187 if (oper == RETURN && tp->tp_fund == VOID) {
188 expr_strict(exp, "return <expression> in function returning void");
189 exp->ex_type = void_type;
192 if (exp->ex_type->tp_fund == FUNCTION) {
193 function2pointer(exp);
195 if (exp->ex_type->tp_fund == ARRAY)
197 if (exp->ex_class == String)
199 oldtp = exp->ex_type;
201 if (oldtp->tp_size <= 0 && oldtp->tp_fund != VOID) {
202 expr_error(exp,"incomplete type in expression");
206 if (oldtp->tp_fund == FIELD) {
208 ch3cast(expp, oper, tp);
211 if (tp->tp_fund == FIELD) {
212 ch3cast(expp, oper, tp->tp_up);
215 #endif /* NOBITFIELD */
217 default: qual_lev = -1; break;
218 case CAST: qual_lev = -999; break; /* ??? hack */
221 case RETURN: ascompat = 1; /* assignment compatibility */
235 if (equal_type(tp, oldtp, qual_lev, 0)) {
237 if (ascompat && tp->tp_fund == POINTER) {
238 if ((tp->tp_up->tp_typequal & oldtp->tp_up->tp_typequal)
239 != oldtp->tp_up->tp_typequal) {
240 expr_strict( exp, "qualifier error");
243 exp->ex_type = tp; /* so qualifiers are allright */
246 if (tp->tp_fund == VOID) {
248 exp->ex_type = void_type;
251 if (is_arith_type(oldtp) && is_arith_type(tp)) {
252 int oldi = is_integral_type(oldtp);
253 int i = is_integral_type(tp);
296 float2float(expp, tp);
299 float2float(expp, tp);
304 if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
312 if (tp->tp_up && oldtp->tp_up) {
313 if (tp->tp_up->tp_fund == VOID
314 && oldtp->tp_up->tp_fund != FUNCTION) {
317 if (oldtp->tp_up->tp_fund == VOID
318 && tp->tp_up->tp_fund != FUNCTION) {
321 if (oldtp->tp_up->tp_fund == VOID
323 && exp->VL_VALUE == (arith)0)
329 expr_strict(exp, "incompatible pointers in call");
331 expr_strict(exp, "incompatible pointers in %s",
338 lint_ptr_conv(oldtp->tp_up->tp_fund, tp->tp_up->tp_fund);
340 exp->ex_type = tp; /* free conversion */
343 if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
344 /* from pointer to integral */
347 "illegal conversion of pointer to %s",
348 symbol2str(tp->tp_fund));
349 if (oldtp->tp_size > tp->tp_size)
351 "conversion of pointer to %s loses accuracy",
352 symbol2str(tp->tp_fund));
353 if (oldtp->tp_size != tp->tp_size) {
359 if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
360 /* from integral to pointer */
369 if (is_cp_cst(exp) && exp->VL_VALUE == (arith)0)
373 "illegal conversion of %s to pointer",
374 symbol2str(oldtp->tp_fund));
377 if (oldtp->tp_size > tp->tp_size)
379 "conversion of %s to pointer loses accuracy",
380 symbol2str(oldtp->tp_fund));
381 if (oldtp->tp_size != tp->tp_size) {
387 if (oldtp->tp_fund == ERRONEOUS) {
388 /* we just won't look */
389 exp->ex_type = tp; /* brute force */
392 if (oldtp->tp_size == tp->tp_size && oper == CAST) {
393 expr_strict(exp, "dubious conversion based on equal size");
394 exp->ex_type = tp; /* brute force */
397 if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
398 expr_error(exp, "cannot convert %s to %s",
399 symbol2str(oldtp->tp_fund),
400 symbol2str(tp->tp_fund)
402 exp->ex_type = tp; /* brute force */
404 /* re-initialize exp, since *expp may have changed */
407 exp->ex_flags |= EX_ILVALUE;
411 /* Determine whether two types are equal.
413 equal_type(tp, otp, qual_lev, diag)
414 register struct type *tp, *otp;
421 || (tp->tp_fund != otp->tp_fund)
422 || (tp->tp_unsigned != otp->tp_unsigned)
423 || (tp->tp_align != otp->tp_align))
425 if (tp->tp_size != otp->tp_size) {
426 if (tp->tp_fund != ARRAY
427 || (tp->tp_size != -1 && otp->tp_size != -1))
431 if (qual_lev >= 0 && tp->tp_typequal != otp->tp_typequal) {
432 strict("missing or illegal qualifiers");
435 switch (tp->tp_fund) {
438 /* If both types have parameter type lists, the type of
439 each parameter in the composite parameter type list
440 is the composite type of the corresponding paramaters.
442 if (tp->tp_proto && otp->tp_proto) {
443 if (!equal_proto(tp->tp_proto, otp->tp_proto, diag))
445 } else if (tp->tp_proto || otp->tp_proto) {
446 if (!legal_mixture(tp, otp, diag))
449 return equal_type(tp->tp_up, otp->tp_up, qual_lev + 1, diag);
452 /* If one type is an array of known size, the composite
453 type is an array of that size
455 if (tp->tp_size != otp->tp_size &&
456 (tp->tp_size != -1 && otp->tp_size != -1))
458 return equal_type(tp->tp_up, otp->tp_up, qual_lev/* ??? +1 */, diag);
461 return equal_type(tp->tp_up, otp->tp_up, qual_lev + 1, diag);
464 return equal_type(tp->tp_up, otp->tp_up, qual_lev/* ??? +1 */, diag);
469 return tp->tp_idf == otp->tp_idf && tp->tp_sdef == otp->tp_sdef;
476 check_pseudoproto(pl, opl, diag)
477 register struct proto *pl, *opl;
481 if (pl->pl_flag & PL_ELLIPSIS) {
483 error("illegal ellipsis terminator");
484 pl->pl_flag |= PL_ERRGIVEN;
485 opl->pl_flag |= PL_ERRGIVEN;
489 if (opl->pl_flag & PL_VOID) {
490 if (!(pl->pl_flag & PL_VOID)) {
492 strict("function is defined without parameters");
499 if (!equal_type(pl->pl_type, opl->pl_type, -1, diag)) {
501 if (!(pl->pl_flag & PL_ERRGIVEN)
502 && !(opl->pl_flag & PL_ERRGIVEN))
503 error("incorrect type for parameter %s of definition",
504 opl->pl_idf->id_text);
505 pl->pl_flag |= PL_ERRGIVEN;
506 opl->pl_flag |= PL_ERRGIVEN;
514 if (diag) error("incorrect number of parameters");
520 legal_mixture(tp, otp, diag)
521 struct type *tp, *otp;
524 struct proto *pl = tp->tp_proto, *opl = otp->tp_proto;
526 register struct proto *prot;
529 ASSERT( (pl != 0) ^ (opl != 0));
535 if (!opl && otp->tp_pseudoproto) {
536 return check_pseudoproto(tp->tp_proto, otp->tp_pseudoproto, diag);
539 if (prot->pl_flag & PL_ELLIPSIS) {
540 if (prot->pl_flag & PL_ERRGIVEN) {
542 error("illegal ellipsis terminator");
543 else error("ellipsis terminator in previous (prototype) declaration");
544 prot->pl_flag |= PL_ERRGIVEN;
549 /* if (!(prot->pl_flag & PL_ELLIPSIS)) {} */
550 fund = prot->pl_type->tp_fund;
551 if (fund == CHAR || fund == SHORT || fund == FLOAT) {
552 if (diag && !(prot->pl_flag & PL_ERRGIVEN))
553 error("illegal %s parameter in %sdeclaration",
554 symbol2str(fund), (opl ? "previous (prototype) " : "" ));
555 prot->pl_flag |= PL_ERRGIVEN;
563 equal_proto(pl, opl, diag)
564 register struct proto *pl, *opl;
570 /* If only one type is a function type with a parameter type list
571 (a function prototype), the composite type is a function
572 prototype with parameter type list.
576 if ((pl->pl_flag & ~PL_ERRGIVEN) != (opl->pl_flag & ~PL_ERRGIVEN) ||
577 !equal_type(pl->pl_type, opl->pl_type, -1, diag))
586 /* check if a type has a consqualified member */
591 register struct sdef *sdf;
595 if (tp->tp_typequal & qual) return 1;
596 switch(tp->tp_fund) {
602 if (recurqual(sdf->sd_type, qual))
611 ch3asgn(expp, oper, expr)
615 /* The assignment operators.
616 "f op= e" should be interpreted as
617 "f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
618 and not as "f = f op (typeof f)e".
619 Consider, for example, (i == 10) i *= 0.9; (i == 9), where
621 The resulting expression tree becomes:
626 EVAL should however take care of evaluating (typeof (f op e))f
628 register struct expr *exp = *expp;
629 int fund = exp->ex_type->tp_fund;
631 char *oper_string = symbol2str(oper);
633 /* We expect an lvalue */
634 if (fund == ARRAY || fund == FUNCTION) exp->ex_lvalue = 0;
635 if (!exp->ex_lvalue) {
636 expr_error(exp, "no lvalue in operand of %s", oper_string);
637 } else if (exp->ex_flags & EX_ILVALUE) {
638 expr_strict(exp, "incorrect lvalue in operand of %s", oper_string);
639 } else if (exp->ex_flags & EX_READONLY) {
640 expr_error(exp, "operand of %s is read-only", oper_string);
641 } else if (fund == STRUCT || fund == UNION) {
642 if (recurqual(exp->ex_type, TQ_CONST))
643 expr_error(exp,"operand of %s contains a const-qualified member",
648 ch3cast(&expr, oper, exp->ex_type);
651 else { /* turn e into e' where typeof(e') = typeof (f op e) */
652 struct expr *extmp = intexpr((arith)0, INT);
654 /* this is really $#@&*%$# ! */
655 /* if you correct this, please correct lint_new_oper() too */
656 extmp->ex_lvalue = 1;
657 extmp->ex_type = exp->ex_type;
658 ch3bin(&extmp, oper, expr);
659 /* Note that ch3bin creates a tree of the expression
660 ((typeof (f op e))f op (typeof (f op e))e),
661 where f ~ extmp and e ~ expr.
662 We want to use (typeof (f op e))e.
663 Ch3bin does not create a tree if both operands
664 were illegal or constants!
666 tp = extmp->ex_type; /* perform the arithmetic in type tp */
667 if (extmp->ex_class == Oper) {
668 expr = extmp->OP_RIGHT;
669 extmp->OP_RIGHT = NILEXPR;
670 free_expression(extmp);
676 exp = new_oper(fund == FIELD ? exp->ex_type->tp_up : exp->ex_type,
678 #else /* NOBITFIELD */
679 exp = new_oper(exp->ex_type, exp, oper, expr);
680 #endif /* NOBITFIELD */
681 exp->OP_TYPE = tp; /* for EVAL() */
682 exp->ex_flags |= EX_SIDEEFFECTS;
686 /* Some interesting (?) questions answered.
690 register struct type *tp;
692 switch (tp->tp_fund) {
701 return is_integral_type(tp->tp_up);
702 #endif /* NOBITFIELD */
710 register struct type *tp;
712 switch (tp->tp_fund) {
724 return is_arith_type(tp->tp_up);
725 #endif /* NOBITFIELD */