Skip to content

Commit

Permalink
Merge pull request #43470 from ballerina-platform/dedup-birpkg-imports
Browse files Browse the repository at this point in the history
Fix duplicates being added to importModules
  • Loading branch information
gimantha authored Oct 9, 2024
2 parents c2c47b3 + 90b8860 commit 0226330
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.PrintStream;
import java.util.List;
import java.util.Set;

import static org.wso2.ballerinalang.compiler.bir.emit.EmitterUtils.emitBasicBlockRef;
import static org.wso2.ballerinalang.compiler.bir.emit.EmitterUtils.emitFlags;
Expand Down Expand Up @@ -101,7 +102,7 @@ public static String emitModule(BIRNode.BIRPackage mod) {
return modStr;
}

private static String emitImports(List<BIRNode.BIRImportModule> impMods) {
private static String emitImports(Set<BIRNode.BIRImportModule> impMods) {

StringBuilder impStr = new StringBuilder();
for (BIRNode.BIRImportModule mod : impMods) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand All @@ -55,7 +56,7 @@ protected BIRNode(Location pos) {
*/
public static class BIRPackage extends BIRNode {
public final PackageID packageID;
public final List<BIRImportModule> importModules;
public final Set<BIRImportModule> importModules;
public final List<BIRTypeDefinition> typeDefs;
public final List<BIRGlobalVariableDcl> globalVars;
public final Set<BIRGlobalVariableDcl> importedGlobalVarsDummyVarDcls;
Expand All @@ -74,7 +75,7 @@ public BIRPackage(Location pos, Name org, Name pkgName, Name name, Name version,
String sourceRoot, boolean skipTest, boolean isTestPkg) {
super(pos);
packageID = new PackageID(org, pkgName, name, version, sourceFileName, sourceRoot, isTestPkg, skipTest);
this.importModules = new ArrayList<>();
this.importModules = new LinkedHashSet<>();
this.typeDefs = new ArrayList<>();
this.globalVars = new ArrayList<>();
this.importedGlobalVarsDummyVarDcls = new HashSet<>();
Expand Down Expand Up @@ -107,6 +108,24 @@ public BIRImportModule(Location pos, Name org, Name name, Name version) {
public void accept(BIRVisitor visitor) {
visitor.visit(this);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

return this.packageID.equals(((BIRImportModule) o).packageID);
}

@Override
public int hashCode() {
return this.packageID.hashCode();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import static org.wso2.ballerinalang.compiler.bir.writer.BIRWriterUtils.writeConstValue;

Expand Down Expand Up @@ -95,7 +96,7 @@ public byte[] serialize() {

// private methods

private void writeImportModuleDecls(ByteBuf buf, List<BIRNode.BIRImportModule> birImpModList) {
private void writeImportModuleDecls(ByteBuf buf, Set<BIRNode.BIRImportModule> birImpModList) {
buf.writeInt(birImpModList.size());
birImpModList.forEach(impMod -> {
PackageID packageID = impMod.packageID;
Expand Down

0 comments on commit 0226330

Please sign in to comment.