Browse Source

[WIP, I have no idea what I'm doing] Trying to get generic parameters working

Let's see if putting them all into one TypeVariableContext works..?

Also, a lambda that returns void cannot conclude the generic header. How should we deal with this?
kindlich 5 years ago
parent
commit
ff6514edae
No known key found for this signature in database

+ 8
- 7
JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeModule.java View File

@@ -81,6 +81,7 @@ public class JavaNativeModule {
81 81
 	private final Map<Class<?>, HighLevelDefinition> definitionByClass = new HashMap<>();
82 82
 	private final Map<Class<?>, TypeID> typeByClass = new HashMap<>();
83 83
 	private final Map<Class<?>, TypeID> unsignedByClass = new HashMap<>();
84
+	private final TypeVariableContext context = new TypeVariableContext();
84 85
 	
85 86
 	public final Map<String, ISymbol> globals = new HashMap<>();
86 87
 	private BracketExpressionParser bep;
@@ -161,7 +162,7 @@ public class JavaNativeModule {
161 162
 		JavaClass jcls = JavaClass.fromInternalName(getInternalName(cls), JavaClass.Kind.CLASS);
162 163
 		compiled.setClassInfo(definition, jcls);
163 164
 		StoredType thisType = registry.getForMyDefinition(definition).stored();
164
-		TypeVariableContext context = new TypeVariableContext();
165
+		//TypeVariableContext context = new TypeVariableContext();
165 166
 		
166 167
 		for (Field field : cls.getDeclaredFields()) {
167 168
 			if (!field.isAnnotationPresent(ZenCodeGlobals.Global.class))
@@ -207,7 +208,7 @@ public class JavaNativeModule {
207 208
 		HighLevelDefinition definition = addClass(method.getDeclaringClass());
208 209
 		JavaClass jcls = JavaClass.fromInternalName(getInternalName(method.getDeclaringClass()), JavaClass.Kind.CLASS);
209 210
 		
210
-		TypeVariableContext context = new TypeVariableContext();
211
+		//TypeVariableContext context = new TypeVariableContext();
211 212
 		MethodMember methodMember = new MethodMember(CodePosition.NATIVE, definition, Modifiers.PUBLIC | Modifiers.STATIC, method.getName(), getHeader(context, method), null);
212 213
 		definition.addMember(methodMember);
213 214
 		boolean isGenericResult = methodMember.header.getReturnType().isGeneric();
@@ -298,7 +299,7 @@ public class JavaNativeModule {
298 299
 		//Moved up here so that circular dependencies are caught (hopefully)
299 300
 		definitionByClass.put(cls, definition);
300 301
 
301
-		TypeVariableContext context = new TypeVariableContext();
302
+		//TypeVariableContext context = new TypeVariableContext();
302 303
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
303 304
 		TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
304 305
 		definition.typeParameters = typeParameters;
@@ -457,7 +458,7 @@ public class JavaNativeModule {
457 458
             if(methodAnnotation != null) {
458 459
                 String name = !methodAnnotation.value().isEmpty() ? methodAnnotation.value() : method.getName();
459 460
 
460
-                TypeVariableContext context = new TypeVariableContext();
461
+                //TypeVariableContext context = new TypeVariableContext();
461 462
 
462 463
                 final Parameter[] parameters = getExpansionParameters(method);
463 464
 
@@ -476,7 +477,7 @@ public class JavaNativeModule {
476 477
                 if (implicit) {
477 478
                     modifiers |= Modifiers.IMPLICIT;
478 479
                 }
479
-                TypeVariableContext context = new TypeVariableContext();
480
+                //TypeVariableContext context = new TypeVariableContext();
480 481
                 StoredType toType = loadStoredType(context, method.getAnnotatedReturnType());
481 482
                 final CasterMember member = new CasterMember(CodePosition.NATIVE, expansion, modifiers, toType,  null);
482 483
 
@@ -807,7 +808,7 @@ public class JavaNativeModule {
807 808
 			Parameter parameter = javaParameters[i];
808 809
 
809 810
 			//AnnotatedType parameterType = parameter.getAnnotatedType();
810
-			StoredType type = loadStoredType(context, parameter);
811
+ 			StoredType type = loadStoredType(context, parameter);
811 812
 			Expression defaultValue = getDefaultValue(parameter, type);
812 813
 			parameters[i] = new FunctionParameter(type, parameter.getName(), defaultValue, parameter.isVarArgs());
813 814
 		}
@@ -957,7 +958,7 @@ public class JavaNativeModule {
957 958
 	}
958 959
 	
959 960
 	private <T> TypeVariableContext convertTypeParameters(Class<T> cls) {
960
-		TypeVariableContext context = new TypeVariableContext();
961
+		//TypeVariableContext context = new TypeVariableContext();
961 962
 		TypeVariable<Class<T>>[] javaTypeParameters = cls.getTypeParameters();
962 963
 		TypeParameter[] typeParameters = new TypeParameter[cls.getTypeParameters().length];
963 964
 

+ 16
- 14
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFunction.java View File

@@ -81,27 +81,29 @@ public class ParsedExpressionFunction extends ParsedExpression {
81 81
 			
82 82
 			header.setReturnType(returnType);
83 83
 		}
84
-		
85
-		if (!scope.genericInferenceMap.isEmpty()) {
84
+
85
+
86
+		if (genericHeader.typeParameters.length > 0 && !scope.genericInferenceMap.isEmpty()) {
86 87
 			// perform type parameter inference
87 88
 			StoredType returnType = statements.getReturnType();
88
-			if(returnType == null) {
89
-				throw new NullPointerException();
90
-			}
89
+			if (returnType != null) {
90
+				Map<TypeParameter, StoredType> inferredTypes = returnType.type.inferTypeParameters(scope.getMemberCache(), genericHeader
91
+						.getReturnType());
92
+				if (inferredTypes == null) {
93
+					throw new CompileException(position, CompileExceptionCode.TYPE_ARGUMENTS_NOT_INFERRABLE, "Could not infer generic type parameters");
94
+				}
91 95
 
92
-			if(returnType.type == null) {
93
-				throw new NullPointerException();
96
+				scope.genericInferenceMap.putAll(inferredTypes);
94 97
 			}
95 98
 
96
-			Map<TypeParameter, StoredType> inferredTypes = returnType.type.inferTypeParameters(scope.getMemberCache(), genericHeader.getReturnType());
97
-			if (inferredTypes == null)
98
-				throw new CompileException(position, CompileExceptionCode.TYPE_ARGUMENTS_NOT_INFERRABLE, "Could not infer generic type parameters");
99
-			
100
-			scope.genericInferenceMap.putAll(inferredTypes);
101 99
 		}
102
-		
100
+
101
+		final FunctionHeader thatOtherHeader = genericHeader.withGenericArguments(new GenericMapper(position, scope.getTypeRegistry(), scope.genericInferenceMap));
102
+		if(thatOtherHeader.getReturnType().isBasic(BasicTypeID.UNDETERMINED)) {
103
+			thatOtherHeader.setReturnType(header.getReturnType());
104
+		}
103 105
 		StoredType functionType = scope.getTypeRegistry()
104
-				.getFunction(genericHeader.withGenericArguments(new GenericMapper(position, scope.getTypeRegistry(), scope.genericInferenceMap)))
106
+				.getFunction(thatOtherHeader)
105 107
 				.stored(storage);
106 108
 		return new FunctionExpression(position, functionType, closure, header, statements);
107 109
 	}

Loading…
Cancel
Save