From b4e123470058def0f686fc45c2298c23457dc116 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 12 Oct 2023 11:54:01 +0530 Subject: [PATCH 1/9] Clean up BIR after codegen phase --- .../compiler/bir/codegen/CodeGenerator.java | 36 ++++++++++++++++++- .../compiler/bir/codegen/JvmDesugarPhase.java | 20 ----------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java index 40b9848ef08b..c741c6244fae 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java @@ -21,6 +21,7 @@ import org.wso2.ballerinalang.compiler.PackageCache; import org.wso2.ballerinalang.compiler.bir.BIRGenUtils; import org.wso2.ballerinalang.compiler.bir.codegen.optimizer.LargeMethodOptimizer; +import org.wso2.ballerinalang.compiler.bir.model.BIRNode; import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLog; import org.wso2.ballerinalang.compiler.semantics.analyzer.Types; import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable; @@ -99,12 +100,45 @@ private CompiledJarFile generate(BPackageSymbol packageSymbol) { // TODO Get-rid of the following assignment CompiledJarFile compiledJarFile = jvmPackageGen.generate(packageSymbol.bir, true); - + cleanUpBirPackage(packageSymbol); //Revert encoding identifier names JvmDesugarPhase.replaceEncodedModuleIdentifiers(packageSymbol.bir, originalIdentifierMap); return compiledJarFile; } + private static void cleanUpBirPackage(BPackageSymbol packageSymbol) { + packageSymbol.birPackageFile = null; + BIRNode.BIRPackage bir = packageSymbol.bir; + for (BIRNode.BIRTypeDefinition typeDef : bir.typeDefs) { + for (BIRNode.BIRFunction attachedFunc : typeDef.attachedFuncs) { + cleanUpBirFunction(attachedFunc); + } + typeDef.annotAttachments = null; + } + bir.importedGlobalVarsDummyVarDcls.clear(); + for (BIRNode.BIRFunction function : bir.functions) { + cleanUpBirFunction(function); + } + bir.annotations.clear(); + bir.constants.clear(); + bir.serviceDecls.clear(); + } + + private static void cleanUpBirFunction(BIRNode.BIRFunction function) { + function.receiver = null; + function.localVars = null; + function.returnVariable = null; + function.parameters = null; + function.basicBlocks = null; + function.errorTable = null; + function.workerChannels = null; + function.annotAttachments = null; + function.returnTypeAnnots = null; + function.dependentGlobalVars = null; + function.pathParams = null; + function.restPathParam = null; + } + private void populateExternalMap(JvmPackageGen jvmPackageGen) { String nativeMap = System.getenv("BALLERINA_NATIVE_MAP"); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java index 58de262b3ae5..001f0421b9d3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java @@ -276,9 +276,6 @@ private static void encodeAttachedFunctionIdentifiers(List fu private static void encodeGlobalVariableIdentifiers(List globalVars, HashMap encodedVsInitialIds) { for (BIRNode.BIRGlobalVariableDcl globalVar : globalVars) { - if (globalVar == null) { - continue; - } globalVar.name = Names.fromString(encodeNonFunctionIdentifier(globalVar.name.value, encodedVsInitialIds)); } } @@ -295,7 +292,6 @@ static void replaceEncodedModuleIdentifiers(BIRNode.BIRPackage module, HashMap encodedVsInitialIds) { replaceEncodedPackageIdentifiers(module.packageID, encodedVsInitialIds); replaceEncodedGlobalVariableIdentifiers(module.globalVars, encodedVsInitialIds); - replaceEncodedImportedGlobalVariableIdentifiers(module.importedGlobalVarsDummyVarDcls, encodedVsInitialIds); replaceEncodedFunctionIdentifiers(module.functions, encodedVsInitialIds); replaceEncodedTypeDefIdentifiers(module.typeDefs, encodedVsInitialIds); } @@ -337,12 +333,6 @@ private static void replaceEncodedFunctionIdentifiers(List function HashMap encodedVsInitialIds) { for (BIRFunction function : functions) { function.name = getInitialIdString(function.name, encodedVsInitialIds); - for (BIRNode.BIRVariableDcl localVar : function.localVars) { - if (localVar.metaVarName == null) { - continue; - } - localVar.metaVarName = getInitialIdString(localVar.metaVarName, encodedVsInitialIds); - } for (BIRNode.BIRParameter parameter : function.requiredParams) { parameter.name = getInitialIdString(parameter.name, encodedVsInitialIds); } @@ -383,16 +373,6 @@ private static void replaceEncodedAttachedFunctionIdentifiers(List globalVars, HashMap encodedVsInitialIds) { - for (BIRNode.BIRGlobalVariableDcl globalVar : globalVars) { - if (globalVar == null) { - continue; - } - globalVar.name = getInitialIdString(globalVar.name, encodedVsInitialIds); - } - } - - private static void replaceEncodedImportedGlobalVariableIdentifiers(Set globalVars, - HashMap encodedVsInitialIds) { for (BIRNode.BIRGlobalVariableDcl globalVar : globalVars) { globalVar.name = getInitialIdString(globalVar.name, encodedVsInitialIds); } From e97fafff228672d34a013a9b320b26b3afade748 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 12 Oct 2023 12:05:49 +0530 Subject: [PATCH 2/9] Refactor JvmDesugarPhase --- .../compiler/bir/codegen/JvmDesugarPhase.java | 46 ++----------------- .../compiler/bir/codegen/JvmPackageGen.java | 2 +- .../compiler/bir/codegen/JvmValueGen.java | 9 ++-- .../codegen/interop/ExternalMethodGen.java | 11 ++--- 4 files changed, 15 insertions(+), 53 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java index 001f0421b9d3..b406cce3835e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmDesugarPhase.java @@ -19,7 +19,6 @@ package org.wso2.ballerinalang.compiler.bir.codegen; import io.ballerina.identifier.Utils; -import org.ballerinalang.compiler.BLangCompilerException; import org.ballerinalang.model.elements.PackageID; import org.wso2.ballerinalang.compiler.bir.codegen.methodgen.InitMethodGen; import org.wso2.ballerinalang.compiler.bir.model.BIRNode; @@ -59,28 +58,15 @@ */ public class JvmDesugarPhase { - public static void addDefaultableBooleanVarsToSignature(BIRFunction func, BType booleanType) { + private JvmDesugarPhase() { + } + public static void addDefaultableBooleanVarsToSignature(BIRFunction func) { func.type = new BInvokableType(func.type.paramTypes, func.type.restType, func.type.retType, func.type.tsymbol); BInvokableType type = func.type; func.type.paramTypes = updateParamTypesWithDefaultableBooleanVar(func.type.paramTypes, - type.restType, booleanType); - } - - public static void enrichWithDefaultableParamInits(BIRFunction currentFunc, InitMethodGen initMethodGen) { - int k = 1; - List functionParams = new ArrayList<>(); - List localVars = currentFunc.localVars; - while (k < localVars.size()) { - BIRVariableDcl localVar = localVars.get(k); - if (localVar instanceof BIRFunctionParameter) { - functionParams.add((BIRFunctionParameter) localVar); - } - k += 1; - } - - initMethodGen.resetIds(); + type.restType); } public static BIRBasicBlock insertAndGetNextBasicBlock(List basicBlocks, @@ -94,11 +80,8 @@ public static int getNextDesugarBBId(InitMethodGen initMethodGen) { return initMethodGen.incrementAndGetNextId(); } - private static List updateParamTypesWithDefaultableBooleanVar(List funcParams, BType restType, - BType booleanType) { - + private static List updateParamTypesWithDefaultableBooleanVar(List funcParams, BType restType) { List paramTypes = new ArrayList<>(); - int counter = 0; int size = funcParams == null ? 0 : funcParams.size(); while (counter < size) { @@ -160,17 +143,6 @@ private static void rewriteRecordInitFunction(BIRFunction func, BRecordType reco func.localVars = updatedLocalVars; } - private static BIRFunctionParameter getFunctionParam(BIRFunctionParameter localVar) { - if (localVar == null) { - throw new BLangCompilerException("Invalid function parameter"); - } - - return localVar; - } - - private JvmDesugarPhase() { - } - static HashMap encodeModuleIdentifiers(BIRNode.BIRPackage module) { HashMap encodedVsInitialIds = new HashMap<>(); encodePackageIdentifiers(module.packageID, encodedVsInitialIds); @@ -396,14 +368,6 @@ private static String encodeNonFunctionIdentifier(String identifier, HashMap encodedVsInitialIds) { - String initialString = encodedVsInitialIds.get(encodedIdString); - if (initialString != null) { - return initialString; - } - return encodedIdString; - } - private static Name getInitialIdString(Name encodedIdString, HashMap encodedVsInitialIds) { String initialString = encodedVsInitialIds.get(encodedIdString.value); if (initialString != null) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java index 30e89a0b681f..2ee14d421d5f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java @@ -623,7 +623,7 @@ private BIRFunctionWrapper getBirFunctionWrapper(boolean isEntry, PackageID pack birModuleClassName, lookupKey, this); } else { if (isEntry && birFunc.receiver == null) { - addDefaultableBooleanVarsToSignature(birFunc, symbolTable.booleanType); + addDefaultableBooleanVarsToSignature(birFunc); } birFuncWrapperOrError = getFunctionWrapper(birFunc, packageID, birModuleClassName); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java index 22d578359833..edf4f2d5a544 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java @@ -105,7 +105,6 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.UNSUPPORTED_OPERATION_EXCEPTION; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.VALUE_CLASS_PREFIX; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmDesugarPhase.addDefaultableBooleanVarsToSignature; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmDesugarPhase.enrichWithDefaultableParamInits; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen.computeLockNameFromString; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.CAST_B_MAPPING_INITIAL_VALUE_ENTRY; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_MAP_ARRAY; @@ -186,13 +185,13 @@ private static void desugarObjectMethods(PackageID module, BType bType, desugarOldExternFuncs((OldStyleExternalFunctionWrapper) extFuncWrapper, birFunc, initMethodGen); } else if (birFunc instanceof JMethodBIRFunction) { desugarInteropFuncs((JMethodBIRFunction) birFunc, initMethodGen); - enrichWithDefaultableParamInits(birFunc, initMethodGen); + initMethodGen.resetIds(); } else if (!(birFunc instanceof JFieldBIRFunction)) { - enrichWithDefaultableParamInits(birFunc, initMethodGen); + initMethodGen.resetIds(); } } else { - addDefaultableBooleanVarsToSignature(birFunc, jvmPackageGen.symbolTable.booleanType); - enrichWithDefaultableParamInits(birFunc, initMethodGen); + addDefaultableBooleanVarsToSignature(birFunc); + initMethodGen.resetIds(); } } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java index c98b888361a4..2356406e872c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/interop/ExternalMethodGen.java @@ -47,7 +47,6 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.toNameString; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmDesugarPhase.addDefaultableBooleanVarsToSignature; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmDesugarPhase.enrichWithDefaultableParamInits; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmDesugarPhase.insertAndGetNextBasicBlock; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen.cleanupPackageName; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen.getFunctionWrapper; @@ -93,12 +92,12 @@ public static void injectDefaultParamInits(BIRPackage module, InitMethodGen init jvmPackageGen); if (extFuncWrapper instanceof OldStyleExternalFunctionWrapper) { desugarOldExternFuncs((OldStyleExternalFunctionWrapper) extFuncWrapper, birFunc, initMethodGen); - enrichWithDefaultableParamInits(birFunc, initMethodGen); + initMethodGen.resetIds(); } else if (birFunc instanceof JMethodBIRFunction) { desugarInteropFuncs((JMethodBIRFunction) birFunc, initMethodGen); - enrichWithDefaultableParamInits(birFunc, initMethodGen); + initMethodGen.resetIds(); } else if (!(birFunc instanceof JFieldBIRFunction)) { - enrichWithDefaultableParamInits(birFunc, initMethodGen); + initMethodGen.resetIds(); } } } @@ -172,7 +171,7 @@ public static OldStyleExternalFunctionWrapper createOldStyleExternalFunctionWrap boolean isEntryModule, SymbolTable symbolTable) { List jMethodPramTypes = new ArrayList<>(birFunc.type.paramTypes); if (isEntryModule) { - addDefaultableBooleanVarsToSignature(birFunc, symbolTable.booleanType); + addDefaultableBooleanVarsToSignature(birFunc); } BInvokableType functionTypeDesc = birFunc.type; @@ -226,7 +225,7 @@ public static BIRFunctionWrapper createExternalFunctionWrapper(boolean isEntry, String jClassName = jvmPackageGen.lookupExternClassName(cleanupPackageName(pkgName), lookupKey); if (birFunc instanceof JBIRFunction) { if (isEntry) { - addDefaultableBooleanVarsToSignature(birFunc, jvmPackageGen.symbolTable.booleanType); + addDefaultableBooleanVarsToSignature(birFunc); } birFuncWrapper = getFunctionWrapper(birFunc, packageID, birModuleClassName); } else { From 4a3cde795bf8fde84abc220a8250209d2d4b55e4 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 12 Oct 2023 12:07:53 +0530 Subject: [PATCH 3/9] Refactor CodeGenerator --- .../compiler/bir/codegen/CodeGenerator.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java index c741c6244fae..b6017da3d339 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/CodeGenerator.java @@ -43,14 +43,12 @@ public class CodeGenerator { private static final CompilerContext.Key CODE_GEN = new CompilerContext.Key<>(); - private SymbolTable symbolTable; - private PackageCache packageCache; - private BLangDiagnosticLog dlog; - private Types types; - private LargeMethodOptimizer largeMethodOptimizer; + private final SymbolTable symbolTable; + private final PackageCache packageCache; + private final BLangDiagnosticLog dlog; + private final Types types; private CodeGenerator(CompilerContext compilerContext) { - compilerContext.put(CODE_GEN, this); this.symbolTable = SymbolTable.getInstance(compilerContext); this.packageCache = PackageCache.getInstance(compilerContext); @@ -59,12 +57,10 @@ private CodeGenerator(CompilerContext compilerContext) { } public static CodeGenerator getInstance(CompilerContext context) { - CodeGenerator codeGenerator = context.get(CODE_GEN); if (codeGenerator == null) { codeGenerator = new CodeGenerator(context); } - return codeGenerator; } @@ -80,7 +76,7 @@ public CompiledJarFile generateTestModule(BLangPackage bLangTestablePackage) { private CompiledJarFile generate(BPackageSymbol packageSymbol) { // Split large BIR functions into smaller methods - largeMethodOptimizer = new LargeMethodOptimizer(symbolTable); + LargeMethodOptimizer largeMethodOptimizer = new LargeMethodOptimizer(symbolTable); largeMethodOptimizer.splitLargeBIRFunctions(packageSymbol.bir); // Desugar BIR to include the observations From 7aff2ff2827b3cb8ffeacec428dd72b48a7c4877 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 12 Oct 2023 12:19:01 +0530 Subject: [PATCH 4/9] Fix listenerDeclarationFound method in JvmPackageGen --- .../compiler/bir/codegen/JvmPackageGen.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java index 2ee14d421d5f..fe3a1b768c88 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java @@ -837,12 +837,10 @@ private BIRFunction getFunction(BIRPackage module, String funcName) { private boolean listenerDeclarationFound(BPackageSymbol packageSymbol) { if (packageSymbol.bir != null && packageSymbol.bir.isListenerAvailable) { return true; - } else { - for (BPackageSymbol importPkgSymbol : packageSymbol.imports) { - if (importPkgSymbol == null) { - continue; - } - return listenerDeclarationFound(importPkgSymbol); + } + for (BPackageSymbol importPkgSymbol : packageSymbol.imports) { + if (importPkgSymbol != null && listenerDeclarationFound(importPkgSymbol)) { + return true; } } return false; From 8a83469bb80f4b240088bbfcf8bd49acc0c49b5c Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 12 Oct 2023 12:19:56 +0530 Subject: [PATCH 5/9] Refactor BIRModelTest --- .../test/java/org/ballerinalang/test/bir/BIRModelTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bir/BIRModelTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bir/BIRModelTest.java index cf851886076f..165cb3988864 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bir/BIRModelTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bir/BIRModelTest.java @@ -22,8 +22,6 @@ import org.ballerinalang.test.CompileResult; import org.testng.Assert; import org.testng.annotations.Test; -import org.wso2.ballerinalang.compiler.bir.model.BIRNode; -import org.wso2.ballerinalang.compiler.tree.BLangPackage; /** * This class contains unit tests to cover AST to BIR lowering. @@ -36,7 +34,5 @@ public class BIRModelTest { public void testBIRGen() { CompileResult result = BCompileUtil.compile("test-src/bir/bir_model.bal"); Assert.assertEquals(result.getErrorCount(), 0); - - BIRNode.BIRPackage birPackage = ((BLangPackage) result.getAST()).symbol.bir; } } From ec28a7c3200c8dd7522acb67894a7c8367251d6a Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 12 Oct 2023 14:12:16 +0530 Subject: [PATCH 6/9] Make BPackageSymbol imports a set --- .../compiler/semantics/analyzer/SymbolEnter.java | 7 ++----- .../compiler/semantics/model/symbols/BPackageSymbol.java | 6 +++--- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java index a64cc3158e1f..6717ab79d19c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SymbolEnter.java @@ -1116,10 +1116,8 @@ public void visit(BLangImportPackage importPkgNode) { return; } - List imports = ((BPackageSymbol) this.env.scope.owner).imports; - if (!imports.contains(pkgSymbol)) { - imports.add(pkgSymbol); - } + Set imports = ((BPackageSymbol) this.env.scope.owner).imports; + imports.add(pkgSymbol); // get a copy of the package symbol, add compilation unit info to it, // and define it in the current package scope @@ -1134,7 +1132,6 @@ public void visit(BLangImportPackage importPkgNode) { public void initPredeclaredModules(Map predeclaredModules, List compUnits, SymbolEnv env) { - SymbolEnv prevEnv = this.env; this.env = env; for (Map.Entry predeclaredModuleEntry : predeclaredModules.entrySet()) { Name alias = predeclaredModuleEntry.getKey(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/symbols/BPackageSymbol.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/symbols/BPackageSymbol.java index 19ac138d2fb4..aecf695a9253 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/symbols/BPackageSymbol.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/model/symbols/BPackageSymbol.java @@ -29,9 +29,9 @@ import org.wso2.ballerinalang.programfile.CompiledBinaryFile.BIRPackageFile; import org.wso2.ballerinalang.programfile.CompiledBinaryFile.PackageFile; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; import java.util.Objects; +import java.util.Set; import static org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag.PACKAGE; @@ -42,7 +42,7 @@ public class BPackageSymbol extends BTypeSymbol { public BInvokableSymbol initFunctionSymbol, startFunctionSymbol, stopFunctionSymbol, testInitFunctionSymbol, testStartFunctionSymbol, testStopFunctionSymbol; - public List imports = new ArrayList<>(); + public Set imports = new HashSet<>(); public PackageFile packageFile; public CompiledPackage compiledPackage; public Name compUnit; From 66ac0fb98b648a41deaeb06b4866bbaa22abe73c Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 16 Oct 2023 12:08:44 +0530 Subject: [PATCH 7/9] Remove text document of dependencies --- .../io/ballerina/projects/DocumentContext.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java index c4a7ef1b98f5..df7efe36c7a0 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java @@ -82,12 +82,12 @@ String name() { } SyntaxTree parse() { - if (syntaxTree != null) { - return syntaxTree; + if (this.syntaxTree != null) { + return this.syntaxTree; } if (!disableSyntaxTree) { - syntaxTree = SyntaxTree.from(this.textDocument(), name); - return syntaxTree; + this.syntaxTree = SyntaxTree.from(this.textDocument(), name); + return this.syntaxTree; } return SyntaxTree.from(this.textDocument(), name); } @@ -97,10 +97,14 @@ SyntaxTree syntaxTree() { } TextDocument textDocument() { - if (this.textDocument == null) { + if (this.textDocument != null) { + return this.textDocument; + } + if (!disableSyntaxTree) { this.textDocument = TextDocuments.from(this.content); + return this.textDocument; } - return this.textDocument; + return TextDocuments.from(this.content); } BLangCompilationUnit compilationUnit(CompilerContext compilerContext, PackageID pkgID, SourceKind sourceKind) { From faf16bb903bca69b31ad09acd3f0b764a30d6a14 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Mon, 16 Oct 2023 14:59:01 +0530 Subject: [PATCH 8/9] Refactor DocumentContext class --- .../ballerina/projects/DocumentContext.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java index df7efe36c7a0..b91b222fe5a5 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/DocumentContext.java @@ -59,7 +59,7 @@ class DocumentContext { private final DocumentId documentId; private final String name; private String content; - private boolean disableSyntaxTree = false; + private final boolean disableSyntaxTree; private DocumentContext(DocumentId documentId, String name, String content, boolean disableSyntaxTree) { this.documentId = documentId; @@ -85,11 +85,11 @@ SyntaxTree parse() { if (this.syntaxTree != null) { return this.syntaxTree; } - if (!disableSyntaxTree) { - this.syntaxTree = SyntaxTree.from(this.textDocument(), name); + if (!this.disableSyntaxTree) { + this.syntaxTree = SyntaxTree.from(this.textDocument(), this.name); return this.syntaxTree; } - return SyntaxTree.from(this.textDocument(), name); + return SyntaxTree.from(this.textDocument(), this.name); } SyntaxTree syntaxTree() { @@ -100,7 +100,7 @@ TextDocument textDocument() { if (this.textDocument != null) { return this.textDocument; } - if (!disableSyntaxTree) { + if (!this.disableSyntaxTree) { this.textDocument = TextDocuments.from(this.content); return this.textDocument; } @@ -109,17 +109,17 @@ TextDocument textDocument() { BLangCompilationUnit compilationUnit(CompilerContext compilerContext, PackageID pkgID, SourceKind sourceKind) { BLangDiagnosticLog dlog = BLangDiagnosticLog.getInstance(compilerContext); - SyntaxTree syntaxTree = syntaxTree(); - reportSyntaxDiagnostics(pkgID, syntaxTree, dlog); + SyntaxTree synTree = syntaxTree(); + reportSyntaxDiagnostics(pkgID, synTree, dlog); - nodeCloner = NodeCloner.getInstance(compilerContext); - if (compilationUnit != null) { - return nodeCloner.cloneCUnit(compilationUnit); + this.nodeCloner = NodeCloner.getInstance(compilerContext); + if (this.compilationUnit != null) { + return this.nodeCloner.cloneCUnit(this.compilationUnit); } BLangNodeBuilder bLangNodeBuilder = new BLangNodeBuilder(compilerContext, pkgID, this.name); - compilationUnit = (BLangCompilationUnit) bLangNodeBuilder.accept(syntaxTree.rootNode()).get(0); - compilationUnit.setSourceKind(sourceKind); - return nodeCloner.cloneCUnit(compilationUnit); + this.compilationUnit = (BLangCompilationUnit) bLangNodeBuilder.accept(synTree.rootNode()).get(0); + this.compilationUnit.setSourceKind(sourceKind); + return this.nodeCloner.cloneCUnit(this.compilationUnit); } Set moduleLoadRequests(ModuleDescriptor currentModuleDesc, PackageDependencyScope scope) { @@ -133,10 +133,10 @@ Set moduleLoadRequests(ModuleDescriptor currentModuleDesc, Pa private Set getModuleLoadRequests(ModuleDescriptor currentModuleDesc, PackageDependencyScope scope) { - Set moduleLoadRequests = new LinkedHashSet<>(); + Set moduleLoadRequestSet = new LinkedHashSet<>(); ModulePartNode modulePartNode = syntaxTree().rootNode(); for (ImportDeclarationNode importDcl : modulePartNode.imports()) { - moduleLoadRequests.add(getModuleLoadRequest(importDcl, scope)); + moduleLoadRequestSet.add(getModuleLoadRequest(importDcl, scope)); } // TODO This is a temporary solution for SLP6 release @@ -149,9 +149,9 @@ private Set getModuleLoadRequests(ModuleDescriptor currentMod ModuleLoadRequest ballerinaiLoadReq = new ModuleLoadRequest( PackageOrg.from(Names.BALLERINA_INTERNAL_ORG.value), moduleName, scope, DependencyResolutionType.PLATFORM_PROVIDED); - moduleLoadRequests.add(ballerinaiLoadReq); + moduleLoadRequestSet.add(ballerinaiLoadReq); } - return moduleLoadRequests; + return moduleLoadRequestSet; } private ModuleLoadRequest getModuleLoadRequest(ImportDeclarationNode importDcl, PackageDependencyScope scope) { From 47dd0cedd9b4efa4e6b5a3b2dea8a7fca8d96b46 Mon Sep 17 00:00:00 2001 From: Nadeeshan96 Date: Thu, 19 Oct 2023 12:02:28 +0530 Subject: [PATCH 9/9] Clean BLangPackage after codegen for Bala projects --- .../main/java/io/ballerina/projects/JBallerinaBackend.java | 3 +++ .../src/main/java/io/ballerina/projects/ModuleContext.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java index ec1fdadad1bf..b0e32539af3b 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java @@ -179,6 +179,9 @@ private void performCodeGen() { //TODO: remove this once ballerina-lang#41407 is fixed ModuleContext.shrinkDocuments(moduleContext); + if (moduleContext.project().kind() == ProjectKind.BALA_PROJECT) { + moduleContext.cleanBLangPackage(); + } } // add compilation diagnostics diagnostics.addAll(moduleDiagnostics); diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleContext.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleContext.java index 6578b424fdc8..0000b47bc0bd 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleContext.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/ModuleContext.java @@ -121,6 +121,10 @@ class ModuleContext { this.compilationCache = projectEnvironment.getService(CompilationCache.class); } + public void cleanBLangPackage() { + this.bLangPackage = null; + } + static ModuleContext from(Project project, ModuleConfig moduleConfig, boolean disableSyntaxTree) { Map srcDocContextMap = new LinkedHashMap<>(); for (DocumentConfig sourceDocConfig : moduleConfig.sourceDocs()) {