Skip to content

Commit

Permalink
Fixes bug where Java array was being deconstructed on JS
Browse files Browse the repository at this point in the history
- Apparently it was not working in node mode;
  • Loading branch information
joaobispo committed Aug 9, 2024
1 parent 534e6aa commit cb28eb9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 69 deletions.
21 changes: 10 additions & 11 deletions Clava-JS/src-api/clava/ClavaJoinPoints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
unwrapJoinPoint,
wrapJoinPoint,
} from "lara-js/api/LaraJoinPoint.js";
import { unwrapJoinPoint, wrapJoinPoint } from "lara-js/api/LaraJoinPoint.js";
import {
arrayFromArgs,
flattenArgsArray,
Expand Down Expand Up @@ -221,7 +218,9 @@ export default class ClavaJoinPoints {
return wrapJoinPoint(
ClavaJavaTypes.AstFactory.callFromFunction(
unwrapJoinPoint($function),
...unwrapJoinPoint(flattenArgsArray(callArgs))
// TODO: Made this change without testing. Similar case to scope()
//...unwrapJoinPoint(flattenArgsArray(callArgs))
unwrapJoinPoint(flattenArgsArray(callArgs))
)
);
}
Expand Down Expand Up @@ -286,7 +285,7 @@ export default class ClavaJoinPoints {
});

return wrapJoinPoint(
ClavaJavaTypes.AstFactory.scope(...unwrapJoinPoint($stmts))
ClavaJavaTypes.AstFactory.scope(unwrapJoinPoint($stmts))
);
}

Expand Down Expand Up @@ -795,7 +794,7 @@ export default class ClavaJoinPoints {

throw new Error(
"ClavaJoinPoints.type: source is join point but is not a type, nor has a .type property: " +
source.joinPointType
source.joinPointType
);
}

Expand Down Expand Up @@ -854,12 +853,12 @@ export default class ClavaJoinPoints {
return wrapJoinPoint(ClavaJavaTypes.AstFactory.declLiteral(declString));
}

/**
/**
* Creates a new empty join point 'program'.
*
* @param declString - The literal code of the decl.
*/
static program(): Joinpoints.Program {
return wrapJoinPoint(ClavaJavaTypes.AstFactory.program());
}
static program(): Joinpoints.Program {
return wrapJoinPoint(ClavaJavaTypes.AstFactory.program());
}
}
17 changes: 10 additions & 7 deletions ClavaLaraApi/src-lara/clava/clava/ClavaJoinPoints.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,13 @@ public static AExpression cxxConstructExpr(AType type, AJoinPoint... constructor
AExpression.class);
}

