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);
}
});
}
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);
}
});
}
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");
}
}
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());
}
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;
}
Aggregations