use of ast.ExpCore.ClassB.MethodWithType in project L42 by ElvisResearchGroup.
the class ErrorFormatter method printMembers.
private static void printMembers(ClassB top) {
for (Member m : top.getMs()) {
if (!(m instanceof MethodWithType)) {
continue;
}
MethodWithType mwt = (MethodWithType) m;
mwt = mwt.withDoc(Doc.empty());
mwt = mwt.with_inner(Optional.empty());
String txt = sugarVisitors.ToFormattedText.of(mwt);
txt = txt.replace("{", "");
txt = txt.replace("}", "");
txt = txt.replace("\n", " ");
System.out.print(" " + txt + "\n");
/* System.out.print(" "+mwt.getMt().getReturnType());
System.out.print(" "+mwt.getMs().getName());
System.out.print("(");
{int i=-1;for(String n:mwt.getMs().getNames()){i+=1;
System.out.print(mwt.getMt().getTs()
}}
System.out.print(")\n");
*/
}
}
use of ast.ExpCore.ClassB.MethodWithType 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.ExpCore.ClassB.MethodWithType in project L42 by ElvisResearchGroup.
the class TsLibrary method coherentF.
static boolean coherentF(Program p, MethodWithType ck, MethodWithType mwt) {
MethodType mt = mwt.getMt();
Mdf m = mt.getMdf();
if (mwt.getMs().getUniqueNum() != ck.getMs().getUniqueNum()) {
return false;
}
// internally do noFwd
Type Ti = _extractTi(ck, mwt.getMs().getName());
if (Ti == null) {
return false;
}
//if(m==Mdf.Class){return false;}
if (m == Mdf.Readable || m == Mdf.Immutable) {
//getter
if (!mt.getTs().isEmpty()) {
return false;
}
Type Ti_ = TypeManipulation._toRead(Ti);
//p|-toRead(Ti)<=T
if (Ti_ == null) {
return false;
}
if (null != TypeSystem.subtype(p, Ti_, mt.getReturnType())) {
return false;
}
return true;
}
if (m != Mdf.Mutable) {
return false;
}
//exposer/setter
if (mt.getTs().isEmpty()) {
//exposer
Type Ti_ = TypeManipulation.capsuleToLent(Ti);
//p|-capsuleToLent(Ti)<=T
if (Ti_ == null) {
return false;
}
if (null != TypeSystem.subtype(p, Ti_, mt.getReturnType())) {
return false;
}
return true;
}
//setter refine? mut method Void m[n?](T that)
if (!mt.getReturnType().equals(Type.immVoid)) {
return false;
}
if (mt.getTs().size() != 1) {
return false;
}
if (!mwt.getMs().getNames().get(0).equals("that")) {
return false;
}
Type T = mt.getTs().get(0);
if (null != TypeSystem.subtype(p, Ti, T)) {
return false;
}
if (Ti.getMdf() == Mdf.Readable) {
Mdf mT = T.getMdf();
if (mT != Mdf.Capsule && mT != Mdf.Immutable) {
return false;
}
}
return true;
}
use of ast.ExpCore.ClassB.MethodWithType 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.ExpCore.ClassB.MethodWithType in project L42 by ElvisResearchGroup.
the class TsLibrary method coherentK.
//coherent(n?,p,T1 x1..Tn xn,
//refine? class method T m[n?] (T1' x1..Tn' xn) exception _)
//where
//p|- This0 <=T.P and p|-Ti'<=fwd Ti
//T.mdf!=class and if T.mdf in {imm,capsule}, mut notin (T1..Tn).mdfs
static boolean coherentK(Program p, MethodWithType ck) {
MethodType mt = ck.getMt();
if (mt.getMdf() != Mdf.Class) {
return false;
}
Type rt = mt.getReturnType();
if (null != TypeSystem.subtype(p, Path.outer(0), rt.getPath())) {
return false;
}
Mdf m = rt.getMdf();
if (m == Mdf.Class) {
return false;
}
boolean immOrC = (m == Mdf.Immutable || m == Mdf.Capsule);
boolean lentOrR = (m == Mdf.Lent || m == Mdf.Readable);
int allowedR = lentOrR ? 1 : 0;
for (Type ti : mt.getTs()) {
Mdf mi = ti.getMdf();
if (mi == Mdf.Lent) {
return false;
}
if (mi == Mdf.Readable) {
if (allowedR == 0) {
return false;
}
allowedR -= 1;
}
if (immOrC & (mi == Mdf.Mutable || mi == Mdf.MutableFwd)) {
return false;
}
}
return true;
}
Aggregations