Search in sources :

Example 1 with C

use of ast.Ast.C in project L42 by ElvisResearchGroup.

the class RefreshUniqueNames method refreshTopLevel.

public static ClassB refreshTopLevel(ClassB e) {
    HashMap<Long, Long> map = new HashMap<>();
    return (ClassB) e.accept(new coreVisitors.CloneVisitor() {

        protected MethodSelector liftMs(MethodSelector ms) {
            return ms.withUniqueNum(newN(map, ms.getUniqueNum()));
        }

        public ClassB.NestedClass visit(ClassB.NestedClass nc) {
            long newN = newN(map, nc.getName().getUniqueNum());
            return super.visit(nc.withName(nc.getName().withUniqueNum(newN)));
        //I need to collect the DECLARED C,ms and those are the only that I need to refresh.
        //refresh all can work only at top level
        }

        @Override
        protected Path liftP(Path s) {
            if (s.isPrimitive()) {
                return s;
            }
            List<C> cs = s.getCBar();
            List<C> newCs = new ArrayList<>();
            for (C c : cs) {
                newCs.add(c.withUniqueNum(newN(map, c.getUniqueNum())));
            }
            return Path.outer(s.outerNumber(), newCs);
        }
    });
}
Also used : Path(ast.Ast.Path) MethodSelector(ast.Ast.MethodSelector) C(ast.Ast.C) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NestedClass(ast.Expression.ClassB.NestedClass) ClassB(ast.ExpCore.ClassB)

Example 2 with C

use of ast.Ast.C in project L42 by ElvisResearchGroup.

the class RefreshUniqueNames method refresh.

@SuppressWarnings("unchecked")
public static <T extends ExpCore> T refresh(T e) {
    HashMap<Long, Long> map = new HashMap<>();
    //load up maps
    e.accept(new coreVisitors.PropagatorVisitor() {

        public void visit(ClassB.MethodWithType mwt) {
            MethodSelector ms = mwt.getMs();
            newN(map, ms.getUniqueNum());
            super.visit(mwt);
            return;
        }

        public void visit(ClassB.NestedClass nc) {
            newN(map, nc.getName().getUniqueNum());
            super.visit(nc);
        }
    });
    return (T) e.accept(new coreVisitors.CloneVisitor() {

        protected MethodSelector liftMs(MethodSelector ms) {
            return ms.withUniqueNum(mappedN(map, ms.getUniqueNum()));
        }

        public ClassB.NestedClass visit(ClassB.NestedClass nc) {
            C name = nc.getName().withUniqueNum(mappedN(map, nc.getName().getUniqueNum()));
            return super.visit(nc.withName(name));
        }

        @Override
        protected Path liftP(Path s) {
            if (s.isPrimitive()) {
                return s;
            }
            List<C> cs = s.getCBar();
            List<C> newCs = new ArrayList<>();
            for (C c : cs) {
                newCs.add(c.withUniqueNum(mappedN(map, c.getUniqueNum())));
            }
            return Path.outer(s.outerNumber(), newCs);
        }
    });
}
Also used : Path(ast.Ast.Path) MethodWithType(ast.Expression.ClassB.MethodWithType) MethodSelector(ast.Ast.MethodSelector) C(ast.Ast.C) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NestedClass(ast.Expression.ClassB.NestedClass) NestedClass(ast.Expression.ClassB.NestedClass) ClassB(ast.ExpCore.ClassB)

Example 3 with C

use of ast.Ast.C in project L42 by ElvisResearchGroup.

the class Errors42 method checkExistsPathMethod.

