Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BIR/JAR for anon types #35870

Merged
merged 3 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType;
import org.wso2.ballerinalang.compiler.semantics.model.types.TypeFlags;
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.tree.expressions.BLangLiteral;
import org.wso2.ballerinalang.compiler.util.BArrayState;
import org.wso2.ballerinalang.compiler.util.CompilerContext;
Expand Down Expand Up @@ -117,6 +119,7 @@
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 @@ -1211,7 +1214,7 @@ public BType readType(int cpI) throws IOException {
}

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

pkgEnv = symTable.pkgEnvMap.get(packageCache.getSymbol(pkgId));
return symbolResolver.lookupSymbolInMainSpace(pkgEnv, names.fromString(objName)).type;
return getType(objectType, pkgEnv, names.fromString(objName));
case TypeTags.BYTE_ARRAY:
// TODO fix
break;
Expand Down Expand Up @@ -1720,6 +1722,45 @@ private void populateIntersectionTypeReferencedFunctions(DataInputStream inputSt
}
}

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 byte[] readDocBytes(DataInputStream inputStream) throws IOException {
int docLength = inputStream.readInt();
byte[] docBytes = new byte[docLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ private void loadUserDefinedType(MethodVisitor mv, BType bType) {
//class symbols
String fieldName = defName.isEmpty() ? getTypeFieldName(toNameString(typeToLoad)) : defName;

boolean samePackage = JvmCodeGenUtil.isSameModule(this.packageID, packageID);
boolean samePackage = JvmCodeGenUtil.isSameModule(this.packageID, pkgID);

// if name contains $anon and doesn't belong to the same package, load type using getAnonType() method.
if (!samePackage && (fieldName.contains(BLangAnonymousModelHelper.ANON_PREFIX)
Expand Down
Loading