Skip to content

Commit

Permalink
Merge pull request #43341 from lochana-chathura/semIntegration/cleanup3
Browse files Browse the repository at this point in the history
 Rewrite BUnionType's `getMemberTypes()` usages using SemTypes
  • Loading branch information
lochana-chathura authored Nov 14, 2024
2 parents 61538aa + ff06641 commit fc1d58c
Show file tree
Hide file tree
Showing 75 changed files with 1,574 additions and 3,104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public boolean equals(Object obj) {
}

Types types = Types.getInstance(this.context);
return types.isSameType(this.bType, ((AbstractTypeSymbol) obj).getBType());
return types.isSameTypeIncludingTags(this.bType, ((AbstractTypeSymbol) obj).getBType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType;
import org.wso2.ballerinalang.compiler.semantics.model.types.SemNamedType;
import org.wso2.ballerinalang.compiler.tree.BLangConstantValue;
import org.wso2.ballerinalang.compiler.tree.BLangPackage;
import org.wso2.ballerinalang.compiler.tree.BLangTypeDefinition;
import org.wso2.ballerinalang.compiler.util.BArrayState;
import org.wso2.ballerinalang.compiler.util.CompilerContext;
import org.wso2.ballerinalang.compiler.util.ImmutableTypeCloner;
Expand Down Expand Up @@ -156,7 +154,6 @@
import static org.ballerinalang.model.symbols.SymbolOrigin.COMPILED_SOURCE;
import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL;
import static org.ballerinalang.model.symbols.SymbolOrigin.toOrigin;
import static org.wso2.ballerinalang.compiler.parser.BLangAnonymousModelHelper.ANON_PREFIX;
import static org.wso2.ballerinalang.compiler.semantics.model.Scope.NOT_FOUND_ENTRY;
import static org.wso2.ballerinalang.util.LambdaExceptionUtils.rethrow;

Expand Down Expand Up @@ -1359,7 +1356,7 @@ public BType readType(int cpI) throws IOException {
}

SymbolEnv pkgEnv = symTable.pkgEnvMap.get(packageCache.getSymbol(pkgId));
return getType(recordType, pkgEnv, Names.fromString(recordName));
return lookupSymbolInMainSpace(pkgEnv, Names.fromString(recordName));
case TypeTags.TYPEDESC:
BTypedescType typedescType = new BTypedescType(symTable.typeEnv(), null, symTable.typeDesc.tsymbol);
typedescType.constraint = readTypeFromCp();
Expand Down Expand Up @@ -1529,7 +1526,7 @@ public BType readType(int cpI) throws IOException {
} else {
pkgEnv = symTable.pkgEnvMap.get(packageCache.getSymbol(unionsPkgId));
if (pkgEnv != null) {
BType existingUnionType = getType(unionType, pkgEnv, unionName);
BType existingUnionType = lookupSymbolInMainSpace(pkgEnv, unionName);
if (existingUnionType != symTable.noType) {
return existingUnionType;
}
Expand Down Expand Up @@ -1732,7 +1729,7 @@ public BType readType(int cpI) throws IOException {
}

pkgEnv = symTable.pkgEnvMap.get(packageCache.getSymbol(pkgId));
return getType(objectType, pkgEnv, Names.fromString(objName));
return lookupSymbolInMainSpace(pkgEnv, Names.fromString(objName));
case TypeTags.BYTE_ARRAY:
// TODO fix
break;
Expand Down Expand Up @@ -2156,43 +2153,8 @@ private XmlSubtype readXmlSubtype() throws IOException {
// --------------------------------------- End of SemType -----------------------------------------------
}

private BType getType(BType readShape, SymbolEnv pkgEnv, Name name) {
BType type = symbolResolver.lookupSymbolInMainSpace(pkgEnv, name).type;

if (type != symTable.noType && (!name.value.contains(ANON_PREFIX) || types.isSameBIRShape(readShape, type))) {
return type;
}

if (pkgEnv.node != null) {
for (BLangTypeDefinition typeDefinition : ((BLangPackage) pkgEnv.node).typeDefinitions) {
BSymbol symbol = typeDefinition.symbol;

String typeDefName = typeDefinition.name.value;
if (typeDefName.contains(ANON_PREFIX)) {
BType anonType = symbol.type;

if (types.isSameBIRShape(readShape, anonType)) {
return anonType;
}
} else if (typeDefName.equals(name.value)) {
return symbol.type;
}
}
} else {
for (Map.Entry<Name, Scope.ScopeEntry> value : pkgEnv.scope.entries.entrySet()) {
BSymbol symbol = value.getValue().symbol;

if (value.getKey().value.contains(ANON_PREFIX)) {
BType anonType = symbol.type;

if (types.isSameBIRShape(readShape, anonType)) {
return anonType;
}
}
}
}

return type;
private BType lookupSymbolInMainSpace(SymbolEnv pkgEnv, Name name) {
return symbolResolver.lookupSymbolInMainSpace(pkgEnv, name).type;
}

private byte[] readDocBytes(DataInputStream inputStream) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.tools.text.LinePosition;
import io.ballerina.tools.text.LineRange;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.model.TreeBuilder;
import org.ballerinalang.model.elements.Flag;
import org.ballerinalang.model.elements.PackageID;
Expand Down Expand Up @@ -59,6 +60,7 @@
import org.wso2.ballerinalang.compiler.bir.model.VarScope;
import org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer;
import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLocation;
import org.wso2.ballerinalang.compiler.semantics.analyzer.SemTypeHelper;
import org.wso2.ballerinalang.compiler.semantics.analyzer.Types;
import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol;
Expand Down Expand Up @@ -2801,10 +2803,7 @@ private void generateMappingAccess(BLangIndexBasedAccess astIndexBasedAccessExpr
if (astIndexBasedAccessExpr.getKind() == NodeKind.XML_ATTRIBUTE_ACCESS_EXPR) {
insKind = InstructionKind.XML_ATTRIBUTE_STORE;
keyRegIndex = getQNameOP(astIndexBasedAccessExpr.indexExpr, keyRegIndex);
} else if (astAccessExprExprType.tag == TypeTags.OBJECT ||
(astAccessExprExprType.tag == TypeTags.UNION &&
Types.getImpliedType(((BUnionType) astAccessExprExprType).getMemberTypes().iterator()
.next()).tag == TypeTags.OBJECT)) {
} else if (SemTypeHelper.isSubtypeSimple(astAccessExprExprType, PredefinedType.OBJECT)) {
insKind = InstructionKind.OBJECT_STORE;
} else {
insKind = InstructionKind.MAP_STORE;
Expand Down Expand Up @@ -2833,10 +2832,7 @@ private void generateMappingAccess(BLangIndexBasedAccess astIndexBasedAccessExpr
keyRegIndex);
this.varAssignment = false;
return;
} else if (astAccessExprExprType.tag == TypeTags.OBJECT ||
(astAccessExprExprType.tag == TypeTags.UNION &&
Types.getImpliedType(((BUnionType) astAccessExprExprType).getMemberTypes().iterator()
.next()).tag == TypeTags.OBJECT)) {
} else if (SemTypeHelper.isSubtypeSimple(astAccessExprExprType, PredefinedType.OBJECT)) {
insKind = InstructionKind.OBJECT_LOAD;
} else {
insKind = InstructionKind.MAP_LOAD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.ballerina.identifier.Utils;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.compiler.BLangCompilerException;
import org.ballerinalang.model.elements.Flag;
import org.ballerinalang.model.elements.PackageID;
Expand Down Expand Up @@ -53,6 +54,7 @@
import org.wso2.ballerinalang.compiler.bir.model.VarKind;
import org.wso2.ballerinalang.compiler.bir.model.VarScope;
import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLocation;
import org.wso2.ballerinalang.compiler.semantics.analyzer.SemTypeHelper;
import org.wso2.ballerinalang.compiler.semantics.model.Scope;
import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAttachedFunction;
Expand All @@ -63,11 +65,9 @@
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag;
import org.wso2.ballerinalang.compiler.semantics.model.types.BErrorType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType;
import org.wso2.ballerinalang.compiler.util.Name;
import org.wso2.ballerinalang.compiler.util.Names;
import org.wso2.ballerinalang.compiler.util.TypeTags;
Expand Down Expand Up @@ -985,20 +985,7 @@ private boolean isObservable(Call callIns) {
* @return True if an error can be assigned and false otherwise
*/
private boolean isErrorAssignable(BIRVariableDcl variableDcl) {
boolean isErrorAssignable = false;
if (variableDcl.type instanceof BUnionType returnUnionType) {
boolean b = false;
for (BType type : returnUnionType.getMemberTypes()) {
if (type instanceof BErrorType) {
b = true;
break;
}
}
isErrorAssignable = b;
} else if (variableDcl.type instanceof BErrorType) {
isErrorAssignable = true;
}
return isErrorAssignable;
return SemTypeHelper.containsBasicType(variableDcl.type, PredefinedType.ERROR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ private void linkTypeDefinitions(BIRPackage module, boolean isEntry) {

private void linkModuleFunction(PackageID packageID, String initClass, String funcName) {
BInvokableType funcType =
new BInvokableType(symbolTable.typeEnv(), Collections.emptyList(), null, BType.createNilType(), null);
new BInvokableType(symbolTable.typeEnv(), Collections.emptyList(), null, symbolTable.nilType, null);
BIRFunction moduleStopFunction = new BIRFunction(null, new Name(funcName), 0, funcType, new Name(""), 0,
VIRTUAL);
birFunctionMap.put(JvmCodeGenUtil.getPackageName(packageID) + funcName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.ballerina.identifier.Utils;
import io.ballerina.tools.diagnostics.Location;
import io.ballerina.types.PredefinedType;
import org.ballerinalang.compiler.BLangCompilerException;
import org.ballerinalang.model.elements.PackageID;
import org.objectweb.asm.Handle;
Expand All @@ -43,6 +44,7 @@
import org.wso2.ballerinalang.compiler.bir.model.BIRTerminator;
import org.wso2.ballerinalang.compiler.bir.model.VarKind;
import org.wso2.ballerinalang.compiler.bir.model.VarScope;
import org.wso2.ballerinalang.compiler.semantics.analyzer.SemTypeHelper;
import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol;
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableTypeSymbol;
Expand Down Expand Up @@ -404,15 +406,7 @@ private void handleErrorRetInUnion(int returnVarRefIndex, List<BIRNode.ChannelDe
return;
}

boolean errorIncluded = false;
for (BType member : bType.getMemberTypes()) {
member = JvmCodeGenUtil.getImpliedType(member);
if (member.tag == TypeTags.ERROR) {
errorIncluded = true;
break;
}
}

boolean errorIncluded = SemTypeHelper.containsBasicType(bType, PredefinedType.ERROR);
if (errorIncluded) {
this.mv.visitVarInsn(ALOAD, returnVarRefIndex);
this.mv.visitVarInsn(ALOAD, localVarOffset);
Expand Down
Loading

0 comments on commit fc1d58c

Please sign in to comment.