public static ACall callFromFunction(AFunction function, AJoinPoint... args) {
public static ACall callFromFunction(AFunction function, Object[] args) {
return callFromFunction(function, SpecsCollections.asListT(AJoinPoint.class, args));
}

public static ACall callFromFunction(AFunction function, List<? extends AJoinPoint> args) {
var functionDecl = (FunctionDecl) function.getNode();
List<Expr> exprArgs = Arrays.stream(args)
List<Expr> exprArgs = args.stream()
.map(arg -> (Expr) arg.getNode())
.collect(Collectors.toList());

Expand Down Expand Up @@ -426,8 +430,8 @@ public static AExpression integerLiteral(int integer) {
return CxxJoinpoints.create(intLiteral, AExpression.class);
}

public static AScope scope(AStatement... statements) {
return scope(Arrays.asList(statements));
public static AScope scope(Object[] statements) {
return scope(SpecsCollections.asListT(AStatement.class, statements));
}

public static AScope scope(List<? extends AStatement> statements) {
Expand Down Expand Up @@ -781,5 +785,5 @@ public static AProgram program() {
var app = CxxWeaver.getFactory().app(Collections.emptyList());
return CxxJoinpoints.create(app, AProgram.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
/**
* 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.weaver.joinpoints;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import pt.up.fe.specs.clava.ClavaLog;
import pt.up.fe.specs.clava.ClavaNode;
import pt.up.fe.specs.clava.ClavaNodes;
import pt.up.fe.specs.clava.ast.attr.CUDAGlobalAttr;
import pt.up.fe.specs.clava.ast.decl.Decl;
import pt.up.fe.specs.clava.ast.decl.FunctionDecl;
import pt.up.fe.specs.clava.ast.decl.IncludeDecl;
import pt.up.fe.specs.clava.ast.decl.ParmVarDecl;
import pt.up.fe.specs.clava.ast.decl.VarDecl;
import pt.up.fe.specs.clava.ast.decl.*;
import pt.up.fe.specs.clava.ast.expr.Expr;
import pt.up.fe.specs.clava.ast.extra.App;
import pt.up.fe.specs.clava.ast.extra.TranslationUnit;
Expand All @@ -39,25 +29,23 @@
import pt.up.fe.specs.clava.weaver.CxxJoinpoints;
import pt.up.fe.specs.clava.weaver.CxxSelects;
import pt.up.fe.specs.clava.weaver.CxxWeaver;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.ABody;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.ACall;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.ADecl;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AFile;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AFunction;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AFunctionType;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AJoinPoint;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AParam;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AScope;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.AType;
import pt.up.fe.specs.clava.weaver.abstracts.joinpoints.*;
import pt.up.fe.specs.clava.weaver.enums.StorageClass;
import pt.up.fe.specs.clava.weaver.importable.AstFactory;
import pt.up.fe.specs.util.SpecsCollections;
import pt.up.fe.specs.util.SpecsIo;
import pt.up.fe.specs.util.SpecsLogs;
import pt.up.fe.specs.util.enums.EnumHelperWithValue;
import pt.up.fe.specs.util.lazy.Lazy;
import pt.up.fe.specs.util.treenode.NodeInsertUtils;
import pt.up.fe.specs.util.treenode.TreeNodeUtils;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class CxxFunction extends AFunction {

// TODO: Move this to generated enums
Expand Down Expand Up @@ -95,7 +83,7 @@ public AFunctionType getFunctionTypeImpl() {

@Override
public ACall newCallImpl(AJoinPoint[] args) {
return AstFactory.callFromFunction(this, args);
return AstFactory.callFromFunction(this, SpecsCollections.asListT(AJoinPoint.class, (Object[]) args));
}

@Override
Expand Down Expand Up @@ -163,20 +151,20 @@ public AJoinPoint replaceWithImpl(AJoinPoint node) {

private AJoinPoint[] insertStmt(Stmt newNode, String position) {
switch (position) {
case "before":
NodeInsertUtils.insertBefore(function, newNode);
return null;

case "after":
NodeInsertUtils.insertAfter(function, newNode);
return null;

case "around":
case "replace":
NodeInsertUtils.replace(function, newNode);
return new AJoinPoint[] { CxxJoinpoints.create(newNode) };
default:
throw new RuntimeException("Case not defined:" + position);
case "before":
NodeInsertUtils.insertBefore(function, newNode);
return null;

case "after":
NodeInsertUtils.insertAfter(function, newNode);
return null;

case "around":
case "replace":
NodeInsertUtils.replace(function, newNode);
return new AJoinPoint[]{CxxJoinpoints.create(newNode)};
default:
throw new RuntimeException("Case not defined:" + position);
}
}

Expand Down Expand Up @@ -232,8 +220,8 @@ public AFunction cloneOnFileImpl(String newName, String fileName) {

fileName = prefix + extension;
}


// First, check if the given filename is the same as a file in the AST
App app = (App) getRootImpl().getNode();
var currentFile = new File(fileName);
Expand Down Expand Up @@ -595,7 +583,7 @@ public AType getReturnTypeImpl() {
@Override
public void defReturnTypeImpl(AType value) {
function.setReturnType((Type) value.getNode());
};
}

@Override
public void setReturnTypeImpl(AType returnType) {
Expand Down Expand Up @@ -623,7 +611,7 @@ public void addParamImpl(String name, AType type) {
if (type == null) {
paramNode = ClavaNodes.toParam(name, function);
} else {
paramNode = getFactory().parmVarDecl(name, (Type) type.getNode());
paramNode = getFactory().parmVarDecl(name, (Type) type.getNode());
}
addParamImpl(CxxJoinpoints.create(paramNode, AParam.class));
}
Expand All @@ -646,13 +634,13 @@ public void setParamImpl(int index, AParam param) {
@Override
public void setParamImpl(int index, String name, AType type) {
ClavaNode paramNode;

if (type == null) {
paramNode = ClavaNodes.toParam(name, function);
} else {
paramNode = getFactory().parmVarDecl(name, (Type) type.getNode());
paramNode = getFactory().parmVarDecl(name, (Type) type.getNode());
}

setParamImpl(index, CxxJoinpoints.create(paramNode, AParam.class));
}

Expand Down

0 comments on commit cb28eb9

Please sign in to comment.