Quellcode durchsuchen

- Improved interface validation: allow definition members to be used as implementation

- Added support for INTERFACE calls in java source code generation
- Minor source code generation quality improvements
Stan Hebben vor 6 Jahren
Ursprung
Commit
54f53f79c1

+ 16
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java Datei anzeigen

6
 package org.openzen.zenscript.codemodel.member;
6
 package org.openzen.zenscript.codemodel.member;
7
 
7
 
8
 import java.util.ArrayList;
8
 import java.util.ArrayList;
9
+import java.util.HashMap;
10
+import java.util.HashSet;
9
 import java.util.List;
11
 import java.util.List;
12
+import java.util.Map;
13
+import java.util.Set;
10
 import org.openzen.zencode.shared.CodePosition;
14
 import org.openzen.zencode.shared.CodePosition;
11
 import org.openzen.zenscript.codemodel.FunctionHeader;
15
 import org.openzen.zenscript.codemodel.FunctionHeader;
12
 import org.openzen.zenscript.codemodel.GenericMapper;
16
 import org.openzen.zenscript.codemodel.GenericMapper;
28
 public class ImplementationMember extends DefinitionMember {
32
 public class ImplementationMember extends DefinitionMember {
29
 	public final TypeID type;
33
 	public final TypeID type;
30
 	public final List<IDefinitionMember> members = new ArrayList<>();
34
 	public final List<IDefinitionMember> members = new ArrayList<>();
35
+	public final Map<DefinitionMemberRef, IDefinitionMember> definitionBorrowedMembers = new HashMap<>(); // contains members from the outer definition to implement interface members
31
 	
36
 	
32
 	public ImplementationMember(CodePosition position, HighLevelDefinition definition, int modifiers, TypeID type) {
37
 	public ImplementationMember(CodePosition position, HighLevelDefinition definition, int modifiers, TypeID type) {
33
 		super(position, definition, modifiers);
38
 		super(position, definition, modifiers);
86
 
91
 
87
 	@Override
92
 	@Override
88
 	public void normalize(TypeScope scope) {
93
 	public void normalize(TypeScope scope) {
89
-		for (IDefinitionMember member : members)
94
+		Set<IDefinitionMember> implemented = new HashSet<>();
95
+		for (IDefinitionMember member : members) {
90
 			member.normalize(scope);
96
 			member.normalize(scope);
97
+			if (member.getOverrides() != null)
98
+				implemented.add(member.getOverrides().getTarget());
99
+		}
100
+		
101
+		TypeMembers interfaceMembers = scope.getTypeMembers(type.stored());
102
+		TypeMembers definitionMembers = scope.getTypeMembers(scope.getTypeRegistry().getForMyDefinition(definition).stored());
103
+		
104
+		definitionBorrowedMembers.clear();
105
+		definitionBorrowedMembers.putAll(interfaceMembers.borrowInterfaceMembersFromDefinition(implemented, definitionMembers));
91
 	}
106
 	}
92
 
107
 
93
 	@Override
108
 	@Override

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberGroup.java Datei anzeigen

87
 		return this.setter == null ? null : this.setter.member;
87
 		return this.setter == null ? null : this.setter.member;
88
 	}
88
 	}
89
 	
89
 	
90
+	public FunctionalMemberRef getMethod(FunctionHeader header) {
91
+		for (TypeMember<FunctionalMemberRef> method : methods)
92
+			if (method.member.getHeader().isEquivalentTo(header))
93
+				return method.member;
94
+		
95
+		return null;
96
+	}
97
+	
90
 	public FunctionalMemberRef getUnaryMethod() {
98
 	public FunctionalMemberRef getUnaryMethod() {
91
 		for (TypeMember<FunctionalMemberRef> method : methods)
99
 		for (TypeMember<FunctionalMemberRef> method : methods)
92
 			if (method.member.getHeader().parameters.length == 0)
100
 			if (method.member.getHeader().parameters.length == 0)

+ 68
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java Datei anzeigen

456
 		return false;
456
 		return false;
457
 	}
457
 	}
458
 	
458
 	
459
+	public Map<DefinitionMemberRef, IDefinitionMember> borrowInterfaceMembersFromDefinition(Set<IDefinitionMember> implemented, TypeMembers definitionMembers) {
460
+		Map<DefinitionMemberRef, IDefinitionMember> result = new HashMap<>();
461
+		
462
+		for (TypeMember<CasterMemberRef> caster : casters) {
463
+			if (implemented.contains(caster.member.member))
464
+				continue;
465
+			
466
+			CasterMemberRef implementation = definitionMembers.getCaster(caster.member.toType);
467
+			if (implementation != null)
468
+				result.put(caster.member, implementation.getTarget());
469
+		}
470
+		for (TypeMember<IteratorMemberRef> iterator : iterators) {
471
+			if (implemented.contains(iterator.member.target))
472
+				continue;
473
+			
474
+			IteratorMemberRef implementation = definitionMembers.getIterator(iterator.member.getLoopVariableCount());
475
+			if (implementation != null)
476
+				result.put(iterator.member, implementation.getTarget());
477
+		}
478
+		for (Map.Entry<String, TypeMemberGroup> entry : members.entrySet()) {
479
+			TypeMemberGroup group = entry.getValue();
480
+			TypeMemberGroup definitionGroup = definitionMembers.getGroup(entry.getKey());
481
+			if (definitionGroup == null)
482
+				continue;
483
+			
484
+			if (group.getGetter() != null) {
485
+				if (!implemented.contains(group.getGetter().member)) {
486
+					GetterMemberRef implementation = definitionGroup.getGetter();
487
+					if (implementation != null)
488
+						result.put(group.getGetter(), implementation.getTarget());
489
+				}
490
+			}
491
+			if (group.getSetter() != null) {
492
+				if (!implemented.contains(group.getSetter().member)) {
493
+					SetterMemberRef implementation = definitionGroup.getSetter();
494
+					if (implementation != null)
495
+						result.put(group.getSetter(), implementation.getTarget());
496
+				}
497
+			}
498
+			for (TypeMember<FunctionalMemberRef> member : group.getMethodMembers()) {
499
+				if (!implemented.contains(member.member.getTarget())) {
500
+					FunctionalMemberRef functional = definitionGroup.getMethod(member.member.getHeader());
501
+					if (functional != null)
502
+						result.put(member.member, functional.getTarget());
503
+				}
504
+			}
505
+		}
506
+		for (Map.Entry<OperatorType, TypeMemberGroup> entry : operators.entrySet()) {
507
+			if (entry.getKey() == OperatorType.DESTRUCTOR)
508
+				continue; // destructor doesn't have to be implemented; the compiler can do so automatically
509
+			
510
+			TypeMemberGroup group = entry.getValue();
511
+			TypeMemberGroup definitionGroup = definitionMembers.getGroup(entry.getKey());
512
+			if (definitionGroup == null)
513
+				continue;
514
+			
515
+			for (TypeMember<FunctionalMemberRef> member : group.getMethodMembers()) {
516
+				if (!implemented.contains(member.member.getTarget())) {
517
+					FunctionalMemberRef functional = definitionGroup.getMethod(member.member.getHeader());
518
+					if (functional != null)
519
+						result.put(member.member, functional.getTarget());
520
+				}
521
+			}
522
+		}
523
+		
524
+		return result;
525
+	}
526
+	
459
 	private Expression castEquivalent(CodePosition position, Expression value, StoredType toType) {
527
 	private Expression castEquivalent(CodePosition position, Expression value, StoredType toType) {
460
 		if (toType.equals(value.type))
528
 		if (toType.equals(value.type))
461
 			return value;
529
 			return value;

+ 7
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceExpressionFormatter.java Datei anzeigen

198
 				FormattingUtils.formatExpansionCall(output, this.target, scope, getValue(target), arguments);
198
 				FormattingUtils.formatExpansionCall(output, this.target, scope, getValue(target), arguments);
199
 				return new ExpressionString(output.toString(), JavaOperator.CALL);
199
 				return new ExpressionString(output.toString(), JavaOperator.CALL);
200
 			}
200
 			}
201
-			case INSTANCE: {
201
+			case INSTANCE:
202
+			case INTERFACE: {
202
 				StringBuilder output = new StringBuilder();
203
 				StringBuilder output = new StringBuilder();
203
 				output.append(getValue(target).value);
204
 				output.append(getValue(target).value);
204
 				output.append('.');
205
 				output.append('.');
1328
 				return compare(call.left, call.right, call.comparison);
1329
 				return compare(call.left, call.right, call.comparison);
1329
 			case UINT_COMPARE:
1330
 			case UINT_COMPARE:
1330
 			case USIZE_COMPARE_UINT:
1331
 			case USIZE_COMPARE_UINT:
1331
-				return compare(callAsStatic("Integer.compareUnsigned", call.left, call.right), call.comparison);
1332
+				if (call.comparison == CompareType.EQ || call.comparison == CompareType.NE) {
1333
+					return compare(call.left, call.right, call.comparison);
1334
+				} else {
1335
+					return compare(callAsStatic("Integer.compareUnsigned", call.left, call.right), call.comparison);
1336
+				}
1332
 			case LONG_COMPARE:
1337
 			case LONG_COMPARE:
1333
 			case LONG_COMPARE_INT:
1338
 			case LONG_COMPARE_INT:
1334
 				return compare(call.left, call.right, call.comparison);
1339
 				return compare(call.left, call.right, call.comparison);

+ 1
- 0
ScriptingExample/scripts/integration.zs Datei anzeigen

1
 import example.TestClass;
1
 import example.TestClass;
2
+import example.TestInterface;
2
 
3
 
3
 val instance = new TestClass("Instance");
4
 val instance = new TestClass("Instance");
4
 println("Name: " + instance.name);
5
 println("Name: " + instance.name);

+ 1
- 8
Shared/src/main/java/compactio/CompactBytesDataInput.java Datei anzeigen

4
 import java.util.Arrays;
4
 import java.util.Arrays;
5
 
5
 
6
 public final class CompactBytesDataInput implements CompactDataInput, AutoCloseable {
6
 public final class CompactBytesDataInput implements CompactDataInput, AutoCloseable {
7
-    private static final int P6 = 1 << 6;
8
     private static final int P7 = 1 << 7;
7
     private static final int P7 = 1 << 7;
9
-    private static final int P13 = 1 << 13;
10
     private static final int P14 = 1 << 14;
8
     private static final int P14 = 1 << 14;
11
-    private static final int P20 = 1 << 20;
12
     private static final int P21 = 1 << 21;
9
     private static final int P21 = 1 << 21;
13
-    private static final int P27 = 1 << 27;
14
     private static final int P28 = 1 << 28;
10
     private static final int P28 = 1 << 28;
15
-    private static final long P34 = 1L << 34;
16
     private static final long P35 = 1L << 35;
11
     private static final long P35 = 1L << 35;
17
-    private static final long P41 = 1L << 41;
18
     private static final long P42 = 1L << 42;
12
     private static final long P42 = 1L << 42;
19
-    private static final long P48 = 1L << 48;
20
     private static final long P49 = 1L << 49;
13
     private static final long P49 = 1L << 49;
21
-    private static final long P55 = 1L << 55;
22
     private static final long P56 = 1L << 56;
14
     private static final long P56 = 1L << 56;
15
+	
23
     private final byte[] data;
16
     private final byte[] data;
24
     private int offset;
17
     private int offset;
25
     
18
     

+ 3
- 0
Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java Datei anzeigen

32
 import org.openzen.zenscript.codemodel.member.OperatorMember;
32
 import org.openzen.zenscript.codemodel.member.OperatorMember;
33
 import org.openzen.zenscript.codemodel.member.SetterMember;
33
 import org.openzen.zenscript.codemodel.member.SetterMember;
34
 import org.openzen.zenscript.codemodel.member.StaticInitializerMember;
34
 import org.openzen.zenscript.codemodel.member.StaticInitializerMember;
35
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
35
 import org.openzen.zenscript.codemodel.scope.TypeScope;
36
 import org.openzen.zenscript.codemodel.scope.TypeScope;
36
 import org.openzen.zenscript.codemodel.statement.Statement;
37
 import org.openzen.zenscript.codemodel.statement.Statement;
37
 import org.openzen.zenscript.codemodel.statement.VarStatement;
38
 import org.openzen.zenscript.codemodel.statement.VarStatement;
255
 		for (IDefinitionMember member : implementation.members)
256
 		for (IDefinitionMember member : implementation.members)
256
 			if (member.getOverrides() != null)
257
 			if (member.getOverrides() != null)
257
 				implemented.add(member.getOverrides().getTarget());
258
 				implemented.add(member.getOverrides().getTarget());
259
+		for (DefinitionMemberRef member : implementation.definitionBorrowedMembers.keySet())
260
+			implemented.add(member.getTarget());
258
 		
261
 		
259
 		TypeMembers members = scope.getTypeMembers(implementation.type.stored(BorrowStorageTag.THIS));
262
 		TypeMembers members = scope.getTypeMembers(implementation.type.stored(BorrowStorageTag.THIS));
260
 		List<IDefinitionMember> unimplemented = members.getUnimplementedMembers(implemented);
263
 		List<IDefinitionMember> unimplemented = members.getUnimplementedMembers(implemented);

Laden…
Abbrechen
Speichern