static Member checkExistsPathMethod(ClassB cb, List<Ast.C> path, Optional<MethodSelector> ms) {
    try {
        //used in closures
        Boolean[] isPrivateRef = new Boolean[] { false };
        for (C c : path) {
            if (c.isUnique()) {
                isPrivateRef[0] = true;
            }
        }
        ClassB cbi = cb.getClassB(path);
        Boolean[] isPrivateMeth = new Boolean[] { false };
        boolean absentMeth = false;
        if (ms.isPresent()) {
            Optional<Member> meth = Functions.getIfInDom(cbi.getMs(), ms.get());
            absentMeth = !meth.isPresent();
            if (meth.isPresent()) {
                meth.get().match(nc -> {
                    throw Assertions.codeNotReachable();
                }, mi -> {
                    return null;
                }, mt -> {
                    if (mt.getMs().isUnique()) {
                        isPrivateMeth[0] = true;
                    }
                    return null;
                });
                if (!isPrivateMeth[0]) {
                    return meth.get();
                }
            }
        }
        MemberUnavailable kind = null;
        if (absentMeth) {
            kind = MemberUnavailable.NonExistentMethod;
        }
        if (isPrivateMeth[0]) {
            kind = MemberUnavailable.PrivateMethod;
        }
        if (isPrivateRef[0]) {
            kind = MemberUnavailable.PrivatePath;
        }
        if (kind == null) {
            return null;
        }
        throw Resources.Error.multiPartStringError("MemberUnavailable", "Path", formatPathIn(path), "Selector", "" + ((ms.isPresent()) ? ms.get() : ""), "InvalidKind", "" + kind.name(), "IsPrivate", "" + kind.name().contains("Private"));
    } catch (ast.ErrorMessage.PathMetaOrNonExistant e) {
        throw Resources.Error.multiPartStringError("MemberUnavailable", "Path", formatPathIn(path), "Selector", "" + ((ms.isPresent()) ? ms.get() : ""), "InvalidKind", "" + MemberUnavailable.NonExistentPath, "IsPrivate", "false");
    }
}
Also used : C(ast.Ast.C) ErrorMessage(ast.ErrorMessage) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB)

Example 4 with C

use of ast.Ast.C in project L42 by ElvisResearchGroup.

the class Redirect method redirectOkAux.

private static void redirectOkAux(Program p, PathSPath current, ClassB cbTop, List<PathSPath> ambiguities, List<SPathSPath> exceptions) {
    assert current.getPathsSet().size() == 1;
    List<Ast.C> cs = current.getPath().getCBar();
    if (cs.isEmpty()) {
        throw Errors42.errorInvalidOnTopLevel();
    }
    Errors42.checkExistsPathMethod(cbTop, cs, Optional.empty());
    //Boolean[] csPrivate=new Boolean[]{false};
    ClassB currentIntCb = cbTop.getClassB(cs);
    //path exists by construction.
    Path path = current.getPathsSet().iterator().next();
    ClassB currentExtCb;
    if (path.isCore()) {
        assert path.outerNumber() > 0 : path;
        currentExtCb = p.extractClassB(path);
    } else {
        assert path.isPrimitive();
        currentExtCb = ClassB.membersClass(Collections.emptyList(), Position.noInfo, cbTop.getPhase()).withInterface(path.equals(Path.Any()));
    }
    assert cs.stream().allMatch(c -> !c.isUnique());
    boolean isPrivateState = ExtractInfo.hasPrivateState(currentIntCb);
    boolean isNoImplementation = ExtractInfo.isNoImplementation(currentIntCb);
    boolean headerOk = currentIntCb.isInterface() == currentExtCb.isInterface();
    ClassKind kindSrc = ExtractInfo.classKind(cbTop, cs, currentIntCb, null, isPrivateState, isNoImplementation);
    if (!headerOk && !currentIntCb.isInterface()) {
        if (kindSrc == ClassKind.FreeTemplate) {
            headerOk = true;
        }
    }
    ClassKind kindDest = ExtractInfo.classKind(null, null, currentExtCb, null, null, null);
    if (isPrivateState || !isNoImplementation) {
        //unexpectedMembers stay empty if there is implementation
        assert kindSrc != ClassKind.FreeTemplate || kindSrc != ClassKind.Template || kindSrc != ClassKind.Interface : kindSrc;
        throw Errors42.errorSourceUnfit(current.getPath().getCBar(), path, kindSrc, kindDest, Collections.emptyList(), headerOk, Collections.emptyList());
    }
    redirectOkImpl(kindSrc, kindDest, ambiguities, current, currentIntCb, currentExtCb);
    List<Member> unexpectedMembers = new ArrayList<>();
    for (Member mi : currentIntCb.getMs()) {
        Optional<Member> miPrime = Functions.getIfInDom(currentExtCb.getMs(), mi);
        if (miPrime.isPresent() && miPrime.get().getClass().equals(mi.getClass())) {
            Member miGet = miPrime.get();
            redirectOkMember(ambiguities, exceptions, mi, miGet, current);
        } else {
            unexpectedMembers.add(mi);
        }
    }
    if (unexpectedMembers.isEmpty() && headerOk) {
        return;
    }
    if (kindSrc == null) {
        kindSrc = ExtractInfo.classKind(cbTop, cs, currentIntCb, null, isPrivateState, isNoImplementation);
    }
    if (kindDest == null) {
        kindDest = ExtractInfo.classKind(null, null, currentExtCb, null, null, null);
    }
    throw Errors42.errorSourceUnfit(cs, path, kindSrc, kindDest, unexpectedMembers, headerOk, Collections.emptyList());
}
Also used : PathPath(ast.Util.PathPath) PathSPath(ast.Util.PathSPath) SPathSPath(ast.Util.SPathSPath) CloneWithPath(coreVisitors.CloneWithPath) Path(ast.Ast.Path) C(ast.Ast.C) ClassKind(is.L42.connected.withSafeOperators.ExtractInfo.ClassKind) ArrayList(java.util.ArrayList) Member(ast.ExpCore.ClassB.Member) ClassB(ast.ExpCore.ClassB)

