From 9753b1e09f0162fbf9ebc62a97c08bb37b3118cb Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Sun, 21 Jul 2024 11:44:27 +0530 Subject: [PATCH] Extract if to switch cases and add functions --- .../compiler/desugar/Desugar.java | 114 ++++++++++-------- .../compiler/parser/BLangNodeBuilder.java | 40 +++--- 2 files changed, 88 insertions(+), 66 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 3a0d57d56708..fbc2f1cfe581 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 @@ -6646,7 +6646,7 @@ private BLangInvocation rewriteXMLAttributeOrElemNameAccess(BLangFieldBasedAcces // Handle element name access. if (fieldName.equals("_")) { return createLanglibXMLInvocation(fieldAccessExpr.pos, XML_INTERNAL_GET_ELEMENT_NAME_NIL_LIFTING, - fieldAccessExpr.expr, new ArrayList<>(), new ArrayList<>()); + fieldAccessExpr.expr, Collections.emptyList(), Collections.emptyList()); } BLangLiteral attributeNameLiteral = createStringLiteral(fieldAccessExpr.field.pos, fieldName); @@ -6654,7 +6654,7 @@ private BLangInvocation rewriteXMLAttributeOrElemNameAccess(BLangFieldBasedAcces args.add(isOptionalAccessToLiteral(fieldAccessExpr)); return createLanglibXMLInvocation(fieldAccessExpr.pos, XML_INTERNAL_GET_ATTRIBUTE, fieldAccessExpr.expr, args, - new ArrayList<>()); + Collections.emptyList()); } private BLangExpression isOptionalAccessToLiteral(BLangFieldBasedAccess fieldAccessExpr) { @@ -8583,7 +8583,7 @@ public void visit(BLangXMLElementAccess xmlElementAccess) { ArrayList filters = expandFilters(xmlElementAccess.filters); BLangInvocation invocationNode = createLanglibXMLInvocation(xmlElementAccess.pos, XML_INTERNAL_GET_ELEMENTS, - xmlElementAccess.expr, new ArrayList<>(), filters); + xmlElementAccess.expr, Collections.emptyList(), filters); result = rewriteExpr(invocationNode); } @@ -8605,8 +8605,8 @@ private ArrayList expandFilters(List fil private BLangInvocation createLanglibXMLInvocation(Location pos, String functionName, BLangExpression invokeOnExpr, - ArrayList args, - ArrayList restArgs) { + List args, + List restArgs) { invokeOnExpr = rewriteExpr(invokeOnExpr); BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode(); @@ -10641,14 +10641,15 @@ private void createMapFunctionForStepExpr(BLangXMLNavigationAccess xmlNavigation } BLangInvocation elements = - createLanglibXMLInvocation(pos, XML_ELEMENTS, xmlNavigation.expr, new ArrayList<>(), - new ArrayList<>()); - BLangInvocation invocationNode = createLanglibXMLInvocation(pos, XML_MAP, elements, args, new ArrayList<>()); + createLanglibXMLInvocation(pos, XML_ELEMENTS, xmlNavigation.expr, Collections.emptyList(), + Collections.emptyList()); + BLangInvocation invocationNode = + createLanglibXMLInvocation(pos, XML_MAP, elements, args, Collections.emptyList()); result = rewriteExpr(invocationNode); } public BLangLambdaFunction createArrowFunctionForNavigation(Location pos, List extensions, - String func, ArrayList filters) { + String func, List filters) { BLangPackage enclPkg = env.enclPkg; PackageID pkgID = enclPkg.packageID; BType xmlType = symTable.xmlType; @@ -10682,12 +10683,12 @@ public BLangLambdaFunction createArrowFunctionForNavigation(Location pos, List(), new ArrayList<>()); + createLanglibXMLInvocation(pos, func, varRef, Collections.emptyList(), Collections.emptyList()); expression = rewriteExpr(expression); if (filters.size() > 0) { - expression = - createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, new ArrayList<>(), filters); + expression = createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, Collections.emptyList(), + filters); expression = rewriteExpr(expression); } @@ -10703,46 +10704,55 @@ public BLangLambdaFunction createArrowFunctionForNavigation(Location pos, List extensions, BLangArrowFunction arrowFunction, BLangExpression expression) { - BType xmlType = symTable.xmlType; for (BLangXMLStepExtend extension : extensions) { - if (extension.getKind() == NodeKind.XML_STEP_INDEXED_EXTEND) { - BLangXMLIndexedStepExtend indexedStepExtend = (BLangXMLIndexedStepExtend) extension; - expression = createIndexBasedAccessNode(pos, indexedStepExtend.indexExpr, expression); - if (indexedStepExtend.indexExpr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) { - BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) indexedStepExtend.indexExpr; - simpleVarRef.symbol.closure = true; - arrowFunction.closureVarSymbols.add(new ClosureVarSymbol(simpleVarRef.symbol, pos)); - } - } else if (extension.getKind() == NodeKind.XML_STEP_FILTER_EXTEND) { - BLangXMLFilterStepExtend filterStepExtend = (BLangXMLFilterStepExtend) extension; - ArrayList filterExtensions = expandFilters(filterStepExtend.filters); - expression = createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, new ArrayList<>(), - filterExtensions); - } else { - BLangXMLMethodCallStepExtend methodCallStepExtend = (BLangXMLMethodCallStepExtend) extension; - BLangInvocation invocation = methodCallStepExtend.invocation; - if (invocation.requiredArgs.size() > 0) { - invocation.requiredArgs.remove(0); - } - for (int i = 0; i < invocation.requiredArgs.size(); i++) { - if (invocation.requiredArgs.get(i).getKind() == NodeKind.SIMPLE_VARIABLE_REF) { - BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) invocation.requiredArgs.get(i); - simpleVarRef.symbol.closure = true; - arrowFunction.closureVarSymbols.add(new ClosureVarSymbol(simpleVarRef.symbol, pos)); - } - } - expression = createLanglibXMLInvocation(pos, invocation.name.value, expression, - (ArrayList) invocation.requiredArgs, - (ArrayList) invocation.restArgs); - expression = rewrite(expression, env); - expression = types.addConversionExprIfRequired(expression, symTable.xmlType); - } - expression.setBType(xmlType); + switch (extension.getKind()) { + case XML_STEP_INDEXED_EXTEND -> + expression = createExpressionForIndexedStepExtend(pos, arrowFunction, expression, + (BLangXMLIndexedStepExtend) extension); + case XML_STEP_FILTER_EXTEND -> expression = + createExpressionForFilterStepExtend(pos, expression, (BLangXMLFilterStepExtend) extension); + case XML_STEP_METHOD_CALL_EXTEND -> + expression = createExpressionForMethodCallStepExtend(pos, arrowFunction, expression, + (BLangXMLMethodCallStepExtend) extension); + default -> throw new IllegalStateException("Invalid xml step extension: " + extension.getKind()); + } + expression.setBType(symTable.xmlType); } - expression.setBType(xmlType); return expression; } + private BLangExpression createExpressionForMethodCallStepExtend(Location pos, BLangArrowFunction arrowFunction, + BLangExpression expression, + BLangXMLMethodCallStepExtend methodCallStepExtend) { + BLangInvocation invocation = methodCallStepExtend.invocation; + if (invocation.requiredArgs.size() > 0) { + invocation.requiredArgs.remove(0); + } + + for (BLangExpression arg: invocation.requiredArgs) { + markAndAddPossibleClosureVarsToArrowFunction(pos, arrowFunction, arg); + } + expression = createLanglibXMLInvocation(pos, invocation.name.value, expression, invocation.requiredArgs, + invocation.restArgs); + expression = rewrite(expression, env); + return types.addConversionExprIfRequired(expression, symTable.xmlType); + } + + private BLangExpression createExpressionForFilterStepExtend(Location pos, BLangExpression expression, + BLangXMLFilterStepExtend filterStepExtend) { + ArrayList filterExtensions = expandFilters(filterStepExtend.filters); + return createLanglibXMLInvocation(pos, XML_INTERNAL_GET_ELEMENTS, expression, Collections.emptyList(), + filterExtensions); + } + + private BLangExpression createExpressionForIndexedStepExtend(Location pos, BLangArrowFunction arrowFunction, + BLangExpression expression, + BLangXMLIndexedStepExtend indexedStepExtend) { + BLangExpression indexExpr = indexedStepExtend.indexExpr; + markAndAddPossibleClosureVarsToArrowFunction(pos, arrowFunction, indexExpr); + return createIndexBasedAccessNode(pos, indexExpr, expression); + } + private BLangIndexBasedAccess createIndexBasedAccessNode(Location pos, BLangExpression indexExpr, BLangExpression expr) { BLangIndexBasedAccess indexBasedAccess = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode(); @@ -10751,4 +10761,14 @@ private BLangIndexBasedAccess createIndexBasedAccessNode(Location pos, BLangExpr indexBasedAccess.expr = expr; return indexBasedAccess; } + + private static void markAndAddPossibleClosureVarsToArrowFunction(Location pos, BLangArrowFunction arrFunction, + BLangExpression expression) { + if (expression.getKind() != NodeKind.SIMPLE_VARIABLE_REF) { + return; + } + BLangSimpleVarRef simpleVarRef = (BLangSimpleVarRef) expression; + simpleVarRef.symbol.closure = true; + arrFunction.closureVarSymbols.add(new ClosureVarSymbol(simpleVarRef.symbol, pos)); + } } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java index e3a348a53ec8..1854b2b48bd9 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java @@ -7086,29 +7086,31 @@ private void setClassQualifiers(NodeList qualifiers, BLangClassDefinition } private List createBLangXMLStepExtends(NodeList nodes, BLangExpression expr) { - List extensions = new ArrayList<>(); + List extensions = new ArrayList<>(nodes.size()); BLangXMLStepExtend curExpr = null; for (Node node : nodes) { Location pos = getPosition(node); - SyntaxKind kind = node.kind(); - if (kind == SyntaxKind.XML_STEP_INDEXED_EXTEND) { - curExpr = - new BLangXMLIndexedStepExtend(pos, - createExpression(((XMLStepIndexedExtendNode) node).expression())); - } else if (kind == SyntaxKind.XML_STEP_METHOD_CALL_EXTEND) { - XMLStepMethodCallExtendNode xmlStepMethodCallExtendNode = (XMLStepMethodCallExtendNode) node; - SimpleNameReferenceNode methodName = xmlStepMethodCallExtendNode.methodName(); - BLangInvocation bLangInvocation = createBLangInvocation(methodName, - xmlStepMethodCallExtendNode.parenthesizedArgList().arguments(), methodName.location(), false); - bLangInvocation.expr = curExpr == null ? expr : curExpr; - curExpr = new BLangXMLMethodCallStepExtend(pos, bLangInvocation); - } else { - XMLNamePatternChainingNode xmlNamePatternChainingNode = (XMLNamePatternChainingNode) node; - List filters = new ArrayList<>(); - for (Node namePattern : xmlNamePatternChainingNode.xmlNamePattern()) { - filters.add(createXMLElementFilter(namePattern)); + switch (node.kind()) { + case XML_STEP_INDEXED_EXTEND -> curExpr = new BLangXMLIndexedStepExtend(pos, + createExpression(((XMLStepIndexedExtendNode) node).expression())); + case XML_STEP_METHOD_CALL_EXTEND -> { + XMLStepMethodCallExtendNode xmlStepMethodCallExtendNode = (XMLStepMethodCallExtendNode) node; + SimpleNameReferenceNode methodName = xmlStepMethodCallExtendNode.methodName(); + BLangInvocation bLangInvocation = createBLangInvocation(methodName, + xmlStepMethodCallExtendNode.parenthesizedArgList().arguments(), methodName.location(), + false); + bLangInvocation.expr = curExpr == null ? expr : curExpr; + curExpr = new BLangXMLMethodCallStepExtend(pos, bLangInvocation); + } + case XML_NAME_PATTERN_CHAIN -> { + XMLNamePatternChainingNode xmlNamePatternChainingNode = (XMLNamePatternChainingNode) node; + List filters = new ArrayList<>(); + for (Node namePattern : xmlNamePatternChainingNode.xmlNamePattern()) { + filters.add(createXMLElementFilter(namePattern)); + } + curExpr = new BLangXMLFilterStepExtend(pos, filters); } - curExpr = new BLangXMLFilterStepExtend(pos, filters); + default -> throw new IllegalStateException("Invalid xml step extension kind: " + node.kind()); } extensions.add(curExpr); }