From 5c0b92f933e9a4ec771a330ffae88c04d80f7122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bispo?= Date: Tue, 17 Dec 2024 23:25:55 +0000 Subject: [PATCH] Changes ForStmt.getCond() and .getInc() to return Expr instead of ExprStmt --- .../fe/specs/clang/utils/ChildrenAdapter.java | 38 +++---- Clava-JS/src-api/Joinpoints.ts | 12 +- Clava-JS/src-api/clava/ClavaJoinPoints.ts | 9 +- .../analysis/flow/control/BasicBlockNode.java | 2 +- .../up/fe/specs/clava/ast/stmt/ForStmt.java | 19 ++-- .../transform/loop/LoopAnalysisUtils.java | 60 +++------- .../foriter/ForIterationsExpression.java | 107 +++++++----------- ClavaLaraApi/src-lara/clava/Joinpoints.js | 12 +- .../src-lara/clava/clava/ClavaJoinPoints.js | 5 +- .../test/api/cpp/results/Code2VecTest.js.txt | 56 ++++----- .../resources/clava/weaverspecs/artifacts.xml | 7 +- .../up/fe/specs/clava/weaver/CxxWeaver.json | 26 ++++- .../weaver/abstracts/joinpoints/ACilkFor.java | 10 ++ .../weaver/abstracts/joinpoints/ALoop.java | 33 +++++- .../clava/weaver/importable/AstFactory.java | 8 +- .../clava/weaver/joinpoints/CxxLoop.java | 19 +++- 16 files changed, 216 insertions(+), 207 deletions(-) diff --git a/ClangAstParser/src/pt/up/fe/specs/clang/utils/ChildrenAdapter.java b/ClangAstParser/src/pt/up/fe/specs/clang/utils/ChildrenAdapter.java index 99c89c3b0..6e4772a7f 100644 --- a/ClangAstParser/src/pt/up/fe/specs/clang/utils/ChildrenAdapter.java +++ b/ClangAstParser/src/pt/up/fe/specs/clang/utils/ChildrenAdapter.java @@ -1,11 +1,11 @@ /** * Copyright 2018 SPeCS. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. under the License. @@ -13,30 +13,12 @@ package pt.up.fe.specs.clang.utils; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.function.BiFunction; - import com.google.common.base.Preconditions; - import pt.up.fe.specs.clava.ClavaNode; import pt.up.fe.specs.clava.ast.decl.NullDecl; import pt.up.fe.specs.clava.ast.decl.VarDecl; import pt.up.fe.specs.clava.ast.expr.Expr; -import pt.up.fe.specs.clava.ast.stmt.CXXCatchStmt; -import pt.up.fe.specs.clava.ast.stmt.CXXForRangeStmt; -import pt.up.fe.specs.clava.ast.stmt.CXXTryStmt; -import pt.up.fe.specs.clava.ast.stmt.CaseStmt; -import pt.up.fe.specs.clava.ast.stmt.CompoundStmt; -import pt.up.fe.specs.clava.ast.stmt.DeclStmt; -import pt.up.fe.specs.clava.ast.stmt.DefaultStmt; -import pt.up.fe.specs.clava.ast.stmt.DoStmt; -import pt.up.fe.specs.clava.ast.stmt.ForStmt; -import pt.up.fe.specs.clava.ast.stmt.IfStmt; -import pt.up.fe.specs.clava.ast.stmt.LabelStmt; -import pt.up.fe.specs.clava.ast.stmt.Stmt; -import pt.up.fe.specs.clava.ast.stmt.WhileStmt; +import pt.up.fe.specs.clava.ast.stmt.*; import pt.up.fe.specs.clava.context.ClavaContext; import pt.up.fe.specs.clava.utils.NullNode; import pt.up.fe.specs.util.SpecsCheck; @@ -45,6 +27,11 @@ import pt.up.fe.specs.util.exceptions.CaseNotDefinedException; import pt.up.fe.specs.util.exceptions.NotImplementedException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.BiFunction; + public class ChildrenAdapter { private final ClavaContext context; @@ -58,6 +45,7 @@ public ChildrenAdapter(ClavaContext context) { // } private final static ClassMap, ClavaContext, List>> CHILDREN_ADAPTERS; + static { CHILDREN_ADAPTERS = new ClassMap<>((list, context) -> list); CHILDREN_ADAPTERS.put(IfStmt.class, ChildrenAdapter::adaptIfStmt); @@ -110,7 +98,7 @@ public List adaptChildren(ClavaNode node, List children) { /** * Checks if the given Clava node is an instance of the expected class. Throws an exception if it is not. - * + * * @param * @param node * @param expectedClass @@ -152,8 +140,8 @@ private static List adaptForStmt(List children, ClavaConte List adaptedChildren = new ArrayList<>(children.size()); adaptedChildren.add(toStmt(check(children.get(0), STMT_OR_EXPR), context)); - adaptedChildren.add(toStmt(check(children.get(1), Expr.class), context)); - adaptedChildren.add(toStmt(check(children.get(2), Expr.class), context)); + adaptedChildren.add(check(children.get(1), Expr.class)); + adaptedChildren.add(check(children.get(2), Expr.class)); adaptedChildren.add(toCompoundStmt(check(children.get(3), STMT_OR_EXPR), false, context)); adaptedChildren.add(check(children.get(4), OPTIONAL_VARDECL)); diff --git a/Clava-JS/src-api/Joinpoints.ts b/Clava-JS/src-api/Joinpoints.ts index 7f33461b0..adbf33fa3 100644 --- a/Clava-JS/src-api/Joinpoints.ts +++ b/Clava-JS/src-api/Joinpoints.ts @@ -2105,11 +2105,11 @@ export class Loop extends Statement { get body(): Scope { return wrapJoinPoint(this._javaObject.getBody()) } set body(value: Scope) { this._javaObject.setBody(unwrapJoinPoint(value)); } /** - * The statement of the loop condition + * (deprecated) The expression of the loop condition wrapped around a new exprStmt */ get cond(): Statement { return wrapJoinPoint(this._javaObject.getCond()) } /** - * The statement of the loop condition + * (deprecated) The expression of the loop condition wrapped around a new exprStmt */ set cond(value: string) { this._javaObject.setCond(unwrapJoinPoint(value)); } /** @@ -2162,13 +2162,17 @@ export class Loop extends Statement { get nestedLevel(): number { return wrapJoinPoint(this._javaObject.getNestedLevel()) } get rank(): number[] { return wrapJoinPoint(this._javaObject.getRank()) } /** - * The statement of the loop step + * (deprecated) The expression of the loop step wrapped around a new exprStmt */ get step(): ExprStmt { return wrapJoinPoint(this._javaObject.getStep()) } /** - * The statement of the loop step + * (deprecated) The expression of the loop step wrapped around a new exprStmt */ set step(value: string) { this._javaObject.setStep(unwrapJoinPoint(value)); } + /** + * The expression of the loop step + */ + get stepExpr(): Expression { return wrapJoinPoint(this._javaObject.getStepExpr()) } /** * The expression of the iteration step */ diff --git a/Clava-JS/src-api/clava/ClavaJoinPoints.ts b/Clava-JS/src-api/clava/ClavaJoinPoints.ts index 052383566..fbc64fdfe 100644 --- a/Clava-JS/src-api/clava/ClavaJoinPoints.ts +++ b/Clava-JS/src-api/clava/ClavaJoinPoints.ts @@ -759,16 +759,11 @@ export default class ClavaJoinPoints { $body = ClavaJoinPoints.stmtLiteral($body); } - // cond and inc must be wrapped around an ExprStmt - let $condExprStmt = $condition != undefined ? this.exprStmt($condition) : undefined; - let $incExprStmt = $inc != undefined ? this.exprStmt($inc) : undefined; - - return wrapJoinPoint( ClavaJavaTypes.AstFactory.forStmt( unwrapJoinPoint($init), - unwrapJoinPoint($condExprStmt), - unwrapJoinPoint($incExprStmt), + unwrapJoinPoint($condition), + unwrapJoinPoint($inc), unwrapJoinPoint($body) ) ); diff --git a/ClavaAst/src/pt/up/fe/specs/clava/analysis/flow/control/BasicBlockNode.java b/ClavaAst/src/pt/up/fe/specs/clava/analysis/flow/control/BasicBlockNode.java index c6117d9dd..3bb1dcccc 100644 --- a/ClavaAst/src/pt/up/fe/specs/clava/analysis/flow/control/BasicBlockNode.java +++ b/ClavaAst/src/pt/up/fe/specs/clava/analysis/flow/control/BasicBlockNode.java @@ -129,7 +129,7 @@ private String getBbCode(Stmt stmt) { builder.append("for").append("("); builder.append(f.getInit().map(init -> init.getCode()).orElse(";")); - builder.append(f.getCond().map(init -> " " + init.getCode()).orElse(";")); + builder.append(f.getCond().map(init -> " " + init.getCode()).orElse("")).append(";"); String incCode = f.getInc().map(init -> " " + init.getCode()).orElse(""); if (incCode.endsWith(";")) { incCode = incCode.substring(0, incCode.length() - 1); diff --git a/ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/ForStmt.java b/ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/ForStmt.java index 3ba81ce90..4d0c6f222 100644 --- a/ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/ForStmt.java +++ b/ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/ForStmt.java @@ -45,12 +45,12 @@ public Optional getInit() { return getOptionalChild(Stmt.class, 0); } - public Optional getCond() { - return getOptionalChild(ExprStmt.class, 1); + public Optional getCond() { + return getOptionalChild(Expr.class, 1); } - public Optional getInc() { - return getOptionalChild(ExprStmt.class, 2); + public Optional getInc() { + return getOptionalChild(Expr.class, 2); } @Override @@ -87,15 +87,15 @@ protected String getCode(String forKeyword) { // System.out.println("COND CODE: " + getCond().map(cond -> " " + cond.getCode()).orElse(";")); // System.out.println("VARDECL CODE: " + condVar.map(var -> var.getCode()).orElse("")); - var condCode = condVar.map(var -> var.getCode() + ";") - .orElse(getCond().map(cond -> " " + cond.getCode()).orElse(";")); + var condCode = condVar.map(var -> var.getCode()) + .orElse(getCond().map(cond -> " " + cond.getCode()).orElse("")) + ";"; code.append(condCode); // code.append(getCond().map(cond -> " " + cond.getCode()).orElse(";")); // Get 'inc' code String incCode = getInc() - .map(init -> " " + init.getExpr().getCode()) + .map(init -> " " + init.getCode()) .orElse(""); code.append(incCode); @@ -136,7 +136,6 @@ public void setInc(LiteralStmt literalStmt) { public Optional getCondOperator() { return getCond() - .map(cond -> cond.getChild(0)) .filter(BinaryOperator.class::isInstance) .map(BinaryOperator.class::cast); @@ -259,7 +258,7 @@ private Optional getInitValueExpr(ClavaNode initExpr) { * Supports values for increment expressions of Canonical Loop Forms as defined by the OpenMP standard. */ public Optional getStepValueExpr() { - return getInc().flatMap(inc -> getStepValueExpr(inc.getChild(0))); + return getInc().flatMap(inc -> getStepValueExpr(inc)); } private Optional getStepValueExpr(ClavaNode incExpr) { @@ -318,6 +317,6 @@ public Optional getIterationsExpr() { @Override public Optional getStmtCondExpr() { - return getCond().map(ExprStmt::getExpr); + return getCond(); } } \ No newline at end of file diff --git a/ClavaAst/src/pt/up/fe/specs/clava/transform/loop/LoopAnalysisUtils.java b/ClavaAst/src/pt/up/fe/specs/clava/transform/loop/LoopAnalysisUtils.java index a4f0a320b..7bc7f427c 100644 --- a/ClavaAst/src/pt/up/fe/specs/clava/transform/loop/LoopAnalysisUtils.java +++ b/ClavaAst/src/pt/up/fe/specs/clava/transform/loop/LoopAnalysisUtils.java @@ -1,11 +1,11 @@ /** * Copyright 2017 SPeCS. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. under the License. @@ -13,38 +13,26 @@ package pt.up.fe.specs.clava.transform.loop; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - import pt.up.fe.specs.clava.ClavaNode; import pt.up.fe.specs.clava.ast.decl.Decl; import pt.up.fe.specs.clava.ast.decl.VarDecl; -import pt.up.fe.specs.clava.ast.expr.BinaryOperator; -import pt.up.fe.specs.clava.ast.expr.DeclRefExpr; -import pt.up.fe.specs.clava.ast.expr.Expr; -import pt.up.fe.specs.clava.ast.expr.Literal; -import pt.up.fe.specs.clava.ast.expr.UnaryOperator; +import pt.up.fe.specs.clava.ast.expr.*; import pt.up.fe.specs.clava.ast.expr.enums.BinaryOperatorKind; import pt.up.fe.specs.clava.ast.expr.enums.ExprUse; import pt.up.fe.specs.clava.ast.expr.enums.UnaryOperatorKind; -import pt.up.fe.specs.clava.ast.stmt.BreakStmt; -import pt.up.fe.specs.clava.ast.stmt.ContinueStmt; -import pt.up.fe.specs.clava.ast.stmt.DeclStmt; -import pt.up.fe.specs.clava.ast.stmt.ExprStmt; -import pt.up.fe.specs.clava.ast.stmt.ForStmt; -import pt.up.fe.specs.clava.ast.stmt.LoopStmt; -import pt.up.fe.specs.clava.ast.stmt.ReturnStmt; -import pt.up.fe.specs.clava.ast.stmt.Stmt; +import pt.up.fe.specs.clava.ast.stmt.*; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; public class LoopAnalysisUtils { /** * Checks if the for loop has init, cond and inc statements. * - * @param forLoop - * the loop to test + * @param forLoop the loop to test * @return true if it has all the elements, false otherwise */ static boolean hasHeader(ForStmt forLoop) { @@ -131,15 +119,7 @@ public static boolean hasSimpleCond(ForStmt targetFor) { return false; } - // cond is a binary expression - Stmt cond = targetFor.getCond().get(); - - if (!(cond instanceof ExprStmt)) { - - return false; - } - - Expr expr = ((ExprStmt) cond).getExpr(); + Expr expr = targetFor.getCond().get(); if (!(expr instanceof BinaryOperator)) { @@ -179,16 +159,8 @@ public static boolean hasSimpleInc(ForStmt targetFor) { return false; } - // inc has to be an expression statement - Stmt inc = targetFor.getInc().get(); - - if (!(inc instanceof ExprStmt)) { - - return false; - } - // the expression needs to be a unary or a binary operation - Expr expr = ((ExprStmt) inc).getExpr(); + Expr expr = targetFor.getInc().get(); if (!(expr instanceof UnaryOperator || expr instanceof BinaryOperator)) { @@ -220,12 +192,12 @@ public static boolean hasSimpleInc(ForStmt targetFor) { public static List getControlVarNames(ForStmt targetFor) { - Stmt inc = targetFor.getInc().orElse(null); + var inc = targetFor.getInc().orElse(null); if (inc == null) { return Collections.emptyList(); } - List controlVars = inc.getDescendants(DeclRefExpr.class).stream() + List controlVars = inc.getDescendantsAndSelf(DeclRefExpr.class).stream() .filter(d -> d.use() == ExprUse.READWRITE || d.use() == ExprUse.WRITE).map(DeclRefExpr::getRefName) .collect(Collectors.toList()); @@ -274,7 +246,7 @@ public static Optional getUpperBound(ForStmt targetFor) { return Optional.empty(); } - BinaryOperator comparison = (BinaryOperator) ((ExprStmt) targetFor.getCond().get()).getExpr(); + BinaryOperator comparison = (BinaryOperator) targetFor.getCond().get(); return Optional.of(comparison.getRhs()); diff --git a/ClavaAst/src/pt/up/fe/specs/clava/utils/foriter/ForIterationsExpression.java b/ClavaAst/src/pt/up/fe/specs/clava/utils/foriter/ForIterationsExpression.java index 9ec5ab26c..f13d30d63 100644 --- a/ClavaAst/src/pt/up/fe/specs/clava/utils/foriter/ForIterationsExpression.java +++ b/ClavaAst/src/pt/up/fe/specs/clava/utils/foriter/ForIterationsExpression.java @@ -1,11 +1,11 @@ /** * Copyright 2020 SPeCS. - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. under the License. @@ -13,25 +13,19 @@ package pt.up.fe.specs.clava.utils.foriter; -import java.util.Optional; - import pt.up.fe.specs.clava.ClavaLog; import pt.up.fe.specs.clava.ClavaNodes; -import pt.up.fe.specs.clava.ast.expr.BinaryOperator; -import pt.up.fe.specs.clava.ast.expr.DeclRefExpr; -import pt.up.fe.specs.clava.ast.expr.Expr; -import pt.up.fe.specs.clava.ast.expr.Literal; -import pt.up.fe.specs.clava.ast.expr.UnaryOperator; +import pt.up.fe.specs.clava.ast.expr.*; import pt.up.fe.specs.clava.ast.expr.enums.BinaryOperatorKind; import pt.up.fe.specs.clava.ast.expr.enums.UnaryOperatorKind; -import pt.up.fe.specs.clava.ast.stmt.ExprStmt; import pt.up.fe.specs.clava.ast.stmt.ForStmt; -import pt.up.fe.specs.clava.ast.stmt.Stmt; import pt.up.fe.specs.clava.ast.type.BuiltinType; import pt.up.fe.specs.clava.ast.type.Type; import pt.up.fe.specs.clava.utils.Typable; import pt.up.fe.specs.util.exceptions.NotImplementedException; +import java.util.Optional; + public class ForIterationsExpression { // private final VarDecl iterVar; @@ -42,7 +36,7 @@ public class ForIterationsExpression { private final ConditionData conditionData; public ForIterationsExpression(String iterVarName, Type iterVarType, Expr initExpr, - StepData stepData, ConditionData conditionData) { + StepData stepData, ConditionData conditionData) { // this.iterVar = iterVar; this.iterVarName = iterVarName; this.iterVarType = iterVarType; @@ -119,14 +113,14 @@ private Expr getBoundary(boolean isStart) { Expr boundary = null; switch (stepData.getDirection()) { - case INC: - boundary = isStart ? initExpr : conditionData.getValue(); - break; - case DEC: - boundary = isStart ? conditionData.getValue() : initExpr; - break; - default: - throw new NotImplementedException(stepData.getDirection()); + case INC: + boundary = isStart ? initExpr : conditionData.getValue(); + break; + case DEC: + boundary = isStart ? conditionData.getValue() : initExpr; + break; + default: + throw new NotImplementedException(stepData.getDirection()); } // Add parenthesis, if necessary @@ -175,7 +169,7 @@ public static Optional newInstance(ForStmt forStmt) { } // Get step value and step direction - var stepData = forStmt.getInc().flatMap(incStmt -> getStepData(incStmt, iterVarName)).orElse(null); + var stepData = forStmt.getInc().flatMap(incExpr -> getStepData(incExpr, iterVarName)).orElse(null); if (stepData == null) { ClavaLog.debug( "ForIterationsExpression: could not determine step data of 'for' at " + forStmt.getLocation()); @@ -209,18 +203,18 @@ private static boolean verify(ForIterationsExpression forIterations) { // LT and LE should only be used with INC, GT and GE should only be used with DEC switch (forIterations.getStepData().getDirection()) { - case INC: - if (condRelation == ForCondRelation.GE || condRelation == ForCondRelation.GT) { - ClavaLog.debug(() -> "ForIterationsExpression: cannot use > or >= in condition when step increases"); - return false; - } - break; - case DEC: - if (condRelation == ForCondRelation.LE || condRelation == ForCondRelation.LT) { - ClavaLog.debug(() -> "ForIterationsExpression: cannot use < or <= in condition when step decreases"); - return false; - } - break; + case INC: + if (condRelation == ForCondRelation.GE || condRelation == ForCondRelation.GT) { + ClavaLog.debug(() -> "ForIterationsExpression: cannot use > or >= in condition when step increases"); + return false; + } + break; + case DEC: + if (condRelation == ForCondRelation.LE || condRelation == ForCondRelation.LT) { + ClavaLog.debug(() -> "ForIterationsExpression: cannot use < or <= in condition when step decreases"); + return false; + } + break; } // LE and GE are only supported when iteration variable in an integer @@ -249,15 +243,8 @@ private static boolean verify(ForIterationsExpression forIterations) { return true; } - private static Optional getConditionData(Stmt conditionStmt, String iterVarName) { + private static Optional getConditionData(Expr condExpr, String iterVarName) { - // Only supports ExprStmt - if (!(conditionStmt instanceof ExprStmt)) { - ClavaLog.debug(() -> "ForIterationsExpression: cond statement is not an ExprStmt, " + conditionStmt); - return Optional.empty(); - } - - var condExpr = ((ExprStmt) conditionStmt).getExpr(); if (!(condExpr instanceof BinaryOperator)) { ClavaLog.debug(() -> "ForIterationsExpression: cond expr is not a binary operator, " + condExpr); @@ -285,19 +272,19 @@ private static Optional getConditionData(Stmt conditionStmt, Stri private static ForCondRelation getCondRelation(BinaryOperatorKind op, int iterVarIndex) { switch (op) { - case LT: - return iterVarIndex == 0 ? ForCondRelation.LT : ForCondRelation.GT; - case GT: - return iterVarIndex == 0 ? ForCondRelation.GT : ForCondRelation.LT; - case LE: - return iterVarIndex == 0 ? ForCondRelation.LE : ForCondRelation.GE; - case GE: - return iterVarIndex == 0 ? ForCondRelation.GE : ForCondRelation.LE; - default: - ClavaLog.debug( - () -> "ForIterationsExpression: unsupported binary operator in condition expression, " - + op.getOpString()); - return null; + case LT: + return iterVarIndex == 0 ? ForCondRelation.LT : ForCondRelation.GT; + case GT: + return iterVarIndex == 0 ? ForCondRelation.GT : ForCondRelation.LT; + case LE: + return iterVarIndex == 0 ? ForCondRelation.LE : ForCondRelation.GE; + case GE: + return iterVarIndex == 0 ? ForCondRelation.GE : ForCondRelation.LE; + default: + ClavaLog.debug( + () -> "ForIterationsExpression: unsupported binary operator in condition expression, " + + op.getOpString()); + return null; } } @@ -333,15 +320,7 @@ private static int getVarIndex(String varName, BinaryOperator expr) { return -1; } - private static Optional getStepData(Stmt incStmt, String iterVarName) { - - // Only supports ExprStmt - if (!(incStmt instanceof ExprStmt)) { - ClavaLog.debug(() -> "ForIterationsExpression: step statement is not an ExprStmt, " + incStmt); - return Optional.empty(); - } - - var incExpr = ((ExprStmt) incStmt).getExpr(); + private static Optional getStepData(Expr incExpr, String iterVarName) { // var += incr // var -= incr diff --git a/ClavaLaraApi/src-lara/clava/Joinpoints.js b/ClavaLaraApi/src-lara/clava/Joinpoints.js index 91828b6c5..f97853f03 100644 --- a/ClavaLaraApi/src-lara/clava/Joinpoints.js +++ b/ClavaLaraApi/src-lara/clava/Joinpoints.js @@ -1807,11 +1807,11 @@ export class Loop extends Statement { get body() { return wrapJoinPoint(this._javaObject.getBody()); } set body(value) { this._javaObject.setBody(unwrapJoinPoint(value)); } /** - * The statement of the loop condition + * (deprecated) The expression of the loop condition wrapped around a new exprStmt */ get cond() { return wrapJoinPoint(this._javaObject.getCond()); } /** - * The statement of the loop condition + * (deprecated) The expression of the loop condition wrapped around a new exprStmt */ set cond(value) { this._javaObject.setCond(unwrapJoinPoint(value)); } /** @@ -1864,13 +1864,17 @@ export class Loop extends Statement { get nestedLevel() { return wrapJoinPoint(this._javaObject.getNestedLevel()); } get rank() { return wrapJoinPoint(this._javaObject.getRank()); } /** - * The statement of the loop step + * (deprecated) The expression of the loop step wrapped around a new exprStmt */ get step() { return wrapJoinPoint(this._javaObject.getStep()); } /** - * The statement of the loop step + * (deprecated) The expression of the loop step wrapped around a new exprStmt */ set step(value) { this._javaObject.setStep(unwrapJoinPoint(value)); } + /** + * The expression of the loop step + */ + get stepExpr() { return wrapJoinPoint(this._javaObject.getStepExpr()); } /** * The expression of the iteration step */ diff --git a/ClavaLaraApi/src-lara/clava/clava/ClavaJoinPoints.js b/ClavaLaraApi/src-lara/clava/clava/ClavaJoinPoints.js index 40635fad1..25860ed88 100644 --- a/ClavaLaraApi/src-lara/clava/clava/ClavaJoinPoints.js +++ b/ClavaLaraApi/src-lara/clava/clava/ClavaJoinPoints.js @@ -403,10 +403,7 @@ export default class ClavaJoinPoints { if (typeof $body === "string") { $body = ClavaJoinPoints.stmtLiteral($body); } - // cond and inc must be wrapped around an ExprStmt - let $condExprStmt = $condition != undefined ? this.exprStmt($condition) : undefined; - let $incExprStmt = $inc != undefined ? this.exprStmt($inc) : undefined; - return wrapJoinPoint(ClavaJavaTypes.AstFactory.forStmt(unwrapJoinPoint($init), unwrapJoinPoint($condExprStmt), unwrapJoinPoint($incExprStmt), unwrapJoinPoint($body))); + return wrapJoinPoint(ClavaJavaTypes.AstFactory.forStmt(unwrapJoinPoint($init), unwrapJoinPoint($condition), unwrapJoinPoint($inc), unwrapJoinPoint($body))); } static whileStmt($condition, $body) { if (typeof $condition === "string") { diff --git a/ClavaWeaver/resources/clava/test/api/cpp/results/Code2VecTest.js.txt b/ClavaWeaver/resources/clava/test/api/cpp/results/Code2VecTest.js.txt index a62178157..0bffa663f 100644 --- a/ClavaWeaver/resources/clava/test/api/cpp/results/Code2VecTest.js.txt +++ b/ClavaWeaver/resources/clava/test/api/cpp/results/Code2VecTest.js.txt @@ -1,55 +1,55 @@ Warning: using laraImport() for file 'JoinPointsCommonPath.js', however it does not define a variable or class 'JoinPointsCommonPath' -decl (up) file (down) decl +decl (up) file (down) decl __________________________________ -decl (up) file (down) function (down) param +decl (up) file (down) function (down) param __________________________________ -decl (up) file (down) function (down) stmt (down) stmt (down) varDecl (down) expr +decl (up) file (down) function (down) stmt (down) stmt (down) varDecl (down) expr __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) varDecl (down) expr +decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) varDecl (down) expr __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) binary (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) binary (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) binary (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) binary (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) expr (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) expr (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) stmt (down) expr (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) stmt (down) expr (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) stmt (down) varRef +decl (up) file (down) function (down) stmt (down) stmt (down) varRef __________________________________ -decl (up) file (down) decl +decl (up) file (down) decl __________________________________ -decl (up) file (down) function (down) param +decl (up) file (down) function (down) param __________________________________ -decl (up) file (down) function (down) stmt (down) stmt (down) varDecl (down) expr +decl (up) file (down) function (down) stmt (down) stmt (down) varDecl (down) expr __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) varDecl (down) expr +decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) varDecl (down) expr __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) binary (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) binary (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) binary (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) binary (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) expr (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) expr (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) stmt (down) expr (down) varRef +decl (up) file (down) function (down) stmt (down) loop (down) stmt (down) stmt (down) expr (down) varRef __________________________________ -decl (up) file (down) function (down) stmt (down) stmt (down) varRef +decl (up) file (down) function (down) stmt (down) stmt (down) varRef __________________________________ -param (up) function (down) stmt (down) stmt (down) varDecl (down) expr +param (up) function (down) stmt (down) stmt (down) varDecl (down) expr __________________________________ -param (up) function (down) stmt (down) loop (down) stmt (down) varDecl (down) expr +param (up) function (down) stmt (down) loop (down) stmt (down) varDecl (down) expr __________________________________ -param (up) function (down) stmt (down) loop (down) stmt (down) binary (down) varRef +param (up) function (down) stmt (down) loop (down) binary (down) varRef __________________________________ -param (up) function (down) stmt (down) loop (down) stmt (down) binary (down) varRef +param (up) function (down) stmt (down) loop (down) binary (down) varRef __________________________________ -param (up) function (down) stmt (down) loop (down) stmt (down) expr (down) varRef +param (up) function (down) stmt (down) loop (down) expr (down) varRef __________________________________ -param (up) function (down) stmt (down) loop (down) stmt (down) stmt (down) expr (down) varRef +param (up) function (down) stmt (down) loop (down) stmt (down) stmt (down) expr (down) varRef __________________________________ -param (up) function (down) stmt (down) stmt (down) varRef +param (up) function (down) stmt (down) stmt (down) varRef __________________________________ -varRef (up) binary (down) varRef -__________________________________ -varRef (up) binary (down) varRef +varRef (up) binary (down) varRef __________________________________ +varRef (up) binary (down) varRef +__________________________________ \ No newline at end of file diff --git a/ClavaWeaver/resources/clava/weaverspecs/artifacts.xml b/ClavaWeaver/resources/clava/weaverspecs/artifacts.xml index b6e867a8a..21d0f368c 100644 --- a/ClavaWeaver/resources/clava/weaverspecs/artifacts.xml +++ b/ClavaWeaver/resources/clava/weaverspecs/artifacts.xml @@ -371,9 +371,12 @@ https://docs.google.com/document/d/1uPrvuVBXHSbjDTfehpEeLDz9hgIr8EuJJJvBc5A70rs/ tooltip="The expression of the first value of the control variable (e.g. '0' in 'size_t i = 0;')"> - + - + + diff --git a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/CxxWeaver.json b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/CxxWeaver.json index 7c693e351..2df86bdc7 100644 --- a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/CxxWeaver.json +++ b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/CxxWeaver.json @@ -15683,7 +15683,7 @@ }, { "type": "attribute", - "tooltip": "The statement of the loop condition", + "tooltip": "(deprecated) The expression of the loop condition wrapped around a new exprStmt", "children": [ { "type": "statement", @@ -15840,13 +15840,22 @@ }, { "type": "attribute", - "tooltip": "The statement of the loop step", + "tooltip": "(deprecated) The expression of the loop step wrapped around a new exprStmt", "children": [ { "type": "exprStmt", "name": "step" }] }, + { + "type": "attribute", + "tooltip": "The expression of the loop step", + "children": [ + { + "type": "expression", + "name": "stepExpr" + }] + }, { "type": "attribute", "tooltip": "The expression of the iteration step", @@ -53475,7 +53484,7 @@ }, { "type": "attribute", - "tooltip": "The statement of the loop condition", + "tooltip": "(deprecated) The expression of the loop condition wrapped around a new exprStmt", "children": [ { "type": "statement", @@ -53632,13 +53641,22 @@ }, { "type": "attribute", - "tooltip": "The statement of the loop step", + "tooltip": "(deprecated) The expression of the loop step wrapped around a new exprStmt", "children": [ { "type": "exprStmt", "name": "step" }] }, + { + "type": "attribute", + "tooltip": "The expression of the loop step", + "children": [ + { + "type": "expression", + "name": "stepExpr" + }] + }, { "type": "attribute", "tooltip": "The expression of the iteration step", diff --git a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ACilkFor.java b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ACilkFor.java index 929300782..37fa40af8 100644 --- a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ACilkFor.java +++ b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ACilkFor.java @@ -169,6 +169,15 @@ public AExprStmt getStepImpl() { return this.aLoop.getStepImpl(); } + /** + * Get value on attribute stepExpr + * @return the attribute's value + */ + @Override + public AExpression getStepExprImpl() { + return this.aLoop.getStepExprImpl(); + } + /** * Get value on attribute endValue * @return the attribute's value @@ -1570,6 +1579,7 @@ protected enum CilkForAttributes { COND("cond"), CONDEXPR("condExpr"), STEP("step"), + STEPEXPR("stepExpr"), ENDVALUE("endValue"), STEPVALUE("stepValue"), HASCONDRELATION("hasCondRelation"), diff --git a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ALoop.java b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ALoop.java index 9381c3a4b..9704788b5 100644 --- a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ALoop.java +++ b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/abstracts/joinpoints/ALoop.java @@ -388,12 +388,12 @@ public void defInitValueImpl(String value) { } /** - * The statement of the loop condition + * (deprecated) The expression of the loop condition wrapped around a new exprStmt */ public abstract AStatement getCondImpl(); /** - * The statement of the loop condition + * (deprecated) The expression of the loop condition wrapped around a new exprStmt */ public final Object getCond() { try { @@ -434,12 +434,12 @@ public final Object getCondExpr() { } /** - * The statement of the loop step + * (deprecated) The expression of the loop step wrapped around a new exprStmt */ public abstract AExprStmt getStepImpl(); /** - * The statement of the loop step + * (deprecated) The expression of the loop step wrapped around a new exprStmt */ public final Object getStep() { try { @@ -456,6 +456,29 @@ public final Object getStep() { } } + /** + * The expression of the loop step + */ + public abstract AExpression getStepExprImpl(); + + /** + * The expression of the loop step + */ + public final Object getStepExpr() { + try { + if(hasListeners()) { + eventTrigger().triggerAttribute(Stage.BEGIN, this, "stepExpr", Optional.empty()); + } + AExpression result = this.getStepExprImpl(); + if(hasListeners()) { + eventTrigger().triggerAttribute(Stage.END, this, "stepExpr", Optional.ofNullable(result)); + } + return result!=null?result:getUndefinedValue(); + } catch(Exception e) { + throw new AttributeException(get_class(), "stepExpr", e); + } + } + /** * The expression of the last value of the control variable (e.g. 'length' in 'i < length;') */ @@ -2046,6 +2069,7 @@ protected void fillWithAttributes(List attributes) { attributes.add("cond"); attributes.add("condExpr"); attributes.add("step"); + attributes.add("stepExpr"); attributes.add("endValue"); attributes.add("stepValue"); attributes.add("hasCondRelation"); @@ -2125,6 +2149,7 @@ protected enum LoopAttributes { COND("cond"), CONDEXPR("condExpr"), STEP("step"), + STEPEXPR("stepExpr"), ENDVALUE("endValue"), STEPVALUE("stepValue"), HASCONDRELATION("hasCondRelation"), diff --git a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/importable/AstFactory.java b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/importable/AstFactory.java index af2d6b5ee..d4730ea98 100644 --- a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/importable/AstFactory.java +++ b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/importable/AstFactory.java @@ -721,19 +721,19 @@ public static AAccessSpecifier accessSpecifier(String accessSpecifierString) { return CxxJoinpoints.create(accessSpecifierDecl, AAccessSpecifier.class); } - public static ALoop forStmt(AStatement init, AExprStmt condition, AExprStmt inc, AStatement body) { + public static ALoop forStmt(AStatement init, AExpression condition, AExpression inc, AStatement body) { // If null, create NullStmt var initStmt = init != null ? (Stmt) init.getNode() : CxxWeaver.getFactory().nullStmt(); - var condStmt = condition != null ? (ExprStmt) condition.getNode() : null; - var incStmt = inc != null ? (ExprStmt) inc.getNode() : null; + var condExpr = condition != null ? (Expr) condition.getNode() : null; + var incExpr = inc != null ? (Expr) inc.getNode() : null; var bodyStmt = body != null ? (Stmt) body.getNode() : CxxWeaver.getFactory().nullStmt(); // If body is not a CompoundStmt, make it var compoundStmt = ClavaNodes.toCompoundStmt(bodyStmt); - var forStmt = CxxWeaver.getFactory().forStmt(initStmt, condStmt, incStmt, compoundStmt); + var forStmt = CxxWeaver.getFactory().forStmt(initStmt, condExpr, incExpr, compoundStmt); return CxxJoinpoints.create(forStmt, ALoop.class); } diff --git a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/joinpoints/CxxLoop.java b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/joinpoints/CxxLoop.java index 0678a4699..ac720a4f7 100644 --- a/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/joinpoints/CxxLoop.java +++ b/ClavaWeaver/src/pt/up/fe/specs/clava/weaver/joinpoints/CxxLoop.java @@ -257,7 +257,22 @@ public AExprStmt getStepImpl() { } - return CxxJoinpoints.create(inc, AExprStmt.class); + return CxxJoinpoints.create(getFactory().exprStmt(inc), AExprStmt.class); + } + + @Override + public AExpression getStepExprImpl() { + if (!(loop instanceof ForStmt)) { + return null; + } + + var inc = ((ForStmt) loop).getInc().orElse(null); + if (inc == null) { + return null; + + } + + return CxxJoinpoints.create(inc, AExpression.class); } @Override @@ -340,7 +355,7 @@ private void convertToWhile() { // WhileStmt whileStmt = ClavaNodeFactory.whileStmt(loop.getInfo(), ((ForStmt) loop).getCond().orElse(null), // loop.getBody()); Stmt cond = ((ForStmt) loop).getCond() - .map(Stmt.class::cast) + .map(expr -> (Stmt) getFactory().exprStmt(expr)) .orElse(CxxWeaver.getFactory().nullStmt()); WhileStmt whileStmt = CxxWeaver.getFactory().whileStmt(cond, loop.getBody());