Search in sources :

Example 1 with From

use of coreVisitors.From in project L42 by ElvisResearchGroup.

the class Methods method methods.

//  -methods(p,P0)=M1'..Mk'
//          p(P0)={interface? implements Ps Ms} 
public List<MethodWithType> methods(Ast.Path p0) {
    if (p0.isPrimitive()) {
        return Collections.emptyList();
    }
    Program p = this;
    ClassB cb0 = p.extractClassB(p0);
    List<Ast.Path> ps = cb0.getSuperPaths();
    //          P1..Pn=collect(p,Ps[from P0]), error unless forall i, p(Pi) is an interface
    List<Ast.Path> p1n = collect(p, Map.of(pi -> From.fromP(pi, p0), ps));
    List<ClassB> cb1n = Map.of(pi -> p.extractClassB(pi), p1n);
    {
        int i = -1;
        for (ClassB cbi : cb1n) {
            i++;
            if (!cbi.isInterface()) {
                throw new ast.ErrorMessage.NonInterfaceImplements(p0, p1n.get(i));
            }
        }
    }
    //          ms1..msk={ms|p(Pi)(ms) is defined}
    HashMap<Ast.MethodSelector, List<ClassB.Member>> ms1k = new LinkedHashMap<>();
    for (ClassB.Member mij : cb0.getMs()) {
        mij.match(nc -> null, mi -> add(true, ms1k, mi.getS(), From.from(mij, p0)), mt -> add(true, ms1k, mt.getMs(), From.from(mij, p0)));
    }
    for (Ast.Path pi : p1n) {
        //call the memoized methods
        for (ClassB.Member mij : p.methods(pi)) {
            mij.match(nc -> null, mi -> add(false, ms1k, mi.getS(), mij), mt -> add(false, ms1k, mt.getMs(), mij));
        }
    }
    //            such that p(Pj)(ms)=mh e? //no refine
    for (Entry<MethodSelector, List<Member>> ei : ms1k.entrySet()) {
        if (!exactly1NoRefine(ei.getValue())) {
            throw new ast.ErrorMessage.NotExaclyOneMethodOrigin(p0, ei.getKey(), ei.getValue());
        }
    }
    List<ClassB.MethodWithType> ms = new ArrayList<>();
    //            for the smallest j in 1..k such that methods(p,Pj)(msi) of form refine? mh
    for (Entry<MethodSelector, List<Member>> ei : ms1k.entrySet()) {
        List<Member> memsi = ei.getValue();
        if (memsi.get(0) != null && memsi.get(0) instanceof MethodWithType) {
            ms.add((MethodWithType) memsi.get(0));
            continue;
        }
        ClassB.MethodImplemented mem0 = (ClassB.MethodImplemented) memsi.get(0);
        for (Member mj : memsi) {
            // 0 will fail instanceof
            if (mj == null || !(mj instanceof MethodWithType)) {
                continue;
            }
            //    Mi'=Mi
            if (mem0 != null) {
                mj = mj.withInner(mem0.getE());
            }
            ms.add(addRefine((MethodWithType) mj));
            break;
        }
    }
    return ms;
}
Also used : From(coreVisitors.From) Ast(ast.Ast) Path(ast.Ast.Path) Map(tools.Map) Set(java.util.Set) HashMap(java.util.HashMap) Member(ast.ExpCore.ClassB.Member) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Collectors(java.util.stream.Collectors) MethodSelector(ast.Ast.MethodSelector) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ClassB(ast.ExpCore.ClassB) Entry(java.util.Map.Entry) Collections(java.util.Collections) MethodSelector(ast.Ast.MethodSelector) ArrayList(java.util.ArrayList) Member(ast.ExpCore.ClassB.Member) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) List(java.util.List) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB) Path(ast.Ast.Path) Ast(ast.Ast) Path(ast.Ast.Path) MethodWithType(ast.ExpCore.ClassB.MethodWithType)

Example 2 with From

use of coreVisitors.From in project L42 by ElvisResearchGroup.

the class Translator method useFrom.

//this should take a class, strip out nested and 'from' it so that it is as at top level
static ClassB useFrom(ClassB ct, Path p) {
    ArrayList<Member> ms = new ArrayList<Member>();
    for (Member m : ct.getMs()) {
        m.match(nc -> null, mi -> {
            throw Assertions.codeNotReachable();
        }, mt -> ms.add(From.from(mt, p)));
    }
    //for(PathMwt pmwt:ct.getStage().getInherited()){
    for (PathMwt pmwt : Collections.<PathMwt>emptyList()) {
        if (Functions.getIfInDom(ms, pmwt.getMwt().getMs()).isPresent()) {
            continue;
        }
        ms.add(From.from(pmwt.getMwt(), p));
    }
    List<Path> sup = tools.Map.of(ti -> (Path) From.fromP(ti.getPath(), p), ct.getSupertypes());
    //tools.Map.of(pi->(Path)From.fromP(pi,p),ct.getStage().getInheritedPaths());
    List<Path> supAll = sup;
    ClassB res = ct.withMs(ms).withSupertypes(tools.Map.of(pi -> pi.toImmNT(), sup));
    return res;
}
Also used : Path(ast.Ast.Path) From(coreVisitors.From) Stage(ast.Ast.Stage) Arrays(java.util.Arrays) Program(programReduction.Program) Ast(ast.Ast) Configuration(facade.Configuration) Type(ast.Ast.Type) HashMap(java.util.HashMap) Assertions(tools.Assertions) ArrayList(java.util.ArrayList) CompilationError(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.CompilationError) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ClassB(ast.ExpCore.ClassB) InterfaceHeader(ast.Ast.InterfaceHeader) Map(java.util.Map) Facade(reduction.Facade) PathMwt(ast.Util.PathMwt) MapClassLoader(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader) Method(java.lang.reflect.Method) IsCompiled(coreVisitors.IsCompiled) NestedClass(ast.ExpCore.ClassB.NestedClass) Files(java.nio.file.Files) Path(ast.Ast.Path) Doc(ast.Ast.Doc) Phase(ast.ExpCore.ClassB.Phase) IOException(java.io.IOException) ExpCore(ast.ExpCore) Member(ast.ExpCore.ClassB.Member) Functions(auxiliaryGrammar.Functions) InvocationTargetException(java.lang.reflect.InvocationTargetException) Util(ast.Util) Consumer(java.util.function.Consumer) List(java.util.List) EmptyProgram(programReduction.Program.EmptyProgram) Paths(java.nio.file.Paths) Optional(java.util.Optional) SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Timer(profiling.Timer) InMemoryJavaCompiler(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler) Collections(java.util.Collections) L42(facade.L42) ArrayList(java.util.ArrayList) Member(ast.ExpCore.ClassB.Member) PathMwt(ast.Util.PathMwt) ClassB(ast.ExpCore.ClassB)

Aggregations

Ast (ast.Ast)2 Path (ast.Ast.Path)2 ClassB (ast.ExpCore.ClassB)2 Member (ast.ExpCore.ClassB.Member)2 From (coreVisitors.From)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Doc (ast.Ast.Doc)1 InterfaceHeader (ast.Ast.InterfaceHeader)1 MethodSelector (ast.Ast.MethodSelector)1 Stage (ast.Ast.Stage)1 Type (ast.Ast.Type)1 ExpCore (ast.ExpCore)1 MethodWithType (ast.ExpCore.ClassB.MethodWithType)1 NestedClass (ast.ExpCore.ClassB.NestedClass)1 Phase (ast.ExpCore.ClassB.Phase)1