Browse Source

Fix calling of functions with generic parameters

Stan Hebben 6 years ago
parent
commit
bcebf627ba

+ 13
- 6
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/DefinitionFormatter.java View File

15
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
15
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
16
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
16
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
17
 import org.openzen.zenscript.codemodel.definition.VariantDefinition;
17
 import org.openzen.zenscript.codemodel.definition.VariantDefinition;
18
-import org.openzen.zenscript.codemodel.expression.CallArguments;
18
+import org.openzen.zenscript.codemodel.member.CallerMember;
19
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
19
 import org.openzen.zenscript.codemodel.member.EnumConstantMember;
20
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
20
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
21
 
21
 
140
 
140
 
141
 	@Override
141
 	@Override
142
 	public Void visitFunction(FunctionDefinition definition) {
142
 	public Void visitFunction(FunctionDefinition definition) {
143
-		FormattingUtils.formatModifiers(output, definition.modifiers);
144
-		output.append("function ");
145
-		output.append(definition.name);
146
-		FormattingUtils.formatHeader(output, settings, definition.header, typeFormatter);
147
-		FormattingUtils.formatBody(output, settings, indent, typeFormatter, definition.statement);
143
+		for (IDefinitionMember member : definition.members) {
144
+			if (member instanceof CallerMember) {
145
+				CallerMember caller = (CallerMember) member;
146
+				FormattingUtils.formatModifiers(output, definition.modifiers);
147
+				output.append("function ");
148
+				output.append(definition.name);
149
+				
150
+				FormattingUtils.formatHeader(output, settings, caller.header, typeFormatter);
151
+				FormattingUtils.formatBody(output, settings, indent, typeFormatter, caller.body);
152
+			}
153
+		}
154
+		
148
 		return null;
155
 		return null;
149
 	}
156
 	}
150
 
157
 

+ 3
- 4
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java View File

9
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10
 import org.openzen.zenscript.codemodel.Modifiers;
10
 import org.openzen.zenscript.codemodel.Modifiers;
11
 import org.openzen.zenscript.codemodel.OperatorType;
11
 import org.openzen.zenscript.codemodel.OperatorType;
12
+import org.openzen.zenscript.codemodel.member.CallerMember;
12
 import org.openzen.zenscript.codemodel.member.OperatorMember;
13
 import org.openzen.zenscript.codemodel.member.OperatorMember;
13
 import org.openzen.zenscript.codemodel.statement.Statement;
14
 import org.openzen.zenscript.codemodel.statement.Statement;
14
 import org.openzen.zenscript.codemodel.type.member.DefinitionMemberGroup;
15
 import org.openzen.zenscript.codemodel.type.member.DefinitionMemberGroup;
