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

Loading…
Cancel
Save