Skip to content

Commit

Permalink
Merge branch 2201.11.0-stage
Browse files Browse the repository at this point in the history
  • Loading branch information
warunalakshitha committed Dec 19, 2024
2 parents 8b46143 + 109f6cc commit e01fe53
Show file tree
Hide file tree
Showing 46 changed files with 953 additions and 356 deletions.
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,10 +49,17 @@ 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;
List<PhaseData> typeResolutionPhases = new ArrayList<>();
List<PhaseData> typeCheckPhases = new ArrayList<>();
private final boolean collectDiagnostic;
private int typeCheckDepth = 0;
private int typeResolutionDepth = 0;

private Context(Env env) {
this.env = env;
this.typeCheckCacheMemo = createTypeCheckCacheMemo();
this.collectDiagnostic = "true".equalsIgnoreCase(System.getenv("BAL_TYPE_CHECK_DIAGNOSTIC_ENABLE"));
}

private static Map<CacheableTypeDescriptor, TypeCheckCache<CacheableTypeDescriptor>> createTypeCheckCacheMemo() {
Expand Down Expand Up @@ -90,6 +98,111 @@ 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;
if (collectDiagnostic) {
typeResolutionPhases.add(new PhaseData());
}
}
case TYPE_RESOLUTION -> {
typeResolutionDepth++;
}
case TYPE_CHECKING -> {
StringBuilder sb = new StringBuilder();
sb.append("Cannot enter type resolution phase while in type checking phase\n");
if (collectDiagnostic) {
appendPhaseDataToError(sb);
}
throw new IllegalStateException(sb.toString());
}
}
}

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);
if (collectDiagnostic) {
typeCheckPhases.add(new PhaseData());
}
phase = Phase.TYPE_CHECKING;
}
case TYPE_RESOLUTION -> {
StringBuilder sb = new StringBuilder();
sb.append("Cannot enter type checking phase while in type resolution phase\n");
if (collectDiagnostic) {
appendPhaseDataToError(sb);
}
throw new IllegalStateException(sb.toString());
}
case TYPE_CHECKING -> {
}
}
}

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

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

private void appendPhaseDataToError(StringBuilder sb) {
sb.append("Type resolution phases:\n");
for (PhaseData phaseData : typeResolutionPhases) {
sb.append(phaseData).append("\n");
}
sb.append("Type checking phases:\n");
for (PhaseData phaseData : typeCheckPhases) {
sb.append(phaseData).append("\n");
}
}

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 +244,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 +253,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 +262,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 +272,28 @@ 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
}

record PhaseData(StackTraceElement[] stackTrace) {

PhaseData() {
this(Thread.currentThread().getStackTrace());
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (StackTraceElement element : stackTrace) {
builder.append("\tat ").append(element).append("\n");
}
return builder.toString();
}
}
}
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

0 comments on commit e01fe53

Please sign in to comment.