Procházet zdrojové kódy

- 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 před 6 roky
rodič
revize
54f53f79c1

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

@@ -6,7 +6,11 @@
6 6
 package org.openzen.zenscript.codemodel.member;
7 7
 
8 8
 import java.util.ArrayList;
9
+import java.util.HashMap;
10
+import java.util.HashSet;
9 11
 import java.util.List;
12
+import java.util.Map;
13
+import java.util.Set;
10 14
 import org.openzen.zencode.shared.CodePosition;
11 15
 import org.openzen.zenscript.codemodel.FunctionHeader;
12 16
 import org.openzen.zenscript.codemodel.GenericMapper;
@@ -28,6 +32,7 @@ import org.openzen.zenscript.codemodel.type.TypeID;
28 32
 public class ImplementationMember extends DefinitionMember {
29 33
 	public final TypeID type;
30 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 37
 	public ImplementationMember(CodePosition position, HighLevelDefinition definition, int modifiers, TypeID type) {
33 38
 		super(position, definition, modifiers);
@@ -86,8 +91,18 @@ public class ImplementationMember extends DefinitionMember {
86 91
 
87 92
 	@Override
88 93
 	public void normalize(TypeScope scope) {
89
-		for (IDefinitionMember member : members)
94
+		Set<IDefinitionMember> implemented = new HashSet<>();
95
+		for (IDefinitionMember member : members) {
90 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 108
 	@Override

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

@@ -87,6 +87,14 @@ public class TypeMemberGroup {
87 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 98
 	public FunctionalMemberRef getUnaryMethod() {
91 99
 		for (TypeMember<FunctionalMemberRef> method : methods)
92 100
 			if (method.member.getHeader().parameters.length == 0)

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

@@ -456,6 +456,74 @@ public final class TypeMembers {
456 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 527
 	private Expression castEquivalent(CodePosition position, Expression value, StoredType toType) {
460 528
 		if (toType.equals(value.type))
461 529
 			return value;

+ 7
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaSourceExpressionFormatter.java Zobrazit soubor

@@ -198,7 +198,8 @@ public class JavaSourceExpressionFormatter implements ExpressionVisitor<Expressi
198 198
 				FormattingUtils.formatExpansionCall(output, this.target, scope, getValue(target), arguments);
199 199
 				return new ExpressionString(output.toString(), JavaOperator.CALL);
200 200
 			}
201
-			case INSTANCE: {
201
+			case INSTANCE:
202
+			case INTERFACE: {
202 203
 				StringBuilder output = new StringBuilder();
203 204
 				output.append(getValue(target).value);
204 205
 				output.append('.');
@@ -1328,7 +1329,11 @@ public class JavaSourceExpressionFormatter implements ExpressionVisitor<Expressi
1328 1329
 				return compare(call.left, call.right, call.comparison);
1329 1330
 			case UINT_COMPARE:
1330 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 1337
 			case LONG_COMPARE:
1333 1338
 			case LONG_COMPARE_INT:
1334 1339
 				return compare(call.left, call.right, call.comparison);

+ 1
- 0
ScriptingExample/scripts/integration.zs Zobrazit soubor

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

+ 1
- 8
Shared/src/main/java/compactio/CompactBytesDataInput.java Zobrazit soubor

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

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

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

Loading…
Zrušit
Uložit