Skip to content

Commit

Permalink
Changes ForStmt.getCond() and .getInc() to return Expr instead of Exp…
Browse files Browse the repository at this point in the history
…rStmt
  • Loading branch information
joaobispo committed Dec 17, 2024
1 parent 6c69795 commit 5c0b92f
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 207 deletions.
38 changes: 13 additions & 25 deletions ClangAstParser/src/pt/up/fe/specs/clang/utils/ChildrenAdapter.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
/**
* Copyright 2018 SPeCS.
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
*/

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;
Expand All @@ -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;
Expand All @@ -58,6 +45,7 @@ public ChildrenAdapter(ClavaContext context) {
// }

private final static ClassMap<ClavaNode, BiFunction<List<ClavaNode>, ClavaContext, List<ClavaNode>>> CHILDREN_ADAPTERS;

static {
CHILDREN_ADAPTERS = new ClassMap<>((list, context) -> list);
CHILDREN_ADAPTERS.put(IfStmt.class, ChildrenAdapter::adaptIfStmt);
Expand Down Expand Up @@ -110,7 +98,7 @@ public List<ClavaNode> adaptChildren(ClavaNode node, List<ClavaNode> children) {

/**
* Checks if the given Clava node is an instance of the expected class. Throws an exception if it is not.
*
*
* @param <T>
* @param node
* @param expectedClass
Expand Down Expand Up @@ -152,8 +140,8 @@ private static List<ClavaNode> adaptForStmt(List<ClavaNode> children, ClavaConte
List<ClavaNode> 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));

Expand Down
12 changes: 8 additions & 4 deletions Clava-JS/src-api/Joinpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)); }
/**
Expand Down Expand Up @@ -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
*/
Expand Down
9 changes: 2 additions & 7 deletions Clava-JS/src-api/clava/ClavaJoinPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/ForStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public Optional<Stmt> getInit() {
return getOptionalChild(Stmt.class, 0);
}

public Optional<ExprStmt> getCond() {
return getOptionalChild(ExprStmt.class, 1);
public Optional<Expr> getCond() {
return getOptionalChild(Expr.class, 1);
}

public Optional<ExprStmt> getInc() {
return getOptionalChild(ExprStmt.class, 2);
public Optional<Expr> getInc() {
return getOptionalChild(Expr.class, 2);
}

@Override
Expand Down Expand Up @@ -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("<no code>"));

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);
Expand Down Expand Up @@ -136,7 +136,6 @@ public void setInc(LiteralStmt literalStmt) {

public Optional<BinaryOperator> getCondOperator() {
return getCond()
.map(cond -> cond.getChild(0))
.filter(BinaryOperator.class::isInstance)
.map(BinaryOperator.class::cast);

Expand Down Expand Up @@ -259,7 +258,7 @@ private Optional<Expr> getInitValueExpr(ClavaNode initExpr) {
* Supports values for increment expressions of Canonical Loop Forms as defined by the OpenMP standard.
*/
public Optional<Expr> getStepValueExpr() {
return getInc().flatMap(inc -> getStepValueExpr(inc.getChild(0)));
return getInc().flatMap(inc -> getStepValueExpr(inc));
}

private Optional<Expr> getStepValueExpr(ClavaNode incExpr) {
Expand Down Expand Up @@ -318,6 +317,6 @@ public Optional<Expr> getIterationsExpr() {

@Override
public Optional<Expr> getStmtCondExpr() {
return getCond().map(ExprStmt::getExpr);
return getCond();
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
/**
* Copyright 2017 SPeCS.
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
*/

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) {
Expand Down Expand Up @@ -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)) {

Expand Down Expand Up @@ -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)) {

Expand Down Expand Up @@ -220,12 +192,12 @@ public static boolean hasSimpleInc(ForStmt targetFor) {

public static List<String> getControlVarNames(ForStmt targetFor) {

Stmt inc = targetFor.getInc().orElse(null);
var inc = targetFor.getInc().orElse(null);
if (inc == null) {
return Collections.emptyList();
}

List<String> controlVars = inc.getDescendants(DeclRefExpr.class).stream()
List<String> controlVars = inc.getDescendantsAndSelf(DeclRefExpr.class).stream()
.filter(d -> d.use() == ExprUse.READWRITE || d.use() == ExprUse.WRITE).map(DeclRefExpr::getRefName)
.collect(Collectors.toList());

Expand Down Expand Up @@ -274,7 +246,7 @@ public static Optional<Expr> 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());

Expand Down
Loading

0 comments on commit 5c0b92f

Please sign in to comment.