1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*
Copyright (C) 2000-2005 The PARI group.
This file is part of the GP2C package.
PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.
Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "header.h"
int gentypefuncdesc(int n, gpfunc *gp);
void
inittrans(int n,int p)
{
int i;
for(i=0;i<s_GPtype.n;i++)
{
if (is_subtype(i,n))
typemax[p][i]=typemax[i][p]=p;
if (is_subtype(p,i))
typemax[i][n]=typemax[n][i]=i;
}
}
int
initmax(int n, int p)
{
int i,m=Gnotype;
for(i=0;i<s_GPtype.n;i++)
if (is_subtype(n,i) && is_subtype(p,i))
if(m==Gnotype || is_subtype(i,m))
m=i;
return m;
}
void outputtype(FILE *fout)
{
int i,j;
fprintf(fout," |");
for(i=0;i<s_GPtype.n;i++)
fprintf(fout,"%-8s",GPname(i));
fprintf(fout,"\n--------+");
for(i=0;i<s_GPtype.n;i++)
fprintf(fout,"--------");
fprintf(fout,"\n");
for(i=0;i<s_GPtype.n;i++)
{
/*No, this is not strange smileys*/
fprintf(fout,"%-8s|",GPname(i));
for(j=0;j<s_GPtype.n;j++)
{
fprintf(fout,"%-8s",GPname(typemax[i][j]));
}
fprintf(fout,"\n");
}
}
void inittype(void)
{
int n;
int i,j;
int Gnbtype=s_GPtype.n;
gpfunc *gp=lfunc+findfuncdesc("_type_preorder");
gpdesc *dsc=gp->dsc;
typemax=(int **) calloc(Gnbtype,sizeof(*typemax));
for(i=0;i<Gnbtype;i++)
typemax[i]=(int *) calloc(Gnbtype,sizeof(**typemax));
for(i=0;i<Gnbtype;i++)
for(j=0;j<Gnbtype;j++)
typemax[i][j]=(i==j)?i:Gnotype;
for(n=0; n<dsc->nb; n++)
{
gpdescarg *ga=dsc->a+n;
int oldt=-1;
for (i=0; i < ga->nargs; i++)
{
descargatom *a=ga->args+i;
int t=a->type;
if (a->t!=AAtype)
die(err_desc,"unexpected atom in _type_preorder");
if (oldt>=0)
typemax[t][oldt]=typemax[oldt][t]=t;
oldt=t;
}
}
for(i=0;i<Gnbtype;i++)
for(j=0;j<Gnbtype;j++)
if (typemax[i][j]==j)
inittrans(i,j);
for(i=0;i<Gnbtype;i++)
for(j=0;j<Gnbtype;j++)
if (typemax[i][j]==Gnotype)
typemax[i][j]=typemax[j][i]=initmax(i,j);
}
int gentypedeclaration(context *fc)
{
int i;
int mode=0;
for(i=0;i<fc->s.n;i++)
{
ctxvar *v=fc->c+i;
int val=v->initval;
if (val!=-1)
{
int t=gentype(val);
int vt=vartype(*v);
if (v->flag&(1<<Cauto) && vt!=t && is_subtype(vt,t))
{
vartype(*v)=t;
lastpass++;
}
mode|=tree[val].m;
}
}
return mode;
}
void gentypedeffunc(int n)
{
int funcid=tree[n].x;
int seq=tree[n].y;
const char *name=entryname(funcid);
int savcf;
int tf;
gpfunc *gp;
context *fc;
/*save current function*/
savcf=currfunc;
/*get function number and context*/
currfunc=findfunction(name);
gp=lfunc+currfunc;
/*gentype for seq*/
gentype(seq);
/*gentype may change functype(*gp)*/
tf=functype(*gp);
fc=block+gp->user->bl;
if (tf==Gnegbool) tf=functype(*gp)=Gbool;
if (tf==Glg) tf=functype(*gp)=Gsmall;
if (tf==Gstr) tf=functype(*gp)=Ggenstr;
if ((funcmode(*gp)^tree[seq].m)&((1<<Mprec)|(1<<Msidef)))
{
lastpass++;
if(debug==2)
fprintf(stderr,"%s mode %d now %d\n",gp->gpname,funcmode(*gp),tree[seq].m);
/*funcmode(*gp)|(tree[seq].m&((1<<Mprec)|(1<<Msidef))));*/
funcmode(*gp)|=tree[seq].m&((1<<Mprec)|(1<<Msidef));
}
currfunc=savcf;
/*create type*/
tree[n].t=Gvoid;
tree[n].m=(1<<Msidef);
}
void gentypeblock(int n)
{
int seq=tree[n].y;
int savc;
int mode;
context *fc=block+tree[n].x;
/*save new context address and gentype for seq*/
savc=s_ctx.n;
/*push context*/
pushctx(fc);
/*gentype for local var*/
mode=gentypedeclaration(fc);
/*gentype for seq*/
gentype(seq);
/*save context*/
copyctx(savc,fc);
/*restore current context*/
restorectx(savc);
/*create type*/
if (fc->ret==-1)
tree[n].t=Gvoid;
else
{
int v=getvarerr(fc->ret);
tree[n].t=vartype(ctxstack[v]);
}
tree[n].m=mode|(tree[seq].m&MODHERIT);
}
int gentype(int n)
{
int t,tx,ty;
int mx,my;
int x,y,c;
gpfunc *gp;
int nf;
if (n<0)
return Gnotype;
x=tree[n].x;
y=tree[n].y;
if (tree[n].f<FneedENTRY)
{
tx=gentype(x);
mx=(x>=0)?tree[x].m:0;
ty=gentype(y);
my=(y>=0)?tree[y].m:0;
}
else
{
tx=-1; mx=-1;
ty=-1; my=-1;
}
switch(tree[n].f)
{
case Fseq:
if (x<0 || y<0)
die(n,"internal error in gentype");
if (mx&(1<<Mterm))
{
tree[n].t=Gvoid;/*the seq end here*/
tree[n].m=mx&MODHERIT;
}
else
{
tree[n].t=ty;/*for `if(1,a;b)' and others construct */
tree[n].m=(my|mx)&MODHERIT;
}
break;
case Fmatrixelts:
tree[n].t=Gnotype;
tree[n].m=(mx|my)&MODHERIT;
break;
case Fmatrixlines:
tree[n].t=Gnotype;
tree[n].m=(mx|my)&MODHERIT;
break;
case Faffect:
{
int z,lx;
ctxvar *v;
tree[n].t=tx;/*Probably it should be ty*/
tree[n].m=(mx|my)&MODHERIT;
z=detag(tree[n].x);
lx=getlvaluerr(z);
v=ctxstack+getvarerr(lx);
if (!is_subtype(ty,tx) && tree[z].f==Fentry)
{
if (!(v->flag&(1<<Cauto)))
{
if ((v->flag&(1<<Cuser)))
warning(n,"Type mismatch: %s!>=%s",GPname(tx),GPname(ty));
}
else
{
vartype(*v)=typemax[tx][ty];
lastpass++;
if (debug)
fprintf(stderr,"casting %s from %s to %s now %s.\n",
varstr(*v),GPname(tx),GPname(ty),GPname(vartype(*v)));
tree[n].t=ty;
tx=ty;
tree[x].t=tx;
}
}
if ((tree[z].f!=Fentry || (v->flag&(1<<Ccompo)))
&& (tree[y].f==Fentry || tree[y].f==Faffect))
tree[y].m|=(1<<Mcopy);
if (v->flag&(1<<Ccompo))
tree[n].m|=(1<<Mcopy);
if (tx!=Gvoid)
tree[n].m|=(1<<Msidef);
tree[n].m|=(tree[z].m&(1<<Mlong))|(1<<Mparens);
}
break;
case Fconst:
tree[n].m=0;
switch(value[x].type)
{
case CSTsmall:
tree[n].t=Gsmall;
break;
case CSTsmallreal:
tree[n].t=Greal;
tree[n].m=(1<<Mprec);
break;
case CSTint:
tree[n].t=Gint;
break;
case CSTreal:
tree[n].t=Greal;
if (FC_const_real>=0)
tree[n].m=(1<<Mprec);
break;
case CSTstr:
tree[n].t=Gstr;
break;
}
break;
case Fsmall:
tree[n].t=Gsmall;
tree[n].m=(1<<Msimple);
break;
case Flistarg:
tree[n].t=Gnotype;
tree[n].m=(mx|my)&MODHERIT;
break;
case Ftag:
tree[n].t=y;
gentype(x);
tree[n].m=tree[x].m&MODHERIT;
break;
case Frefarg:
gentype(x);
tree[n].t=tree[x].t;
tree[n].m=(tree[x].m&~(1<<Mlong));
break;
case Fentry:
c=getvar(n);
if (c>=0)
{
ctxvar *v=ctxstack+c;
tree[n].t=vartype(*v);
tree[n].m=(1<<Msimple)|(1<<Mvar);
if (v->flag&(1<<Ccompo))
tree[n].m|=(1<<Mcopy);
}
else
die(n,"Internal error: extra variable `%s' in gentype",value[x].val.str);
break;
case Fentryfunc:
nf=findfunction(entryname(n));
if (nf>=0)
{
gp=lfunc+nf;
if (functype(*gp)==Gnotype && gp->spec>0)
/* is a special function ? */
tree[n].t=gentypefuncspec(n,gp);
else if (gp->dsc && (t=gentypefuncdesc(n,gp))!=Gnotype)
/* else has the function a description ?*/
tree[n].t=t;
else
/* else use the function a type */
{
gentype(y);
tree[n].t=functype(*gp);
my=(y==-1)?0:tree[y].m;
tree[n].m=(funcmode(*gp)&(~(1<<Msemicomma)))|(my&MODHERIT);
}
}
else
{
gentype(y);
my=(y==-1)?0:tree[y].m;
tree[n].t=Ggen;
tree[n].m=my&MODHERIT;
warning(n,"function prototype is unknown");
}
break;
case Fdeffunc:
gentypedeffunc(n);
break;
case Fblock:
gentypeblock(n);
break;
case Fgnil:
if (n!=GNOARG && n!=GNIL)
{
tree[n].m=0;
tree[n].t=Gvoid;
warning(n,"Internal warning: new gnil.");
}
break;
default:
die(n,"Internal error: Incorrect node %s in gentype",funcname(tree[n].f));
}
return tree[n].t;
}
|