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

Test cleaning BLangPackage #86

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -82,40 +82,44 @@ 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;
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() {
return parse();
}

TextDocument textDocument() {
if (this.textDocument == null) {
if (this.textDocument != null) {
return this.textDocument;
}
if (!this.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) {
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<ModuleLoadRequest> moduleLoadRequests(ModuleDescriptor currentModuleDesc, PackageDependencyScope scope) {
Expand All @@ -129,10 +133,10 @@ Set<ModuleLoadRequest> moduleLoadRequests(ModuleDescriptor currentModuleDesc, Pa

private Set<ModuleLoadRequest> getModuleLoadRequests(ModuleDescriptor currentModuleDesc,
PackageDependencyScope scope) {
Set<ModuleLoadRequest> moduleLoadRequests = new LinkedHashSet<>();
Set<ModuleLoadRequest> 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
Expand All @@ -145,9 +149,9 @@ private Set<ModuleLoadRequest> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentId, DocumentContext> srcDocContextMap = new LinkedHashMap<>();
for (DocumentConfig sourceDocConfig : moduleConfig.sourceDocs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,14 +43,12 @@
public class CodeGenerator {

private static final CompilerContext.Key<CodeGenerator> 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);
Expand All @@ -58,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;
}

Expand All @@ -79,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
Expand All @@ -99,12 +96,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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BIRFunctionParameter> functionParams = new ArrayList<>();
List<BIRVariableDcl> 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<BIRBasicBlock> basicBlocks,
Expand All @@ -94,11 +80,8 @@ public static int getNextDesugarBBId(InitMethodGen initMethodGen) {
return initMethodGen.incrementAndGetNextId();
}

private static List<BType> updateParamTypesWithDefaultableBooleanVar(List<BType> funcParams, BType restType,
BType booleanType) {

private static List<BType> updateParamTypesWithDefaultableBooleanVar(List<BType> funcParams, BType restType) {
List<BType> paramTypes = new ArrayList<>();

int counter = 0;
int size = funcParams == null ? 0 : funcParams.size();
while (counter < size) {
Expand Down Expand Up @@ -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<String, String> encodeModuleIdentifiers(BIRNode.BIRPackage module) {
HashMap<String, String> encodedVsInitialIds = new HashMap<>();
encodePackageIdentifiers(module.packageID, encodedVsInitialIds);
Expand Down Expand Up @@ -276,9 +248,6 @@ private static void encodeAttachedFunctionIdentifiers(List<BAttachedFunction> fu
private static void encodeGlobalVariableIdentifiers(List<BIRNode.BIRGlobalVariableDcl> globalVars,
HashMap<String, String> encodedVsInitialIds) {
for (BIRNode.BIRGlobalVariableDcl globalVar : globalVars) {
if (globalVar == null) {
continue;
}
globalVar.name = Names.fromString(encodeNonFunctionIdentifier(globalVar.name.value, encodedVsInitialIds));
}
}
Expand All @@ -295,7 +264,6 @@ static void replaceEncodedModuleIdentifiers(BIRNode.BIRPackage module,
HashMap<String, String> encodedVsInitialIds) {
replaceEncodedPackageIdentifiers(module.packageID, encodedVsInitialIds);
replaceEncodedGlobalVariableIdentifiers(module.globalVars, encodedVsInitialIds);
replaceEncodedImportedGlobalVariableIdentifiers(module.importedGlobalVarsDummyVarDcls, encodedVsInitialIds);
replaceEncodedFunctionIdentifiers(module.functions, encodedVsInitialIds);
replaceEncodedTypeDefIdentifiers(module.typeDefs, encodedVsInitialIds);
}
Expand Down Expand Up @@ -337,12 +305,6 @@ private static void replaceEncodedFunctionIdentifiers(List<BIRFunction> function
HashMap<String, String> 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);
}
Expand Down Expand Up @@ -383,16 +345,6 @@ private static void replaceEncodedAttachedFunctionIdentifiers(List<BAttachedFunc

private static void replaceEncodedGlobalVariableIdentifiers(List<BIRNode.BIRGlobalVariableDcl> globalVars,
HashMap<String, String> encodedVsInitialIds) {
for (BIRNode.BIRGlobalVariableDcl globalVar : globalVars) {
if (globalVar == null) {
continue;
}
globalVar.name = getInitialIdString(globalVar.name, encodedVsInitialIds);
}
}

private static void replaceEncodedImportedGlobalVariableIdentifiers(Set<BIRNode.BIRGlobalVariableDcl> globalVars,
HashMap<String, String> encodedVsInitialIds) {
for (BIRNode.BIRGlobalVariableDcl globalVar : globalVars) {
globalVar.name = getInitialIdString(globalVar.name, encodedVsInitialIds);
}
Expand All @@ -416,14 +368,6 @@ private static String encodeNonFunctionIdentifier(String identifier, HashMap<Str
return encodedString;
}

private static String getInitialIdString(String encodedIdString, HashMap<String, String> encodedVsInitialIds) {
String initialString = encodedVsInitialIds.get(encodedIdString);
if (initialString != null) {
return initialString;
}
return encodedIdString;
}

private static Name getInitialIdString(Name encodedIdString, HashMap<String, String> encodedVsInitialIds) {
String initialString = encodedVsInitialIds.get(encodedIdString.value);
if (initialString != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Expand Down
Loading
Loading