use of ast.Ast.Path in project L42 by ElvisResearchGroup.
the class ErrorFormatter method errorFormat.
public static String errorFormat(Object obj, ArrayList<Ast.Position> ps) {
if (obj instanceof ast.Ast.HasPos) {
ps.add(((ast.Ast.HasPos) obj).getP());
}
if (obj instanceof String) {
return (String) obj;
}
if (obj instanceof Integer) {
return obj.toString();
}
if (obj instanceof Expression) {
Ast.Position p = CollapsePositions.of((Expression) obj);
ps.add(p);
return ToFormattedText.ofCompact((Expression) obj);
}
if (obj instanceof ExpCore) {
//thanks to Path, this have to be after Expression
Ast.Position p = CollapsePositions.of((ExpCore) obj);
ps.add(p);
ExpCore exp = (ExpCore) obj;
Expression expression = exp.accept(new InjectionOnSugar());
return errorFormat(expression, ps);
}
if (obj instanceof Ast.MethodSelector) {
return formatSelectorCompact((Ast.MethodSelector) obj);
}
if (obj instanceof ExpCore.ClassB.MethodWithType) {
return ToFormattedText.of((MethodWithType) obj).trim().replace("\n", " ");
}
// }
if (obj instanceof Reporter) {
return ((Reporter) obj).toReport(ps);
}
if (obj instanceof Collection<?>) {
Collection<?> c = (Collection<?>) obj;
if (c.size() == 0) {
return "[]";
}
if (c.size() == 1) {
return "[" + errorFormat(c.iterator().next(), ps) + "]";
}
String res = "[\n";
for (Object o : c) {
res += " " + errorFormat(o, ps) + "\n";
}
return res + " ]\n";
}
if (obj instanceof Ast.Type) {
return obj.toString();
}
if (obj instanceof ClassB.Member) {
return ToFormattedText.of((ClassB.Member) obj);
}
//if(obj instanceof Expression){return ToFormattedText.of((Expression)obj);}
if (obj instanceof Expression.ClassB.Member) {
return ToFormattedText.of((Expression.ClassB.Member) obj);
}
if (obj instanceof java.nio.file.Path) {
return obj.toString();
}
if (obj instanceof Ast.Position) {
return obj.toString();
}
if (obj instanceof HashMap) {
return obj.toString();
}
if (obj instanceof Collection) {
return obj.toString();
}
if (obj instanceof ast.Util.PathMwt) {
return obj.toString();
}
if (obj instanceof ast.Ast.C) {
return obj.toString();
}
if (obj instanceof Boolean) {
return obj.toString();
}
if (obj instanceof ast.Ast.Path) {
return obj.toString();
}
return "unknown kind " + obj.getClass().getName();
}
use of ast.Ast.Path in project L42 by ElvisResearchGroup.
the class TsLibrary method memberMethod.
default default TOutM memberMethod(TIn in, List<Type> supertypes, MethodWithType mwt) {
//(member method)
//Phase| p| Ps |-M ~> M'
// where
// M =refine? mdf method T m(T1 x1 .. Tn xn)exceptions Ps0 e?
// M'=refine? mdf method T m(T1 x1 .. Tn xn)exceptions Ps0 e?'
// G=this:mdf This0,x1:T1,..,xn:Tn
// if e?=e then
// Typed| p| G |- e ~> e?':_ <=fwd% T | empty;Ps1
// forall P1 in Ps1 exists P0 in Ps0 such that p|-P1<=P0
//else
// e?=e?'
MethodWithType mwt1;
if (!mwt.get_inner().isPresent()) {
mwt1 = mwt;
} else {
TIn newIn = in.freshGFromMt(mwt);
TOut out = type(newIn);
if (!out.isOk()) {
return out.toError();
}
for (Path P1 : out.toOk().exceptions) {
//exists P0 in Ps0 such that p|-P1<=P0
boolean ok = mwt.getMt().getExceptions().stream().anyMatch(P0 -> null == TypeSystem.subtype(newIn.p, P1, P0.getPath()));
if (!ok) {
return new TErr(newIn, "", P1.toImmNT(), ErrorKind.MethodLeaksExceptions);
}
}
if (!out.toOk().returns.isEmpty()) {
return new TErr(newIn, "", out.toOk().returns.get(0), ErrorKind.MethodLeaksReturns);
}
mwt1 = mwt.with_inner(Optional.of(out.toOk().annotated));
}
if (mwt.getMt().isRefine()) {
// /or method declares an exception (P) which is not a subtype of ancestor exceptions
for (Type t : supertypes) {
Path P = t.getPath();
ClassB cbP = in.p.extractClassB(P);
MethodWithType mwtP = (MethodWithType) cbP._getMember(mwt.getMs());
if (mwtP == null) {
continue;
}
MethodType M0 = From.from(mwtP.getMt(), P);
ErrorKind kind = TypeSystem.subtype(in.p, mwt.getMt().getReturnType(), M0.getReturnType());
if (kind != null) {
return new TErr(in, "", P.toImmNT(), ErrorKind.InvalidImplements);
}
{
int i = -1;
for (Type Ti : mwt.getMt().getTs()) {
i += 1;
Type T1i = M0.getTs().get(i);
if (!in.p.equiv(Ti, T1i)) {
return new TErr(in, "", P.toImmNT(), ErrorKind.InvalidImplements);
}
}
}
for (Type Pi : mwt.getMt().getExceptions()) {
//exists Pj in Ps' such that p |- Pi<=Pj
boolean ok = M0.getExceptions().stream().anyMatch(Pj -> null == TypeSystem.subtype(in.p, Pi.getPath(), Pj.getPath()));
if (!ok) {
return new TErr(in, "", P.toImmNT(), ErrorKind.InvalidImplements);
}
}
}
}
return new TOkM(mwt1);
}
use of ast.Ast.Path in project L42 by ElvisResearchGroup.
the class TsMCall method tsMCall.
default default TOut tsMCall(TIn in, MCall s) {
Type _rec = GuessTypeCore._of(in.p, in, s.getInner());
if (_rec == null) {
return new TErr(in, "", null, ErrorKind.SelectorNotFound);
}
Path rec = _rec.getPath();
if (rec.isPrimitive()) {
return new TErr(in, "MethodCall on primitive type", null, ErrorKind.SelectorNotFound);
}
MethodType mDec = AlternativeMethodTypes._mtDeclared(in.p, rec, s.getS());
if (mDec == null) {
return new TErr(in, "", null, ErrorKind.SelectorNotFound);
}
Type ret = mDec.getReturnType();
ErrorKind kind = TypeSystem.subtype(in.p, ret.getPath(), in.expected.getPath());
if (kind != null) {
return new TErr(in, "", ret, kind);
}
List<MethodType> mTypes = AlternativeMethodTypes.types(mDec);
MethodType mType = AlternativeMethodTypes._firstMatchReturn(in.p, in.expected, mTypes);
if (mType == null) {
return new TErr(in, "", ret, ErrorKind.NotSubtypeMdf);
}
//unachievable return type (T) for method (P.ms) [line numbers of expression and declaration]
//2 type all the parameters with mutOnlyToLent(Ts) //we may include mutOnlyToLent in the computation of the MTypes, instead of in the loop below
List<TOk> resp = new ArrayList<>();
List<Type> computed = new ArrayList<>();
List<ExpCore> annotated = new ArrayList<>();
ExpCore e0 = s.getInner();
Type t0 = new Type(mType.getMdf(), rec, Doc.empty());
TOut _res0 = type(in.withE(e0, TypeManipulation.mutOnlyToLent(t0)));
if (!_res0.isOk()) {
return improveReceiverError(in, t0, _res0.toError());
}
TOk res0 = _res0.toOk();
Mdf recMdf = _res0.toOk().computed.getMdf();
{
int i = -1;
for (ExpCore ei : s.getEs()) {
i += 1;
Type ti = mType.getTs().get(i);
TOut _resi = type(in.withE(ei, TypeManipulation.mutOnlyToLent(ti)));
if (!_resi.isOk()) {
return _resi.toError();
}
resp.add(_resi.toOk());
computed.add(_resi.toOk().computed);
annotated.add(_resi.toOk().annotated);
}
}
MethodType computedMt = new MethodType(false, recMdf, computed, in.expected, Collections.emptyList());
MethodType mTypeRev = AlternativeMethodTypes._bestMatchMtype(in.p, computedMt, mTypes);
if (mTypeRev != null) {
MCall resM = new MCall(res0.annotated, s.getS(), s.getDoc(), annotated, s.getP());
TOk res = new TOk(in, resM, mTypeRev.getReturnType());
// Trs[with r in resp (use[r.Tr])].collapse())
res = res.trUnion(res0);
for (TOk oki : resp) {
res = res.trUnion(oki);
}
return res;
}
//3 if there is no matching method, we may need to retype some mut
//in capsule caused by mvp:
//it is not over if there is a mathing method type with mutToCapsule(result param types)
//tsToCaps=Ts[with r in resp (use[mutToCapsule(r.T)])]
//mTypeMVP=_bestMatchMtype(tsToCaps,TSIn.T,mTypes)
List<Type> tsToCaps = new ArrayList<>();
for (TOk r : resp) {
Mdf m = r.computed.getMdf();
if (m == Mdf.MutableFwd || m == Mdf.MutablePFwd) {
return new TErr(in, "impossible to search for mvp since mdf " + m, mTypes.get(0).getReturnType(), ErrorKind.NotSubtypeClass);
}
Type nt = TypeManipulation.mutToCapsule(r.computed);
tsToCaps.add(nt);
}
computedMt = computedMt.withTs(tsToCaps).withMdf(TypeManipulation.mutToCapsule(computedMt.getMdf()));
MethodType mTypeMVP = AlternativeMethodTypes._bestMatchMtype(in.p, computedMt, mTypes);
if (mTypeMVP == null) {
return new TErr(in, "mvp candidate notfound", mTypes.get(0).getReturnType(), ErrorKind.NotSubtypeClass);
}
//To be happy, we can retype the obtained mut parameters into expected capsule
TOut _newRes0 = innerMVPRetype(res0, t0.withMdf(mTypeMVP.getMdf()));
if (!_newRes0.isOk()) {
return _newRes0;
}
TOk newRes0 = _newRes0.toOk();
List<TOk> newResp = new ArrayList<>();
{
int i = -1;
for (TOk ri : resp) {
i += 1;
Type ti = mTypeMVP.getTs().get(i);
TOut outi = innerMVPRetype(ri, ti);
if (!outi.isOk()) {
return outi.toError();
}
newResp.add(outi.toOk());
}
}
//return res=makeMCallOK(TSIn,respMVP,mTypeMVP)
MCall resM = new MCall(newRes0.annotated, s.getS(), s.getDoc(), Map.of(r -> r.annotated, newResp), s.getP());
TOk res = new TOk(in, resM, mTypeMVP.getReturnType());
// Trs[with r in resp (use[r.Tr])].collapse())
res = res.trUnion(newRes0);
for (TOk oki : newResp) {
res = res.trUnion(oki);
}
return res;
}
use of ast.Ast.Path in project L42 by ElvisResearchGroup.
the class GuessTypeCore method visit.
@Override
public Type visit(MCall s) {
Type former = s.getInner().accept(this);
if (former == null) {
return null;
}
Path path = former.getPath();
assert path != null;
List<MethodSelectorX> msl = new ArrayList<>();
MethodSelectorX msx = new MethodSelectorX(s.getS(), "");
msl.add(msx);
MethodWithType meth = (MethodWithType) p.extractClassB(path)._getMember(s.getS());
if (meth == null) {
return null;
}
return (Type) From.fromT(meth.getMt().getReturnType(), path);
}
use of ast.Ast.Path in project L42 by ElvisResearchGroup.
the class TOkK method trCapture.
public This trCapture(Program p, On k) {
//(Ts;Ps).capture(catch return P x e)={T| T in Ts, not p|-T.P<=P};Ps
if (k.getKind() == SignalKind.Error) {
return self();
}
This result = trClean();
if (k.getKind() == SignalKind.Exception) {
result.exceptions = new ArrayList<>();
for (Path pi : exceptions) {
if (null != TypeSystem.subtype(p, pi, k.getT().getPath())) {
result.exceptions.add(pi);
}
}
}
//otherwise, is return
result.returns = new ArrayList<>();
for (Type ti : returns) {
if (null != TypeSystem.subtype(p, ti.getPath(), k.getT().getPath())) {
result.returns.add(ti);
}
}
return result;
}
Aggregations