Example 5 with C

use of ast.Ast.C in project L42 by ElvisResearchGroup.

the class Rename method renameClass.

public static ClassB renameClass(Program p, ClassB cb, List<Ast.C> src, List<Ast.C> dest) {
    Errors42.checkExistsPathMethod(cb, src, Optional.empty());
    if (src.equals(dest)) {
        return cb;
    }
    if (src.isEmpty()) {
        //push is asked
        return Push.pushMany(cb, dest);
    }
    if (dest.isEmpty() && src.size() == 1) {
        //pop is asked
        boolean rightSize = cb.getMs().size() == 1;
        if (rightSize && ExtractInfo.isBox(cb, cb, Collections.emptyList())) {
            return Pop.pop(cb);
        }
    //otherwise, proceed with encoding
    }
    cb = ClassOperations.normalizePaths(cb);
    if (!ExtractInfo.isPrefix(src, dest)) {
        return ClassOperations.normalizePaths(directRename(p, cb, src, dest));
    }
    src = new ArrayList<>(src);
    dest = new ArrayList<>(dest);
    C result = C.of("Result");
    src.add(0, result);
    dest.add(0, result);
    cb = Push.pushOne(cb, result);
    List<Ast.C> tmp = Collections.singletonList(C.of("Tmp"));
    cb = directRename(p, cb, src, tmp);
    if (!L42.trustPluginsAndFinalProgram) {
        newTypeSystem.TypeSystem.instance().topTypeLib(Phase.Typed, p.evilPush(cb));
    }
    cb = directRename(p, cb, tmp, dest);
    cb = Pop.directPop(cb);
    return cb;
}
Also used : C(ast.Ast.C)

Aggregations

C (ast.Ast.C)5 ClassB (ast.ExpCore.ClassB)4 Path (ast.Ast.Path)3 ArrayList (java.util.ArrayList)3 MethodSelector (ast.Ast.MethodSelector)2 Member (ast.ExpCore.ClassB.Member)2 NestedClass (ast.Expression.ClassB.NestedClass)2 HashMap (java.util.HashMap)2 ErrorMessage (ast.ErrorMessage)1 MethodWithType (ast.Expression.ClassB.MethodWithType)1 PathPath (ast.Util.PathPath)1 PathSPath (ast.Util.PathSPath)1 SPathSPath (ast.Util.SPathSPath)1 CloneWithPath (coreVisitors.CloneWithPath)1 ClassKind (is.L42.connected.withSafeOperators.ExtractInfo.ClassKind)1