summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsefftinge2008-03-12 09:43:12 (EDT)
committersefftinge2008-03-12 09:43:12 (EDT)
commit3f740fe2ecaa5a2335d5f0e42b0a27669ed6ca86 (patch)
treedebf730d01451e3e381dbd30df9e5f5b2797890d
parent867e7ea7f99a584380bce866e64f785801df3b08 (diff)
downloadorg.eclipse.xpand-3f740fe2ecaa5a2335d5f0e42b0a27669ed6ca86.zip
org.eclipse.xpand-3f740fe2ecaa5a2335d5f0e42b0a27669ed6ca86.tar.gz
org.eclipse.xpand-3f740fe2ecaa5a2335d5f0e42b0a27669ed6ca86.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java2
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java134
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/util/PolymorphicResolver.java48
-rw-r--r--plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java7
4 files changed, 171 insertions, 20 deletions
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java
index 0e04953..becae55 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/builtin/BuiltinTypeSystem.java
@@ -82,7 +82,7 @@ public class BuiltinTypeSystem implements TypeSystem {
if (cls == Boolean.class || cls == Boolean.TYPE)
return types.get(BOOLEAN);
- if (cls == Long.class || cls == Long.TYPE)
+ if (cls == Long.class || cls == Long.TYPE || cls == Integer.TYPE || cls == Integer.class)
return types.get(INTEGER);
if (cls == Double.class || cls == Double.TYPE)
return types.get(REAL);
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java
index 1bbee26..b7354b3 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/java/JavaTypeSystem.java
@@ -24,12 +24,15 @@ import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xpand3.analyzation.GenericsUtil;
import org.eclipse.xpand3.analyzation.TypeSystem;
import org.eclipse.xpand3.analyzation.TypeSystemFactory;
+import org.eclipse.xpand3.analyzation.typesystem.util.PolymorphicResolver;
import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
import org.eclipse.xpand3.staticTypesystem.DeclaredProperty;
@@ -49,7 +52,6 @@ import org.eclipse.xtend.middleend.javaannotations.M2tNoFunction;
public class JavaTypeSystem implements TypeSystem {
private CreateCache<String, DeclaredType> types = new CreateCache<String, DeclaredType>() {
-
@Override
protected DeclaredType create(String key) {
if (key.equals(cls.getSimpleName())) {
@@ -62,7 +64,6 @@ public class JavaTypeSystem implements TypeSystem {
protected void initialize(String key, DeclaredType value) {
initializeDeclaredType(value, key);
}
-
};
private TypeSystemFactory typeSystemFactory = null;
@@ -112,7 +113,8 @@ public class JavaTypeSystem implements TypeSystem {
}
// supertypes
- if (cls.getGenericSuperclass() != null && !cls.getSuperclass().equals(Object.class))
+ if (cls.getGenericSuperclass() != null
+ && !cls.getSuperclass().equals(Object.class))
dt.getSuperTypes().add(toTypeRef(cls.getGenericSuperclass()));
dt.getSuperTypes().addAll(toTypeRefs(cls.getGenericInterfaces()));
if (dt.getSuperTypes().isEmpty()) {
@@ -227,6 +229,19 @@ public class JavaTypeSystem implements TypeSystem {
return newTv;
}
}
+ } else if (genericDeclaration instanceof Method) {
+ Method m = (Method) genericDeclaration;
+ DeclaredFunction f = functions.get(m);
+ EList<DeclaredTypeParameter> parameters = f
+ .getDeclaredTypeParameters();
+ for (DeclaredTypeParameter declaredTypeParameter : parameters) {
+ if (declaredTypeParameter.getName().equals(tv.getName())) {
+ org.eclipse.xpand3.staticTypesystem.TypeVariable newTv = StaticTypesystemFactory.eINSTANCE
+ .createTypeVariable();
+ newTv.setDeclaredTypeParameter(declaredTypeParameter);
+ return newTv;
+ }
+ }
}
throw new IllegalStateException(
"Couldn't find declaration for type variable "
@@ -264,32 +279,113 @@ public class JavaTypeSystem implements TypeSystem {
c.getSimpleName());
}
- /*
- * (non-Javadoc)
+ /**
+ * this implementation maps Java methods to functions. Example : interface
+ * Foo<T extends Bar> { T foo(T x); }
+ *
+ * will be mapped to a function with the following signature:
+ *
+ * <T extends Bar> T foo(Foo<T> this, T x)
*
- * @see org.eclipse.xand3.analyzation.typesystem.DeclarationsContributor#functionForName(java.lang.String,
- * org.eclipse.xpand3.staticTypesystem.AbstractTypeReference[])
*/
public DeclaredFunction functionForName(String name,
AbstractTypeReference... parameterTypes) {
+ if (functions.isEmpty()) {
+ initialize();
+ }
+ Collection<DeclaredFunction> values = functions.getMap().values();
+ return PolymorphicResolver.findFunction(values, name, parameterTypes,
+ null);
+ }
+
+ /**
+ *
+ */
+ private void initialize() {
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
if (method.getAnnotation(M2tNoFunction.class) == null) {
- DeclaredFunction f = StaticTypesystemFactory.eINSTANCE
- .createDeclaredFunction();
- f.setName(method.getName());
- // parameters
- if (!Modifier.isStatic(method.getModifiers())) {
- org.eclipse.xpand3.staticTypesystem.DeclaredParameter dp = StaticTypesystemFactory.eINSTANCE
- .createDeclaredParameter();
- dp.setName("this");
- dp.setType(toTypeRef(method.getDeclaringClass()));
- f.getDeclaredParameters().add(dp);
+ functions.get(method);
+ }
+ }
+ }
+
+ private final CreateCache<Method, DeclaredFunction> functions = new CreateCache<Method, DeclaredFunction>() {
+
+ @Override
+ protected DeclaredFunction create(Method key) {
+ return StaticTypesystemFactory.eINSTANCE.createDeclaredFunction();
+ }
+
+ @Override
+ protected void initialize(Method method, DeclaredFunction f) {
+ f.setName(method.getName());
+ // parameters
+
+ // if not static add all type parameters from declaring type
+ if (!Modifier.isStatic(method.getModifiers())) {
+ org.eclipse.xpand3.staticTypesystem.DeclaredParameter dp = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredParameter();
+ dp.setName("this");
+ Class<?> declaringClass = method.getDeclaringClass();
+ DeclaredType dt = getDeclaredTypeForJClass(declaringClass);
+ Type typeRef = StaticTypesystemFactory.eINSTANCE.createType();
+ typeRef.setDeclaredType(dt);
+ for (DeclaredTypeParameter dtp : dt.getDeclaredTypeParameters()) {
+ // clone the declared type parameters
+ DeclaredTypeParameter copy = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredTypeParameter();
+ copy.setName(dtp.getName());
+ copy.getUpperBounds()
+ .addAll(cloneAll(dtp.getUpperBounds()));
+ f.getDeclaredTypeParameters().add(copy);
+ // and create a type var for the first argument of the
+ // function
+ org.eclipse.xpand3.staticTypesystem.TypeVariable var = StaticTypesystemFactory.eINSTANCE
+ .createTypeVariable();
+ var.setDeclaredTypeParameter(copy);
+ typeRef.getActualTypeArguments().add(var);
}
- f.setReturnType(toTypeRef(method.getGenericReturnType()));
+ dp.setType(typeRef);
+ f.getDeclaredParameters().add(dp);
}
+ // add type parameters declared for the method
+ TypeVariable<Method>[] typeParameters = method.getTypeParameters();
+ for (TypeVariable<Method> tv : typeParameters) {
+ // clone the declared type parameters
+ DeclaredTypeParameter copy = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredTypeParameter();
+ copy.setName(tv.getName());
+ copy.getUpperBounds().addAll(toTypeRefs(tv.getBounds()));
+ f.getDeclaredTypeParameters().add(copy);
+ }
+ // now add the return type
+ java.lang.reflect.Type returnType = method.getGenericReturnType();
+ f.setReturnType(toTypeRef(returnType));
+
+ // now the other parameters
+ java.lang.reflect.Type[] pts = method.getGenericParameterTypes();
+ int i = 1;
+ for (java.lang.reflect.Type pt : pts) {
+ org.eclipse.xpand3.staticTypesystem.DeclaredParameter dp1 = StaticTypesystemFactory.eINSTANCE
+ .createDeclaredParameter();
+ f.getDeclaredParameters().add(dp1);
+ dp1.setName("arg" + (i++));
+ dp1.setType(toTypeRef(pt));
+ }
+ f.setReturnType(toTypeRef(method.getGenericReturnType()));
+
}
- return null;
+
+ };
+
+ /**
+ * @param upperBounds
+ * @return
+ */
+ private Collection<? extends AbstractTypeReference> cloneAll(
+ EList<AbstractTypeReference> upperBounds) {
+ return new EcoreUtil.Copier().copyAll(upperBounds);
}
}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/util/PolymorphicResolver.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/util/PolymorphicResolver.java
new file mode 100644
index 0000000..3c42453
--- /dev/null
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/analyzation/typesystem/util/PolymorphicResolver.java
@@ -0,0 +1,48 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2002-2007 itemis AG and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * itemis AG - Initial API and implementation
+ *
+ * </copyright>
+ *
+ */
+package org.eclipse.xpand3.analyzation.typesystem.util;
+
+import java.util.Collection;
+
+import org.eclipse.xpand3.staticTypesystem.AbstractTypeReference;
+import org.eclipse.xpand3.staticTypesystem.DeclaredFunction;
+
+/**
+ * @author Sven Efftinge
+ *
+ */
+public class PolymorphicResolver {
+
+ /**
+ * @param values
+ * @param name
+ * @param parameterTypes
+ * @param object
+ */
+ public static DeclaredFunction findFunction(Collection<DeclaredFunction> functions,
+ String name, AbstractTypeReference[] parameterTypes, AbstractTypeReference[] typeArguments) {
+ for (DeclaredFunction f : functions) {
+ if (f.getName().equals(name)) {
+ if (f.getDeclaredParameters().size() == parameterTypes.length) {
+ // TODO check types etc.
+ return f;
+ }
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java
index 0864d58..746ec35 100644
--- a/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java
+++ b/plugins/org.eclipse.xpand3/src/org/eclipse/xpand3/util/CreateCache.java
@@ -43,4 +43,11 @@ public abstract class CreateCache<K, V> {
public Map<K, V> getMap() {
return _cache;
}
+
+ /**
+ * @return
+ */
+ public boolean isEmpty() {
+ return _cache.isEmpty();
+ }
} \ No newline at end of file