1989-09-25 14:28:10 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1994-06-27 08:03:14 +00:00
|
|
|
/* $Id$ */
|
1989-09-25 14:28:10 +00:00
|
|
|
/* 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 */
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
#include <assert.h>
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "parameters.h"
|
1989-09-25 14:28:10 +00:00
|
|
|
#include <flt_arith.h>
|
|
|
|
#include "arith.h"
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "idf.h"
|
1989-09-25 14:28:10 +00:00
|
|
|
#include "proto.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "struct.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "def.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "file_info.h"
|
|
|
|
|
|
|
|
extern char options[];
|
|
|
|
extern char *symbol2str();
|
|
|
|
extern struct type *qualifier_type();
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void ch3cast();
|
|
|
|
|
1989-09-25 14:28:10 +00:00
|
|
|
/* Most expression-handling routines have a pointer to a
|
|
|
|
(struct type *) as first parameter. The object under the pointer
|
|
|
|
gets updated in the process.
|
|
|
|
*/
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3sel(expp, oper, idf)
|
|
|
|
struct expr **expp;
|
|
|
|
struct idf *idf;
|
|
|
|
{
|
|
|
|
/* The selector idf is applied to *expp; oper may be '.' or
|
|
|
|
ARROW.
|
|
|
|
*/
|
|
|
|
register struct expr *exp;
|
|
|
|
register struct type *tp;
|
|
|
|
register struct sdef *sd;
|
|
|
|
|
|
|
|
any2opnd(expp, oper);
|
|
|
|
exp = *expp;
|
|
|
|
tp = exp->ex_type;
|
|
|
|
if (oper == ARROW) {
|
|
|
|
if (tp->tp_fund == POINTER &&
|
|
|
|
( tp->tp_up->tp_fund == STRUCT ||
|
|
|
|
tp->tp_up->tp_fund == UNION)) /* normal case */
|
|
|
|
tp = tp->tp_up;
|
|
|
|
else { /* constructions like "12->selector" and
|
|
|
|
"char c; c->selector"
|
|
|
|
*/
|
|
|
|
switch (tp->tp_fund) {
|
|
|
|
case POINTER:
|
|
|
|
break;
|
|
|
|
case INT:
|
|
|
|
case LONG:
|
|
|
|
/* An error is given in idf2sdef() */
|
|
|
|
ch3cast(expp, CAST, pa_type);
|
|
|
|
sd = idf2sdef(idf, tp);
|
|
|
|
tp = sd->sd_stype;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
expr_error(exp, "-> applied to %s",
|
|
|
|
symbol2str(tp->tp_fund));
|
|
|
|
case ERRONEOUS:
|
|
|
|
exp->ex_type = error_type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { /* oper == '.' */
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
exp = *expp;
|
|
|
|
switch (tp->tp_fund) {
|
|
|
|
case POINTER: /* for int *p; p->next = ... */
|
|
|
|
case STRUCT:
|
|
|
|
case UNION:
|
|
|
|
break;
|
|
|
|
case INT:
|
|
|
|
case LONG:
|
|
|
|
/* warning will be given by idf2sdef() */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (!is_anon_idf(idf))
|
|
|
|
expr_error(exp, "selector %s applied to %s",
|
|
|
|
idf->id_text, symbol2str(tp->tp_fund));
|
|
|
|
case ERRONEOUS:
|
|
|
|
exp->ex_type = error_type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sd = idf2sdef(idf, tp);
|
|
|
|
if (oper == '.') {
|
|
|
|
/* there are 3 cases in which the selection can be
|
|
|
|
performed compile-time:
|
|
|
|
I: n.sel (n either an identifier or a constant)
|
|
|
|
II: (e.s1).s2 (transformed into (e.(s1+s2)))
|
|
|
|
III: (e->s1).s2 (transformed into (e->(s1+s2)))
|
|
|
|
The code performing these conversions is
|
|
|
|
extremely obscure.
|
|
|
|
*/
|
|
|
|
if (exp->ex_class == Value) {
|
|
|
|
/* It is an object we know the address of; so
|
|
|
|
we can calculate the address of the
|
|
|
|
selected member
|
|
|
|
*/
|
|
|
|
exp->VL_VALUE += sd->sd_offset;
|
|
|
|
exp->ex_type = sd->sd_type;
|
|
|
|
exp->ex_lvalue = exp->ex_type->tp_fund != ARRAY;
|
|
|
|
if (exp->ex_type == error_type) {
|
|
|
|
exp->ex_flags |= EX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (exp->ex_class == Oper) {
|
|
|
|
struct oper *op = &(exp->ex_object.ex_oper);
|
|
|
|
|
|
|
|
if (op->op_oper == '.' || op->op_oper == ARROW) {
|
2017-11-10 03:22:13 +00:00
|
|
|
assert(is_cp_cst(op->op_right));
|
1989-09-25 14:28:10 +00:00
|
|
|
op->op_right->VL_VALUE += sd->sd_offset;
|
|
|
|
exp->ex_type = sd->sd_type;
|
|
|
|
exp->ex_lvalue = exp->ex_type->tp_fund != ARRAY;
|
|
|
|
if (exp->ex_type == error_type) {
|
|
|
|
exp->ex_flags |= EX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exp = new_oper(sd->sd_type, exp, '.',
|
|
|
|
intexpr(sd->sd_offset, INT));
|
|
|
|
exp->ex_lvalue = sd->sd_type->tp_fund != ARRAY;
|
|
|
|
if (!exp->OP_LEFT->ex_lvalue)
|
|
|
|
exp->ex_flags |= EX_ILVALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { /* oper == ARROW */
|
1990-03-29 10:41:46 +00:00
|
|
|
if (is_ld_cst(exp)) {
|
|
|
|
exp->VL_VALUE += sd->sd_offset;
|
|
|
|
exp->ex_type = sd->sd_type;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exp = new_oper(sd->sd_type,
|
|
|
|
exp, oper, intexpr(sd->sd_offset, INT));
|
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
exp->ex_lvalue = (sd->sd_type->tp_fund != ARRAY);
|
|
|
|
exp->ex_flags &= ~EX_ILVALUE;
|
|
|
|
}
|
1991-06-13 15:56:14 +00:00
|
|
|
if ((sd->sd_type->tp_typequal & TQ_CONST)
|
|
|
|
|| (tp->tp_typequal & TQ_CONST))
|
1989-09-25 14:28:10 +00:00
|
|
|
exp->ex_flags |= EX_READONLY;
|
1991-06-13 15:56:14 +00:00
|
|
|
if ((sd->sd_type->tp_typequal & TQ_VOLATILE)
|
|
|
|
|| (tp->tp_typequal & TQ_VOLATILE))
|
1989-09-25 14:28:10 +00:00
|
|
|
exp->ex_flags |= EX_VOLATILE;
|
|
|
|
if (oper == '.' && exp->ex_flags & EX_READONLY) {
|
|
|
|
exp->ex_type = qualifier_type(exp->ex_type, TQ_CONST);
|
|
|
|
}
|
1990-04-02 15:57:51 +00:00
|
|
|
if (exp->ex_flags & EX_VOLATILE) {
|
|
|
|
exp->ex_type = qualifier_type(exp->ex_type, TQ_VOLATILE);
|
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
*expp = exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch3incr(expp, oper)
|
|
|
|
struct expr **expp;
|
|
|
|
{
|
|
|
|
/* The monadic prefix/postfix incr/decr operator oper is
|
|
|
|
applied to *expp.
|
|
|
|
*/
|
|
|
|
ch3asgn(expp, oper, intexpr((arith)1, INT));
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3cast(expp, oper, tp)
|
|
|
|
register struct expr **expp;
|
|
|
|
register struct type *tp;
|
|
|
|
{
|
|
|
|
/* The expression *expp is cast to type tp; the cast is
|
|
|
|
caused by the operator oper. If the cast has
|
|
|
|
to be passed on to run time, its left operand will be an
|
|
|
|
expression of class Type.
|
|
|
|
*/
|
|
|
|
register struct type *oldtp;
|
1990-10-23 14:13:10 +00:00
|
|
|
register struct expr *exp = *expp;
|
1989-11-27 11:37:11 +00:00
|
|
|
int qual_lev, ascompat = 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
if (oper == RETURN && tp->tp_fund == VOID) {
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_strict(exp, "return <expression> in function returning void");
|
|
|
|
exp->ex_type = void_type;
|
1989-09-25 14:28:10 +00:00
|
|
|
return;
|
|
|
|
}
|
1990-10-23 14:13:10 +00:00
|
|
|
if (exp->ex_type->tp_fund == FUNCTION) {
|
|
|
|
function2pointer(exp);
|
1989-12-12 12:52:03 +00:00
|
|
|
}
|
1990-10-23 14:13:10 +00:00
|
|
|
if (exp->ex_type->tp_fund == ARRAY)
|
|
|
|
array2pointer(exp);
|
|
|
|
if (exp->ex_class == String)
|
|
|
|
string2pointer(exp);
|
|
|
|
oldtp = exp->ex_type;
|
1989-09-25 14:28:10 +00:00
|
|
|
|
1990-10-23 14:13:10 +00:00
|
|
|
if (oldtp->tp_size <= 0 && oldtp->tp_fund != VOID) {
|
|
|
|
expr_error(exp,"incomplete type in expression");
|
1990-10-19 11:21:43 +00:00
|
|
|
}
|
|
|
|
|
1989-09-25 14:28:10 +00:00
|
|
|
#ifndef NOBITFIELD
|
|
|
|
if (oldtp->tp_fund == FIELD) {
|
|
|
|
field2arith(expp);
|
|
|
|
ch3cast(expp, oper, tp);
|
1989-11-17 11:28:38 +00:00
|
|
|
return;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
if (tp->tp_fund == FIELD) {
|
|
|
|
ch3cast(expp, oper, tp->tp_up);
|
1989-11-17 11:28:38 +00:00
|
|
|
return;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* NOBITFIELD */
|
1989-11-27 11:37:11 +00:00
|
|
|
switch (oper) {
|
|
|
|
default: qual_lev = -1; break;
|
|
|
|
case CAST: qual_lev = -999; break; /* ??? hack */
|
|
|
|
case CASTAB:
|
|
|
|
case '=':
|
|
|
|
case RETURN: ascompat = 1; /* assignment compatibility */
|
|
|
|
/* fallthrough */
|
|
|
|
case '-':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case LESSEQ:
|
|
|
|
case GREATEREQ:
|
|
|
|
case EQUAL:
|
|
|
|
case NOTEQUAL:
|
|
|
|
case ':':
|
|
|
|
qual_lev = -2;
|
|
|
|
break;
|
|
|
|
}
|
1989-11-13 14:01:50 +00:00
|
|
|
|
1991-03-26 09:45:20 +00:00
|
|
|
if (equal_type(tp, oldtp, qual_lev, 0)) {
|
1989-09-25 14:28:10 +00:00
|
|
|
/* life is easy */
|
1989-11-27 11:37:11 +00:00
|
|
|
if (ascompat && tp->tp_fund == POINTER) {
|
1989-11-13 14:01:50 +00:00
|
|
|
if ((tp->tp_up->tp_typequal & oldtp->tp_up->tp_typequal)
|
|
|
|
!= oldtp->tp_up->tp_typequal) {
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_strict( exp, "qualifier error");
|
1989-11-13 14:01:50 +00:00
|
|
|
}
|
|
|
|
}
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp; /* so qualifiers are allright */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (tp->tp_fund == VOID) {
|
|
|
|
/* easy again */
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = void_type;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (is_arith_type(oldtp) && is_arith_type(tp)) {
|
|
|
|
int oldi = is_integral_type(oldtp);
|
|
|
|
int i = is_integral_type(tp);
|
|
|
|
|
|
|
|
if (oldi && i) {
|
|
|
|
#ifdef LINT
|
|
|
|
if (oper == CAST)
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp;
|
|
|
|
else {
|
1989-09-25 14:28:10 +00:00
|
|
|
int2int(expp, tp);
|
1990-10-23 14:13:10 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#else /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
int2int(expp, tp);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (oldi && !i) {
|
|
|
|
#ifdef LINT
|
|
|
|
if (oper == CAST)
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp;
|
|
|
|
else {
|
1989-09-25 14:28:10 +00:00
|
|
|
int2float(expp, tp);
|
1990-10-23 14:13:10 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#else /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
int2float(expp, tp);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (!oldi && i) {
|
|
|
|
#ifdef LINT
|
|
|
|
if (oper == CAST)
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp;
|
|
|
|
else {
|
1989-09-25 14:28:10 +00:00
|
|
|
float2int(expp, tp);
|
1990-10-23 14:13:10 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#else /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
float2int(expp, tp);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* !oldi && !i */
|
|
|
|
#ifdef LINT
|
|
|
|
if (oper == CAST)
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp;
|
|
|
|
else {
|
1989-09-25 14:28:10 +00:00
|
|
|
float2float(expp, tp);
|
1990-10-23 14:13:10 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#else /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
float2float(expp, tp);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (oldtp->tp_fund == POINTER && tp->tp_fund == POINTER) {
|
1989-11-22 13:58:36 +00:00
|
|
|
switch (oper) {
|
|
|
|
case EQUAL:
|
|
|
|
case NOTEQUAL:
|
|
|
|
case '=':
|
|
|
|
case CASTAB:
|
|
|
|
case RETURN:
|
1989-12-12 12:52:03 +00:00
|
|
|
case ':':
|
1989-11-22 13:58:36 +00:00
|
|
|
if (tp->tp_up && oldtp->tp_up) {
|
|
|
|
if (tp->tp_up->tp_fund == VOID
|
|
|
|
&& oldtp->tp_up->tp_fund != FUNCTION) {
|
|
|
|
break; /* switch */
|
|
|
|
}
|
|
|
|
if (oldtp->tp_up->tp_fund == VOID
|
|
|
|
&& tp->tp_up->tp_fund != FUNCTION) {
|
|
|
|
break; /* switch */
|
|
|
|
}
|
1989-12-12 12:52:03 +00:00
|
|
|
if (oldtp->tp_up->tp_fund == VOID
|
1990-10-23 14:13:10 +00:00
|
|
|
&& is_cp_cst(exp)
|
|
|
|
&& exp->VL_VALUE == (arith)0)
|
1989-12-12 12:52:03 +00:00
|
|
|
break; /* switch */
|
1989-11-22 13:58:36 +00:00
|
|
|
}
|
|
|
|
/* falltrough */
|
|
|
|
default:
|
|
|
|
if (oper == CASTAB)
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_strict(exp, "incompatible pointers in call");
|
1989-11-22 13:58:36 +00:00
|
|
|
else
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_strict(exp, "incompatible pointers in %s",
|
1989-09-25 14:28:10 +00:00
|
|
|
symbol2str(oper));
|
1989-11-22 13:58:36 +00:00
|
|
|
break;
|
|
|
|
case CAST: break;
|
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
#ifdef LINT
|
|
|
|
if (oper != CAST)
|
|
|
|
lint_ptr_conv(oldtp->tp_up->tp_fund, tp->tp_up->tp_fund);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp; /* free conversion */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (oldtp->tp_fund == POINTER && is_integral_type(tp)) {
|
|
|
|
/* from pointer to integral */
|
|
|
|
if (oper != CAST)
|
1990-11-02 09:23:27 +00:00
|
|
|
expr_strict(exp,
|
1989-09-25 14:28:10 +00:00
|
|
|
"illegal conversion of pointer to %s",
|
|
|
|
symbol2str(tp->tp_fund));
|
|
|
|
if (oldtp->tp_size > tp->tp_size)
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_warning(exp,
|
1989-09-25 14:28:10 +00:00
|
|
|
"conversion of pointer to %s loses accuracy",
|
|
|
|
symbol2str(tp->tp_fund));
|
1990-10-23 14:13:10 +00:00
|
|
|
if (oldtp->tp_size != tp->tp_size) {
|
1989-09-25 14:28:10 +00:00
|
|
|
int2int(expp, tp);
|
1990-10-23 14:13:10 +00:00
|
|
|
} else
|
|
|
|
exp->ex_type = tp;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (tp->tp_fund == POINTER && is_integral_type(oldtp)) {
|
|
|
|
/* from integral to pointer */
|
|
|
|
switch (oper) {
|
|
|
|
case CAST:
|
|
|
|
break;
|
|
|
|
case CASTAB:
|
|
|
|
case EQUAL:
|
|
|
|
case NOTEQUAL:
|
|
|
|
case '=':
|
|
|
|
case RETURN:
|
1990-10-23 14:13:10 +00:00
|
|
|
if (is_cp_cst(exp) && exp->VL_VALUE == (arith)0)
|
1989-09-25 14:28:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
1990-11-02 09:23:27 +00:00
|
|
|
expr_strict(exp,
|
1989-09-25 14:28:10 +00:00
|
|
|
"illegal conversion of %s to pointer",
|
|
|
|
symbol2str(oldtp->tp_fund));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (oldtp->tp_size > tp->tp_size)
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_warning(exp,
|
1989-09-25 14:28:10 +00:00
|
|
|
"conversion of %s to pointer loses accuracy",
|
|
|
|
symbol2str(oldtp->tp_fund));
|
1990-10-23 14:13:10 +00:00
|
|
|
if (oldtp->tp_size != tp->tp_size) {
|
1989-09-25 14:28:10 +00:00
|
|
|
int2int(expp, tp);
|
1990-10-23 14:13:10 +00:00
|
|
|
} else
|
|
|
|
exp->ex_type = tp;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (oldtp->tp_fund == ERRONEOUS) {
|
|
|
|
/* we just won't look */
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp; /* brute force */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (oldtp->tp_size == tp->tp_size && oper == CAST) {
|
1990-11-02 09:23:27 +00:00
|
|
|
expr_strict(exp, "dubious conversion based on equal size");
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_type = tp; /* brute force */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (oldtp->tp_fund != ERRONEOUS && tp->tp_fund != ERRONEOUS)
|
1990-10-23 14:13:10 +00:00
|
|
|
expr_error(exp, "cannot convert %s to %s",
|
|
|
|
symbol2str(oldtp->tp_fund),
|
|
|
|
symbol2str(tp->tp_fund)
|
|
|
|
);
|
|
|
|
exp->ex_type = tp; /* brute force */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
1990-10-23 14:13:10 +00:00
|
|
|
/* re-initialize exp, since *expp may have changed */
|
|
|
|
exp = *expp;
|
1989-09-25 14:28:10 +00:00
|
|
|
if (oper == CAST) {
|
1990-10-23 14:13:10 +00:00
|
|
|
exp->ex_flags |= EX_ILVALUE;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine whether two types are equal.
|
|
|
|
*/
|
1991-03-26 09:45:20 +00:00
|
|
|
equal_type(tp, otp, qual_lev, diag)
|
1989-09-25 14:28:10 +00:00
|
|
|
register struct type *tp, *otp;
|
1991-03-26 09:45:20 +00:00
|
|
|
int qual_lev, diag;
|
1989-09-25 14:28:10 +00:00
|
|
|
{
|
1989-11-13 14:01:50 +00:00
|
|
|
if (tp == otp)
|
1989-09-25 14:28:10 +00:00
|
|
|
return 1;
|
1989-10-18 13:12:31 +00:00
|
|
|
if (!tp
|
|
|
|
|| !otp
|
|
|
|
|| (tp->tp_fund != otp->tp_fund)
|
|
|
|
|| (tp->tp_unsigned != otp->tp_unsigned)
|
|
|
|
|| (tp->tp_align != otp->tp_align))
|
1989-09-25 14:28:10 +00:00
|
|
|
return 0;
|
1989-11-28 15:28:52 +00:00
|
|
|
if (tp->tp_size != otp->tp_size) {
|
|
|
|
if (tp->tp_fund != ARRAY
|
|
|
|
|| (tp->tp_size != -1 && otp->tp_size != -1))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1991-03-26 09:45:20 +00:00
|
|
|
if (qual_lev >= 0 && tp->tp_typequal != otp->tp_typequal) {
|
1991-10-17 13:27:53 +00:00
|
|
|
strict("missing or illegal qualifiers");
|
1989-11-13 14:01:50 +00:00
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
switch (tp->tp_fund) {
|
|
|
|
|
|
|
|
case FUNCTION:
|
|
|
|
/* If both types have parameter type lists, the type of
|
|
|
|
each parameter in the composite parameter type list
|
|
|
|
is the composite type of the corresponding paramaters.
|
|
|
|
*/
|
|
|
|
if (tp->tp_proto && otp->tp_proto) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (!equal_proto(tp->tp_proto, otp->tp_proto, diag))
|
1989-09-25 14:28:10 +00:00
|
|
|
return 0;
|
|
|
|
} else if (tp->tp_proto || otp->tp_proto) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (!legal_mixture(tp, otp, diag))
|
1989-09-25 14:28:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1991-03-26 09:45:20 +00:00
|
|
|
return equal_type(tp->tp_up, otp->tp_up, qual_lev + 1, diag);
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
/* If one type is an array of known size, the composite
|
|
|
|
type is an array of that size
|
|
|
|
*/
|
|
|
|
if (tp->tp_size != otp->tp_size &&
|
|
|
|
(tp->tp_size != -1 && otp->tp_size != -1))
|
|
|
|
return 0;
|
1991-03-26 09:45:20 +00:00
|
|
|
return equal_type(tp->tp_up, otp->tp_up, qual_lev/* ??? +1 */, diag);
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
case POINTER:
|
1991-03-26 09:45:20 +00:00
|
|
|
return equal_type(tp->tp_up, otp->tp_up, qual_lev + 1, diag);
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
case FIELD:
|
1991-03-26 09:45:20 +00:00
|
|
|
return equal_type(tp->tp_up, otp->tp_up, qual_lev/* ??? +1 */, diag);
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
case STRUCT:
|
|
|
|
case UNION:
|
|
|
|
case ENUM:
|
|
|
|
return tp->tp_idf == otp->tp_idf && tp->tp_sdef == otp->tp_sdef;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1991-03-26 09:45:20 +00:00
|
|
|
check_pseudoproto(pl, opl, diag)
|
1989-09-25 14:28:10 +00:00
|
|
|
register struct proto *pl, *opl;
|
|
|
|
{
|
|
|
|
int retval = 1;
|
|
|
|
|
|
|
|
if (pl->pl_flag & PL_ELLIPSIS) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (diag) {
|
|
|
|
error("illegal ellipsis terminator");
|
|
|
|
pl->pl_flag |= PL_ERRGIVEN;
|
|
|
|
opl->pl_flag |= PL_ERRGIVEN;
|
|
|
|
}
|
1989-10-19 14:53:25 +00:00
|
|
|
return 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
if (opl->pl_flag & PL_VOID) {
|
1992-03-02 14:37:37 +00:00
|
|
|
if (!(pl->pl_flag & PL_VOID)) {
|
|
|
|
if (diag) {
|
|
|
|
strict("function is defined without parameters");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
while (pl && opl) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (!equal_type(pl->pl_type, opl->pl_type, -1, diag)) {
|
|
|
|
if (diag) {
|
|
|
|
if (!(pl->pl_flag & PL_ERRGIVEN)
|
|
|
|
&& !(opl->pl_flag & PL_ERRGIVEN))
|
|
|
|
error("incorrect type for parameter %s of definition",
|
|
|
|
opl->pl_idf->id_text);
|
|
|
|
pl->pl_flag |= PL_ERRGIVEN;
|
|
|
|
opl->pl_flag |= PL_ERRGIVEN;
|
|
|
|
}
|
1989-10-19 14:53:25 +00:00
|
|
|
retval = 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
pl = pl->next;
|
|
|
|
opl = opl->next;
|
|
|
|
}
|
|
|
|
if (pl || opl) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (diag) error("incorrect number of parameters");
|
1989-10-19 14:53:25 +00:00
|
|
|
retval = 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
1991-03-26 09:45:20 +00:00
|
|
|
legal_mixture(tp, otp, diag)
|
1989-09-25 14:28:10 +00:00
|
|
|
struct type *tp, *otp;
|
1991-03-26 09:45:20 +00:00
|
|
|
int diag;
|
1989-09-25 14:28:10 +00:00
|
|
|
{
|
1991-03-27 12:46:37 +00:00
|
|
|
struct proto *pl = tp->tp_proto, *opl = otp->tp_proto;
|
1989-09-25 14:28:10 +00:00
|
|
|
int retval = 1;
|
1991-03-27 12:46:37 +00:00
|
|
|
register struct proto *prot;
|
1989-09-25 14:28:10 +00:00
|
|
|
int fund;
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
assert( (pl != 0) ^ (opl != 0));
|
1989-09-25 14:28:10 +00:00
|
|
|
if (pl) {
|
|
|
|
prot = pl;
|
|
|
|
} else {
|
|
|
|
prot = opl;
|
|
|
|
}
|
|
|
|
if (!opl && otp->tp_pseudoproto) {
|
1991-03-26 09:45:20 +00:00
|
|
|
return check_pseudoproto(tp->tp_proto, otp->tp_pseudoproto, diag);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prot->pl_flag & PL_ELLIPSIS) {
|
1991-04-03 10:02:40 +00:00
|
|
|
if (prot->pl_flag & PL_ERRGIVEN) {
|
1989-09-25 14:28:10 +00:00
|
|
|
if (pl)
|
|
|
|
error("illegal ellipsis terminator");
|
|
|
|
else error("ellipsis terminator in previous (prototype) declaration");
|
1989-10-19 14:53:25 +00:00
|
|
|
prot->pl_flag |= PL_ERRGIVEN;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
1989-10-19 14:53:25 +00:00
|
|
|
return 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
while (prot) {
|
|
|
|
/* if (!(prot->pl_flag & PL_ELLIPSIS)) {} */
|
|
|
|
fund = prot->pl_type->tp_fund;
|
|
|
|
if (fund == CHAR || fund == SHORT || fund == FLOAT) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (diag && !(prot->pl_flag & PL_ERRGIVEN))
|
1989-09-25 14:28:10 +00:00
|
|
|
error("illegal %s parameter in %sdeclaration",
|
|
|
|
symbol2str(fund), (opl ? "previous (prototype) " : "" ));
|
|
|
|
prot->pl_flag |= PL_ERRGIVEN;
|
1989-10-19 14:53:25 +00:00
|
|
|
retval = 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
prot = prot->next;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
1991-03-26 09:45:20 +00:00
|
|
|
equal_proto(pl, opl, diag)
|
1989-09-25 14:28:10 +00:00
|
|
|
register struct proto *pl, *opl;
|
1991-03-26 09:45:20 +00:00
|
|
|
int diag;
|
1989-09-25 14:28:10 +00:00
|
|
|
{
|
|
|
|
if (pl == opl)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* If only one type is a function type with a parameter type list
|
|
|
|
(a function prototype), the composite type is a function
|
|
|
|
prototype with parameter type list.
|
|
|
|
*/
|
|
|
|
while ( pl && opl) {
|
|
|
|
|
1989-10-19 14:53:25 +00:00
|
|
|
if ((pl->pl_flag & ~PL_ERRGIVEN) != (opl->pl_flag & ~PL_ERRGIVEN) ||
|
1991-03-26 09:45:20 +00:00
|
|
|
!equal_type(pl->pl_type, opl->pl_type, -1, diag))
|
1989-09-25 14:28:10 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
pl = pl->next;
|
|
|
|
opl = opl->next;
|
|
|
|
}
|
|
|
|
return !(pl || opl);
|
|
|
|
}
|
|
|
|
|
1990-04-02 15:57:51 +00:00
|
|
|
/* check if a type has a consqualified member */
|
|
|
|
recurqual(tp, qual)
|
1989-09-25 14:28:10 +00:00
|
|
|
struct type *tp;
|
1990-04-02 15:57:51 +00:00
|
|
|
int qual;
|
1989-09-25 14:28:10 +00:00
|
|
|
{
|
|
|
|
register struct sdef *sdf;
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
assert(tp);
|
1989-10-20 13:06:10 +00:00
|
|
|
|
1990-04-02 15:57:51 +00:00
|
|
|
if (tp->tp_typequal & qual) return 1;
|
1990-05-15 15:28:01 +00:00
|
|
|
switch(tp->tp_fund) {
|
|
|
|
case UNION:
|
|
|
|
case STRUCT:
|
|
|
|
case ENUM:
|
|
|
|
sdf = tp->tp_sdef;
|
|
|
|
while (sdf) {
|
|
|
|
if (recurqual(sdf->sd_type, qual))
|
|
|
|
return 1;
|
|
|
|
sdf = sdf->sd_sdef;
|
|
|
|
}
|
|
|
|
break;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch3asgn(expp, oper, expr)
|
|
|
|
struct expr **expp;
|
|
|
|
struct expr *expr;
|
|
|
|
{
|
|
|
|
/* The assignment operators.
|
|
|
|
"f op= e" should be interpreted as
|
|
|
|
"f = (typeof f)((typeof (f op e))f op (typeof (f op e))e)"
|
|
|
|
and not as "f = f op (typeof f)e".
|
|
|
|
Consider, for example, (i == 10) i *= 0.9; (i == 9), where
|
|
|
|
typeof i == int.
|
|
|
|
The resulting expression tree becomes:
|
|
|
|
op=
|
|
|
|
/ \
|
|
|
|
/ \
|
|
|
|
f (typeof (f op e))e
|
|
|
|
EVAL should however take care of evaluating (typeof (f op e))f
|
|
|
|
*/
|
|
|
|
register struct expr *exp = *expp;
|
|
|
|
int fund = exp->ex_type->tp_fund;
|
|
|
|
struct type *tp;
|
1989-10-19 19:29:39 +00:00
|
|
|
char *oper_string = symbol2str(oper);
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
/* We expect an lvalue */
|
1989-11-22 13:58:36 +00:00
|
|
|
if (fund == ARRAY || fund == FUNCTION) exp->ex_lvalue = 0;
|
1989-09-25 14:28:10 +00:00
|
|
|
if (!exp->ex_lvalue) {
|
1989-10-19 19:29:39 +00:00
|
|
|
expr_error(exp, "no lvalue in operand of %s", oper_string);
|
1989-09-25 14:28:10 +00:00
|
|
|
} else if (exp->ex_flags & EX_ILVALUE) {
|
1989-11-08 16:52:34 +00:00
|
|
|
expr_strict(exp, "incorrect lvalue in operand of %s", oper_string);
|
1989-09-25 14:28:10 +00:00
|
|
|
} else if (exp->ex_flags & EX_READONLY) {
|
1989-10-19 19:29:39 +00:00
|
|
|
expr_error(exp, "operand of %s is read-only", oper_string);
|
1989-09-25 14:28:10 +00:00
|
|
|
} else if (fund == STRUCT || fund == UNION) {
|
1990-04-02 15:57:51 +00:00
|
|
|
if (recurqual(exp->ex_type, TQ_CONST))
|
1990-04-06 15:37:16 +00:00
|
|
|
expr_error(exp,"operand of %s contains a const-qualified member",
|
1989-10-19 19:29:39 +00:00
|
|
|
oper_string);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oper == '=') {
|
|
|
|
ch3cast(&expr, oper, exp->ex_type);
|
|
|
|
tp = expr->ex_type;
|
|
|
|
}
|
|
|
|
else { /* turn e into e' where typeof(e') = typeof (f op e) */
|
|
|
|
struct expr *extmp = intexpr((arith)0, INT);
|
|
|
|
|
|
|
|
/* this is really $#@&*%$# ! */
|
|
|
|
/* if you correct this, please correct lint_new_oper() too */
|
|
|
|
extmp->ex_lvalue = 1;
|
|
|
|
extmp->ex_type = exp->ex_type;
|
|
|
|
ch3bin(&extmp, oper, expr);
|
|
|
|
/* Note that ch3bin creates a tree of the expression
|
|
|
|
((typeof (f op e))f op (typeof (f op e))e),
|
|
|
|
where f ~ extmp and e ~ expr.
|
|
|
|
We want to use (typeof (f op e))e.
|
1989-09-29 16:20:38 +00:00
|
|
|
Ch3bin does not create a tree if both operands
|
1989-09-25 14:28:10 +00:00
|
|
|
were illegal or constants!
|
|
|
|
*/
|
|
|
|
tp = extmp->ex_type; /* perform the arithmetic in type tp */
|
|
|
|
if (extmp->ex_class == Oper) {
|
|
|
|
expr = extmp->OP_RIGHT;
|
|
|
|
extmp->OP_RIGHT = NILEXPR;
|
|
|
|
free_expression(extmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
expr = extmp;
|
|
|
|
}
|
|
|
|
#ifndef NOBITFIELD
|
1989-10-19 19:29:39 +00:00
|
|
|
exp = new_oper(fund == FIELD ? exp->ex_type->tp_up : exp->ex_type,
|
|
|
|
exp, oper, expr);
|
1991-12-17 13:12:22 +00:00
|
|
|
#else /* NOBITFIELD */
|
1989-09-25 14:28:10 +00:00
|
|
|
exp = new_oper(exp->ex_type, exp, oper, expr);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* NOBITFIELD */
|
1989-09-25 14:28:10 +00:00
|
|
|
exp->OP_TYPE = tp; /* for EVAL() */
|
1990-04-02 15:57:51 +00:00
|
|
|
exp->ex_flags |= EX_SIDEEFFECTS;
|
1989-09-25 14:28:10 +00:00
|
|
|
*expp = exp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some interesting (?) questions answered.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
is_integral_type(tp)
|
|
|
|
register struct type *tp;
|
|
|
|
{
|
|
|
|
switch (tp->tp_fund) {
|
|
|
|
case CHAR:
|
|
|
|
case SHORT:
|
|
|
|
case INT:
|
|
|
|
case LONG:
|
|
|
|
case ENUM:
|
|
|
|
return 1;
|
|
|
|
#ifndef NOBITFIELD
|
|
|
|
case FIELD:
|
|
|
|
return is_integral_type(tp->tp_up);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* NOBITFIELD */
|
1989-09-25 14:28:10 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
is_arith_type(tp)
|
|
|
|
register struct type *tp;
|
|
|
|
{
|
|
|
|
switch (tp->tp_fund) {
|
|
|
|
case CHAR:
|
|
|
|
case SHORT:
|
|
|
|
case INT:
|
|
|
|
case LONG:
|
|
|
|
case ENUM:
|
|
|
|
case FLOAT:
|
|
|
|
case DOUBLE:
|
|
|
|
case LNGDBL:
|
|
|
|
return 1;
|
|
|
|
#ifndef NOBITFIELD
|
|
|
|
case FIELD:
|
|
|
|
return is_arith_type(tp->tp_up);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* NOBITFIELD */
|
1989-09-25 14:28:10 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|