Skip to content

Commit

Permalink
Merge pull request #43279 from heshanpadmasiri/feat/runtime-stream
Browse files Browse the repository at this point in the history
Implement runtime type checker on SemTypes
  • Loading branch information
lochana-chathura authored Nov 20, 2024
2 parents 802f909 + 016da7e commit 0ca2edc
Show file tree
Hide file tree
Showing 232 changed files with 18,307 additions and 5,959 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[
{
"description": "Define types.",
"code": "type Person record { string name; int age; }; type Employee record { string name; int age; int empNo; }; type Department record { string code; };"
"code": "type PersonOP record { string name; int age; }; type EmployeeOP record { string name; int age; int empNo; }; type DepartmentOP record { string code; };"
},
{
"description": "Define employee.",
"code": "Employee employee = {name: \"Jane Doe\", age: 25, empNo: 1};"
"code": "EmployeeOP employee = {name: \"Jane Doe\", age: 25, empNo: 1};"
},
{
"description": "Cas employee to person.",
"code": "Person person = <Person>employee;"
"code": "PersonOP person = <PersonOP>employee;"
},
{
"description": "Cas employee to person - get value.",
Expand All @@ -18,7 +18,7 @@
},
{
"description": "Recast back to employee.",
"code": "Employee employeeTwo = <Employee>person;"
"code": "EmployeeOP employeeTwo = <EmployeeOP>person;"
},
{
"description": "Recast back to employee - get value.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[
{
"description": "Define types.",
"code": "type Employee record { string name; int id; }; type Person record { string name; };"
"code": "type EmployeeEQ record { string name; int id; }; type PersonEQ record { string name; };"
},
{
"description": "Define employee.",
"code": "final Employee moduleEmployee = {name: \"John\", id: 2102};"
"code": "final EmployeeEQ moduleEmployee = {name: \"John\", id: 2102};"
},
{
"description": "Define module ref getter.",
"code": "function getModuleEmployee() returns Employee { return moduleEmployee; }"
"code": "function getModuleEmployee() returns EmployeeEQ { return moduleEmployee; }"
},
{
"description": "Equality ==.",
Expand Down Expand Up @@ -49,7 +49,7 @@
},
{
"description": "Deep inequality in records.",
"code": "Employee e1 = {name: \"Jane\", id: 1100}; Employee e2 = {name: \"Jane\", id: 1100};"
"code": "EmployeeEQ e1 = {name: \"Jane\", id: 1100}; EmployeeEQ e2 = {name: \"Jane\", id: 1100};"
},
{
"description": "Deep inequality in records. - get value",
Expand All @@ -58,7 +58,7 @@
},
{
"description": "Deep equality in records.",
"code": "Employee e3 = {name: \"Anne\", id: 1100};"
"code": "EmployeeEQ e3 = {name: \"Anne\", id: 1100};"
},
{
"description": "Deep equality in records. - get value",
Expand All @@ -67,7 +67,7 @@
},
{
"description": "Reference equality ===.",
"code": "Employee e4 = getModuleEmployee(); Person e5 = getModuleEmployee();"
"code": "EmployeeEQ e4 = getModuleEmployee(); PersonEQ e5 = getModuleEmployee();"
},
{
"description": "Reference equality ===. - get value",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[
{
"description": "Define types.",
"code": "type Address record { string country; string state; string city; string street; }; type Person record { string name; int age; boolean married; float salary; Address address; };"
"code": "type AddressCloneTest record { string country; string state; string city; string street; }; type PersonCloneTest record { string name; int age; boolean married; float salary; AddressCloneTest address; };"
},
{
"description": "Define address.",
"code": "Address address = { country: \"USA\", state: \"NC\", city: \"Raleigh\", street: \"Daniels St\" };"
"code": "AddressCloneTest address = { country: \"USA\", state: \"NC\", city: \"Raleigh\", street: \"Daniels St\" };"
},
{
"description": "Define person.",
"code": "Person person = { name: \"Alex\", age: 24, married: false, salary: 8000.0, address: address };"
"code": "PersonCloneTest person = { name: \"Alex\", age: 24, married: false, salary: 8000.0, address: address };"
},
{
"description": "Clone operation.",
"code": "Person result = person.clone();"
"code": "PersonCloneTest result = person.clone();"
},
{
"description": "Check reference equality.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[
{
"description": "Define Details.",
"code": "type Details record {| string name; int id; |};"
"code": "type DetailsImmutableTest record {| string name; int id; |};"
},
{
"description": "Define Student.",
"code": "type Student record {| int 'class; Details details; map<int> marks; |};"
"code": "type StudentImmutableTest record {| int 'class; DetailsImmutableTest details; map<int> marks; |};"
},
{
"description": "Define addEntryToMap.",
"code": "function addEntryToMap(map<string|int> m, string key, string|int value) { m[key] = value; }"
},
{
"description": "Define immutable Details",
"code": "Details & readonly immutableDetails = { name: \"May\", id: 112233 };"
"code": "DetailsImmutableTest & readonly immutableDetails = { name: \"May\", id: 112233 };"
},
{
"description": "Define immutable Student &",
"code": "Student & readonly student = { 'class: 12, details: immutableDetails, marks: { math: 80, physics: 85, chemistry: 75 } };"
"code": "StudentImmutableTest & readonly student = { 'class: 12, details: immutableDetails, marks: { math: 80, physics: 85, chemistry: 75 } };"
},
{
"description": "Readonly status of student.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public final class RuntimeConstants {
// Empty value for string
public static final BString STRING_EMPTY_VALUE = StringUtils.fromString("");

public static final Long INT_MAX_VALUE = 9223372036854775807L;
public static final Long INT_MIN_VALUE = -9223372036854775807L - 1L;
public static final Integer BBYTE_MIN_VALUE = 0;
public static final Integer BBYTE_MAX_VALUE = 255;
public static final Integer SIGNED32_MAX_VALUE = 2147483647;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* Class @{@link TypeCreator} provides APIs to create ballerina type instances.
Expand All @@ -58,6 +59,7 @@
*/
public final class TypeCreator {

private static final RecordTypeCache registeredRecordTypes = new RecordTypeCache();
/**
* Creates a new array type with given element type.
*
Expand Down Expand Up @@ -147,7 +149,7 @@ public static TupleType createTupleType(List<Type> typeList, Type restType, int
* @return the new tuple type
*/
public static TupleType createTupleType(List<Type> typeList, Type restType,
int typeFlags, boolean isCyclic, boolean readonly) {
int typeFlags, boolean isCyclic, boolean readonly) {
return new BTupleType(typeList, restType, typeFlags, isCyclic, readonly);
}

Expand All @@ -162,16 +164,16 @@ public static TupleType createTupleType(List<Type> typeList, Type restType,
* @return the new tuple type
*/
public static TupleType createTupleType(String name, Module pkg,
int typeFlags, boolean isCyclic, boolean readonly) {
int typeFlags, boolean isCyclic, boolean readonly) {
return new BTupleType(name, pkg, typeFlags, isCyclic, readonly);
}

/**
* Create a {@code MapType} which represents the map type.
*
* @param constraint constraint type which particular map is bound to.
* @return the new map type
*/
* Create a {@code MapType} which represents the map type.
*
* @param constraint constraint type which particular map is bound to.
* @return the new map type
*/
public static MapType createMapType(Type constraint) {
return new BMapType(constraint);
}
Expand Down Expand Up @@ -224,6 +226,10 @@ public static MapType createMapType(String typeName, Type constraint, Module mod
*/
public static RecordType createRecordType(String typeName, Module module, long flags, boolean sealed,
int typeFlags) {
BRecordType memo = registeredRecordType(typeName, module);
if (memo != null) {
return memo;
}
return new BRecordType(typeName, typeName, module, flags, sealed, typeFlags);
}

Expand All @@ -240,8 +246,11 @@ public static RecordType createRecordType(String typeName, Module module, long f
* @return the new record type
*/
public static RecordType createRecordType(String typeName, Module module, long flags, Map<String, Field> fields,
Type restFieldType,
boolean sealed, int typeFlags) {
Type restFieldType, boolean sealed, int typeFlags) {
BRecordType memo = registeredRecordType(typeName, module);
if (memo != null) {
return memo;
}
return new BRecordType(typeName, module, flags, fields, restFieldType, sealed, typeFlags);
}

Expand Down Expand Up @@ -520,4 +529,45 @@ public static FiniteType createFiniteType(String typeName, Set<Object> values, i

private TypeCreator() {
}

private static BRecordType registeredRecordType(String typeName, Module pkg) {
if (typeName == null || pkg == null) {
return null;
}
return registeredRecordTypes.get(new TypeIdentifier(typeName, pkg));
}

public static void registerRecordType(BRecordType recordType) {
String name = recordType.getName();
Module pkg = recordType.getPackage();
if (name == null || pkg == null) {
return;
}
if (name.contains("$anon")) {
return;
}
TypeIdentifier typeIdentifier = new TypeIdentifier(name, pkg);
registeredRecordTypes.put(typeIdentifier, recordType);
}

private static final class RecordTypeCache {

private static final Map<TypeIdentifier, BRecordType> cache = new ConcurrentHashMap<>();

BRecordType get(TypeIdentifier key) {
return cache.get(key);
}

void put(TypeIdentifier identifier, BRecordType value) {
cache.put(identifier, value);
}
}

public record TypeIdentifier(String typeName, Module pkg) {

public TypeIdentifier {
assert typeName != null;
assert pkg != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
*/
public final class PredefinedTypes {

private static final Module EMPTY_MODULE = new Module(null, null, null);
public static final Module EMPTY_MODULE = new Module(null, null, null);

public static final IntegerType TYPE_INT = new BIntegerType(TypeConstants.INT_TNAME, EMPTY_MODULE);
public static final IntegerType TYPE_INT_SIGNED_8 =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://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.runtime.api.types.semtype;

/**
* Represent the BDD atom.
*
* @since 2201.11.0
*/
public sealed interface Atom permits RecAtom, TypeAtom {

/**
* Get the index of the atom. For {@code TypeAtoms} this is a unique index within the {@code Env}. Each
* {@code RecAtom} that points to the same {@code TypeAtom} will have the same index.
*
* @return index of the atom
*/
int index();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://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.runtime.api.types.semtype;

import io.ballerina.runtime.internal.types.semtype.CellAtomicType;
import io.ballerina.runtime.internal.types.semtype.FunctionAtomicType;
import io.ballerina.runtime.internal.types.semtype.ListAtomicType;
import io.ballerina.runtime.internal.types.semtype.MappingAtomicType;

/**
* Marker type representing AtomicType.
*
* @since 2201.11.0
*/
public sealed interface AtomicType permits CellAtomicType, FunctionAtomicType, ListAtomicType, MappingAtomicType {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.ballerina.runtime.api.types.semtype;

abstract sealed class BasicTypeBitSet permits SemType {

private int all;

protected BasicTypeBitSet(int all) {
this.all = all;
}

protected void setAll(int all) {
this.all = all;
}

public final int all() {
assert all != -1 : "SemType created by no arg constructor must be initialized with setAll";
return all;
}
}
Loading

0 comments on commit 0ca2edc

Please sign in to comment.