Skip to content

Commit

Permalink
Adds changes of 5 commits
Browse files Browse the repository at this point in the history
  • Loading branch information
joaobispo committed Dec 18, 2024
1 parent f220eb7 commit 293c183
Show file tree
Hide file tree
Showing 29 changed files with 553 additions and 471 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
18 changes: 13 additions & 5 deletions Clava-JS/src-api/Joinpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2105,13 +2105,17 @@ 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)); }
/**
* The expression of the loop condition
*/
get condExpr(): Expression { return wrapJoinPoint(this._javaObject.getCondExpr()) }
get condRelation(): Relation { return wrapJoinPoint(this._javaObject.getCondRelation()) }
set condRelation(value: Relation) { this._javaObject.setCondRelation(unwrapJoinPoint(value)); }
get controlVar(): string { return wrapJoinPoint(this._javaObject.getControlVar()) }
Expand Down Expand Up @@ -2158,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(): Statement { return wrapJoinPoint(this._javaObject.getStep()) }
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
8 changes: 4 additions & 4 deletions Clava-JS/src-api/clava/ClavaJoinPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,18 +742,18 @@ export default class ClavaJoinPoints {
*/
static forStmt(
$init?: Joinpoints.Statement | string,
$condition?: Joinpoints.Statement | string,
$inc?: Joinpoints.Statement | string,
$condition?: Joinpoints.Expression | string,
$inc?: Joinpoints.Expression | string,
$body?: Joinpoints.Statement | string
): Joinpoints.Loop {
if (typeof $init === "string") {
$init = ClavaJoinPoints.stmtLiteral($init);
}
if (typeof $condition === "string") {
$condition = ClavaJoinPoints.stmtLiteral($condition);
$condition = ClavaJoinPoints.exprLiteral($condition);
}
if (typeof $inc === "string") {
$inc = ClavaJoinPoints.stmtLiteral($inc);
$inc = ClavaJoinPoints.exprLiteral($inc);
}
if (typeof $body === "string") {
$body = ClavaJoinPoints.stmtLiteral($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
21 changes: 12 additions & 9 deletions ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/CXXForRangeStmt.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
/**
* Copyright 2016 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.ast.stmt;

import java.util.Collection;
import java.util.Optional;

import org.suikasoft.jOptions.Interfaces.DataStore;

import pt.up.fe.specs.clava.ClavaNode;
import pt.up.fe.specs.clava.ast.decl.VarDecl;
import pt.up.fe.specs.clava.ast.expr.Expr;

import java.util.Collection;
import java.util.Optional;

/**
* This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (range-declarator :
* range-expression)' or 'for (init-statement range-declarator : range-expression)'.
*
* @author JBispo
*
* @author JBispo
*/
public class CXXForRangeStmt extends LoopStmt {

Expand Down Expand Up @@ -95,4 +93,9 @@ public String getCode() {
public Optional<ClavaNode> getStmtCondition() {
return Optional.of(getCond());
}

@Override
public Optional<Expr> getStmtCondExpr() {
return Optional.of(getCond());
}
}
18 changes: 11 additions & 7 deletions ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/DoStmt.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/**
* 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.ast.stmt;

import java.util.Collection;
import java.util.Optional;

import org.suikasoft.jOptions.Interfaces.DataStore;

import pt.up.fe.specs.clava.ClavaNode;
import pt.up.fe.specs.clava.ast.expr.Expr;

import java.util.Collection;
import java.util.Optional;

public class DoStmt extends LoopStmt {

Expand Down Expand Up @@ -51,4 +51,8 @@ public String getCode() {

}

@Override
public Optional<Expr> getStmtCondExpr() {
return Optional.of(getCondition().getExpr());
}
}
48 changes: 25 additions & 23 deletions ClavaAst/src/pt/up/fe/specs/clava/ast/stmt/ForStmt.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
/**
* Copyright 2016 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.ast.stmt;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

import org.suikasoft.jOptions.Interfaces.DataStore;

import pt.up.fe.specs.clava.ClavaLog;
import pt.up.fe.specs.clava.ClavaNode;
import pt.up.fe.specs.clava.ast.decl.VarDecl;
Expand All @@ -33,6 +27,11 @@
import pt.up.fe.specs.clava.utils.foriter.ForIterationsExpression;
import pt.up.fe.specs.util.treenode.NodeInsertUtils;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;

public class ForStmt extends LoopStmt {

private static final Set<BinaryOperatorKind> RELATIONAL_OPS = EnumSet.of(BinaryOperatorKind.LE,
Expand All @@ -46,12 +45,12 @@ public Optional<Stmt> getInit() {
return getOptionalChild(Stmt.class, 0);
}

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

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

@Override
Expand Down Expand Up @@ -88,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 -> " " + removeSemicolon(init.getCode()))
.map(init -> " " + init.getCode())
.orElse("");

code.append(incCode);
Expand Down Expand Up @@ -137,19 +136,18 @@ public void setInc(LiteralStmt literalStmt) {

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

}

/**
* The value expression of the test relation in the condition of an OpenM canonical loop form.
*
*
* <p>
* The value expression is define if the condition expression is a binary operator with one of the following
* operators: <, <=, >, >=
*
*
* @return
*/
public Optional<Expr> getConditionValueExpr() {
Expand Down Expand Up @@ -177,7 +175,7 @@ public Optional<VarDecl> getInitVar() {

/**
* A ClavaNode that implements Nameable, that represents the iteration variable.
*
*
* @return
*/
public Optional<ClavaNode> getIterationVarNode() {
Expand Down Expand Up @@ -255,12 +253,12 @@ private Optional<Expr> getInitValueExpr(ClavaNode initExpr) {

/**
* The value by which the iteration variable changes each iteration.
*
*
* <p>
* 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 @@ -310,11 +308,15 @@ private Optional<Expr> getStepValueExpr(ClavaNode incExpr) {
}

/**
*
* @return an expression that represents the number of iterations of the loop
*/
@Override
public Optional<Expr> getIterationsExpr() {
return ForIterationsExpression.newInstance(this).flatMap(iter -> iter.getIterationsExpr());
}

@Override
public Optional<Expr> getStmtCondExpr() {
return getCond();
}
}
Loading

0 comments on commit 293c183

Please sign in to comment.