Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 11.x fixes to nutcracker #43723

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.BT_TYPEDESC;
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.BT_XML;
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.CODE_UNDEF;
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.VT_INHERENTLY_IMMUTABLE;
import static io.ballerina.runtime.api.types.semtype.BasicTypeCode.VT_MASK;
import static io.ballerina.runtime.api.types.semtype.BddNode.bddAtom;
import static io.ballerina.runtime.api.types.semtype.Core.union;
Expand All @@ -68,6 +69,7 @@ public final class Builder {
private static final String[] EMPTY_STRING_ARR = new String[0];
private static final SemType VAL = SemType.from(VT_MASK);
private static final SemType OBJECT = from(BT_OBJECT);
private static final SemType INHERENTLY_IMMUTABLE = SemType.from(VT_INHERENTLY_IMMUTABLE);

private static final SemType INNER = getBasicTypeUnion(VAL.all() | from(BasicTypeCode.BT_UNDEF).all());
private static final SemType ANY = getBasicTypeUnion(BasicTypeCode.VT_MASK & ~(1 << BasicTypeCode.BT_ERROR.code()));
Expand Down Expand Up @@ -357,6 +359,10 @@ public static SemType getObjectType() {
return OBJECT;
}

public static SemType getInherentlyImmutable() {
return INHERENTLY_IMMUTABLE;
}

static SemType mappingRO() {
return MAPPING_RO.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.ballerina.runtime.internal.types.semtype.FunctionAtomicType;
import io.ballerina.runtime.internal.types.semtype.ListAtomicType;
import io.ballerina.runtime.internal.types.semtype.MappingAtomicType;
import io.ballerina.runtime.internal.types.semtype.MutableSemType;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand All @@ -48,6 +49,9 @@ public final class Context {
public final Map<Bdd, BddMemo> functionMemo = new WeakHashMap<>();
private static final int MAX_CACHE_SIZE = 100;
private final Map<CacheableTypeDescriptor, TypeCheckCache<CacheableTypeDescriptor>> typeCheckCacheMemo;
private Phase phase = Phase.INIT;
private int typeCheckDepth = 0;
private int typeResolutionDepth = 0;

private Context(Env env) {
this.env = env;
Expand Down Expand Up @@ -90,6 +94,64 @@ public boolean memoSubtypeIsEmpty(Map<Bdd, BddMemo> memoTable, BddIsEmptyPredica
return m.isEmpty().orElseGet(() -> memoSubTypeIsEmptyInner(isEmptyPredicate, bdd, m));
}

public void enterTypeResolutionPhase(MutableSemType type) throws InterruptedException {
switch (phase) {
case INIT -> {
typeResolutionDepth++;
env.enterTypeResolutionPhase(this, type);
phase = Phase.TYPE_RESOLUTION;
}
case TYPE_RESOLUTION -> typeResolutionDepth++;
case TYPE_CHECKING -> throw new IllegalStateException(
"Cannot enter type resolution phase while in type checking phase\n");
}
}

public void exitTypeResolutionPhaseAbruptly(Exception ex) {
env.exitTypeResolutionPhaseAbruptly(this, ex);
}

public void enterTypeCheckingPhase(SemType t1, SemType t2) {
typeCheckDepth++;
switch (phase) {
case INIT -> {
env.enterTypeCheckingPhase(this, t1, t2);
phase = Phase.TYPE_CHECKING;
}
case TYPE_RESOLUTION -> throw new IllegalStateException(
"Cannot enter type checking phase while in type resolution phase\n");
case TYPE_CHECKING -> {
}
}
}

public void exitTypeResolutionPhase() {
if (phase == Phase.TYPE_RESOLUTION) {
typeResolutionDepth--;
if (typeResolutionDepth == 0) {
env.exitTypeResolutionPhase(this);
phase = Phase.INIT;
}
} else {
throw new IllegalStateException("Cannot exit type resolution phase without entering it");
}
}

public void exitTypeCheckingPhase() {
switch (phase) {
case INIT -> throw new IllegalStateException("Cannot exit type checking phase without entering it");
case TYPE_RESOLUTION ->
throw new IllegalStateException("Cannot exit type checking phase while in type resolution phase\n");
case TYPE_CHECKING -> {
env.exitTypeCheckingPhase(this);
typeCheckDepth--;
if (typeCheckDepth == 0) {
phase = Phase.INIT;
}
}
}
}

private boolean memoSubTypeIsEmptyInner(BddIsEmptyPredicate isEmptyPredicate, Bdd bdd, BddMemo m) {
// We are staring the type check with the assumption our type is empty (see: inductive type)
m.isEmpty = BddMemo.Status.PROVISIONAL;
Expand Down Expand Up @@ -131,6 +193,7 @@ private void resetMemoizedValues(int initStackDepth, boolean isEmpty, boolean is

public ListAtomicType listAtomType(Atom atom) {
if (atom instanceof RecAtom recAtom) {
assert this.env.getRecListAtomType(recAtom) != null;
return this.env.getRecListAtomType(recAtom);
} else {
return (ListAtomicType) ((TypeAtom) atom).atomicType();
Expand All @@ -139,6 +202,7 @@ public ListAtomicType listAtomType(Atom atom) {

public MappingAtomicType mappingAtomType(Atom atom) {
if (atom instanceof RecAtom recAtom) {
assert this.env.getRecMappingAtomType(recAtom) != null;
return this.env.getRecMappingAtomType(recAtom);
} else {
return (MappingAtomicType) ((TypeAtom) atom).atomicType();
Expand All @@ -147,6 +211,7 @@ public MappingAtomicType mappingAtomType(Atom atom) {

public FunctionAtomicType functionAtomicType(Atom atom) {
if (atom instanceof RecAtom recAtom) {
assert this.env.getRecFunctionAtomType(recAtom) != null;
return this.env.getRecFunctionAtomType(recAtom);
} else {
return (FunctionAtomicType) ((TypeAtom) atom).atomicType();
Expand All @@ -156,4 +221,12 @@ public FunctionAtomicType functionAtomicType(Atom atom) {
public TypeCheckCache<CacheableTypeDescriptor> getTypeCheckCache(CacheableTypeDescriptor typeDescriptor) {
return typeCheckCacheMemo.computeIfAbsent(typeDescriptor, TypeCheckCache::new);
}

public void registerAbruptTypeCheckEnd(Exception ex) {
env.registerAbruptTypeCheckEnd(this, ex);
}

enum Phase {
INIT, TYPE_RESOLUTION, TYPE_CHECKING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,15 @@ public static boolean isNever(SemType t) {
}

public static boolean isSubType(Context cx, SemType t1, SemType t2) {
return isEmpty(cx, diff(t1, t2));
try {
cx.enterTypeCheckingPhase(t1, t2);
boolean res = isEmpty(cx, diff(t1, t2));
cx.exitTypeCheckingPhase();
return res;
} catch (Exception e) {
cx.registerAbruptTypeCheckEnd(e);
throw e;
}
}

public static boolean isSubtypeSimple(SemType t1, SemType t2) {
Expand Down
Loading