22
 public class FunctionDefinition extends HighLevelDefinition {
23
 public class FunctionDefinition extends HighLevelDefinition {
23
 	public FunctionHeader header;
24
 	public FunctionHeader header;
24
 	public Statement statement;
25
 	public Statement statement;
25
-	public OperatorMember caller;
26
+	public CallerMember caller;
26
 	public final DefinitionMemberGroup callerGroup;
27
 	public final DefinitionMemberGroup callerGroup;
27
 	
28
 	
28
 	public FunctionDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
29
 	public FunctionDefinition(CodePosition position, ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
37
 	
38
 	
38
 	public void setHeader(FunctionHeader header) {
39
 	public void setHeader(FunctionHeader header) {
39
 		this.header = header;
40
 		this.header = header;
40
-		this.genericParameters = header.typeParameters;
41
-		header = new FunctionHeader(null, header.returnType, header.thrownType, header.parameters);
42
-		addMember(caller = new OperatorMember(position, this, modifiers | Modifiers.STATIC, OperatorType.CALL, header));
41
+		addMember(caller = new CallerMember(position, this, modifiers | Modifiers.STATIC, header));
43
 		callerGroup.addMethod(caller, TypeMemberPriority.SPECIFIED);
42
 		callerGroup.addMethod(caller, TypeMemberPriority.SPECIFIED);
44
 	}
43
 	}
45
 	
44
 	

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java View File

49
 			if (types.get(name.name).genericParameters.length != name.getNumberOfArguments())
49
 			if (types.get(name.name).genericParameters.length != name.getNumberOfArguments())
50
 				throw new CompileException(position, CompileExceptionCode.TYPE_ARGUMENTS_INVALID_NUMBER, "Invalid number of type arguments");
50
 				throw new CompileException(position, CompileExceptionCode.TYPE_ARGUMENTS_INVALID_NUMBER, "Invalid number of type arguments");
51
 			
51
 			
52
-			return new PartialTypeExpression(position, registry.getForDefinition(types.get(name.name), name.arguments));
52
+			return new PartialTypeExpression(position, registry.getForDefinition(types.get(name.name), name.arguments), name.arguments);
53
 		}
53
 		}
54
 		
54
 		
55
 		return null;
55
 		return null;

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallStaticExpression.java View File

22
 	public final FunctionHeader instancedHeader;
22
 	public final FunctionHeader instancedHeader;
23
 	
23
 	
24
 	public CallStaticExpression(CodePosition position, ITypeID target, FunctionalMember member, CallArguments arguments, FunctionHeader instancedHeader, TypeScope scope) {
24
 	public CallStaticExpression(CodePosition position, ITypeID target, FunctionalMember member, CallArguments arguments, FunctionHeader instancedHeader, TypeScope scope) {
25
-		super(position, member.header.returnType, multiThrow(position, arguments.arguments));
25
+		super(position, instancedHeader.returnType, multiThrow(position, arguments.arguments));
26
 		
26
 		
27
 		this.member = member;
27
 		this.member = member;
28
 		this.target = target;
28
 		this.target = target;

+ 4
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/partial/PartialTypeExpression.java View File

26
 public class PartialTypeExpression implements IPartialExpression {
26
 public class PartialTypeExpression implements IPartialExpression {
27
 	private final CodePosition position;
27
 	private final CodePosition position;
28
 	private final ITypeID type;
28
 	private final ITypeID type;
29
+	private final ITypeID[] typeParameters;
29
 	
30
 	
30
-	public PartialTypeExpression(CodePosition position, ITypeID type) {
31
+	public PartialTypeExpression(CodePosition position, ITypeID type, ITypeID[] typeParameters) {
31
 		this.position = position;
32
 		this.position = position;
32
 		this.type = type;
33
 		this.type = type;
34
+		this.typeParameters = typeParameters;
33
 	}
35
 	}
34
 
36
 
35
 	@Override
37
 	@Override
64
 
66
 
65
 	@Override
67
 	@Override
66
 	public ITypeID[] getGenericCallTypes() {
68
 	public ITypeID[] getGenericCallTypes() {
67
-		return null;
69
+		return typeParameters;
68
 	}
70
 	}
69
 	
71
 	
70
 	@Override
72
 	@Override

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GlobalTypeRegistry.java View File

10
 import java.util.Map;
10
 import java.util.Map;
11
 import org.openzen.zenscript.codemodel.FunctionHeader;
11
 import org.openzen.zenscript.codemodel.FunctionHeader;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
13
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
14
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
14
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
15
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
15
 import static org.openzen.zenscript.codemodel.type.member.TypeMembers.MODIFIER_CONST;
16
 import static org.openzen.zenscript.codemodel.type.member.TypeMembers.MODIFIER_CONST;
119
 	
120
 	
120
 	public DefinitionTypeID getForDefinition(HighLevelDefinition definition, ITypeID[] typeParameters, Map<TypeParameter, ITypeID> outerInstance) {
121
 	public DefinitionTypeID getForDefinition(HighLevelDefinition definition, ITypeID[] typeParameters, Map<TypeParameter, ITypeID> outerInstance) {
121
 		DefinitionTypeID id;
122
 		DefinitionTypeID id;
122
-		if (definition.genericParameters == null && typeParameters == null && outerInstance.isEmpty()) {
123
+		if ((definition instanceof FunctionDefinition) || (definition.genericParameters == null && typeParameters == null && outerInstance.isEmpty())) {
123
 			// make it a static one
124
 			// make it a static one
124
 			id = new StaticDefinitionTypeID(definition);
125
 			id = new StaticDefinitionTypeID(definition);
125
 		} else {
126
 		} else {

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java View File

439
 		if (members.containsKey(name.name))
439
 		if (members.containsKey(name.name))
440
 			return new PartialStaticMemberGroupExpression(position, type, members.get(name.name), name.arguments);
440
 			return new PartialStaticMemberGroupExpression(position, type, members.get(name.name), name.arguments);
441
 		if (innerTypes.containsKey(name.name))
441
 		if (innerTypes.containsKey(name.name))
442
-			return new PartialTypeExpression(position, innerTypes.get(name.name).instance(cache.getRegistry(), name.arguments));
442
+			return new PartialTypeExpression(position, innerTypes.get(name.name).instance(cache.getRegistry(), name.arguments), name.arguments);
443
 		
443
 		
444
 		return null;
444
 		return null;
445
 	}
445
 	}

+ 2
- 2
Linker/src/main/java/org/openzen/zenscript/linker/DefinitionScope.java View File

75
 	public IPartialExpression get(CodePosition position, GenericName name) {
75
 	public IPartialExpression get(CodePosition position, GenericName name) {
76
 		if (members != null) {
76
 		if (members != null) {
77
 			if (members.hasInnerType(name.name))
77
 			if (members.hasInnerType(name.name))
78
-				return new PartialTypeExpression(position, members.getInnerType(position, name));
78
+				return new PartialTypeExpression(position, members.getInnerType(position, name), name.arguments);
79
 			if (members.hasMember(name.name) && !name.hasArguments())
79
 			if (members.hasMember(name.name) && !name.hasArguments())
80
 				return members.getMemberExpression(position, new ThisExpression(position, type), name, true);
80
 				return members.getMemberExpression(position, new ThisExpression(position, type), name, true);
81
 		}
81
 		}
82
 		if (genericParameters.containsKey(name.name) && !name.hasArguments())
82
 		if (genericParameters.containsKey(name.name) && !name.hasArguments())
83
-			return new PartialTypeExpression(position, getTypeRegistry().getGeneric(genericParameters.get(name.name)));
83
+			return new PartialTypeExpression(position, getTypeRegistry().getGeneric(genericParameters.get(name.name)), name.arguments);
84
 		
84
 		
85
 		return outer.get(position, name);
85
 		return outer.get(position, name);
86
 	}
86
 	}

+ 2
- 2
Linker/src/main/java/org/openzen/zenscript/linker/FileScope.java View File

69
 	@Override
69
 	@Override
70
 	public IPartialExpression get(CodePosition position, GenericName name) {
70
 	public IPartialExpression get(CodePosition position, GenericName name) {
71
 		if (importedTypes.containsKey(name.name))
71
 		if (importedTypes.containsKey(name.name))
72
-			return new PartialTypeExpression(position, getTypeRegistry().getForDefinition(importedTypes.get(name.name), name.arguments));
72
+			return new PartialTypeExpression(position, getTypeRegistry().getForDefinition(importedTypes.get(name.name), name.arguments), name.arguments);
73
 		
73
 		
74
 		HighLevelDefinition localDefinition = packageDefinitions.getDefinition(name.name);
74
 		HighLevelDefinition localDefinition = packageDefinitions.getDefinition(name.name);
75
 		if (localDefinition != null)
75
 		if (localDefinition != null)
76
-			return new PartialTypeExpression(position, globalRegistry.getForDefinition(localDefinition, name.arguments));
76
+			return new PartialTypeExpression(position, globalRegistry.getForDefinition(localDefinition, name.arguments), name.arguments);
77
 		
77
 		
78
 		if (globalSymbols.containsKey(name.name)) {
78
 		if (globalSymbols.containsKey(name.name)) {
79
 			IPartialExpression resolution = globalSymbols.get(name.name).getExpression(position, globalRegistry, name.arguments);
79
 			IPartialExpression resolution = globalSymbols.get(name.name).getExpression(position, globalRegistry, name.arguments);

+ 1
- 1
Linker/src/main/java/org/openzen/zenscript/linker/FunctionScope.java View File

64
 			if (header.typeParameters != null) {
64
 			if (header.typeParameters != null) {
65
 				for (TypeParameter parameter : header.typeParameters) {
65
 				for (TypeParameter parameter : header.typeParameters) {
66
 					if (parameter.name.equals(name.name))
66
 					if (parameter.name.equals(name.name))
67
-						return new PartialTypeExpression(position, getTypeRegistry().getGeneric(parameter));
67
+						return new PartialTypeExpression(position, getTypeRegistry().getGeneric(parameter), name.arguments);
68
 				}
68
 				}
69
 			}
69
 			}
70
 		}
70
 		}

+ 1
- 1
Linker/src/main/java/org/openzen/zenscript/linker/GenericFunctionScope.java View File

44
 	@Override
44
 	@Override
45
 	public IPartialExpression get(CodePosition position, GenericName name) {
45
 	public IPartialExpression get(CodePosition position, GenericName name) {
46
 		if (parameters.containsKey(name.name) && name.hasNoArguments())
46
 		if (parameters.containsKey(name.name) && name.hasNoArguments())
47
-			return new PartialTypeExpression(position, getTypeRegistry().getGeneric(parameters.get(name.name)));
47
+			return new PartialTypeExpression(position, getTypeRegistry().getGeneric(parameters.get(name.name)), name.arguments);
48
 		
48
 		
49
 		return outer.get(position, name);
49
 		return outer.get(position, name);
50
 	}
50
 	}

+ 1
- 1
Linker/src/main/java/org/openzen/zenscript/linker/ImplementationScope.java View File

52
 	@Override
52
 	@Override
53
 	public IPartialExpression get(CodePosition position, GenericName name) {
53
 	public IPartialExpression get(CodePosition position, GenericName name) {
54
 		if (members.hasInnerType(name.name))
54
 		if (members.hasInnerType(name.name))
55
-			return new PartialTypeExpression(position, members.getInnerType(position, name));
55
+			return new PartialTypeExpression(position, members.getInnerType(position, name), name.arguments);
56
 		if (members.hasMember(name.name))
56
 		if (members.hasMember(name.name))
57
 			return members.getMemberExpression(position, new ThisExpression(position, outer.getThisType()), name, true);
57
 			return members.getMemberExpression(position, new ThisExpression(position, outer.getThisType()), name, true);
58
 		
58
 		

+ 1
- 1
Linker/src/main/java/org/openzen/zenscript/linker/symbol/TypeSymbol.java View File

25
 	
25
 	
26
 	@Override
26
 	@Override
27
 	public IPartialExpression getExpression(CodePosition position, GlobalTypeRegistry types, ITypeID[] typeArguments) {
27
 	public IPartialExpression getExpression(CodePosition position, GlobalTypeRegistry types, ITypeID[] typeArguments) {
28
-		return new PartialTypeExpression(position, types.getForDefinition(definition, typeArguments));
28
+		return new PartialTypeExpression(position, types.getForDefinition(definition, typeArguments), typeArguments);
29
 	}
29
 	}
30
 
30
 
31
 	@Override
31
 	@Override

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionSuper.java View File

28
 		if (type.getSuperType() == null)
28
 		if (type.getSuperType() == null)
29
 			throw new CompileException(position, CompileExceptionCode.SUPER_CALL_NO_SUPERCLASS, "Type has no superclass");
29
 			throw new CompileException(position, CompileExceptionCode.SUPER_CALL_NO_SUPERCLASS, "Type has no superclass");
30
 		
30
 		
31
-		return new PartialTypeExpression(position, scope.getThisType().getSuperType());
31
+		return new PartialTypeExpression(position, scope.getThisType().getSuperType(), null);
32
 	}
32
 	}
33
 
33
 
34
 	@Override
34
 	@Override

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedTypeExpression.java View File

26
 
26
 
27
 	@Override
27
 	@Override
28
 	public IPartialExpression compile(ExpressionScope scope) {
28
 	public IPartialExpression compile(ExpressionScope scope) {
29
-		return new PartialTypeExpression(position, type.compile(scope));
29
+		return new PartialTypeExpression(position, type.compile(scope), null);
30
 	}
30
 	}
31
 
31
 
32
 	@Override
32
 	@Override

+ 1
- 1
Parser/src/main/java/org/openzen/zenscript/parser/type/ParsedTypeGenericMap.java View File

76
 		@Override
76
 		@Override
77
 		public IPartialExpression get(CodePosition position, GenericName name) {
77
 		public IPartialExpression get(CodePosition position, GenericName name) {
78
 			if (typeParameters.containsKey(name.name) && name.hasNoArguments())
78
 			if (typeParameters.containsKey(name.name) && name.hasNoArguments())
79
-				return new PartialTypeExpression(position, getTypeRegistry().getGeneric(typeParameters.get(name.name)));
79
+				return new PartialTypeExpression(position, getTypeRegistry().getGeneric(typeParameters.get(name.name)), name.arguments);
80
 			
80
 			
81
 			return outer.get(position, name);
81
 			return outer.get(position, name);
82
 		}
82
 		}

Loading…
Cancel
Save