Skip to content
New issue

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

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

Already on GitHub? # to your account

[master] Implement semtype for type system in Ballerina #43685

Merged
merged 1,031 commits into from
Feb 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
1031 commits
Select commit Hold shift + click to select a range
730ca2f
Fix compile time type bugs
heshanpadmasiri May 29, 2024
a7a5628
Refactor type resolvers
heshanpadmasiri Jun 3, 2024
8763e85
Add runtime type tests provider
heshanpadmasiri May 28, 2024
74ba9d3
Make it possible to create cell type
heshanpadmasiri May 29, 2024
bf0c1ba
Implement bdd operations
heshanpadmasiri May 30, 2024
52999a7
Introduce cell delegate
heshanpadmasiri May 30, 2024
f2d8b70
Implement cell subtype
heshanpadmasiri May 30, 2024
70fbe72
Cache cell type creation
heshanpadmasiri Jun 3, 2024
4452d46
Use read write locks for atom table
heshanpadmasiri Jun 3, 2024
bb2fce5
Add JDoc comments to new classes
heshanpadmasiri Jun 3, 2024
0e78d75
Add minimum implementation to create list types
heshanpadmasiri Jun 4, 2024
11f1e03
Implemented list type projection
heshanpadmasiri Jun 4, 2024
8216b33
Implement list semtype
heshanpadmasiri Jun 5, 2024
6dc5436
Refactor list projection
heshanpadmasiri Jun 6, 2024
f5a6686
Ennable list type tests
heshanpadmasiri Jun 4, 2024
cdd1af7
Make context thread safe
heshanpadmasiri Jun 7, 2024
1556d96
Optimize TypeChecker:basicType
heshanpadmasiri Jun 7, 2024
f3e9361
Port list subtype improvements
heshanpadmasiri Jun 7, 2024
1ad8284
Fix hash and equals for semtypes
heshanpadmasiri Jun 9, 2024
793dd7d
Cache type check results
heshanpadmasiri Jun 10, 2024
6d6cd7a
Reduce the cost of Objects.hash
heshanpadmasiri Jun 10, 2024
1589a9f
Reduce type check cache overhead
heshanpadmasiri Jun 10, 2024
232e271
Implement MappingDefinition
heshanpadmasiri Jun 11, 2024
7a0700f
Implement mapping subtype empty
heshanpadmasiri Jun 11, 2024
44354fd
Enable semtype tests expect projections for mappings
heshanpadmasiri Jun 11, 2024
a9b3ac1
Fix illegal modification of mapping atomic type
heshanpadmasiri Jun 11, 2024
0c738ca
Enable more tests
heshanpadmasiri Jun 11, 2024
9e97681
Implement mapping type projection
heshanpadmasiri Jun 11, 2024
3c7f837
Implement singleton types for BMaps
heshanpadmasiri Jun 12, 2024
a8ba6e6
Fix cyclic recursive type problem
heshanpadmasiri Jun 12, 2024
224139b
Fix never type not being set correctly for records
heshanpadmasiri Jun 13, 2024
e3fe7e7
Make type creation synchronized for definition based types
heshanpadmasiri Jun 16, 2024
f781285
Extend mapping type projection to support getting cell type of field
heshanpadmasiri Jun 16, 2024
d0866bf
Refactor type checker with "widenedType"
heshanpadmasiri Jun 16, 2024
80af2cb
Refactor shape calculation
heshanpadmasiri Jun 16, 2024
4142b2e
Type with shape for mappings
heshanpadmasiri Jun 16, 2024
68f3445
Use shape with lists
heshanpadmasiri Jun 16, 2024
4a9c949
Fix type checker not using shape correctly
heshanpadmasiri Jun 16, 2024
5e8a13c
Fixed optional fields not been handled correctly in shape
heshanpadmasiri Jun 16, 2024
7bf84d2
Fixed readonly values not being handled correctly
heshanpadmasiri Jun 16, 2024
b92edc1
Fix synchronizing bug
heshanpadmasiri Jun 16, 2024
974ca31
Implement caching for shape of map
heshanpadmasiri Jun 16, 2024
08b19f5
Fix expected error message
heshanpadmasiri Jun 17, 2024
c2d39dd
Refactor MappingAtomicType
heshanpadmasiri Jun 20, 2024
3ff74ed
Fix cyclic typing issue
heshanpadmasiri Jun 20, 2024
dd1ba1c
Switch to non sparse array to represent SubTypeData
heshanpadmasiri Jun 22, 2024
367872c
Refact: introduce explicit empty class for TypeCheckCache
heshanpadmasiri Jun 24, 2024
0a9a0db
Implement function semtype
heshanpadmasiri Jul 17, 2024
fce02be
Implement object semtype
heshanpadmasiri Jul 23, 2024
3620471
Fix record shape
heshanpadmasiri Jul 28, 2024
2eb02e7
Implement error semtype
heshanpadmasiri Jul 29, 2024
4c80881
Fix unit tests
heshanpadmasiri Aug 2, 2024
97e4d3d
Add workaround to value converter
heshanpadmasiri Aug 5, 2024
74f2712
Use lazy containers to xml types in Builder
heshanpadmasiri Aug 4, 2024
f64b89f
Use lazy suppliers for PredefinedEnv
heshanpadmasiri Aug 4, 2024
8946917
Rename Lazy containers to lazy suppliers
heshanpadmasiri Aug 4, 2024
d142606
Simplify context supplying
heshanpadmasiri Aug 4, 2024
05f32c3
More lazy initialization in predefined env
heshanpadmasiri Aug 4, 2024
72e1045
Get rid of unwanted private methods and rearrange the code
heshanpadmasiri Aug 4, 2024
4472698
Impmlement handle semtype
heshanpadmasiri Aug 6, 2024
e4b7950
Cleanup BSemTypeWrapper
heshanpadmasiri Aug 6, 2024
a992aa7
Re-ennable type check cache
heshanpadmasiri Aug 6, 2024
1cda781
Encapsulate access to all and some
heshanpadmasiri Aug 6, 2024
cdee3ba
Refactor the semtype class hierachy
heshanpadmasiri Aug 6, 2024
c8e1f0e
Move immutable semtype class to internals
heshanpadmasiri Aug 6, 2024
87eb849
Make BTypes proper semtypes
heshanpadmasiri Aug 7, 2024
92143f9
Patch unit tests with clashing type names
heshanpadmasiri Aug 7, 2024
04c1224
Implement future semtype
heshanpadmasiri Aug 7, 2024
fdff0b9
Implement regex type
heshanpadmasiri Aug 7, 2024
15bc7ac
Implement typedesc semtype
heshanpadmasiri Aug 9, 2024
8449519
Implement table and stream types
heshanpadmasiri Aug 9, 2024
ede9857
Remove fallback type checker from type checking
heshanpadmasiri Aug 13, 2024
df7f942
Implement isSimpleBasicType with SemTypes
heshanpadmasiri Aug 13, 2024
e7e7886
Implement isReferenceEqual with semtypes
heshanpadmasiri Aug 13, 2024
cfeef03
Re ennable type result caching
heshanpadmasiri Aug 13, 2024
5679b6b
Implement value conversion
heshanpadmasiri Aug 13, 2024
40930d6
Remove fallback type checker
heshanpadmasiri Aug 13, 2024
78192d0
Make semtype part of the top type
heshanpadmasiri Aug 13, 2024
bbfbd9a
Refactor isSameType
heshanpadmasiri Aug 13, 2024
b19271d
Optimize convertible type for int subtypes
heshanpadmasiri Aug 14, 2024
b83f54a
Introduce SimpleBdd
heshanpadmasiri Aug 14, 2024
cacca7b
Move MutableSemTypeDependencyManager to internal
heshanpadmasiri Aug 14, 2024
35e1941
Make any and readonly types immutable semtypes
heshanpadmasiri Aug 14, 2024
49fe835
Lazy initialize inner BTypes
heshanpadmasiri Aug 14, 2024
e67c217
Cache cell atom creation
heshanpadmasiri Aug 14, 2024
9a22113
Optimize mapping subtype check
heshanpadmasiri Aug 15, 2024
b9379ad
Use semtypes for valuesEquals
heshanpadmasiri Aug 15, 2024
fed5b55
Use semtypes for casting
heshanpadmasiri Aug 15, 2024
a027250
Refact shapeOf to support inlining
heshanpadmasiri Aug 15, 2024
3a47c04
Refactor memoSubtypeIsEmpty to make it inline
heshanpadmasiri Aug 15, 2024
ada2ffc
Fix thread safty issue in createSemType
heshanpadmasiri Aug 15, 2024
f1dc339
Cache BType results as well
heshanpadmasiri Aug 15, 2024
bb380cd
Only hold weak references in the mutable semtype dependency manager
heshanpadmasiri Aug 15, 2024
7c563ed
Make all context memo tables weak hash maps
heshanpadmasiri Aug 16, 2024
433bb04
Make atom table hold only weak references
heshanpadmasiri Aug 16, 2024
a6e9f47
Cache repeated semtype creation
heshanpadmasiri Aug 16, 2024
9075316
Introduce simplified cell subtype
heshanpadmasiri Aug 18, 2024
039d82e
Avoid caching cell semtypes for complex types
heshanpadmasiri Aug 18, 2024
4557342
Avoid unnecessarily checking for shape
heshanpadmasiri Aug 18, 2024
e97c00e
Cache type check results for user defined types
heshanpadmasiri Aug 18, 2024
5891223
Cache runtime type creation
heshanpadmasiri Aug 18, 2024
6669713
Remove unwanted caching logic
heshanpadmasiri Aug 18, 2024
38b1470
Cleanup access to context
heshanpadmasiri Aug 18, 2024
94f5a74
Refactor anydata type creation
heshanpadmasiri Aug 18, 2024
82e4b34
Use a simple weak map for dependency manager
heshanpadmasiri Aug 19, 2024
a8a4d08
Introduce copy of write BMapType
heshanpadmasiri Aug 19, 2024
10ad23b
Simplify getType
heshanpadmasiri Aug 19, 2024
b06ec83
Remove unwanted optimizations
heshanpadmasiri Aug 19, 2024
61017f9
Fix result caching for futures
heshanpadmasiri Aug 19, 2024
972e724
Fix Union type not getting reset correctly
heshanpadmasiri Aug 19, 2024
a96f7bf
Reduce the overhead for calculating couldShapeBeDifferent
heshanpadmasiri Aug 20, 2024
33da1ac
Be lazy about calculating the flags of tuple types
heshanpadmasiri Aug 20, 2024
fe4382d
Be lazy when creating the semtype result cache
heshanpadmasiri Aug 20, 2024
7e52a7b
Get rid of the stream when filtering nulls
heshanpadmasiri Aug 20, 2024
648ae20
Use concurrent lazy suppliers for BSemTypeWrappers
heshanpadmasiri Aug 21, 2024
097bbb8
Reduce contention in dependency manager
heshanpadmasiri Aug 21, 2024
8747624
Fix runtime semtype resolver
heshanpadmasiri Aug 23, 2024
c7868fa
Fix BFunction Equal
heshanpadmasiri Aug 23, 2024
21d5d6c
Add workarounds to make libraries compile
heshanpadmasiri Aug 23, 2024
c9a5ed4
Better caching of the type creator
heshanpadmasiri Aug 25, 2024
ad1bd59
Make shape calculation thread safe
heshanpadmasiri Aug 25, 2024
a22ba53
Add workaround to handle cyclic shapes
heshanpadmasiri Aug 25, 2024
1859892
Add default implementations to make TestUtils work
heshanpadmasiri Aug 25, 2024
32d4d24
Fix type casts in Intersection type
heshanpadmasiri Aug 25, 2024
dbc26d2
Fix error equals
heshanpadmasiri Aug 25, 2024
f220fbb
Add workaround to make SQL work
heshanpadmasiri Aug 26, 2024
5c542c4
Patch runtime resolver to handle dependtly typed func
heshanpadmasiri Aug 26, 2024
2a98e5a
Fix runtime test errors
heshanpadmasiri Aug 26, 2024
3e8dcdc
Change synchronization of atom table
heshanpadmasiri Aug 28, 2024
3456d8d
Remove BasicTypeBitSet
heshanpadmasiri Aug 31, 2024
19cf208
Restructure semtype structure
heshanpadmasiri Aug 31, 2024
09ede0b
Make tag directly accesible
heshanpadmasiri Sep 1, 2024
7c3aefc
Avoid creating BType when trying to calculate hash and equal for wrapper
heshanpadmasiri Sep 1, 2024
b166f5c
Use result caching with all semtypes
heshanpadmasiri Sep 1, 2024
a058102
Add fast path to hasFillerValue
heshanpadmasiri Sep 2, 2024
03dc027
Refactor workarounds to isSubType
heshanpadmasiri Sep 2, 2024
7da4d8f
Remove MutableSemTypeDependencyManager
heshanpadmasiri Sep 2, 2024
67ac35b
Get rid of BType basic type
heshanpadmasiri Sep 3, 2024
827d22b
Refactor shape anlayzer
heshanpadmasiri Sep 3, 2024
4829592
Avoid creating the inner BType with BSemTypeWrapper when possible
heshanpadmasiri Sep 3, 2024
1752ebc
Fix failing windows errors
heshanpadmasiri Sep 3, 2024
c6efe8d
Fix string shape calculation
heshanpadmasiri Sep 4, 2024
6bea345
Avoid result caching for basic types
heshanpadmasiri Sep 4, 2024
262bfda
Avoid unnecessarily calculating inherent type
heshanpadmasiri Sep 4, 2024
95c4b0f
Avoid unnecessarily creating singleton types
heshanpadmasiri Sep 6, 2024
75e4bcc
Refactor atoms
heshanpadmasiri Sep 8, 2024
db0cc8b
Refactor shape calculation
heshanpadmasiri Sep 6, 2024
3c8b6a6
Get rid of unwanted sychronizations
heshanpadmasiri Sep 8, 2024
c4066b7
Move BddMemo to inner
heshanpadmasiri Sep 11, 2024
093542a
Use accepted type for is likeShape
heshanpadmasiri Sep 15, 2024
58ed6d9
Introduce wrapper for bddEvery
heshanpadmasiri Sep 18, 2024
329c349
Move mutable semtype to inner
heshanpadmasiri Sep 18, 2024
32a95a3
Refactor method names
heshanpadmasiri Sep 29, 2024
8aa8128
Use holder classes to avoid initialization costs in TypeChecker
heshanpadmasiri Sep 29, 2024
aa9fc15
Fix invalid accepted type for union and tables
heshanpadmasiri Oct 1, 2024
d60e067
Refactor type check caching
heshanpadmasiri Oct 3, 2024
c85ccb4
Make cache sharable between type descriptors
heshanpadmasiri Oct 4, 2024
fcede96
Avoid necessary caching in TypeCreator
heshanpadmasiri Oct 4, 2024
54223f5
Avoid duplicate record type creation
heshanpadmasiri Oct 6, 2024
054cd60
Fix narrow type getting cached invalidly
heshanpadmasiri Oct 8, 2024
411e485
Fix tests with duplicate type definitions
heshanpadmasiri Oct 9, 2024
33487c5
Fix comments and naming
heshanpadmasiri Oct 11, 2024
3846215
Fix Dependently typed functions
heshanpadmasiri Oct 13, 2024
04293c3
Fix caching of annon types
heshanpadmasiri Nov 9, 2024
0d36359
Avoid unnecessary locking
heshanpadmasiri Nov 10, 2024
4d22d01
Refactor synchronization logic
heshanpadmasiri Nov 10, 2024
c53f877
Fix future equals
heshanpadmasiri Nov 10, 2024
95367ac
Avoid caching for anon types
heshanpadmasiri Nov 10, 2024
8d1aeca
Use DefinitionContainer to maintain concurrentcy invariants
heshanpadmasiri Nov 11, 2024
016da7e
Fix checkstyle violations
heshanpadmasiri Nov 19, 2024
0ca2edc
Merge pull request #43279 from heshanpadmasiri/feat/runtime-stream
lochana-chathura Nov 20, 2024
3a9a0de
Increase the timeout value
gimantha Nov 20, 2024
ec8cf5b
Merge branch 'master' into nutcracker
gimantha Nov 20, 2024
b274bd2
Support passing a custom truststore with central calls
azinneera Nov 20, 2024
3701963
Bump bir version
lochana-chathura Nov 21, 2024
78fe460
Fix merging issue for LangLibRecordTest class
lochana-chathura Nov 21, 2024
aca4b5e
Merge pull request #43628 from HindujaB/fix-record-field-master
gimantha Nov 21, 2024
19d4dff
Disable debugger test
HindujaB Nov 21, 2024
9dcc7c7
Merge pull request #43639 from HindujaB/disable-debugger-test-stage
warunalakshitha Nov 21, 2024
8c9c1b8
Pick Module.md as the default doc for v2 balas
azinneera Nov 22, 2024
7a51dad
Increase server log reading rate
lochana-chathura Nov 22, 2024
1e2ad86
Fix checkStyle failure
lochana-chathura Nov 22, 2024
7feee68
Merge pull request #43630 from ballerina-platform/flow_model
dulajdilshan Nov 22, 2024
4e3496c
Merge branch 'master' of https://github.com/ballerina-platform/baller…
gimantha Nov 22, 2024
e049457
Make process local to method
gimantha Nov 22, 2024
3eeb6dc
Merge pull request #43632 from azinneera/prj_api_readme_new
azinneera Nov 23, 2024
1b89090
Fix import order
gimantha Nov 24, 2024
e79f6e8
Increase server reading rate further
lochana-chathura Nov 24, 2024
b832aa6
Add sqlite jdbc dependency
nipunayf Nov 25, 2024
8b48602
Support adding a single cert
azinneera Nov 22, 2024
ab03f4e
Merge pull request #43647 from nipunayf/add-sqllite-dep-11
nipunayf Nov 25, 2024
4a86052
Increment the wait time
gimantha Nov 25, 2024
5299c50
Merge branch 'nutcracker' of https://github.com/ballerina-platform/ba…
gimantha Nov 25, 2024
2ecf8a2
Increment the wait time
gimantha Nov 25, 2024
03fa53f
Merge pull request #43621 from azinneera/prj_api_customTruststore
azinneera Nov 25, 2024
d2827d0
Disable integration tests temporarily
gimantha Nov 26, 2024
f765e2f
Revert the sleep time
gimantha Nov 26, 2024
ca76ab4
Update pull_request_windows_build.yml
gimantha Nov 27, 2024
fcbcfda
Merge pull request #43655 from ballerina-platform/gimantha-increase-w…
chiranSachintha Nov 27, 2024
72eb483
Revert using threads
gimantha Nov 27, 2024
45094df
Revert "Revert using threads"
gimantha Nov 28, 2024
d239379
Change arrays to maps for keeping the grouping keys in query expressions
gimantha Nov 28, 2024
1c5a1c1
Fix step-over behaviour followed by step-in instructions
NipunaRanasinghe Nov 29, 2024
c775dba
Merge pull request #43661 from gimantha/2201.11.0-stage
gimantha Nov 30, 2024
95e90fe
Merge pull request #43665 from NipunaRanasinghe/2201.11.0-stage
NipunaRanasinghe Nov 30, 2024
f793e5b
Make runtime calls synchronous
gimantha Dec 2, 2024
ce2986d
Fix checkstyle issue
gimantha Dec 2, 2024
ff7837e
Update netty version
NipunaMadhushan Dec 3, 2024
fb67d88
Merge pull request #43669 from NipunaMadhushan/update-netty-version
NipunaMadhushan Dec 3, 2024
c23e419
Set encoded function name in lambda details
gimantha Dec 3, 2024
cf28096
Add full build pipeline for stage and `2201.x.x` branch pull requests
chiranSachintha Dec 3, 2024
6735dc3
Merge pull request #43680 from chiranSachintha/full-build-pipeline-pu…
chiranSachintha Dec 4, 2024
fc33b75
Merge pull request #43678 from gimantha/2201.11.0-stage-set-encoded-f…
chiranSachintha Dec 4, 2024
fb56dd8
Merge branch '2201.11.0-stage' into nutcracker
chiranSachintha Dec 4, 2024
504cc0d
Apply suggestions from code review
gimantha Dec 4, 2024
ae4ef0a
Update .github/workflows/pull_request_ubuntu_build.yml
gimantha Dec 4, 2024
d2c8ceb
Apply suggestions from code review
gimantha Dec 4, 2024
461e94b
Add hanging unit test
heshanpadmasiri Dec 29, 2024
825e4dd
Add optimization to compiler
heshanpadmasiri Dec 5, 2024
6240099
Refactor runtime type checker concurrency model
heshanpadmasiri Dec 14, 2024
01c58f5
Refactor logging
heshanpadmasiri Dec 29, 2024
1809dae
Use CAS for DefinitionContainer
heshanpadmasiri Dec 29, 2024
d75251c
Fix checkstyle violations
heshanpadmasiri Jan 2, 2025
234a29c
Merge pull request #43723 from heshanpadmasiri/fix/11-fixes
gimantha Jan 2, 2025
6a0dd24
Add the perf fixes
heshanpadmasiri Feb 2, 2025
e94a602
Code cleanup
heshanpadmasiri Feb 14, 2025
3b995f1
Fix CLI issues
heshanpadmasiri Feb 14, 2025
593aa4c
Reset cache based on time
heshanpadmasiri Feb 14, 2025
dfcf9ff
Fix graalVM configs
heshanpadmasiri Feb 14, 2025
d58d387
Fix function type caches
heshanpadmasiri Feb 15, 2025
d03edfa
Test: use time based caches instead of concurrent hash maps
heshanpadmasiri Feb 14, 2025
3bdc221
Use initial capacity to reduce contention
heshanpadmasiri Feb 15, 2025
94813ee
Fix graalVM
heshanpadmasiri Feb 15, 2025
3375c2a
Refact caches
heshanpadmasiri Feb 16, 2025
833c2bb
Merge pull request #43820 from heshanpadmasiri/optim/runtime-perf
gimantha Feb 17, 2025
bfd9faf
Merge branch 'master' into nutcracker
heshanpadmasiri Feb 17, 2025
6e7f2c1
Cleanup merge results
heshanpadmasiri Feb 17, 2025
4341b8b
Update bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/api/t…
gimantha Feb 18, 2025
51043d3
Fix license headers
heshanpadmasiri Feb 18, 2025
73ef466
Fix checkstyle
heshanpadmasiri Feb 19, 2025
b1a6266
Merge branch 'master' into nutcracker
heshanpadmasiri Feb 19, 2025
77f2a1f
Fix checkstyle
heshanpadmasiri Feb 19, 2025
c1814a2
Fix locking
heshanpadmasiri Feb 19, 2025
87d4de0
Dissable debugger tests
heshanpadmasiri Feb 19, 2025
e6eb741
Fix langlib test
heshanpadmasiri Feb 19, 2025
735d0f1
Merge branch 'master' into nutcracker
heshanpadmasiri Feb 19, 2025
f1c4ee7
Merge branch 'master' into nutcracker
heshanpadmasiri Feb 20, 2025
de22659
Fix string literal values
heshanpadmasiri Feb 20, 2025
fe5e00e
Merge branch 'master' into nutcracker
heshanpadmasiri Feb 20, 2025
f78d78c
Fix merge conflict
heshanpadmasiri Feb 20, 2025
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
Port never type
  • Loading branch information
