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

Port nBallerina cell type to jBallerina #42285

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
46 changes: 46 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/CellAtomicType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.types;

/**
* CellAtomicType node.
*
* @since 2201.10.0
*/
public final class CellAtomicType implements AtomicType {
public final SemType ty;
public final CellMutability mut;

public static final CellAtomicType CELL_ATOMIC_VAL = from(PredefinedType.TOP, CellMutability.CELL_MUT_LIMITED);
public static final CellAtomicType CELL_ATOMIC_NEVER = from(PredefinedType.NEVER, CellMutability.CELL_MUT_LIMITED);

private CellAtomicType(SemType ty, CellMutability mut) {
this.ty = ty;
this.mut = mut;
}

public static CellAtomicType from(SemType ty, CellMutability mut) {
return new CellAtomicType(ty, mut);
}

public enum CellMutability {
heshanpadmasiri marked this conversation as resolved.
Show resolved Hide resolved
CELL_MUT_NONE,
CELL_MUT_LIMITED,
CELL_MUT_UNLIMITED;
}
}
31 changes: 31 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/CellSemType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.types;

/**
* This is to represent a SemType belonging to cell basic type.
*
* @since 2201.10.0
*/
public class CellSemType extends ComplexSemType {

public CellSemType(ProperSubtypeData[] subtypeDataList) {
super(UniformTypeBitSet.from(0), PredefinedType.CELL, subtypeDataList);
assert subtypeDataList.length == 1;
}
}
22 changes: 21 additions & 1 deletion semtypes/src/main/java/io/ballerina/types/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import static io.ballerina.types.Conjunction.and;
import static io.ballerina.types.UniformTypeCode.UT_LIST_RO;
import static io.ballerina.types.UniformTypeCode.UT_LIST_RW;
import static io.ballerina.types.typeops.BddCommonOps.bddComplement;
import static io.ballerina.types.typeops.BddCommonOps.bddDiff;
import static io.ballerina.types.typeops.BddCommonOps.bddIntersect;
import static io.ballerina.types.typeops.BddCommonOps.bddUnion;

/**
* Code common to implementation of multiple basic types.
Expand Down Expand Up @@ -125,7 +129,7 @@
Instead, we transform the BDD to avoid cases that would give the wrong answer.
Atom index 0 is LIST_SUBTYPE_RO and MAPPING_SUBTYPE_RO */
public static Bdd bddFixReadOnly(Bdd b) {
return bddPosMaybeEmpty(b) ? BddCommonOps.bddIntersect(b, BddCommonOps.bddAtom(RecAtom.createRecAtom(0))) : b;
return bddPosMaybeEmpty(b) ? bddIntersect(b, BddCommonOps.bddAtom(RecAtom.createRecAtom(0))) : b;
}

public static boolean bddPosMaybeEmpty(Bdd b) {
Expand All @@ -144,6 +148,22 @@
return and(atom, next);
}

public static SubtypeData bddSubtypeUnion(SubtypeData t1, SubtypeData t2) {
return bddUnion((Bdd) t1, (Bdd) t2);
}

public static SubtypeData bddSubtypeIntersect(SubtypeData t1, SubtypeData t2) {
return bddIntersect((Bdd) t1, (Bdd) t2);
}

public static SubtypeData bddSubtypeDiff(SubtypeData t1, SubtypeData t2) {
return bddDiff((Bdd) t1, (Bdd) t2);
}

public static SubtypeData bddSubtypeComplement(SubtypeData t) {
return bddComplement((Bdd) t);

Check warning on line 164 in semtypes/src/main/java/io/ballerina/types/Common.java

View check run for this annotation

Codecov / codecov/patch

semtypes/src/main/java/io/ballerina/types/Common.java#L164

Added line #L164 was not covered by tests
}

public static SemType[] shallowCopyTypes(SemType[] v) {
return Arrays.copyOf(v, v.length);
}
Expand Down
4 changes: 4 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ public static boolean isSubtypeSimple(SemType t1, UniformTypeBitSet t2) {
return (bits & ~t2.bitset) == 0;
}

public static boolean isSameType(Context context, SemType t1, SemType t2) {
return isSubtype(context, t1, t2) && isSubtype(context, t2, t1);
}

