diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-09 12:28:19 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-09 12:28:19 +0100 |
| commit | f005b283a1b1c74c8b98499069e6afcb5e9a9ade (patch) | |
| tree | d56dfb884ba10225c7268e8b950f8ef826b51670 /src/main/java/org/zwobble | |
| parent | 8fe29f6add04fb9c2dbc3ad1d13d582787aa6e88 (diff) | |
Use Identifier instead of ScalarType in TypeParameter
Diffstat (limited to 'src/main/java/org/zwobble')
5 files changed, 40 insertions, 17 deletions
diff --git a/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java b/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java index 0c0d047..c753034 100644 --- a/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java +++ b/src/main/java/org/zwobble/couscous/ast/identifiers/Identifier.java @@ -7,6 +7,7 @@ import java.util.List; import static org.zwobble.couscous.util.ExtraLists.list; public class Identifier { + // TODO: Should we have two Identifier classes, one for each namespace? (variables and types) public static final Identifier TOP = new Identifier(list()); private final List<String> parts; @@ -15,6 +16,10 @@ public class Identifier { this.parts = parts; } + public List<String> getParts() { + return parts; + } + public Identifier extend(String name) { ImmutableList.Builder<String> parts = ImmutableList.builder(); parts.addAll(this.parts); diff --git a/src/main/java/org/zwobble/couscous/backends/naming/Names.java b/src/main/java/org/zwobble/couscous/backends/naming/Names.java index e018e7b..537633b 100644 --- a/src/main/java/org/zwobble/couscous/backends/naming/Names.java +++ b/src/main/java/org/zwobble/couscous/backends/naming/Names.java @@ -6,6 +6,7 @@ import org.zwobble.couscous.ast.MethodSignature; import org.zwobble.couscous.types.*; import static com.google.common.collect.Iterables.transform; +import static org.zwobble.couscous.util.ExtraIterables.lazyMap; import static org.zwobble.couscous.util.ExtraLists.list; public class Names { @@ -30,7 +31,9 @@ public class Names { @Override public String visit(TypeParameter parameter) { - return typeToString(parameter.getDeclaringType()) + "_" + parameter.getName(); + // TODO + Iterable<String> parts = lazyMap(parameter.getDeclaringScope().getParts(), part -> part.replace(".", "_")); + return String.join("_", parts) + "_" + parameter.getName(); } @Override 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 3b7a472..4b3c5ea 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaTypes.java @@ -2,6 +2,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.types.*; import org.zwobble.couscous.types.ParameterizedType; import org.zwobble.couscous.types.Type; @@ -42,11 +43,8 @@ public class JavaTypes { } ITypeBinding outerClass = typeBinding.getDeclaringClass(); if (typeBinding.isTypeVariable()) { - if (outerClass == null) { - // TODO: handle type variables for methods properly - outerClass = typeBinding.getDeclaringMethod().getDeclaringClass(); - } - return new TypeParameter(erasure(typeOf(outerClass)), typeBinding.getName()); + Identifier declaringScope = getDeclaringScope(typeBinding); + return new TypeParameter(declaringScope, typeBinding.getName()); } ScalarType rawType = outerClass == null ? ScalarType.of(typeBinding.getErasure().getQualifiedName()) @@ -62,6 +60,20 @@ public class JavaTypes { } } + private static Identifier getDeclaringScope(ITypeBinding typeParameter) { + if (typeParameter.getDeclaringClass() == null) { + IMethodBinding method = typeParameter.getDeclaringMethod(); + // TODO: disambiguate overloads + return identifierForType(method.getDeclaringClass()).extend(method.getName()); + } else { + return identifierForType(typeParameter.getDeclaringClass()); + } + } + + private static Identifier identifierForType(ITypeBinding type) { + return Identifier.TOP.extend(erasure(typeOf(type)).getQualifiedName()); + } + static Set<Type> superTypes(TypeDeclaration declaration) { return superTypes(declaration.resolveBinding()); } diff --git a/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java b/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java index b8c007b..c296d01 100644 --- a/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java +++ b/src/main/java/org/zwobble/couscous/interpreter/types/UserDefinedInterpreterType.java @@ -1,6 +1,7 @@ package org.zwobble.couscous.interpreter.types; import org.zwobble.couscous.ast.*; +import org.zwobble.couscous.ast.identifiers.Identifier; import org.zwobble.couscous.interpreter.Arguments; import org.zwobble.couscous.interpreter.Environment; import org.zwobble.couscous.interpreter.Executor; @@ -48,7 +49,7 @@ public class UserDefinedInterpreterType implements InterpreterType { public Type getType() { List<Type> parameters = eagerMap( type.getTypeParameters(), - parameter -> new TypeParameter(type.getName(), parameter.getName())); + parameter -> new TypeParameter(Identifier.TOP.extend(type.getName().getQualifiedName()), parameter.getName())); return parameters.isEmpty() ? type.getName() : parameterizedType(type.getName(), parameters); diff --git a/src/main/java/org/zwobble/couscous/types/TypeParameter.java b/src/main/java/org/zwobble/couscous/types/TypeParameter.java index 2bd3123..39bf46b 100644 --- a/src/main/java/org/zwobble/couscous/types/TypeParameter.java +++ b/src/main/java/org/zwobble/couscous/types/TypeParameter.java @@ -1,20 +1,22 @@ package org.zwobble.couscous.types; +import org.zwobble.couscous.ast.identifiers.Identifier; + public class TypeParameter implements Type { - public static TypeParameter typeParameter(ScalarType declaringType, String name) { - return new TypeParameter(declaringType, name); + public static TypeParameter typeParameter(Identifier declaringScope, String name) { + return new TypeParameter(declaringScope, name); } - private final ScalarType declaringType; + private final Identifier declaringScope; private final String name; - public TypeParameter(ScalarType declaringType, String name) { - this.declaringType = declaringType; + public TypeParameter(Identifier declaringScope, String name) { + this.declaringScope = declaringScope; this.name = name; } - public ScalarType getDeclaringType() { - return declaringType; + public Identifier getDeclaringScope() { + return declaringScope; } public String getName() { @@ -29,7 +31,7 @@ public class TypeParameter implements Type { @Override public String toString() { return "TypeParameter(" + - "declaringType=" + declaringType + + "declaringScope=" + declaringScope + ", name=" + name + ')'; } @@ -41,14 +43,14 @@ public class TypeParameter implements Type { TypeParameter that = (TypeParameter) o; - if (!declaringType.equals(that.declaringType)) return false; + if (!declaringScope.equals(that.declaringScope)) return false; return name.equals(that.name); } @Override public int hashCode() { - int result = declaringType.hashCode(); + int result = declaringScope.hashCode(); result = 31 * result + name.hashCode(); return result; } |
