ack/lang/m2/comp/chk_expr.c

883 lines
18 KiB
C
Raw Normal View History

1986-04-08 18:15:46 +00:00
/* E X P R E S S I O N C H E C K I N G */
static char *RcsId = "$Header$";
/* Check expressions, and try to evaluate them as far as possible.
*/
#include <em_arith.h>
#include <em_label.h>
#include <assert.h>
1986-04-09 18:14:49 +00:00
#include <alloc.h>
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
#include "Lpars.h"
1986-04-08 18:15:46 +00:00
#include "idf.h"
#include "type.h"
#include "def.h"
#include "LLlex.h"
#include "node.h"
#include "scope.h"
1986-04-09 18:14:49 +00:00
#include "const.h"
#include "standards.h"
1986-04-18 17:53:47 +00:00
1986-04-11 11:57:19 +00:00
#include "debug.h"
1986-04-08 18:15:46 +00:00
int
1986-04-10 01:08:49 +00:00
chk_expr(expp)
1986-04-08 18:15:46 +00:00
register struct node *expp;
{
/* Check the expression indicated by expp for semantic errors,
identify identifiers used in it, replace constants by
1986-04-18 17:53:47 +00:00
their value, and try to evaluate the expression.
1986-04-08 18:15:46 +00:00
*/
switch(expp->nd_class) {
case Oper:
1986-04-10 01:08:49 +00:00
return chk_expr(expp->nd_left) &&
chk_expr(expp->nd_right) &&
chk_oper(expp);
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case Uoper:
1986-04-10 01:08:49 +00:00
return chk_expr(expp->nd_right) &&
chk_uoper(expp);
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case Value:
switch(expp->nd_symb) {
case REAL:
case STRING:
case INTEGER:
return 1;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
default:
assert(0);
}
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case Xset:
1986-04-10 01:08:49 +00:00
return chk_set(expp);
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case Name:
1986-04-10 01:08:49 +00:00
return chk_name(expp);
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case Call:
1986-04-10 01:08:49 +00:00
return chk_call(expp);
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case Link:
1986-04-10 01:08:49 +00:00
return chk_name(expp);
1986-04-22 22:36:16 +00:00
1986-04-08 23:34:10 +00:00
default:
assert(0);
1986-04-08 18:15:46 +00:00
}
/*NOTREACHED*/
}
int
1986-04-10 01:08:49 +00:00
chk_set(expp)
1986-04-08 18:15:46 +00:00
register struct node *expp;
{
1986-04-09 18:14:49 +00:00
/* Check the legality of a SET aggregate, and try to evaluate it
compile time. Unfortunately this is all rather complicated.
*/
1986-04-08 23:34:10 +00:00
struct type *tp;
struct def *df;
register struct node *nd;
1986-04-09 18:14:49 +00:00
arith *set;
1986-04-08 23:34:10 +00:00
assert(expp->nd_symb == SET);
/* First determine the type of the set
*/
1986-04-22 22:36:16 +00:00
if (nd = expp->nd_left) {
1986-04-08 23:34:10 +00:00
/* A type was given. Check it out
*/
1986-04-22 22:36:16 +00:00
findname(nd);
assert(nd->nd_class == Def);
df = nd->nd_def;
1986-04-18 17:53:47 +00:00
if (!(df->df_kind & (D_TYPE|D_ERROR)) ||
1986-04-10 01:08:49 +00:00
(df->df_type->tp_fund != T_SET)) {
1986-04-22 22:36:16 +00:00
node_error(expp, "specifier does not represent a set type");
1986-04-08 23:34:10 +00:00
return 0;
}
tp = df->df_type;
1986-04-22 22:36:16 +00:00
FreeNode(expp->nd_left);
expp->nd_left = 0;
1986-04-08 23:34:10 +00:00
}
else tp = bitset_type;
1986-04-09 18:14:49 +00:00
/* Now check the elements given, and try to compute a constant set.
1986-04-22 22:36:16 +00:00
First allocate room for the set
1986-04-08 23:34:10 +00:00
*/
1986-04-18 17:53:47 +00:00
set = (arith *)
Malloc((unsigned) (tp->tp_size * sizeof(arith) / word_size));
1986-04-22 22:36:16 +00:00
/* Now check the elements, one by one
*/
1986-04-08 23:34:10 +00:00
nd = expp->nd_right;
while (nd) {
assert(nd->nd_class == Link && nd->nd_symb == ',');
1986-04-22 22:36:16 +00:00
1986-04-10 01:08:49 +00:00
if (!chk_el(nd->nd_left, tp->next, &set)) return 0;
1986-04-08 23:34:10 +00:00
nd = nd->nd_right;
}
1986-04-22 22:36:16 +00:00
1986-04-09 18:14:49 +00:00
expp->nd_type = tp;
1986-04-22 22:36:16 +00:00
1986-04-09 18:14:49 +00:00
if (set) {
1986-04-18 17:53:47 +00:00
/* Yes, it was a constant set, and we managed to compute it!
Notice that at the moment there is no such thing as
partial evaluation. Either we evaluate the set, or we
don't (at all). Improvement not neccesary. (???)
1986-04-09 18:14:49 +00:00
*/
expp->nd_class = Set;
expp->nd_set = set;
FreeNode(expp->nd_right);
1986-04-22 22:36:16 +00:00
expp->nd_right = 0;
1986-04-09 18:14:49 +00:00
}
1986-04-22 22:36:16 +00:00
1986-04-08 23:34:10 +00:00
return 1;
}
int
1986-04-10 01:08:49 +00:00
chk_el(expp, tp, set)
1986-04-09 18:14:49 +00:00
register struct node *expp;
1986-04-08 23:34:10 +00:00
struct type *tp;
1986-04-09 18:14:49 +00:00
arith **set;
1986-04-08 23:34:10 +00:00
{
/* Check elements of a set. This routine may call itself
1986-04-09 18:14:49 +00:00
recursively.
Also try to compute the set!
1986-04-08 23:34:10 +00:00
*/
1986-04-18 17:53:47 +00:00
register int i;
1986-04-22 22:36:16 +00:00
register struct node *left = expp->nd_left;
register struct node *right = expp->nd_right;
1986-04-18 17:53:47 +00:00
1986-04-08 23:34:10 +00:00
if (expp->nd_class == Link && expp->nd_symb == UPTO) {
1986-04-09 18:14:49 +00:00
/* { ... , expr1 .. expr2, ... }
First check expr1 and expr2, and try to compute them.
*/
1986-04-22 22:36:16 +00:00
if (!chk_el(left, tp, set) || !chk_el(right, tp, set)) {
1986-04-08 23:34:10 +00:00
return 0;
}
1986-04-22 22:36:16 +00:00
if (left->nd_class == Value && right->nd_class == Value) {
1986-04-09 18:14:49 +00:00
/* We have a constant range. Put all elements in the
set
*/
1986-04-22 22:36:16 +00:00
if (left->nd_INT > right->nd_INT) {
1986-04-18 17:53:47 +00:00
node_error(expp, "lower bound exceeds upper bound in range");
1986-04-09 18:14:49 +00:00
return rem_set(set);
1986-04-08 23:34:10 +00:00
}
1986-04-22 22:36:16 +00:00
if (*set) {
for (i=left->nd_INT+1; i<right->nd_INT; i++) {
(*set)[i/wrd_bits] |= (1<<(i%wrd_bits));
}
1986-04-09 18:14:49 +00:00
}
}
else if (*set) {
1986-04-17 09:28:09 +00:00
free((char *) *set);
1986-04-09 18:14:49 +00:00
*set = 0;
1986-04-08 23:34:10 +00:00
}
1986-04-22 22:36:16 +00:00
1986-04-08 23:34:10 +00:00
return 1;
}
1986-04-09 18:14:49 +00:00
/* Here, a single element is checked
*/
1986-04-10 01:08:49 +00:00
if (!chk_expr(expp)) {
1986-04-09 18:14:49 +00:00
return rem_set(set);
}
1986-04-22 22:36:16 +00:00
1986-04-08 23:34:10 +00:00
if (!TstCompat(tp, expp->nd_type)) {
1986-04-18 17:53:47 +00:00
node_error(expp, "set element has incompatible type");
1986-04-09 18:14:49 +00:00
return rem_set(set);
1986-04-08 23:34:10 +00:00
}
1986-04-22 22:36:16 +00:00
1986-04-08 23:34:10 +00:00
if (expp->nd_class == Value) {
1986-04-22 22:36:16 +00:00
/* a constant element
*/
1986-04-18 17:53:47 +00:00
i = expp->nd_INT;
1986-04-22 22:36:16 +00:00
1986-04-10 01:08:49 +00:00
if ((tp->tp_fund != T_ENUMERATION &&
1986-04-18 17:53:47 +00:00
(i < tp->sub_lb || i > tp->sub_ub))
1986-04-08 23:34:10 +00:00
||
1986-04-10 01:08:49 +00:00
(tp->tp_fund == T_ENUMERATION &&
1986-04-18 17:53:47 +00:00
(i < 0 || i > tp->enm_ncst))
1986-04-08 23:34:10 +00:00
) {
1986-04-18 17:53:47 +00:00
node_error(expp, "set element out of range");
1986-04-09 18:14:49 +00:00
return rem_set(set);
1986-04-08 23:34:10 +00:00
}
1986-04-22 22:36:16 +00:00
1986-04-18 17:53:47 +00:00
if (*set) (*set)[i/wrd_bits] |= (1 << (i%wrd_bits));
1986-04-08 23:34:10 +00:00
}
1986-04-22 22:36:16 +00:00
1986-04-08 18:15:46 +00:00
return 1;
}
1986-04-09 18:14:49 +00:00
int
rem_set(set)
arith **set;
{
/* This routine is only used for error exits of chk_el.
It frees the set indicated by "set", and returns 0.
*/
if (*set) {
free((char *) *set);
*set = 0;
}
return 0;
}
1986-04-10 01:08:49 +00:00
struct node *
getarg(argp, bases)
struct node *argp;
{
struct type *tp;
if (!argp->nd_right) {
1986-04-11 11:57:19 +00:00
node_error(argp, "too few arguments supplied");
1986-04-10 01:08:49 +00:00
return 0;
}
argp = argp->nd_right;
if (!chk_expr(argp->nd_left)) return 0;
tp = argp->nd_left->nd_type;
if (tp->tp_fund == T_SUBRANGE) tp = tp->next;
1986-04-18 17:53:47 +00:00
if (bases && !(tp->tp_fund & bases)) {
node_error(argp, "unexpected type");
1986-04-10 01:08:49 +00:00
return 0;
}
return argp;
}
struct node *
getname(argp, kinds)
struct node *argp;
{
if (!argp->nd_right) {
1986-04-11 11:57:19 +00:00
node_error(argp, "too few arguments supplied");
1986-04-10 01:08:49 +00:00
return 0;
}
argp = argp->nd_right;
1986-04-17 09:28:09 +00:00
findname(argp->nd_left);
1986-04-10 01:08:49 +00:00
assert(argp->nd_left->nd_class == Def);
if (!(argp->nd_left->nd_def->df_kind & kinds)) {
1986-04-18 17:53:47 +00:00
node_error(argp, "unexpected type");
1986-04-10 01:08:49 +00:00
return 0;
}
return argp;
}
1986-04-08 18:15:46 +00:00
int
1986-04-10 01:08:49 +00:00
chk_call(expp)
1986-04-08 18:15:46 +00:00
register struct node *expp;
{
1986-04-11 11:57:19 +00:00
/* Check something that looks like a procedure or function call.
Of course this does not have to be a call at all.
it may also be a cast or a standard procedure call.
*/
1986-04-09 18:14:49 +00:00
register struct node *left;
1986-04-10 01:08:49 +00:00
register struct node *arg;
1986-04-09 18:14:49 +00:00
1986-04-18 17:53:47 +00:00
/* First, get the name of the function or procedure
*/
1986-04-09 18:14:49 +00:00
expp->nd_type = error_type;
left = expp->nd_left;
1986-04-17 09:28:09 +00:00
findname(left);
1986-04-09 18:14:49 +00:00
1986-04-11 11:57:19 +00:00
if (left->nd_type == error_type) return 0;
1986-04-09 18:14:49 +00:00
if (left->nd_class == Def &&
(left->nd_def->df_kind & (D_HTYPE|D_TYPE|D_HIDDEN))) {
1986-04-18 17:53:47 +00:00
/* It was a type cast. This is of course not portable.
1986-04-09 18:14:49 +00:00
No runtime action. Remove it.
*/
1986-04-10 01:08:49 +00:00
arg = expp->nd_right;
1986-04-11 11:57:19 +00:00
if ((! arg) || arg->nd_right) {
1986-04-18 17:53:47 +00:00
node_error(expp, "only one parameter expected in type cast");
1986-04-09 18:14:49 +00:00
return 0;
}
1986-04-11 11:57:19 +00:00
arg = arg->nd_left;
if (! chk_expr(arg)) return 0;
if (arg->nd_type->tp_size != left->nd_type->tp_size) {
1986-04-18 17:53:47 +00:00
node_error(expp, "size of type in type cast does not match size of operand");
1986-04-09 18:14:49 +00:00
return 0;
}
1986-04-11 11:57:19 +00:00
arg->nd_type = left->nd_type;
1986-04-09 18:14:49 +00:00
FreeNode(expp->nd_left);
1986-04-15 17:51:53 +00:00
expp->nd_right->nd_left = 0;
FreeNode(expp->nd_right);
*expp = *arg;
1986-04-11 11:57:19 +00:00
arg->nd_left = 0;
arg->nd_right = 0;
1986-04-10 01:08:49 +00:00
FreeNode(arg);
1986-04-09 18:14:49 +00:00
return 1;
}
if ((left->nd_class == Def && left->nd_def->df_kind == D_PROCEDURE) ||
1986-04-11 11:57:19 +00:00
left->nd_type->tp_fund == T_PROCEDURE) {
1986-04-09 18:14:49 +00:00
/* A procedure call. it may also be a call to a
standard procedure
*/
1986-04-10 01:08:49 +00:00
arg = expp;
1986-04-11 11:57:19 +00:00
if (left->nd_type == std_type) {
/* A standard procedure
*/
1986-04-09 18:14:49 +00:00
assert(left->nd_class == Def);
1986-04-18 17:53:47 +00:00
DO_DEBUG(3, debug("standard name \"%s\", %d",
1986-04-11 11:57:19 +00:00
left->nd_def->df_idf->id_text, left->nd_def->df_value.df_stdname));
1986-04-09 18:14:49 +00:00
switch(left->nd_def->df_value.df_stdname) {
case S_ABS:
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_NUMERIC);
1986-04-10 01:08:49 +00:00
if (! arg) return 0;
1986-04-11 11:57:19 +00:00
left = arg->nd_left;
expp->nd_type = left->nd_type;
if (left->nd_class == Value) {
cstcall(expp, S_ABS);
}
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_CAP:
1986-04-10 01:08:49 +00:00
arg = getarg(arg, T_CHAR);
expp->nd_type = char_type;
if (!arg) return 0;
1986-04-11 11:57:19 +00:00
left = arg->nd_left;
if (left->nd_class == Value) {
cstcall(expp, S_CAP);
}
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_CHR:
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_INTORCARD);
1986-04-10 01:08:49 +00:00
expp->nd_type = char_type;
if (!arg) return 0;
1986-04-11 11:57:19 +00:00
if (arg->nd_left->nd_class == Value) {
cstcall(expp, S_CHR);
}
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_FLOAT:
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_INTORCARD);
1986-04-10 01:08:49 +00:00
expp->nd_type = real_type;
if (!arg) return 0;
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_HIGH:
1986-04-10 01:08:49 +00:00
arg = getarg(arg, T_ARRAY);
if (!arg) return 0;
expp->nd_type = arg->nd_left->nd_type->next;
1986-04-11 11:57:19 +00:00
if (!expp->nd_type) {
/* A dynamic array has no explicit
index type
*/
1986-04-22 23:22:19 +00:00
expp->nd_type = intorcard_type;
1986-04-11 11:57:19 +00:00
}
else cstcall(expp, S_MAX);
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_MAX:
case S_MIN:
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_DISCRETE);
1986-04-10 01:08:49 +00:00
if (!arg) return 0;
expp->nd_type = arg->nd_left->nd_type;
1986-04-11 11:57:19 +00:00
cstcall(expp,left->nd_def->df_value.df_stdname);
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_ODD:
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_INTORCARD);
1986-04-10 01:08:49 +00:00
if (!arg) return 0;
expp->nd_type = bool_type;
1986-04-11 11:57:19 +00:00
if (arg->nd_left->nd_class == Value) {
cstcall(expp, S_ODD);
}
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_ORD:
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_DISCRETE);
1986-04-10 01:08:49 +00:00
if (!arg) return 0;
expp->nd_type = card_type;
1986-04-11 11:57:19 +00:00
if (arg->nd_left->nd_class == Value) {
cstcall(expp, S_ORD);
}
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case S_TSIZE: /* ??? */
1986-04-09 18:14:49 +00:00
case S_SIZE:
1986-04-10 01:08:49 +00:00
arg = getname(arg, D_FIELD|D_VARIABLE|D_TYPE|D_HIDDEN|D_HTYPE);
expp->nd_type = intorcard_type;
if (!arg) return 0;
1986-04-11 11:57:19 +00:00
cstcall(expp, S_SIZE);
1986-04-10 01:08:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_TRUNC:
1986-04-10 01:08:49 +00:00
arg = getarg(arg, T_REAL);
if (!arg) return 0;
expp->nd_type = card_type;
break;
1986-04-18 17:53:47 +00:00
1986-04-11 11:57:19 +00:00
case S_VAL: {
struct type *tp;
1986-04-10 01:08:49 +00:00
arg = getname(arg, D_HIDDEN|D_HTYPE|D_TYPE);
if (!arg) return 0;
tp = arg->nd_left->nd_def->df_type;
if (tp->tp_fund == T_SUBRANGE) tp = tp->next;
1986-04-11 11:57:19 +00:00
if (!(tp->tp_fund & T_DISCRETE)) {
1986-04-10 01:08:49 +00:00
node_error(arg, "unexpected type");
return 0;
}
expp->nd_type = arg->nd_left->nd_def->df_type;
1986-04-11 11:57:19 +00:00
expp->nd_right = arg->nd_right;
arg->nd_right = 0;
FreeNode(arg);
arg = getarg(expp, T_INTORCARD);
1986-04-10 01:08:49 +00:00
if (!arg) return 0;
1986-04-11 11:57:19 +00:00
if (arg->nd_left->nd_class == Value) {
cstcall(expp, S_VAL);
}
1986-04-10 01:08:49 +00:00
break;
1986-04-11 11:57:19 +00:00
}
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case S_ADR:
arg = getname(arg, D_VARIABLE|D_FIELD|D_PROCEDURE);
expp->nd_type = address_type;
if (!arg) return 0;
1986-04-09 18:14:49 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_DEC:
case S_INC:
1986-04-10 01:08:49 +00:00
expp->nd_type = 0;
arg = getname(arg, D_VARIABLE|D_FIELD);
if (!arg) return 0;
if (arg->nd_right) {
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_INTORCARD);
1986-04-10 01:08:49 +00:00
if (!arg) return 0;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_HALT:
1986-04-10 01:08:49 +00:00
expp->nd_type = 0;
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case S_EXCL:
1986-04-11 11:57:19 +00:00
case S_INCL: {
struct type *tp;
1986-04-09 18:14:49 +00:00
expp->nd_type = 0;
1986-04-10 01:08:49 +00:00
arg = getname(arg, D_VARIABLE|D_FIELD);
if (!arg) return 0;
tp = arg->nd_left->nd_type;
if (tp->tp_fund != T_SET) {
node_error(arg, "EXCL and INCL expect a SET parameter");
return 0;
}
1986-04-11 11:57:19 +00:00
arg = getarg(arg, T_DISCRETE);
1986-04-10 01:08:49 +00:00
if (!arg) return 0;
if (!TstCompat(tp->next, arg->nd_left->nd_type)) {
1986-04-18 17:53:47 +00:00
node_error(arg, "unexpected type");
1986-04-10 01:08:49 +00:00
return 0;
}
1986-04-09 18:14:49 +00:00
break;
1986-04-11 11:57:19 +00:00
}
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
default:
assert(0);
}
1986-04-10 01:08:49 +00:00
if (arg->nd_right) {
node_error(arg->nd_right,
1986-04-11 11:57:19 +00:00
"too many parameters supplied");
1986-04-10 01:08:49 +00:00
return 0;
}
1986-04-09 18:14:49 +00:00
return 1;
}
1986-04-18 17:53:47 +00:00
/* Here, we have found a real procedure call. The left hand
side may also represent a procedure variable.
1986-04-11 11:57:19 +00:00
*/
1986-04-18 17:53:47 +00:00
return chk_proccall(expp);
1986-04-09 18:14:49 +00:00
}
node_error(expp->nd_left, "procedure, type, or function expected");
return 0;
1986-04-08 18:15:46 +00:00
}
1986-04-18 17:53:47 +00:00
chk_proccall(expp)
struct node *expp;
{
/* Check a procedure call
*/
register struct node *left = expp->nd_left;
register struct node *arg;
register struct paramlist *param;
expp->nd_type = left->nd_type->next;
param = left->nd_type->prc_params;
arg = expp;
while (param) {
arg = getarg(arg, 0);
if (!arg) return 0;
if (param->par_var &&
! TstCompat(param->par_type, arg->nd_left->nd_type)) {
node_error(arg->nd_left, "type incompatibility in var parameter");
return 0;
}
else
if (!param->par_var &&
!TstAssCompat(param->par_type, arg->nd_left->nd_type)) {
node_error(arg->nd_left, "type incompatibility in value parameter");
return 0;
}
param = param->next;
}
if (arg->nd_right) {
node_error(arg->nd_right, "too many parameters supplied");
return 0;
}
return 1;
}
1986-04-08 18:15:46 +00:00
findname(expp)
register struct node *expp;
{
/* Find the name indicated by "expp", starting from the current
scope.
*/
register struct def *df;
1986-04-09 18:14:49 +00:00
register struct type *tp;
1986-04-17 09:28:09 +00:00
struct def *lookfor();
1986-04-08 18:15:46 +00:00
1986-04-09 18:14:49 +00:00
expp->nd_type = error_type;
1986-04-08 18:15:46 +00:00
if (expp->nd_class == Name) {
1986-04-09 18:14:49 +00:00
expp->nd_def = lookfor(expp, CurrentScope, 1);
expp->nd_class = Def;
expp->nd_type = expp->nd_def->df_type;
return;
1986-04-08 18:15:46 +00:00
}
1986-04-09 18:14:49 +00:00
if (expp->nd_class == Link) {
assert(expp->nd_symb == '.');
assert(expp->nd_right->nd_class == Name);
findname(expp->nd_left);
tp = expp->nd_left->nd_type;
if (tp == error_type) {
df = ill_df;
1986-04-08 18:15:46 +00:00
}
1986-04-10 01:08:49 +00:00
else if (tp->tp_fund != T_RECORD) {
1986-04-09 18:14:49 +00:00
/* This is also true for modules */
1986-04-18 17:53:47 +00:00
node_error(expp,"illegal selection");
1986-04-09 18:14:49 +00:00
df = ill_df;
}
else df = lookup(expp->nd_right->nd_IDF, tp->rec_scope);
1986-04-08 18:15:46 +00:00
if (!df) {
1986-04-09 18:14:49 +00:00
df = ill_df;
id_not_declared(expp->nd_right);
1986-04-08 18:15:46 +00:00
}
1986-04-09 18:14:49 +00:00
else if (df != ill_df) {
expp->nd_type = df->df_type;
if (!(df->df_flags & (D_EXPORTED|D_QEXPORTED))) {
node_error(expp->nd_right,
1986-04-22 22:36:16 +00:00
"identifier \"%s\" not exported from qualifying module",
1986-04-08 18:15:46 +00:00
df->df_idf->id_text);
1986-04-09 18:14:49 +00:00
}
1986-04-08 18:15:46 +00:00
}
1986-04-09 18:14:49 +00:00
if (expp->nd_left->nd_class == Def) {
expp->nd_class = Def;
expp->nd_def = df;
FreeNode(expp->nd_left);
FreeNode(expp->nd_right);
expp->nd_left = expp->nd_right = 0;
}
return;
}
if (expp->nd_class == Oper) {
assert(expp->nd_symb == '[');
1986-04-17 09:28:09 +00:00
findname(expp->nd_left);
if (chk_expr(expp->nd_right) &&
1986-04-09 18:14:49 +00:00
expp->nd_left->nd_type != error_type &&
chk_oper(expp)) /* ??? */ ;
1986-04-17 09:28:09 +00:00
return;
1986-04-08 18:15:46 +00:00
}
1986-04-09 18:14:49 +00:00
if (expp->nd_class == Uoper && expp->nd_symb == '^') {
1986-04-17 09:28:09 +00:00
findname(expp->nd_right);
1986-04-09 18:14:49 +00:00
if (expp->nd_right->nd_type != error_type &&
chk_uoper(expp)) /* ??? */ ;
}
1986-04-17 09:28:09 +00:00
return;
1986-04-08 18:15:46 +00:00
}
int
1986-04-10 01:08:49 +00:00
chk_name(expp)
1986-04-08 18:15:46 +00:00
register struct node *expp;
{
register struct def *df;
1986-04-17 09:28:09 +00:00
findname(expp);
1986-04-09 18:14:49 +00:00
assert(expp->nd_class == Def);
df = expp->nd_def;
1986-04-10 01:08:49 +00:00
if (df->df_kind == D_ERROR) return 0;
1986-04-09 18:14:49 +00:00
if (df->df_kind & (D_ENUM | D_CONST)) {
1986-04-08 18:15:46 +00:00
if (df->df_kind == D_ENUM) {
expp->nd_class = Value;
expp->nd_INT = df->enm_val;
expp->nd_symb = INTEGER;
}
else if (df->df_kind == D_CONST) {
*expp = *(df->con_const);
}
}
1986-04-10 01:08:49 +00:00
return 1;
1986-04-08 18:15:46 +00:00
}
int
1986-04-10 01:08:49 +00:00
chk_oper(expp)
1986-04-08 18:15:46 +00:00
register struct node *expp;
{
1986-04-10 01:08:49 +00:00
/* Check a binary operation.
1986-04-08 18:15:46 +00:00
*/
register struct type *tpl = expp->nd_left->nd_type;
register struct type *tpr = expp->nd_right->nd_type;
char *symbol2str();
int errval = 1;
if (tpl == intorcard_type) {
if (tpr == int_type || tpr == card_type) {
expp->nd_left->nd_type = tpl = tpr;
}
}
if (tpr == intorcard_type) {
if (tpl == int_type || tpl == card_type) {
expp->nd_right->nd_type = tpr = tpl;
}
}
1986-04-09 18:14:49 +00:00
expp->nd_type = error_type;
1986-04-08 18:15:46 +00:00
if (expp->nd_symb == IN) {
/* Handle this one specially */
1986-04-09 18:14:49 +00:00
expp->nd_type = bool_type;
1986-04-10 01:08:49 +00:00
if (tpr->tp_fund != T_SET) {
1986-04-08 18:15:46 +00:00
node_error(expp, "RHS of IN operator not a SET type");
return 0;
}
if (!TstCompat(tpl, tpr->next)) {
node_error(expp, "IN operator: type of LHS not compatible with element type of RHS");
return 0;
}
1986-04-11 11:57:19 +00:00
if (expp->nd_left->nd_class == Value &&
expp->nd_right->nd_class == Set) {
cstset(expp);
}
1986-04-08 18:15:46 +00:00
return 1;
}
1986-04-09 18:14:49 +00:00
if (expp->nd_symb == '[') {
1986-04-22 23:22:19 +00:00
/* Handle ARRAY selection specially too!
*/
1986-04-10 01:08:49 +00:00
if (tpl->tp_fund != T_ARRAY) {
1986-04-11 11:57:19 +00:00
node_error(expp,
"array index not belonging to an ARRAY");
1986-04-09 18:14:49 +00:00
return 0;
}
1986-04-22 23:22:19 +00:00
if ((tpl->next && !TstCompat(tpl->next, tpr)) ||
(!tpl->next && !TstCompat(intorcard_type, tpr)) {
1986-04-11 11:57:19 +00:00
node_error(expp, "incompatible index type");
1986-04-09 18:14:49 +00:00
}
1986-04-22 23:22:19 +00:00
1986-04-09 18:14:49 +00:00
expp->nd_type = tpl->arr_elem;
return 1;
}
1986-04-10 01:08:49 +00:00
if (tpl->tp_fund == T_SUBRANGE) tpl = tpl->next;
1986-04-08 18:15:46 +00:00
expp->nd_type = tpl;
if (!TstCompat(tpl, tpr)) {
1986-04-11 11:57:19 +00:00
node_error(expp,
1986-04-15 17:51:53 +00:00
"incompatible types for operator \"%s\"",
1986-04-11 11:57:19 +00:00
symbol2str(expp->nd_symb));
1986-04-08 18:15:46 +00:00
return 0;
}
switch(expp->nd_symb) {
case '+':
case '-':
case '*':
switch(tpl->tp_fund) {
1986-04-10 01:08:49 +00:00
case T_INTEGER:
case T_CARDINAL:
1986-04-11 11:57:19 +00:00
case T_INTORCARD:
1986-04-08 18:15:46 +00:00
if (expp->nd_left->nd_class == Value &&
expp->nd_right->nd_class == Value) {
cstbin(expp);
}
return 1;
1986-04-18 17:53:47 +00:00
1986-04-11 11:57:19 +00:00
case T_SET:
if (expp->nd_left->nd_class == Set &&
expp->nd_right->nd_class == Set) {
cstset(expp);
}
/* Fall through */
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case T_REAL:
1986-04-08 18:15:46 +00:00
return 1;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case '/':
switch(tpl->tp_fund) {
1986-04-10 01:08:49 +00:00
case T_SET:
1986-04-11 11:57:19 +00:00
if (expp->nd_left->nd_class == Set &&
expp->nd_right->nd_class == Set) {
cstset(expp);
1986-04-08 18:15:46 +00:00
}
1986-04-11 11:57:19 +00:00
/* Fall through */
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case T_REAL:
1986-04-08 18:15:46 +00:00
return 1;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case DIV:
case MOD:
1986-04-11 11:57:19 +00:00
if (tpl->tp_fund & T_INTORCARD) {
1986-04-08 18:15:46 +00:00
if (expp->nd_left->nd_class == Value &&
expp->nd_right->nd_class == Value) {
cstbin(expp);
}
return 1;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case OR:
case AND:
1986-04-22 22:36:16 +00:00
case '&':
1986-04-08 18:15:46 +00:00
if (tpl == bool_type) {
if (expp->nd_left->nd_class == Value &&
expp->nd_right->nd_class == Value) {
cstbin(expp);
}
return 1;
}
errval = 3;
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case '=':
case '#':
1986-04-22 22:36:16 +00:00
case UNEQUAL:
1986-04-08 18:15:46 +00:00
case GREATEREQUAL:
case LESSEQUAL:
case '<':
case '>':
1986-04-22 22:36:16 +00:00
expp->nd_type = bool_type;
1986-04-08 18:15:46 +00:00
switch(tpl->tp_fund) {
1986-04-10 01:08:49 +00:00
case T_SET:
1986-04-08 18:15:46 +00:00
if (expp->nd_symb == '<' || expp->nd_symb == '>') {
break;
}
1986-04-10 01:08:49 +00:00
if (expp->nd_left->nd_class == Set &&
expp->nd_right->nd_class == Set) {
1986-04-11 11:57:19 +00:00
cstset(expp);
1986-04-10 01:08:49 +00:00
}
return 1;
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case T_INTEGER:
case T_CARDINAL:
case T_ENUMERATION: /* includes boolean */
case T_CHAR:
1986-04-11 11:57:19 +00:00
case T_INTORCARD:
1986-04-08 18:15:46 +00:00
if (expp->nd_left->nd_class == Value &&
expp->nd_right->nd_class == Value) {
cstbin(expp);
}
return 1;
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case T_POINTER:
1986-04-22 22:36:16 +00:00
if (expp->nd_symb == '=' ||
expp->nd_symb == UNEQUAL ||
expp->nd_symb == '#') return 1;
break;
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
case T_REAL:
1986-04-08 18:15:46 +00:00
return 1;
}
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
default:
assert(0);
}
switch(errval) {
case 1:
1986-04-18 17:53:47 +00:00
node_error(expp,"operator \"%s\": illegal operand type(s)", symbol2str(expp->nd_symb));
1986-04-08 18:15:46 +00:00
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case 3:
node_error(expp, "BOOLEAN type(s) expected");
break;
1986-04-18 17:53:47 +00:00
1986-04-10 01:08:49 +00:00
default:
assert(0);
1986-04-08 18:15:46 +00:00
}
return 0;
}
int
1986-04-10 01:08:49 +00:00
chk_uoper(expp)
1986-04-08 18:15:46 +00:00
register struct node *expp;
{
1986-04-10 01:08:49 +00:00
/* Check an unary operation.
1986-04-08 18:15:46 +00:00
*/
register struct type *tpr = expp->nd_right->nd_type;
1986-04-10 01:08:49 +00:00
if (tpr->tp_fund == T_SUBRANGE) tpr = tpr->next;
1986-04-08 18:15:46 +00:00
expp->nd_type = tpr;
switch(expp->nd_symb) {
case '+':
1986-04-11 11:57:19 +00:00
if (tpr->tp_fund & T_NUMERIC) {
1986-04-08 18:15:46 +00:00
expp->nd_token = expp->nd_right->nd_token;
FreeNode(expp->nd_right);
expp->nd_right = 0;
return 1;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case '-':
1986-04-11 11:57:19 +00:00
if (tpr->tp_fund & T_INTORCARD) {
1986-04-08 18:15:46 +00:00
if (expp->nd_right->nd_class == Value) {
cstunary(expp);
}
return 1;
1986-04-11 11:57:19 +00:00
}
else if (tpr->tp_fund == T_REAL) {
1986-04-08 18:15:46 +00:00
if (expp->nd_right->nd_class == Value) {
expp->nd_token = expp->nd_right->nd_token;
if (*(expp->nd_REL) == '-') {
expp->nd_REL++;
}
else expp->nd_REL--;
FreeNode(expp->nd_right);
expp->nd_right = 0;
}
return 1;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
case NOT:
1986-04-22 22:36:16 +00:00
case '~':
1986-04-08 18:15:46 +00:00
if (tpr == bool_type) {
if (expp->nd_right->nd_class == Value) {
cstunary(expp);
}
return 1;
}
break;
1986-04-18 17:53:47 +00:00
1986-04-09 18:14:49 +00:00
case '^':
1986-04-10 01:08:49 +00:00
if (tpr->tp_fund != T_POINTER) break;
1986-04-09 18:14:49 +00:00
expp->nd_type = tpr->next;
return 1;
1986-04-18 17:53:47 +00:00
1986-04-08 18:15:46 +00:00
default:
assert(0);
}
1986-04-11 11:57:19 +00:00
node_error(expp, "illegal operand for unary operator \"%s\"",
1986-04-08 18:15:46 +00:00
symbol2str(expp->nd_symb));
return 0;
}