public static UniformTypeBitSet widenToBasicTypes(SemType t) {
if (t instanceof UniformTypeBitSet uniformTypeBitSet) {
return uniformTypeBitSet;
Expand Down
8 changes: 8 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public TypeAtom mappingAtom(MappingAtomicType atomicType) {
return this.typeAtom(atomicType);
}

public TypeAtom cellAtom(CellAtomicType atomicType) {
return this.typeAtom(atomicType);
}

private TypeAtom typeAtom(AtomicType atomicType) {
synchronized (this.atomTable) {
TypeAtom ta = this.atomTable.get(atomicType);
Expand Down Expand Up @@ -147,6 +151,10 @@ public MappingAtomicType getRecMappingAtomType(RecAtom ra) {
}
}

public static CellAtomicType cellAtomType(Atom atom) {
return (CellAtomicType) ((TypeAtom) atom).atomicType;
}

public void addTypeDef(String typeName, SemType semType) {
this.types.put(typeName, semType);
}
Expand Down
3 changes: 2 additions & 1 deletion semtypes/src/main/java/io/ballerina/types/OpsTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.ballerina.types;

import io.ballerina.types.typeops.BooleanOps;
import io.ballerina.types.typeops.CellOps;
import io.ballerina.types.typeops.DecimalOps;
import io.ballerina.types.typeops.ErrorOps;
import io.ballerina.types.typeops.FloatOps;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class OpsTable {
OPS[i++] = new FunctionOps(); // function
OPS[i++] = PANIC_IMPL; // typedesc
OPS[i++] = PANIC_IMPL; // handle
OPS[i++] = PANIC_IMPL; // unused
OPS[i++] = new CellOps(); // cell
OPS[i++] = PANIC_IMPL; // RW future
OPS[i++] = PANIC_IMPL; // RW stream
OPS[i++] = new ListTypeRwOps(); // RW list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class PredefinedType {
public static final UniformTypeBitSet STREAM = uniformType(UniformTypeCode.UT_STREAM);
public static final UniformTypeBitSet FUTURE = uniformType(UniformTypeCode.UT_FUTURE);

public static final UniformTypeBitSet CELL = uniformType(UniformTypeCode.BT_CELL);

// this is SubtypeData|error
public static final UniformTypeBitSet TOP = uniformTypeUnion(UniformTypeCode.UT_MASK);
public static final UniformTypeBitSet ANY =
Expand Down
18 changes: 17 additions & 1 deletion semtypes/src/main/java/io/ballerina/types/SemTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,26 @@ public static SemType union(SemType t1, SemType t2) {
return Core.union(t1, t2);
}

public static SemType union(SemType first, SemType second, SemType... rest) {
SemType u = Core.union(first, second);
for (SemType s : rest) {
u = Core.union(u, s);
}
return u;
}

public static SemType intersect(SemType t1, SemType t2) {
return Core.intersect(t1, t2);
}

public static SemType intersect(SemType first, SemType second, SemType... rest) {
SemType i = Core.intersect(first, second);
for (SemType s : rest) {
i = Core.intersect(i, s);
}
return i;
}

public static SemType tuple(Env env, SemType[] members) {
return ListDefinition.tuple(env, members);
}
Expand All @@ -97,7 +113,7 @@ public static boolean isSubtypeSimple(SemType t1, UniformTypeBitSet t2) {
}

public static boolean isSameType(Context context, SemType t1, SemType t2) {
return isSubtype(context, t1, t2) && isSubtype(context, t2, t1);
return Core.isSameType(context, t1, t2);
}

public static SemType errorDetail(SemType detail) {
Expand Down
6 changes: 6 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/TypeAtom.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
package io.ballerina.types;

import static io.ballerina.types.CellAtomicType.CELL_ATOMIC_NEVER;
import static io.ballerina.types.CellAtomicType.CELL_ATOMIC_VAL;

/**
* Represent a TypeAtom.
*
Expand All @@ -26,6 +29,9 @@ public class TypeAtom implements Atom {
public final long index;
public final AtomicType atomicType;

public static final TypeAtom ATOM_CELL_VAL = createTypeAtom(0, CELL_ATOMIC_VAL);
public static final TypeAtom ATOM_CELL_NEVER = createTypeAtom(1, CELL_ATOMIC_NEVER);

private TypeAtom(long index, AtomicType atomicType) {
this.index = index;
this.atomicType = atomicType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class UniformTypeCode {
public static final UniformTypeCode UT_TYPEDESC = from(0x0D);
public static final UniformTypeCode UT_HANDLE = from(0x0E);

// Non-val
public static final UniformTypeCode BT_CELL = from(0x0F);

// Inherently mutable
public static final UniformTypeCode UT_FUTURE = from(0x10);
public static final UniformTypeCode UT_STREAM = from(0x11);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.types.subtypedata;

import io.ballerina.types.CellAtomicType;
import io.ballerina.types.CellSemType;
import io.ballerina.types.ComplexSemType;
import io.ballerina.types.Env;
import io.ballerina.types.PredefinedType;
import io.ballerina.types.SemType;
import io.ballerina.types.TypeAtom;
import io.ballerina.types.UniformTypeCode;

import static io.ballerina.types.typeops.BddCommonOps.bddAtom;

/**
* CellSubType.
*
* @since 2201.10.0
*/
public class CellSubtype {

Check warning on line 36 in semtypes/src/main/java/io/ballerina/types/subtypedata/CellSubtype.java

View check run for this annotation

Codecov / codecov/patch

semtypes/src/main/java/io/ballerina/types/subtypedata/CellSubtype.java#L36

Added line #L36 was not covered by tests

public static CellSemType cellContaining(Env env, SemType ty, CellAtomicType.CellMutability mut) {
CellAtomicType atomicCell = CellAtomicType.from(ty, mut);
TypeAtom atom = env.cellAtom(atomicCell);
BddNode bdd = bddAtom(atom);
ComplexSemType complexSemType = (ComplexSemType) PredefinedType.uniformSubtype(UniformTypeCode.BT_CELL, bdd);
return new CellSemType(complexSemType.subtypeDataList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,35 @@
if (b instanceof BddAllOrNothing) {
return ((BddAllOrNothing) b).complement();
} else {
BddNode bdd = (BddNode) b;
BddAllOrNothing bddNothing = BddAllOrNothing.bddNothing();
if (bdd.right.equals(bddNothing)) {
return bddCreate(bdd.atom,
bddNothing,
bddComplement(bddUnion(bdd.left, bdd.middle)),
bddComplement(bdd.middle));
} else if (bdd.left.equals(bddNothing)) {
return bddCreate(bdd.atom,
bddComplement(bdd.middle),
bddComplement(bddUnion(bdd.right, bdd.middle)),
bddNothing);
} else if (bdd.middle.equals(bddNothing)) {
return bddCreate(bdd.atom,
bddComplement(bdd.left),
bddComplement(bddUnion(bdd.left, bdd.right)),
bddComplement(bdd.right));
} else {
// There is a typo in the Frisch PhD thesis for this formula.
// (It has left and right swapped.)
// Castagna (the PhD supervisor) confirms that this is the correct formula.
return bddCreate(bdd.atom,
bddComplement(bddUnion(bdd.left, bdd.middle)),
bddNothing,
bddComplement(bddUnion(bdd.right, bdd.middle)));
}
return bddNodeComplement((BddNode) b);
}
}

public static Bdd bddNodeComplement(BddNode b) {
BddAllOrNothing bddNothing = BddAllOrNothing.bddNothing();
if (b.right.equals(bddNothing)) {
return bddCreate(b.atom,
bddNothing,
bddComplement(bddUnion(b.left, b.middle)),
bddComplement(b.middle));
} else if (b.left.equals(bddNothing)) {
return bddCreate(b.atom,
bddComplement(b.middle),
bddComplement(bddUnion(b.right, b.middle)),

Check warning on line 160 in semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java

View check run for this annotation

Codecov / codecov/patch

semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java#L158-L160

Added lines #L158 - L160 were not covered by tests
bddNothing);
} else if (b.middle.equals(bddNothing)) {
return bddCreate(b.atom,
bddComplement(b.left),
bddComplement(bddUnion(b.left, b.right)),
bddComplement(b.right));

Check warning on line 166 in semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java

View check run for this annotation

Codecov / codecov/patch

semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java#L163-L166

Added lines #L163 - L166 were not covered by tests
} else {
// There is a typo in the Frisch PhD thesis for this formula.
// (It has left and right swapped.)
// Castagna (the PhD supervisor) confirms that this is the correct formula.
return bddCreate(b.atom,
bddComplement(bddUnion(b.left, b.middle)),

Check warning on line 172 in semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java

View check run for this annotation

Codecov / codecov/patch

semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java#L171-L172

Added lines #L171 - L172 were not covered by tests
bddNothing,
bddComplement(bddUnion(b.right, b.middle)));

Check warning on line 174 in semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java

View check run for this annotation

Codecov / codecov/patch

semtypes/src/main/java/io/ballerina/types/typeops/BddCommonOps.java#L174

Added line #L174 was not covered by tests
}
}

Expand Down
Loading
Loading