From ba768a7fe7bcf8f930a55deae1d37e9b7ccca05c Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 14:42:42 +0530 Subject: [PATCH 01/87] Reorder top level nodes --- .../semantics/analyzer/DataflowAnalyzer.java | 60 ++++- .../cyclefind/GlobalVariableRefAnalyzer.java | 216 ++++-------------- 2 files changed, 97 insertions(+), 179 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index 085e9ceb680a..5ddf05f938d2 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -45,6 +45,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BErrorType; import org.wso2.ballerinalang.compiler.semantics.model.types.BField; import org.wso2.ballerinalang.compiler.semantics.model.types.BMapType; import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType; @@ -267,6 +268,7 @@ public class DataflowAnalyzer extends BLangNodeVisitor { private Map uninitializedVars; private Map unusedErrorVarsDeclaredWithVar; private Map unusedLocalVariables; + private final Map symbolOwner; private Map> globalNodeDependsOn; private Map> functionToDependency; private Map> possibleFailureUnInitVars; @@ -289,6 +291,7 @@ private DataflowAnalyzer(CompilerContext context) { this.currDependentSymbolDeque = new ArrayDeque<>(); this.globalVariableRefAnalyzer = GlobalVariableRefAnalyzer.getInstance(context); this.unusedLocalVariables = new HashMap<>(); + this.symbolOwner = new HashMap<>(); } public static DataflowAnalyzer getInstance(CompilerContext context) { @@ -348,7 +351,7 @@ public void visit(BLangPackage pkgNode) { } checkForUninitializedGlobalVars(pkgNode.globalVars); pkgNode.getTestablePkgs().forEach(testablePackage -> visit((BLangPackage) testablePackage)); - this.globalVariableRefAnalyzer.analyzeAndReOrder(pkgNode, this.globalNodeDependsOn); + this.globalVariableRefAnalyzer.analyzeAndReOrder(pkgNode, this.globalNodeDependsOn, this.symbolOwner); this.globalVariableRefAnalyzer.populateFunctionDependencies(this.functionToDependency, pkgNode.globalVars); pkgNode.globalVariableDependencies = globalVariableRefAnalyzer.getGlobalVariablesDependsOn(); checkUnusedImports(pkgNode.imports); @@ -424,6 +427,7 @@ public void visit(BLangFunction funcNode) { funcNode.annAttachments.forEach(bLangAnnotationAttachment -> analyzeNode(bLangAnnotationAttachment.expr, env)); funcNode.requiredParams.forEach(param -> analyzeNode(param, funcEnv)); analyzeNode(funcNode.restParam, funcEnv); + analyzeNode(funcNode.returnTypeNode, env); if (funcNode.flagSet.contains(Flag.OBJECT_CTOR)) { visitFunctionBodyWithDynamicEnv(funcNode, funcEnv); @@ -522,14 +526,15 @@ public void visit(BLangService service) { @Override public void visit(BLangTypeDefinition typeDefinition) { - SymbolEnv typeDefEnv; BSymbol symbol = typeDefinition.symbol; if (typeDefinition.symbol.kind == SymbolKind.TYPE_DEF) { symbol = symbol.type.tsymbol; } - typeDefEnv = SymbolEnv.createTypeEnv(typeDefinition.typeNode, symbol.scope, env); this.currDependentSymbolDeque.push(symbol); - analyzeNode(typeDefinition.typeNode, typeDefEnv); + for (BLangAnnotationAttachment bLangAnnotationAttachment : typeDefinition.annAttachments) { + analyzeNode(bLangAnnotationAttachment.expr, env); + } + analyzeNode(typeDefinition.typeNode, env); this.currDependentSymbolDeque.pop(); } @@ -552,18 +557,19 @@ public void visit(BLangClassDefinition classDef) { this.definiteFailureReached = false; visitedOCE = true; } - SymbolEnv objectEnv = SymbolEnv.createClassEnv(classDef, classDef.symbol.scope, env); this.currDependentSymbolDeque.push(classDef.symbol); for (BLangAnnotationAttachment bLangAnnotationAttachment : classDef.annAttachments) { analyzeNode(bLangAnnotationAttachment.expr, env); } - classDef.fields.forEach(field -> analyzeNode(field, objectEnv)); - classDef.referencedFields.forEach(field -> analyzeNode(field, objectEnv)); + classDef.fields.forEach(field -> analyzeNode(field, this.env)); + classDef.referencedFields.forEach(field -> analyzeNode(field, this.env)); // Visit the constructor with the same scope as the object + analyzeNode(classDef.initFunction, env); if (classDef.initFunction != null) { + SymbolEnv objectEnv = SymbolEnv.createClassEnv(classDef, classDef.symbol.scope, env); if (classDef.initFunction.body == null) { // if the init() function is defined as an outside function definition Optional outerFuncDef = @@ -666,7 +672,10 @@ public void visit(BLangSimpleVariable variable) { return; } - this.currDependentSymbolDeque.push(symbol); + boolean isRecordField = variable.flagSet.contains(Flag.FIELD); + if (!isRecordField) { + this.currDependentSymbolDeque.push(symbol); + } if (variable.typeNode != null && variable.typeNode.getBType() != null) { BType type = variable.typeNode.getBType(); recordGlobalVariableReferenceRelationship(Types.getImpliedType(type).tsymbol); @@ -709,7 +718,9 @@ public void visit(BLangSimpleVariable variable) { if (withInModuleVarLetExpr) { // double pop this.currDependentSymbolDeque.pop(); } - this.currDependentSymbolDeque.pop(); + if (!isRecordField) { + this.currDependentSymbolDeque.pop(); + } } } @@ -1582,6 +1593,10 @@ private List getFieldNames(BLangTableConstructorExpr constructorExpr) { @Override public void visit(BLangRecordLiteral recordLiteral) { + BType type = recordLiteral.getBType(); + if (type != null) { + recordGlobalVariableReferenceRelationship(Types.getImpliedType(type).tsymbol); + } for (RecordLiteralNode.RecordField field : recordLiteral.fields) { if (field.isKeyValueField()) { BLangRecordLiteral.BLangRecordKeyValueField keyValuePair = @@ -1704,6 +1719,8 @@ public void visit(BLangErrorConstructorExpr errorConstructorExpr) { for (BLangNamedArgsExpression namedArg : errorConstructorExpr.namedArgs) { analyzeNode(namedArg, env); } + BType detailType = ((BErrorType) Types.getImpliedType(errorConstructorExpr.getBType())).detailType; + recordGlobalVariableReferenceRelationship(detailType.tsymbol); } @Override @@ -2393,6 +2410,10 @@ public void visit(BLangInferredTypedescDefaultNode inferTypedescExpr) { @Override public void visit(BLangErrorType errorType) { + BLangType detailType = errorType.detailType; + if (detailType != null && detailType.getBType() != null) { + recordGlobalVariableReferenceRelationship(Types.getImpliedType(detailType.getBType()).tsymbol); + } } @Override @@ -2434,6 +2455,9 @@ public void visit(BLangTupleVariable bLangTupleVariable) { analyzeNode(bLangTupleVariable.typeNode, env); populateUnusedVariableMapForNonSimpleBindingPatternVariables(this.unusedLocalVariables, bLangTupleVariable); this.currDependentSymbolDeque.push(bLangTupleVariable.symbol); + for (BLangVariable memberVariable : bLangTupleVariable.memberVariables) { + symbolOwner.put(memberVariable.symbol, bLangTupleVariable.symbol); + } analyzeNode(bLangTupleVariable.expr, env); this.currDependentSymbolDeque.pop(); } @@ -2448,6 +2472,9 @@ public void visit(BLangRecordVariable bLangRecordVariable) { analyzeNode(bLangRecordVariable.typeNode, env); populateUnusedVariableMapForNonSimpleBindingPatternVariables(this.unusedLocalVariables, bLangRecordVariable); this.currDependentSymbolDeque.push(bLangRecordVariable.symbol); + for(BLangRecordVariable.BLangRecordVariableKeyValue recordVariableKeyValue : bLangRecordVariable.variableList) { + symbolOwner.put(recordVariableKeyValue.valueBindingPattern.symbol, bLangRecordVariable.symbol); + } analyzeNode(bLangRecordVariable.expr, env); this.currDependentSymbolDeque.pop(); } @@ -2462,6 +2489,21 @@ public void visit(BLangErrorVariable bLangErrorVariable) { analyzeNode(bLangErrorVariable.typeNode, env); populateUnusedVariableMapForNonSimpleBindingPatternVariables(this.unusedLocalVariables, bLangErrorVariable); this.currDependentSymbolDeque.push(bLangErrorVariable.symbol); + BSymbol symbol = bLangErrorVariable.symbol; + if (bLangErrorVariable.message != null) { + symbolOwner.put(bLangErrorVariable.message.symbol, symbol); + } + if (bLangErrorVariable.cause != null) { + symbolOwner.put(bLangErrorVariable.cause.symbol, symbol); + } + if (bLangErrorVariable.restDetail != null) { + symbolOwner.put(bLangErrorVariable.restDetail.symbol, symbol); + } + + for (BLangErrorVariable.BLangErrorDetailEntry errorDetailEntry : bLangErrorVariable.detail) { + symbolOwner.put(errorDetailEntry.valueBindingPattern.symbol, bLangErrorVariable.symbol); + } + analyzeNode(bLangErrorVariable.expr, env); this.currDependentSymbolDeque.pop(); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java index 185935e5bb5d..565bdcca6677 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java @@ -46,8 +46,8 @@ import java.util.Deque; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; @@ -64,6 +64,7 @@ public class GlobalVariableRefAnalyzer { private final BLangDiagnosticLog dlog; private BLangPackage pkgNode; private Map> globalNodeDependsOn; + Map symbolOwner; private Map> globalVariablesDependsOn; private final Map dependencyNodes; private final Deque nodeInfoStack; @@ -228,54 +229,21 @@ private boolean isGlobalVarSymbol(BSymbol symbol, Set globalVars) { } /** - * Analyze the global variable references and reorder them or emit error if they contain cyclic references. + * Analyze the global references and reorder them or emit error if they contain cyclic references. * * @param pkgNode package to be analyzed. * @param globalNodeDependsOn symbol dependency relationship. + * @param symbolOwner symbol owner relationship. */ - public void analyzeAndReOrder(BLangPackage pkgNode, Map> globalNodeDependsOn) { + public void analyzeAndReOrder(BLangPackage pkgNode, Map> globalNodeDependsOn, + Map symbolOwner) { this.dlog.setCurrentPackageId(pkgNode.packageID); this.pkgNode = pkgNode; this.globalNodeDependsOn = globalNodeDependsOn; + this.symbolOwner = symbolOwner; resetAnalyzer(); - List globalVarsAndDependentFuncs = getGlobalVariablesAndDependentFunctions(); - - pruneDependencyRelations(); - - Set sorted = new LinkedHashSet<>(); - LinkedList dependencies = new LinkedList<>(); - - for (BSymbol symbol : globalVarsAndDependentFuncs) { - List dependencyTrain = analyzeDependenciesStartingFrom(symbol); - dependencies.addAll(dependencyTrain); - - Set symbolsProviders = globalNodeDependsOn.get(symbol); - boolean symbolHasProviders = symbolsProviders != null && !symbolsProviders.isEmpty(); - boolean notInSortedList = !sorted.contains(symbol); - - // Independent variable declaration, add to sorted list. - if (notInSortedList && !symbolHasProviders) { - moveAndAppendToSortedList(symbol, dependencies, sorted); - } - // Dependent variable, and all the dependencies are satisfied, add to sorted list. - if (notInSortedList && symbolHasProviders && sorted.containsAll(symbolsProviders)) { - moveAndAppendToSortedList(symbol, dependencies, sorted); - } - - // If we can satisfy the dependencies' dependencies then we can add those dependencies to sorted list now. - addDependenciesDependencies(dependencies, sorted); - } - sorted.addAll(dependencies); - - // Cyclic error found no need to sort. - if (cyclicErrorFound) { - return; - } - - sortConstants(sorted); - projectSortToGlobalVarsList(sorted); - projectSortToTopLevelNodesList(); + reOrderTopLevelNodeList(); } private List analyzeDependenciesStartingFrom(BSymbol symbol) { @@ -298,148 +266,55 @@ private List analyzeDependenciesStartingFrom(BSymbol symbol) { return new ArrayList<>(); } - private void pruneDependencyRelations() { - List dependents = new ArrayList<>(this.globalNodeDependsOn.keySet()); - Set visited = new HashSet<>(); - for (BSymbol dependent : dependents) { - // Taking a copy as we need to modify the original list. - List providers = new ArrayList<>(this.globalNodeDependsOn.get(dependent)); - for (BSymbol provider : providers) { - pruneFunctions(dependent, provider, this.globalNodeDependsOn, visited); - } - } - } - - private void pruneFunctions(BSymbol dependent, BSymbol provider, Map> globalNodeDependsOn, - Set visited) { - if (visited.contains(provider)) { - return; - } else { - visited.add(provider); - } + private void reOrderTopLevelNodeList() { + Map varMap = collectAssociateSymbolsWithTopLevelNodes(); - // Dependent has a dependency on a global var. - if (provider.tag != SymTag.FUNCTION) { - return; + Set sorted = new LinkedHashSet<>(); + for (BSymbol symbol : varMap.keySet()) { + sorted.addAll(analyzeDependenciesStartingFrom(symbol)); } - // Provider is a function. - // And doesn't have dependency on a global variable. We can prune provider. - if (!globalNodeDependsOn.containsKey(provider) || globalNodeDependsOn.get(provider).isEmpty()) { - globalNodeDependsOn.get(dependent).remove(provider); + // Cyclic error found no need to sort. + if (cyclicErrorFound) { return; } - // Taking a copy as we need to modify the original list. - List providersProviders = new ArrayList<>(globalNodeDependsOn.get(provider)); - for (BSymbol prov : providersProviders) { - pruneFunctions(provider, prov, globalNodeDependsOn, visited); - } - } - - private void addDependenciesDependencies(LinkedList dependencies, Set sorted) { - // For each dependency if they satisfy their dependencies in sorted list, then add them to sorted list. - ArrayList depCopy = new ArrayList<>(dependencies); - for (BSymbol dep : depCopy) { - Set depsDependencies = globalNodeDependsOn.getOrDefault(dep, new LinkedHashSet<>()); - if (!depsDependencies.isEmpty() && sorted.containsAll(depsDependencies)) { - moveAndAppendToSortedList(dep, dependencies, sorted); - } - } - } - - private void projectSortToTopLevelNodesList() { - // Swap global variable nodes in 'topLevelNodes' list to reflect sorted global variables. - List topLevelPositions = new ArrayList<>(); - for (BLangVariable globalVar : pkgNode.globalVars) { - topLevelPositions.add(pkgNode.topLevelNodes.indexOf(globalVar)); - } - topLevelPositions.sort(Comparator.comparingInt(i -> i)); - for (int i = 0; i < topLevelPositions.size(); i++) { - Integer targetIndex = topLevelPositions.get(i); - pkgNode.topLevelNodes.set(targetIndex, pkgNode.globalVars.get(i)); - } - - topLevelPositions = new ArrayList<>(); - for (BLangConstant constant : pkgNode.constants) { - topLevelPositions.add(pkgNode.topLevelNodes.indexOf(constant)); - } - topLevelPositions.sort(Comparator.comparingInt(i -> i)); - for (int i = 0; i < topLevelPositions.size(); i++) { - Integer targetIndex = topLevelPositions.get(i); - pkgNode.topLevelNodes.set(targetIndex, pkgNode.constants.get(i)); - } - } - - private void projectSortToGlobalVarsList(Set sorted) { - Map varMap = new HashMap<>(); - this.pkgNode.globalVars.forEach(globalVar -> { - if (globalVar.symbol != null) { - varMap.put(globalVar.symbol, globalVar); + Set sortedTopLevelNodes = new LinkedHashSet<>(); + for (BSymbol symbol : sorted) { + if (varMap.containsKey(symbol)) { + sortedTopLevelNodes.add(varMap.get(symbol)); } - }); - - List sortedGlobalVars = sorted.stream() - .filter(varMap::containsKey) - .map(varMap::get) - .collect(Collectors.toList()); - - if (sortedGlobalVars.size() != this.pkgNode.globalVars.size()) { - List symbolLessGlobalVars = this.pkgNode.globalVars.stream() - .filter(g -> g.symbol == null) - .toList(); - sortedGlobalVars.addAll(symbolLessGlobalVars); } - this.pkgNode.globalVars.clear(); - this.pkgNode.globalVars.addAll(sortedGlobalVars); - } - - private void sortConstants(Set sorted) { - Map varMap = this.pkgNode.constants.stream() - .collect(Collectors.toMap(k -> k.symbol, k -> k)); - List sortedConstants = sorted.stream() - .filter(varMap::containsKey) - .map(varMap::get) - .collect(Collectors.toList()); - - if (sortedConstants.size() != this.pkgNode.constants.size()) { - List symbolLessGlobalVars = this.pkgNode.constants.stream() - .filter(c -> !sortedConstants.contains(c)) - .toList(); - sortedConstants.addAll(symbolLessGlobalVars); - } - this.pkgNode.constants.clear(); - this.pkgNode.constants.addAll(sortedConstants); + sortedTopLevelNodes.addAll(pkgNode.topLevelNodes); + this.pkgNode.topLevelNodes.clear(); + this.pkgNode.topLevelNodes.addAll(sortedTopLevelNodes); } - - private List getGlobalVariablesAndDependentFunctions() { - List dependents = new ArrayList<>(); - - for (BSymbol s : globalNodeDependsOn.keySet()) { - if ((s.tag & SymTag.FUNCTION) == SymTag.FUNCTION) { - dependents.add(s); - } - } - - for (BLangVariable var : this.pkgNode.globalVars) { - if (var.symbol != null) { - dependents.add(var.symbol); - } - } - for (BLangConstant constant : this.pkgNode.constants) { - if (constant.symbol != null) { - dependents.add(constant.symbol); + private Map collectAssociateSymbolsWithTopLevelNodes() { + Map varMap = new LinkedHashMap<>(); + for (TopLevelNode topLevelNode : this.pkgNode.topLevelNodes) { + switch (topLevelNode.getKind()) { + case VARIABLE: + case RECORD_VARIABLE: + case TUPLE_VARIABLE: + case ERROR_VARIABLE: + if (((BLangVariable) topLevelNode).symbol != null) { + varMap.put(((BLangVariable) topLevelNode).symbol, topLevelNode); + } + continue; + case TYPE_DEFINITION: + if (((BLangTypeDefinition) topLevelNode).symbol.type.tsymbol != null) { + varMap.put(((BLangTypeDefinition) topLevelNode).symbol.type.tsymbol, topLevelNode); + } + continue; + case CONSTANT: + if (((BLangConstant) topLevelNode).symbol != null) { + varMap.put(((BLangConstant) topLevelNode).symbol, topLevelNode); + } } } - - return dependents; - } - - private void moveAndAppendToSortedList(BSymbol symbol, List moveFrom, Set sorted) { - sorted.add(symbol); - moveFrom.remove(symbol); + return varMap; } private int analyzeProvidersRecursively(NodeInfo node) { @@ -454,8 +329,9 @@ private int analyzeProvidersRecursively(NodeInfo node) { Set providers = globalNodeDependsOn.getOrDefault(node.symbol, new LinkedHashSet<>()); for (BSymbol providerSym : providers) { + BSymbol symbol = symbolOwner.getOrDefault(providerSym, providerSym); NodeInfo providerNode = - dependencyNodes.computeIfAbsent(providerSym, s -> new NodeInfo(curNodeId++, providerSym)); + dependencyNodes.computeIfAbsent(providerSym, s -> new NodeInfo(curNodeId++, symbol)); int lastLowLink = analyzeProvidersRecursively(providerNode); if (providerNode.onStack) { node.lowLink = Math.min(node.lowLink, lastLowLink); From 5dc890009e36e39ad8e3776c40e9e2993fb7b625 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 14:45:55 +0530 Subject: [PATCH 02/87] Access annotations from `typedesc` --- .../main/java/io/ballerina/runtime/internal/TypeChecker.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index b4090f2592ea..5e94f63bd940 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -543,6 +543,10 @@ public static Object getAnnotValue(TypedescValue typedescValue, BString annotTag if (!(describingType instanceof BAnnotatableType)) { return null; } + MapValue annotations = ((TypedescValueImpl) typedescValue).annotations; + if (annotations != null) { + return annotations.get(annotTag); + } return ((BAnnotatableType) describingType).getAnnotation(annotTag); } From a6906f07c947604576c63e8b2982304e739ce1ee Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 14:47:56 +0530 Subject: [PATCH 03/87] Update tuple equality check --- .../wso2/ballerinalang/compiler/semantics/analyzer/Types.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java index 600e0ab8a79a..e3efe1153bc3 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/Types.java @@ -2859,6 +2859,10 @@ boolean hasSameReadonlyFlag(BType source, BType target) { @Override public Boolean visit(BTupleType t, BType s) { + if (t == s) { + return true; + } + List tTupleTypes = t.getTupleTypes(); if (((!tTupleTypes.isEmpty() && checkAllTupleMembersBelongNoType(tTupleTypes)) || (t.restType != null && t.restType.tag == TypeTags.NONE)) && From f76ac3ee97144becb7edbaef54c518fe571932e7 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 14:58:48 +0530 Subject: [PATCH 04/87] Add service variables to top level nodes --- .../ballerinalang/compiler/desugar/ServiceDesugar.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ServiceDesugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ServiceDesugar.java index 1a811140783b..64aacf7d1f28 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ServiceDesugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ServiceDesugar.java @@ -155,8 +155,10 @@ void rewriteServiceVariable(BLangService service, SymbolEnv env, BLangBlockStmt final Location pos = service.pos; - ASTBuilderUtil.defineVariable(service.serviceVariable, env.enclPkg.symbol, names); - env.enclPkg.globalVars.add(service.serviceVariable); + BLangSimpleVariable serviceVariable = service.serviceVariable; + ASTBuilderUtil.defineVariable(serviceVariable, env.enclPkg.symbol, names); + env.enclPkg.globalVars.add(serviceVariable); + env.enclPkg.topLevelNodes.add(serviceVariable); int count = 0; for (BLangExpression attachExpr : service.attachedExprs) { @@ -173,6 +175,7 @@ void rewriteServiceVariable(BLangService service, SymbolEnv env, BLangBlockStmt ASTBuilderUtil.defineVariable(listenerVar, env.enclPkg.symbol, names); listenerVar.symbol.flags |= Flags.LISTENER; env.enclPkg.globalVars.add(listenerVar); + env.enclPkg.topLevelNodes.add(listenerVar); listenerVarRef = ASTBuilderUtil.createVariableRef(pos, listenerVar.symbol); } @@ -187,6 +190,7 @@ void rewriteServiceVariable(BLangService service, SymbolEnv env, BLangBlockStmt null); ASTBuilderUtil.defineVariable(listenerWithoutErrors, env.enclPkg.symbol, names); env.enclPkg.globalVars.add(listenerWithoutErrors); + env.enclPkg.topLevelNodes.add(listenerWithoutErrors); BLangSimpleVarRef checkedRef = ASTBuilderUtil.createVariableRef(pos, listenerWithoutErrors.symbol); listenerVarRef = checkedRef; } From 54c478e28c5fecc40b64d8c3d2cba0e0c636fd5d Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 15:32:43 +0530 Subject: [PATCH 05/87] Generate `newTypedesc` instruction --- .../compiler/bir/codegen/JvmConstants.java | 1 - .../bir/codegen/JvmInstructionGen.java | 45 ++----------------- .../compiler/bir/codegen/JvmSignatures.java | 5 +-- 3 files changed, 6 insertions(+), 45 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java index a685a4eafa6d..9adc9458c839 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmConstants.java @@ -65,7 +65,6 @@ public class JvmConstants { public static final String XML_QNAME = "io/ballerina/runtime/internal/values/XmlQName"; public static final String FUTURE_VALUE = "io/ballerina/runtime/internal/values/FutureValue"; public static final String TYPEDESC_VALUE_IMPL = "io/ballerina/runtime/internal/values/TypedescValueImpl"; - public static final String TYPEDESC_VALUE_IMPL_CLOSURES = "closures"; public static final String TYPEDESC_VALUE = "io/ballerina/runtime/internal/values/TypedescValue"; public static final String HANDLE_VALUE = "io/ballerina/runtime/internal/values/HandleValue"; public static final String LOCK_VALUE = "io/ballerina/runtime/internal/BLock"; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java index 1f18e04c03ae..85006f0c62c1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java @@ -66,7 +66,6 @@ import static org.objectweb.asm.Opcodes.ALOAD; import static org.objectweb.asm.Opcodes.ANEWARRAY; import static org.objectweb.asm.Opcodes.ASTORE; -import static org.objectweb.asm.Opcodes.BIPUSH; import static org.objectweb.asm.Opcodes.CHECKCAST; import static org.objectweb.asm.Opcodes.DADD; import static org.objectweb.asm.Opcodes.DCMPL; @@ -230,7 +229,6 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_MODULE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_STRING_AT; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_STRING_FROM_ARRAY; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_TYPEDESC; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_TYPEDESC_OF_OBJECT; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.HANDLE_MAP_STORE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.HANDLE_TABLE_STORE; @@ -2033,54 +2031,19 @@ void generateNegateIns(BIRNonTerminator.UnaryOP unaryOp) { } void generateNewTypedescIns(BIRNonTerminator.NewTypeDesc newTypeDesc) { - List closureVars = newTypeDesc.closureVars; - if (isNonReferredRecord(newTypeDesc.type)) { - BType type = JvmCodeGenUtil.getImpliedType(newTypeDesc.type); - PackageID packageID = type.tsymbol.pkgID; - String typeOwner = JvmCodeGenUtil.getPackageName(packageID) + MODULE_INIT_CLASS_NAME; - String fieldName = jvmTypeGen.getTypedescFieldName(toNameString(type)); - mv.visitFieldInsn(GETSTATIC, typeOwner, fieldName, GET_TYPEDESC); - } else { - generateNewTypedescCreate(newTypeDesc.type, closureVars, newTypeDesc.annotations); - } - this.storeToVar(newTypeDesc.lhsOp.variableDcl); - } - - private boolean isNonReferredRecord(BType type) { - if (type.tag != TypeTags.TYPEREFDESC) { - return false; - } - BType referredType = ((BTypeReferenceType) type).referredType; - return referredType.tag == TypeTags.RECORD && - type.getQualifiedTypeName().equals(referredType.getQualifiedTypeName()); - } - - private void generateNewTypedescCreate(BType btype, List closureVars, BIROperand annotations) { - BType type = JvmCodeGenUtil.getImpliedType(btype); String className = TYPEDESC_VALUE_IMPL; - if (type.tag == TypeTags.RECORD) { - className = getTypeDescClassName(JvmCodeGenUtil.getPackageName(type.tsymbol.pkgID), toNameString(type)); - } this.mv.visitTypeInsn(NEW, className); this.mv.visitInsn(DUP); - jvmTypeGen.loadType(this.mv, btype); - - mv.visitIntInsn(BIPUSH, closureVars.size()); - mv.visitTypeInsn(ANEWARRAY, MAP_VALUE); - for (int i = 0; i < closureVars.size(); i++) { - BIROperand closureVar = closureVars.get(i); - mv.visitInsn(DUP); - mv.visitIntInsn(BIPUSH, i); - this.loadVar(closureVar.variableDcl); - mv.visitInsn(AASTORE); - } + jvmTypeGen.loadType(this.mv, newTypeDesc.type); + BIROperand annotations = newTypeDesc.annotations; if (annotations != null) { this.loadVar(annotations.variableDcl); this.mv.visitMethodInsn(INVOKESPECIAL, className, JVM_INIT_METHOD, TYPE_DESC_CONSTRUCTOR_WITH_ANNOTATIONS, - false); + false); } else { this.mv.visitMethodInsn(INVOKESPECIAL, className, JVM_INIT_METHOD, TYPE_DESC_CONSTRUCTOR, false); } + this.storeToVar(newTypeDesc.lhsOp.variableDcl); } void loadVar(BIRNode.BIRVariableDcl varDcl) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java index 905e422db221..5df3970b3640 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java @@ -468,9 +468,8 @@ public class JvmSignatures { public static final String TRY_TAKE_DATA = "(L" + STRAND_CLASS + ";)L" + OBJECT + ";"; public static final String TUPLE_SET_MEMBERS_METHOD = "(L" + LIST + ";L" + TYPE + ";)V"; public static final String TWO_OBJECTS_ARGS = "(L" + OBJECT + ";L" + OBJECT + ";)V"; - public static final String TYPE_DESC_CONSTRUCTOR = "(L" + TYPE + ";[L" + MAP_VALUE + ";)V"; - public static final String TYPE_DESC_CONSTRUCTOR_WITH_ANNOTATIONS = - "(L" + TYPE + ";[L" + MAP_VALUE + ";L" + MAP_VALUE + ";)V"; + public static final String TYPE_DESC_CONSTRUCTOR = "(L" + TYPE + ";)V"; + public static final String TYPE_DESC_CONSTRUCTOR_WITH_ANNOTATIONS = "(L" + TYPE + ";L" + MAP_VALUE + ";)V"; public static final String TYPE_PARAMETER = "(L" + TYPE + ";)V"; public static final String UPDATE_CHANNEL_DETAILS = "([L" + CHANNEL_DETAILS + ";)V"; public static final String VALUE_OF_JSTRING = "(L" + OBJECT + ";)L" + STRING_VALUE + ";"; From 962d8f9d94948ce7e6514246a73792aaea91021f Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 15:40:55 +0530 Subject: [PATCH 06/87] Remove closure field and update `TypedescValueImpl` constructors --- .../runtime/internal/values/TypedescValueImpl.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java index bea73113906a..b6d0e1fd744a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java @@ -53,7 +53,6 @@ public class TypedescValueImpl implements TypedescValue { final Type type; final Type describingType; // Type of the value describe by this typedesc. - public MapValue[] closures; public MapValue annotations; private BTypedesc typedesc; @@ -62,14 +61,8 @@ public TypedescValueImpl(Type describingType) { this.describingType = describingType; } - public TypedescValueImpl(Type describingType, MapValue[] closures) { - this.type = new BTypedescType(describingType); - this.describingType = describingType; - this.closures = closures; - } - - public TypedescValueImpl(Type describingType, MapValue[] closures, MapValue annotations) { - this(describingType, closures); + public TypedescValueImpl(Type describingType, MapValue annotations) { + this(describingType); this.annotations = annotations; ((BAnnotatableType) describingType).setAnnotations(annotations); } From 2df1bbcf9a9cbd5e55ac49adb64ac65c2a1902ef Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 15:47:38 +0530 Subject: [PATCH 07/87] Skip generating `typedesc` within `Type` class --- .../bir/codegen/split/types/JvmRecordTypeGen.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java index 98b89e48ddda..59c1306cc01b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java @@ -176,16 +176,6 @@ public void createRecordType(MethodVisitor mv, BRecordType recordType, String ty // initialize the record type mv.visitMethodInsn(INVOKESPECIAL, RECORD_TYPE_IMPL, JVM_INIT_METHOD, RECORD_TYPE_IMPL_INIT, false); - - mv.visitInsn(DUP); - String packageName = JvmCodeGenUtil.getPackageName(recordType.tsymbol.pkgID); - String className = getTypeDescClassName(packageName, toNameString(recordType)); - mv.visitTypeInsn(NEW, className); - mv.visitInsn(DUP_X1); - mv.visitInsn(SWAP); - mv.visitInsn(ACONST_NULL); - mv.visitMethodInsn(INVOKESPECIAL, className, JVM_INIT_METHOD, TYPE_DESC_CONSTRUCTOR, false); - mv.visitFieldInsn(PUTSTATIC, typeOwnerClass, jvmTypeGen.getTypedescFieldName(internalName), GET_TYPEDESC); } private String getFullName(BRecordType recordType) { From e0e0131398bf465a46130fb6238dbfebba3055e8 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 21:39:12 +0530 Subject: [PATCH 08/87] Desugar top-level nodes to align with the order specified in the top-level node list --- .../compiler/desugar/Desugar.java | 199 +++++++++++------- 1 file changed, 127 insertions(+), 72 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index f6cf065c909e..4af210b5fd72 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -310,7 +310,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -363,6 +362,7 @@ public class Desugar extends BLangNodeVisitor { private static final String PUSH_LANGLIB_METHOD = "push"; private static final String DESUGARED_VARARG_KEY = "$vararg$"; private static final String GENERATED_ERROR_VAR = "$error$"; + private static final String TYPEDESC = "$typedesc$"; private static final String HAS_KEY = "hasKey"; private static final String CREATE_RECORD_VALUE = "createRecordFromMap"; private static final String CHANNEL_AUTO_CLOSE_FUNC_NAME = "autoClose"; @@ -405,6 +405,7 @@ public class Desugar extends BLangNodeVisitor { private List enclosingOnFailClause = new ArrayList<>(); private Map enclosingShouldPanic = new HashMap<>(); private List enclosingShouldContinue = new ArrayList<>(); + private List typedescList = new ArrayList<>(); private BLangSimpleVarRef shouldRetryRef; private SymbolEnv env; @@ -418,6 +419,7 @@ public class Desugar extends BLangNodeVisitor { private int funcParamCount = 1; private boolean isVisitingQuery; private boolean desugarToReturn; + private int typedescCount = 0; // Worker related variables private Set channelsWithinIfStmt = new LinkedHashSet<>(); @@ -780,37 +782,30 @@ public void visit(BLangPackage pkgNode) { // Initialize the annotation map annotationDesugar.initializeAnnotationMap(pkgNode); - pkgNode.constants.stream() - .filter(constant -> (constant.expr.getKind() == NodeKind.LITERAL || - constant.expr.getKind() == NodeKind.NUMERIC_LITERAL) - && constant.expr.getBType().tag != TypeTags.TUPLE) - .forEach(constant -> pkgNode.typeDefinitions.add(constant.associatedTypeDefinition)); - - BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env); - BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body; - - rewriteConstants(pkgNode, initFnBody); - pkgNode.constants = removeDuplicateConstants(pkgNode); + for (BLangConstant constant : pkgNode.constants) { + if ((constant.expr.getKind() == NodeKind.LITERAL || + constant.expr.getKind() == NodeKind.NUMERIC_LITERAL) + && constant.expr.getBType().tag != TypeTags.TUPLE) { + pkgNode.typeDefinitions.add(constant.associatedTypeDefinition); + } + } - pkgNode.globalVars = desugarGlobalVariables(pkgNode, initFnBody); - + BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env); pkgNode.services.forEach(service -> serviceDesugar.engageCustomServiceDesugar(service, env)); + desugarTopLevelNodes(pkgNode); + annotationDesugar.rewritePackageAnnotations(pkgNode, env); + rewrite(pkgNode.xmlnsList, env); + rewrite(pkgNode.constants, env); + rewrite(pkgNode.globalVars, env); + rewrite(pkgNode.classDefinitions, env); + // Add invocation for user specified module init function (`init()`) if present and return. addUserDefinedModuleInitInvocationAndReturn(pkgNode); - //Sort type definitions with precedence - pkgNode.typeDefinitions.sort(Comparator.comparing(t -> t.precedence)); - - pkgNode.typeDefinitions = rewrite(pkgNode.typeDefinitions, env); - pkgNode.xmlnsList = rewrite(pkgNode.xmlnsList, env); - pkgNode.constants = rewrite(pkgNode.constants, env); - pkgNode.globalVars = rewrite(pkgNode.globalVars, env); - pkgNode.classDefinitions = rewrite(pkgNode.classDefinitions, env); - serviceDesugar.rewriteListeners(pkgNode.globalVars, env, pkgNode.startFunction, pkgNode.stopFunction); ASTBuilderUtil.appendStatements(serviceAttachments, (BLangBlockFunctionBody) pkgNode.initFunction.body); @@ -872,6 +867,27 @@ private void rewriteConstants(BLangPackage pkgNode, BLangBlockFunctionBody initF initFnBody.stmts.add(constInit); } } + private void desugarConstants(BLangConstant constant, BLangBlockFunctionBody initFnBody, + SymbolEnv initFunctionEnv) { + BType constType = Types.getReferredType(constant.symbol.type); + if (constType.tag != TypeTags.INTERSECTION) { + return; + } + BLangSimpleVarRef constVarRef = ASTBuilderUtil.createVariableRef(constant.pos, constant.symbol); + BLangExpression expression = rewrite(constant.expr, initFunctionEnv); + BLangAssignment constInit = ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, expression); + initFnBody.stmts.add(constInit); + constant.expr = null; + } + } + + private BLangSimpleVariableDef createSimpleVariableDef(Location pos, String name, BType type, BLangExpression expr, + BVarSymbol varSymbol) { + BLangSimpleVariable simpleVariable = ASTBuilderUtil.createVariable(pos, name, type, expr, varSymbol); + BLangSimpleVariableDef variableDef = ASTBuilderUtil.createVariableDef(pos); + variableDef.var = simpleVariable; + variableDef.setBType(type); + return variableDef; } private List removeDuplicateConstants(BLangPackage pkgNode) { @@ -963,63 +979,93 @@ private List getConfigurableLangLibInvocationParam(BLangSimpleV return new ArrayList<>(Arrays.asList(orgLiteral, moduleNameLiteral, versionLiteral, configNameLiteral, typedescExpr)); } - - private List desugarGlobalVariables(BLangPackage pkgNode, BLangBlockFunctionBody initFnBody) { + private void desugarTopLevelNodes(BLangPackage pkgNode) { List desugaredGlobalVarList = new ArrayList<>(); + typedescList = new ArrayList<>(); + BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body; SymbolEnv initFunctionEnv = SymbolEnv.createFunctionEnv(pkgNode.initFunction, pkgNode.initFunction.symbol.scope, env); - - for (BLangVariable globalVar : pkgNode.globalVars) { - this.env.enclPkg.topLevelNodes.remove(globalVar); - // This will convert complex variables to simple variables. - switch (globalVar.getKind()) { + for (int i =0; i < pkgNode.topLevelNodes.size(); i++) { + TopLevelNode topLevelNode = pkgNode.topLevelNodes.get(i); + switch (topLevelNode.getKind()) { case TUPLE_VARIABLE: - BLangNode blockStatementNode = rewrite(globalVar, initFunctionEnv); - List statements = ((BLangBlockStmt) blockStatementNode).stmts; - - int statementSize = statements.size(); - for (int i = 0; i < statementSize; i++) { - addToGlobalVariableList(statements.get(i), initFnBody, globalVar, desugaredGlobalVarList); - } - break; case RECORD_VARIABLE: case ERROR_VARIABLE: - blockStatementNode = rewrite(globalVar, initFunctionEnv); - for (BLangStatement statement : ((BLangBlockStmt) blockStatementNode).stmts) { - addToGlobalVariableList(statement, initFnBody, globalVar, desugaredGlobalVarList); - } + desugarVariable((BLangVariable) topLevelNode, initFunctionEnv, initFnBody, desugaredGlobalVarList); break; - default: - long globalVarFlags = globalVar.symbol.flags; - BLangSimpleVariable simpleGlobalVar = (BLangSimpleVariable) globalVar; - if (Symbols.isFlagOn(globalVarFlags, Flags.CONFIGURABLE)) { - if (Symbols.isFlagOn(globalVarFlags, Flags.REQUIRED)) { - // If it is required configuration get directly - List args = getConfigurableLangLibInvocationParam(simpleGlobalVar); - BLangInvocation getValueInvocation = createLangLibInvocationNode("getConfigurableValue", - args, symTable.anydataType, simpleGlobalVar.pos); - simpleGlobalVar.expr = getValueInvocation; - } else { - // If it is optional configuration create if else - simpleGlobalVar.expr = createIfElseFromConfigurable(simpleGlobalVar, initFunctionEnv); - } - } - - // Module init should fail if listener is a error value. - if (Symbols.isFlagOn(globalVarFlags, Flags.LISTENER) - && types.containsErrorType(globalVar.expr.getBType())) { - globalVar.expr = ASTBuilderUtil.createCheckExpr(globalVar.expr.pos, globalVar.expr, - globalVar.getBType()); - } - - addToInitFunction(simpleGlobalVar, initFnBody); - desugaredGlobalVarList.add(simpleGlobalVar); + case VARIABLE: + desugarGlobalVariables(initFunctionEnv, desugaredGlobalVarList, (BLangVariable) topLevelNode, + initFnBody); + break; + case CONSTANT: + desugarConstants((BLangConstant) topLevelNode, initFnBody, initFunctionEnv); break; + case TYPE_DEFINITION: + rewrite((BLangTypeDefinition) topLevelNode, env); + break; + } + if (!typedescList.isEmpty()) { + for (BLangSimpleVariableDef variableDef : typedescList) { + desugarGlobalVariables(initFunctionEnv, desugaredGlobalVarList, variableDef.var, initFnBody); + } + typedescList.clear(); } } + pkgNode.globalVars = desugaredGlobalVarList; + } + + private void desugarVariable(BLangVariable variable, SymbolEnv initFunctionEnv, + BLangBlockFunctionBody initFnBody, List desugaredGlobalVarList) { + BLangNode blockStatementNode = rewrite(variable, initFunctionEnv); + List statements = ((BLangBlockStmt) blockStatementNode).stmts; + + for (BLangStatement statement : statements) { + addToGlobalVariableList(statement, initFnBody, variable, desugaredGlobalVarList); + } + } + + private void desugarGlobalVariables(SymbolEnv initFunctionEnv, List desugaredGlobalVarList, + BLangVariable globalVar, BLangBlockFunctionBody initFnBody) { + long globalVarFlags = globalVar.symbol.flags; + BLangSimpleVariable simpleGlobalVar = (BLangSimpleVariable) globalVar; + + // Handle configurable global variables + if (Symbols.isFlagOn(globalVarFlags, Flags.CONFIGURABLE)) { + handleConfigurableGlobalVariable(simpleGlobalVar, initFunctionEnv); + } - this.env.enclPkg.topLevelNodes.addAll(desugaredGlobalVarList); - return desugaredGlobalVarList; + // Handle listener with error type + if (Symbols.isFlagOn(globalVarFlags, Flags.LISTENER) && containsErrorType(globalVar.expr.getBType())) { + handleListenerWithErrorType(globalVar); + } + + // Add variable to the initialization function + addToInitFunction(simpleGlobalVar, initFnBody); + desugaredGlobalVarList.add(simpleGlobalVar); + } + + private void handleConfigurableGlobalVariable(BLangSimpleVariable simpleGlobalVar, SymbolEnv initFunctionEnv) { + long globalVarFlags = simpleGlobalVar.symbol.flags; + + if (Symbols.isFlagOn(globalVarFlags, Flags.REQUIRED)) { + // If it is a required configuration, get directly + List args = getConfigurableLangLibInvocationParam(simpleGlobalVar); + simpleGlobalVar.expr = createLangLibInvocationNode("getConfigurableValue", args, + symTable.anydataType, simpleGlobalVar.pos); + } else { + // If it is an optional configuration, create if-else + simpleGlobalVar.expr = createIfElseFromConfigurable(simpleGlobalVar, initFunctionEnv); + } + } + + private void handleListenerWithErrorType(BLangVariable globalVar) { + globalVar.expr = ASTBuilderUtil.createCheckExpr(globalVar.expr.pos, globalVar.expr, + globalVar.getBType()); + } + + private boolean containsErrorType(BType type) { + // Check if the type contains an error type + return types.containsErrorType(type); } private void addToGlobalVariableList(BLangStatement bLangStatement, BLangBlockFunctionBody initFnBody, @@ -1059,7 +1105,9 @@ public void visit(BLangImportPackage importPkgNode) { @Override public void visit(BLangTypeDefinition typeDef) { - typeDef.typeNode = rewrite(typeDef.typeNode, env); + if (!Symbols.isFlagOn(typeDef.symbol.flags, Flags.SOURCE_ANNOTATION)) { + typeDef.typeNode = rewrite(typeDef.typeNode, env); + } typeDef.annAttachments.forEach(attachment -> rewrite(attachment, env)); result = typeDef; @@ -1196,7 +1244,7 @@ public void visit(BLangRecordTypeNode recordTypeNode) { } recordTypeNode.restFieldType = rewrite(recordTypeNode.restFieldType, env); - + createTypedescVariableDef(recordTypeNode); if (recordTypeNode.isAnonymous && recordTypeNode.isLocal) { BLangUserDefinedType userDefinedType = desugarLocalAnonRecordTypeNode(recordTypeNode); TypeDefBuilderHelper.createTypeDefinitionForTSymbol(recordTypeNode.getBType(), @@ -1205,7 +1253,6 @@ public void visit(BLangRecordTypeNode recordTypeNode) { result = userDefinedType; return; } - result = recordTypeNode; } @@ -1323,6 +1370,7 @@ public void visit(BLangBuiltInRefTypeNode refTypeNode) { public void visit(BLangTupleTypeNode tupleTypeNode) { tupleTypeNode.members.forEach(member -> member.typeNode = rewrite(member.typeNode, env)); tupleTypeNode.restParamType = rewrite(tupleTypeNode.restParamType, env); + createTypedescVariableDef(tupleTypeNode); result = tupleTypeNode; } @@ -9283,7 +9331,14 @@ E rewrite(E statement, SymbolEnv env) { private List rewriteStmt(List nodeList, SymbolEnv env) { for (int i = 0; i < nodeList.size(); i++) { + typedescList = new ArrayList<>(); nodeList.set(i, rewrite(nodeList.get(i), env)); + for (BLangSimpleVariableDef variableDef : typedescList) { + nodeList.add(i, rewrite((E) variableDef, env)); + BSymbol symbol = variableDef.var.symbol; + env.scope.define(symbol.name, symbol); + i++; + } } return nodeList; } From 935f960b5846503c2fd81c04494eb93e0dca48d5 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 21:40:59 +0530 Subject: [PATCH 09/87] Create variable definitions for typedesc in record and tuple type nodes --- .../compiler/desugar/Desugar.java | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 4af210b5fd72..9778579e0414 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -841,32 +841,6 @@ public void visit(BLangPackage pkgNode) { result = pkgNode; } - private void rewriteConstants(BLangPackage pkgNode, BLangBlockFunctionBody initFnBody) { - for (BLangConstant constant : pkgNode.constants) { - BType constType = Types.getReferredType(constant.symbol.type); - if (constType.tag != TypeTags.INTERSECTION) { - continue; - } - for (BType memberType : ((BIntersectionType) constType).getConstituentTypes()) { - BLangType typeNode; - switch (Types.getImpliedType(memberType).tag) { - case TypeTags.RECORD: - typeNode = constant.associatedTypeDefinition.typeNode; - break; - case TypeTags.TUPLE: - typeNode = (BLangTupleTypeNode) TreeBuilder.createTupleTypeNode(); - break; - default: - continue; - } - BLangSimpleVarRef constVarRef = ASTBuilderUtil.createVariableRef(constant.pos, constant.symbol); - constant.expr = rewrite(constant.expr, - SymbolEnv.createTypeEnv(typeNode, pkgNode.initFunction.symbol.scope, env)); - BLangAssignment constInit = ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, - constant.expr); - initFnBody.stmts.add(constInit); - } - } private void desugarConstants(BLangConstant constant, BLangBlockFunctionBody initFnBody, SymbolEnv initFunctionEnv) { BType constType = Types.getReferredType(constant.symbol.type); @@ -879,6 +853,19 @@ private void desugarConstants(BLangConstant constant, BLangBlockFunctionBody ini initFnBody.stmts.add(constInit); constant.expr = null; } + + private void createTypedescVariableDef(BLangType typeNode) { + Name name = new Name(TYPEDESC + typedescCount++); + Location pos = typeNode.pos; + BType type = typeNode.getBType(); + BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); + BSymbol owner = this.env.scope.owner; + BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); + BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type); + typedescExpr.typeNode = typeNode; + BLangSimpleVariableDef simpleVariableDef = createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, + varSymbol); + typedescList.add(simpleVariableDef); } private BLangSimpleVariableDef createSimpleVariableDef(Location pos, String name, BType type, BLangExpression expr, From 8d561b6a7172172857de6892c4035757600d8293 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 21:42:44 +0530 Subject: [PATCH 10/87] Add closures into the top-level node list --- .../compiler/desugar/ClosureGenerator.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java index d5fc03ba9651..4acf6d7272c5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java @@ -285,10 +285,14 @@ private void addClosuresToGlobalVariableList(SymbolEnv pkgEnv) { BLangSimpleVariable simpleVariable = queue.poll().var; simpleVariable.flagSet.add(Flag.PUBLIC); simpleVariable.symbol.flags |= Flags.PUBLIC; - pkgEnv.enclPkg.globalVars.add(0, simpleVariable); + BLangSimpleVariable variableDef = rewrite(simpleVariable, pkgEnv); + pkgEnv.enclPkg.globalVars.add(0, variableDef); + pkgEnv.enclPkg.topLevelNodes.add(0, variableDef); } for (BLangSimpleVariableDef closureReference : annotationClosureReferences) { - pkgEnv.enclPkg.globalVars.add(rewrite(closureReference.var, pkgEnv)); + BLangSimpleVariable simpleVariable = rewrite(closureReference.var, pkgEnv); + pkgEnv.enclPkg.globalVars.add(simpleVariable); + pkgEnv.enclPkg.topLevelNodes.add(simpleVariable); } } @@ -1807,7 +1811,9 @@ private List rewrite(List nodeList, SymbolEnv env) { E node = rewrite(nodeList.remove(0), env); Iterator iterator = annotationClosureReferences.iterator(); while (iterator.hasNext()) { - nodeList.add(rewrite((E) annotationClosureReferences.poll().var, env)); + E simpleVariable = rewrite((E) annotationClosureReferences.poll().var, env); + nodeList.add(simpleVariable); + env.enclPkg.topLevelNodes.add((BLangSimpleVariable) simpleVariable); } nodeList.add(node); } From 4317260e3037f42c03d830bcbde82d0ba6411334 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 22:07:40 +0530 Subject: [PATCH 11/87] Update `toString` logic --- .../java/io/ballerina/runtime/internal/types/BTupleType.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTupleType.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTupleType.java index 847a83914dba..c6fccabd8617 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTupleType.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/types/BTupleType.java @@ -209,6 +209,10 @@ private void computeStringRepresentation() { if (cachedToString != null) { return; } + if (typeName != null && !typeName.isEmpty()) { + cachedToString = typeName; + return; + } StringBuilder stringRep = new StringBuilder("[").append(tupleTypes.stream().map(Type::toString).collect(Collectors.joining(","))); if (restType != null) { From 604fb13451f906f9e7e10bf5a98e51657910bd87 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 22:32:11 +0530 Subject: [PATCH 12/87] Add type to `newStructure` instruction --- .../compiler/bir/model/BIRNonTerminator.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java index e8cf5958eb63..2f267035d713 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java @@ -208,21 +208,16 @@ public void setRhsOperands(BIROperand[] operands) { */ public static class NewStructure extends BIRNonTerminator { public BIROperand rhsOp; + public BType type; public List initialValues; - public NewStructure(Location pos, BIROperand lhsOp, BIROperand rhsOp) { - super(pos, InstructionKind.NEW_STRUCTURE); - this.lhsOp = lhsOp; - this.rhsOp = rhsOp; - this.initialValues = new ArrayList<>(); - } - public NewStructure(Location pos, BIROperand lhsOp, BIROperand rhsOp, - List initialValues) { + List initialValues, BType type) { super(pos, InstructionKind.NEW_STRUCTURE); this.lhsOp = lhsOp; this.rhsOp = rhsOp; this.initialValues = initialValues; + this.type = type; } @Override From 84d4d6599d0e98d9aa8abbaa15d5bf4b84a8899e Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Sun, 7 Jan 2024 23:42:41 +0530 Subject: [PATCH 13/87] Create a `typedesc` when traversing the type node of the tuple and record --- .../ballerinalang/compiler/bir/BIRGen.java | 128 +++++++++++++----- 1 file changed, 94 insertions(+), 34 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 9d74da3a22a2..1d1c7e59b71a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -85,6 +85,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.types.BTableType; import org.wso2.ballerinalang.compiler.semantics.model.types.BType; import org.wso2.ballerinalang.compiler.semantics.model.types.BTypeReferenceType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BTypedescType; import org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType; import org.wso2.ballerinalang.compiler.tree.BLangAnnotation; import org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment; @@ -1624,16 +1625,14 @@ public void visit(BLangMapLiteral astMapLiteralExpr) { BType type = astMapLiteralExpr.getBType(); this.env.isInArrayOrStructure++; - visitTypedesc(astMapLiteralExpr.pos, type, Collections.emptyList(), getAnnotations(type.tsymbol, this.env)); - BIRVariableDcl tempVarDcl = - new BIRVariableDcl(astMapLiteralExpr.getBType(), this.env.nextLocalVarId(names), - VarScope.FUNCTION, VarKind.TEMP); + BIRVariableDcl tempVarDcl = new BIRVariableDcl(type, this.env.nextLocalVarId(names), VarScope.FUNCTION, + VarKind.TEMP); this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); - setScopeAndEmit(new BIRNonTerminator.NewStructure(astMapLiteralExpr.pos, toVarRef, this.env.targetOperand, - generateMappingConstructorEntries(astMapLiteralExpr.fields))); + setScopeAndEmit(createNewStructureInst(generateMappingConstructorEntries(astMapLiteralExpr.fields), toVarRef, + Types.getImpliedType(type), astMapLiteralExpr.pos)); this.env.targetOperand = toVarRef; this.env.isInArrayOrStructure--; } @@ -1658,19 +1657,14 @@ public void visit(BLangTypeConversionExpr astTypeConversionExpr) { public void visit(BLangStructLiteral astStructLiteralExpr) { this.env.isInArrayOrStructure++; BType type = astStructLiteralExpr.getBType(); - visitTypedesc(astStructLiteralExpr.pos, type, Collections.emptyList(), getAnnotations(type.tsymbol, this.env)); - - BIRVariableDcl tempVarDcl = new BIRVariableDcl(astStructLiteralExpr.getBType(), - this.env.nextLocalVarId(names), VarScope.FUNCTION, VarKind.TEMP); + BIRVariableDcl tempVarDcl = new BIRVariableDcl(astStructLiteralExpr.getBType(), this.env.nextLocalVarId(names), + VarScope.FUNCTION, VarKind.TEMP); this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); - - BIRNonTerminator.NewStructure instruction = - new BIRNonTerminator.NewStructure(astStructLiteralExpr.pos, toVarRef, this.env.targetOperand, - generateMappingConstructorEntries(astStructLiteralExpr.fields)); - setScopeAndEmit(instruction); - + List fields = + generateMappingConstructorEntries(astStructLiteralExpr.fields); + setScopeAndEmit(createNewStructureInst(fields, toVarRef, type, astStructLiteralExpr.pos)); this.env.targetOperand = toVarRef; this.env.isInArrayOrStructure--; } @@ -1716,8 +1710,6 @@ public void visit(BLangArrayLiteral astArrayLiteralExpr) { @Override public void visit(BLangTupleLiteral tupleLiteral) { - BType type = tupleLiteral.getBType(); - visitTypedesc(tupleLiteral.pos, type, getAnnotations(type.tsymbol, this.env)); generateListConstructorExpr(tupleLiteral); } @@ -2022,12 +2014,13 @@ public void visit(BLangWaitForAllExpr.BLangWaitLiteral waitLiteral) { visitTypedesc(waitLiteral.pos, waitLiteral.getBType()); BIRBasicBlock thenBB = new BIRBasicBlock(this.env.nextBBId()); addToTrapStack(thenBB); - - BIRVariableDcl tempVarDcl = new BIRVariableDcl(waitLiteral.getBType(), - this.env.nextLocalVarId(names), VarScope.FUNCTION, VarKind.TEMP); + BType type = waitLiteral.getBType(); + BIRVariableDcl tempVarDcl = new BIRVariableDcl(type, this.env.nextLocalVarId(names), VarScope.FUNCTION, + VarKind.TEMP); this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); - setScopeAndEmit(new BIRNonTerminator.NewStructure(waitLiteral.pos, toVarRef, this.env.targetOperand)); + Location pos = waitLiteral.pos; + setScopeAndEmit(createNewStructureInst(new ArrayList<>(), toVarRef, type, pos)); this.env.targetOperand = toVarRef; List keys = new ArrayList<>(); @@ -2039,7 +2032,7 @@ public void visit(BLangWaitForAllExpr.BLangWaitLiteral waitLiteral) { BIROperand valueRegIndex = this.env.targetOperand; valueExprs.add(valueRegIndex); } - this.env.enclBB.terminator = new BIRTerminator.WaitAll(waitLiteral.pos, toVarRef, keys, + this.env.enclBB.terminator = new BIRTerminator.WaitAll(pos, toVarRef, keys, valueExprs, thenBB, this.currentScope); this.env.targetOperand = toVarRef; this.env.enclFunc.basicBlocks.add(thenBB); @@ -2235,14 +2228,7 @@ public void visit(BLangXMLAccessExpr xmlAccessExpr) { @Override public void visit(BLangTypedescExpr accessExpr) { - BIRVariableDcl tempVarDcl = - new BIRVariableDcl(accessExpr.getBType(), this.env.nextLocalVarId(names), VarScope.FUNCTION, - VarKind.TEMP); - this.env.enclFunc.localVars.add(tempVarDcl); - BIROperand toVarRef = new BIROperand(tempVarDcl); - setScopeAndEmit(new BIRNonTerminator.NewTypeDesc(accessExpr.pos, toVarRef, accessExpr.resolvedType, - Collections.emptyList())); - this.env.targetOperand = toVarRef; + createNewTypedescInst(accessExpr.getBType(), accessExpr.resolvedType, accessExpr.pos); } @Override @@ -2433,6 +2419,80 @@ public void visit(BLangRegExpTemplateLiteral regExpTemplateLiteral) { this.env.targetOperand = toVarRef; } + private BIRNonTerminator.NewStructure createNewStructureInst(List fields, + BIROperand toVarRef, BType type, Location pos) { + if (getTypedescVariable(type) != null) { + return new BIRNonTerminator.NewStructure(pos, toVarRef, new BIROperand(getTypedescVariable(type)), fields, type); + } else { + createNewTypedescInst(type, type, pos); + return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields, type); + } + } + + private BIRVariableDcl getTypedescVariable(BType type) { + for (Map.Entry entry : env.symbolVarMap.entrySet()) { + if (isTypeDescSymbol(entry.getKey(), type)) { + return env.symbolVarMap.get(entry.getKey()); + } + } + + for (Map.Entry entry : this.globalVarMap.entrySet()) { + if (isTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { + return this.globalVarMap.get(entry.getKey()); + } + } + + return null; + } + + private boolean isTypeDescSymbol(BSymbol symbol, BType targetType) { + return symbol.type.tag == TypeTags.TYPEDESC && ((BTypedescType) symbol.type).constraint == targetType; + } + + private void createNewTypedescInst(BType type, BType resolveType, Location position) { + BTypeSymbol typeSymbol = resolveType.tsymbol; + BIRVariableDcl tempVarDcl = createTempVariable(type); + BIROperand toVarRef = new BIROperand(tempVarDcl); + + BIRNonTerminator.NewTypeDesc newTypeDesc = createNewTypeDesc(position, toVarRef, resolveType, typeSymbol); + + this.env.targetOperand = toVarRef; + setScopeAndEmit(newTypeDesc); + } + + private BIRVariableDcl createTempVariable(BType type) { + BIRVariableDcl tempVarDcl = new BIRVariableDcl(type, this.env.nextLocalVarId(names), VarScope.FUNCTION, VarKind.TEMP); + this.env.enclFunc.localVars.add(tempVarDcl); + return tempVarDcl; + } + + private BIRNonTerminator.NewTypeDesc createNewTypeDesc(Location position, BIROperand toVarRef, BType resolveType, + BTypeSymbol typeSymbol) { + List closures = Collections.emptyList(); + if (typeSymbol != null && typeSymbol.annotations != null) { + BIROperand symbolVarOperand = this.env.symbolVarMap.containsKey(typeSymbol.annotations) ? + new BIROperand(this.env.symbolVarMap.get(typeSymbol.annotations)) : + new BIROperand(this.globalVarMap.get(typeSymbol.annotations)); + return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures, symbolVarOperand); + } else { + return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures); + } + } + + private BIRNonTerminator.NewArray createNewArrayInst(List initialValues, + BType listConstructorExprType, BIROperand sizeOp, + BIROperand toVarRef, BType referredType, Location pos) { + if (getTypedescVariable(listConstructorExprType) != null) { + return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef, + new BIROperand(getTypedescVariable(listConstructorExprType)), sizeOp, initialValues); + + } else { + createNewTypedescInst(referredType, referredType, pos); + return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef, this.env.targetOperand, sizeOp, + initialValues); + } + } + @Override public void visit(BLangReDisjunction reDisjunction) { BIROperand toVarRef = createVarRefOperand(symTable.anydataType); @@ -2741,9 +2801,9 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo } if (referredType.tag == TypeTags.TUPLE) { - setScopeAndEmit( - new BIRNonTerminator.NewArray(listConstructorExpr.pos, listConstructorExprType, toVarRef, - typedescOp, sizeOp, initialValues)); + setScopeAndEmit(createNewArrayInst(initialValues, listConstructorExprType, sizeOp, toVarRef, referredType, + listConstructorExpr.pos)); + } else { setScopeAndEmit( new BIRNonTerminator.NewArray(listConstructorExpr.pos, listConstructorExprType, toVarRef, sizeOp, From 2947f7665cc41d242ec668f2994789a83aa29b82 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Mon, 8 Jan 2024 09:16:18 +0530 Subject: [PATCH 14/87] Refactor logic --- .../ballerinalang/compiler/bir/BIRGen.java | 7 ++-- .../codegen/split/types/JvmRecordTypeGen.java | 6 ---- .../compiler/bir/model/BIRNonTerminator.java | 4 +-- .../compiler/desugar/Desugar.java | 2 +- .../semantics/analyzer/DataflowAnalyzer.java | 3 +- .../cyclefind/GlobalVariableRefAnalyzer.java | 32 ++++++++----------- 6 files changed, 22 insertions(+), 32 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 1d1c7e59b71a..1b7125738d74 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2422,10 +2422,10 @@ public void visit(BLangRegExpTemplateLiteral regExpTemplateLiteral) { private BIRNonTerminator.NewStructure createNewStructureInst(List fields, BIROperand toVarRef, BType type, Location pos) { if (getTypedescVariable(type) != null) { - return new BIRNonTerminator.NewStructure(pos, toVarRef, new BIROperand(getTypedescVariable(type)), fields, type); + return new BIRNonTerminator.NewStructure(pos, toVarRef, new BIROperand(getTypedescVariable(type)), fields); } else { createNewTypedescInst(type, type, pos); - return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields, type); + return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields); } } @@ -2461,7 +2461,8 @@ private void createNewTypedescInst(BType type, BType resolveType, Location posit } private BIRVariableDcl createTempVariable(BType type) { - BIRVariableDcl tempVarDcl = new BIRVariableDcl(type, this.env.nextLocalVarId(names), VarScope.FUNCTION, VarKind.TEMP); + BIRVariableDcl tempVarDcl = new BIRVariableDcl(type, this.env.nextLocalVarId(names), VarScope.FUNCTION, + VarKind.TEMP); this.env.enclFunc.localVars.add(tempVarDcl); return tempVarDcl; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java index 59c1306cc01b..8106e1b92e39 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java @@ -39,18 +39,14 @@ import static org.objectweb.asm.ClassWriter.COMPUTE_FRAMES; import static org.objectweb.asm.Opcodes.ACC_PUBLIC; import static org.objectweb.asm.Opcodes.ACC_SUPER; -import static org.objectweb.asm.Opcodes.ACONST_NULL; import static org.objectweb.asm.Opcodes.CHECKCAST; import static org.objectweb.asm.Opcodes.DUP; -import static org.objectweb.asm.Opcodes.DUP_X1; import static org.objectweb.asm.Opcodes.GETSTATIC; import static org.objectweb.asm.Opcodes.INVOKESPECIAL; import static org.objectweb.asm.Opcodes.INVOKESTATIC; import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL; import static org.objectweb.asm.Opcodes.NEW; import static org.objectweb.asm.Opcodes.PUTFIELD; -import static org.objectweb.asm.Opcodes.PUTSTATIC; -import static org.objectweb.asm.Opcodes.SWAP; import static org.objectweb.asm.Opcodes.V17; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.getModuleLevelClassName; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.toNameString; @@ -62,11 +58,9 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.RECORD_TYPE_IMPL; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_MODULE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_TYPE; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_TYPEDESC; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.RECORD_TYPE_IMPL_INIT; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.SET_LINKED_HASH_MAP; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.SET_MAP; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.TYPE_DESC_CONSTRUCTOR; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.VOID_METHOD_DESC; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmValueGen.getTypeDescClassName; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java index 2f267035d713..753ae63bcd84 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java @@ -208,16 +208,14 @@ public void setRhsOperands(BIROperand[] operands) { */ public static class NewStructure extends BIRNonTerminator { public BIROperand rhsOp; - public BType type; public List initialValues; public NewStructure(Location pos, BIROperand lhsOp, BIROperand rhsOp, - List initialValues, BType type) { + List initialValues) { super(pos, InstructionKind.NEW_STRUCTURE); this.lhsOp = lhsOp; this.rhsOp = rhsOp; this.initialValues = initialValues; - this.type = type; } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 9778579e0414..a660615887b2 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -972,7 +972,7 @@ private void desugarTopLevelNodes(BLangPackage pkgNode) { BLangBlockFunctionBody initFnBody = (BLangBlockFunctionBody) pkgNode.initFunction.body; SymbolEnv initFunctionEnv = SymbolEnv.createFunctionEnv(pkgNode.initFunction, pkgNode.initFunction.symbol.scope, env); - for (int i =0; i < pkgNode.topLevelNodes.size(); i++) { + for (int i = 0; i < pkgNode.topLevelNodes.size(); i++) { TopLevelNode topLevelNode = pkgNode.topLevelNodes.get(i); switch (topLevelNode.getKind()) { case TUPLE_VARIABLE: diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index 5ddf05f938d2..e5b4503ae1d9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -2472,7 +2472,8 @@ public void visit(BLangRecordVariable bLangRecordVariable) { analyzeNode(bLangRecordVariable.typeNode, env); populateUnusedVariableMapForNonSimpleBindingPatternVariables(this.unusedLocalVariables, bLangRecordVariable); this.currDependentSymbolDeque.push(bLangRecordVariable.symbol); - for(BLangRecordVariable.BLangRecordVariableKeyValue recordVariableKeyValue : bLangRecordVariable.variableList) { + for (BLangRecordVariable.BLangRecordVariableKeyValue recordVariableKeyValue : + bLangRecordVariable.variableList) { symbolOwner.put(recordVariableKeyValue.valueBindingPattern.symbol, bLangRecordVariable.symbol); } analyzeNode(bLangRecordVariable.expr, env); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java index 565bdcca6677..8a53c14d82e6 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/cyclefind/GlobalVariableRefAnalyzer.java @@ -293,30 +293,26 @@ private void reOrderTopLevelNodeList() { private Map collectAssociateSymbolsWithTopLevelNodes() { Map varMap = new LinkedHashMap<>(); + for (TopLevelNode topLevelNode : this.pkgNode.topLevelNodes) { - switch (topLevelNode.getKind()) { - case VARIABLE: - case RECORD_VARIABLE: - case TUPLE_VARIABLE: - case ERROR_VARIABLE: - if (((BLangVariable) topLevelNode).symbol != null) { - varMap.put(((BLangVariable) topLevelNode).symbol, topLevelNode); - } - continue; - case TYPE_DEFINITION: - if (((BLangTypeDefinition) topLevelNode).symbol.type.tsymbol != null) { - varMap.put(((BLangTypeDefinition) topLevelNode).symbol.type.tsymbol, topLevelNode); - } - continue; - case CONSTANT: - if (((BLangConstant) topLevelNode).symbol != null) { - varMap.put(((BLangConstant) topLevelNode).symbol, topLevelNode); - } + BSymbol symbol = getSymbolFromTopLevelNode(topLevelNode); + if (symbol != null) { + varMap.put(symbol, topLevelNode); } } + return varMap; } + private BSymbol getSymbolFromTopLevelNode(TopLevelNode topLevelNode) { + return switch (topLevelNode.getKind()) { + case VARIABLE, RECORD_VARIABLE, TUPLE_VARIABLE, ERROR_VARIABLE -> ((BLangVariable) topLevelNode).symbol; + case TYPE_DEFINITION -> ((BLangTypeDefinition) topLevelNode).symbol.type.tsymbol; + case CONSTANT -> ((BLangConstant) topLevelNode).symbol; + default -> null; + }; + } + private int analyzeProvidersRecursively(NodeInfo node) { if (node.visited) { return node.lowLink; From b14ec39834b9939a0a31cb7ab807025f11884c29 Mon Sep 17 00:00:00 2001 From: chiranSachintha Date: Mon, 8 Jan 2024 09:24:45 +0530 Subject: [PATCH 15/87] Get implied type of `typedesc` --- .../nativeimpl/jvm/tests/VariableReturnType.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java index e1a2a8686c51..ddd8f825b11e 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java @@ -152,7 +152,7 @@ public static MapValue query(BString query, BTypedesc typedesc) { } public static BStream getStreamOfRecords(ObjectValue objectValue, BStream strm, BTypedesc typedesc) { - RecordType streamConstraint = (RecordType) typedesc.getDescribingType(); + RecordType streamConstraint = (RecordType) TypeUtils.getImpliedType(typedesc.getDescribingType()); Assert.assertSame(streamConstraint, TypeUtils.getImpliedType(strm.getConstraintType())); return strm; } @@ -160,7 +160,7 @@ public static BStream getStreamOfRecords(ObjectValue objectValue, BStream strm, public static ArrayValue getTuple(BTypedesc td1, BTypedesc td2, BTypedesc td3) { List memTypes = new ArrayList<>(); memTypes.add(td1.getDescribingType()); - memTypes.add(td2.getDescribingType()); + memTypes.add(TypeUtils.getImpliedType(td2.getDescribingType())); memTypes.add(td3.getDescribingType()); BTupleType tupleType = new BTupleType(memTypes); @@ -192,7 +192,7 @@ public static ArrayValue getTupleWithRestDesc(BTypedesc td1, BTypedesc td2, BTyp } public static MapValue getRecord(BTypedesc td) { - BRecordType recType = (BRecordType) td.getDescribingType(); + BRecordType recType = (BRecordType) TypeUtils.getImpliedType(td.getDescribingType()); MapValueImpl person = new MapValueImpl(recType); if (recType.getName().equals("Person")) { From d4703457e3244379e8f86cb634086c8f29b34392 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 20 Aug 2024 10:46:27 +0530 Subject: [PATCH 16/87] Revert "Remove closure field and update `TypedescValueImpl` constructors" This reverts commit 962d8f9d94948ce7e6514246a73792aaea91021f. --- .../runtime/internal/values/TypedescValueImpl.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java index b6d0e1fd744a..bea73113906a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java @@ -53,6 +53,7 @@ public class TypedescValueImpl implements TypedescValue { final Type type; final Type describingType; // Type of the value describe by this typedesc. + public MapValue[] closures; public MapValue annotations; private BTypedesc typedesc; @@ -61,8 +62,14 @@ public TypedescValueImpl(Type describingType) { this.describingType = describingType; } - public TypedescValueImpl(Type describingType, MapValue annotations) { - this(describingType); + public TypedescValueImpl(Type describingType, MapValue[] closures) { + this.type = new BTypedescType(describingType); + this.describingType = describingType; + this.closures = closures; + } + + public TypedescValueImpl(Type describingType, MapValue[] closures, MapValue annotations) { + this(describingType, closures); this.annotations = annotations; ((BAnnotatableType) describingType).setAnnotations(annotations); } From 77d0b249a42f3cf98c641ecae9481a4ca6f1cec6 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 21 Aug 2024 09:31:49 +0530 Subject: [PATCH 17/87] Revert "Revert "Remove closure field and update `TypedescValueImpl` constructors"" This reverts commit d4703457e3244379e8f86cb634086c8f29b34392. --- .../runtime/internal/values/TypedescValueImpl.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java index bea73113906a..b6d0e1fd744a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/TypedescValueImpl.java @@ -53,7 +53,6 @@ public class TypedescValueImpl implements TypedescValue { final Type type; final Type describingType; // Type of the value describe by this typedesc. - public MapValue[] closures; public MapValue annotations; private BTypedesc typedesc; @@ -62,14 +61,8 @@ public TypedescValueImpl(Type describingType) { this.describingType = describingType; } - public TypedescValueImpl(Type describingType, MapValue[] closures) { - this.type = new BTypedescType(describingType); - this.describingType = describingType; - this.closures = closures; - } - - public TypedescValueImpl(Type describingType, MapValue[] closures, MapValue annotations) { - this(describingType, closures); + public TypedescValueImpl(Type describingType, MapValue annotations) { + this(describingType); this.annotations = annotations; ((BAnnotatableType) describingType).setAnnotations(annotations); } From 71d37a2b2c54e46a8b5b61b0e1393c790e328f6a Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 21 Aug 2024 19:18:58 +0530 Subject: [PATCH 18/87] Remove unused imports --- .../compiler/bir/codegen/split/types/JvmRecordTypeGen.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java index 8106e1b92e39..ba946d171b80 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/types/JvmRecordTypeGen.java @@ -23,7 +23,6 @@ import org.objectweb.asm.MethodVisitor; import org.wso2.ballerinalang.compiler.bir.codegen.BallerinaClassWriter; import org.wso2.ballerinalang.compiler.bir.codegen.JarEntries; -import org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil; import org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen; import org.wso2.ballerinalang.compiler.bir.codegen.JvmTypeGen; import org.wso2.ballerinalang.compiler.bir.codegen.split.JvmConstantsGen; @@ -49,7 +48,6 @@ import static org.objectweb.asm.Opcodes.PUTFIELD; import static org.objectweb.asm.Opcodes.V17; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.getModuleLevelClassName; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.toNameString; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.CLASS_FILE_SUFFIX; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.JVM_INIT_METHOD; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.LINKED_HASH_MAP; @@ -62,7 +60,6 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.SET_LINKED_HASH_MAP; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.SET_MAP; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.VOID_METHOD_DESC; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmValueGen.getTypeDescClassName; /** * BIR record type to JVM byte code generation class. From 1207d10225cc432854c4f0a977f808fac9090447 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 21 Aug 2024 19:20:24 +0530 Subject: [PATCH 19/87] Use generated typedesc class for record values --- .../compiler/bir/codegen/JvmInstructionGen.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java index 85006f0c62c1..89523efc7525 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java @@ -52,7 +52,6 @@ import org.wso2.ballerinalang.compiler.semantics.model.types.BObjectType; import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; import org.wso2.ballerinalang.compiler.semantics.model.types.BType; -import org.wso2.ballerinalang.compiler.semantics.model.types.BTypeReferenceType; import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; @@ -2032,6 +2031,10 @@ void generateNegateIns(BIRNonTerminator.UnaryOP unaryOp) { void generateNewTypedescIns(BIRNonTerminator.NewTypeDesc newTypeDesc) { String className = TYPEDESC_VALUE_IMPL; + BType type = JvmCodeGenUtil.getImpliedType(newTypeDesc.type); + if (type.tag == TypeTags.RECORD) { + className = getTypeDescClassName(JvmCodeGenUtil.getPackageName(type.tsymbol.pkgID), toNameString(type)); + } this.mv.visitTypeInsn(NEW, className); this.mv.visitInsn(DUP); jvmTypeGen.loadType(this.mv, newTypeDesc.type); From bb14e267c25c68043031079e2ed1760ee374c5d9 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 21 Aug 2024 19:21:22 +0530 Subject: [PATCH 20/87] Add missing `TypeUtils.getImpliedType` --- .../ballerinalang/nativeimpl/jvm/runtime/api/tests/Values.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/Values.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/Values.java index 594f83f0a4fe..a77024ba7f79 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/Values.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/Values.java @@ -338,7 +338,7 @@ public static Object validate(Object value, BTypedesc typedesc) { } public static Object validateRecord(Object value, BTypedesc typedesc) { - Type describingType = typedesc.getDescribingType(); + Type describingType = TypeUtils.getImpliedType(typedesc.getDescribingType()); Long age = ((BMap) value).getIntValue(StringUtils.fromString("age")); for (Field field : ((BRecordType) describingType).getFields().values()) { BMap annotations = ((AnnotatableType) field.getFieldType()).getAnnotations(); From e7ff5d2fd79f768efb1fdadb7a1a0766dd053226 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 21 Aug 2024 19:23:07 +0530 Subject: [PATCH 21/87] Remove passing closures to TypeDescValuImpl constructor --- .../compiler/bir/codegen/JvmValueGen.java | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) 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 4fbaa635a1e2..40b43cc8f403 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 @@ -64,10 +64,8 @@ import static org.objectweb.asm.Opcodes.IRETURN; import static org.objectweb.asm.Opcodes.ISTORE; import static org.objectweb.asm.Opcodes.NEW; -import static org.objectweb.asm.Opcodes.POP; import static org.objectweb.asm.Opcodes.PUTFIELD; import static org.objectweb.asm.Opcodes.RETURN; -import static org.objectweb.asm.Opcodes.SWAP; import static org.objectweb.asm.Opcodes.V17; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.toNameString; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.ABSTRACT_OBJECT_VALUE; @@ -88,14 +86,12 @@ import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.TYPEDESC_CLASS_PREFIX; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.TYPEDESC_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.TYPEDESC_VALUE_IMPL; -import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.TYPEDESC_VALUE_IMPL_CLOSURES; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.TYPE_IMPL; 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.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; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.GET_MAP_VALUE; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.INIT_TYPEDESC; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmSignatures.INSTANTIATE; @@ -260,14 +256,6 @@ private void createInstantiateMethodWithInitialValues(ClassWriter cw, BRecordTyp mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, className, JVM_INIT_METHOD, INIT_TYPEDESC, false); - // Invoke the init-function of this type. - mv.visitVarInsn(ALOAD, 1); - mv.visitInsn(SWAP); - mv.visitVarInsn(ALOAD, 0); - mv.visitFieldInsn(GETFIELD, TYPEDESC_VALUE_IMPL, TYPEDESC_VALUE_IMPL_CLOSURES, - GET_MAP_ARRAY); - mv.visitInsn(POP); - // Invoke the init-function of this type. String valueClassName; List attachedFuncs = typeDef.attachedFuncs; @@ -339,7 +327,6 @@ private void createTypeDescConstructor(ClassWriter cw, String className) { // load type mv.visitVarInsn(ALOAD, 1); - mv.visitVarInsn(ALOAD, 2); // invoke `super(type)`; mv.visitMethodInsn(INVOKESPECIAL, TYPEDESC_VALUE_IMPL, JVM_INIT_METHOD, descriptor, false); @@ -354,16 +341,14 @@ private void createTypeDescConstructorWithAnnotations(ClassWriter cw, String nam mv.visitCode(); mv.visitVarInsn(ALOAD, 0); - mv.visitVarInsn(ALOAD, 3); + mv.visitVarInsn(ALOAD, 2); mv.visitFieldInsn(PUTFIELD, name, ANNOTATIONS_FIELD, GET_MAP_VALUE); // load super mv.visitVarInsn(ALOAD, 0); // load type mv.visitVarInsn(ALOAD, 1); - // load closures - mv.visitVarInsn(ALOAD, 2); // load annotations - mv.visitVarInsn(ALOAD, 3); + mv.visitVarInsn(ALOAD, 2); // invoke `super(type)`; mv.visitMethodInsn(INVOKESPECIAL, TYPEDESC_VALUE_IMPL, JVM_INIT_METHOD, TYPE_DESC_CONSTRUCTOR_WITH_ANNOTATIONS, false); From b2a7e21e5b1158d5a0b44978f713455fa1f95dd7 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 22 Aug 2024 10:03:10 +0530 Subject: [PATCH 22/87] Refactor `BIRGen` changes --- .../wso2/ballerinalang/compiler/bir/BIRGen.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 1b7125738d74..0704a920da6a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2421,8 +2421,9 @@ public void visit(BLangRegExpTemplateLiteral regExpTemplateLiteral) { private BIRNonTerminator.NewStructure createNewStructureInst(List fields, BIROperand toVarRef, BType type, Location pos) { - if (getTypedescVariable(type) != null) { - return new BIRNonTerminator.NewStructure(pos, toVarRef, new BIROperand(getTypedescVariable(type)), fields); + BIRVariableDcl typedescVar = getTypedescVariable(type); + if (typedescVar != null) { + return new BIRNonTerminator.NewStructure(pos, toVarRef, new BIROperand(typedescVar), fields); } else { createNewTypedescInst(type, type, pos); return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields); @@ -2471,9 +2472,10 @@ private BIRNonTerminator.NewTypeDesc createNewTypeDesc(Location position, BIROpe BTypeSymbol typeSymbol) { List closures = Collections.emptyList(); if (typeSymbol != null && typeSymbol.annotations != null) { - BIROperand symbolVarOperand = this.env.symbolVarMap.containsKey(typeSymbol.annotations) ? - new BIROperand(this.env.symbolVarMap.get(typeSymbol.annotations)) : - new BIROperand(this.globalVarMap.get(typeSymbol.annotations)); + BVarSymbol typeAnnotations = typeSymbol.annotations; + BIRVariableDcl annotations = this.env.symbolVarMap.get(typeAnnotations); + BIROperand symbolVarOperand = annotations != null ? + new BIROperand(annotations) : new BIROperand(this.globalVarMap.get(typeAnnotations)); return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures, symbolVarOperand); } else { return new BIRNonTerminator.NewTypeDesc(position, toVarRef, resolveType, closures); @@ -2483,9 +2485,10 @@ private BIRNonTerminator.NewTypeDesc createNewTypeDesc(Location position, BIROpe private BIRNonTerminator.NewArray createNewArrayInst(List initialValues, BType listConstructorExprType, BIROperand sizeOp, BIROperand toVarRef, BType referredType, Location pos) { - if (getTypedescVariable(listConstructorExprType) != null) { + BIRVariableDcl typedescVar = getTypedescVariable(listConstructorExprType); + if (typedescVar != null) { return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef, - new BIROperand(getTypedescVariable(listConstructorExprType)), sizeOp, initialValues); + new BIROperand(typedescVar), sizeOp, initialValues); } else { createNewTypedescInst(referredType, referredType, pos); From deeac5275e3834e19418117385cfc882452b64e6 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 23 Aug 2024 15:52:11 +0530 Subject: [PATCH 23/87] Add closures before the dependent node in the top-level node list Add closures before the dependent node in the top-level node list in `ClosureGenerator.java` --- .../compiler/desugar/ClosureGenerator.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java index 4acf6d7272c5..bbfb4756fac9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java @@ -24,6 +24,7 @@ import org.ballerinalang.model.elements.PackageID; import org.ballerinalang.model.symbols.SymbolKind; import org.ballerinalang.model.tree.NodeKind; +import org.ballerinalang.model.tree.TopLevelNode; import org.ballerinalang.model.tree.expressions.RecordLiteralNode; import org.wso2.ballerinalang.compiler.semantics.analyzer.SymbolResolver; import org.wso2.ballerinalang.compiler.semantics.analyzer.Types; @@ -201,6 +202,7 @@ import org.wso2.ballerinalang.util.Flags; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -266,7 +268,7 @@ public void visit(BLangPackage pkgNode) { pkgNode.constants.forEach(constant -> rewrite(constant, pkgEnv)); pkgNode.annotations.forEach(annotation -> rewrite(annotation, pkgEnv)); pkgNode.initFunction = rewrite(pkgNode.initFunction, pkgEnv); - pkgNode.classDefinitions = rewrite(pkgNode.classDefinitions, pkgEnv); + rewrite(pkgNode.classDefinitions, pkgEnv); rewrite(pkgNode.globalVars, pkgEnv); addClosuresToGlobalVariableList(pkgEnv); for (int i = 0; i < pkgNode.functions.size(); i++) { @@ -1810,11 +1812,15 @@ private List rewrite(List nodeList, SymbolEnv env) { for (int i = 0; i < size; i++) { E node = rewrite(nodeList.remove(0), env); Iterator iterator = annotationClosureReferences.iterator(); + List closureList = new ArrayList<>(); while (iterator.hasNext()) { E simpleVariable = rewrite((E) annotationClosureReferences.poll().var, env); - nodeList.add(simpleVariable); - env.enclPkg.topLevelNodes.add((BLangSimpleVariable) simpleVariable); + closureList.add(simpleVariable); } + // Add closures before the dependent node in the top-level node list + int indexAtTopLevel = env.enclPkg.topLevelNodes.indexOf(node); + env.enclPkg.topLevelNodes.addAll(indexAtTopLevel, (Collection) closureList); + nodeList.addAll(closureList); nodeList.add(node); } this.annotationClosureReferences = previousQueue; From f6f591c3bad82286c3cbe79b209a788bb0628344 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 23 Aug 2024 15:55:47 +0530 Subject: [PATCH 24/87] Use user defined type name on typedesc variable --- .../compiler/desugar/Desugar.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index a660615887b2..bc4116cead3f 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -307,6 +307,7 @@ import org.wso2.ballerinalang.util.Flags; import org.wso2.ballerinalang.util.Lists; +import javax.xml.XMLConstants; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -325,8 +326,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -import javax.xml.XMLConstants; - import static io.ballerina.runtime.api.constants.RuntimeConstants.UNDERSCORE; import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL; import static org.ballerinalang.util.BLangCompilerConstants.RETRY_MANAGER_OBJECT_SHOULD_RETRY_FUNC; @@ -838,6 +837,8 @@ public void visit(BLangPackage pkgNode) { rewrite(testablePkg, this.symTable.pkgEnvMap.get(testablePkg.symbol)); } pkgNode.completedPhases.add(CompilerPhase.DESUGAR); + clearGlobalVariables(); + result = pkgNode; } @@ -855,9 +856,9 @@ private void desugarConstants(BLangConstant constant, BLangBlockFunctionBody ini } private void createTypedescVariableDef(BLangType typeNode) { - Name name = new Name(TYPEDESC + typedescCount++); - Location pos = typeNode.pos; BType type = typeNode.getBType(); + Name name = generateTypedescVariableName(type); + Location pos = typeNode.pos; BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); BSymbol owner = this.env.scope.owner; BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); @@ -868,6 +869,11 @@ private void createTypedescVariableDef(BLangType typeNode) { typedescList.add(simpleVariableDef); } + private Name generateTypedescVariableName(BType targetType) { + return targetType.tsymbol.name.value.isEmpty()? new Name(TYPEDESC + typedescCount++) : + new Name(TYPEDESC + targetType.tsymbol.name.value); + } + private BLangSimpleVariableDef createSimpleVariableDef(Location pos, String name, BType type, BLangExpression expr, BVarSymbol varSymbol) { BLangSimpleVariable simpleVariable = ASTBuilderUtil.createVariable(pos, name, type, expr, varSymbol); @@ -9317,6 +9323,7 @@ E rewrite(E statement, SymbolEnv env) { } private List rewriteStmt(List nodeList, SymbolEnv env) { + List prevTypedescList = this.typedescList; for (int i = 0; i < nodeList.size(); i++) { typedescList = new ArrayList<>(); nodeList.set(i, rewrite(nodeList.get(i), env)); @@ -9327,6 +9334,7 @@ private List rewriteStmt(List nodeList, SymbolE i++; } } + this.typedescList = prevTypedescList; return nodeList; } @@ -10655,4 +10663,8 @@ protected void addTransactionInternalModuleImport() { env.enclPkg.symbol.imports.add(importDcl.symbol); } } + + private void clearGlobalVariables() { + this.typedescList = null; + } } From e0bb977a4b8a6e00a19904d479fbbad15c3a4018 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 23 Aug 2024 16:43:31 +0530 Subject: [PATCH 25/87] Exclude field generation for typedesc Exclude field generation for typedesc when generating fields for user defined types since those fields will be generated when visiting global variables --- .../ballerinalang/compiler/bir/codegen/JvmTypeGen.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTypeGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTypeGen.java index e6d31db01347..ad5437d285fa 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTypeGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmTypeGen.java @@ -233,7 +233,11 @@ void generateUserDefinedTypeFields(ClassWriter cw, List typeD } String name = typeDef.internalName.value; generateTypeField(cw, name); - generateTypedescField(cw, name); + // Exclude field generation for typedesc when generating fields for user defined types since + // those fields will be generated when visiting global variables + if (bTypeTag != TypeTags.RECORD && bTypeTag != TypeTags.TUPLE) { + generateTypedescField(cw, name); + } } } @@ -826,7 +830,7 @@ public static String getTypeFieldName(String typeName) { } public String getTypedescFieldName(String name) { - return "$typedesce$" + name; + return "$typedesc$" + name; } private void loadFutureType(MethodVisitor mv, BFutureType bType) { From 0cde6eb122add190ccd6bf31c06aaaed9814dea2 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 23 Aug 2024 18:10:26 +0530 Subject: [PATCH 26/87] Get typedesc variables from different packages --- .../ballerinalang/compiler/bir/BIRGen.java | 59 +++++++++++++++++-- .../asyncFunctionPackage/Dependencies.toml | 26 +++++++- .../ModuleNameClashProject/Dependencies.toml | 29 ++++++++- .../PackageNameClashProject/Dependencies.toml | 26 +++++++- .../bar/Dependencies.toml | 29 ++++++++- .../baz/Dependencies.toml | 29 ++++++++- .../foo/Dependencies.toml | 29 ++++++++- .../qux/Dependencies.toml | 29 ++++++++- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../versions/myPackage/Dependencies.toml | 26 +++++++- .../versions/myPackage2/Dependencies.toml | 32 ++++++++-- .../versions/websub/Dependencies.toml | 2 +- .../AnonymousTupleAnnotationTest.java | 9 +-- 29 files changed, 316 insertions(+), 43 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 0704a920da6a..59a45bcc11ee 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -60,6 +60,7 @@ import org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer; import org.wso2.ballerinalang.compiler.diagnostic.BLangDiagnosticLocation; import org.wso2.ballerinalang.compiler.semantics.analyzer.Types; +import org.wso2.ballerinalang.compiler.semantics.model.Scope; import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BAttachedFunction; @@ -69,6 +70,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BObjectTypeSymbol; +import org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BResourceFunction; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BResourcePathSegmentSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.BServiceSymbol; @@ -2430,19 +2432,66 @@ private BIRNonTerminator.NewStructure createNewStructureInst(List entry : env.symbolVarMap.entrySet()) { - if (isTypeDescSymbol(entry.getKey(), type)) { - return env.symbolVarMap.get(entry.getKey()); + BIRVariableDcl variableDcl = findInPackageScope(type); + if (variableDcl != null) { + return variableDcl; + } + + variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap); + if (variableDcl != null) { + return variableDcl; + } + + return findInGlobalSymbolVarMap(type, this.globalVarMap); + } + + private BIRVariableDcl findInPackageScope(BType type) { + BTypeSymbol typeSymbol = type.tsymbol; + if (typeSymbol != null && isDifferentPackage(type)) { + BPackageSymbol packageSymbol = (BPackageSymbol) typeSymbol.owner; + Scope.ScopeEntry scopeEntry = + packageSymbol.scope.lookup(new Name(getTypedescFieldName(typeSymbol.name.value))); + BSymbol symbol = scopeEntry.symbol; + if (symbol != null) { + BLangPackageVarRef packageVarRef = createPackageVarRef(symbol); + visit(packageVarRef); + return this.env.targetOperand.variableDcl; } } + return null; + } - for (Map.Entry entry : this.globalVarMap.entrySet()) { + private boolean isDifferentPackage(BType type) { + return type.tsymbol.owner.tag == SymTag.PACKAGE && !type.tsymbol.pkgID.equals(env.enclPkg.packageID); + } + + private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { + BLangPackageVarRef packageVarRef = new BLangPackageVarRef((BVarSymbol) symbol); + packageVarRef.pos = symbol.pos; + packageVarRef.setBType(symbol.getType()); + return packageVarRef; + } + + private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { + for (Map.Entry entry : varMap.entrySet()) { if (isTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { - return this.globalVarMap.get(entry.getKey()); + return varMap.get(entry.getKey()); } } + return null; + } + private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map varMap) { + for (Map.Entry entry : varMap.entrySet()) { + if (isTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { + return varMap.get(entry.getKey()); + } + } return null; } diff --git a/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml index bce290a1cbde..0e618f9a47fe 100644 --- a/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -15,6 +15,24 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -23,12 +41,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" + [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml index 140442cf8660..5592b01c9064 100644 --- a/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,6 +13,26 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -22,6 +42,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" +scope = "testOnly" + [[package]] org = "ballerina" name = "test" @@ -29,6 +55,7 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml index ea459518354a..50f750a7f01b 100644 --- a/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "a" @@ -37,6 +37,24 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -45,12 +63,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" + [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml index 9a64ecf1dce3..30626ea0dbea 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,6 +13,26 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -22,6 +42,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" +scope = "testOnly" + [[package]] org = "ballerina" name = "test" @@ -29,6 +55,7 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml index 0a50c1e34b6b..a3f069710613 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,6 +13,26 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -22,6 +42,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" +scope = "testOnly" + [[package]] org = "ballerina" name = "test" @@ -29,6 +55,7 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml index 26e4ab30b2db..98f4886160d1 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,6 +13,26 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -22,6 +42,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" +scope = "testOnly" + [[package]] org = "ballerina" name = "test" @@ -29,6 +55,7 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml index dddea62ca3c3..0a277dcf961d 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,6 +13,26 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -22,6 +42,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" +scope = "testOnly" + [[package]] org = "ballerina" name = "test" @@ -29,6 +55,7 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml index 62ce04d10ce7..0e8bbcac625d 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml index 52b25f319ce3..5c941e4ff5b1 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml index 5ac3b997dd76..112d4e16eda5 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml index ffffcc1753a6..dbc4a5679ebf 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml index 78c92672185f..8de6f85b3f5a 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml index 046df8e7362f..811601b7030c 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml index 11b3dfaecfc7..2eb4fd4a250f 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml index f85033065246..cda67e8fd104 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml index 27e6ac36f2e6..b1cb39b56f59 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml index e2524f13eb0c..ffaaa6044c61 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml index 637c33a1418b..a2d7051b7e46 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml index 89503e842498..d573929beb8a 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml index 754af8541970..289d752c9283 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml index 68a1bb047e18..d02cdf214ac8 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml index 1839e511a48f..1274ee12b359 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml index b1f86063f902..a243ce68538f 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml index 0a781bdbdccd..1717c4977121 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml index 75f9ad6346d3..39dcdfd8a038 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -15,6 +15,24 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -23,12 +41,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" + [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml index db406d87ecd9..45b9e524813e 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "ballerina" @@ -15,6 +15,24 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.__internal" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.object"} +] + +[[package]] +org = "ballerina" +name = "lang.array" +version = "0.0.0" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.__internal"} +] + [[package]] org = "ballerina" name = "lang.error" @@ -23,12 +41,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.object" +version = "0.0.0" + [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ @@ -36,7 +60,7 @@ modules = [ ] [[package]] -org = "gabilan" +org = "dulminakodagoda" name = "myPackage2" version = "0.1.0" dependencies = [ @@ -46,8 +70,8 @@ dependencies = [ {org = "waruna", name = "websub"} ] modules = [ - {org = "gabilan", packageName = "myPackage2", moduleName = "myPackage2"}, - {org = "gabilan", packageName = "myPackage2", moduleName = "myPackage2.mod1"} + {org = "dulminakodagoda", packageName = "myPackage2", moduleName = "myPackage2"}, + {org = "dulminakodagoda", packageName = "myPackage2", moduleName = "myPackage2.mod1"} ] [[package]] diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml index a17bfc27d78b..633472fbc3ef 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.6.0-SNAPSHOT" +distribution-version = "2201.10.0-SNAPSHOT" [[package]] org = "waruna" diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java index 36b2699b6510..64dda2ff064e 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java @@ -51,14 +51,7 @@ public void testLocalRecordAnnotations(String function) { @DataProvider(name = "dataToTestAnnotationsOfLocalTuple") public Object[] dataToTestAnnotationsOfLocalTuple() { return new String[]{ - "testAnnotationOnTupleFields", - "testAnnotationOnTupleFields2", - "testAnnotationOnTupleWithGlobalVariable", - "testMultipleAnnotationsOnLocalTuple", - "testTupleAnnotationsOnFunctionPointerReturnType", - "testGlobalAnnotationsOnFunctionReturnType", - "testGlobalAnnotationsOnFunctionParameterType", - "testTupleMemberAnnotations" + "testAnnotationOnTupleFields" }; } From aaa3a9b0e0e80affe45f33e1b3c664b40fdbb8dc Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 28 Aug 2024 13:57:18 +0530 Subject: [PATCH 27/87] Rewrite constants and variables at `desugarTopLevelNodes` --- .../org/wso2/ballerinalang/compiler/desugar/Desugar.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index bc4116cead3f..236734fc51c0 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -793,13 +793,12 @@ public void visit(BLangPackage pkgNode) { BLangBlockStmt serviceAttachments = serviceDesugar.rewriteServiceVariables(pkgNode.services, env); pkgNode.services.forEach(service -> serviceDesugar.engageCustomServiceDesugar(service, env)); + // Desugar variables, constants and type definitions. desugarTopLevelNodes(pkgNode); annotationDesugar.rewritePackageAnnotations(pkgNode, env); rewrite(pkgNode.xmlnsList, env); - rewrite(pkgNode.constants, env); - rewrite(pkgNode.globalVars, env); rewrite(pkgNode.classDefinitions, env); // Add invocation for user specified module init function (`init()`) if present and return. @@ -853,6 +852,7 @@ private void desugarConstants(BLangConstant constant, BLangBlockFunctionBody ini BLangAssignment constInit = ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, expression); initFnBody.stmts.add(constInit); constant.expr = null; + rewrite(constant, initFunctionEnv); } private void createTypedescVariableDef(BLangType typeNode) { @@ -1015,6 +1015,7 @@ private void desugarVariable(BLangVariable variable, SymbolEnv initFunctionEnv, for (BLangStatement statement : statements) { addToGlobalVariableList(statement, initFnBody, variable, desugaredGlobalVarList); } + rewrite(variable, initFunctionEnv); } private void desugarGlobalVariables(SymbolEnv initFunctionEnv, List desugaredGlobalVarList, @@ -1035,6 +1036,7 @@ private void desugarGlobalVariables(SymbolEnv initFunctionEnv, List Date: Wed, 28 Aug 2024 13:58:50 +0530 Subject: [PATCH 28/87] Remove `visitTypeDesc` method --- .../ballerinalang/compiler/bir/BIRGen.java | 41 ++++--------------- 1 file changed, 7 insertions(+), 34 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 59a45bcc11ee..2063a14248f4 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -207,6 +207,7 @@ import org.wso2.ballerinalang.compiler.util.Unifier; import org.wso2.ballerinalang.util.Flags; +import javax.xml.XMLConstants; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -217,8 +218,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -import javax.xml.XMLConstants; - import static org.ballerinalang.model.tree.NodeKind.CLASS_DEFN; import static org.ballerinalang.model.tree.NodeKind.INVOCATION; import static org.ballerinalang.model.tree.NodeKind.STATEMENT_EXPRESSION; @@ -1119,6 +1118,7 @@ public void visit(BLangSimpleVariable varNode) { birVarDcl.annotAttachments.addAll(getBIRAnnotAttachments(varNode.symbol.getAnnotations())); this.env.enclPkg.globalVars.add(birVarDcl); + this.env.symbolVarMap.put(varNode.symbol, birVarDcl); this.globalVarMap.put(varNode.symbol, birVarDcl); env.enclPkg.isListenerAvailable |= Symbols.isFlagOn(varNode.symbol.flags, Flags.LISTENER); @@ -1703,10 +1703,6 @@ public void visit(BLangSimpleVarRef.BLangFieldVarRef fieldVarRef) { @Override public void visit(BLangArrayLiteral astArrayLiteralExpr) { - BType bType = astArrayLiteralExpr.getBType(); - if (bType.tag == TypeTags.TUPLE) { - visitTypedesc(astArrayLiteralExpr.pos, bType); - } generateListConstructorExpr(astArrayLiteralExpr); } @@ -1722,10 +1718,6 @@ public void visit(BLangGroupExpr groupExpr) { @Override public void visit(BLangJSONArrayLiteral jsonArrayLiteralExpr) { - BType bType = jsonArrayLiteralExpr.getBType(); - if (bType.tag == TypeTags.TUPLE) { - visitTypedesc(jsonArrayLiteralExpr.pos, bType); - } generateListConstructorExpr(jsonArrayLiteralExpr); } @@ -2013,7 +2005,6 @@ public void visit(BLangWaitExpr waitExpr) { @Override public void visit(BLangWaitForAllExpr.BLangWaitLiteral waitLiteral) { this.env.isInArrayOrStructure++; - visitTypedesc(waitLiteral.pos, waitLiteral.getBType()); BIRBasicBlock thenBB = new BIRBasicBlock(this.env.nextBBId()); addToTrapStack(thenBB); BType type = waitLiteral.getBType(); @@ -2279,30 +2270,12 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr) { public void visit(BLangSimpleVarRef.BLangTypeLoad typeLoad) { BType type = typeLoad.symbol.tag == SymTag.TYPE_DEF ? ((BTypeDefinitionSymbol) typeLoad.symbol).referenceType : typeLoad.symbol.type; - visitTypedesc(typeLoad.pos, type); - } - - private void visitTypedesc(Location pos, BType type) { - visitTypedesc(pos, type, Collections.emptyList(), null); - } - - private void visitTypedesc(Location pos, BType type, BIROperand annotations) { - visitTypedesc(pos, type, Collections.emptyList(), annotations); - } - - private void visitTypedesc(Location pos, BType type, List varDcls, BIROperand annotations) { - BIRVariableDcl tempVarDcl = new BIRVariableDcl(symTable.typeDesc, this.env.nextLocalVarId(names), - VarScope.FUNCTION, VarKind.TEMP); - BIRGenEnv env = this.env; - env.enclFunc.localVars.add(tempVarDcl); - BIROperand toVarRef = new BIROperand(tempVarDcl); - if (annotations != null) { - setScopeAndEmit(new BIRNonTerminator.NewTypeDesc(pos, toVarRef, type, varDcls, annotations)); - env.targetOperand = toVarRef; - return; + BIRVariableDcl typedescVar = getTypedescVariable(type); + if (typedescVar != null) { + env.targetOperand = new BIROperand(typedescVar); + } else { + createNewTypedescInst(type, type, typeLoad.pos); } - setScopeAndEmit(new BIRNonTerminator.NewTypeDesc(pos, toVarRef, type, Collections.emptyList())); - env.targetOperand = toVarRef; } @Override From 5e40d0dc897dd42d90ddcafd73786c9443c6fec2 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 3 Sep 2024 12:00:34 +0530 Subject: [PATCH 29/87] Add typedesc statement before the var declaration Add typedesc statement before the var declaration in the init function body --- .../compiler/desugar/Desugar.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 236734fc51c0..026d9947fb02 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -841,18 +841,18 @@ public void visit(BLangPackage pkgNode) { result = pkgNode; } - private void desugarConstants(BLangConstant constant, BLangBlockFunctionBody initFnBody, - SymbolEnv initFunctionEnv) { + private void desugarConstants(BLangConstant constant, List desugaredGlobalVarList, + BLangBlockFunctionBody initFnBody, SymbolEnv initFunctionEnv) { BType constType = Types.getReferredType(constant.symbol.type); if (constType.tag != TypeTags.INTERSECTION) { return; } + rewrite(constant, initFunctionEnv); + addTypeDescStmtsToInitFunction(initFunctionEnv, desugaredGlobalVarList, initFnBody); BLangSimpleVarRef constVarRef = ASTBuilderUtil.createVariableRef(constant.pos, constant.symbol); - BLangExpression expression = rewrite(constant.expr, initFunctionEnv); - BLangAssignment constInit = ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, expression); + BLangAssignment constInit = ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, constant.expr); initFnBody.stmts.add(constInit); constant.expr = null; - rewrite(constant, initFunctionEnv); } private void createTypedescVariableDef(BLangType typeNode) { @@ -972,6 +972,7 @@ private List getConfigurableLangLibInvocationParam(BLangSimpleV return new ArrayList<>(Arrays.asList(orgLiteral, moduleNameLiteral, versionLiteral, configNameLiteral, typedescExpr)); } + private void desugarTopLevelNodes(BLangPackage pkgNode) { List desugaredGlobalVarList = new ArrayList<>(); typedescList = new ArrayList<>(); @@ -981,41 +982,42 @@ private void desugarTopLevelNodes(BLangPackage pkgNode) { for (int i = 0; i < pkgNode.topLevelNodes.size(); i++) { TopLevelNode topLevelNode = pkgNode.topLevelNodes.get(i); switch (topLevelNode.getKind()) { - case TUPLE_VARIABLE: - case RECORD_VARIABLE: - case ERROR_VARIABLE: - desugarVariable((BLangVariable) topLevelNode, initFunctionEnv, initFnBody, desugaredGlobalVarList); - break; - case VARIABLE: - desugarGlobalVariables(initFunctionEnv, desugaredGlobalVarList, (BLangVariable) topLevelNode, - initFnBody); - break; - case CONSTANT: - desugarConstants((BLangConstant) topLevelNode, initFnBody, initFunctionEnv); - break; - case TYPE_DEFINITION: + case TUPLE_VARIABLE, RECORD_VARIABLE, ERROR_VARIABLE -> + desugarVariable((BLangVariable) topLevelNode, initFunctionEnv, initFnBody, + desugaredGlobalVarList); + case VARIABLE -> + desugarGlobalVariables(initFunctionEnv, desugaredGlobalVarList, (BLangVariable) topLevelNode, + initFnBody); + case CONSTANT -> + desugarConstants((BLangConstant) topLevelNode, desugaredGlobalVarList, initFnBody, + initFunctionEnv); + case TYPE_DEFINITION -> { rewrite((BLangTypeDefinition) topLevelNode, env); - break; - } - if (!typedescList.isEmpty()) { - for (BLangSimpleVariableDef variableDef : typedescList) { - desugarGlobalVariables(initFunctionEnv, desugaredGlobalVarList, variableDef.var, initFnBody); + addTypeDescStmtsToInitFunction(initFunctionEnv, desugaredGlobalVarList, initFnBody); } - typedescList.clear(); } } pkgNode.globalVars = desugaredGlobalVarList; } + private void addTypeDescStmtsToInitFunction(SymbolEnv initFunctionEnv, List desugaredGlobalVarList, + BLangBlockFunctionBody initFnBody) { + for (BLangSimpleVariableDef variableDef : typedescList) { + rewrite(variableDef, initFunctionEnv); + addToInitFunction(variableDef.var, initFnBody); + desugaredGlobalVarList.add(variableDef.var); + } + typedescList.clear(); + } + private void desugarVariable(BLangVariable variable, SymbolEnv initFunctionEnv, BLangBlockFunctionBody initFnBody, List desugaredGlobalVarList) { BLangNode blockStatementNode = rewrite(variable, initFunctionEnv); List statements = ((BLangBlockStmt) blockStatementNode).stmts; - + addTypeDescStmtsToInitFunction(initFunctionEnv, desugaredGlobalVarList, initFnBody); for (BLangStatement statement : statements) { addToGlobalVariableList(statement, initFnBody, variable, desugaredGlobalVarList); } - rewrite(variable, initFunctionEnv); } private void desugarGlobalVariables(SymbolEnv initFunctionEnv, List desugaredGlobalVarList, @@ -1032,11 +1034,11 @@ private void desugarGlobalVariables(SymbolEnv initFunctionEnv, List Date: Thu, 5 Sep 2024 16:22:37 +0530 Subject: [PATCH 30/87] Fix the pkdId and owner of the typedesc statement --- .../org/wso2/ballerinalang/compiler/desugar/Desugar.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 026d9947fb02..d96f2ac0ff5e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -1002,7 +1002,13 @@ private void desugarTopLevelNodes(BLangPackage pkgNode) { private void addTypeDescStmtsToInitFunction(SymbolEnv initFunctionEnv, List desugaredGlobalVarList, BLangBlockFunctionBody initFnBody) { + BSymbol owner = this.env.scope.owner; for (BLangSimpleVariableDef variableDef : typedescList) { + // typedesc statements are created while rewriting the type node. For that the env will be initFuncEnv. + // But the owner of the symbol should be the package. Hence, correct it here. + BSymbol varSymbol = variableDef.var.symbol; + varSymbol.pkgID = owner.pkgID; + varSymbol.owner = owner; rewrite(variableDef, initFunctionEnv); addToInitFunction(variableDef.var, initFnBody); desugaredGlobalVarList.add(variableDef.var); From 11c0ffef4006690a6d2ad68a8db8408f170ae8b8 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 6 Sep 2024 19:12:03 +0530 Subject: [PATCH 31/87] Preserve element typedesc variableDecl in `NewArray` --- .../wso2/ballerinalang/compiler/bir/BIRGen.java | 13 ++++++++++--- .../compiler/bir/codegen/JvmInstructionGen.java | 15 +++++---------- .../bir/codegen/model/JLargeArrayInstruction.java | 4 +++- .../codegen/optimizer/LargeMethodOptimizer.java | 3 ++- .../compiler/bir/model/BIRNonTerminator.java | 1 + 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 2063a14248f4..05c4115cce1a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2831,9 +2831,16 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo listConstructorExpr.pos)); } else { - setScopeAndEmit( - new BIRNonTerminator.NewArray(listConstructorExpr.pos, listConstructorExprType, toVarRef, sizeOp, - initialValues)); + BIRNonTerminator.NewArray newArrayIns = new BIRNonTerminator.NewArray(listConstructorExpr.pos, + listConstructorExprType, toVarRef, sizeOp, initialValues); + BType elementType = Types.getImpliedType(((BArrayType) referredType).getElementType()); + // If the referredType is an array type and the element type is record type, then we need to set + // the element type desc which will be used to initialize the `ArrayValueImpl` + if (elementType.tag == TypeTags.RECORD) { + BIRVariableDcl typedescVar = getTypedescVariable(elementType); + newArrayIns.elementTypedescOp = new BIROperand(typedescVar); + } + setScopeAndEmit(newArrayIns); } this.env.targetOperand = toVarRef; this.env.isInArrayOrStructure--; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java index 89523efc7525..0045e7de9fca 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmInstructionGen.java @@ -529,9 +529,8 @@ private void generateJLargeArrayIns(int localVarOffset, JLargeArrayInstruction i jvmTypeGen.loadType(this.mv, inst.type); loadListInitialValues(inst); BType elementType = JvmCodeGenUtil.getImpliedType(((BArrayType) instType).eType); - if (elementType.tag == TypeTags.RECORD || (elementType.tag == TypeTags.INTERSECTION && - ((BIntersectionType) elementType).effectiveType.tag == TypeTags.RECORD)) { - visitNewRecordArray(elementType); + if (elementType.tag == TypeTags.RECORD) { + visitNewRecordArray(inst.elementTypedescOp.variableDcl); } else { this.mv.visitMethodInsn(INVOKESPECIAL, ARRAY_VALUE_IMPL, JVM_INIT_METHOD, INIT_ARRAY, false); @@ -1447,7 +1446,7 @@ void generateArrayNewIns(BIRNonTerminator.NewArray inst, int localVarOffset) { BType elementType = JvmCodeGenUtil.getImpliedType(((BArrayType) instType).eType); if (elementType.tag == TypeTags.RECORD) { - visitNewRecordArray(elementType); + visitNewRecordArray(inst.elementTypedescOp.variableDcl); } else { this.mv.visitMethodInsn(INVOKESPECIAL, ARRAY_VALUE_IMPL, JVM_INIT_METHOD, INIT_ARRAY, false); @@ -1463,12 +1462,8 @@ void generateArrayNewIns(BIRNonTerminator.NewArray inst, int localVarOffset) { } } - private void visitNewRecordArray(BType type) { - BType elementType = JvmCodeGenUtil.getImpliedType(type); - String typeOwner = JvmCodeGenUtil.getPackageName(type.tsymbol.pkgID) + MODULE_INIT_CLASS_NAME; - String typedescFieldName = - jvmTypeGen.getTypedescFieldName(toNameString(elementType)); - this.mv.visitFieldInsn(GETSTATIC, typeOwner, typedescFieldName, "L" + TYPEDESC_VALUE + ";"); + private void visitNewRecordArray(BIRNode.BIRVariableDcl elementTypeDesc) { + this.loadVar(elementTypeDesc); this.mv.visitMethodInsn(INVOKESPECIAL, ARRAY_VALUE_IMPL, JVM_INIT_METHOD, INIT_ARRAY_WITH_INITIAL_VALUES, false); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/model/JLargeArrayInstruction.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/model/JLargeArrayInstruction.java index 94fc56ae626f..b97694085c6c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/model/JLargeArrayInstruction.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/model/JLargeArrayInstruction.java @@ -31,6 +31,7 @@ public class JLargeArrayInstruction extends JInstruction { public BIROperand typedescOp; + public BIROperand elementTypedescOp; public BIROperand sizeOp; public BType type; public BIROperand values; @@ -46,9 +47,10 @@ public JLargeArrayInstruction(Location location, BType type, BIROperand lhsOp, B } public JLargeArrayInstruction(Location location, BType type, BIROperand lhsOp, BIROperand typedescOp, - BIROperand sizeOp, BIROperand values) { + BIROperand elementTypedescOp, BIROperand sizeOp, BIROperand values) { this(location, type, lhsOp, sizeOp, values); this.typedescOp = typedescOp; + this.elementTypedescOp = elementTypedescOp; } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index 20aa3d646e15..13930579b766 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -248,7 +248,8 @@ private void periodicSplitArray(BIRFunction parentFunc, List newlyA BIROperand handleArrayOperand = new BIROperand(handleArray); createAndAddNewHandleArrayForLargeArrayIns(parentFuncEnv, arrayIns, handleArray, handleArrayOperand); JLargeArrayInstruction newLargeArrayIns = new JLargeArrayInstruction(arrayIns.pos, - arrayIns.type, arrayIns.lhsOp, arrayIns.typedescOp, arrayIns.sizeOp, handleArrayOperand); + arrayIns.type, arrayIns.lhsOp, arrayIns.typedescOp, arrayIns.elementTypedescOp, arrayIns.sizeOp, + handleArrayOperand); // populating ListConstructorEntry array elements at runtime using jMethodCalls // creating method calls diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java index 753ae63bcd84..57435ba432fc 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java @@ -319,6 +319,7 @@ public void setRhsOperands(BIROperand[] operands) { */ public static class NewArray extends BIRNonTerminator { public BIROperand typedescOp; + public BIROperand elementTypedescOp; public BIROperand sizeOp; public BType type; public List values; From 3e12916aa32dc71d5fcb66e0cefbcb01d78beac5 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 10 Sep 2024 12:03:48 +0530 Subject: [PATCH 32/87] Order typedefinitions with toplevel nodes --- .../semantics/analyzer/DataflowAnalyzer.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index e5b4503ae1d9..6ed2c8c55000 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -2635,23 +2635,23 @@ private void recordGlobalVariableReferenceRelationship(BSymbol symbol) { boolean globalVarSymbol = isGlobalVarSymbol(symbol); BSymbol ownerSymbol = this.env.scope.owner; - boolean isInPkgLevel = ownerSymbol.getKind() == SymbolKind.PACKAGE; - // Restrict to observations made in pkg level. - if (isInPkgLevel && (globalVarSymbol || symbol instanceof BTypeSymbol) - || (ownerSymbol.tag == SymTag.LET && globalVarSymbol)) { - BSymbol dependent = this.currDependentSymbolDeque.peek(); - addDependency(dependent, symbol); - } else if (ownerSymbol.kind == SymbolKind.FUNCTION && globalVarSymbol) { - // Global variable ref from non package level. - BInvokableSymbol invokableOwnerSymbol = (BInvokableSymbol) ownerSymbol; - addDependency(invokableOwnerSymbol, symbol); - } else if (ownerSymbol.kind == SymbolKind.OBJECT && globalVarSymbol) { - // Global variable reference from a field assignment of an object or a service. - // Or global variable reference from a init function of an object or a service. - addDependency(ownerSymbol, symbol); - } else if (ownerSymbol.kind == SymbolKind.RECORD && globalVarSymbol) { - // Global variable reference from a field assignment of an record type declaration. - addDependency(ownerSymbol, symbol); + switch (ownerSymbol.getKind()) { + case FUNCTION : + // Global variable ref from non package level. + case OBJECT: + // Global variable reference from a field assignment of an object or a service. + // Or global variable reference from an init function of an object or a service. + case RECORD: + // Global variable reference from a field assignment of a record type declaration. + if (globalVarSymbol) { + addDependency(ownerSymbol, symbol); + break; + } + default: + if (globalVarSymbol || symbol instanceof BTypeSymbol || ownerSymbol.tag == SymTag.LET) { + BSymbol dependent = this.currDependentSymbolDeque.peek(); + addDependency(dependent, symbol); + } } } From 077313b570678521bf92b9df8b9f18c6c6800a90 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 11 Sep 2024 16:56:08 +0530 Subject: [PATCH 33/87] Fix issue in getting the typedesc var ref from different packages --- .../main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 05c4115cce1a..d65b3c7da509 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2431,9 +2431,7 @@ private BIRVariableDcl findInPackageScope(BType type) { packageSymbol.scope.lookup(new Name(getTypedescFieldName(typeSymbol.name.value))); BSymbol symbol = scopeEntry.symbol; if (symbol != null) { - BLangPackageVarRef packageVarRef = createPackageVarRef(symbol); - visit(packageVarRef); - return this.env.targetOperand.variableDcl; + return getVarRef(createPackageVarRef(symbol)); } } return null; From 5d22ed656cb57c76abc8ebffa3720287d1bb0555 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 13 Sep 2024 12:14:34 +0530 Subject: [PATCH 34/87] Undo test removal commit --- .../test/annotations/AnonymousTupleAnnotationTest.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java index 64dda2ff064e..36b2699b6510 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/AnonymousTupleAnnotationTest.java @@ -51,7 +51,14 @@ public void testLocalRecordAnnotations(String function) { @DataProvider(name = "dataToTestAnnotationsOfLocalTuple") public Object[] dataToTestAnnotationsOfLocalTuple() { return new String[]{ - "testAnnotationOnTupleFields" + "testAnnotationOnTupleFields", + "testAnnotationOnTupleFields2", + "testAnnotationOnTupleWithGlobalVariable", + "testMultipleAnnotationsOnLocalTuple", + "testTupleAnnotationsOnFunctionPointerReturnType", + "testGlobalAnnotationsOnFunctionReturnType", + "testGlobalAnnotationsOnFunctionParameterType", + "testTupleMemberAnnotations" }; } From 7f545f98d9ff358adfdd8c3f13b49b0f22aad68e Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 19 Sep 2024 18:51:06 +0530 Subject: [PATCH 35/87] Fix issue in different module var ref names Add var ref from different modules in the same package to `importedGlobalVarsDummyVarDcls` --- .../ballerinalang/compiler/bir/BIRGen.java | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index d65b3c7da509..a6cdd84eaea1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -1693,8 +1693,8 @@ public void visit(BLangTypeInit connectorInitExpr) { this.env.targetOperand = toVarRef; } - private boolean isInSamePackage(BSymbol objectTypeSymbol, PackageID packageID) { - return objectTypeSymbol.pkgID.equals(packageID); + private boolean isInSamePackage(BSymbol symbol, PackageID packageID) { + return symbol.pkgID.equals(packageID); } @Override @@ -2425,7 +2425,8 @@ private BIRVariableDcl getTypedescVariable(BType type) { private BIRVariableDcl findInPackageScope(BType type) { BTypeSymbol typeSymbol = type.tsymbol; - if (typeSymbol != null && isDifferentPackage(type)) { + if (typeSymbol != null && typeSymbol.owner.tag == SymTag.PACKAGE && + !isInSamePackage(typeSymbol, env.enclPkg.packageID)) { BPackageSymbol packageSymbol = (BPackageSymbol) typeSymbol.owner; Scope.ScopeEntry scopeEntry = packageSymbol.scope.lookup(new Name(getTypedescFieldName(typeSymbol.name.value))); @@ -2437,10 +2438,6 @@ private BIRVariableDcl findInPackageScope(BType type) { return null; } - private boolean isDifferentPackage(BType type) { - return type.tsymbol.owner.tag == SymTag.PACKAGE && !type.tsymbol.pkgID.equals(env.enclPkg.packageID); - } - private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { BLangPackageVarRef packageVarRef = new BLangPackageVarRef((BVarSymbol) symbol); packageVarRef.pos = symbol.pos; @@ -2450,7 +2447,7 @@ private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { - if (isTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { + if (isMatchingTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { return varMap.get(entry.getKey()); } } @@ -2459,14 +2456,19 @@ private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { - if (isTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { - return varMap.get(entry.getKey()); + BSymbol varSymbol = entry.getKey(); + if (isMatchingTypeDescSymbol(varSymbol, Types.getImpliedType(type))) { + BIRGlobalVariableDcl globalVarDcl = varMap.get(varSymbol); + if (!isInSamePackage(varSymbol, env.enclPkg.packageID) || env.enclPkg.packageID.isTestPkg) { + this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); + } + return globalVarDcl; } } return null; } - private boolean isTypeDescSymbol(BSymbol symbol, BType targetType) { + private boolean isMatchingTypeDescSymbol(BSymbol symbol, BType targetType) { return symbol.type.tag == TypeTags.TYPEDESC && ((BTypedescType) symbol.type).constraint == targetType; } From e2b2b3b804f369e8504a819cf9d29e55372f1ec8 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 20 Sep 2024 16:40:23 +0530 Subject: [PATCH 36/87] Refactor code --- .../io/ballerina/runtime/internal/MapUtils.java | 4 ++-- .../org/wso2/ballerinalang/compiler/bir/BIRGen.java | 13 ++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/MapUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/MapUtils.java index 730910c05232..e2f4ac3a6e3b 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/MapUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/MapUtils.java @@ -49,7 +49,7 @@ public class MapUtils { public static void handleMapStore(MapValue mapValue, BString fieldName, Object value) { - updateMapValue(TypeUtils.getImpliedType(mapValue.getType()), mapValue, fieldName, value); + updateMapValue(mapValue.getType(), mapValue, fieldName, value); } public static void handleInherentTypeViolatingMapUpdate(Object value, BMapType mapType) { @@ -144,7 +144,7 @@ public static void checkIsMapOnlyOperation(Type mapType, String op) { private static void updateMapValue(Type mapType, MapValue mapValue, BString fieldName, Object value) { - + mapType = TypeUtils.getImpliedType(mapType); switch (mapType.getTag()) { case TypeTags.MAP_TAG: handleInherentTypeViolatingMapUpdate(value, (BMapType) mapType); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index a6cdd84eaea1..0f4bad7a2170 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -1118,7 +1118,6 @@ public void visit(BLangSimpleVariable varNode) { birVarDcl.annotAttachments.addAll(getBIRAnnotAttachments(varNode.symbol.getAnnotations())); this.env.enclPkg.globalVars.add(birVarDcl); - this.env.symbolVarMap.put(varNode.symbol, birVarDcl); this.globalVarMap.put(varNode.symbol, birVarDcl); env.enclPkg.isListenerAvailable |= Symbols.isFlagOn(varNode.symbol.flags, Flags.LISTENER); @@ -1634,7 +1633,7 @@ public void visit(BLangMapLiteral astMapLiteralExpr) { BIROperand toVarRef = new BIROperand(tempVarDcl); setScopeAndEmit(createNewStructureInst(generateMappingConstructorEntries(astMapLiteralExpr.fields), toVarRef, - Types.getImpliedType(type), astMapLiteralExpr.pos)); + type, astMapLiteralExpr.pos)); this.env.targetOperand = toVarRef; this.env.isInArrayOrStructure--; } @@ -1681,7 +1680,7 @@ public void visit(BLangTypeInit connectorInitExpr) { BType objectType = getEffectiveObjectType(exprType); BTypeSymbol objectTypeSymbol = Types.getImpliedType(objectType).tsymbol; BIRNonTerminator.NewInstance instruction; - if (isInSamePackage(objectTypeSymbol, env.enclPkg.packageID)) { + if (isInSameModule(objectTypeSymbol, env.enclPkg.packageID)) { BIRTypeDefinition def = typeDefs.get(objectTypeSymbol); instruction = new BIRNonTerminator.NewInstance(connectorInitExpr.pos, def, toVarRef, objectType); } else { @@ -1693,7 +1692,7 @@ public void visit(BLangTypeInit connectorInitExpr) { this.env.targetOperand = toVarRef; } - private boolean isInSamePackage(BSymbol symbol, PackageID packageID) { + private boolean isInSameModule(BSymbol symbol, PackageID packageID) { return symbol.pkgID.equals(packageID); } @@ -1921,7 +1920,7 @@ private BIRGlobalVariableDcl getVarRef(BLangPackageVarRef astPackageVarRefExpr) this.globalVarMap.put(symbol, globalVarDcl); } - if (!isInSamePackage(astPackageVarRefExpr.varSymbol, env.enclPkg.packageID) || + if (!isInSameModule(astPackageVarRefExpr.varSymbol, env.enclPkg.packageID) || env.enclPkg.packageID.isTestPkg) { this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); } @@ -2426,7 +2425,7 @@ private BIRVariableDcl getTypedescVariable(BType type) { private BIRVariableDcl findInPackageScope(BType type) { BTypeSymbol typeSymbol = type.tsymbol; if (typeSymbol != null && typeSymbol.owner.tag == SymTag.PACKAGE && - !isInSamePackage(typeSymbol, env.enclPkg.packageID)) { + !isInSameModule(typeSymbol, env.enclPkg.packageID)) { BPackageSymbol packageSymbol = (BPackageSymbol) typeSymbol.owner; Scope.ScopeEntry scopeEntry = packageSymbol.scope.lookup(new Name(getTypedescFieldName(typeSymbol.name.value))); @@ -2459,7 +2458,7 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map Date: Tue, 24 Sep 2024 14:20:01 +0530 Subject: [PATCH 37/87] Use the original type when creating the typedesc statements --- .../wso2/ballerinalang/compiler/bir/BIRGen.java | 15 ++++++++++----- .../ballerinalang/compiler/desugar/Desugar.java | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 0f4bad7a2170..3499b7327854 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -238,6 +238,7 @@ public class BIRGen extends BLangNodeVisitor { new CompilerContext.Key<>(); public static final String DEFAULT_WORKER_NAME = "function"; + private static final String TYPEDESC = "$typedesc$"; private BIRGenEnv env; private final Names names; private final SymbolTable symTable; @@ -2446,7 +2447,7 @@ private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { - if (isMatchingTypeDescSymbol(entry.getKey(), Types.getImpliedType(type))) { + if (isMatchingTypeDescSymbol(entry.getKey(), type)) { return varMap.get(entry.getKey()); } } @@ -2456,7 +2457,7 @@ private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { BSymbol varSymbol = entry.getKey(); - if (isMatchingTypeDescSymbol(varSymbol, Types.getImpliedType(type))) { + if (isMatchingTypeDescSymbol(varSymbol, type)) { BIRGlobalVariableDcl globalVarDcl = varMap.get(varSymbol); if (!isInSameModule(varSymbol, env.enclPkg.packageID) || env.enclPkg.packageID.isTestPkg) { this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); @@ -2468,7 +2469,11 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map desuga } private void createTypedescVariableDef(BLangType typeNode) { - BType type = typeNode.getBType(); + BType type = null; + BLangNode parentNode = typeNode.parent; + if (parentNode != null && parentNode.getKind() == NodeKind.TYPE_DEFINITION) { + // type should be the original user defined type if exists + BSymbol typeDefSymbol = ((BLangTypeDefinition) parentNode).symbol; + if (typeDefSymbol.kind == SymbolKind.TYPE_DEF && typeDefSymbol.origin != VIRTUAL) { + type = ((BTypeDefinitionSymbol)((BLangTypeDefinition) parentNode).symbol).referenceType; + } + } + if (type == null) { + type = typeNode.getBType(); + } + Name name = generateTypedescVariableName(type); Location pos = typeNode.pos; BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); From 1d66ee29d3b681c86685032958ab1579ddc9a5cd Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 30 Sep 2024 10:44:31 +0530 Subject: [PATCH 38/87] Fix failing `BIROptimizer` tests --- .../test-src/bir/bir-dump/failLockWithinLock | 30 +-- .../test-src/bir/bir-dump/failWithinOnFail | 27 +- .../bir/bir-dump/globalVarsAndAnonFunctions | 6 +- .../resources/test-src/bir/bir-dump/mapInits | 234 +++++++++--------- .../test-src/bir/bir-dump/setNillableField | 34 ++- .../test-src/bir/bir-dump/setOptionalField | 26 +- .../test-src/bir/bir-dump/setRequiredField | 18 +- 7 files changed, 184 insertions(+), 191 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock index 776a8a92d311..0cbac375b008 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock @@ -8,14 +8,14 @@ failLockWithinLock function() -> (int, string) { %14(LOCAL) error; %16(TEMP) string; %18(TEMP) (); - %19(TEMP) typeDesc; - %20(TEMP) map; + %19(TEMP) map; + %20(TEMP) string; %21(TEMP) string; - %22(TEMP) string; - %23(TEMP) map; + %22(TEMP) map; %24(LOCAL) error; + %40(TEMP) int; %41(TEMP) int; - %42(TEMP) int; + %43(TEMP) (int, string); bb0 { lock -> bb1; @@ -50,14 +50,14 @@ failLockWithinLock function() -> (int, string) { %16 = ConstLoad custom error; %18 = ConstLoad 0; %12 = %18; - %19 = newType map; - %21 = ConstLoad message; - %22 = ConstLoad error value; - %20 = NewMap %19{%21:%22}; - %23 = cloneReadOnly(%20) -> bb8; + %20 = ConstLoad message; + %21 = ConstLoad error value; + %22 = newType map; + %19 = NewMap %22{%20:%21}; + %22 = cloneReadOnly(%19) -> bb8; } bb8 { - %14 = error error(%16, %12, %23); + %14 = error error(%16, %12, %22); GOTO bb9; } bb9 { @@ -101,10 +101,10 @@ failLockWithinLock function() -> (int, string) { panic %12; } bb19 { - %19 = newType (int, string); - %41 = ConstLoad 2; - %42 = lockWithinLockInt; - %0 = newArray %19[%41]{%42,lockWithinLockString}; + %40 = ConstLoad 2; + %41 = lockWithinLockInt; + %43 = newType (int, string); + %0 = newArray %43[%40]{%41,lockWithinLockString}; GOTO bb20; } bb20 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail index 7703bcf0ce93..3db7760d4930 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail @@ -8,11 +8,10 @@ failWithinOnFail function() -> string|error { %14(LOCAL) error; %17(TEMP) error; %18(TEMP) (); - %19(TEMP) typeDesc; - %20(TEMP) map; + %19(TEMP) map; + %20(TEMP) string; %21(TEMP) string; - %22(TEMP) string; - %23(TEMP) map; + %22(TEMP) map; bb0 { %1 = ConstLoad 0; @@ -32,22 +31,22 @@ failWithinOnFail function() -> string|error { %9 = ConstLoad custom error; %18 = ConstLoad 0; %17 = %18; - %19 = newType map; - %21 = ConstLoad message; - %22 = ConstLoad error value; - %20 = NewMap %19{%21:%22}; - %23 = cloneReadOnly(%20) -> bb3; + %20 = ConstLoad message; + %21 = ConstLoad error value; + %22 = newType map; + %19 = NewMap %22{%20:%21}; + %22 = cloneReadOnly(%19) -> bb3; } bb3 { - %14 = error error(%9, %17, %23); - %21 = ConstLoad -> Error caught in inner on fail; - %3 = %3 + %21; + %14 = error error(%9, %17, %22); + %20 = ConstLoad -> Error caught in inner on fail; + %3 = %3 + %20; %0 = %14; GOTO bb6; } bb4 { - %22 = ConstLoad -> After do statement; - %3 = %3 + %22; + %21 = ConstLoad -> After do statement; + %3 = %3 + %21; GOTO bb1; } bb5 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions index 46c78071843e..ebc4f8081356 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions @@ -1,15 +1,15 @@ public globalVarsAndAnonFunctions function() -> () { %0(RETURN) (); %1(SYNTHETIC) map; - %2(TEMP) typeDesc; + %3(TEMP) map; %4(TEMP) any|error; %5(TEMP) int; %7(TEMP) string; %12(LOCAL) function(int) -> int; bb0 { - %2 = newType map; - %1 = NewMap %2{}; + %3 = newType map; + %1 = NewMap %3{}; %5 = ConstLoad 3; %4 = %5; %7 = ConstLoad a; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits index c63308da760c..215b677cb562 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits @@ -1,256 +1,256 @@ mapInits function() -> (string|(), int|()) { %0(RETURN) (string|(), int|()); %1(LOCAL) map; - %2(TEMP) typeDesc; + %3(TEMP) map; %4(LOCAL) Person; + %6(TEMP) string; %7(TEMP) string; %8(TEMP) string; - %9(TEMP) string; - %10(TEMP) int; + %9(TEMP) int; + %10(TEMP) string; %11(TEMP) string; - %12(TEMP) string; - %13(TEMP) Employee; - %20(SYNTHETIC) string|(); - %21(SYNTHETIC) Employee|(); - %25(SYNTHETIC) Employee|(); - %27(SYNTHETIC) boolean; - %28(SYNTHETIC) boolean; - %29(SYNTHETIC) any|error; - %30(TEMP) boolean; - %42(SYNTHETIC) boolean; - %43(SYNTHETIC) boolean; - %44(SYNTHETIC) any|error; - %60(SYNTHETIC) boolean; - %61(SYNTHETIC) boolean; - %71(TEMP) (); - %73(SYNTHETIC) int|(); - %74(SYNTHETIC) Employee|(); - %78(SYNTHETIC) Employee|(); - %80(SYNTHETIC) boolean; - %81(SYNTHETIC) boolean; - %82(SYNTHETIC) any|error; - %95(SYNTHETIC) boolean; - %96(SYNTHETIC) boolean; - %97(SYNTHETIC) any|error; - %109(TEMP) int; - %113(SYNTHETIC) boolean; - %114(SYNTHETIC) boolean; + %12(TEMP) Employee; + %18(SYNTHETIC) string|(); + %19(SYNTHETIC) Employee|(); + %23(SYNTHETIC) Employee|(); + %25(SYNTHETIC) boolean; + %26(SYNTHETIC) boolean; + %27(SYNTHETIC) any|error; + %28(TEMP) boolean; + %40(SYNTHETIC) boolean; + %41(SYNTHETIC) boolean; + %42(SYNTHETIC) any|error; + %58(SYNTHETIC) boolean; + %59(SYNTHETIC) boolean; + %69(TEMP) (); + %71(SYNTHETIC) int|(); + %72(SYNTHETIC) Employee|(); + %76(SYNTHETIC) Employee|(); + %78(SYNTHETIC) boolean; + %79(SYNTHETIC) boolean; + %80(SYNTHETIC) any|error; + %93(SYNTHETIC) boolean; + %94(SYNTHETIC) boolean; + %95(SYNTHETIC) any|error; + %107(TEMP) int; + %111(SYNTHETIC) boolean; + %112(SYNTHETIC) boolean; + %124(TEMP) (string|(), int|()); bb0 { - %2 = newType map; - %1 = NewMap %2{}; - %2 = newType Person; - %7 = ConstLoad name; - %8 = ConstLoad Jack; - %9 = ConstLoad age; - %10 = ConstLoad 25; - %11 = ConstLoad address; - %12 = ConstLoad Usa; - %4 = NewMap %2{%7:%8,%9:%10,%11:%12}; - %13 = %4; + %3 = newType map; + %1 = NewMap %3{}; + %6 = ConstLoad name; + %7 = ConstLoad Jack; + %8 = ConstLoad age; + %9 = ConstLoad 25; + %10 = ConstLoad address; + %11 = ConstLoad Usa; + %4 = NewMap $typedesc$Person{%6:%7,%8:%9,%10:%11}; + %12 = %4; + %6 = ConstLoad jack; + %1[%6] = %12; + %9 = ConstLoad 2; %7 = ConstLoad jack; - %1[%7] = %13; - %2 = newType (string|(), int|()); - %10 = ConstLoad 2; - %8 = ConstLoad jack; - %21 = %1[%8]; - %25 = %21; - %30 = ConstLoad true; - %30? bb1 : bb2; + %19 = %1[%7]; + %23 = %19; + %28 = ConstLoad true; + %28? bb1 : bb2; } bb1 { - %28 = ConstLoad true; - %29 = %25; + %26 = ConstLoad true; + %27 = %23; GOTO bb3; } bb2 { - %28 = ConstLoad false; + %26 = ConstLoad false; GOTO bb3; } bb3 { - %28? bb4 : bb5; + %26? bb4 : bb5; } bb4 { - %27 = %29 is (); + %25 = %27 is (); GOTO bb6; } bb5 { - %27 = ConstLoad false; + %25 = ConstLoad false; GOTO bb6; } bb6 { - %27? bb7 : bb8; + %25? bb7 : bb8; } bb7 { - %20 = %29; + %18 = %27; GOTO bb24; } bb8 { - %30 = ConstLoad true; - %30? bb9 : bb10; + %28 = ConstLoad true; + %28? bb9 : bb10; } bb9 { - %43 = ConstLoad true; - %44 = %25; + %41 = ConstLoad true; + %42 = %23; GOTO bb11; } bb10 { - %43 = ConstLoad false; + %41 = ConstLoad false; GOTO bb11; } bb11 { - %43? bb12 : bb13; + %41? bb12 : bb13; } bb12 { - %42 = %44 is Employee; + %40 = %42 is Employee; GOTO bb14; } bb13 { - %42 = ConstLoad false; + %40 = ConstLoad false; GOTO bb14; } bb14 { - %42? bb15 : bb16; + %40? bb15 : bb16; } bb15 { - %13 = %44; - %9 = ConstLoad name; - %11 = %13[%9]; - %20 = %11; + %12 = %42; + %8 = ConstLoad name; + %10 = %12[%8]; + %18 = %10; GOTO bb24; } bb16 { - %30 = ConstLoad true; - %30? bb17 : bb18; + %28 = ConstLoad true; + %28? bb17 : bb18; } bb17 { - %61 = ConstLoad true; + %59 = ConstLoad true; GOTO bb19; } bb18 { - %61 = %25 is any; + %59 = %23 is any; GOTO bb19; } bb19 { - %61? bb20 : bb21; + %59? bb20 : bb21; } bb20 { - %60 = ConstLoad true; + %58 = ConstLoad true; GOTO bb22; } bb21 { - %60 = ConstLoad false; + %58 = ConstLoad false; GOTO bb22; } bb22 { - %60? bb23 : bb24; + %58? bb23 : bb24; } bb23 { - %71 = ConstLoad 0; - %20 = %71; + %69 = ConstLoad 0; + %18 = %69; GOTO bb24; } bb24 { - %12 = ConstLoad jack; - %74 = %1[%12]; - %78 = %74; - %30 = ConstLoad true; - %30? bb25 : bb26; + %11 = ConstLoad jack; + %72 = %1[%11]; + %76 = %72; + %28 = ConstLoad true; + %28? bb25 : bb26; } bb25 { - %81 = ConstLoad true; - %82 = %78; + %79 = ConstLoad true; + %80 = %76; GOTO bb27; } bb26 { - %81 = ConstLoad false; + %79 = ConstLoad false; GOTO bb27; } bb27 { - %81? bb28 : bb29; + %79? bb28 : bb29; } bb28 { - %80 = %82 is (); + %78 = %80 is (); GOTO bb30; } bb29 { - %80 = ConstLoad false; + %78 = ConstLoad false; GOTO bb30; } bb30 { - %80? bb31 : bb32; + %78? bb31 : bb32; } bb31 { - %73 = %82; + %71 = %80; GOTO bb48; } bb32 { - %30 = ConstLoad true; - %30? bb33 : bb34; + %28 = ConstLoad true; + %28? bb33 : bb34; } bb33 { - %96 = ConstLoad true; - %97 = %78; + %94 = ConstLoad true; + %95 = %76; GOTO bb35; } bb34 { - %96 = ConstLoad false; + %94 = ConstLoad false; GOTO bb35; } bb35 { - %96? bb36 : bb37; + %94? bb36 : bb37; } bb36 { - %95 = %97 is Employee; + %93 = %95 is Employee; GOTO bb38; } bb37 { - %95 = ConstLoad false; + %93 = ConstLoad false; GOTO bb38; } bb38 { - %95? bb39 : bb40; + %93? bb39 : bb40; } bb39 { - %13 = %97; - %7 = ConstLoad age; - %109 = %13[%7]; - %73 = %109; + %12 = %95; + %6 = ConstLoad age; + %107 = %12[%6]; + %71 = %107; GOTO bb48; } bb40 { - %30 = ConstLoad true; - %30? bb41 : bb42; + %28 = ConstLoad true; + %28? bb41 : bb42; } bb41 { - %114 = ConstLoad true; + %112 = ConstLoad true; GOTO bb43; } bb42 { - %114 = %78 is any; + %112 = %76 is any; GOTO bb43; } bb43 { - %114? bb44 : bb45; + %112? bb44 : bb45; } bb44 { - %113 = ConstLoad true; + %111 = ConstLoad true; GOTO bb46; } bb45 { - %113 = ConstLoad false; + %111 = ConstLoad false; GOTO bb46; } bb46 { - %113? bb47 : bb48; + %111? bb47 : bb48; } bb47 { - %71 = ConstLoad 0; - %73 = %71; + %69 = ConstLoad 0; + %71 = %69; GOTO bb48; } bb48 { - %0 = newArray %2[%10]{%20,%73}; + %124 = newType (string|(), int|()); + %0 = newArray %124[%9]{%18,%71}; GOTO bb49; } bb49 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setNillableField b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setNillableField index d216c36953cb..723f77f33d8e 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setNillableField +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setNillableField @@ -1,26 +1,24 @@ setNillableField function() -> () { %0(RETURN) (); %1(LOCAL) R2; - %2(TEMP) typeDesc; - %4(TEMP) string; - %5(TEMP) int|(); - %6(TEMP) int; - %12(TEMP) (); + %3(TEMP) string; + %4(TEMP) int|(); + %5(TEMP) int; + %11(TEMP) (); bb0 { - %2 = newType R2; - %4 = ConstLoad x; - %6 = ConstLoad 1; - %5 = %6; - %1 = NewMap %2{%4:%5}; - %6 = ConstLoad 2; - %5 = %6; - %4 = ConstLoad x; - %1[%4] = %5; - %12 = ConstLoad 0; - %5 = %12; - %4 = ConstLoad x; - %1[%4] = %5; + %3 = ConstLoad x; + %5 = ConstLoad 1; + %4 = %5; + %1 = NewMap $typedesc$R2{%3:%4}; + %5 = ConstLoad 2; + %4 = %5; + %3 = ConstLoad x; + %1[%3] = %4; + %11 = ConstLoad 0; + %4 = %11; + %3 = ConstLoad x; + %1[%3] = %4; %0 = ConstLoad 0; GOTO bb1; } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setOptionalField b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setOptionalField index b792e069ca63..8e0f02260b01 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setOptionalField +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setOptionalField @@ -1,22 +1,20 @@ setOptionalField function() -> () { %0(RETURN) (); %1(LOCAL) R3; - %2(TEMP) typeDesc; - %4(TEMP) int; - %6(TEMP) string; - %7(TEMP) int|(); - %8(TEMP) (); + %3(TEMP) int; + %5(TEMP) string; + %6(TEMP) int|(); + %7(TEMP) (); bb0 { - %2 = newType R3; - %1 = NewMap %2{}; - %4 = ConstLoad 2; - %6 = ConstLoad x; - %1[%6] = %4; - %8 = ConstLoad 0; - %7 = %8; - %6 = ConstLoad x; - %1[%6] = %7; + %1 = NewMap $typedesc$R3{}; + %3 = ConstLoad 2; + %5 = ConstLoad x; + %1[%5] = %3; + %7 = ConstLoad 0; + %6 = %7; + %5 = ConstLoad x; + %1[%5] = %6; %0 = ConstLoad 0; GOTO bb1; } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setRequiredField b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setRequiredField index 24101ec4fd7a..dede62304784 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setRequiredField +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/setRequiredField @@ -1,18 +1,16 @@ setRequiredField function() -> () { %0(RETURN) (); %1(LOCAL) R1; - %2(TEMP) typeDesc}>; - %4(TEMP) string; - %5(TEMP) int; + %3(TEMP) string; + %4(TEMP) int; bb0 { - %2 = newType R1; - %4 = ConstLoad x; - %5 = ConstLoad 1; - %1 = NewMap %2{%4:%5}; - %5 = ConstLoad 2; - %4 = ConstLoad x; - %1[%4] = %5; + %3 = ConstLoad x; + %4 = ConstLoad 1; + %1 = NewMap $typedesc$R1{%3:%4}; + %4 = ConstLoad 2; + %3 = ConstLoad x; + %1[%3] = %4; %0 = ConstLoad 0; GOTO bb1; } From 1f88965837d5d6205ba6cf12487d69c8c666dce2 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 10 Oct 2024 14:00:14 +0530 Subject: [PATCH 39/87] Fix failing set of annotation tests --- .../main/java/io/ballerina/runtime/internal/TypeChecker.java | 1 + .../compiler/bir/codegen/split/JvmAnnotationsGen.java | 4 +--- .../ballerinalang/test/annotations/DisplayAnnotationTest.java | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index 5e94f63bd940..3b7a25e92683 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -30,6 +30,7 @@ import io.ballerina.runtime.api.types.UnionType; import io.ballerina.runtime.api.types.XmlNodeType; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BDecimal; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BObject; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java index cde5f6a01cab..7d36c6cae970 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java @@ -29,7 +29,6 @@ import org.wso2.ballerinalang.compiler.bir.codegen.JvmTypeGen; import org.wso2.ballerinalang.compiler.bir.model.BIRNode; import org.wso2.ballerinalang.compiler.semantics.model.types.BType; -import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; import java.util.List; @@ -127,8 +126,7 @@ private void loadAnnotations(MethodVisitor mv, String pkgName, BIRNode.BIRTypeDe jvmPackageGen.lookupGlobalVarClassName(pkgName, ANNOTATION_MAP_NAME); mv.visitFieldInsn(GETSTATIC, pkgClassName, ANNOTATION_MAP_NAME, JvmSignatures.GET_MAP_VALUE); BType type = typeDef.type; - BType refType = typeDef.referenceType == null || type.tag == TypeTags.RECORD - ? type : typeDef.referenceType; + BType refType = typeDef.referenceType == null ? type : typeDef.referenceType; jvmTypeGen.loadType(mv, refType); mv.visitMethodInsn(INVOKESTATIC, ANNOTATION_UTILS, "processAnnotations", JvmSignatures.PROCESS_ANNOTATIONS, false); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java index 155c2050b1a3..4124cf770fdb 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/annotations/DisplayAnnotationTest.java @@ -97,7 +97,7 @@ public void testDisplayAnnotOnObjectAndMemberFunction() { @Test public void testDisplayAnnotOnRecord() { - TypeDefinition typeDefinition = result.getAST().getTypeDefinitions().get(15); + TypeDefinition typeDefinition = result.getAST().getTypeDefinitions().get(3); List annot = typeDefinition.getAnnotationAttachments(); Assert.assertEquals(annot.size(), 1); Assert.assertEquals(annot.get(0).getExpression().toString(), From ba08a23232629fbd1a6655bfe66209c4064f6733 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 10 Oct 2024 18:27:15 +0530 Subject: [PATCH 40/87] Generate typedesc for const literal type and intersection type Generate typedesc for const literal type if record or tuple and intersection type if the effective type is record or tuple. Also did some refactoring for BIRGen --- .../ballerinalang/compiler/bir/BIRGen.java | 57 +++++++++++++------ .../compiler/desugar/Desugar.java | 45 +++++++++++++-- 2 files changed, 82 insertions(+), 20 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 3499b7327854..88ab92bc7878 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -215,6 +215,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -2400,6 +2401,7 @@ private BIRNonTerminator.NewStructure createNewStructureInst(List[] checks = new Supplier[] { + () -> findInPackageScope(type), + () -> findInLocalSymbolVarMap(type, env.symbolVarMap, false), + () -> findInLocalSymbolVarMap(type, env.symbolVarMap, true), + () -> findInGlobalSymbolVarMap(type, this.globalVarMap, false), + () -> findInGlobalSymbolVarMap(type, this.globalVarMap, true) + }; - variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap); - if (variableDcl != null) { - return variableDcl; + // Iterate through the suppliers and return the first non-null result + for (Supplier check : checks) { + BIRVariableDcl variableDcl = check.get(); + if (variableDcl != null) { + return variableDcl; + } } - return findInGlobalSymbolVarMap(type, this.globalVarMap); + return null; } private BIRVariableDcl findInPackageScope(BType type) { @@ -2445,19 +2453,21 @@ private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { return packageVarRef; } - private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { + private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap, + boolean checkImpliedType) { for (Map.Entry entry : varMap.entrySet()) { - if (isMatchingTypeDescSymbol(entry.getKey(), type)) { + if (isMatchingTypeDescSymbol(entry.getKey(), type, checkImpliedType)) { return varMap.get(entry.getKey()); } } return null; } - private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map varMap) { + private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map varMap, + boolean checkImpliedType) { for (Map.Entry entry : varMap.entrySet()) { BSymbol varSymbol = entry.getKey(); - if (isMatchingTypeDescSymbol(varSymbol, type)) { + if (isMatchingTypeDescSymbol(varSymbol, type, checkImpliedType)) { BIRGlobalVariableDcl globalVarDcl = varMap.get(varSymbol); if (!isInSameModule(varSymbol, env.enclPkg.packageID) || env.enclPkg.packageID.isTestPkg) { this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); @@ -2468,12 +2478,26 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map & readonly q = table [{name: "Jo"}]; + // Row type will be Immutable Identifier and there will be not intersection type created for that + BType constraint = ((BTypedescType) symbol.type).constraint; + constraint = checkImpliedType ? Types.getImpliedType(constraint) : constraint; + targetType = checkImpliedType ? Types.getImpliedType(targetType) : targetType; + return constraint == targetType; } private void createNewTypedescInst(BType type, BType resolveType, Location position) { @@ -2517,6 +2541,7 @@ private BIRNonTerminator.NewArray createNewArrayInst(List + (Types.getReferredType(typeDef.typeNode.getBType()).tag == TypeTags.INTERSECTION) && + typeDef.symbol.name.value.equals(typeSymbolName)))) { + // This is a workaround for an issue where we create two type defs with same name for the below sample + // type T1 [T1] & readonly; + return; + } + Name name = generateTypedescVariableName(type); Location pos = typeNode.pos; BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); BSymbol owner = this.env.scope.owner; BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); +// varSymbol.closure = true; BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type); - typedescExpr.typeNode = typeNode; BLangSimpleVariableDef simpleVariableDef = createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, varSymbol); typedescList.add(simpleVariableDef); } private Name generateTypedescVariableName(BType targetType) { + // tsymbol.name.value is empty for anonymous types except for record types return targetType.tsymbol.name.value.isEmpty()? new Name(TYPEDESC + typedescCount++) : new Name(TYPEDESC + targetType.tsymbol.name.value); } @@ -1335,6 +1346,11 @@ public void visit(BLangIntersectionTypeNode intersectionTypeNode) { rewrittenConstituents.add(rewrite(constituentTypeNode, env)); } + int tag = Types.getImpliedType(intersectionTypeNode.getBType()).tag; + if (tag == TypeTags.RECORD || tag == TypeTags.TUPLE) { + createTypedescVariableDef(intersectionTypeNode); + } + intersectionTypeNode.constituentTypeNodes = rewrittenConstituents; result = intersectionTypeNode; } @@ -8914,9 +8930,10 @@ public void visit(BLangJSONArrayLiteral jsonArrayLiteral) { public void visit(BLangConstant constant) { BConstantSymbol constSymbol = constant.symbol; - BType refType = Types.getImpliedType(constSymbol.literalType); - if (refType.tag <= TypeTags.BOOLEAN || refType.tag == TypeTags.NIL) { - if (refType.tag != TypeTags.NIL && (constSymbol.value == null || + BType impliedType = Types.getImpliedType(constSymbol.literalType); + int tag = impliedType.tag; + if (tag <= TypeTags.BOOLEAN || tag == TypeTags.NIL) { + if (tag != TypeTags.NIL && (constSymbol.value == null || constSymbol.value.value == null)) { throw new IllegalStateException(); } @@ -8926,6 +8943,26 @@ public void visit(BLangConstant constant) { } else { constant.expr = rewriteExpr(constant.expr); } + + if ((tag == TypeTags.RECORD && constant.expr.getKind() == NodeKind.RECORD_LITERAL_EXPR) || + (tag == TypeTags.TUPLE && constant.expr.getKind() == NodeKind.LIST_CONSTRUCTOR_EXPR)) { + // Literal type will have a typedesc var created via the associated type def. + // The issue is that type def has the effective type not the original intersection. + // Hence, create the typedesc var here. + // Todo: In the below sample, `a` and `b` will have the same literal type. Then the typedesc will be + // created with the same type. ATM in the BIRGen we lookup the typedesc given the type. Hence that + // logic will fail to identify the correct typedesc and it will fail when there are large methods. + // Need to find a fix for this. ATM we create only one typedesc for the + // following sample to overcome that issue. + // + // const string[] a = ["apple", "orange"]; + // const string[] b = a; + BLangType blangIntersection = (BLangIntersectionTypeNode) TreeBuilder.createIntersectionTypeNode(); + blangIntersection.setBType(constSymbol.literalType); + blangIntersection.pos = constSymbol.literalType.tsymbol.pos; + createTypedescVariableDef(blangIntersection); + } + constant.annAttachments.forEach(attachment -> rewrite(attachment, env)); result = constant; } From eb32a05546bffa93e3cd306d733de1ea45808dc9 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 14 Oct 2024 10:43:24 +0530 Subject: [PATCH 41/87] Rewrite globalVar and constants after `annotationDesugar` --- .../compiler/desugar/Desugar.java | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 3d237c694c46..61d160910588 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -800,6 +800,8 @@ public void visit(BLangPackage pkgNode) { annotationDesugar.rewritePackageAnnotations(pkgNode, env); rewrite(pkgNode.xmlnsList, env); + rewrite(pkgNode.constants, env); + rewrite(pkgNode.globalVars, env); rewrite(pkgNode.classDefinitions, env); // Add invocation for user specified module init function (`init()`) if present and return. @@ -848,7 +850,29 @@ private void desugarConstants(BLangConstant constant, List desuga if (constType.tag != TypeTags.INTERSECTION) { return; } - rewrite(constant, initFunctionEnv); + + BConstantSymbol constSymbol = constant.symbol; + BType impliedType = Types.getImpliedType(constSymbol.literalType); + int tag = impliedType.tag; + if ((tag == TypeTags.RECORD && constant.expr.getKind() == NodeKind.RECORD_LITERAL_EXPR) || + (tag == TypeTags.TUPLE && constant.expr.getKind() == NodeKind.LIST_CONSTRUCTOR_EXPR)) { + // Literal type will have a typedesc var created via the associated type def. + // The issue is that type def has the effective type not the original intersection. + // Hence, create the typedesc var here. + // Todo: In the below sample, `a` and `b` will have the same literal type. Then the typedesc will be + // created with the same type. ATM in the BIRGen we lookup the typedesc given the type. Hence that + // logic will fail to identify the correct typedesc and it will fail when there are large methods. + // Need to find a fix for this. ATM we create only one typedesc for the + // following sample to overcome that issue. + // + // const string[] a = ["apple", "orange"]; + // const string[] b = a; + BLangType blangIntersection = (BLangIntersectionTypeNode) TreeBuilder.createIntersectionTypeNode(); + blangIntersection.setBType(constSymbol.literalType); + blangIntersection.pos = constSymbol.literalType.tsymbol.pos; + createTypedescVariableDef(blangIntersection); + } + addTypeDescStmtsToInitFunction(initFunctionEnv, desugaredGlobalVarList, initFnBody); BLangSimpleVarRef constVarRef = ASTBuilderUtil.createVariableRef(constant.pos, constant.symbol); BLangAssignment constInit = ASTBuilderUtil.createAssignmentStmt(constant.pos, constVarRef, constant.expr); @@ -1064,7 +1088,12 @@ private void desugarGlobalVariables(SymbolEnv initFunctionEnv, List rewrite(attachment, env)); result = constant; } From b80968c9f140ea726c4f012a0978a3bf14d1bb57 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 14 Oct 2024 16:38:22 +0530 Subject: [PATCH 42/87] Fix issue in `ClosureGenerator` --- .../ballerinalang/compiler/desugar/ClosureGenerator.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java index bbfb4756fac9..70913a4a66ba 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/ClosureGenerator.java @@ -287,9 +287,8 @@ private void addClosuresToGlobalVariableList(SymbolEnv pkgEnv) { BLangSimpleVariable simpleVariable = queue.poll().var; simpleVariable.flagSet.add(Flag.PUBLIC); simpleVariable.symbol.flags |= Flags.PUBLIC; - BLangSimpleVariable variableDef = rewrite(simpleVariable, pkgEnv); - pkgEnv.enclPkg.globalVars.add(0, variableDef); - pkgEnv.enclPkg.topLevelNodes.add(0, variableDef); + pkgEnv.enclPkg.globalVars.add(0, simpleVariable); + pkgEnv.enclPkg.topLevelNodes.add(0, simpleVariable); } for (BLangSimpleVariableDef closureReference : annotationClosureReferences) { BLangSimpleVariable simpleVariable = rewrite(closureReference.var, pkgEnv); From f1ebb95a0cb6ec48b77ec4c1adc08ab36b18ce7f Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 15 Oct 2024 12:20:22 +0530 Subject: [PATCH 43/87] Fix set of failing test due to type mismatch --- .../bala/record/ClosedRecordTypeInclusionTest.java | 6 ++++-- .../bala/record/OpenRecordTypeInclusionTest.java | 7 +++++-- .../test/record/ClosedRecordOptionalFieldsTest.java | 12 ++++++++---- .../test/record/OpenRecordOptionalFieldsTest.java | 12 ++++++++---- .../test/types/anydata/AnydataTernaryConvTest.java | 7 ++++--- .../test/types/anydata/AnydataTest.java | 3 ++- .../test/types/typedesc/TypedescTests.java | 4 +++- 7 files changed, 34 insertions(+), 17 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java index 48ba6acd5de8..e5f8d8ece41c 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java @@ -21,6 +21,7 @@ import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; import org.ballerinalang.test.BAssertUtil; @@ -123,7 +124,7 @@ public void testRefTypes() { io.ballerina.runtime.api.TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(), "John Doe"); - assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), + assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("ra")))).getTag(), io.ballerina.runtime.api.TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " + "Lanka\"}"); @@ -137,7 +138,8 @@ public void testRefTypes() { io.ballerina.runtime.api.TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("crp"))).get(StringUtils.fromString("name")).toString(), "Jane Doe"); - assertEquals(getType(foo2.get(StringUtils.fromString("cra"))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("cra")))).getTag(), + TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("cra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " + "Lanka\"}"); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java index 122965a4088d..092e072352eb 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java @@ -21,6 +21,7 @@ import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; import org.ballerinalang.test.BAssertUtil; @@ -112,7 +113,8 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(), "John Doe"); - assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("ra")))).getTag(), + TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri Lanka\"}"); assertEquals(getType(foo2.get(StringUtils.fromString("crx"))).getTag(), TypeTags.XML_ELEMENT_TAG); @@ -123,7 +125,8 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("crp"))).get(StringUtils.fromString("name")).toString(), "Jane Doe"); - assertEquals(getType(foo2.get(StringUtils.fromString("cra"))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("cra")))).getTag(), + TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("cra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri Lanka\"}"); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java index da2f74175d23..db728fcfae63 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java @@ -18,7 +18,9 @@ package org.ballerinalang.test.record; import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; import org.ballerinalang.test.BAssertUtil; @@ -133,12 +135,14 @@ public void testOptionalNonDefField4() { BArray returns = (BArray) BRunUtil.invoke(compileResult, "testOptionalNonDefField4"); Assert.assertEquals(returns.get(0).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\"," + "\"country\":\"LK\"}"); - Assert.assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(getType(returns.get(0)).getName(), "Address3"); + Type type = TypeUtils.getReferredType(getType(returns.get(0))); + Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(type.getName(), "Address3"); Assert.assertEquals(returns.get(1).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\"," + "\"country\":\"LK\"}"); - Assert.assertEquals(getType(returns.get(1)).getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(getType(returns.get(1)).getName(), "Address3"); + type = TypeUtils.getReferredType(getType(returns.get(1))); + Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(type.getName(), "Address3"); } @Test(description = "Test defaultable user defined type as an optional field") diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java index a242beaca77e..a750ca30b2d0 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java @@ -18,7 +18,9 @@ package org.ballerinalang.test.record; import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; import org.ballerinalang.test.BAssertUtil; @@ -136,12 +138,14 @@ public void testOptionalNonDefField4() { BArray returns = (BArray) BRunUtil.invoke(compileResult, "testOptionalNonDefField4"); Assert.assertEquals(returns.get(0).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\",\"country\":\"LK\"}"); - Assert.assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(getType(returns.get(0)).getName(), "Address3"); + Type type = TypeUtils.getReferredType(getType(returns.get(0))); + Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(type.getName(), "Address3"); Assert.assertEquals(returns.get(1).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\",\"country\":\"LK\"}"); - Assert.assertEquals(getType(returns.get(1)).getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(getType(returns.get(1)).getName(), "Address3"); + type = TypeUtils.getReferredType(getType(returns.get(1))); + Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(type.getName(), "Address3"); } @Test(description = "Test defaultable user defined type as an optional field") diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java index de9fdb2b56ab..71391a4f997c 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java @@ -19,6 +19,7 @@ package org.ballerinalang.test.types.anydata; import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.internal.types.BArrayType; import org.ballerinalang.test.BCompileUtil; @@ -73,7 +74,7 @@ public void testAnydataToXml() { @Test(description = "Test anydata to record conversion") public void testAnydataToRecord() { BArray returns = (BArray) BRunUtil.invoke(result, "testAnydataToRecord"); - assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(returns.get(0))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(returns.get(0).toString(), "{\"a\":15}"); } @@ -97,8 +98,8 @@ public void testAnydataToUnion2() { BArray returns = (BArray) BRunUtil.invoke(result, "testAnydataToUnion2"); assertEquals(getType(returns.get(0)).getTag(), TypeTags.MAP_TAG); assertEquals(getType(returns.get(1)).getTag(), TypeTags.XML_ELEMENT_TAG); - assertEquals(getType(returns.get(2)).getTag(), TypeTags.RECORD_TYPE_TAG); - assertEquals(getType(returns.get(3)).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(returns.get(2))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(returns.get(3))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(returns.get(0).toString(), "{\"name\":\"apple\",\"color\":\"red\",\"price\":40}"); assertEquals(returns.get(1).toString(), "The Lost World"); assertEquals(returns.get(2).toString(), "{\"a\":15}"); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java index 9fdd7bf63909..23b830e190b6 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java @@ -20,6 +20,7 @@ import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BXml; @@ -181,7 +182,7 @@ public void testAnydataToXml() { @Test(description = "Test anydata to record conversion") public void testAnydataToRecord() { BArray returns = (BArray) BRunUtil.invoke(result, "testAnydataToRecord"); - assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(returns.get(0))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(returns.get(0).toString(), "{\"a\":15}"); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java index 1b2961b21ff0..ead5f1f3d890 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java @@ -17,6 +17,7 @@ package org.ballerinalang.test.types.typedesc; import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BTypedesc; import org.ballerinalang.test.BCompileUtil; @@ -124,7 +125,8 @@ public void testRecordTypes() { Assert.assertEquals(returns.size(), 2); Assert.assertEquals(returns.get(0).toString(), "typedesc RecordA"); Assert.assertTrue(returns.get(1) instanceof BTypedesc); - Assert.assertEquals(TypeTags.RECORD_TYPE_TAG, ((BTypedesc) returns.get(1)).getDescribingType().getTag()); + Assert.assertEquals(TypeTags.RECORD_TYPE_TAG, + TypeUtils.getReferredType(((BTypedesc) returns.get(1)).getDescribingType()).getTag()); } @Test(description = "Test any to typedesc cast") From f831aa0c5eda83e490a10ac42c16db212a458235 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 15 Oct 2024 16:07:15 +0530 Subject: [PATCH 44/87] Fix annotation not processed correctly for record typedef --- .../compiler/bir/codegen/split/JvmAnnotationsGen.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java index 7d36c6cae970..cb088288b21e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java @@ -19,6 +19,7 @@ package org.wso2.ballerinalang.compiler.bir.codegen.split; import org.ballerinalang.model.elements.PackageID; +import org.ballerinalang.model.symbols.SymbolOrigin; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.wso2.ballerinalang.compiler.bir.codegen.BallerinaClassWriter; @@ -29,6 +30,7 @@ import org.wso2.ballerinalang.compiler.bir.codegen.JvmTypeGen; import org.wso2.ballerinalang.compiler.bir.model.BIRNode; import org.wso2.ballerinalang.compiler.semantics.model.types.BType; +import org.wso2.ballerinalang.compiler.util.TypeTags; import org.wso2.ballerinalang.util.Flags; import java.util.List; @@ -126,7 +128,8 @@ private void loadAnnotations(MethodVisitor mv, String pkgName, BIRNode.BIRTypeDe jvmPackageGen.lookupGlobalVarClassName(pkgName, ANNOTATION_MAP_NAME); mv.visitFieldInsn(GETSTATIC, pkgClassName, ANNOTATION_MAP_NAME, JvmSignatures.GET_MAP_VALUE); BType type = typeDef.type; - BType refType = typeDef.referenceType == null ? type : typeDef.referenceType; + BType refType = typeDef.referenceType == null || + (type.tag == TypeTags.RECORD && typeDef.origin == SymbolOrigin.VIRTUAL)? type : typeDef.referenceType; jvmTypeGen.loadType(mv, refType); mv.visitMethodInsn(INVOKESTATIC, ANNOTATION_UTILS, "processAnnotations", JvmSignatures.PROCESS_ANNOTATIONS, false); From 7eaa889c795874309d1f7f1d20967a6c5cc4029a Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 16 Oct 2024 12:01:52 +0530 Subject: [PATCH 45/87] Generate type desc variable for map type --- .../compiler/desugar/Desugar.java | 9 +- .../resources/test-src/bir/bir-dump/mapInits | 226 +++++++++--------- 2 files changed, 119 insertions(+), 116 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 61d160910588..da05e67864eb 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -917,9 +917,9 @@ private void createTypedescVariableDef(BLangType typeNode) { } private Name generateTypedescVariableName(BType targetType) { - // tsymbol.name.value is empty for anonymous types except for record types - return targetType.tsymbol.name.value.isEmpty()? new Name(TYPEDESC + typedescCount++) : - new Name(TYPEDESC + targetType.tsymbol.name.value); + // tsymbol.name.value is empty for anonymous types except for record types and map types + return targetType.tag == TypeTags.MAP || targetType.tsymbol.name.value.isEmpty()? + new Name(TYPEDESC + typedescCount++) : new Name(TYPEDESC + targetType.tsymbol.name.value); } private BLangSimpleVariableDef createSimpleVariableDef(Location pos, String name, BType type, BLangExpression expr, @@ -1326,6 +1326,9 @@ public void visit(BLangArrayType arrayType) { @Override public void visit(BLangConstrainedType constrainedType) { constrainedType.constraint = rewrite(constrainedType.constraint, env); + if (constrainedType.getBType() != null && constrainedType.getBType().tag == TypeTags.MAP) { + createTypedescVariableDef(constrainedType); + } result = constrainedType; } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits index 215b677cb562..94af4ef44e21 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits @@ -1,256 +1,256 @@ mapInits function() -> (string|(), int|()) { %0(RETURN) (string|(), int|()); - %1(LOCAL) map; - %3(TEMP) map; - %4(LOCAL) Person; - %6(TEMP) string; + %1(SYNTHETIC) typeDesc>; + %3(LOCAL) map; + %5(LOCAL) Person; %7(TEMP) string; %8(TEMP) string; - %9(TEMP) int; - %10(TEMP) string; + %9(TEMP) string; + %10(TEMP) int; %11(TEMP) string; - %12(TEMP) Employee; - %18(SYNTHETIC) string|(); - %19(SYNTHETIC) Employee|(); - %23(SYNTHETIC) Employee|(); - %25(SYNTHETIC) boolean; + %12(TEMP) string; + %13(TEMP) Employee; + %19(SYNTHETIC) string|(); + %20(SYNTHETIC) Employee|(); + %24(SYNTHETIC) Employee|(); %26(SYNTHETIC) boolean; - %27(SYNTHETIC) any|error; - %28(TEMP) boolean; - %40(SYNTHETIC) boolean; + %27(SYNTHETIC) boolean; + %28(SYNTHETIC) any|error; + %29(TEMP) boolean; %41(SYNTHETIC) boolean; - %42(SYNTHETIC) any|error; - %58(SYNTHETIC) boolean; + %42(SYNTHETIC) boolean; + %43(SYNTHETIC) any|error; %59(SYNTHETIC) boolean; - %69(TEMP) (); - %71(SYNTHETIC) int|(); - %72(SYNTHETIC) Employee|(); - %76(SYNTHETIC) Employee|(); - %78(SYNTHETIC) boolean; + %60(SYNTHETIC) boolean; + %70(TEMP) (); + %72(SYNTHETIC) int|(); + %73(SYNTHETIC) Employee|(); + %77(SYNTHETIC) Employee|(); %79(SYNTHETIC) boolean; - %80(SYNTHETIC) any|error; - %93(SYNTHETIC) boolean; + %80(SYNTHETIC) boolean; + %81(SYNTHETIC) any|error; %94(SYNTHETIC) boolean; - %95(SYNTHETIC) any|error; - %107(TEMP) int; - %111(SYNTHETIC) boolean; + %95(SYNTHETIC) boolean; + %96(SYNTHETIC) any|error; + %108(TEMP) int; %112(SYNTHETIC) boolean; - %124(TEMP) (string|(), int|()); + %113(SYNTHETIC) boolean; + %125(TEMP) (string|(), int|()); bb0 { - %3 = newType map; - %1 = NewMap %3{}; - %6 = ConstLoad name; - %7 = ConstLoad Jack; - %8 = ConstLoad age; - %9 = ConstLoad 25; - %10 = ConstLoad address; - %11 = ConstLoad Usa; - %4 = NewMap $typedesc$Person{%6:%7,%8:%9,%10:%11}; - %12 = %4; - %6 = ConstLoad jack; - %1[%6] = %12; - %9 = ConstLoad 2; + %1 = newType map; + %3 = NewMap %1{}; + %7 = ConstLoad name; + %8 = ConstLoad Jack; + %9 = ConstLoad age; + %10 = ConstLoad 25; + %11 = ConstLoad address; + %12 = ConstLoad Usa; + %5 = NewMap $typedesc$Person{%7:%8,%9:%10,%11:%12}; + %13 = %5; %7 = ConstLoad jack; - %19 = %1[%7]; - %23 = %19; - %28 = ConstLoad true; - %28? bb1 : bb2; + %3[%7] = %13; + %10 = ConstLoad 2; + %8 = ConstLoad jack; + %20 = %3[%8]; + %24 = %20; + %29 = ConstLoad true; + %29? bb1 : bb2; } bb1 { - %26 = ConstLoad true; - %27 = %23; + %27 = ConstLoad true; + %28 = %24; GOTO bb3; } bb2 { - %26 = ConstLoad false; + %27 = ConstLoad false; GOTO bb3; } bb3 { - %26? bb4 : bb5; + %27? bb4 : bb5; } bb4 { - %25 = %27 is (); + %26 = %28 is (); GOTO bb6; } bb5 { - %25 = ConstLoad false; + %26 = ConstLoad false; GOTO bb6; } bb6 { - %25? bb7 : bb8; + %26? bb7 : bb8; } bb7 { - %18 = %27; + %19 = %28; GOTO bb24; } bb8 { - %28 = ConstLoad true; - %28? bb9 : bb10; + %29 = ConstLoad true; + %29? bb9 : bb10; } bb9 { - %41 = ConstLoad true; - %42 = %23; + %42 = ConstLoad true; + %43 = %24; GOTO bb11; } bb10 { - %41 = ConstLoad false; + %42 = ConstLoad false; GOTO bb11; } bb11 { - %41? bb12 : bb13; + %42? bb12 : bb13; } bb12 { - %40 = %42 is Employee; + %41 = %43 is Employee; GOTO bb14; } bb13 { - %40 = ConstLoad false; + %41 = ConstLoad false; GOTO bb14; } bb14 { - %40? bb15 : bb16; + %41? bb15 : bb16; } bb15 { - %12 = %42; - %8 = ConstLoad name; - %10 = %12[%8]; - %18 = %10; + %13 = %43; + %9 = ConstLoad name; + %11 = %13[%9]; + %19 = %11; GOTO bb24; } bb16 { - %28 = ConstLoad true; - %28? bb17 : bb18; + %29 = ConstLoad true; + %29? bb17 : bb18; } bb17 { - %59 = ConstLoad true; + %60 = ConstLoad true; GOTO bb19; } bb18 { - %59 = %23 is any; + %60 = %24 is any; GOTO bb19; } bb19 { - %59? bb20 : bb21; + %60? bb20 : bb21; } bb20 { - %58 = ConstLoad true; + %59 = ConstLoad true; GOTO bb22; } bb21 { - %58 = ConstLoad false; + %59 = ConstLoad false; GOTO bb22; } bb22 { - %58? bb23 : bb24; + %59? bb23 : bb24; } bb23 { - %69 = ConstLoad 0; - %18 = %69; + %70 = ConstLoad 0; + %19 = %70; GOTO bb24; } bb24 { - %11 = ConstLoad jack; - %72 = %1[%11]; - %76 = %72; - %28 = ConstLoad true; - %28? bb25 : bb26; + %12 = ConstLoad jack; + %73 = %3[%12]; + %77 = %73; + %29 = ConstLoad true; + %29? bb25 : bb26; } bb25 { - %79 = ConstLoad true; - %80 = %76; + %80 = ConstLoad true; + %81 = %77; GOTO bb27; } bb26 { - %79 = ConstLoad false; + %80 = ConstLoad false; GOTO bb27; } bb27 { - %79? bb28 : bb29; + %80? bb28 : bb29; } bb28 { - %78 = %80 is (); + %79 = %81 is (); GOTO bb30; } bb29 { - %78 = ConstLoad false; + %79 = ConstLoad false; GOTO bb30; } bb30 { - %78? bb31 : bb32; + %79? bb31 : bb32; } bb31 { - %71 = %80; + %72 = %81; GOTO bb48; } bb32 { - %28 = ConstLoad true; - %28? bb33 : bb34; + %29 = ConstLoad true; + %29? bb33 : bb34; } bb33 { - %94 = ConstLoad true; - %95 = %76; + %95 = ConstLoad true; + %96 = %77; GOTO bb35; } bb34 { - %94 = ConstLoad false; + %95 = ConstLoad false; GOTO bb35; } bb35 { - %94? bb36 : bb37; + %95? bb36 : bb37; } bb36 { - %93 = %95 is Employee; + %94 = %96 is Employee; GOTO bb38; } bb37 { - %93 = ConstLoad false; + %94 = ConstLoad false; GOTO bb38; } bb38 { - %93? bb39 : bb40; + %94? bb39 : bb40; } bb39 { - %12 = %95; - %6 = ConstLoad age; - %107 = %12[%6]; - %71 = %107; + %13 = %96; + %7 = ConstLoad age; + %108 = %13[%7]; + %72 = %108; GOTO bb48; } bb40 { - %28 = ConstLoad true; - %28? bb41 : bb42; + %29 = ConstLoad true; + %29? bb41 : bb42; } bb41 { - %112 = ConstLoad true; + %113 = ConstLoad true; GOTO bb43; } bb42 { - %112 = %76 is any; + %113 = %77 is any; GOTO bb43; } bb43 { - %112? bb44 : bb45; + %113? bb44 : bb45; } bb44 { - %111 = ConstLoad true; + %112 = ConstLoad true; GOTO bb46; } bb45 { - %111 = ConstLoad false; + %112 = ConstLoad false; GOTO bb46; } bb46 { - %111? bb47 : bb48; + %112? bb47 : bb48; } bb47 { - %69 = ConstLoad 0; - %71 = %69; + %70 = ConstLoad 0; + %72 = %70; GOTO bb48; } bb48 { - %124 = newType (string|(), int|()); - %0 = newArray %124[%9]{%18,%71}; + %125 = newType (string|(), int|()); + %0 = newArray %125[%10]{%19,%72}; GOTO bb49; } bb49 { From 5a97157160327c3210bea29eae62fa982bda16d8 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 16 Oct 2024 12:03:12 +0530 Subject: [PATCH 46/87] Fix failing typedesc test --- .../ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java index ddd8f825b11e..d1afb0abc38f 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/tests/VariableReturnType.java @@ -259,6 +259,7 @@ public static Object getInvalidValue(BTypedesc td1, BTypedesc td2) { } private static Object getValue(Type type) { + type = TypeUtils.getImpliedType(type); switch (type.getTag()) { case INT_TAG: return 150L; From 5876754de6824e25ce273c092c38a0e9d7b39a7b Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 16 Oct 2024 14:00:56 +0530 Subject: [PATCH 47/87] Fix issue where record default values are not being populated correctly --- .../io/ballerina/runtime/internal/values/MapValueImpl.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index 699747cf318d..9d1d3ef2584a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -23,6 +23,7 @@ import io.ballerina.runtime.api.types.Field; import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BFunctionPointer; @@ -319,8 +320,9 @@ public void setTypeForcefully(Type type) { protected void populateInitialValues(BMapInitialValueEntry[] initialValues) { Map> defaultValues = new HashMap<>(); - if (type.getTag() == TypeTags.RECORD_TYPE_TAG) { - defaultValues.putAll(((BRecordType) type).getDefaultValues()); + Type impliedType = TypeUtils.getImpliedType(type); + if (impliedType.getTag() == TypeTags.RECORD_TYPE_TAG) { + defaultValues.putAll(((BRecordType) impliedType).getDefaultValues()); } for (BMapInitialValueEntry initialValue : initialValues) { From 68246cf5551ed13d915ec259ab7c2550daf0a0c8 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 16 Oct 2024 14:12:16 +0530 Subject: [PATCH 48/87] Add missing `getImpliedType` Usage --- .../nativeimpl/jvm/runtime/api/tests/JsonValues.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java index 0d602ce171a5..9792423735f2 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/nativeimpl/jvm/runtime/api/tests/JsonValues.java @@ -22,6 +22,7 @@ import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.utils.JsonUtils; import io.ballerina.runtime.api.utils.StringUtils; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BString; @@ -44,7 +45,7 @@ public class JsonValues { public static BMap testConvertJSONToRecord(Object record, BTypedesc t) throws BError { - Type describingType = t.getDescribingType(); + Type describingType = TypeUtils.getImpliedType(t.getDescribingType()); if (describingType instanceof StructureType) { return convertJSONToRecord(record, (StructureType) describingType); } else { From 2aed9d2b435071921d107e6472ea3b967f1b4fcd Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 18 Oct 2024 21:40:35 +0530 Subject: [PATCH 49/87] Refactor `BIRGen` code --- .../wso2/ballerinalang/compiler/bir/BIRGen.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 88ab92bc7878..30990c81e574 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2222,7 +2222,7 @@ public void visit(BLangXMLAccessExpr xmlAccessExpr) { @Override public void visit(BLangTypedescExpr accessExpr) { - createNewTypedescInst(accessExpr.getBType(), accessExpr.resolvedType, accessExpr.pos); + createNewTypedescInst(accessExpr.resolvedType, accessExpr.pos); } @Override @@ -2275,7 +2275,7 @@ public void visit(BLangSimpleVarRef.BLangTypeLoad typeLoad) { if (typedescVar != null) { env.targetOperand = new BIROperand(typedescVar); } else { - createNewTypedescInst(type, type, typeLoad.pos); + createNewTypedescInst(type, typeLoad.pos); } } @@ -2402,7 +2402,7 @@ private BIRNonTerminator.NewStructure createNewStructureInst(List Date: Fri, 18 Oct 2024 21:40:59 +0530 Subject: [PATCH 50/87] Add extra diagnostic --- .../variabledef/ForwardReferencingGlobalDefinitionTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/variabledef/ForwardReferencingGlobalDefinitionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/variabledef/ForwardReferencingGlobalDefinitionTest.java index fcdd97caf597..e3198e9a7205 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/variabledef/ForwardReferencingGlobalDefinitionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/variabledef/ForwardReferencingGlobalDefinitionTest.java @@ -131,6 +131,8 @@ public void moduleVariableWithLetExprCausingCycle() { BAssertUtil.validateError(cycle, i++, "illegal cyclic reference '[modVarQueryLet1, queryRef, modVarQuery]'", 19, 1); BAssertUtil.validateError(cycle, i++, "illegal cyclic reference '[modVarQueryLet2, queryRef2]'", 20, 1); + BAssertUtil.validateError(cycle, i++, + "illegal cyclic reference '[recD, RecordTypeWithDefaultLetExpr, moduleCode]'", 26, 1); Assert.assertEquals(cycle.getDiagnostics().length, i); } From 64caa3e7ce32b37ab94207a3611c23a46f6bc6cb Mon Sep 17 00:00:00 2001 From: rdulmina Date: Sat, 19 Oct 2024 12:35:43 +0530 Subject: [PATCH 51/87] Update failing `BIROptimizer` tests --- .../test-src/bir/bir-dump/failLockWithinLock | 12 ++++++------ .../resources/test-src/bir/bir-dump/failWithinOnFail | 7 ++++--- .../test-src/bir/bir-dump/globalVarsAndAnonFunctions | 2 +- .../test/resources/test-src/bir/bir-dump/mapInits | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock index 0cbac375b008..37e8e47d6524 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failLockWithinLock @@ -11,11 +11,11 @@ failLockWithinLock function() -> (int, string) { %19(TEMP) map; %20(TEMP) string; %21(TEMP) string; - %22(TEMP) map; + %22(TEMP) typeDesc; + %23(TEMP) map; %24(LOCAL) error; %40(TEMP) int; %41(TEMP) int; - %43(TEMP) (int, string); bb0 { lock -> bb1; @@ -54,10 +54,10 @@ failLockWithinLock function() -> (int, string) { %21 = ConstLoad error value; %22 = newType map; %19 = NewMap %22{%20:%21}; - %22 = cloneReadOnly(%19) -> bb8; + %23 = cloneReadOnly(%19) -> bb8; } bb8 { - %14 = error error(%16, %12, %22); + %14 = error error(%16, %12, %23); GOTO bb9; } bb9 { @@ -103,8 +103,8 @@ failLockWithinLock function() -> (int, string) { bb19 { %40 = ConstLoad 2; %41 = lockWithinLockInt; - %43 = newType (int, string); - %0 = newArray %43[%40]{%41,lockWithinLockString}; + %22 = newType (int, string); + %0 = newArray %22[%40]{%41,lockWithinLockString}; GOTO bb20; } bb20 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail index 3db7760d4930..8ea9cc934c9d 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail @@ -11,7 +11,8 @@ failWithinOnFail function() -> string|error { %19(TEMP) map; %20(TEMP) string; %21(TEMP) string; - %22(TEMP) map; + %22(TEMP) typeDesc; + %23(TEMP) map; bb0 { %1 = ConstLoad 0; @@ -35,10 +36,10 @@ failWithinOnFail function() -> string|error { %21 = ConstLoad error value; %22 = newType map; %19 = NewMap %22{%20:%21}; - %22 = cloneReadOnly(%19) -> bb3; + %23 = cloneReadOnly(%19) -> bb3; } bb3 { - %14 = error error(%9, %17, %22); + %14 = error error(%9, %17, %23); %20 = ConstLoad -> Error caught in inner on fail; %3 = %3 + %20; %0 = %14; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions index ebc4f8081356..eb207acbaa25 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/globalVarsAndAnonFunctions @@ -1,7 +1,7 @@ public globalVarsAndAnonFunctions function() -> () { %0(RETURN) (); %1(SYNTHETIC) map; - %3(TEMP) map; + %3(TEMP) typeDesc; %4(TEMP) any|error; %5(TEMP) int; %7(TEMP) string; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits index 94af4ef44e21..9c3639e8d4b1 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits @@ -35,7 +35,7 @@ mapInits function() -> (string|(), int|()) { %108(TEMP) int; %112(SYNTHETIC) boolean; %113(SYNTHETIC) boolean; - %125(TEMP) (string|(), int|()); + %125(TEMP) typeDesc; bb0 { %1 = newType map; From ec8cc503220808368aa64fb00434e52758a88c3f Mon Sep 17 00:00:00 2001 From: rdulmina Date: Sat, 19 Oct 2024 12:41:38 +0530 Subject: [PATCH 52/87] Fix failing test due to incorrect error msg --- .../test-src/typedefs/tuple-type-definitions-cyclic.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/tuple-type-definitions-cyclic.bal b/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/tuple-type-definitions-cyclic.bal index 8d3ef4fcf0fc..e530f2e7645a 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/tuple-type-definitions-cyclic.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/typedefs/tuple-type-definitions-cyclic.bal @@ -278,7 +278,7 @@ function testCastingToImmutableCyclicTuple() { error err = b; assert(err.message(), "{ballerina}TypeCastError"); assert( checkpanic err.detail()["message"], "incompatible types: 'MyCyclicTuple' cannot be " + - "cast to '[int,(MyCyclicTuple[] & readonly)] & readonly'"); + "cast to '(MyCyclicTuple & readonly)'"); MyCyclicTuple c = <[int, MyCyclicTuple[]] & readonly> [1, []]; MyCyclicTuple & readonly d = c; assert(d is [int, MyCyclicTuple[]] & readonly, true); From a1c03fdffae61699beb79f0db6bae193a7c4fa78 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 23 Oct 2024 14:05:00 +0530 Subject: [PATCH 53/87] Create New typedesc for type if the typedesc var is not created in the Desugar --- .../java/org/wso2/ballerinalang/compiler/bir/BIRGen.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 30990c81e574..4f44a109199d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2867,7 +2867,12 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo // the element type desc which will be used to initialize the `ArrayValueImpl` if (Types.getImpliedType(elementType).tag == TypeTags.RECORD) { BIRVariableDcl typedescVar = getTypedescVariable(elementType); - newArrayIns.elementTypedescOp = new BIROperand(typedescVar); + if (typedescVar != null) { + newArrayIns.elementTypedescOp = new BIROperand(typedescVar); + } else { + createNewTypedescInst(elementType, elementType.tsymbol.pos); + newArrayIns.elementTypedescOp = this.env.targetOperand; + } } setScopeAndEmit(newArrayIns); } From 544c3e7d0c8aab2317f3c3b518d750d67f4e1d04 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 23 Oct 2024 14:05:58 +0530 Subject: [PATCH 54/87] Fix set of failing tests --- .../compiler/desugar/Desugar.java | 60 ++++++++++--------- .../semantics/analyzer/DataflowAnalyzer.java | 13 ++-- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index da05e67864eb..8db4607fc4dc 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -854,8 +854,8 @@ private void desugarConstants(BLangConstant constant, List desuga BConstantSymbol constSymbol = constant.symbol; BType impliedType = Types.getImpliedType(constSymbol.literalType); int tag = impliedType.tag; - if ((tag == TypeTags.RECORD && constant.expr.getKind() == NodeKind.RECORD_LITERAL_EXPR) || - (tag == TypeTags.TUPLE && constant.expr.getKind() == NodeKind.LIST_CONSTRUCTOR_EXPR)) { + if (((tag == TypeTags.RECORD || tag == TypeTags.MAP) && constant.expr.getKind() == NodeKind.RECORD_LITERAL_EXPR) + || (tag == TypeTags.TUPLE && constant.expr.getKind() == NodeKind.LIST_CONSTRUCTOR_EXPR)) { // Literal type will have a typedesc var created via the associated type def. // The issue is that type def has the effective type not the original intersection. // Hence, create the typedesc var here. @@ -870,7 +870,7 @@ private void desugarConstants(BLangConstant constant, List desuga BLangType blangIntersection = (BLangIntersectionTypeNode) TreeBuilder.createIntersectionTypeNode(); blangIntersection.setBType(constSymbol.literalType); blangIntersection.pos = constSymbol.literalType.tsymbol.pos; - createTypedescVariableDef(blangIntersection); + createTypedescVariableForAnonType(blangIntersection); } addTypeDescStmtsToInitFunction(initFunctionEnv, desugaredGlobalVarList, initFnBody); @@ -880,40 +880,33 @@ private void desugarConstants(BLangConstant constant, List desuga constant.expr = null; } - private void createTypedescVariableDef(BLangType typeNode) { - BType type = null; - BLangNode parentNode = typeNode.parent; - if (parentNode != null && parentNode.getKind() == NodeKind.TYPE_DEFINITION) { - // type should be the original user defined type if exists - BSymbol typeDefSymbol = ((BLangTypeDefinition) parentNode).symbol; - if (typeDefSymbol.kind == SymbolKind.TYPE_DEF && typeDefSymbol.origin != VIRTUAL) { - type = ((BTypeDefinitionSymbol)((BLangTypeDefinition) parentNode).symbol).referenceType; - } - } - if (type == null) { - type = typeNode.getBType(); - } - - String typeSymbolName = type.tsymbol.name.value; + private void createTypedescVariable(BType type, Location pos) { if ((Types.getReferredType(type).tag != TypeTags.INTERSECTION && this.env.enclPkg.typeDefinitions.stream() .anyMatch(typeDef -> (Types.getReferredType(typeDef.typeNode.getBType()).tag == TypeTags.INTERSECTION) && - typeDef.symbol.name.value.equals(typeSymbolName)))) { + typeDef.symbol.name.value.equals(type.tsymbol.name.value)))) { // This is a workaround for an issue where we create two type defs with same name for the below sample // type T1 [T1] & readonly; return; } Name name = generateTypedescVariableName(type); - Location pos = typeNode.pos; BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); BSymbol owner = this.env.scope.owner; BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); -// varSymbol.closure = true; BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type); - BLangSimpleVariableDef simpleVariableDef = createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, - varSymbol); - typedescList.add(simpleVariableDef); + typedescList.add(createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, varSymbol)); + } + + private void createTypedescVariableForAnonType(BLangType typeNode) { + BLangNode parentNode = typeNode.parent; + if (parentNode != null && parentNode.getKind() == NodeKind.TYPE_DEFINITION) { + BSymbol typeDefSymbol = ((BLangTypeDefinition) parentNode).symbol; + if (typeDefSymbol.kind == SymbolKind.TYPE_DEF && typeDefSymbol.origin != VIRTUAL) { + return; + } + } + createTypedescVariable(typeNode.getBType(), typeNode.pos); } private Name generateTypedescVariableName(BType targetType) { @@ -1165,6 +1158,15 @@ public void visit(BLangTypeDefinition typeDef) { typeDef.typeNode = rewrite(typeDef.typeNode, env); } + BSymbol typeDefSymbol = typeDef.symbol; + if (typeDefSymbol.kind == SymbolKind.TYPE_DEF && typeDefSymbol.origin != VIRTUAL) { + BType referenceType = ((BTypeDefinitionSymbol) typeDefSymbol).referenceType; + int typeTag = Types.getImpliedType(referenceType).tag; + if (typeTag == TypeTags.RECORD || typeTag == TypeTags.MAP || typeTag == TypeTags.TUPLE) { + createTypedescVariable(referenceType, typeDefSymbol.pos); + } + } + typeDef.annAttachments.forEach(attachment -> rewrite(attachment, env)); result = typeDef; } @@ -1300,7 +1302,7 @@ public void visit(BLangRecordTypeNode recordTypeNode) { } recordTypeNode.restFieldType = rewrite(recordTypeNode.restFieldType, env); - createTypedescVariableDef(recordTypeNode); + createTypedescVariableForAnonType(recordTypeNode); if (recordTypeNode.isAnonymous && recordTypeNode.isLocal) { BLangUserDefinedType userDefinedType = desugarLocalAnonRecordTypeNode(recordTypeNode); TypeDefBuilderHelper.createTypeDefinitionForTSymbol(recordTypeNode.getBType(), @@ -1327,7 +1329,7 @@ public void visit(BLangArrayType arrayType) { public void visit(BLangConstrainedType constrainedType) { constrainedType.constraint = rewrite(constrainedType.constraint, env); if (constrainedType.getBType() != null && constrainedType.getBType().tag == TypeTags.MAP) { - createTypedescVariableDef(constrainedType); + createTypedescVariableForAnonType(constrainedType); } result = constrainedType; } @@ -1379,8 +1381,8 @@ public void visit(BLangIntersectionTypeNode intersectionTypeNode) { } int tag = Types.getImpliedType(intersectionTypeNode.getBType()).tag; - if (tag == TypeTags.RECORD || tag == TypeTags.TUPLE) { - createTypedescVariableDef(intersectionTypeNode); + if (tag == TypeTags.RECORD || tag == TypeTags.TUPLE || tag == TypeTags.MAP) { + createTypedescVariableForAnonType(intersectionTypeNode); } intersectionTypeNode.constituentTypeNodes = rewrittenConstituents; @@ -1434,7 +1436,7 @@ public void visit(BLangBuiltInRefTypeNode refTypeNode) { public void visit(BLangTupleTypeNode tupleTypeNode) { tupleTypeNode.members.forEach(member -> member.typeNode = rewrite(member.typeNode, env)); tupleTypeNode.restParamType = rewrite(tupleTypeNode.restParamType, env); - createTypedescVariableDef(tupleTypeNode); + createTypedescVariableForAnonType(tupleTypeNode); result = tupleTypeNode; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index 6ed2c8c55000..5dd7b157682e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -664,6 +664,11 @@ public void visit(BLangSimpleVariableDef varDefNode) { @Override public void visit(BLangSimpleVariable variable) { BVarSymbol symbol = variable.symbol; + boolean isRecordField = variable.flagSet.contains(Flag.FIELD); + if (!isRecordField) { + this.currDependentSymbolDeque.push(symbol); + } + analyzeNode(variable.typeNode, env); if (symbol == null) { if (variable.expr != null) { @@ -671,11 +676,6 @@ public void visit(BLangSimpleVariable variable) { } return; } - - boolean isRecordField = variable.flagSet.contains(Flag.FIELD); - if (!isRecordField) { - this.currDependentSymbolDeque.push(symbol); - } if (variable.typeNode != null && variable.typeNode.getBType() != null) { BType type = variable.typeNode.getBType(); recordGlobalVariableReferenceRelationship(Types.getImpliedType(type).tsymbol); @@ -2107,6 +2107,7 @@ public void visit(BLangLambdaFunction bLangLambdaFunction) { BLangFunction funcNode = bLangLambdaFunction.function; SymbolEnv funcEnv = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, env); visitFunctionBodyWithDynamicEnv(funcNode, funcEnv); + recordGlobalVariableReferenceRelationship(funcNode.symbol); } @Override @@ -2244,7 +2245,7 @@ public void visit(BLangUserDefinedType userDefinedType) { if (this.currDependentSymbolDeque.isEmpty()) { return; } - BType resolvedType = Types.getImpliedType(userDefinedType.getBType()); + BType resolvedType = userDefinedType.getBType(); if (resolvedType == symTable.semanticError) { return; } From a89be92675035ae9b0f9bc9b9c0d844eb4018388 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 24 Oct 2024 12:19:05 +0530 Subject: [PATCH 55/87] Remove unwanted reference type creation in `ReadOnlyUtils` --- .../ballerina/runtime/internal/values/ReadOnlyUtils.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ReadOnlyUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ReadOnlyUtils.java index 9b46eba3d30c..7ea743d029fc 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ReadOnlyUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ReadOnlyUtils.java @@ -288,11 +288,8 @@ private static BIntersectionType setImmutableIntersectionType(Type type, Set Date: Thu, 24 Oct 2024 15:35:51 +0530 Subject: [PATCH 56/87] Update failing test --- .../test-src/annotations/annotation_access_negative.bal | 2 +- ..._vs_array_readonly_violation_without_tuple_update_method.bal | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal index c3fa341d4c0b..5ba6a1dec1da 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal @@ -48,7 +48,7 @@ function testRecordTypeAnnotationReadonlyValueEdit() { error resError = res; assertEquality("{ballerina/lang.map}InherentTypeViolation", resError.message()); - assertEquality("cannot update 'readonly' field 'foo' in record of type 'Annot & readonly'", + assertEquality("cannot update 'readonly' field 'foo' in record of type '(Annot & readonly)'", resError.detail()["message"]); } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal index b04fe3c9e75a..b43baf877141 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal @@ -34,5 +34,5 @@ function testFrozenAnyArrayElementUpdate() returns error? { test:assertTrue(actualError is error); test:assertEquals((actualError).message(), "{ballerina/lang.map}InherentTypeViolation"); test:assertEquals((actualError).detail()["message"], - "cannot update 'readonly' field 'name' in record of type 'Employee & readonly'"); + "cannot update 'readonly' field 'name' in record of type '(Employee & readonly)'"); } From 8f9056d2316128ce0ce249282072dc16d53f4af2 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 29 Oct 2024 19:17:19 +0530 Subject: [PATCH 57/87] Fix issue in loading record default values --- .../optimizer/BIRRecordValueOptimizer.java | 45 ++++--------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java index 5b1f5c0e8e7d..9395d8d519fa 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java @@ -26,21 +26,17 @@ import org.wso2.ballerinalang.compiler.bir.model.InstructionKind; import org.wso2.ballerinalang.compiler.bir.model.VarKind; import org.wso2.ballerinalang.compiler.bir.model.VarScope; -import org.wso2.ballerinalang.compiler.semantics.analyzer.Types; -import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; -import org.wso2.ballerinalang.compiler.semantics.model.types.BType; import org.wso2.ballerinalang.compiler.util.Name; -import org.wso2.ballerinalang.compiler.util.TypeTags; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import static org.wso2.ballerinalang.compiler.bir.model.InstructionKind.CONST_LOAD; import static org.wso2.ballerinalang.compiler.bir.model.InstructionKind.FP_CALL; import static org.wso2.ballerinalang.compiler.bir.model.InstructionKind.TYPE_CAST; +import static org.wso2.ballerinalang.compiler.util.Constants.RECORD_DELIMITER; /** * Remove redundant default function calls for record value creation. @@ -49,9 +45,6 @@ */ public class BIRRecordValueOptimizer extends BIRVisitor { - private final List recordOperandList = new ArrayList<>(); - private final Map recordOperandTypeMap = new HashMap<>(); - private BIRNode.BIRBasicBlock lastBB = null; private BIRNode.BIRFunction currentFunction = null; private List newBBs = new ArrayList<>(); @@ -90,10 +83,9 @@ public void visit(BIRNode.BIRFunction birFunction) { public void visit(BIRNode.BIRBasicBlock basicBlock) { List instructions = basicBlock.instructions; for (BIRNonTerminator inst : instructions) { - if (Objects.requireNonNull(inst.kind) == InstructionKind.NEW_TYPEDESC) { - handleNewTypeDesc(inst); - } else if (inst.kind == InstructionKind.NEW_STRUCTURE) { - handleNewStructure((BIRNonTerminator.NewStructure) inst); + if (inst.kind == InstructionKind.NEW_STRUCTURE) { + valueCreated = true; + break; } } if (!fpRemoved) { @@ -111,16 +103,8 @@ public void visit(BIRNode.BIRBasicBlock basicBlock) { private void handleFPCall(BIRNode.BIRBasicBlock basicBlock) { BIRTerminator.FPCall fpCall = (BIRTerminator.FPCall) basicBlock.terminator; - BIROperand recOperand = recordOperandList.isEmpty() ? null : - recordOperandList.get(recordOperandList.size() - 1); - BRecordType recordType = recordOperandTypeMap.get(recOperand); - - if (recordType == null || recordType.tsymbol == null) { - resetBasicBlock(basicBlock); - return; - } - if (!fpCall.fp.variableDcl.name.value.contains(recordType.tsymbol.name.value)) { + if (!fpCall.fp.variableDcl.name.value.contains(RECORD_DELIMITER)) { resetBasicBlock(basicBlock); return; } @@ -143,19 +127,6 @@ private void handleFPCall(BIRNode.BIRBasicBlock basicBlock) { } } - private void handleNewStructure(BIRNonTerminator.NewStructure inst) { - recordOperandList.remove(inst.rhsOp); - valueCreated = true; - } - - private void handleNewTypeDesc(BIRNonTerminator inst) { - BType referredType = Types.getReferredType(((BIRNonTerminator.NewTypeDesc) inst).type); - if (referredType.tag == TypeTags.RECORD) { - recordOperandList.add(inst.lhsOp); - recordOperandTypeMap.put(inst.lhsOp, (BRecordType) referredType); - } - } - private void moveConstLoadInstruction(BIRTerminator.FPCall fpCall, BIRNode.BIRBasicBlock firstBB) { BIRNonTerminator.ConstantLoad constantLoad = (BIRNonTerminator.ConstantLoad) firstBB.instructions.get(0); if (firstBB.instructions.size() == 2) { @@ -196,8 +167,10 @@ private boolean containsOnlyConstantLoad(BIRNode.BIRFunction defaultFunction) { return false; } return switch (firstBB.instructions.size()) { - case 1 -> firstBB.instructions.get(0).kind == CONST_LOAD; - case 2 -> firstBB.instructions.get(0).kind == CONST_LOAD && firstBB.instructions.get(1).kind == TYPE_CAST; + case 1 -> firstBB.instructions.get(0).kind == CONST_LOAD && firstBB.instructions.get(0).lhsOp.variableDcl + .kind == VarKind.RETURN; + case 2 -> firstBB.instructions.get(0).kind == CONST_LOAD && firstBB.instructions.get(1).kind == TYPE_CAST + && firstBB.instructions.get(1).lhsOp.variableDcl.kind == VarKind.RETURN; default -> false; }; } From 955e8421351a79fee13fbd157d19d8fd00937be7 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 31 Oct 2024 15:08:15 +0530 Subject: [PATCH 58/87] Create typedesc for parent type before generating for nested types This is required to prevent passing large number of arguments for split functions generated via `LargeMethodSplitter.Java`. We need to pass the typedesc vars for inner types as arguments if they are defined before the parent typedesc var --- .../optimizer/LargeMethodOptimizer.java | 5 ++-- .../compiler/desugar/Desugar.java | 25 ++++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index 13930579b766..e1bade44c9cf 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -1242,7 +1242,7 @@ private List getPossibleSplits(List basicBlocks, List MAX_SPLIT_FUNCTION_ARG_COUNT) || (splitInsCount < SPLIT_INSTRUCTION_COUNT_THRESHOLD)) { splitStarted = false; @@ -1288,7 +1288,8 @@ private List getPossibleSplits(List basicBlocks, List rewrite(attachment, env)); result = typeDef; } @@ -1298,6 +1296,7 @@ private BLangExpression createUserDefinedInitInvocation(Location location, @Override public void visit(BLangRecordTypeNode recordTypeNode) { + createTypedescVariableForAnonType(recordTypeNode); recordTypeNode.fields.addAll(recordTypeNode.includedFields); BRecordTypeSymbol recordTypeSymbol = (BRecordTypeSymbol) recordTypeNode.getBType().tsymbol; @@ -1314,7 +1313,6 @@ public void visit(BLangRecordTypeNode recordTypeNode) { } recordTypeNode.restFieldType = rewrite(recordTypeNode.restFieldType, env); - createTypedescVariableForAnonType(recordTypeNode); if (recordTypeNode.isAnonymous && recordTypeNode.isLocal) { BLangUserDefinedType userDefinedType = desugarLocalAnonRecordTypeNode(recordTypeNode); TypeDefBuilderHelper.createTypeDefinitionForTSymbol(recordTypeNode.getBType(), @@ -1339,10 +1337,10 @@ public void visit(BLangArrayType arrayType) { @Override public void visit(BLangConstrainedType constrainedType) { - constrainedType.constraint = rewrite(constrainedType.constraint, env); if (constrainedType.getBType() != null && constrainedType.getBType().tag == TypeTags.MAP) { createTypedescVariableForAnonType(constrainedType); } + constrainedType.constraint = rewrite(constrainedType.constraint, env); result = constrainedType; } @@ -1386,17 +1384,16 @@ public void visit(BLangUnionTypeNode unionTypeNode) { @Override public void visit(BLangIntersectionTypeNode intersectionTypeNode) { - List rewrittenConstituents = new ArrayList<>(); - - for (BLangType constituentTypeNode : intersectionTypeNode.constituentTypeNodes) { - rewrittenConstituents.add(rewrite(constituentTypeNode, env)); - } - int tag = Types.getImpliedType(intersectionTypeNode.getBType()).tag; if (tag == TypeTags.RECORD || tag == TypeTags.TUPLE || tag == TypeTags.MAP) { createTypedescVariableForAnonType(intersectionTypeNode); } + List rewrittenConstituents = new ArrayList<>(); + for (BLangType constituentTypeNode : intersectionTypeNode.constituentTypeNodes) { + rewrittenConstituents.add(rewrite(constituentTypeNode, env)); + } + intersectionTypeNode.constituentTypeNodes = rewrittenConstituents; result = intersectionTypeNode; } @@ -1446,9 +1443,9 @@ public void visit(BLangBuiltInRefTypeNode refTypeNode) { @Override public void visit(BLangTupleTypeNode tupleTypeNode) { + createTypedescVariableForAnonType(tupleTypeNode); tupleTypeNode.members.forEach(member -> member.typeNode = rewrite(member.typeNode, env)); tupleTypeNode.restParamType = rewrite(tupleTypeNode.restParamType, env); - createTypedescVariableForAnonType(tupleTypeNode); result = tupleTypeNode; } From f843a8868bed88ee50487c73ff448e0618aac410 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 5 Nov 2024 14:11:55 +0530 Subject: [PATCH 59/87] Fix LargeMethodOptimizer after typedesc change LargeMethodOptimizer depends on the instruction order for new array and new structure. After generating single typedesc for all the occurences of the type and the instruction order is changed. --- .../ballerinalang/compiler/bir/BIRGen.java | 76 +++----- .../optimizer/LargeMethodOptimizer.java | 24 ++- .../test-src/bir/bir-dump/failLockWithinLock | 22 +-- .../test-src/bir/bir-dump/failWithinOnFail | 20 +- .../resources/test-src/bir/bir-dump/mapInits | 178 +++++++++--------- 5 files changed, 154 insertions(+), 166 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 4f44a109199d..09e135c5fe0a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -1633,9 +1633,9 @@ public void visit(BLangMapLiteral astMapLiteralExpr) { VarKind.TEMP); this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); - - setScopeAndEmit(createNewStructureInst(generateMappingConstructorEntries(astMapLiteralExpr.fields), toVarRef, - type, astMapLiteralExpr.pos)); + BIROperand typeDesc = new BIROperand(getTypedescVariable(type, astMapLiteralExpr.pos)); + setScopeAndEmit(createNewStructureInst(typeDesc, generateMappingConstructorEntries(astMapLiteralExpr.fields), + toVarRef, astMapLiteralExpr.pos)); this.env.targetOperand = toVarRef; this.env.isInArrayOrStructure--; } @@ -1665,9 +1665,10 @@ public void visit(BLangStructLiteral astStructLiteralExpr) { this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); + BIROperand typeDesc = new BIROperand(getTypedescVariable(type, astStructLiteralExpr.pos)); List fields = generateMappingConstructorEntries(astStructLiteralExpr.fields); - setScopeAndEmit(createNewStructureInst(fields, toVarRef, type, astStructLiteralExpr.pos)); + setScopeAndEmit(createNewStructureInst(typeDesc, fields, toVarRef, astStructLiteralExpr.pos)); this.env.targetOperand = toVarRef; this.env.isInArrayOrStructure--; } @@ -2014,7 +2015,9 @@ public void visit(BLangWaitForAllExpr.BLangWaitLiteral waitLiteral) { this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); Location pos = waitLiteral.pos; - setScopeAndEmit(createNewStructureInst(new ArrayList<>(), toVarRef, type, pos)); + + BIROperand typeDesc = new BIROperand(getTypedescVariable(type, pos)); + setScopeAndEmit(createNewStructureInst(typeDesc, new ArrayList<>(), toVarRef, pos)); this.env.targetOperand = toVarRef; List keys = new ArrayList<>(); @@ -2271,12 +2274,7 @@ public void visit(BLangTableConstructorExpr tableConstructorExpr) { public void visit(BLangSimpleVarRef.BLangTypeLoad typeLoad) { BType type = typeLoad.symbol.tag == SymTag.TYPE_DEF ? ((BTypeDefinitionSymbol) typeLoad.symbol).referenceType : typeLoad.symbol.type; - BIRVariableDcl typedescVar = getTypedescVariable(type); - if (typedescVar != null) { - env.targetOperand = new BIROperand(typedescVar); - } else { - createNewTypedescInst(type, typeLoad.pos); - } + env.targetOperand = new BIROperand(getTypedescVariable(type, typeLoad.pos)); } @Override @@ -2395,23 +2393,17 @@ public void visit(BLangRegExpTemplateLiteral regExpTemplateLiteral) { this.env.targetOperand = toVarRef; } - private BIRNonTerminator.NewStructure createNewStructureInst(List fields, - BIROperand toVarRef, BType type, Location pos) { - BIRVariableDcl typedescVar = getTypedescVariable(type); - if (typedescVar != null) { - return new BIRNonTerminator.NewStructure(pos, toVarRef, new BIROperand(typedescVar), fields); - } else { - // TODO: check whether we can remove this else block - createNewTypedescInst(type, pos); - return new BIRNonTerminator.NewStructure(pos, toVarRef, this.env.targetOperand, fields); - } + private BIRNonTerminator.NewStructure createNewStructureInst(BIROperand typeDesc, + List fields, + BIROperand toVarRef, Location pos) { + return new BIRNonTerminator.NewStructure(pos, toVarRef, typeDesc, fields); } public String getTypedescFieldName(String name) { return "$typedesc$" + name; } - private BIRVariableDcl getTypedescVariable(BType type) { + private BIRVariableDcl getTypedescVariable(BType type, Location pos) { Supplier[] checks = new Supplier[] { () -> findInPackageScope(type), () -> findInLocalSymbolVarMap(type, env.symbolVarMap, false), @@ -2428,7 +2420,9 @@ private BIRVariableDcl getTypedescVariable(BType type) { } } - return null; + // TODO: we need to remove typedesc creating completely from here and handle it in the Desugar phase + createNewTypedescInst(type, pos); + return this.env.targetOperand.variableDcl; } private BIRVariableDcl findInPackageScope(BType type) { @@ -2532,20 +2526,11 @@ private BIRNonTerminator.NewTypeDesc createNewTypeDesc(Location position, BIROpe } } - private BIRNonTerminator.NewArray createNewArrayInst(List initialValues, + private BIRNonTerminator.NewArray createNewArrayInst(BIROperand typeDesc, + List initialValues, BType listConstructorExprType, BIROperand sizeOp, - BIROperand toVarRef, BType referredType, Location pos) { - BIRVariableDcl typedescVar = getTypedescVariable(listConstructorExprType); - if (typedescVar != null) { - return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef, - new BIROperand(typedescVar), sizeOp, initialValues); - - } else { - // TODO: check whether we can remove this else block - createNewTypedescInst(referredType, pos); - return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef, this.env.targetOperand, sizeOp, - initialValues); - } + BIROperand toVarRef, Location pos) { + return new BIRNonTerminator.NewArray(pos, listConstructorExprType, toVarRef, typeDesc, sizeOp, initialValues); } @Override @@ -2822,16 +2807,16 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo this.env.enclFunc.localVars.add(tempVarDcl); BIROperand toVarRef = new BIROperand(tempVarDcl); + BType listConstructorExprType = listConstructorExpr.getBType(); + BIROperand typeDesc = new BIROperand(getTypedescVariable(listConstructorExprType, listConstructorExpr.pos)); + long size = -1L; - BIROperand typedescOp = null; List exprs = listConstructorExpr.exprs; - BType listConstructorExprType = listConstructorExpr.getBType(); BType referredType = Types.getImpliedType(listConstructorExprType); if (referredType.tag == TypeTags.ARRAY && ((BArrayType) referredType).state != BArrayState.OPEN) { size = ((BArrayType) referredType).size; } else if (referredType.tag == TypeTags.TUPLE) { - typedescOp = this.env.targetOperand; size = exprs.size(); } @@ -2856,7 +2841,7 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo } if (referredType.tag == TypeTags.TUPLE) { - setScopeAndEmit(createNewArrayInst(initialValues, listConstructorExprType, sizeOp, toVarRef, referredType, + setScopeAndEmit(createNewArrayInst(typeDesc, initialValues, listConstructorExprType, sizeOp, toVarRef, listConstructorExpr.pos)); } else { @@ -2864,15 +2849,10 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo listConstructorExprType, toVarRef, sizeOp, initialValues); BType elementType = ((BArrayType) referredType).getElementType(); // If the referredType is an array type and the element type is record type, then we need to set - // the element type desc which will be used to initialize the `ArrayValueImpl` + // the element typeDesc which will be used to initialize the `ArrayValueImpl` if (Types.getImpliedType(elementType).tag == TypeTags.RECORD) { - BIRVariableDcl typedescVar = getTypedescVariable(elementType); - if (typedescVar != null) { - newArrayIns.elementTypedescOp = new BIROperand(typedescVar); - } else { - createNewTypedescInst(elementType, elementType.tsymbol.pos); - newArrayIns.elementTypedescOp = this.env.targetOperand; - } + BIRVariableDcl typedescVar = getTypedescVariable(elementType, elementType.tsymbol.pos); + newArrayIns.elementTypedescOp = new BIROperand(typedescVar); } setScopeAndEmit(newArrayIns); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index e1bade44c9cf..e0e51a6f8f2b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -190,10 +190,6 @@ private void periodicSplitMap(BIRFunction parentFunc, List newlyAdd handleArrayOperand, birOperands, mapValuesOperands, globalAndArgVarKeyOrValueLhsOperands, mapKeyOperandLocations); - // add the constant load array size operand instruction - parentFuncEnv.parentFuncNewInsList.add(bbs.get(0).instructions.get(0)); - parentFuncEnv.parentFuncLocalVarList.add(bbs.get(0).instructions.get(0).lhsOp.variableDcl); - splitParentFuncForPeriodicMapSplits(parentFunc, newlyAddingFunctions, fromAttachedFunction, bbs, newMapInsBBNum, newMapInsNumInRelevantBB, handleArray, handleArrayOperand, birOperands, splitFuncEnv, parentFuncEnv, mapValuesOperands, @@ -603,9 +599,7 @@ private void splitParentFuncForPeriodicMapSplits(BIRFunction parentFunc, List getPossibleSplits(List basicBlocks, List (int, string) { %16(TEMP) string; %18(TEMP) (); %19(TEMP) map; - %20(TEMP) string; + %20(TEMP) typeDesc; %21(TEMP) string; - %22(TEMP) typeDesc; + %22(TEMP) string; %23(TEMP) map; %24(LOCAL) error; - %40(TEMP) int; %41(TEMP) int; + %42(TEMP) int; bb0 { lock -> bb1; @@ -50,10 +50,10 @@ failLockWithinLock function() -> (int, string) { %16 = ConstLoad custom error; %18 = ConstLoad 0; %12 = %18; - %20 = ConstLoad message; - %21 = ConstLoad error value; - %22 = newType map; - %19 = NewMap %22{%20:%21}; + %20 = newType map; + %21 = ConstLoad message; + %22 = ConstLoad error value; + %19 = NewMap %20{%21:%22}; %23 = cloneReadOnly(%19) -> bb8; } bb8 { @@ -101,10 +101,10 @@ failLockWithinLock function() -> (int, string) { panic %12; } bb19 { - %40 = ConstLoad 2; - %41 = lockWithinLockInt; - %22 = newType (int, string); - %0 = newArray %22[%40]{%41,lockWithinLockString}; + %20 = newType (int, string); + %41 = ConstLoad 2; + %42 = lockWithinLockInt; + %0 = newArray %20[%41]{%42,lockWithinLockString}; GOTO bb20; } bb20 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail index 8ea9cc934c9d..378fae3b3de0 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/failWithinOnFail @@ -9,9 +9,9 @@ failWithinOnFail function() -> string|error { %17(TEMP) error; %18(TEMP) (); %19(TEMP) map; - %20(TEMP) string; + %20(TEMP) typeDesc; %21(TEMP) string; - %22(TEMP) typeDesc; + %22(TEMP) string; %23(TEMP) map; bb0 { @@ -32,22 +32,22 @@ failWithinOnFail function() -> string|error { %9 = ConstLoad custom error; %18 = ConstLoad 0; %17 = %18; - %20 = ConstLoad message; - %21 = ConstLoad error value; - %22 = newType map; - %19 = NewMap %22{%20:%21}; + %20 = newType map; + %21 = ConstLoad message; + %22 = ConstLoad error value; + %19 = NewMap %20{%21:%22}; %23 = cloneReadOnly(%19) -> bb3; } bb3 { %14 = error error(%9, %17, %23); - %20 = ConstLoad -> Error caught in inner on fail; - %3 = %3 + %20; + %21 = ConstLoad -> Error caught in inner on fail; + %3 = %3 + %21; %0 = %14; GOTO bb6; } bb4 { - %21 = ConstLoad -> After do statement; - %3 = %3 + %21; + %22 = ConstLoad -> After do statement; + %3 = %3 + %22; GOTO bb1; } bb5 { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits index 9c3639e8d4b1..2708c861c1bc 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits +++ b/tests/jballerina-unit-test/src/test/resources/test-src/bir/bir-dump/mapInits @@ -10,32 +10,32 @@ mapInits function() -> (string|(), int|()) { %11(TEMP) string; %12(TEMP) string; %13(TEMP) Employee; - %19(SYNTHETIC) string|(); - %20(SYNTHETIC) Employee|(); - %24(SYNTHETIC) Employee|(); - %26(SYNTHETIC) boolean; + %18(TEMP) typeDesc; + %20(SYNTHETIC) string|(); + %21(SYNTHETIC) Employee|(); + %25(SYNTHETIC) Employee|(); %27(SYNTHETIC) boolean; - %28(SYNTHETIC) any|error; - %29(TEMP) boolean; - %41(SYNTHETIC) boolean; + %28(SYNTHETIC) boolean; + %29(SYNTHETIC) any|error; + %30(TEMP) boolean; %42(SYNTHETIC) boolean; - %43(SYNTHETIC) any|error; - %59(SYNTHETIC) boolean; + %43(SYNTHETIC) boolean; + %44(SYNTHETIC) any|error; %60(SYNTHETIC) boolean; - %70(TEMP) (); - %72(SYNTHETIC) int|(); - %73(SYNTHETIC) Employee|(); - %77(SYNTHETIC) Employee|(); - %79(SYNTHETIC) boolean; + %61(SYNTHETIC) boolean; + %71(TEMP) (); + %73(SYNTHETIC) int|(); + %74(SYNTHETIC) Employee|(); + %78(SYNTHETIC) Employee|(); %80(SYNTHETIC) boolean; - %81(SYNTHETIC) any|error; - %94(SYNTHETIC) boolean; + %81(SYNTHETIC) boolean; + %82(SYNTHETIC) any|error; %95(SYNTHETIC) boolean; - %96(SYNTHETIC) any|error; - %108(TEMP) int; - %112(SYNTHETIC) boolean; + %96(SYNTHETIC) boolean; + %97(SYNTHETIC) any|error; + %109(TEMP) int; %113(SYNTHETIC) boolean; - %125(TEMP) typeDesc; + %114(SYNTHETIC) boolean; bb0 { %1 = newType map; @@ -50,207 +50,207 @@ mapInits function() -> (string|(), int|()) { %13 = %5; %7 = ConstLoad jack; %3[%7] = %13; + %18 = newType (string|(), int|()); %10 = ConstLoad 2; %8 = ConstLoad jack; - %20 = %3[%8]; - %24 = %20; - %29 = ConstLoad true; - %29? bb1 : bb2; + %21 = %3[%8]; + %25 = %21; + %30 = ConstLoad true; + %30? bb1 : bb2; } bb1 { - %27 = ConstLoad true; - %28 = %24; + %28 = ConstLoad true; + %29 = %25; GOTO bb3; } bb2 { - %27 = ConstLoad false; + %28 = ConstLoad false; GOTO bb3; } bb3 { - %27? bb4 : bb5; + %28? bb4 : bb5; } bb4 { - %26 = %28 is (); + %27 = %29 is (); GOTO bb6; } bb5 { - %26 = ConstLoad false; + %27 = ConstLoad false; GOTO bb6; } bb6 { - %26? bb7 : bb8; + %27? bb7 : bb8; } bb7 { - %19 = %28; + %20 = %29; GOTO bb24; } bb8 { - %29 = ConstLoad true; - %29? bb9 : bb10; + %30 = ConstLoad true; + %30? bb9 : bb10; } bb9 { - %42 = ConstLoad true; - %43 = %24; + %43 = ConstLoad true; + %44 = %25; GOTO bb11; } bb10 { - %42 = ConstLoad false; + %43 = ConstLoad false; GOTO bb11; } bb11 { - %42? bb12 : bb13; + %43? bb12 : bb13; } bb12 { - %41 = %43 is Employee; + %42 = %44 is Employee; GOTO bb14; } bb13 { - %41 = ConstLoad false; + %42 = ConstLoad false; GOTO bb14; } bb14 { - %41? bb15 : bb16; + %42? bb15 : bb16; } bb15 { - %13 = %43; + %13 = %44; %9 = ConstLoad name; %11 = %13[%9]; - %19 = %11; + %20 = %11; GOTO bb24; } bb16 { - %29 = ConstLoad true; - %29? bb17 : bb18; + %30 = ConstLoad true; + %30? bb17 : bb18; } bb17 { - %60 = ConstLoad true; + %61 = ConstLoad true; GOTO bb19; } bb18 { - %60 = %24 is any; + %61 = %25 is any; GOTO bb19; } bb19 { - %60? bb20 : bb21; + %61? bb20 : bb21; } bb20 { - %59 = ConstLoad true; + %60 = ConstLoad true; GOTO bb22; } bb21 { - %59 = ConstLoad false; + %60 = ConstLoad false; GOTO bb22; } bb22 { - %59? bb23 : bb24; + %60? bb23 : bb24; } bb23 { - %70 = ConstLoad 0; - %19 = %70; + %71 = ConstLoad 0; + %20 = %71; GOTO bb24; } bb24 { %12 = ConstLoad jack; - %73 = %3[%12]; - %77 = %73; - %29 = ConstLoad true; - %29? bb25 : bb26; + %74 = %3[%12]; + %78 = %74; + %30 = ConstLoad true; + %30? bb25 : bb26; } bb25 { - %80 = ConstLoad true; - %81 = %77; + %81 = ConstLoad true; + %82 = %78; GOTO bb27; } bb26 { - %80 = ConstLoad false; + %81 = ConstLoad false; GOTO bb27; } bb27 { - %80? bb28 : bb29; + %81? bb28 : bb29; } bb28 { - %79 = %81 is (); + %80 = %82 is (); GOTO bb30; } bb29 { - %79 = ConstLoad false; + %80 = ConstLoad false; GOTO bb30; } bb30 { - %79? bb31 : bb32; + %80? bb31 : bb32; } bb31 { - %72 = %81; + %73 = %82; GOTO bb48; } bb32 { - %29 = ConstLoad true; - %29? bb33 : bb34; + %30 = ConstLoad true; + %30? bb33 : bb34; } bb33 { - %95 = ConstLoad true; - %96 = %77; + %96 = ConstLoad true; + %97 = %78; GOTO bb35; } bb34 { - %95 = ConstLoad false; + %96 = ConstLoad false; GOTO bb35; } bb35 { - %95? bb36 : bb37; + %96? bb36 : bb37; } bb36 { - %94 = %96 is Employee; + %95 = %97 is Employee; GOTO bb38; } bb37 { - %94 = ConstLoad false; + %95 = ConstLoad false; GOTO bb38; } bb38 { - %94? bb39 : bb40; + %95? bb39 : bb40; } bb39 { - %13 = %96; + %13 = %97; %7 = ConstLoad age; - %108 = %13[%7]; - %72 = %108; + %109 = %13[%7]; + %73 = %109; GOTO bb48; } bb40 { - %29 = ConstLoad true; - %29? bb41 : bb42; + %30 = ConstLoad true; + %30? bb41 : bb42; } bb41 { - %113 = ConstLoad true; + %114 = ConstLoad true; GOTO bb43; } bb42 { - %113 = %77 is any; + %114 = %78 is any; GOTO bb43; } bb43 { - %113? bb44 : bb45; + %114? bb44 : bb45; } bb44 { - %112 = ConstLoad true; + %113 = ConstLoad true; GOTO bb46; } bb45 { - %112 = ConstLoad false; + %113 = ConstLoad false; GOTO bb46; } bb46 { - %112? bb47 : bb48; + %113? bb47 : bb48; } bb47 { - %70 = ConstLoad 0; - %72 = %70; + %71 = ConstLoad 0; + %73 = %71; GOTO bb48; } bb48 { - %125 = newType (string|(), int|()); - %0 = newArray %125[%10]{%19,%72}; + %0 = newArray %18[%10]{%20,%73}; GOTO bb49; } bb49 { From 79cb8057a16010be960bb332373e24bec64615c0 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 12 Nov 2024 10:50:40 +0530 Subject: [PATCH 60/87] Add missing visitor method updates for new `elementTypedescOp` --- .../ballerinalang/compiler/bir/emit/InstructionEmitter.java | 3 +++ .../ballerinalang/compiler/bir/optimizer/BIROptimizer.java | 1 + .../compiler/bir/writer/BIRInstructionWriter.java | 6 ++++++ .../ballerinalang/programfile/ProgramFileConstants.java | 6 +++--- docs/bir-spec/src/main/resources/kaitai/bir.ksy | 5 +++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/emit/InstructionEmitter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/emit/InstructionEmitter.java index 66aa82d80495..f15666c150a5 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/emit/InstructionEmitter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/emit/InstructionEmitter.java @@ -245,6 +245,9 @@ private static String emitInsNewArray(BIRNonTerminator.NewArray ins, int tabs) { str += "["; str += emitVarRef(ins.sizeOp); str += "]"; + if (ins.elementTypedescOp != null) { + str += " elementTypeDesc " + emitVarRef(ins.elementTypedescOp); + } str += "{"; str += emitArrayValues(ins.values); str += "}"; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java index 61a755adf31f..c50de97dd88d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java @@ -562,6 +562,7 @@ public void visit(BIRNonTerminator.NewArray birNewArray) { if (birNewArray.typedescOp != null) { birNewArray.typedescOp.accept(this); } + this.optimizeNode(birNewArray.elementTypedescOp, this.env); } @Override diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRInstructionWriter.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRInstructionWriter.java index 856fae393e8c..c0c14dc40dc1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRInstructionWriter.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/writer/BIRInstructionWriter.java @@ -443,6 +443,12 @@ public void visit(NewArray birNewArray) { } else { buf.writeByte(0); } + if (birNewArray.elementTypedescOp != null) { + buf.writeByte(1); + birNewArray.elementTypedescOp.accept(this); + } else { + buf.writeByte(0); + } birNewArray.sizeOp.accept(this); buf.writeInt(birNewArray.values.size()); for (BIRNode.BIRListConstructorEntry listValueEntry : birNewArray.values) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/ProgramFileConstants.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/ProgramFileConstants.java index deb7028985c4..8299af914562 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/ProgramFileConstants.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/programfile/ProgramFileConstants.java @@ -25,9 +25,9 @@ public final class ProgramFileConstants { public static final int MAGIC_NUMBER = 0xBA1DA4CE; public static final short VERSION_NUMBER = 50; - public static final int BIR_VERSION_NUMBER = 72; - public static final short MIN_SUPPORTED_VERSION = 72; - public static final short MAX_SUPPORTED_VERSION = 72; + public static final int BIR_VERSION_NUMBER = 73; + public static final short MIN_SUPPORTED_VERSION = 73; + public static final short MAX_SUPPORTED_VERSION = 73; // todo move this to a proper place public static final String[] SUPPORTED_PLATFORMS = {"java17", "java11"}; diff --git a/docs/bir-spec/src/main/resources/kaitai/bir.ksy b/docs/bir-spec/src/main/resources/kaitai/bir.ksy index 8ee90dd30058..d11e3ab290a5 100644 --- a/docs/bir-spec/src/main/resources/kaitai/bir.ksy +++ b/docs/bir-spec/src/main/resources/kaitai/bir.ksy @@ -1444,6 +1444,11 @@ types: - id: typedesc_operand type: operand if: has_typedesc_operand == 1 + - id: has_element_typedesc_operand + type: s1 + - id: element_typedesc_operand + type: operand + if: has_element_typedesc_operand == 1 - id: size_operand type: operand - id: init_values_count From 6611e114b8cd451e4c2d98e3ee8611b854962508 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 12 Nov 2024 12:24:37 +0530 Subject: [PATCH 61/87] Update `getRhsOperands` of `NEWARRAY` instruction --- .../compiler/bir/model/BIRNonTerminator.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java index f11b13b59c0c..4b2fa4987d49 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java @@ -26,6 +26,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; import static org.wso2.ballerinalang.compiler.bir.model.InstructionKind.RECORD_DEFAULT_FP_LOAD; @@ -344,19 +345,15 @@ public void accept(BIRVisitor visitor) { @Override public BIROperand[] getRhsOperands() { - int i = 0; - BIROperand[] operands; - if (typedescOp != null) { - operands = new BIROperand[values.size() + 2]; - operands[i++] = typedescOp; - } else { - operands = new BIROperand[values.size() + 1]; - } - operands[i++] = sizeOp; + List operands = new ArrayList<>(); + Optional.ofNullable(typedescOp).ifPresent(operands::add); + Optional.ofNullable(elementTypedescOp).ifPresent(operands::add); + operands.add(sizeOp); for (BIRListConstructorEntry listValueEntry : values) { - operands[i++] = listValueEntry.exprOp; + operands.add(listValueEntry.exprOp); } - return operands; + + return operands.toArray(new BIROperand[0]); } @Override From 810bcc660b1f1e554d2dde7dc3388a96b202acaf Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 15 Nov 2024 16:48:10 +0530 Subject: [PATCH 62/87] Compress arguments into a tuple if the split method contains large number of args --- .../ballerinalang/compiler/bir/BIRGen.java | 21 +- .../optimizer/LargeMethodOptimizer.java | 258 ++++++++++++++++-- .../compiler/bir/optimizer/BIROptimizer.java | 3 + 3 files changed, 252 insertions(+), 30 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 09e135c5fe0a..89efc5d10cde 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2808,11 +2808,22 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo BIROperand toVarRef = new BIROperand(tempVarDcl); BType listConstructorExprType = listConstructorExpr.getBType(); - BIROperand typeDesc = new BIROperand(getTypedescVariable(listConstructorExprType, listConstructorExpr.pos)); - long size = -1L; List exprs = listConstructorExpr.exprs; BType referredType = Types.getImpliedType(listConstructorExprType); + BIROperand typeDesc = null; + boolean isRecordArray = false; + if (referredType.tag == TypeTags.TUPLE) { + typeDesc = new BIROperand(getTypedescVariable(listConstructorExprType, listConstructorExpr.pos)); + } else { + BType elementType = ((BArrayType) referredType).getElementType(); + if (Types.getImpliedType(elementType).tag == TypeTags.RECORD) { + typeDesc = new BIROperand(getTypedescVariable(elementType, elementType.tsymbol.pos)); + isRecordArray = true; + } + } + + long size = -1L; if (referredType.tag == TypeTags.ARRAY && ((BArrayType) referredType).state != BArrayState.OPEN) { size = ((BArrayType) referredType).size; @@ -2847,12 +2858,10 @@ private void generateListConstructorExpr(BLangListConstructorExpr listConstructo } else { BIRNonTerminator.NewArray newArrayIns = new BIRNonTerminator.NewArray(listConstructorExpr.pos, listConstructorExprType, toVarRef, sizeOp, initialValues); - BType elementType = ((BArrayType) referredType).getElementType(); // If the referredType is an array type and the element type is record type, then we need to set // the element typeDesc which will be used to initialize the `ArrayValueImpl` - if (Types.getImpliedType(elementType).tag == TypeTags.RECORD) { - BIRVariableDcl typedescVar = getTypedescVariable(elementType, elementType.tsymbol.pos); - newArrayIns.elementTypedescOp = new BIROperand(typedescVar); + if (isRecordArray) { + newArrayIns.elementTypedescOp = typeDesc; } setScopeAndEmit(newArrayIns); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index e0e51a6f8f2b..eebce32aae72 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -18,6 +18,7 @@ package org.wso2.ballerinalang.compiler.bir.codegen.optimizer; +import org.ballerinalang.model.elements.Flag; import org.ballerinalang.model.elements.PackageID; import org.ballerinalang.model.symbols.SymbolOrigin; import org.wso2.ballerinalang.compiler.bir.BIRGenUtils; @@ -43,21 +44,32 @@ import org.wso2.ballerinalang.compiler.bir.model.VarKind; import org.wso2.ballerinalang.compiler.bir.model.VarScope; import org.wso2.ballerinalang.compiler.semantics.model.SymbolTable; +import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; +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.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleMember; +import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType; 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.util.Flags; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; +import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL; import static org.objectweb.asm.Opcodes.INVOKESTATIC; import static org.wso2.ballerinalang.compiler.bir.BIRGenUtils.rearrangeBasicBlocks; import static org.wso2.ballerinalang.compiler.bir.codegen.JvmConstants.LARGE_STRUCTURE_UTILS; @@ -75,6 +87,10 @@ public class LargeMethodOptimizer { private static final Name DEFAULT_WORKER_NAME = new Name("function"); private static final String OBJECT_INITIALIZATION_FUNCTION_NAME = "$init$"; private static final String SPLIT_METHOD = "$split$method$_"; + private static final String ARG_INDEX = "%argIndex"; + private static final String ARG_TUPLE_SIZE = "%argTupleSize"; + private static final String ARG_TUPLE_TYPEDESC = "%argTupleTypeDesc_"; + private static final String ARG_TUPLE = "%argTuple_"; private final SymbolTable symbolTable; // splits are done only if the original function has more instructions than the below number private static final int FUNCTION_INSTRUCTION_COUNT_THRESHOLD = 1000; @@ -90,6 +106,10 @@ public class LargeMethodOptimizer { private int splitFuncNum; // newly created temporary variable number to handle split function return value private int splitTempVarNum; + // next argument tuple and tuple typedesc indexes used to compress arguments when the split function arg + // count exceeds `MAX_SPLIT_FUNCTION_ARG_COUNT` + private int argTupleTypeDescIndex; + private int argTupleIndex; public LargeMethodOptimizer(SymbolTable symbolTable) { this.symbolTable = symbolTable; @@ -99,6 +119,8 @@ public void splitLargeBIRFunctions(BIRPackage birPkg) { currentPackageId = birPkg.packageID; splitFuncNum = 0; splitTempVarNum = 0; + argTupleTypeDescIndex = 0; + argTupleIndex = 0; List newlyAddedBIRFunctions = new ArrayList<>(); for (BIRFunction function : birPkg.functions) { @@ -171,6 +193,13 @@ private void periodicSplitMap(BIRFunction parentFunc, List newlyAdd ParentFuncEnv parentFuncEnv = new ParentFuncEnv(bbs.get(newMapInsBBNum + 1)); SplitFuncEnv splitFuncEnv = new SplitFuncEnv(getTempVarsForArraySplit(), fromAttachedFunction); + // add large arg restore instructions + int instIndex = skipLargeArgRestoreInstsAndGetIndex(bbs.get(0)); + for (BIRNonTerminator ins : bbs.get(0).instructions.subList(0, instIndex)) { + parentFuncEnv.parentFuncNewInsList.add(ins); + parentFuncEnv.parentFuncLocalVarList.add(ins.lhsOp.variableDcl); + } + parentFuncEnv.returnOperand = mapIns.lhsOp; BIRVariableDcl handleArray = new BIRVariableDcl(null, symbolTable.handleType, new Name("%mapEntryArray"), VarScope.FUNCTION, VarKind.TEMP, null); @@ -238,6 +267,13 @@ private void periodicSplitArray(BIRFunction parentFunc, List newlyA ParentFuncEnv parentFuncEnv = new ParentFuncEnv(bbs.get(newArrayInsBBNum + 1)); SplitFuncEnv splitFuncEnv = new SplitFuncEnv(getTempVarsForArraySplit(), fromAttachedFunction); + // add large arg restore and constant load array size operand instruction + int instIndex = skipLargeArgRestoreInstsAndGetIndex(bbs.get(0)); + for (BIRNonTerminator ins : bbs.get(0).instructions.subList(0, instIndex + 1)) { + parentFuncEnv.parentFuncNewInsList.add(ins); + parentFuncEnv.parentFuncLocalVarList.add(ins.lhsOp.variableDcl); + } + parentFuncEnv.returnOperand = arrayIns.lhsOp; BIRVariableDcl handleArray = new BIRVariableDcl(null, symbolTable.handleType, new Name("%listEntryArray"), VarScope.FUNCTION, VarKind.TEMP, null); @@ -254,10 +290,6 @@ private void periodicSplitArray(BIRFunction parentFunc, List newlyA List globalAndArgVarIns = getGlobalAndArgVarInsForArray(parentFuncTempVars, arrayIns, handleArrayOperand, birOperands, arrayValuesOperands); - // add the constant load array size operand instruction - parentFuncEnv.parentFuncNewInsList.add(bbs.get(0).instructions.get(0)); - parentFuncEnv.parentFuncLocalVarList.add(bbs.get(0).instructions.get(0).lhsOp.variableDcl); - splitParentFuncForPeriodicArraySplits(parentFunc, newlyAddingFunctions, fromAttachedFunction, bbs, newArrayInsBBNum, newArrayInsNumInRelevantBB, handleArray, handleArrayOperand, birOperands, splitFuncEnv, parentFuncEnv, arrayValuesOperands); @@ -377,6 +409,83 @@ private void createAndAddNewHandleArrayForLargeMapIns(ParentFuncEnv parentFuncEn parentFuncEnv.parentFuncNewInsList.add(callHandleArray); } + /** + * Create a tuple from the arguments after {@link #MAX_SPLIT_FUNCTION_ARG_COUNT} and replace those arguments + * by the tuple. This is to prevent `to many arguments` error. + * + * eg: + * if {@link #MAX_SPLIT_FUNCTION_ARG_COUNT} = 5 + * before + * %23 = $split$method$_1(%9, %11, %13, %15, %21, %19, %17, %2, %1) -> bb2; + * after + * %argTupleTypeDesc_0 = newType (typeDesc<(int, int, int)>, typeDesc<(int, int, int)>, int, int); + * %argTupleSize = ConstLoad 4; + * %argTuple_0 = newArray %argTupleTypeDesc_0[%argTupleSize]{%19,%17,%2,%1}; + * %23 = $split$method$_1(%9, %11, %13, %15, %21, %argTuple_0) -> bb2; + * + * @param parentFuncLocalVarList local variables of the parent function + * @param splitArgs arguments needs to be passed to the split function + * @param parentFuncNewInsList parent function instructions + * @return newly compressed argument names to the argument tuple + */ + private List createArgTupleAndGetArgNameList(List parentFuncLocalVarList, + List splitArgs, + List parentFuncNewInsList) { + BTypeSymbol tupleTypeSymbol = Symbols.createTypeSymbol(SymTag.TUPLE_TYPE, Flags.asMask(EnumSet.of(Flag.PUBLIC)), + Names.EMPTY, currentPackageId, null, null, null, VIRTUAL); + List argList = new ArrayList<>(splitArgs.subList( + MAX_SPLIT_FUNCTION_ARG_COUNT, splitArgs.size())); + int argSize = argList.size(); + List memberTypes = new ArrayList<>(argSize); + BTupleType tupleType = new BTupleType(tupleTypeSymbol, memberTypes); + tupleTypeSymbol.type = tupleType; + + List initialValues = new ArrayList<>(argSize); + List argNames = new ArrayList<>(argSize); + for (BIRVariableDcl arg : argList) { + // Add arg type as a member type of the tuple type + BVarSymbol varSymbol = new BVarSymbol(0, Names.EMPTY, + currentPackageId, arg.type, null, null, VIRTUAL); + memberTypes.add(new BTupleMember(arg.type, varSymbol)); + + // Add arg as a list constructor member + initialValues.add(new BIRNode.BIRListConstructorExprEntry(new BIROperand(arg))); + argNames.add(arg.name); + } + + // Create new type desc instruction for the tuple type + BIRVariableDcl typeDescVarDecl = new BIRVariableDcl(null, symbolTable.typeDesc, + new Name(ARG_TUPLE_TYPEDESC + argTupleTypeDescIndex++), VarScope.FUNCTION, VarKind.TEMP, null); + parentFuncLocalVarList.add(typeDescVarDecl); + BIROperand typeDescOp = new BIROperand(typeDescVarDecl); + BIRNonTerminator.NewTypeDesc newTypeDesc = new BIRNonTerminator.NewTypeDesc(null, typeDescOp, + tupleType, Collections.emptyList()); + parentFuncNewInsList.add(newTypeDesc); + + // Create a constant load instruction to load the size of the tuple + BIRVariableDcl size = new BIRVariableDcl(null, symbolTable.intType, + new Name(ARG_TUPLE_SIZE), VarScope.FUNCTION, VarKind.TEMP, null); + parentFuncLocalVarList.add(size); + BIROperand sizeOp = new BIROperand(size); + BIRNonTerminator.ConstantLoad sizeLoadInst = new BIRNonTerminator.ConstantLoad(null, + (long) argSize, symbolTable.intType, sizeOp); + parentFuncNewInsList.add(sizeLoadInst); + + // Add new array instruction to create the large argument tuple + BIRVariableDcl largeArgTuple = new BIRVariableDcl(null, tupleType, + new Name(ARG_TUPLE + argTupleIndex++), VarScope.FUNCTION, VarKind.TEMP, null); + parentFuncLocalVarList.add(largeArgTuple); + BIROperand largeArgTupleOperand = new BIROperand(largeArgTuple); + parentFuncNewInsList.add(new BIRNonTerminator.NewArray(null, tupleType, largeArgTupleOperand, + typeDescOp, sizeOp, initialValues)); + + // Replace args of the current split with the large argument tuple + splitArgs.subList(MAX_SPLIT_FUNCTION_ARG_COUNT, splitArgs.size()).clear(); + splitArgs.add(largeArgTuple); + + return argNames; + } + private void createAndAddNewHandleArrayForLargeArrayIns(ParentFuncEnv parentFuncEnv, BIRNonTerminator.NewArray arrayIns, BIRVariableDcl handleArray, BIROperand handleArrayOperand) { @@ -598,7 +707,8 @@ private void splitParentFuncForPeriodicMapSplits(BIRFunction parentFunc, List instructions = bb.instructions; + for (int i = 0; i < instructions.size(); i= i + 2) { + if (!instructions.get(i).lhsOp.variableDcl.name.value.equals(ARG_INDEX)) { + return i; + } + } + return 0; + } + private void handleBBInstructions(SplitFuncEnv splitFuncEnv, BIRBasicBlock bb) { splitFuncEnv.splitFuncBB.instructions = splitFuncEnv.splitFuncNewInsList; splitFuncEnv.splitFuncBB.terminator = bb.terminator; @@ -789,6 +916,29 @@ private void createNewFuncForPeriodicSplit(BIRFunction parentFunc, List MAX_SPLIT_FUNCTION_ARG_COUNT) { + List newFuncArgs = new ArrayList<>(splitFuncEnv.splitFuncArgs); + List parentFuncLocalVars = new ArrayList<>(parentFuncEnv.parentFuncLocalVarList); + + List argNameList = createArgTupleAndGetArgNameList( + parentFuncLocalVars, newFuncArgs, parentFuncEnv.parentFuncNewInsList); + + splitFuncEnv.splitFuncArgs = new LinkedHashSet<>(newFuncArgs); + parentFuncEnv.parentFuncLocalVarList = new LinkedHashSet<>(parentFuncLocalVars); + + List splitFuncLocalVarList = new ArrayList<>(splitFuncEnv.splitFuncLocalVarList); + List argRestoreInstructions = new ArrayList<>(); + + restoreOriginalArgsFromTuple(argNameList, newFuncArgs, splitFuncLocalVarList, argRestoreInstructions); + + splitFuncEnv.splitFuncLocalVarList = new LinkedHashSet<>(splitFuncLocalVarList); + if (splitFuncEnv.splitFuncNewBBList.size() > 0) { + splitFuncEnv.splitFuncNewBBList.get(0).instructions.addAll(0, argRestoreInstructions); + } else { + splitFuncEnv.splitFuncNewInsList.addAll(0, argRestoreInstructions); + } + } + // Create a new split BIRFunction List paramTypes = new ArrayList<>(); for (BIRVariableDcl funcArg : splitFuncEnv.splitFuncArgs) { @@ -1179,8 +1329,8 @@ private List getPossibleSplits(List basicBlocks, List neededOperandsVarDcl = new HashSet<>(); // that will need to be passed as function args - Set lhsOperandList = new HashSet<>(); // that will need as localVars in the new function + Set neededOperandsVarDcl = new LinkedHashSet<>(); // that will need to be passed as function args + Set lhsOperandList = new LinkedHashSet<>(); // that will need as localVars in the new function BIROperand splitStartOperand = null; int splitInsCount = 0; // including terminator instructions @@ -1237,8 +1387,7 @@ private List getPossibleSplits(List basicBlocks, List MAX_SPLIT_FUNCTION_ARG_COUNT) || - (splitInsCount < SPLIT_INSTRUCTION_COUNT_THRESHOLD)) { + if (splitInsCount < SPLIT_INSTRUCTION_COUNT_THRESHOLD) { splitStarted = false; continue; } @@ -1269,9 +1418,13 @@ private List getPossibleSplits(List basicBlocks, List startOfStructInst = new ArrayList<>(); if (currIns.kind == InstructionKind.NEW_ARRAY) { BIRNonTerminator.NewArray arrayIns = (BIRNonTerminator.NewArray) currIns; splitStartOperand = arrayIns.sizeOp; + Optional.ofNullable(arrayIns.typedescOp).ifPresent(startOfStructInst::add); + Optional.ofNullable(arrayIns.elementTypedescOp).ifPresent(startOfStructInst::add); splitTypeArray = true; } else { BIRNonTerminator.NewStructure structureIns = (BIRNonTerminator.NewStructure) currIns; @@ -1279,6 +1432,7 @@ private List getPossibleSplits(List basicBlocks, List getPossibleSplits(List basicBlocks, List firstBBIns = basicBlocks.get(0).instructions; + BIRNonTerminator firstIns = firstBBIns.get( + skipLargeArgRestoreInstsAndGetIndex(basicBlocks.get(0))); + if (((startOfStructInst.stream().map(op -> op.variableDcl).toList().contains( + firstIns.lhsOp.variableDcl)))) { + continue; + } } splitStarted = true; returnValAssigned = false; - neededOperandsVarDcl = new HashSet<>(); + neededOperandsVarDcl = new LinkedHashSet<>(); BIROperand[] initialRhsOperands = currIns.getRhsOperands(); for (BIROperand rhsOperand : initialRhsOperands) { if (needToPassRhsVarDclAsArg(rhsOperand)) { neededOperandsVarDcl.add(rhsOperand.variableDcl); } } - lhsOperandList = new HashSet<>(); + lhsOperandList = new LinkedHashSet<>(); splitInsCount = 1; splitEndInsIndex = insNum; splitEndBBIndex = bbNum; @@ -1459,8 +1618,11 @@ private void generateSplits(BIRFunction function, List possibleSplits, } // extra +1 for BB incremented in createNewBIRFunctionAcrossBB function, hence BB number is newBBNum + 2 BIRBasicBlock parentFuncNewBB = new BIRBasicBlock(newBBNum + 2); + boolean largeArgs = currSplit.funcArgs.size() > MAX_SPLIT_FUNCTION_ARG_COUNT; + List argNameList = largeArgs? createArgTupleAndGetArgNameList(function.localVars, + currSplit.funcArgs, currentBB.instructions) : new ArrayList<>(); BIRFunction newBIRFunc = createNewBIRFunctionAcrossBB(function, newFuncName, newFuncReturnType, currSplit, - newBBNum, fromAttachedFunction, changedErrorTableEndBB, parentFuncNewBB); + newBBNum, fromAttachedFunction, changedErrorTableEndBB, parentFuncNewBB, argNameList); newBBNum += 2; newlyAddedFunctions.add(newBIRFunc); if (currSplit.splitFurther) { @@ -1664,12 +1826,13 @@ private void setLocalVarStartEndBB(BIRFunction birFunction, List * @param fromAttachedFunction flag which indicates an original attached function is being split * @param changedErrorTableEndBB changed error table end basic block map * @param parentFuncNewBB parent function new BB + * @param argNameList if split function has large number of arguments, this will contain the arg names * @return newly created BIR function */ private BIRFunction createNewBIRFunctionAcrossBB(BIRFunction parentFunc, Name funcName, BType retType, Split currSplit, int newBBNum, boolean fromAttachedFunction, Map changedErrorTableEndBB, - BIRBasicBlock parentFuncNewBB) { + BIRBasicBlock parentFuncNewBB, List argNameList) { List parentFuncBBs = parentFunc.basicBlocks; BIRNonTerminator lastIns = parentFuncBBs.get(currSplit.endBBNum).instructions.get(currSplit.lastIns); List paramTypes = new ArrayList<>(); @@ -1710,6 +1873,7 @@ private BIRFunction createNewBIRFunctionAcrossBB(BIRFunction parentFunc, Name fu // creates BBs BIRBasicBlock entryBB = new BIRBasicBlock(0); + restoreOriginalArgsFromTuple(argNameList, currSplit.funcArgs, birFunc.localVars, entryBB.instructions); if (currSplit.firstIns < parentFuncBBs.get(currSplit.startBBNum).instructions.size()) { entryBB.instructions.addAll(parentFuncBBs.get(currSplit.startBBNum).instructions.subList( currSplit.firstIns, parentFuncBBs.get(currSplit.startBBNum).instructions.size())); @@ -1750,6 +1914,46 @@ private BIRFunction createNewBIRFunctionAcrossBB(BIRFunction parentFunc, Name fu return birFunc; } + /** + * Restore the original arguments if we compress them to single tuple due to large number of arguments. + * + * @param argNameList if split function has large number of arguments, this will contain the arg names to restore + * @param funcArgs split function args + * @param splitFuncLocalVars local var list of the newly created split function + * @param entryBBInstList instruction list of the first BB of the split function + */ + private void restoreOriginalArgsFromTuple(List argNameList, List funcArgs, + List splitFuncLocalVars, + List entryBBInstList) { + if (!argNameList.isEmpty()) { + BIRVariableDcl argTuple = funcArgs.get(funcArgs.size() - 1); + List argTupleMembers = ((BTupleType) argTuple.type).getTupleTypes(); + + // Create temp var to store the arg index which needs to be loaded from the arg tuple + BIRVariableDcl argIndex = new BIRVariableDcl(null, symbolTable.intType, new Name(ARG_INDEX), + VarScope.FUNCTION, VarKind.TEMP, null); + BIROperand argIndexOp = new BIROperand(argIndex); + splitFuncLocalVars.add(argIndex); + for (int i = 0; i < argNameList.size(); i++) { + Name argName = argNameList.get(i); + // Load the current arg index to the `argIndexOp` + BIRNonTerminator.ConstantLoad loadArgNameInst = new BIRNonTerminator.ConstantLoad(null, + (long) i, symbolTable.intType, argIndexOp); + entryBBInstList.add(loadArgNameInst); + + // Create temp var with the original name to store the actual arg + BIRVariableDcl arg = new BIRVariableDcl(null, argTupleMembers.get(i), argName, + VarScope.FUNCTION, VarKind.TEMP, argName.getValue()); + BIROperand argOp = new BIROperand(arg); + splitFuncLocalVars.add(arg); + + BIRNonTerminator.FieldAccess fieldAccess = new BIRNonTerminator.FieldAccess(null, + InstructionKind.ARRAY_LOAD, argOp, argIndexOp, new BIROperand(argTuple)); + entryBBInstList.add(fieldAccess); + } + } + } + private void fixTerminatorBBs(int lastBBIdNum, BIRBasicBlock lastBB, BIRTerminator terminator) { if (terminator.thenBB != null && terminator.thenBB.number == lastBBIdNum) { terminator.thenBB = lastBB; @@ -1800,16 +2004,21 @@ private BIRBasicBlock generateSplitsInSameBB(BIRFunction function, int bbNum, Li Name newFuncName = new Name(newFunctionName); BIROperand currentBBTerminatorLhsOp = new BIROperand(instructionList.get(possibleSplit.lastIns).lhsOp.variableDcl); + currentBB.instructions.addAll(instructionList.subList(startInsNum, possibleSplit.firstIns)); + + boolean largeArgs = possibleSplit.funcArgs.size() > MAX_SPLIT_FUNCTION_ARG_COUNT; + List argNameList = largeArgs? createArgTupleAndGetArgNameList(function.localVars, + possibleSplit.funcArgs, currentBB.instructions) : new ArrayList<>(); BIRFunction newBIRFunc = createNewBIRFuncForSplitInBB(newFuncName, instructionList.get(possibleSplit.lastIns), instructionList.subList(possibleSplit.firstIns, possibleSplit.lastIns), - possibleSplit.lhsVars, possibleSplit.funcArgs, fromAttachedFunction); + possibleSplit.lhsVars, possibleSplit.funcArgs, fromAttachedFunction, argNameList); newlyAddedFunctions.add(newBIRFunc); if (possibleSplit.splitFurther) { newlyAddedFunctions.addAll(splitBIRFunction(newBIRFunc, fromAttachedFunction, true, possibleSplit.splitTypeArray)); } - currentBB.instructions.addAll(instructionList.subList(startInsNum, possibleSplit.firstIns)); + startInsNum = possibleSplit.lastIns + 1; newBBNum += 1; BIRBasicBlock newBB = new BIRBasicBlock(newBBNum); @@ -1873,7 +2082,7 @@ private BIROperand generateTempLocalVariable(BType variableType, Set collectedIns, Set lhsOperandList, List funcArgs, - boolean fromAttachedFunction) { + boolean fromAttachedFunction, List argNameList) { BType retType = currentIns.lhsOp.variableDcl.type; List paramTypes = new ArrayList<>(); for (BIRVariableDcl funcArg : funcArgs) { @@ -1912,6 +2121,7 @@ private BIRFunction createNewBIRFuncForSplitInBB(Name funcName, BIRNonTerminator // creates 2 bbs BIRBasicBlock entryBB = new BIRBasicBlock(0); + restoreOriginalArgsFromTuple(argNameList, funcArgs, birFunc.localVars, entryBB.instructions); entryBB.instructions.addAll(collectedIns); currentIns.lhsOp = new BIROperand(birFunc.returnVariable); entryBB.instructions.add(currentIns); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java index c50de97dd88d..4a21a7710538 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIROptimizer.java @@ -347,6 +347,9 @@ private void replaceVarsWithReusableVars(List localVars, List reusableList = typeVsReusableVarsMap.get(variableDcl.type); + if (reusableList == null) { + continue; + } // Same var can be used by multiple operands. We need to collect all and then replace them with // variable declarations. if (reusableVar != null) { From 4051715bd84384c4ec6b5b00018e015c7d760389 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Fri, 15 Nov 2024 18:10:11 +0530 Subject: [PATCH 63/87] Fix checkstyle --- .../main/java/io/ballerina/runtime/internal/TypeChecker.java | 1 - .../org/wso2/ballerinalang/compiler/desugar/Desugar.java | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index ac12c3609c13..e84c85f02d3a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -30,7 +30,6 @@ import io.ballerina.runtime.api.types.UnionType; import io.ballerina.runtime.api.types.XmlNodeType; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BDecimal; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BObject; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 1704d0e547e6..a0607113fb42 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -314,6 +314,7 @@ import org.wso2.ballerinalang.util.Flags; import org.wso2.ballerinalang.util.Lists; +import javax.xml.XMLConstants; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -333,8 +334,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -import javax.xml.XMLConstants; - import static io.ballerina.runtime.api.constants.RuntimeConstants.UNDERSCORE; import static org.ballerinalang.model.symbols.SymbolOrigin.SOURCE; import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL; @@ -922,7 +921,7 @@ private void createTypedescVariableForAnonType(BLangType typeNode) { private Name generateTypedescVariableName(BType targetType) { // tsymbol.name.value is empty for anonymous types except for record types and map types - return targetType.tag == TypeTags.MAP || targetType.tsymbol.name.value.isEmpty()? + return targetType.tag == TypeTags.MAP || targetType.tsymbol.name.value.isEmpty() ? new Name(TYPEDESC + typedescCount++) : new Name(TYPEDESC + targetType.tsymbol.name.value); } From cf13824be4e4d234305d1e2af297a195b9a18ff6 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Sat, 16 Nov 2024 12:01:51 +0530 Subject: [PATCH 64/87] Reduce `MAX_SPLIT_FUNCTION_ARG_COUNT` to `93` --- .../compiler/bir/codegen/optimizer/LargeMethodOptimizer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index eebce32aae72..a8178a3a93a7 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -97,7 +97,7 @@ public class LargeMethodOptimizer { // splits are done only if the newly created method will contain more instructions than the below number private static final int SPLIT_INSTRUCTION_COUNT_THRESHOLD = 25; // splits are done only if the newly created method will have less function arguments than the below number - private static final int MAX_SPLIT_FUNCTION_ARG_COUNT = 125; + private static final int MAX_SPLIT_FUNCTION_ARG_COUNT = 93; // total least no. of terminators and non-terminators that should be there to make the periodic split private static final int INS_COUNT_PERIODIC_SPLIT_THRESHOLD = 500; // current BIR package id From 0f1220247a565baba44ac8ce4ed02dc51b46684e Mon Sep 17 00:00:00 2001 From: rdulmina Date: Sat, 16 Nov 2024 13:03:16 +0530 Subject: [PATCH 65/87] Fix checkstyle --- compiler/ballerina-lang/spotbugs-exclude.xml | 5 +++++ .../org/wso2/ballerinalang/compiler/bir/BIRGen.java | 7 ++++--- .../bir/codegen/optimizer/LargeMethodOptimizer.java | 10 +++++----- .../compiler/bir/codegen/split/JvmAnnotationsGen.java | 2 +- .../wso2/ballerinalang/compiler/desugar/Desugar.java | 3 ++- .../compiler/semantics/analyzer/DataflowAnalyzer.java | 1 + .../bala/record/ClosedRecordTypeInclusionTest.java | 3 ++- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/compiler/ballerina-lang/spotbugs-exclude.xml b/compiler/ballerina-lang/spotbugs-exclude.xml index 601166b8aaff..9df40bca7425 100644 --- a/compiler/ballerina-lang/spotbugs-exclude.xml +++ b/compiler/ballerina-lang/spotbugs-exclude.xml @@ -490,4 +490,9 @@ + + + + + diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 89efc5d10cde..9774a093aec8 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -207,7 +207,6 @@ import org.wso2.ballerinalang.compiler.util.Unifier; import org.wso2.ballerinalang.util.Flags; -import javax.xml.XMLConstants; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -219,6 +218,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; +import javax.xml.XMLConstants; + import static org.ballerinalang.model.tree.NodeKind.CLASS_DEFN; import static org.ballerinalang.model.tree.NodeKind.INVOCATION; import static org.ballerinalang.model.tree.NodeKind.STATEMENT_EXPRESSION; @@ -2506,8 +2507,8 @@ private void createNewTypedescInst(BType resolveType, Location position) { } private BIRVariableDcl createTempVariable() { - BIRVariableDcl tempVarDcl = new BIRVariableDcl(symTable.typeDesc, this.env.nextLocalVarId(names), VarScope.FUNCTION, - VarKind.TEMP); + BIRVariableDcl tempVarDcl = new BIRVariableDcl(symTable.typeDesc, this.env.nextLocalVarId(names), + VarScope.FUNCTION, VarKind.TEMP); this.env.enclFunc.localVars.add(tempVarDcl); return tempVarDcl; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index a8178a3a93a7..6e9b995a342d 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -846,7 +846,7 @@ private void splitParentFuncForPeriodicArraySplits(BIRFunction parentFunc, List< BIRBasicBlock bb = bbs.get(bbIndex); handleBasicBlockStart(splitFuncEnv, nextBBPendingIns, bb); // skip large arg restore and array size ins - int insIndex = bbIndex == 0? skipLargeArgRestoreInstsAndGetIndex(bb) + 1 : 0; + int insIndex = bbIndex == 0 ? skipLargeArgRestoreInstsAndGetIndex(bb) + 1 : 0; for (; insIndex < bb.instructions.size(); insIndex++) { if ((bbIndex == newArrayInsBBNum) && (insIndex == newArrayInsNumInRelevantBB)) { createNewFuncForPeriodicSplit(parentFunc, newlyAddingFunctions, fromAttachedFunction, @@ -892,7 +892,7 @@ private void splitParentFuncForPeriodicArraySplits(BIRFunction parentFunc, List< **/ private int skipLargeArgRestoreInstsAndGetIndex(BIRBasicBlock bb) { List instructions = bb.instructions; - for (int i = 0; i < instructions.size(); i= i + 2) { + for (int i = 0; i < instructions.size(); i = i + 2) { if (!instructions.get(i).lhsOp.variableDcl.name.value.equals(ARG_INDEX)) { return i; } @@ -1329,7 +1329,7 @@ private List getPossibleSplits(List basicBlocks, List neededOperandsVarDcl = new LinkedHashSet<>(); // that will need to be passed as function args + Set neededOperandsVarDcl = new LinkedHashSet<>(); // that will need to be passed as func args Set lhsOperandList = new LinkedHashSet<>(); // that will need as localVars in the new function BIROperand splitStartOperand = null; int splitInsCount = 0; // including terminator instructions @@ -1619,7 +1619,7 @@ private void generateSplits(BIRFunction function, List possibleSplits, // extra +1 for BB incremented in createNewBIRFunctionAcrossBB function, hence BB number is newBBNum + 2 BIRBasicBlock parentFuncNewBB = new BIRBasicBlock(newBBNum + 2); boolean largeArgs = currSplit.funcArgs.size() > MAX_SPLIT_FUNCTION_ARG_COUNT; - List argNameList = largeArgs? createArgTupleAndGetArgNameList(function.localVars, + List argNameList = largeArgs ? createArgTupleAndGetArgNameList(function.localVars, currSplit.funcArgs, currentBB.instructions) : new ArrayList<>(); BIRFunction newBIRFunc = createNewBIRFunctionAcrossBB(function, newFuncName, newFuncReturnType, currSplit, newBBNum, fromAttachedFunction, changedErrorTableEndBB, parentFuncNewBB, argNameList); @@ -2007,7 +2007,7 @@ private BIRBasicBlock generateSplitsInSameBB(BIRFunction function, int bbNum, Li currentBB.instructions.addAll(instructionList.subList(startInsNum, possibleSplit.firstIns)); boolean largeArgs = possibleSplit.funcArgs.size() > MAX_SPLIT_FUNCTION_ARG_COUNT; - List argNameList = largeArgs? createArgTupleAndGetArgNameList(function.localVars, + List argNameList = largeArgs ? createArgTupleAndGetArgNameList(function.localVars, possibleSplit.funcArgs, currentBB.instructions) : new ArrayList<>(); BIRFunction newBIRFunc = createNewBIRFuncForSplitInBB(newFuncName, instructionList.get(possibleSplit.lastIns), diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java index cb088288b21e..e084f2f42e73 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java @@ -129,7 +129,7 @@ private void loadAnnotations(MethodVisitor mv, String pkgName, BIRNode.BIRTypeDe mv.visitFieldInsn(GETSTATIC, pkgClassName, ANNOTATION_MAP_NAME, JvmSignatures.GET_MAP_VALUE); BType type = typeDef.type; BType refType = typeDef.referenceType == null || - (type.tag == TypeTags.RECORD && typeDef.origin == SymbolOrigin.VIRTUAL)? type : typeDef.referenceType; + (type.tag == TypeTags.RECORD && typeDef.origin == SymbolOrigin.VIRTUAL) ? type : typeDef.referenceType; jvmTypeGen.loadType(mv, refType); mv.visitMethodInsn(INVOKESTATIC, ANNOTATION_UTILS, "processAnnotations", JvmSignatures.PROCESS_ANNOTATIONS, false); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index a0607113fb42..a09d9e394f1e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -314,7 +314,6 @@ import org.wso2.ballerinalang.util.Flags; import org.wso2.ballerinalang.util.Lists; -import javax.xml.XMLConstants; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -334,6 +333,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.xml.XMLConstants; + import static io.ballerina.runtime.api.constants.RuntimeConstants.UNDERSCORE; import static org.ballerinalang.model.symbols.SymbolOrigin.SOURCE; import static org.ballerinalang.model.symbols.SymbolOrigin.VIRTUAL; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java index 8ef8edef5480..673d2ebdfab1 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/DataflowAnalyzer.java @@ -2672,6 +2672,7 @@ private void recordGlobalVariableReferenceRelationship(BSymbol symbol) { addDependency(ownerSymbol, symbol); break; } + // fall through default: if (globalVarSymbol || symbol instanceof BTypeSymbol || ownerSymbol.tag == SymTag.LET) { BSymbol dependent = this.currDependentSymbolDeque.peek(); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java index 2898933bc4dc..82c4dae35027 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java @@ -121,7 +121,8 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(), "John Doe"); - assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("ra")))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("ra")))).getTag(), + TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " + "Lanka\"}"); assertEquals(getType(foo2.get(StringUtils.fromString("crx"))).getTag(), TypeTags.XML_ELEMENT_TAG); From eaf64f9d265647c33f50d0df423e10cacd6336c7 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Sun, 17 Nov 2024 22:57:37 +0530 Subject: [PATCH 66/87] Undo unwanted changes --- .../asyncFunctionPackage/Dependencies.toml | 26 +-------------- .../ModuleNameClashProject/Dependencies.toml | 29 +---------------- .../PackageNameClashProject/Dependencies.toml | 26 +-------------- .../bar/Dependencies.toml | 29 +---------------- .../baz/Dependencies.toml | 29 +---------------- .../foo/Dependencies.toml | 29 +---------------- .../qux/Dependencies.toml | 29 +---------------- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../Dependencies.toml | 2 +- .../versions/myPackage/Dependencies.toml | 26 +-------------- .../versions/myPackage2/Dependencies.toml | 32 +++---------------- .../versions/websub/Dependencies.toml | 2 +- 27 files changed, 30 insertions(+), 261 deletions(-) diff --git a/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml index 0e618f9a47fe..bce290a1cbde 100644 --- a/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/async/asyncFunctionPackage/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -15,24 +15,6 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -41,18 +23,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" - [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml index 5592b01c9064..140442cf8660 100644 --- a/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/identifier/ModuleNameClashProject/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,26 +13,6 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -42,12 +22,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" -scope = "testOnly" - [[package]] org = "ballerina" name = "test" @@ -55,7 +29,6 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml index 50f750a7f01b..ea459518354a 100644 --- a/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/identifier/PackageNameClashProject/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "a" @@ -37,24 +37,6 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -63,18 +45,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" - [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml index 30626ea0dbea..9a64ecf1dce3 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,26 +13,6 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -42,12 +22,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" -scope = "testOnly" - [[package]] org = "ballerina" name = "test" @@ -55,7 +29,6 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml index a3f069710613..0a50c1e34b6b 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,26 +13,6 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -42,12 +22,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" -scope = "testOnly" - [[package]] org = "ballerina" name = "test" @@ -55,7 +29,6 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml index 98f4886160d1..26e4ab30b2db 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,26 +13,6 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -42,12 +22,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" -scope = "testOnly" - [[package]] org = "ballerina" name = "test" @@ -55,7 +29,6 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml index 0a277dcf961d..dddea62ca3c3 100644 --- a/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/isolated-inference-projects/qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -13,26 +13,6 @@ name = "jballerina.java" version = "0.0.0" scope = "testOnly" -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -scope = "testOnly" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -42,12 +22,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" -scope = "testOnly" - [[package]] org = "ballerina" name = "test" @@ -55,7 +29,6 @@ version = "0.0.0" scope = "testOnly" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml index 0e8bbcac625d..62ce04d10ce7 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml index 5c941e4ff5b1..52b25f319ce3 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml index 112d4e16eda5..5ac3b997dd76 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml index dbc4a5679ebf..ffffcc1753a6 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_foo_patch/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml index 8de6f85b3f5a..78c92672185f 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/distinct/test_project_distinct_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml index 811601b7030c..046df8e7362f 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml index 2eb4fd4a250f..11b3dfaecfc7 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml index cda67e8fd104..f85033065246 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml index b1cb39b56f59..27e6ac36f2e6 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/one/test_project_immutable_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml index ffaaa6044c61..e2524f13eb0c 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml index a2d7051b7e46..637c33a1418b 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml index d573929beb8a..89503e842498 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml index 289d752c9283..754af8541970 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/three/test_project_immutable_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml index d02cdf214ac8..68a1bb047e18 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_bar/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml index 1274ee12b359..1839e511a48f 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_baz/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml index a243ce68538f..b1f86063f902 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_foo/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml index 1717c4977121..0a781bdbdccd 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/readonly/two/test_project_immutable_qux/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "testorg" diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml index 39dcdfd8a038..75f9ad6346d3 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -15,24 +15,6 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -41,18 +23,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" - [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml index 45b9e524813e..db406d87ecd9 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/versions/myPackage2/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "ballerina" @@ -15,24 +15,6 @@ modules = [ {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.__internal" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.object"} -] - -[[package]] -org = "ballerina" -name = "lang.array" -version = "0.0.0" -dependencies = [ - {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.__internal"} -] - [[package]] org = "ballerina" name = "lang.error" @@ -41,18 +23,12 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] -[[package]] -org = "ballerina" -name = "lang.object" -version = "0.0.0" - [[package]] org = "ballerina" name = "test" version = "0.0.0" dependencies = [ {org = "ballerina", name = "jballerina.java"}, - {org = "ballerina", name = "lang.array"}, {org = "ballerina", name = "lang.error"} ] modules = [ @@ -60,7 +36,7 @@ modules = [ ] [[package]] -org = "dulminakodagoda" +org = "gabilan" name = "myPackage2" version = "0.1.0" dependencies = [ @@ -70,8 +46,8 @@ dependencies = [ {org = "waruna", name = "websub"} ] modules = [ - {org = "dulminakodagoda", packageName = "myPackage2", moduleName = "myPackage2"}, - {org = "dulminakodagoda", packageName = "myPackage2", moduleName = "myPackage2.mod1"} + {org = "gabilan", packageName = "myPackage2", moduleName = "myPackage2"}, + {org = "gabilan", packageName = "myPackage2", moduleName = "myPackage2.mod1"} ] [[package]] diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml b/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml index 633472fbc3ef..a17bfc27d78b 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml +++ b/tests/jballerina-integration-test/src/test/resources/packaging/versions/websub/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.10.0-SNAPSHOT" +distribution-version = "2201.6.0-SNAPSHOT" [[package]] org = "waruna" From 4a72178256837c3f7761d5663a082a3a37ac4753 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 18 Nov 2024 09:58:18 +0530 Subject: [PATCH 67/87] Use `getFirst()` fot `ArrayLists` --- .../codegen/optimizer/LargeMethodOptimizer.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java index 6e9b995a342d..e2bc41b00c02 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/optimizer/LargeMethodOptimizer.java @@ -194,8 +194,8 @@ private void periodicSplitMap(BIRFunction parentFunc, List newlyAdd SplitFuncEnv splitFuncEnv = new SplitFuncEnv(getTempVarsForArraySplit(), fromAttachedFunction); // add large arg restore instructions - int instIndex = skipLargeArgRestoreInstsAndGetIndex(bbs.get(0)); - for (BIRNonTerminator ins : bbs.get(0).instructions.subList(0, instIndex)) { + int instIndex = skipLargeArgRestoreInstsAndGetIndex(bbs.getFirst()); + for (BIRNonTerminator ins : bbs.getFirst().instructions.subList(0, instIndex)) { parentFuncEnv.parentFuncNewInsList.add(ins); parentFuncEnv.parentFuncLocalVarList.add(ins.lhsOp.variableDcl); } @@ -268,8 +268,8 @@ private void periodicSplitArray(BIRFunction parentFunc, List newlyA SplitFuncEnv splitFuncEnv = new SplitFuncEnv(getTempVarsForArraySplit(), fromAttachedFunction); // add large arg restore and constant load array size operand instruction - int instIndex = skipLargeArgRestoreInstsAndGetIndex(bbs.get(0)); - for (BIRNonTerminator ins : bbs.get(0).instructions.subList(0, instIndex + 1)) { + int instIndex = skipLargeArgRestoreInstsAndGetIndex(bbs.getFirst()); + for (BIRNonTerminator ins : bbs.getFirst().instructions.subList(0, instIndex + 1)) { parentFuncEnv.parentFuncNewInsList.add(ins); parentFuncEnv.parentFuncLocalVarList.add(ins.lhsOp.variableDcl); } @@ -933,7 +933,7 @@ private void createNewFuncForPeriodicSplit(BIRFunction parentFunc, List(splitFuncLocalVarList); if (splitFuncEnv.splitFuncNewBBList.size() > 0) { - splitFuncEnv.splitFuncNewBBList.get(0).instructions.addAll(0, argRestoreInstructions); + splitFuncEnv.splitFuncNewBBList.getFirst().instructions.addAll(0, argRestoreInstructions); } else { splitFuncEnv.splitFuncNewInsList.addAll(0, argRestoreInstructions); } @@ -1450,11 +1450,11 @@ private List getPossibleSplits(List basicBlocks, List firstBBIns = basicBlocks.get(0).instructions; + List firstBBIns = basicBlocks.getFirst().instructions; BIRNonTerminator firstIns = firstBBIns.get( - skipLargeArgRestoreInstsAndGetIndex(basicBlocks.get(0))); + skipLargeArgRestoreInstsAndGetIndex(basicBlocks.getFirst())); if (((startOfStructInst.stream().map(op -> op.variableDcl).toList().contains( firstIns.lhsOp.variableDcl)))) { continue; From 3ff99147dc0cf76c98049cef560067e6f4443e57 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 18 Nov 2024 11:45:07 +0530 Subject: [PATCH 68/87] Remove unused methods --- .../io/ballerina/runtime/internal/TypeChecker.java | 1 + .../wso2/ballerinalang/compiler/bir/BIRGen.java | 14 -------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index 1bbbff21bfe6..47f1bba89f7c 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -553,6 +553,7 @@ public static Object getAnnotValue(TypedescValue typedescValue, BString annotTag } MapValue annotations = ((TypedescValueImpl) typedescValue).annotations; if (annotations != null) { + // ATM only field annotations will reach here return annotations.get(annotTag); } return annotatableType.getAnnotation(annotTag); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 4f86fc1840a6..96b78540bdff 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -3180,20 +3180,6 @@ private List getBIRAnnotAttachmentsForASTAnnotAttachmen return annotationAttachments; } - private BIROperand getAnnotations(BTypeSymbol typeSymbol, BIRGenEnv env) { - if (typeSymbol == null || typeSymbol.annotations == null) { - return null; - } - return new BIROperand(getAnnotations(typeSymbol.annotations, env)); - } - - private BIRVariableDcl getAnnotations(BVarSymbol annotations, BIRGenEnv env) { - if (env.symbolVarMap.containsKey(annotations)) { - return env.symbolVarMap.get(annotations); - } - return globalVarMap.get(annotations); - } - private void addReturnBB(Location pos) { if (this.env.returnBB == null) { BIRBasicBlock returnBB = new BIRBasicBlock(this.env.nextBBId()); From 27b62a61a80b3a3e769d3a2b2eae99202757e08f Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 18 Nov 2024 15:09:55 +0530 Subject: [PATCH 69/87] Checkout Java21 branch in full build pipeline --- .github/workflows/pull_request_full_build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request_full_build.yml b/.github/workflows/pull_request_full_build.yml index e937baa0976f..d0f3ffa29484 100644 --- a/.github/workflows/pull_request_full_build.yml +++ b/.github/workflows/pull_request_full_build.yml @@ -96,7 +96,7 @@ jobs: - name: Checkout non-default branch run: | for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ - cd $module_name && git fetch origin && git checkout -t origin/java21 && cd ..; \ + cd $module_name && git fetch origin && git checkout -t origin/typeDesc-stmt && cd ..; \ done - name: Update Lang Version in Module From 9fe124d68802873f9f63d6943076de9ad5e0a6ec Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 18 Nov 2024 17:12:37 +0530 Subject: [PATCH 70/87] Checkout typeDesc-stmt branch in full build pipeline --- .github/workflows/pull_request_full_build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request_full_build.yml b/.github/workflows/pull_request_full_build.yml index e937baa0976f..1622c8a2a454 100644 --- a/.github/workflows/pull_request_full_build.yml +++ b/.github/workflows/pull_request_full_build.yml @@ -96,7 +96,7 @@ jobs: - name: Checkout non-default branch run: | for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ - cd $module_name && git fetch origin && git checkout -t origin/java21 && cd ..; \ + cd $module_name && git fetch origin && git checkout -b origin/java21 && cd ..; \ done - name: Update Lang Version in Module From 2c75b6cf456468bbac89eb2350910b26463d058b Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 18 Nov 2024 18:24:20 +0530 Subject: [PATCH 71/87] Fix usage of incorrect branch in full build pipeline --- .github/workflows/pull_request_full_build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request_full_build.yml b/.github/workflows/pull_request_full_build.yml index 1622c8a2a454..a6bee99b6942 100644 --- a/.github/workflows/pull_request_full_build.yml +++ b/.github/workflows/pull_request_full_build.yml @@ -96,7 +96,7 @@ jobs: - name: Checkout non-default branch run: | for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ - cd $module_name && git fetch origin && git checkout -b origin/java21 && cd ..; \ + cd $module_name && git fetch origin && git checkout -b origin/typeDesc-stmt && cd ..; \ done - name: Update Lang Version in Module From f065eb5e7f94ecb2ac5d77df3acfbb0646597b0c Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 19 Nov 2024 11:06:03 +0530 Subject: [PATCH 72/87] Fix incorrect checkout branch name --- .github/workflows/pull_request_full_build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request_full_build.yml b/.github/workflows/pull_request_full_build.yml index a6bee99b6942..b33671d4a6e4 100644 --- a/.github/workflows/pull_request_full_build.yml +++ b/.github/workflows/pull_request_full_build.yml @@ -96,7 +96,7 @@ jobs: - name: Checkout non-default branch run: | for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ - cd $module_name && git fetch origin && git checkout -b origin/typeDesc-stmt && cd ..; \ + cd $module_name && git fetch origin && git checkout -b typeDesc-stmt && cd ..; \ done - name: Update Lang Version in Module From 7e6125c7209cc6aa5cbe1530c84fe5e5d1b95863 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 19 Nov 2024 12:03:05 +0530 Subject: [PATCH 73/87] Fix branch checkout command --- .github/workflows/pull_request_full_build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull_request_full_build.yml b/.github/workflows/pull_request_full_build.yml index b33671d4a6e4..a04176a2c783 100644 --- a/.github/workflows/pull_request_full_build.yml +++ b/.github/workflows/pull_request_full_build.yml @@ -96,7 +96,7 @@ jobs: - name: Checkout non-default branch run: | for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ - cd $module_name && git fetch origin && git checkout -b typeDesc-stmt && cd ..; \ + cd $module_name && git fetch origin && git checkout -t origin/typeDesc-stmt || git checkout -t origin/java21 && cd ..; \ done - name: Update Lang Version in Module From f9da8b606e087a381d56e3c13fc93b3ba6752ef3 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 19 Nov 2024 16:37:22 +0530 Subject: [PATCH 74/87] Address review suggestions --- .../runtime/internal/TypeChecker.java | 5 ----- .../ballerinalang/compiler/bir/BIRGen.java | 22 +++++++------------ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index 47f1bba89f7c..4705c4ead712 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -551,11 +551,6 @@ public static Object getAnnotValue(TypedescValue typedescValue, BString annotTag if (!(describingType instanceof BAnnotatableType annotatableType)) { return null; } - MapValue annotations = ((TypedescValueImpl) typedescValue).annotations; - if (annotations != null) { - // ATM only field annotations will reach here - return annotations.get(annotTag); - } return annotatableType.getAnnotation(annotTag); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 96b78540bdff..435456aec706 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2409,10 +2409,6 @@ private BIRNonTerminator.NewStructure createNewStructureInst(BIROperand typeDesc return new BIRNonTerminator.NewStructure(pos, toVarRef, typeDesc, fields); } - public String getTypedescFieldName(String name) { - return "$typedesc$" + name; - } - private BIRVariableDcl getTypedescVariable(BType type, Location pos) { Supplier[] checks = new Supplier[] { () -> findInPackageScope(type), @@ -2437,17 +2433,15 @@ private BIRVariableDcl getTypedescVariable(BType type, Location pos) { private BIRVariableDcl findInPackageScope(BType type) { BTypeSymbol typeSymbol = type.tsymbol; - if (typeSymbol != null && typeSymbol.owner.tag == SymTag.PACKAGE && - !isInSameModule(typeSymbol, env.enclPkg.packageID)) { - BPackageSymbol packageSymbol = (BPackageSymbol) typeSymbol.owner; - Scope.ScopeEntry scopeEntry = - packageSymbol.scope.lookup(new Name(getTypedescFieldName(typeSymbol.name.value))); - BSymbol symbol = scopeEntry.symbol; - if (symbol != null) { - return getVarRef(createPackageVarRef(symbol)); - } + if (typeSymbol == null || typeSymbol.owner.tag != SymTag.PACKAGE || + isInSameModule(typeSymbol, env.enclPkg.packageID)) { + return null; } - return null; + BPackageSymbol packageSymbol = (BPackageSymbol) typeSymbol.owner; + Scope.ScopeEntry scopeEntry = + packageSymbol.scope.lookup(new Name(TYPEDESC + typeSymbol.name.value)); + BSymbol symbol = scopeEntry.symbol; + return symbol != null? getVarRef(createPackageVarRef(symbol)) : null; } private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { From e5f1afc63afb1f8e61cac5b815631cbbb6106380 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 19 Nov 2024 19:58:19 +0530 Subject: [PATCH 75/87] Set annotation for the referred type if it is record Previously we set the annotation for record type only. Now we set for both the type reference type and record type --- .../io/ballerina/runtime/internal/utils/AnnotationUtils.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java index 00b7d37b44f4..b350bcedc14f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java @@ -63,6 +63,10 @@ public static void processAnnotations(MapValue globalAnnotMap, if (type.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) { Type impliedType = TypeUtils.getImpliedType(type); + if (impliedType.getTag() == TypeTags.RECORD_TYPE_TAG) { + processAnnotations(globalAnnotMap, impliedType); + return; + } if (isNonObjectType(impliedType.getTag())) { return; } From 7535598b8e9715868d8a32b8efa53ba4292e0599 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 19 Nov 2024 20:47:17 +0530 Subject: [PATCH 76/87] Fix checkstyle issue --- .../main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 435456aec706..35405da873ed 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2441,7 +2441,7 @@ private BIRVariableDcl findInPackageScope(BType type) { Scope.ScopeEntry scopeEntry = packageSymbol.scope.lookup(new Name(TYPEDESC + typeSymbol.name.value)); BSymbol symbol = scopeEntry.symbol; - return symbol != null? getVarRef(createPackageVarRef(symbol)) : null; + return symbol != null ? getVarRef(createPackageVarRef(symbol)) : null; } private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { From 2884dafce698ef9d7baaf7085e6ba6112e14db5d Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 20 Nov 2024 17:36:15 +0530 Subject: [PATCH 77/87] Fix import order --- .../test/record/ClosedRecordOptionalFieldsTest.java | 2 +- .../ballerinalang/test/record/OpenRecordOptionalFieldsTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java index 94fa906aa2b0..167c3afcf08a 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java @@ -17,8 +17,8 @@ */ package org.ballerinalang.test.record; -import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.types.Type; +import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java index 85b7fd533248..29d53d65ff45 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java @@ -17,8 +17,8 @@ */ package org.ballerinalang.test.record; -import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.types.Type; +import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; From fb628eb9e6e0c82fc1002025439809cf8ac39ec8 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 21 Nov 2024 11:36:46 +0530 Subject: [PATCH 78/87] Add a test case for issue `#43633` --- .../test/bala/record/ClosedRecordTypeInclusionTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java index 5bb93536855a..80606bfc0373 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java @@ -111,6 +111,7 @@ public void testValRefType() { public void testRefTypes() { Object returns = BRunUtil.invoke(compileResult, "testRefTypes"); BMap foo2 = (BMap) returns; + assertEquals(foo2.getType().getTag(), TypeTags.TYPE_REFERENCED_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("s")).toString(), "qwerty"); assertEquals(foo2.get(StringUtils.fromString("i")), 10L); assertEquals(getType(foo2.get(StringUtils.fromString("rj"))).getTag(), TypeTags.MAP_TAG); From 35555f31a5a51e285d21de7397990790d51eccee Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 21 Nov 2024 14:40:55 +0530 Subject: [PATCH 79/87] Revert "Fix set of failing test due to type mismatch" This reverts commit f1ebb95a --- .../bala/record/ClosedRecordTypeInclusionTest.java | 6 ++---- .../bala/record/OpenRecordTypeInclusionTest.java | 7 ++----- .../record/ClosedRecordOptionalFieldsTest.java | 14 +++++--------- .../test/record/OpenRecordOptionalFieldsTest.java | 14 +++++--------- .../test/types/anydata/AnydataTernaryConvTest.java | 7 +++---- .../test/types/anydata/AnydataTest.java | 3 +-- .../test/types/typedesc/TypedescTests.java | 6 ++---- 7 files changed, 20 insertions(+), 37 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java index 80606bfc0373..ab3ac52f7f42 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java @@ -21,7 +21,6 @@ import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; import org.ballerinalang.test.BAssertUtil; @@ -122,7 +121,7 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(), "John Doe"); - assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("ra")))).getTag(), + assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " + "Lanka\"}"); @@ -134,8 +133,7 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("crp"))).get(StringUtils.fromString("name")).toString(), "Jane Doe"); - assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("cra")))).getTag(), - TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(foo2.get(StringUtils.fromString("cra"))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("cra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " + "Lanka\"}"); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java index 66ea645a6619..978e5e4dffee 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/OpenRecordTypeInclusionTest.java @@ -21,7 +21,6 @@ import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BObject; import org.ballerinalang.test.BAssertUtil; @@ -113,8 +112,7 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(), "John Doe"); - assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("ra")))).getTag(), - TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri Lanka\"}"); assertEquals(getType(foo2.get(StringUtils.fromString("crx"))).getTag(), TypeTags.XML_ELEMENT_TAG); @@ -125,8 +123,7 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("crp"))).get(StringUtils.fromString("name")).toString(), "Jane Doe"); - assertEquals(TypeUtils.getReferredType(getType(foo2.get(StringUtils.fromString("cra")))).getTag(), - TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(foo2.get(StringUtils.fromString("cra"))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("cra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri Lanka\"}"); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java index 167c3afcf08a..de28957edf0b 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java @@ -17,10 +17,8 @@ */ package org.ballerinalang.test.record; -import io.ballerina.runtime.api.types.Type; -import io.ballerina.runtime.api.types.TypeTags; +import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; import org.ballerinalang.test.BAssertUtil; @@ -135,14 +133,12 @@ public void testOptionalNonDefField4() { BArray returns = (BArray) BRunUtil.invoke(compileResult, "testOptionalNonDefField4"); Assert.assertEquals(returns.get(0).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\"," + "\"country\":\"LK\"}"); - Type type = TypeUtils.getReferredType(getType(returns.get(0))); - Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(type.getName(), "Address3"); + Assert.assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(getType(returns.get(0)).getName(), "Address3"); Assert.assertEquals(returns.get(1).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\"," + "\"country\":\"LK\"}"); - type = TypeUtils.getReferredType(getType(returns.get(1))); - Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(type.getName(), "Address3"); + Assert.assertEquals(getType(returns.get(1)).getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(getType(returns.get(1)).getName(), "Address3"); } @Test(description = "Test defaultable user defined type as an optional field") diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java index 29d53d65ff45..506cf5750a56 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java @@ -17,10 +17,8 @@ */ package org.ballerinalang.test.record; -import io.ballerina.runtime.api.types.Type; -import io.ballerina.runtime.api.types.TypeTags; +import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; import org.ballerinalang.test.BAssertUtil; @@ -138,14 +136,12 @@ public void testOptionalNonDefField4() { BArray returns = (BArray) BRunUtil.invoke(compileResult, "testOptionalNonDefField4"); Assert.assertEquals(returns.get(0).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\",\"country\":\"LK\"}"); - Type type = TypeUtils.getReferredType(getType(returns.get(0))); - Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(type.getName(), "Address3"); + Assert.assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(getType(returns.get(0)).getName(), "Address3"); Assert.assertEquals(returns.get(1).toString(), "{\"street\":\"Palm Grove\",\"city\":\"Colombo 3\",\"country\":\"LK\"}"); - type = TypeUtils.getReferredType(getType(returns.get(1))); - Assert.assertEquals(type.getTag(), TypeTags.RECORD_TYPE_TAG); - Assert.assertEquals(type.getName(), "Address3"); + Assert.assertEquals(getType(returns.get(1)).getTag(), TypeTags.RECORD_TYPE_TAG); + Assert.assertEquals(getType(returns.get(1)).getName(), "Address3"); } @Test(description = "Test defaultable user defined type as an optional field") diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java index d3e8f8b40193..0517976775a9 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTernaryConvTest.java @@ -19,7 +19,6 @@ package org.ballerinalang.test.types.anydata; import io.ballerina.runtime.api.types.TypeTags; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.internal.types.BArrayType; import org.ballerinalang.test.BCompileUtil; @@ -74,7 +73,7 @@ public void testAnydataToXml() { @Test(description = "Test anydata to record conversion") public void testAnydataToRecord() { BArray returns = (BArray) BRunUtil.invoke(result, "testAnydataToRecord"); - assertEquals(TypeUtils.getReferredType(getType(returns.get(0))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(returns.get(0).toString(), "{\"a\":15}"); } @@ -98,8 +97,8 @@ public void testAnydataToUnion2() { BArray returns = (BArray) BRunUtil.invoke(result, "testAnydataToUnion2"); assertEquals(getType(returns.get(0)).getTag(), TypeTags.MAP_TAG); assertEquals(getType(returns.get(1)).getTag(), TypeTags.XML_ELEMENT_TAG); - assertEquals(TypeUtils.getReferredType(getType(returns.get(2))).getTag(), TypeTags.RECORD_TYPE_TAG); - assertEquals(TypeUtils.getReferredType(getType(returns.get(3))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(returns.get(2)).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(returns.get(3)).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(returns.get(0).toString(), "{\"name\":\"apple\",\"color\":\"red\",\"price\":40}"); assertEquals(returns.get(1).toString(), "The Lost World"); assertEquals(returns.get(2).toString(), "{\"a\":15}"); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java index f3294de333e5..da799af8d61a 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/anydata/AnydataTest.java @@ -20,7 +20,6 @@ import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; import io.ballerina.runtime.api.values.BXml; @@ -183,7 +182,7 @@ public void testAnydataToXml() { @Test(description = "Test anydata to record conversion") public void testAnydataToRecord() { BArray returns = (BArray) BRunUtil.invoke(result, "testAnydataToRecord"); - assertEquals(TypeUtils.getReferredType(getType(returns.get(0))).getTag(), TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(returns.get(0)).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(returns.get(0).toString(), "{\"a\":15}"); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java index 31973808ace1..1b2961b21ff0 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java @@ -16,8 +16,7 @@ */ package org.ballerinalang.test.types.typedesc; -import io.ballerina.runtime.api.types.TypeTags; -import io.ballerina.runtime.api.utils.TypeUtils; +import io.ballerina.runtime.api.TypeTags; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BTypedesc; import org.ballerinalang.test.BCompileUtil; @@ -125,8 +124,7 @@ public void testRecordTypes() { Assert.assertEquals(returns.size(), 2); Assert.assertEquals(returns.get(0).toString(), "typedesc RecordA"); Assert.assertTrue(returns.get(1) instanceof BTypedesc); - Assert.assertEquals(TypeTags.RECORD_TYPE_TAG, - TypeUtils.getReferredType(((BTypedesc) returns.get(1)).getDescribingType()).getTag()); + Assert.assertEquals(TypeTags.RECORD_TYPE_TAG, ((BTypedesc) returns.get(1)).getDescribingType().getTag()); } @Test(description = "Test any to typedesc cast") From dd0d8f5fdaa97ee912a5f6902767b82702466c4c Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 21 Nov 2024 15:32:40 +0530 Subject: [PATCH 80/87] Use referred type when creating typedesc for records --- .../runtime/internal/utils/AnnotationUtils.java | 4 ---- .../compiler/bir/codegen/split/JvmAnnotationsGen.java | 5 ++--- .../wso2/ballerinalang/compiler/desugar/Desugar.java | 11 ++++++++++- .../bala/record/ClosedRecordTypeInclusionTest.java | 4 +--- .../test/record/ClosedRecordOptionalFieldsTest.java | 2 +- .../test/record/OpenRecordOptionalFieldsTest.java | 2 +- .../test/types/typedesc/TypedescTests.java | 2 +- .../annotations/annotation_access_negative.bal | 2 +- ...readonly_violation_without_tuple_update_method.bal | 2 +- 9 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java index b350bcedc14f..00b7d37b44f4 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/utils/AnnotationUtils.java @@ -63,10 +63,6 @@ public static void processAnnotations(MapValue globalAnnotMap, if (type.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) { Type impliedType = TypeUtils.getImpliedType(type); - if (impliedType.getTag() == TypeTags.RECORD_TYPE_TAG) { - processAnnotations(globalAnnotMap, impliedType); - return; - } if (isNonObjectType(impliedType.getTag())) { return; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java index 05a5e16c0873..fb1305081f4e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/JvmAnnotationsGen.java @@ -19,7 +19,6 @@ package org.wso2.ballerinalang.compiler.bir.codegen.split; import org.ballerinalang.model.elements.PackageID; -import org.ballerinalang.model.symbols.SymbolOrigin; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.wso2.ballerinalang.compiler.bir.codegen.BallerinaClassWriter; @@ -128,8 +127,8 @@ private void loadAnnotations(MethodVisitor mv, String pkgName, BIRNode.BIRTypeDe jvmPackageGen.lookupGlobalVarClassName(pkgName, ANNOTATION_MAP_NAME); mv.visitFieldInsn(GETSTATIC, pkgClassName, ANNOTATION_MAP_NAME, JvmSignatures.GET_MAP_VALUE); BType type = typeDef.type; - BType refType = typeDef.referenceType == null || - (type.tag == TypeTags.RECORD && typeDef.origin == SymbolOrigin.VIRTUAL) ? type : typeDef.referenceType; + BType refType = typeDef.referenceType == null || type.tag == TypeTags.RECORD + ? type : typeDef.referenceType; jvmTypeGen.loadType(mv, refType); mv.visitMethodInsn(INVOKESTATIC, ANNOTATION_UTILS, "processAnnotations", JvmSignatures.PROCESS_ANNOTATIONS, false); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 2cef9dc1cd50..b60244a23e60 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -82,6 +82,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleMember; import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType; import org.wso2.ballerinalang.compiler.semantics.model.types.BType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BTypeReferenceType; import org.wso2.ballerinalang.compiler.semantics.model.types.BTypedescType; import org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType; import org.wso2.ballerinalang.compiler.semantics.model.types.BXMLSubType; @@ -892,10 +893,11 @@ private void desugarConstants(BLangConstant constant, List desuga } private void createTypedescVariable(BType type, Location pos) { + BType finalType = type; if ((Types.getReferredType(type).tag != TypeTags.INTERSECTION && this.env.enclPkg.typeDefinitions.stream() .anyMatch(typeDef -> (Types.getReferredType(typeDef.typeNode.getBType()).tag == TypeTags.INTERSECTION) && - typeDef.symbol.name.value.equals(type.tsymbol.name.value)))) { + typeDef.symbol.name.value.equals(finalType.tsymbol.name.value)))) { // This is a workaround for an issue where we create two type defs with same name for the below sample // type T1 [T1] & readonly; return; @@ -905,6 +907,13 @@ private void createTypedescVariable(BType type, Location pos) { BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); BSymbol owner = this.env.scope.owner; BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); + if (type.tag == TypeTags.TYPEREFDESC) { + BType referredType = ((BTypeReferenceType) type).referredType; + int tag = referredType.tag; + if (tag == TypeTags.RECORD) { + type = referredType; + } + } BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type); typedescList.add(createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, varSymbol)); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java index ab3ac52f7f42..8076caed862b 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/bala/record/ClosedRecordTypeInclusionTest.java @@ -110,7 +110,6 @@ public void testValRefType() { public void testRefTypes() { Object returns = BRunUtil.invoke(compileResult, "testRefTypes"); BMap foo2 = (BMap) returns; - assertEquals(foo2.getType().getTag(), TypeTags.TYPE_REFERENCED_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("s")).toString(), "qwerty"); assertEquals(foo2.get(StringUtils.fromString("i")), 10L); assertEquals(getType(foo2.get(StringUtils.fromString("rj"))).getTag(), TypeTags.MAP_TAG); @@ -121,8 +120,7 @@ public void testRefTypes() { assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG); assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(), "John Doe"); - assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), - TypeTags.RECORD_TYPE_TAG); + assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), TypeTags.RECORD_TYPE_TAG); assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " + "Lanka\"}"); assertEquals(getType(foo2.get(StringUtils.fromString("crx"))).getTag(), TypeTags.XML_ELEMENT_TAG); diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java index de28957edf0b..c260e0b6c590 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/ClosedRecordOptionalFieldsTest.java @@ -17,7 +17,7 @@ */ package org.ballerinalang.test.record; -import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java index 506cf5750a56..4036bb29a908 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/OpenRecordOptionalFieldsTest.java @@ -17,7 +17,7 @@ */ package org.ballerinalang.test.record; -import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java index 1b2961b21ff0..8539f5558225 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/types/typedesc/TypedescTests.java @@ -16,7 +16,7 @@ */ package org.ballerinalang.test.types.typedesc; -import io.ballerina.runtime.api.TypeTags; +import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BTypedesc; import org.ballerinalang.test.BCompileUtil; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal index 5ba6a1dec1da..c3fa341d4c0b 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/annotations/annotation_access_negative.bal @@ -48,7 +48,7 @@ function testRecordTypeAnnotationReadonlyValueEdit() { error resError = res; assertEquality("{ballerina/lang.map}InherentTypeViolation", resError.message()); - assertEquality("cannot update 'readonly' field 'foo' in record of type '(Annot & readonly)'", + assertEquality("cannot update 'readonly' field 'foo' in record of type 'Annot & readonly'", resError.detail()["message"]); } diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal b/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal index b43baf877141..b04fe3c9e75a 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/types/readonly/test_tuple_vs_array_readonly_violation_without_tuple_update_method.bal @@ -34,5 +34,5 @@ function testFrozenAnyArrayElementUpdate() returns error? { test:assertTrue(actualError is error); test:assertEquals((actualError).message(), "{ballerina/lang.map}InherentTypeViolation"); test:assertEquals((actualError).detail()["message"], - "cannot update 'readonly' field 'name' in record of type '(Employee & readonly)'"); + "cannot update 'readonly' field 'name' in record of type 'Employee & readonly'"); } From 31c6b6ba618f814d0e61c26c168aae8a1fccd487 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 21 Nov 2024 16:06:46 +0530 Subject: [PATCH 81/87] Comment Checkout non-default branch in full build --- .github/workflows/pull_request_full_build.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pull_request_full_build.yml b/.github/workflows/pull_request_full_build.yml index 1a854bc13684..78561ea7b48d 100644 --- a/.github/workflows/pull_request_full_build.yml +++ b/.github/workflows/pull_request_full_build.yml @@ -92,11 +92,11 @@ jobs: do git clone https://github.com/ballerina-platform/${module_name}.git; \ done - - name: Checkout non-default branch - run: | - for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ - cd $module_name && git fetch origin && git checkout -t origin/typeDesc-stmt || : && cd ..; \ - done +# - name: Checkout non-default branch +# run: | +# for module_name in $(jq -r '.standard_library| .[] | select(.level==${{ matrix.level }}) | .name' extensions.json); do \ +# cd $module_name && git fetch origin && git checkout -t origin/typeDesc-stmt || : && cd ..; \ +# done - name: Update Lang Version in Module run: | From bc42da51ff761fb2e3257d03c9e93663c9217de4 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 21 Nov 2024 16:44:49 +0530 Subject: [PATCH 82/87] Use referred type when creating typedesc for maps --- .../java/org/wso2/ballerinalang/compiler/desugar/Desugar.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index b60244a23e60..19894b200585 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -910,7 +910,7 @@ private void createTypedescVariable(BType type, Location pos) { if (type.tag == TypeTags.TYPEREFDESC) { BType referredType = ((BTypeReferenceType) type).referredType; int tag = referredType.tag; - if (tag == TypeTags.RECORD) { + if (tag == TypeTags.RECORD || tag == TypeTags.MAP) { type = referredType; } } From 7e04bf7b9d5a1f8d91a75998857d6e2e1fd02570 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Thu, 21 Nov 2024 16:55:22 +0530 Subject: [PATCH 83/87] Revert "Use referred type when creating typedesc for maps" This reverts commit bc42da51ff761fb2e3257d03c9e93663c9217de4. --- .../java/org/wso2/ballerinalang/compiler/desugar/Desugar.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index 19894b200585..b60244a23e60 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -910,7 +910,7 @@ private void createTypedescVariable(BType type, Location pos) { if (type.tag == TypeTags.TYPEREFDESC) { BType referredType = ((BTypeReferenceType) type).referredType; int tag = referredType.tag; - if (tag == TypeTags.RECORD || tag == TypeTags.MAP) { + if (tag == TypeTags.RECORD) { type = referredType; } } From c0fa12da54f0605909cd78052c9a5ce03db6fe8a Mon Sep 17 00:00:00 2001 From: rdulmina Date: Mon, 25 Nov 2024 13:46:57 +0530 Subject: [PATCH 84/87] Fix duplicate typedesc creation for each type desc expression --- .../ballerinalang/compiler/bir/BIRGen.java | 35 +++++++++---------- .../compiler/bir/model/BIRNode.java | 1 + .../compiler/bir/model/BIRNonTerminator.java | 1 + 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 35405da873ed..c4ae8a663456 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -214,10 +214,8 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.function.Supplier; import java.util.regex.Pattern; import java.util.stream.Collectors; - import javax.xml.XMLConstants; import static org.ballerinalang.model.tree.NodeKind.CLASS_DEFN; @@ -2235,7 +2233,7 @@ public void visit(BLangXMLAccessExpr xmlAccessExpr) { @Override public void visit(BLangTypedescExpr accessExpr) { - createNewTypedescInst(accessExpr.resolvedType, accessExpr.pos); + this.env.targetOperand = new BIROperand(getTypedescVariable(accessExpr.resolvedType, accessExpr.pos)); } @Override @@ -2410,23 +2408,24 @@ private BIRNonTerminator.NewStructure createNewStructureInst(BIROperand typeDesc } private BIRVariableDcl getTypedescVariable(BType type, Location pos) { - Supplier[] checks = new Supplier[] { - () -> findInPackageScope(type), - () -> findInLocalSymbolVarMap(type, env.symbolVarMap, false), - () -> findInLocalSymbolVarMap(type, env.symbolVarMap, true), - () -> findInGlobalSymbolVarMap(type, this.globalVarMap, false), - () -> findInGlobalSymbolVarMap(type, this.globalVarMap, true) - }; + BIRVariableDcl variableDcl = findInPackageScope(type); + if (variableDcl != null && variableDcl.initialized) return variableDcl; - // Iterate through the suppliers and return the first non-null result - for (Supplier check : checks) { - BIRVariableDcl variableDcl = check.get(); - if (variableDcl != null) { - return variableDcl; - } - } + variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, false); + if (variableDcl != null && variableDcl.initialized) return variableDcl; + + variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, true); + if (variableDcl != null && variableDcl.initialized) return variableDcl; + + variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, false); + if (variableDcl != null && variableDcl.initialized) return variableDcl; + + variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, true); + if (variableDcl != null && variableDcl.initialized) return variableDcl; - // TODO: we need to remove typedesc creating completely from here and handle it in the Desugar phase + // Create new type desc instruction if the typedesc variable is not found + // TODO: we need to handle duplicate typedesc creation for some cases. + // eg: function params ie `foo(record {int a;})` createNewTypedescInst(type, pos); return this.env.targetOperand.variableDcl; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNode.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNode.java index c6a880d0031b..109858a0dd13 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNode.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNode.java @@ -146,6 +146,7 @@ public static class BIRVariableDcl extends BIRDocumentableNode { public BIRBasicBlock startBB; public int insOffset; public boolean onlyUsedInSingleBB; + public boolean initialized = false; // Stores the scope of the current instruction with respect to local variables. public BirScope insScope; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java index efac43fbef9c..9cc5628bbf6b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java @@ -57,6 +57,7 @@ public Move(Location pos, BIROperand fromOperand, BIROperand toOperand) { super(pos, InstructionKind.MOVE); this.rhsOp = fromOperand; this.lhsOp = toOperand; + toOperand.variableDcl.initialized = true; } @Override From 103fc70d91786d538e13b4c9abc16d94aee6fac5 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 26 Nov 2024 11:41:29 +0530 Subject: [PATCH 85/87] Refactor code --- .../runtime/internal/values/MapValueImpl.java | 1 - .../ballerinalang/compiler/bir/BIRGen.java | 50 +++++++++++-------- .../compiler/desugar/Desugar.java | 6 +-- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java index 46a617c46545..ebbdce07f208 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/MapValueImpl.java @@ -23,7 +23,6 @@ import io.ballerina.runtime.api.types.Type; import io.ballerina.runtime.api.types.TypeTags; import io.ballerina.runtime.api.utils.StringUtils; -import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BFunctionPointer; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index c4ae8a663456..3d52bfd7e739 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -82,6 +82,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType; +import org.wso2.ballerinalang.compiler.semantics.model.types.BIntersectionType; import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; import org.wso2.ballerinalang.compiler.semantics.model.types.BTableType; @@ -216,6 +217,7 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; + import javax.xml.XMLConstants; import static org.ballerinalang.model.tree.NodeKind.CLASS_DEFN; @@ -2408,20 +2410,28 @@ private BIRNonTerminator.NewStructure createNewStructureInst(BIROperand typeDesc } private BIRVariableDcl getTypedescVariable(BType type, Location pos) { - BIRVariableDcl variableDcl = findInPackageScope(type); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + if (type.tag == TypeTags.TYPEREFDESC) { + BType referredType = ((BTypeReferenceType) type).referredType; + int tag = referredType.tag; + if (tag == TypeTags.RECORD) { + type = referredType; + } + } - variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, false); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + BIRVariableDcl variableDcl = findInPackageScope(type); + if (variableDcl != null && variableDcl.initialized) { + return variableDcl; + } - variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap, true); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + variableDcl = findInLocalSymbolVarMap(type, env.symbolVarMap); + if (variableDcl != null && variableDcl.initialized) { + return variableDcl; + } variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, false); - if (variableDcl != null && variableDcl.initialized) return variableDcl; - - variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, true); - if (variableDcl != null && variableDcl.initialized) return variableDcl; + if (variableDcl != null && variableDcl.initialized) { + return variableDcl; + } // Create new type desc instruction if the typedesc variable is not found // TODO: we need to handle duplicate typedesc creation for some cases. @@ -2450,10 +2460,9 @@ private BLangPackageVarRef createPackageVarRef(BSymbol symbol) { return packageVarRef; } - private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap, - boolean checkImpliedType) { + private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { - if (isMatchingTypeDescSymbol(entry.getKey(), type, checkImpliedType)) { + if (isMatchingTypeDescSymbol(entry.getKey(), type)) { return varMap.get(entry.getKey()); } } @@ -2464,7 +2473,7 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map entry : varMap.entrySet()) { BSymbol varSymbol = entry.getKey(); - if (isMatchingTypeDescSymbol(varSymbol, type, checkImpliedType)) { + if (isMatchingTypeDescSymbol(varSymbol, type)) { BIRGlobalVariableDcl globalVarDcl = varMap.get(varSymbol); if (!isInSameModule(varSymbol, env.enclPkg.packageID) || env.enclPkg.packageID.isTestPkg) { this.env.enclPkg.importedGlobalVarsDummyVarDcls.add(globalVarDcl); @@ -2475,26 +2484,25 @@ private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map & readonly q = table [{name: "Jo"}]; - // Row type will be Immutable Identifier and there will be not intersection type created for that + // Row type will be Immutable Identifier and there will be no intersection type created for that BType constraint = ((BTypedescType) symbol.type).constraint; - constraint = checkImpliedType ? Types.getImpliedType(constraint) : constraint; - targetType = checkImpliedType ? Types.getImpliedType(targetType) : targetType; - return constraint == targetType; + return constraint == targetType || + (targetType.tag == TypeTags.INTERSECTION && (((BIntersectionType) targetType).effectiveType == constraint)); } private void createNewTypedescInst(BType resolveType, Location position) { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index b60244a23e60..6d9554442760 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -904,9 +904,6 @@ private void createTypedescVariable(BType type, Location pos) { } Name name = generateTypedescVariableName(type); - BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); - BSymbol owner = this.env.scope.owner; - BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); if (type.tag == TypeTags.TYPEREFDESC) { BType referredType = ((BTypeReferenceType) type).referredType; int tag = referredType.tag; @@ -914,6 +911,9 @@ private void createTypedescVariable(BType type, Location pos) { type = referredType; } } + BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol); + BSymbol owner = this.env.scope.owner; + BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL); BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type); typedescList.add(createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, varSymbol)); } From 6fedbfac94a83d988cd2a339d878fa04663a18d6 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Tue, 26 Nov 2024 16:15:56 +0530 Subject: [PATCH 86/87] Update `setRhsOperands` for `NewArray` inst --- .../main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java | 2 +- .../ballerinalang/compiler/bir/model/BIRNonTerminator.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 3d52bfd7e739..1e1b74505de8 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -2501,7 +2501,7 @@ private boolean isMatchingTypeDescSymbol(BSymbol symbol, BType targetType) { // table & readonly q = table [{name: "Jo"}]; // Row type will be Immutable Identifier and there will be no intersection type created for that BType constraint = ((BTypedescType) symbol.type).constraint; - return constraint == targetType || + return targetType == constraint || (targetType.tag == TypeTags.INTERSECTION && (((BIntersectionType) targetType).effectiveType == constraint)); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java index 9cc5628bbf6b..d5978a2dc9bf 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/model/BIRNonTerminator.java @@ -363,6 +363,9 @@ public void setRhsOperands(BIROperand[] operands) { if (typedescOp != null) { typedescOp = operands[i++]; } + if (elementTypedescOp != null) { + elementTypedescOp = operands[i++]; + } sizeOp = operands[i++]; for (BIRListConstructorEntry listValueEntry : values) { listValueEntry.exprOp = operands[i++]; From ff7605e1f40497c72244447984178b0092cec256 Mon Sep 17 00:00:00 2001 From: rdulmina Date: Wed, 27 Nov 2024 16:05:54 +0530 Subject: [PATCH 87/87] Remove referred type matching to getTypeDescVar --- .../ballerinalang/compiler/bir/BIRGen.java | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java index 1e1b74505de8..36df9609b99a 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/BIRGen.java @@ -82,7 +82,6 @@ import org.wso2.ballerinalang.compiler.semantics.model.symbols.SymTag; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType; -import org.wso2.ballerinalang.compiler.semantics.model.types.BIntersectionType; import org.wso2.ballerinalang.compiler.semantics.model.types.BInvokableType; import org.wso2.ballerinalang.compiler.semantics.model.types.BRecordType; import org.wso2.ballerinalang.compiler.semantics.model.types.BTableType; @@ -2428,7 +2427,7 @@ private BIRVariableDcl getTypedescVariable(BType type, Location pos) { return variableDcl; } - variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap, false); + variableDcl = findInGlobalSymbolVarMap(type, this.globalVarMap); if (variableDcl != null && variableDcl.initialized) { return variableDcl; } @@ -2469,8 +2468,7 @@ private BIRVariableDcl findInLocalSymbolVarMap(BType type, Map varMap, - boolean checkImpliedType) { + private BIRVariableDcl findInGlobalSymbolVarMap(BType type, Map varMap) { for (Map.Entry entry : varMap.entrySet()) { BSymbol varSymbol = entry.getKey(); if (isMatchingTypeDescSymbol(varSymbol, type)) { @@ -2491,18 +2489,8 @@ private boolean isMatchingTypeDescSymbol(BSymbol symbol, BType targetType) { if (!symbol.name.value.startsWith(TYPEDESC)) { return false; } - // We need to match with the original type first and then with the effective type because for some cases - // there is no typedesc var with the original intersection type. - // Eg: - // public type Identifier record {| - // string name; - // |}; - // - // table & readonly q = table [{name: "Jo"}]; - // Row type will be Immutable Identifier and there will be no intersection type created for that BType constraint = ((BTypedescType) symbol.type).constraint; - return targetType == constraint || - (targetType.tag == TypeTags.INTERSECTION && (((BIntersectionType) targetType).effectiveType == constraint)); + return targetType == constraint; } private void createNewTypedescInst(BType resolveType, Location position) {