Skip to content

Commit

Permalink
Refactor code based on the review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lochana-chathura committed Nov 11, 2024
1 parent 6b38208 commit b54109b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9346,7 +9346,7 @@ private BLangLiteral createByteLiteral(Location pos, Byte value) {
}

private BLangExpression createTypeCastExpr(BLangExpression expr, BType targetType) {
if (expr.getBType().tag == targetType.tag && types.isSameType(expr.getBType(), targetType)) {
if (types.isSameTypeIncludingTags(expr.getBType(), targetType)) {
return expr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private void addMethodInvocation(Location pos, BLangSimpleVarRef varRef, BInvoka
// call is generated in BIRGen. Casting to the first listener type should be fine as actual method invocation
// is based on the value rather than the type.
BType listenerType = getListenerType(varRef.getBType());
if (listenerType.tag != varRef.getBType().tag || !types.isSameType(listenerType, varRef.getBType())) {
if (!types.isSameTypeIncludingTags(listenerType, varRef.getBType())) {
BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
castExpr.expr = varRef;
castExpr.setBType(listenerType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2272,7 +2272,7 @@ private void addErrorTypesToSet(BType returnType, LinkedHashSet<BType> errorType
}

private boolean hasNonErrorType(BType returnType) {
return !Core.isEmpty(types.typeCtx(), Core.diff(returnType.semType(), PredefinedType.ERROR));
return !Core.isSubtypeSimple(returnType.semType(), PredefinedType.ERROR);
}

@Override
Expand Down Expand Up @@ -3318,7 +3318,7 @@ public void visit(BLangCheckedExpr checkedExpr, AnalyzerData data) {
BType checkedExprType = checkedExpr.expr.getBType();
SemType errorType = types.getErrorIntersection(checkedExprType.semType());

if (PredefinedType.NEVER.equals(errorType)) {
if (Core.isNever(errorType)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import io.ballerina.types.SemTypePair;
import io.ballerina.types.SemTypes;
import io.ballerina.types.SubtypeData;
import io.ballerina.types.definition.ObjectDefinition;
import io.ballerina.types.definition.ObjectQualifiers;
import io.ballerina.types.subtypedata.BddAllOrNothing;
import io.ballerina.types.subtypedata.BddNode;
import io.ballerina.types.subtypedata.IntSubtype;
Expand Down Expand Up @@ -145,6 +143,8 @@
import static io.ballerina.runtime.api.constants.RuntimeConstants.UNDERSCORE;
import static io.ballerina.types.BasicTypeCode.BT_OBJECT;
import static io.ballerina.types.Core.combineRanges;
import static io.ballerina.types.Core.createIsolatedObject;
import static io.ballerina.types.Core.createServiceObject;
import static io.ballerina.types.Core.isSubtypeSimple;
import static org.ballerinalang.model.symbols.SymbolOrigin.SOURCE;
import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL;
Expand Down Expand Up @@ -3730,10 +3730,8 @@ public boolean isNonNilSimpleBasicTypeOrString(BType bType) {
}

public boolean isSubTypeOfReadOnlyOrIsolatedObjectUnion(BType bType) {
ObjectQualifiers quals = new ObjectQualifiers(true, false, ObjectQualifiers.NetworkQualifier.None);
SemType isolatedObjTy = new ObjectDefinition().define(typeEnv(), quals, new ArrayList<>(0));
return SemTypes.isSubtype(semTypeCtx, bType.semType(),
SemTypes.union(PredefinedType.VAL_READONLY, isolatedObjTy));
SemTypes.union(PredefinedType.VAL_READONLY, createIsolatedObject(semTypeCtx)));
}

private boolean isImmutable(BType type) {
Expand Down Expand Up @@ -4017,9 +4015,7 @@ private boolean checkAttachMethod(BAttachedFunction func) {
}

private boolean isServiceObject(BType bType) {
ObjectQualifiers quals = new ObjectQualifiers(false, false, ObjectQualifiers.NetworkQualifier.Service);
SemType serviceObjTy = new ObjectDefinition().define(typeEnv(), quals, new ArrayList<>(0));
return types.isSubtype(bType, serviceObjTy);
return types.isSubtype(bType, createServiceObject(semTypeCtx));
}
}

Expand Down
2 changes: 2 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public final class Context {
SemType anydataMemo;
SemType jsonMemo;
SemType cloneableMemo;
SemType isolatedObjectMemo;
SemType serviceObjectMemo;

private Context(Env env) {
this.env = env;
Expand Down
27 changes: 27 additions & 0 deletions semtypes/src/main/java/io/ballerina/types/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import io.ballerina.types.definition.ListDefinition;
import io.ballerina.types.definition.MappingDefinition;
import io.ballerina.types.definition.ObjectDefinition;
import io.ballerina.types.definition.ObjectQualifiers;
import io.ballerina.types.subtypedata.AllOrNothingSubtype;
import io.ballerina.types.subtypedata.BddAllOrNothing;
import io.ballerina.types.subtypedata.BddNode;
Expand All @@ -36,6 +38,7 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -911,6 +914,30 @@ public static SemType createCloneable(Context context) {
return ad;
}

public static SemType createIsolatedObject(Context context) {
SemType memo = context.isolatedObjectMemo;
if (memo != null) {
return memo;
}

ObjectQualifiers quals = new ObjectQualifiers(true, false, ObjectQualifiers.NetworkQualifier.None);
SemType isolatedObj = new ObjectDefinition().define(context.env, quals, Collections.emptyList());
context.isolatedObjectMemo = isolatedObj;
return isolatedObj;
}

public static SemType createServiceObject(Context context) {
SemType memo = context.serviceObjectMemo;
if (memo != null) {
return memo;
}

ObjectQualifiers quals = new ObjectQualifiers(false, false, ObjectQualifiers.NetworkQualifier.Service);
SemType serviceObj = new ObjectDefinition().define(context.env, quals, Collections.emptyList());
context.serviceObjectMemo = serviceObj;
return serviceObj;
}

public static SemType createBasicSemType(BasicTypeCode typeCode, SubtypeData subtypeData) {
if (subtypeData instanceof AllOrNothingSubtype) {
if (Common.isAllSubtype(subtypeData)) {
Expand Down

0 comments on commit b54109b

Please sign in to comment.