Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
rdulmina committed Nov 26, 2024
1 parent c0fa12d commit 103fc70
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -2450,10 +2460,9 @@ private BLangPackageVarRef createPackageVarRef(BSymbol symbol) {
return packageVarRef;
}

private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map<BSymbol, BIRVariableDcl> varMap,
boolean checkImpliedType) {
private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map<BSymbol, BIRVariableDcl> varMap) {
for (Map.Entry<BSymbol, BIRVariableDcl> entry : varMap.entrySet()) {
if (isMatchingTypeDescSymbol(entry.getKey(), type, checkImpliedType)) {
if (isMatchingTypeDescSymbol(entry.getKey(), type)) {
return varMap.get(entry.getKey());
}
}
Expand All @@ -2464,7 +2473,7 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map<BSymbol, BIRGlob
boolean checkImpliedType) {
for (Map.Entry<BSymbol, BIRGlobalVariableDcl> 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);
Expand All @@ -2475,26 +2484,25 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map<BSymbol, BIRGlob
return null;
}

private boolean isMatchingTypeDescSymbol(BSymbol symbol, BType targetType, boolean checkImpliedType) {
private boolean isMatchingTypeDescSymbol(BSymbol symbol, BType targetType) {
// ATM there is no proper way to generate the full name of the target typedesc variable for the anonymous
// types because `tsymbol.name.value` is empty for those.
// Hence, we are using the TYPEDESC prefix to identify the typedesc variables and then match type constant type.
if (!symbol.name.value.startsWith(TYPEDESC)) {
return false;
}
// We need to match with the original type first and then with the implied type because for some cases
// there is no typedesc var with the original type.
// We need to match with the original type first and then with the effective type because for some cases
// there is no typedesc var with the original intersection type.
// Eg:
// public type Identifier record {|
// string name;
// |};
//
// table<Identifier> & 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -904,16 +904,16 @@ 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;
if (tag == TypeTags.RECORD) {
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));
}
Expand Down

0 comments on commit 103fc70

Please sign in to comment.