From 56b5ac9213698a80de9d512752038b642e2b1014 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 6 Dec 2015 11:31:18 +0000 Subject: Use fresh declarations for constructor of closure --- .../couscous/frontends/java/JavaFrontend.java | 2 +- .../zwobble/couscous/frontends/java/JavaReader.java | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java index 2e598d5..b8747fc 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java @@ -26,7 +26,7 @@ public class JavaFrontend implements Frontend { List classNodes = flatMap( findJavaFiles(directoryPath), javaFile -> JavaReader.readClassFromFile(directoryPath, javaFile)); - //ensureDeclarationsAreUnique(classNodes); + ensureDeclarationsAreUnique(classNodes); return classNodes; } diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java index 6f5780f..273dbbe 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; @@ -19,8 +20,10 @@ import static org.zwobble.couscous.ast.AnnotationNode.annotation; import static org.zwobble.couscous.ast.AssignmentNode.assignStatement; import static org.zwobble.couscous.ast.ConstructorNode.constructor; import static org.zwobble.couscous.ast.FieldAccessNode.fieldAccess; +import static org.zwobble.couscous.ast.FormalArgumentNode.formalArg; import static org.zwobble.couscous.ast.ReturnNode.returns; import static org.zwobble.couscous.ast.ThisReferenceNode.thisReference; +import static org.zwobble.couscous.ast.VariableDeclaration.var; import static org.zwobble.couscous.ast.VariableReferenceNode.reference; import static org.zwobble.couscous.frontends.java.FreeVariables.findFreeVariables; import static org.zwobble.couscous.frontends.java.JavaTypes.typeOf; @@ -103,11 +106,19 @@ public class JavaReader { } private ConstructorNode buildConstructor(TypeName type, List freeVariables) { - // TODO: generate fresh variables for captures - List arguments = eagerMap(freeVariables, FormalArgumentNode::formalArg); + // TODO: find a more reliable of generating IDs. For instance, this clashes if + // the same variable is captured more than once + Map argumentDeclarationsById = Maps.transformValues( + Maps.uniqueIndex( + freeVariables, + VariableDeclaration::getId), + freeVariable -> var(freeVariable.getId() + "__capture", freeVariable.getName(), freeVariable.getType())); + List arguments = eagerMap( + freeVariables, + freeVariable -> formalArg(argumentDeclarationsById.get(freeVariable.getId()))); List body = eagerMap(freeVariables, freeVariable -> assignStatement( captureAccess(type, freeVariable), - reference(freeVariable))); + reference(argumentDeclarationsById.get(freeVariable.getId())))); return constructor(arguments, body); } @@ -261,7 +272,7 @@ public class JavaReader { return readSingleVariableDeclaration((SingleVariableDeclaration) parameter); } else { VariableDeclarationFragment fragment = (VariableDeclarationFragment)parameter; - return FormalArgumentNode.formalArg(VariableDeclaration.var( + return formalArg(var( fragment.resolveBinding().getKey(), fragment.getName().getIdentifier(), typeOf(fragment.resolveBinding().getType()))); @@ -321,7 +332,7 @@ public class JavaReader { } private FormalArgumentNode readSingleVariableDeclaration(SingleVariableDeclaration parameter) { - return FormalArgumentNode.formalArg(VariableDeclaration.var( + return formalArg(var( parameter.resolveBinding().getKey(), parameter.getName().getIdentifier(), typeOf(parameter.resolveBinding()))); -- cgit v1.2.3