diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-09 14:21:48 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-09 14:21:59 +0100 |
| commit | 0a550c3bb4e624d76b3876e92a2bd0e235301fba (patch) | |
| tree | ee89fc17f79155f830b3413318cf3c2c76364e2d /src/main/java/org/zwobble/couscous/frontends | |
| parent | f005b283a1b1c74c8b98499069e6afcb5e9a9ade (diff) | |
Remove MethodTypeParameter and adjust Identifier generation
Diffstat (limited to 'src/main/java/org/zwobble/couscous/frontends')
3 files changed, 25 insertions, 18 deletions
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 063ffc6..44cbfab 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -6,6 +6,7 @@ import com.google.common.collect.Maps; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.identifiers.Identifier; import org.zwobble.couscous.ast.sugar.AnonymousClass; import org.zwobble.couscous.ast.sugar.Lambda; import org.zwobble.couscous.ast.sugar.TypeDeclarationBody; @@ -98,7 +99,7 @@ public class JavaReader { private TypeNode readTypeDeclaration(TypeDeclaration type) { ScalarType name = erasure(typeOf(type.resolveBinding())); Scope scope = topScope.enterClass(name); - List<FormalTypeParameterNode> typeParameters = readTypeParameters(type); + List<FormalTypeParameterNode> typeParameters = readTypeParameters(scope.getIdentifier(), type); TypeDeclarationBody body = readTypeDeclarationBody(scope, name, type.bodyDeclarations()); Set<Type> superTypes = superTypes(type); if (type.isInterface()) { @@ -116,14 +117,14 @@ public class JavaReader { } } - private List<FormalTypeParameterNode> readTypeParameters(TypeDeclaration typeDeclaration) { - return readTypeParameters(typeDeclaration.resolveBinding().getTypeParameters()); + private List<FormalTypeParameterNode> readTypeParameters(Identifier declaringScope, TypeDeclaration typeDeclaration) { + return readTypeParameters(declaringScope, typeDeclaration.resolveBinding().getTypeParameters()); } - private List<FormalTypeParameterNode> readTypeParameters(ITypeBinding[] typeParameters) { + private List<FormalTypeParameterNode> readTypeParameters(Identifier declaringScope, ITypeBinding[] typeParameters) { return eagerMap( asList(typeParameters), - parameter -> formalTypeParameter(parameter.getName())); + parameter -> formalTypeParameter(declaringScope, parameter.getName())); } GeneratedClosure readExpressionMethodReference(Scope outerScope, ExpressionMethodReference expression) { @@ -420,7 +421,7 @@ public class JavaReader { annotations, isStatic, methodName, - readTypeParameters(method.resolveBinding().getTypeParameters()), + readTypeParameters(scope.getIdentifier(), method.resolveBinding().getTypeParameters()), formalArguments, returnType.get(), body, diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java index 4b3c5ea..1e373d8 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java @@ -3,6 +3,7 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.ImmutableSet; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.identifiers.Identifiers; import org.zwobble.couscous.types.*; import org.zwobble.couscous.types.ParameterizedType; import org.zwobble.couscous.types.Type; @@ -64,14 +65,14 @@ public class JavaTypes { if (typeParameter.getDeclaringClass() == null) { IMethodBinding method = typeParameter.getDeclaringMethod(); // TODO: disambiguate overloads - return identifierForType(method.getDeclaringClass()).extend(method.getName()); + return Identifiers.method(identifierForType(method.getDeclaringClass()), method.getName()); } else { return identifierForType(typeParameter.getDeclaringClass()); } } private static Identifier identifierForType(ITypeBinding type) { - return Identifier.TOP.extend(erasure(typeOf(type)).getQualifiedName()); + return Identifiers.forType(erasure(typeOf(type)).getQualifiedName()); } static Set<Type> superTypes(TypeDeclaration declaration) { diff --git a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java index 98dd30b..0cc369b 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/Scope.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/Scope.java @@ -2,6 +2,7 @@ package org.zwobble.couscous.frontends.java; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.identifiers.Identifier; +import org.zwobble.couscous.ast.identifiers.Identifiers; import org.zwobble.couscous.types.ScalarType; import org.zwobble.couscous.types.Type; import org.zwobble.couscous.util.NaturalNumbers; @@ -10,7 +11,7 @@ import java.util.*; public class Scope { public static Scope create() { - return new Scope(new HashMap<>(), new HashSet<>(), Identifier.TOP, NaturalNumbers.INSTANCE.iterator()); + return new Scope(new HashMap<>(), new HashSet<>(), Identifiers.TOP, NaturalNumbers.INSTANCE.iterator()); } private final Map<String, VariableDeclaration> variablesByKey; @@ -30,21 +31,25 @@ public class Scope { this.temporaryCounter = temporaryCounter; } + public Identifier getIdentifier() { + return identifier; + } + public Scope enterClass(ScalarType className) { - return enter("class#" + className.getQualifiedName()); + return enter(Identifiers.type(identifier, className.getQualifiedName())); } public Scope enterConstructor() { - return enter("constructor"); + return enter(Identifiers.constructor(identifier)); } public Scope enterMethod(String name) { // TODO: distinguish overloads - return enter("method#" + name); + return enter(Identifiers.method(identifier, name)); } - private Scope enter(String name) { - return new Scope(variablesByKey, identifiers, identifier.extend(name), temporaryCounter); + private Scope enter(Identifier newIdentifier) { + return new Scope(variablesByKey, identifiers, newIdentifier, temporaryCounter); } public FormalArgumentNode formalArgument(String name, Type type) { @@ -102,15 +107,15 @@ public class Scope { } public VariableDeclaration generateVariable(String name, Type type) { - Identifier identifier = generateIdentifier(name); + Identifier identifier = generateVariableIdentifier(name); return VariableDeclaration.var(identifier, name, type); } - private Identifier generateIdentifier(String name) { - Identifier identifier = this.identifier.extend(name); + private Identifier generateVariableIdentifier(String name) { + Identifier identifier = Identifiers.variable(this.identifier, name); int index = 0; while (identifiers.contains(identifier)) { - identifier = this.identifier.extend(name + "_" + index); + identifier = Identifiers.variable(this.identifier, name + "_" + index); index++; } identifiers.add(identifier); |
