Skip to content

Commit

Permalink
Refactor BAnydataType semtype resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
lochana-chathura committed Nov 19, 2024
1 parent 97bd5c4 commit b38c20b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ public void bootstrapAnydataType() {
continue;
}
BUnionType type = (BUnionType) Types.getImpliedType(entry.symbol.type);
symTable.anydataType = new BAnydataType(type);
symTable.anydataType = new BAnydataType(types.semTypeCtx, type);
Optional<BIntersectionType> immutableType = Types.getImmutableType(symTable, PackageID.ANNOTATIONS, type);
if (immutableType.isPresent()) {
Types.addImmutableType(symTable, PackageID.ANNOTATIONS, symTable.anydataType, immutableType.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private BType createTypeParamType(BSymbol symbol, BType type, Name name, long fl
}

private BAnydataType createAnydataType(BUnionType unionType, Name name, long flags) {
BAnydataType anydataType = new BAnydataType(unionType);
BAnydataType anydataType = new BAnydataType(types.typeCtx(), unionType);
Optional<BIntersectionType> immutableType = Types.getImmutableType(symTable, PackageID.ANNOTATIONS,
unionType);
immutableType.ifPresent(bIntersectionType ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3210,7 +3210,7 @@ public BType getSafeType(BType bType, boolean liftNil, boolean liftError) {
case TypeTags.ANY:
return BAnyType.newNilLiftedBAnyType();
case TypeTags.ANYDATA:
return new BAnydataType((BAnydataType) type, false);
return BAnydataType.newNilLiftedBAnydataType((BAnydataType) type);

Check warning on line 3213 in compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java#L3213

Added line #L3213 was not covered by tests
case TypeTags.READONLY:
if (liftError) {
return symTable.anyAndReadonly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ private void defineAnydataCyclicTypeAndDependentTypes() {
stringType, xmlType);
addCyclicArrayMapTableOfMapMembers(anyDataInternal);

anydataType = new BAnydataType(anyDataInternal);
anydataType = new BAnydataType(types.typeCtx(), anyDataInternal);
PackageID pkgID = rootPkgSymbol.pkgID;
Optional<BIntersectionType> immutableType = Types.getImmutableType(this, pkgID, anyDataInternal);
if (immutableType.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import io.ballerina.types.Context;
import io.ballerina.types.Core;
import io.ballerina.types.Env;
import io.ballerina.types.PredefinedType;
import io.ballerina.types.SemType;
import org.ballerinalang.model.Name;
import org.ballerinalang.model.types.TypeKind;
Expand All @@ -31,8 +31,6 @@

import java.util.LinkedHashSet;

import static io.ballerina.types.PredefinedType.VAL_READONLY;

/**
* {@code BAnydataType} represents the data types in Ballerina.
*
Expand All @@ -41,17 +39,19 @@
public class BAnydataType extends BUnionType {
private boolean nullable;
private static final int INITIAL_CAPACITY = 10;
private final Context typeCtx;

public BAnydataType(Env env, BTypeSymbol tsymbol, Name name, long flags, boolean nullable) {
super(env, tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), false);
private BAnydataType(Context typeCtx, BTypeSymbol tsymbol, Name name, long flags, boolean nullable) {
super(typeCtx.env, tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY), false);
this.tag = TypeTags.ANYDATA;
this.setFlags(flags);
this.name = name;
this.isCyclic = true;
this.nullable = nullable;
this.typeCtx = typeCtx;
}

public BAnydataType(BUnionType type) {
public BAnydataType(Context typeCtx, BUnionType type) {
super(type.env, type.tsymbol, new LinkedHashSet<>(type.memberTypes.size()),
Symbols.isFlagOn(type.getFlags(), Flags.READONLY));
this.tag = TypeTags.ANYDATA;
Expand All @@ -60,16 +60,18 @@ public BAnydataType(BUnionType type) {
this.setFlags(type.getFlags());
this.nullable = type.isNullable();
mergeUnionType(type);
this.typeCtx = typeCtx;
}

public BAnydataType(BAnydataType type, boolean nullable) {
super(type.env, type.tsymbol, new LinkedHashSet<>(INITIAL_CAPACITY),
Symbols.isFlagOn(type.getFlags(), Flags.READONLY));
this.setFlags(type.getFlags());
this.tag = TypeTags.ANYDATA;
this.isCyclic = true;
this.nullable = nullable;
mergeUnionType(type);
public static BAnydataType newNilLiftedBAnydataType(BAnydataType type) {
BAnydataType result = new BAnydataType(type.typeCtx, type);
result.nullable = false;
return result;

Check warning on line 69 in compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java#L67-L69

Added lines #L67 - L69 were not covered by tests
}

public static BAnydataType newImmutableBAnydataType(BAnydataType type, BTypeSymbol typeSymbol, Name name,
boolean nullable) {
return new BAnydataType(type.typeCtx, typeSymbol, name, type.getFlags() | Flags.READONLY, nullable);
}

@Override
Expand Down Expand Up @@ -100,10 +102,13 @@ public <T, R> R accept(BTypeVisitor<T, R> visitor, T t) {

@Override
public SemType semType() {
SemType s = Core.createAnydata(Context.from(env));
SemType anydata = Core.createAnydata(typeCtx);
if (!nullable) {
anydata = Core.diff(anydata, PredefinedType.NIL);

Check warning on line 107 in compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/types/BAnydataType.java#L107

Added line #L107 was not covered by tests
}
if (Symbols.isFlagOn(getFlags(), Flags.READONLY)) {
return Core.intersect(s, VAL_READONLY);
anydata = Core.intersect(anydata, PredefinedType.VAL_READONLY);
}
return s;
return anydata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -750,16 +750,14 @@ private static BAnydataType defineImmutableAnydataType(SymbolEnv env, PackageID
BTypeSymbol immutableAnydataTSymbol = getReadonlyTSymbol(type.tsymbol, env, pkgId, owner);

if (immutableAnydataTSymbol != null) {
BAnydataType immutableAnydataType =
new BAnydataType(type.env, immutableAnydataTSymbol,
immutableAnydataTSymbol.name, type.getFlags() | Flags.READONLY,
BAnydataType immutableAnydataType = BAnydataType.newImmutableBAnydataType(type, immutableAnydataTSymbol,
immutableAnydataTSymbol.name,
type.isNullable());
immutableAnydataTSymbol.type = immutableAnydataType;
return immutableAnydataType;
}
return new BAnydataType(type.env, null,
Types.getImmutableTypeName(TypeKind.ANYDATA.typeName()),
type.getFlags() | Flags.READONLY, type.isNullable());
return BAnydataType.newImmutableBAnydataType(type, null,
Types.getImmutableTypeName(TypeKind.ANYDATA.typeName()), type.isNullable());

Check warning on line 760 in compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/ImmutableTypeCloner.java

View check run for this annotation

Codecov / codecov/patch

compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/ImmutableTypeCloner.java#L759-L760

Added lines #L759 - L760 were not covered by tests
}

private static BJSONType defineImmutableJsonType(SymbolEnv env, PackageID pkgId, BSymbol owner, Names names,
Expand Down

0 comments on commit b38c20b

Please sign in to comment.