Browse Source

WIP: More work on generic parameters in expansions

kindlich 4 years ago
parent
commit
8f1960209a
No known key found for this signature in database

+ 9
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/CompilerUtils.java View File

18
 import org.openzen.zenscript.javashared.JavaCompiledModule;
18
 import org.openzen.zenscript.javashared.JavaCompiledModule;
19
 import org.openzen.zenscript.javashared.JavaTypeParameterInfo;
19
 import org.openzen.zenscript.javashared.JavaTypeParameterInfo;
20
 
20
 
21
+import java.util.List;
22
+
21
 public class CompilerUtils {
23
 public class CompilerUtils {
22
 
24
 
23
 	private CompilerUtils() {}
25
 	private CompilerUtils() {}
48
 		return out;
50
 		return out;
49
 	}
51
 	}
50
 
52
 
51
-    public static void tagMethodParameters(JavaBytecodeContext context, JavaCompiledModule module, FunctionHeader header, boolean isStatic) {
53
+    public static void tagMethodParameters(JavaBytecodeContext context, JavaCompiledModule module, FunctionHeader header, boolean isStatic, List<TypeParameter> baseTypeTypeParameters) {
52
 		int index = isStatic ? 0 : 1;
54
 		int index = isStatic ? 0 : 1;
55
+
56
+		for (TypeParameter baseTypeTypeParameter : baseTypeTypeParameters) {
57
+			module.setTypeParameterInfo(baseTypeTypeParameter, new JavaTypeParameterInfo(index));
58
+			index++;
59
+		}
60
+
53
 		for (int i = 0; i < header.typeParameters.length; i++) {
61
 		for (int i = 0; i < header.typeParameters.length; i++) {
54
 			TypeParameter parameter = header.typeParameters[i];
62
 			TypeParameter parameter = header.typeParameters[i];
55
 			module.setTypeParameterInfo(parameter, new JavaTypeParameterInfo(index));
63
 			module.setTypeParameterInfo(parameter, new JavaTypeParameterInfo(index));

+ 31
- 9
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

8
 import org.openzen.zenscript.codemodel.FunctionParameter;
8
 import org.openzen.zenscript.codemodel.FunctionParameter;
9
 import org.openzen.zenscript.codemodel.expression.*;
9
 import org.openzen.zenscript.codemodel.expression.*;
10
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
10
 import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
11
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
11
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
12
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
12
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
13
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
13
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
14
 import org.openzen.zenscript.codemodel.statement.ReturnStatement;
23
 
24
 
24
 import java.lang.reflect.Method;
25
 import java.lang.reflect.Method;
25
 import java.lang.reflect.Modifier;
26
 import java.lang.reflect.Modifier;
26
-import java.util.Arrays;
27
-import java.util.Comparator;
28
-import java.util.Objects;
29
-import java.util.StringJoiner;
27
+import java.util.*;
28
+import java.util.stream.Collectors;
30
 
29
 
31
 public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativeTranslator<Void> {
30
 public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativeTranslator<Void> {
32
     private static final JavaMethod OBJECTS_TOSTRING = JavaMethod.getNativeStatic(new JavaClass("java.util", "Objects", JavaClass.Kind.CLASS), "toString", "(Ljava/lang/Object;)Ljava/lang/String;");
31
     private static final JavaMethod OBJECTS_TOSTRING = JavaMethod.getNativeStatic(new JavaClass("java.util", "Objects", JavaClass.Kind.CLASS), "toString", "(Ljava/lang/Object;)Ljava/lang/String;");
384
 		if (builtin == null) {
383
 		if (builtin == null) {
385
 			expression.target.accept(this);
384
 			expression.target.accept(this);
386
 
385
 
386
+			final List<TypeParameter> typeParameters;
387
+			{
388
+				final List<TypeParameter> parameters = new ArrayList<>(
389
+						Arrays.asList(expression.member.getTarget().definition.typeParameters)
390
+				);
391
+
392
+				expression.member.getOwnerType().type.extractTypeParameters(parameters);
393
+				for (FunctionParameter parameter : expression.instancedHeader.parameters) {
394
+					parameter.type.type.extractTypeParameters(parameters);
395
+				}
396
+				typeParameters = parameters.stream().distinct().collect(Collectors.toList());
397
+			}
398
+
387
 			JavaMethod methodInfo = context.getJavaMethod(expression.member);
399
 			JavaMethod methodInfo = context.getJavaMethod(expression.member);
388
-			for (int i = 0; i < expression.arguments.typeArguments.length; i++) {
389
-				if (methodInfo.typeParameterArguments[i]) {
390
-					StoredType arguments = expression.arguments.typeArguments[i];
391
 
400
 
401
+			if(methodInfo.compile) {
402
+				for (TypeParameter typeParameter : typeParameters) {
403
+					javaWriter.aConstNull(); // TODO: Replace with actual class
392
 				}
404
 				}
393
 			}
405
 			}
394
 
406
 
1951
 
1963
 
1952
 	@Override
1964
 	@Override
1953
 	public Void visitFunction(FunctionExpression expression) {
1965
 	public Void visitFunction(FunctionExpression expression) {
1954
-		CompilerUtils.tagMethodParameters(context, module, expression.header, false);
1966
+		CompilerUtils.tagMethodParameters(context, module, expression.header, false, Collections.emptyList());
1955
 
1967
 
1956
         /*if (expression.header.parameters.length == 0 && expression.body instanceof ReturnStatement && expression.body.hasTag(MatchExpression.class) && expression.closure.captures.isEmpty()) {
1968
         /*if (expression.header.parameters.length == 0 && expression.body instanceof ReturnStatement && expression.body.hasTag(MatchExpression.class) && expression.closure.captures.isEmpty()) {
1957
             ((ReturnStatement) expression.body).value.accept(this);
1969
             ((ReturnStatement) expression.body).value.accept(this);
2188
 				javaWriter.getField(context.getJavaField(expression.getter));
2200
 				javaWriter.getField(context.getJavaField(expression.getter));
2189
 				return null;
2201
 				return null;
2190
 			}
2202
 			}
2203
+
2204
+
2205
+			final List<TypeParameter> typeParameters = new ArrayList<>();
2206
+			expression.getter.member.getType().type.extractTypeParameters(typeParameters);
2207
+
2208
+			for (TypeParameter typeParameter : typeParameters) {
2209
+				javaWriter.aConstNull(); //TODO: Replace with actual type
2210
+			}
2211
+
2191
 			if (!checkAndExecuteMethodInfo(expression.getter, expression.type, expression))
2212
 			if (!checkAndExecuteMethodInfo(expression.getter, expression.type, expression))
2192
 				throw new IllegalStateException("Call target has no method info!");
2213
 				throw new IllegalStateException("Call target has no method info!");
2193
 
2214
 
3516
 								}
3537
 								}
3517
 								
3538
 								
3518
 								if (funcExpression instanceof FunctionExpression && ((FunctionExpression) funcExpression).body instanceof ReturnStatement) {
3539
 								if (funcExpression instanceof FunctionExpression && ((FunctionExpression) funcExpression).body instanceof ReturnStatement) {
3519
-									CompilerUtils.tagMethodParameters(context, module, ((FunctionExpression) funcExpression).header, false);
3540
+									CompilerUtils.tagMethodParameters(context, module, ((FunctionExpression) funcExpression).header, false, Collections
3541
+                                            .emptyList());
3520
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3542
 									((ReturnStatement) ((FunctionExpression) funcExpression).body).value.accept(visitor);
3521
 									javaWriter.addVariableInfo(new JavaLocalVariableInfo(projectedElementType, projectedElementLocal, inlineBegin, ((FunctionExpression) funcExpression).header.parameters[0].name, inlineEnd));
3543
 									javaWriter.addVariableInfo(new JavaLocalVariableInfo(projectedElementType, projectedElementLocal, inlineBegin, ((FunctionExpression) funcExpression).header.parameters[0].name, inlineEnd));
3522
 									
3544
 									

+ 3
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaDefinitionVisitor.java View File

29
 import org.openzen.zenscript.javashared.JavaVariantOption;
29
 import org.openzen.zenscript.javashared.JavaVariantOption;
30
 
30
 
31
 import java.util.ArrayList;
31
 import java.util.ArrayList;
32
+import java.util.Collections;
32
 import java.util.List;
33
 import java.util.List;
33
 
34
 
34
 
35
 
145
 
146
 
146
 	@Override
147
 	@Override
147
 	public byte[] visitFunction(FunctionDefinition definition) {
148
 	public byte[] visitFunction(FunctionDefinition definition) {
148
-		CompilerUtils.tagMethodParameters(context, context.getJavaModule(definition.module), definition.header, true);
149
+		CompilerUtils.tagMethodParameters(context, context.getJavaModule(definition.module), definition.header, true, Collections
150
+                .emptyList());
149
 
151
 
150
         final String signature = context.getMethodSignature(definition.header);
152
         final String signature = context.getMethodSignature(definition.header);
151
 		final JavaMethod method = context.getJavaMethod(definition.caller);
153
 		final JavaMethod method = context.getJavaMethod(definition.caller);

+ 116
- 11
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaExpansionMemberVisitor.java View File

5
 import org.objectweb.asm.Type;
5
 import org.objectweb.asm.Type;
6
 import org.openzen.zenscript.codemodel.FunctionParameter;
6
 import org.openzen.zenscript.codemodel.FunctionParameter;
7
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
7
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
8
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
8
 import org.openzen.zenscript.codemodel.member.*;
9
 import org.openzen.zenscript.codemodel.member.*;
9
 import org.openzen.zenscript.codemodel.type.StoredType;
10
 import org.openzen.zenscript.codemodel.type.StoredType;
10
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
11
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
15
 import org.openzen.zenscript.javashared.JavaField;
16
 import org.openzen.zenscript.javashared.JavaField;
16
 import org.openzen.zenscript.javashared.JavaMethod;
17
 import org.openzen.zenscript.javashared.JavaMethod;
17
 
18
 
19
+import java.util.ArrayList;
20
+import java.util.stream.Collectors;
21
+
18
 public class JavaExpansionMemberVisitor implements MemberVisitor<Void> {
22
 public class JavaExpansionMemberVisitor implements MemberVisitor<Void> {
19
 
23
 
20
 	private final ClassWriter writer;
24
 	private final ClassWriter writer;
79
 		if (!method.compile)
83
 		if (!method.compile)
80
 			return null;
84
 			return null;
81
 
85
 
86
+		final ArrayList<TypeParameter> typeParameters = new ArrayList<>();
87
+		expandedClass.type.extractTypeParameters(typeParameters);
82
 
88
 
83
-		CompilerUtils.tagMethodParameters(context, javaModule, member.header, member.isStatic());
89
+		CompilerUtils.tagMethodParameters(context, javaModule, member.header, member.isStatic(), typeParameters);
84
 
90
 
85
 		final String expandedClassDescriptor = context.getDescriptor(expandedClass);
91
 		final String expandedClassDescriptor = context.getDescriptor(expandedClass);
92
+		final String expandedClassSignature = context.getSignature(expandedClass);
86
 		final Label methodStart = new Label();
93
 		final Label methodStart = new Label();
87
 		final Label methodEnd = new Label();
94
 		final Label methodEnd = new Label();
88
 		final String methodSignature;
95
 		final String methodSignature;
96
+		final String methodDescriptor;
97
+
98
+
89
 
99
 
90
 		if (!isStatic) {
100
 		if (!isStatic) {
91
-			methodSignature = "(" + expandedClassDescriptor + context.getMethodSignature(member.header).substring(1);
101
+			String methodSignature1 = context.getMethodSignature(member.header);
102
+
103
+			//Add the expanded type as first generic parameter to the list.
104
+			if(!typeParameters.isEmpty()){
105
+				final String collect = typeParameters.stream()
106
+						.map(t -> t.name + ":" + "Ljava/lang/Object;")
107
+						.collect(Collectors.joining("", "<", ""));
108
+				if(methodSignature1.startsWith("<")) {
109
+					methodSignature1 = collect + methodSignature1.substring(1);
110
+				} else {
111
+					methodSignature1 = collect + ">" + methodSignature1;
112
+				}
113
+			}
114
+
115
+			final StringBuilder typeParamSigBuilder = new StringBuilder();
116
+			final StringBuilder typeParamDescBuilder = new StringBuilder();
117
+			int i = 1;
118
+			for (TypeParameter typeParameter : typeParameters) {
119
+				typeParamSigBuilder.append("Ljava/lang/Class<T").append(typeParameter.name).append(";>;");
120
+				typeParamDescBuilder.append("Ljava/lang/Class;");
121
+			}
122
+
123
+
124
+			final int index = methodSignature1.lastIndexOf('(') + 1;
125
+			methodSignature = methodSignature1.substring(0, index) + expandedClassSignature + typeParamSigBuilder.toString() + methodSignature1.substring(index);
126
+			methodDescriptor = "(" + expandedClassDescriptor + typeParamDescBuilder.toString() + context.getMethodDescriptor(member.header).substring(1);
92
 		} else {
127
 		} else {
93
 			methodSignature = context.getMethodSignature(member.header);
128
 			methodSignature = context.getMethodSignature(member.header);
129
+			methodDescriptor = context.getMethodDescriptor(member.header);
94
 		}
130
 		}
95
 
131
 
96
 
132
 
97
-		final JavaWriter methodWriter = new JavaWriter(member.position, writer, true, method, definition, true, methodSignature, methodSignature, null);
133
+		final JavaWriter methodWriter = new JavaWriter(member.position, writer, true, method, definition, true, methodSignature, methodDescriptor, null);
98
 		methodWriter.label(methodStart);
134
 		methodWriter.label(methodStart);
99
 
135
 
100
 		if (!isStatic) {
136
 		if (!isStatic) {
101
 			methodWriter.nameVariable(0, "expandedObj", methodStart, methodEnd, Type.getType(expandedClassDescriptor));
137
 			methodWriter.nameVariable(0, "expandedObj", methodStart, methodEnd, Type.getType(expandedClassDescriptor));
102
 			methodWriter.nameParameter(0, "expandedObj");
138
 			methodWriter.nameParameter(0, "expandedObj");
103
-			for (final FunctionParameter parameter : member.header.parameters) {
104
-				methodWriter.nameParameter(0, parameter.name);
105
-				methodWriter.nameVariable(javaModule.getParameterInfo(parameter).index, parameter.name, methodStart, methodEnd, context.getType(parameter.type));
106
-			}
107
-		} else {
108
-			for (final FunctionParameter parameter : member.header.parameters) {
109
-				methodWriter.nameParameter(0, parameter.name);
110
-				methodWriter.nameVariable(javaModule.getParameterInfo(parameter).index, parameter.name, methodStart, methodEnd, context.getType(parameter.type));
139
+
140
+			for (TypeParameter typeParameter : typeParameters) {
141
+				methodWriter.nameParameter(0, "typeOf" + typeParameter.name);
142
+				methodWriter.nameVariable(javaModule.getTypeParameterInfo(typeParameter).parameterIndex, "typeOf" + typeParameter.name, methodStart, methodEnd, Type.getType(Class.class));
111
 			}
143
 			}
112
 		}
144
 		}
113
 
145
 
146
+		for (TypeParameter typeParameter : member.header.typeParameters) {
147
+			methodWriter.nameParameter(0, "typeOf" + typeParameter.name);
148
+			methodWriter.nameVariable(javaModule.getTypeParameterInfo(typeParameter).parameterIndex, "typeOf" + typeParameter.name, methodStart, methodEnd, Type.getType(Class.class));
149
+		}
150
+
151
+		for (final FunctionParameter parameter : member.header.parameters) {
152
+			methodWriter.nameParameter(0, parameter.name);
153
+			methodWriter.nameVariable(javaModule.getParameterInfo(parameter).index, parameter.name, methodStart, methodEnd, context.getType(parameter.type));
154
+		}
155
+
114
 
156
 
115
 		{
157
 		{
116
 			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
158
 			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
126
 
168
 
127
 	@Override
169
 	@Override
128
 	public Void visitGetter(GetterMember member) {
170
 	public Void visitGetter(GetterMember member) {
171
+		final boolean isStatic = member.isStatic();
172
+		final StoredType returnType = member.getType();
173
+		final String descriptor;
174
+		final String signature;
175
+
176
+		final ArrayList<TypeParameter> typeParameters = new ArrayList<>();
177
+		expandedClass.type.extractTypeParameters(typeParameters);
178
+
179
+		final String descMiddle, signatureMiddle, signatureStart;
180
+		if(typeParameters.isEmpty()) {
181
+			descMiddle = signatureMiddle = signatureStart = "";
182
+		} else {
183
+			final StringBuilder descMiddleBuilder = new StringBuilder();
184
+			final StringBuilder signatureMiddleBuilder = new StringBuilder();
185
+			final StringBuilder signatureStartBuilder = new StringBuilder("<");
186
+
187
+			for (TypeParameter typeParameter : typeParameters) {
188
+				descMiddleBuilder.append("Ljava/lang/Class;");
189
+				signatureMiddleBuilder.append("Ljava/lang/Class<T").append(typeParameter.name).append(";>;");
190
+				signatureStartBuilder.append(typeParameter.name).append(":Ljava/lang/Object;");
191
+			}
192
+
193
+			descMiddle = descMiddleBuilder.toString();
194
+			signatureMiddle = signatureMiddleBuilder.toString();
195
+			signatureStart = signatureStartBuilder.append(">").toString();
196
+		}
197
+
198
+		if (isStatic) {
199
+			descriptor = "(" + descMiddle + ")" + context.getDescriptor(returnType);
200
+			signature = signatureStart + "(" + signatureMiddle + ")" + context.getSignature(returnType);
201
+		} else {
202
+			descriptor = "(" + context.getDescriptor(expandedClass) + descMiddle + ")" + context.getDescriptor(returnType);
203
+			signature = signatureStart + "(" + context.getSignature(expandedClass) + signatureMiddle + ")" + context.getSignature(returnType);
204
+		}
205
+
206
+		final Label methodStart = new Label();
207
+		final Label methodEnd = new Label();
208
+
209
+		final JavaMethod method = context.getJavaMethod(member);
210
+		final JavaWriter methodWriter = new JavaWriter(member.position, this.writer, true, method, definition, true, signature, descriptor, new String[0]);
211
+
212
+		methodWriter.label(methodStart);
213
+
214
+		if (!isStatic) {
215
+			methodWriter.nameVariable(0, "expandedObj", methodStart, methodEnd, context.getType(this.expandedClass));
216
+			methodWriter.nameParameter(0, "expandedObj");
217
+		}
218
+
219
+		int i = isStatic ? 0 : 1;
220
+		for (TypeParameter typeParameter : typeParameters) {
221
+			final String name = "typeOf" + typeParameter.name;
222
+			methodWriter.nameVariable(i, name, methodStart, methodEnd, Type.getType(Class.class));
223
+			methodWriter.nameParameter(0, name);
224
+		}
225
+
226
+		{
227
+			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(context, javaModule, methodWriter);
228
+			statementVisitor.start();
229
+			member.body.accept(statementVisitor);
230
+			methodWriter.label(methodEnd);
231
+			statementVisitor.end();
232
+		}
233
+
129
 		return null;
234
 		return null;
130
 	}
235
 	}
131
 
236
 

+ 2
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/definitions/JavaMemberVisitor.java View File

12
 import org.openzen.zenscript.codemodel.member.*;
12
 import org.openzen.zenscript.codemodel.member.*;
13
 import org.openzen.zenscript.javabytecode.compiler.*;
13
 import org.openzen.zenscript.javabytecode.compiler.*;
14
 
14
 
15
+import java.util.Collections;
15
 import java.util.List;
16
 import java.util.List;
16
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
17
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
17
 import org.openzen.zenscript.javashared.JavaClass;
18
 import org.openzen.zenscript.javashared.JavaClass;
129
 
130
 
130
     @Override
131
     @Override
131
     public Void visitMethod(MethodMember member) {
132
     public Void visitMethod(MethodMember member) {
132
-        CompilerUtils.tagMethodParameters(context, javaModule, member.header, member.isStatic());
133
+        CompilerUtils.tagMethodParameters(context, javaModule, member.header, member.isStatic(), Collections.emptyList());
133
 
134
 
134
         final boolean isAbstract = member.body == null || Modifiers.isAbstract(member.getEffectiveModifiers());
135
         final boolean isAbstract = member.body == null || Modifiers.isAbstract(member.getEffectiveModifiers());
135
         final JavaMethod method = context.getJavaMethod(member);
136
         final JavaMethod method = context.getJavaMethod(member);

+ 18
- 3
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaContext.java View File

125
 	public abstract String getDescriptor(TypeID type);
125
 	public abstract String getDescriptor(TypeID type);
126
 	
126
 	
127
 	public abstract String getDescriptor(StoredType type);
127
 	public abstract String getDescriptor(StoredType type);
128
-	
128
+
129
+	public String getSignature(StoredType type) {
130
+		return new JavaTypeGenericVisitor(this).getGenericSignature(type);
131
+	}
132
+
129
 	public void addModule(Module module, JavaCompiledModule target) {
133
 	public void addModule(Module module, JavaCompiledModule target) {
130
 		modules.put(module, target);
134
 		modules.put(module, target);
131
 
135
 
225
 	}
229
 	}
226
 
230
 
227
 	public String getMethodDescriptorExpansion(FunctionHeader header, StoredType expandedType) {
231
 	public String getMethodDescriptorExpansion(FunctionHeader header, StoredType expandedType) {
228
-		return getMethodDescriptor(header, false, getDescriptor(expandedType));
232
+		StringBuilder startBuilder = new StringBuilder(getDescriptor(expandedType));
233
+		final List<TypeParameter> typeParameters = new ArrayList<>();
234
+		expandedType.type.extractTypeParameters(typeParameters);
235
+		for (TypeParameter typeParameter : typeParameters) {
236
+			startBuilder.append("Ljava/lang/Class;");
237
+		}
238
+
239
+		return getMethodDescriptor(header, false, startBuilder.toString());
229
 	}
240
 	}
230
 	
241
 	
231
     public String getMethodSignature(FunctionHeader header) {
242
     public String getMethodSignature(FunctionHeader header) {
232
-        return new JavaTypeGenericVisitor(this).getGenericMethodSignature(header);
243
+        return getMethodSignature(header, true);
233
     }
244
     }
245
+
246
+    public String getMethodSignature(FunctionHeader header, boolean withGenerics) {
247
+		return new JavaTypeGenericVisitor(this).getGenericMethodSignature(header, withGenerics);
248
+	}
234
 	
249
 	
235
 	public String getEnumConstructorDescriptor(FunctionHeader header) {
250
 	public String getEnumConstructorDescriptor(FunctionHeader header) {
236
 		return getMethodDescriptor(header, true, "");
251
 		return getMethodDescriptor(header, true, "");

+ 52
- 3
JavaShared/src/main/java/org/openzen/zenscript/javashared/JavaTypeGenericVisitor.java View File

4
 import org.openzen.zenscript.codemodel.FunctionParameter;
4
 import org.openzen.zenscript.codemodel.FunctionParameter;
5
 import org.openzen.zenscript.codemodel.generic.*;
5
 import org.openzen.zenscript.codemodel.generic.*;
6
 import org.openzen.zenscript.codemodel.type.*;
6
 import org.openzen.zenscript.codemodel.type.*;
7
+import org.openzen.zenscript.codemodel.type.storage.ValueStorageTag;
7
 
8
 
9
+import java.util.Arrays;
8
 import java.util.Collection;
10
 import java.util.Collection;
9
 
11
 
10
 public class JavaTypeGenericVisitor implements TypeVisitorWithContext<StoredType, String, RuntimeException> {
12
 public class JavaTypeGenericVisitor implements TypeVisitorWithContext<StoredType, String, RuntimeException> {
59
 				getGenericSignature(header.getReturnType());
61
 				getGenericSignature(header.getReturnType());
60
 	}
62
 	}
61
 
63
 
64
+	public String getGenericMethodSignature(FunctionHeader header, boolean addGenerics) {
65
+		final StringBuilder sb = new StringBuilder();
66
+		final boolean doGenerics = addGenerics && header.typeParameters.length > 0;
67
+
68
+		if(doGenerics) {
69
+			sb.append("<");
70
+			for (TypeParameter typeParameter : header.typeParameters) {
71
+				//TODO: Eventually replace with upper bound
72
+				sb.append(typeParameter.name).append(":").append("Ljava/lang/Object;");
73
+			}
74
+			sb.append(">");
75
+		}
76
+
77
+
78
+		sb.append("(");
79
+		if(doGenerics) {
80
+			for (TypeParameter typeParameter : header.typeParameters) {
81
+				//TODO: Eventually replace with -TT; or +TT; for "? super T" and "? extends T"
82
+				sb.append("Ljava/lang/Class<T").append(typeParameter.name).append(";>;");
83
+			}
84
+		}
85
+
86
+		sb.append(getGenericSignature(header.parameters));
87
+		sb.append(")");
88
+		sb.append(getGenericSignature(header.getReturnType()));
89
+		return sb.toString();
90
+	}
91
+
62
 
92
 
63
 	public String getGenericBounds(Collection<TypeParameterBound> collection) {
93
 	public String getGenericBounds(Collection<TypeParameterBound> collection) {
64
 		if (collection == null)
94
 		if (collection == null)
93
 
123
 
94
 	@Override
124
 	@Override
95
 	public String visitArray(StoredType context, ArrayTypeID array) {
125
 	public String visitArray(StoredType context, ArrayTypeID array) {
96
-		return this.context.getDescriptor(array);
126
+		final char[] dim = new char[array.dimension];
127
+		Arrays.fill(dim, '[');
128
+		return new String(dim) + this.context.getSignature(array.elementType);
97
 	}
129
 	}
98
 
130
 
99
 	@Override
131
 	@Override
100
 	public String visitAssoc(StoredType context, AssocTypeID assoc) {
132
 	public String visitAssoc(StoredType context, AssocTypeID assoc) {
101
-		return this.context.getDescriptor(assoc);
133
+		return "Ljava/util/Map<"
134
+				+ assoc.keyType.type.accept(context, this)
135
+				+ assoc.valueType.type.accept(context, this)
136
+				+ ">;";
102
 	}
137
 	}
103
 
138
 
104
 	@Override
139
 	@Override
113
 
148
 
114
 	@Override
149
 	@Override
115
 	public String visitFunction(StoredType context, FunctionTypeID function) {
150
 	public String visitFunction(StoredType context, FunctionTypeID function) {
116
-		return this.context.getDescriptor(function);
151
+		final JavaSynthesizedFunctionInstance function1 = this.context.getFunction(function);
152
+		if(function1.typeArguments == null || function1.typeArguments.length == 0) {
153
+			return this.context.getDescriptor(function);
154
+		}
155
+
156
+		StringBuilder sb = new StringBuilder("L").append(function1.getCls().internalName).append("<");
157
+		for (TypeID typeArgument : function1.typeArguments) {
158
+			final String n = typeArgument instanceof GenericTypeID
159
+					? ((GenericTypeID) typeArgument).parameter.name
160
+					: "Ljava/lang/Object"; //Can latter even happen?
161
+
162
+			sb.append("T").append(n).append(";");
163
+		}
164
+
165
+		return sb.append(">;").toString();
117
 	}
166
 	}
118
 
167
 
119
 	@Override
168
 	@Override

Loading…
Cancel
Save