heshanpadmasiri committed Nov 19, 2024
commit 5c6ad826c68aa3f10e2309f98987ee1eeb66c03e
Original file line number Diff line number Diff line change
@@ -20,12 +20,12 @@

import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.internal.types.BType;
import io.ballerina.runtime.internal.types.BTypeConverter;
import io.ballerina.runtime.internal.types.semtype.BBasicTypeBitSet;
import io.ballerina.runtime.internal.types.semtype.BFloatSubType;
import io.ballerina.runtime.internal.types.semtype.BIntSubType;
import io.ballerina.runtime.internal.types.semtype.BSemType;
import io.ballerina.runtime.internal.types.semtype.BStringSubType;
import io.ballerina.runtime.internal.types.semtype.BSubType;

import java.util.ArrayList;
import java.util.List;
@@ -49,7 +49,11 @@ public static SemType from(Type type) {
}

public static SemType from(BType innerType) {
return basicSubType(BasicTypeCode.BT_B_TYPE, BSubType.wrap(innerType));
return BTypeConverter.from(innerType);
}

public static SemType neverType() {
return basicTypeUnion(0);
}

public static SemType basicTypeUnion(int bitset) {
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
public final class Core {

private static final SemType SEMTYPE_TOP = BBasicTypeBitSet.from((1 << (CODE_UNDEF + 1)) - 1);
private static final BasicTypeBitSet B_TYPE_TOP = BBasicTypeBitSet.from(1 << BT_B_TYPE.code());

private Core() {
}
@@ -225,7 +226,7 @@ public static SemType complement(SemType t1) {
}

public static boolean isNever(SemType t) {
throw new IllegalStateException("Unimplemented");
return t.all() == 0 && t.some() == 0;
}

public static boolean isSubType(Context cx, SemType t1, SemType t2) {
@@ -242,9 +243,14 @@ public static boolean isSubType(Context cx, SemType t1, SemType t2,

private static boolean applyFallback(SemType t1, SemType t2,
BiFunction<? super BType, ? super BType, Boolean> fallback) {
BType bType1 = (BType) subTypeData(t1, BT_B_TYPE);
BType bType2 = (BType) subTypeData(t2, BT_B_TYPE);
return fallback.apply(bType1, bType2);
boolean t1HasBType = containsBasicType(t1, B_TYPE_TOP);
boolean t2HasBType = containsBasicType(t2, B_TYPE_TOP);
if (t1HasBType && t2HasBType) {
BType bType1 = (BType) subTypeData(t1, BT_B_TYPE);
BType bType2 = (BType) subTypeData(t2, BT_B_TYPE);
return fallback.apply(bType1, bType2);
}
return !t1HasBType;
}

private static SubTypeData subTypeData(SemType s, BasicTypeCode code) {
@@ -257,8 +263,14 @@ private static SubTypeData subTypeData(SemType s, BasicTypeCode code) {
return s.subTypeData().get(code.code()).data();
}

private static boolean containsBasicType(SemType t1, BasicTypeBitSet t2) {
int bits = t1.all() | t1.some();
return (bits & t2.all()) != 0;
}

public static boolean isSubTypeSimple(SemType t1, BasicTypeBitSet t2) {
throw new IllegalStateException("Unimplemented");
int bits = t1.all() | t1.some();
return (bits & ~t2.all()) == 0;
}

public static boolean isSameType(Context cx, SemType t1, SemType t2) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.internal.types;

import io.ballerina.runtime.api.flags.SymbolFlags;
import io.ballerina.runtime.api.types.Field;
import io.ballerina.runtime.api.types.SemType.BasicTypeCode;
import io.ballerina.runtime.api.types.SemType.Builder;
import io.ballerina.runtime.api.types.SemType.Core;
import io.ballerina.runtime.api.types.SemType.SemType;
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.internal.types.semtype.BSubType;

import java.util.ArrayList;
import java.util.List;

// NOTE: this is so that we don't have to expose any utility constructors as public to builder
public final class BTypeConverter {

private BTypeConverter() {
}

private static SemType from(Type type) {
if (type instanceof SemType semType) {
return semType;
} else if (type instanceof BType bType) {
return from(bType);
}
throw new IllegalArgumentException("Unsupported type: " + type);
}

public static SemType from(BType innerType) {
if (innerType instanceof BNeverType) {
return Builder.neverType();
} else if (innerType instanceof BUnionType unionType) {
return fromUnionType(unionType);
} else if (innerType instanceof BRecordType recordType) {
return fromRecordType(recordType);
} else if (innerType instanceof BTupleType tupleType) {
return fromTupleType(tupleType);
}
return wrapAsPureBType(innerType);
}

private static SemType fromTupleType(BTupleType tupleType) {
for (Type type : tupleType.getTupleTypes()) {
if (Core.isNever(from(type))) {
return Builder.neverType();
}
}
return wrapAsPureBType(tupleType);
}

private static SemType wrapAsPureBType(BType tupleType) {
return Builder.basicSubType(BasicTypeCode.BT_B_TYPE, BSubType.wrap(tupleType));
}

private static SemType fromRecordType(BRecordType recordType) {
for (Field field : recordType.fields.values()) {
if (!SymbolFlags.isFlagOn(field.getFlags(), SymbolFlags.OPTIONAL)) {
SemType fieldType = from(field.getFieldType());
if (Core.isNever(fieldType)) {
return Builder.neverType();
}
}
}
return wrapAsPureBType(recordType);
}

private static SemType fromUnionType(BUnionType unionType) {
List<Type> members = unionType.getMemberTypes();
List<Type> bTypeMembers = new ArrayList<>(members.size());
SemType semTypePart = Builder.neverType();
for (Type member : members) {
if (isSemType(member)) {
semTypePart = Core.union(from(member), semTypePart);
} else {
bTypeMembers.add(member);
}
}
BUnionType newUnionType = unionType.cloneWithMembers(bTypeMembers);
SemType bTypePart = Builder.basicSubType(BasicTypeCode.BT_B_TYPE, BSubType.wrap(newUnionType));
return Core.union(semTypePart, bTypePart);
}

private static boolean isSemType(Type type) {
return type instanceof BNeverType;
}
}
Original file line number Diff line number Diff line change
@@ -135,6 +135,27 @@ protected BUnionType(String typeName, Module pkg, boolean readonly, Class<? exte
this.readonly = readonly;
}

BUnionType cloneWithMembers(List<Type> newMembers) {
BUnionType newUnionType = new BUnionType(memberTypes, this.typeFlags, this.readonly, this.isCyclic);
newUnionType.isCyclic = isCyclic;
newUnionType.memberTypes = newMembers;
newUnionType.originalMemberTypes = newMembers;
newUnionType.nullable = nullable;
newUnionType.flags = flags;
newUnionType.typeFlags = typeFlags;
newUnionType.readonly = readonly;
newUnionType.immutableType = immutableType;
newUnionType.intersectionType = intersectionType;
newUnionType.cachedToString = cachedToString;
newUnionType.resolving = resolving;
newUnionType.resolvingReadonly = resolvingReadonly;

newUnionType.typeName = typeName;
newUnionType.pkg = pkg;

return newUnionType;
}

/**
* Constructor used when defining union type defs where cyclic reference is possible.
*