From 103fc70d91786d538e13b4c9abc16d94aee6fac5 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 26 Nov 2024 11:41:29 +0530 Subject: [PATCH] Refactor code --- .../runtime/internal/values/MapValueImpl.java | 1 - .../ballerinalang/compiler/bir/BIRGen.java | 50 +++++++++++-------- .../compiler/desugar/Desugar.java | 6 +-- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index 46a617c46545..ebbdce07f208 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -23,7 +23,6 @@ import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BFunctionPointer; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index c4ae8a663456..3d52bfd7e739 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -82,6 +82,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BIntersectionType; import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; import org.wso2.ballerinalang.compiler.semantics.model.types.BTableType; @@ -216,6 +217,7 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; + import javax.xml.XMLConstants; import static org.ballerinalang.model.tree.NodeKind.CLASS_DEFN; @@ -2408,20 +2410,28 @@ private BIRNonTerminator.NewStructure createNewStructureInst(BIROperand typeDesc } private BIRVariableDcl getTypedescVariable(BType type, Location pos) { - BIRVariableDcl variableDcl = findInPackageScope(type); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + if (type.tag == TypeTags.TYPEREFDESC) { + BType referredType = ((BTypeReferenceType) type).referredType; + int tag = referredType.tag; + if (tag == TypeTags.RECORD) { + type = referredType; + } + } - variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, false); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + BIRVariableDcl variableDcl = findInPackageScope(type); + if (variableDcl != null && variableDcl.initialized) { + return variableDcl; + } - variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, true); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap); + if (variableDcl != null && variableDcl.initialized) { + return variableDcl; + } variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, false); - if (variableDcl != null && variableDcl.initialized) return variableDcl; - - variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, true); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + if (variableDcl != null && variableDcl.initialized) { + return variableDcl; + } // Create new type desc instruction if the typedesc variable is not found // TODO: we need to handle duplicate typedesc creation for some cases. @@ -2450,10 +2460,9 @@ private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { return packageVarRef; } - private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap, - boolean checkImpliedType) { + private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { - if (isMatchingTypeDescSymbol(entry.getKey(), type, checkImpliedType)) { + if (isMatchingTypeDescSymbol(entry.getKey(), type)) { return varMap.get(entry.getKey()); } } @@ -2464,7 +2473,7 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map entry : varMap.entrySet()) { BSymbol varSymbol = entry.getKey(); - if (isMatchingTypeDescSymbol(varSymbol, type, checkImpliedType)) { + if (isMatchingTypeDescSymbol(varSymbol, type)) { BIRGlobalVariableDcl globalVarDcl = varMap.get(varSymbol); if (!isInSameModule(varSymbol, env.enclPkg.packageID) || env.enclPkg.packageID.isTestPkg) { this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); @@ -2475,26 +2484,25 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map & readonly q = table [{name: "Jo"}]; - // Row type will be Immutable Identifier and there will be not intersection type created for that + // Row type will be Immutable Identifier and there will be no intersection type created for that BType constraint = ((BTypedescType) symbol.type).constraint; - constraint = checkImpliedType ? Types.getImpliedType(constraint) : constraint; - targetType = checkImpliedType ? Types.getImpliedType(targetType) : targetType; - return constraint == targetType; + return constraint == targetType || + (targetType.tag == TypeTags.INTERSECTION && (((BIntersectionType) targetType).effectiveType == constraint)); } private void createNewTypedescInst(BType resolveType, Location position) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index b60244a23e60..6d9554442760 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -904,9 +904,6 @@ private void createTypedescVariable(BType type, Location pos) { } Name name = generateTypedescVariableName(type); - BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); - BSymbol owner = this.env.scope.owner; - BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); if (type.tag == TypeTags.TYPEREFDESC) { BType referredType = ((BTypeReferenceType) type).referredType; int tag = referredType.tag; @@ -914,6 +911,9 @@ private void createTypedescVariable(BType type, Location pos) { type = referredType; } } + BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); + BSymbol owner = this.env.scope.owner; + BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type); typedescList.add(createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, varSymbol)); }