From 4d6ec7a72866591f29ac8b8cddcffaac897e8bff Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 01:18:18 +0200 Subject: [PATCH 01/13] WIP: Extract ResolvingType --- .../zenscript/codemodel/GenericName.java | 2 +- .../codemodel/HighLevelDefinition.java | 16 ++++---- .../codemodel/compilation/CompileContext.java | 2 +- .../codemodel/compilation/ResolvedType.java | 11 ----- .../codemodel/compilation/ResolvingType.java | 12 ++++++ .../impl/compiler/LocalTypeImpl.java | 3 +- .../codemodel/definition/ClassDefinition.java | 2 +- .../generic/ParameterSuperBound.java | 4 +- .../codemodel/generic/ParameterTypeBound.java | 4 +- .../codemodel/generic/TypeParameterBound.java | 4 +- .../codemodel/identifiers/TypeSymbol.java | 4 +- .../zenscript/codemodel/type/ArrayTypeID.java | 4 +- .../zenscript/codemodel/type/AssocTypeID.java | 4 +- .../zenscript/codemodel/type/BasicTypeID.java | 6 +-- .../codemodel/type/DefinitionTypeID.java | 4 +- .../codemodel/type/FunctionTypeID.java | 4 +- .../codemodel/type/GenericMapTypeID.java | 4 +- .../codemodel/type/GenericTypeID.java | 9 ++-- .../codemodel/type/InvalidTypeID.java | 4 +- .../codemodel/type/IteratorTypeID.java | 4 +- .../codemodel/type/OptionalTypeID.java | 4 +- .../zenscript/codemodel/type/RangeTypeID.java | 4 +- .../zenscript/codemodel/type/TypeID.java | 12 +++--- .../type/builtin/ArrayTypeSymbol.java | 4 +- .../type/builtin/FunctionTypeSymbol.java | 4 +- .../type/builtin/GenericMapTypeSymbol.java | 4 +- .../codemodel/type/builtin/MapTypeSymbol.java | 4 +- .../type/builtin/OptionalTypeSymbol.java | 8 ++-- .../type/builtin/RangeTypeSymbol.java | 4 +- .../type/builtin/ResultTypeSymbol.java | 4 +- .../type/member/ExpandedResolvedType.java | 15 +++++-- .../type/member/ExpandedResolvingType.java | 39 ++++++++++++++++++ .../type/member/InterfaceResolvedType.java | 19 +++++---- .../type/member/InterfaceResolvingType.java | 41 +++++++++++++++++++ .../codemodel/type/member/MemberSet.java | 12 +++++- .../type/member/OptionalResolvingType.java | 29 +++++++++++++ .../type/member/SubclassResolvedType.java | 11 ++++- .../type/member/SubclassResolvingType.java | 30 ++++++++++++++ .../java/JavaNativeTypeMembersResolving.java | 33 +++++++++++++++ .../module/JavaAnnotatedRuntimeClass.java | 15 +++---- .../zencode/java/module/JavaRuntimeClass.java | 4 +- .../zenscript/parser/ParsedFileCompiler.java | 2 +- .../visitors/DefinitionMemberValidator.java | 2 +- 43 files changed, 308 insertions(+), 103 deletions(-) create mode 100644 CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvingType.java create mode 100644 CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java create mode 100644 CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java create mode 100644 CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java create mode 100644 CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvingType.java create mode 100644 JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java index 2308d55a1..8f861e81a 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java @@ -27,7 +27,7 @@ public GenericName(String name, TypeID[] arguments) { public static Optional getInnerType(TypeID type, List name, int index, List expansions) { while (index < name.size()) { GenericName innerName = name.get(index++); - ResolvedType members = type.resolve(); + ResolvedType members = type.resolve().withExpansions(expansions); Optional inner = members .findInnerType(innerName.name) .map(t -> DefinitionTypeID.create(t, innerName.arguments)); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java index b9d074c58..c425f8241 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java @@ -4,6 +4,7 @@ import org.openzen.zencode.shared.Taggable; import org.openzen.zenscript.codemodel.annotations.DefinitionAnnotation; import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.definition.*; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; @@ -13,10 +14,7 @@ import org.openzen.zenscript.codemodel.type.BasicTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol; -import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; -import org.openzen.zenscript.codemodel.type.member.InterfaceResolvedType; -import org.openzen.zenscript.codemodel.type.member.MemberSet; -import org.openzen.zenscript.codemodel.type.member.SubclassResolvedType; +import org.openzen.zenscript.codemodel.type.member.*; import java.util.ArrayList; import java.util.List; @@ -144,7 +142,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { MemberSet.Builder members = MemberSet.create(type); GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); for (IDefinitionMember member : this.members) { @@ -155,18 +153,18 @@ public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { members.method(new MethodInstance(BuiltinMethodSymbol.OBJECT_SAME, new FunctionHeader(BasicTypeID.BOOL, type), type)); members.method(new MethodInstance(BuiltinMethodSymbol.OBJECT_NOTSAME, new FunctionHeader(BasicTypeID.BOOL, type), type)); resolveAdditional(type, members, mapper); - ResolvedType resolved = members.build(); + ResolvingType resolved = members.build(); if (superType != null) { TypeID instancedSuperType = mapper.map(superType); - ResolvedType superResolved = instancedSuperType.resolve(); - resolved = new SubclassResolvedType(superResolved, resolved, superType); + ResolvingType superResolved = instancedSuperType.resolve(); + resolved = new SubclassResolvingType(superResolved, resolved, superType); } if (interfaces.isEmpty()) { return resolved; } else { - return new InterfaceResolvedType(resolved, interfaces); + return new InterfaceResolvingType(resolved, interfaces); } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java index 41ea854ef..fe7a40b9c 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java @@ -102,7 +102,7 @@ public Optional resolve(CodePosition position, List name) { .flatMap(t -> t.getType(position, this, name.get(0).arguments)) .flatMap(t -> { for (int i = 1; i < name.size(); i++) { - Optional inner = t.resolve().findInnerType(name.get(i).name); + Optional inner = t.resolveIgnoringExpansions().findInnerType(name.get(i).name); if (inner.isPresent()) { t = DefinitionTypeID.create(inner.get(), name.get(i).arguments); } else { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvedType.java index aece87020..41fafe381 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvedType.java @@ -6,13 +6,10 @@ import org.openzen.zenscript.codemodel.expression.CallArguments; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.type.TypeID; -import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; -import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -78,14 +75,6 @@ default boolean canCastImplicitlyTo(TypeID target) { Optional findStaticOperator(OperatorType operator); - default ResolvedType withExpansions(List expansions) { - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(getType()).ifPresent(resolutions::add); - } - return ExpandedResolvedType.of(this, resolutions); - } - interface SwitchMember { SwitchValue toSwitchValue(List bindings); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvingType.java new file mode 100644 index 000000000..9c819eb47 --- /dev/null +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvingType.java @@ -0,0 +1,12 @@ +package org.openzen.zenscript.codemodel.compilation; + +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; +import org.openzen.zenscript.codemodel.type.TypeID; + +import java.util.List; + +public interface ResolvingType { + TypeID getType(); + + ResolvedType withExpansions(List expansions); +} diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java index 1351bfaaf..cd66f09c6 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java @@ -8,6 +8,7 @@ import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; import org.openzen.zenscript.codemodel.type.TypeID; +import java.util.Collections; import java.util.Optional; public class LocalTypeImpl implements LocalType { @@ -45,7 +46,7 @@ public Optional superCall() { return Optional.empty(); if (resolvedSuper == null) - resolvedSuper = thisType.getSuperType().resolve(); + resolvedSuper = thisType.getSuperType().resolveIgnoringExpansions(); return Optional.ofNullable(resolvedSuper) .map(super_ -> super_.getConstructor().map(constructor -> new SuperCallable(thisType.getSuperType(), constructor))); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java index 19cc9b146..2a2f17ae5 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java @@ -43,7 +43,7 @@ public void addDefaultMembers() { boolean hasNoConstructor = members.stream().noneMatch(IDefinitionMember::isConstructor); Optional superConstructor = Optional.ofNullable(getSuperType()) - .flatMap(t -> t.resolve().getConstructor().getSingleOverload()) + .flatMap(t -> t.resolveIgnoringExpansions().getConstructor().getSingleOverload()) .flatMap(AnyMethod::asMethod); if (hasNoConstructor) { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterSuperBound.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterSuperBound.java index dea56bfab..a3b465917 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterSuperBound.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterSuperBound.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.generic; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.type.TypeID; import java.util.Optional; @@ -19,7 +19,7 @@ public String getCanonical() { } @Override - public Optional resolveMembers() { + public Optional resolveMembers() { return Optional.empty(); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterTypeBound.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterTypeBound.java index 132816a81..ee1336db7 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterTypeBound.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/ParameterTypeBound.java @@ -2,7 +2,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.type.TypeID; import java.util.Optional; @@ -22,7 +22,7 @@ public String getCanonical() { } @Override - public Optional resolveMembers() { + public Optional resolveMembers() { return Optional.of(type.resolve()); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/TypeParameterBound.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/TypeParameterBound.java index a6b88084d..84721a1bb 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/TypeParameterBound.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/generic/TypeParameterBound.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.generic; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.type.TypeID; import java.util.Optional; @@ -13,7 +13,7 @@ public interface TypeParameterBound { R accept(C context, GenericParameterBoundVisitorWithContext visitor); - Optional resolveMembers(); + Optional resolveMembers(); boolean matches(TypeID type); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java index 04928beb8..6903417c2 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.identifiers; import org.openzen.zenscript.codemodel.Modifiers; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; @@ -19,7 +19,7 @@ public interface TypeSymbol extends DefinitionSymbol { String getName(); - ResolvedType resolve(TypeID type, TypeID[] typeArguments); + ResolvingType resolve(TypeID type, TypeID[] typeArguments); Optional getOuter(); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java index 72af0d37a..f464336ad 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java @@ -2,7 +2,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.expression.ArrayExpression; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.generic.TypeParameter; @@ -72,7 +72,7 @@ public Optional asArray() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return type.resolve(this, new TypeID[] { elementType }); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java index 5ad5d68c2..eacba9b97 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.type; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.builtin.MapTypeSymbol; @@ -53,7 +53,7 @@ public Optional asAssoc() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return MapTypeSymbol.INSTANCE.resolve(this, new TypeID[] { keyType, valueType }); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java index bee54a3a7..3d18a807e 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java @@ -3,8 +3,8 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.Modifiers; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.expression.*; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; @@ -75,7 +75,7 @@ public boolean isValueType() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { if (members == null) members = BasicTypeMembers.get(this); @@ -183,7 +183,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { if (members == null) members = BasicTypeMembers.get(this); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java index 71963499e..2f3b9d4cc 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.type; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.definition.EnumDefinition; import org.openzen.zenscript.codemodel.definition.StructDefinition; import org.openzen.zenscript.codemodel.definition.VariantDefinition; @@ -53,7 +53,7 @@ public DefinitionTypeID(TypeSymbol definition, TypeID[] typeArguments, Definitio * * @return base type members */ - public ResolvedType resolve() { + public ResolvingType resolve() { return definition.resolve(this, typeArguments); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java index 14759d73f..8af16469d 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java @@ -3,7 +3,7 @@ import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.FunctionParameter; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.builtin.FunctionTypeSymbol; @@ -56,7 +56,7 @@ public Optional asFunction() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return type.resolve(this, TypeID.NONE); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java index 04bf4fa9a..3baf0f3d7 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.type; import org.openzen.zenscript.codemodel.*; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.builtin.GenericMapTypeSymbol; @@ -42,7 +42,7 @@ public Optional asGenericMap() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return GenericMapTypeSymbol.INSTANCE.resolve(this, new TypeID[] { new GenericTypeID(key), value }); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java index 2589db796..eea4870f6 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java @@ -2,10 +2,11 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.generic.TypeParameterBound; import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; -import org.openzen.zenscript.codemodel.type.member.InterfaceResolvedType; +import org.openzen.zenscript.codemodel.type.member.ExpandedResolvingType; import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.ArrayList; @@ -70,12 +71,12 @@ public Optional asGeneric() { } @Override - public ResolvedType resolve() { - List fromBounds = new ArrayList<>(); + public ResolvingType resolve() { + List fromBounds = new ArrayList<>(); for (TypeParameterBound bound : parameter.bounds) { bound.resolveMembers().ifPresent(fromBounds::add); } - return ExpandedResolvedType.of(new MemberSet(this), fromBounds); + return ExpandedResolvingType.of(new MemberSet(this), fromBounds); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java index 96c62b94b..1fdc87864 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java @@ -3,7 +3,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zencode.shared.CompileError; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.member.MemberSet; @@ -44,7 +44,7 @@ public boolean isInvalid() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return new MemberSet(this); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java index 20a29a2d1..20e0b168f 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.type; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.member.MemberSet; @@ -36,7 +36,7 @@ public boolean isValueType() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return new MemberSet(this); // no members yet } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java index 0d83b456a..57d90f660 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java @@ -2,7 +2,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.expression.NullExpression; import org.openzen.zenscript.codemodel.generic.TypeParameter; @@ -34,7 +34,7 @@ public Optional asOptional() { } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return OptionalTypeSymbol.INSTANCE.resolve(this, new TypeID[] { baseType }); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java index 722a3683d..55db04f6b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java @@ -1,7 +1,7 @@ package org.openzen.zenscript.codemodel.type; import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.builtin.RangeTypeSymbol; @@ -37,7 +37,7 @@ public R accept(C context, TypeVisitorWithContext asRange() { return Optional.of(this); } @Override - public ResolvedType resolve() { + public ResolvingType resolve() { return RangeTypeSymbol.INSTANCE.resolve(this, new TypeID[] { baseType }); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java index 1093068cf..be97c0d6a 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java @@ -3,13 +3,11 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; public interface TypeID { TypeID[] NONE = new TypeID[0]; @@ -154,7 +152,11 @@ default Optional asGeneric() { return Optional.empty(); } - ResolvedType resolve(); + ResolvingType resolve(); + + default ResolvedType resolveIgnoringExpansions() { + return this.resolve().withExpansions(Collections.emptyList()); + } default boolean extendsOrImplements(TypeID type) { return false; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java index 5f4961124..3d8a6e94b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java @@ -2,7 +2,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.*; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; @@ -89,7 +89,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { TypeID baseType = typeArguments[0]; GenericMapper mapper = GenericMapper.single(parameters[0], baseType); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/FunctionTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/FunctionTypeSymbol.java index a02441782..f4a57b18f 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/FunctionTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/FunctionTypeSymbol.java @@ -2,8 +2,8 @@ import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.Modifiers; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; @@ -65,7 +65,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { return MemberSet.create(type).method(new MethodInstance(caller)).build(); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/GenericMapTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/GenericMapTypeSymbol.java index 3ea50a27c..5bd394fd8 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/GenericMapTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/GenericMapTypeSymbol.java @@ -2,7 +2,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.*; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; @@ -73,7 +73,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { TypeParameter parameter = ((GenericTypeID)typeArguments[0]).parameter; TypeID value = typeArguments[1]; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java index 77ac3b5c7..26fc56921 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java @@ -2,7 +2,7 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.*; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; @@ -70,7 +70,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type_, TypeID[] typeArguments) { GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); AssocTypeID type = (AssocTypeID) type_; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java index 06893d117..4fb8a1ffa 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java @@ -1,6 +1,7 @@ package org.openzen.zenscript.codemodel.type.builtin; import org.openzen.zenscript.codemodel.Modifiers; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; @@ -8,6 +9,7 @@ import org.openzen.zenscript.codemodel.type.OptionalTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.OptionalResolvedType; +import org.openzen.zenscript.codemodel.type.member.OptionalResolvingType; import java.util.Optional; @@ -57,10 +59,10 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type_, TypeID[] typeArguments) { OptionalTypeID type = (OptionalTypeID) type_; - ResolvedType baseMembers = typeArguments[0].resolve(); - return new OptionalResolvedType(type, baseMembers); + ResolvingType baseMembers = typeArguments[0].resolve(); + return new OptionalResolvingType(type, baseMembers); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java index f2e84c81a..62bd17d15 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java @@ -3,8 +3,8 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.Modifiers; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; @@ -65,7 +65,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type_, TypeID[] typeArguments) { TypeID baseType = typeArguments[0]; GenericMapper mapper = GenericMapper.single(PARAMETER, baseType); RangeTypeID type = (RangeTypeID) type_; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java index 91c4fa2f0..23a55095e 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java @@ -2,8 +2,8 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.Modifiers; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.type.TypeID; @@ -64,7 +64,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { MemberSet members = new MemberSet(type); // TODO return members; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java index 0a77f1882..7047e3831 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java @@ -198,9 +198,18 @@ public Optional findIterator(int variables) { return base.findIterator(variables); } - @Override public ResolvedType withExpansions(List expansions) { - List newExpansions = this.expansions.stream().map(expansion -> expansion.withExpansions(expansions)).collect(Collectors.toList()); - return ExpandedResolvedType.of(base.withExpansions(expansions), newExpansions); + List newExpansions = this.expansions.stream().map(expansion -> { + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion1 : expansions) { + expansion1.resolve(expansion.getType()).ifPresent(resolutions::add); + } + return of(expansion, resolutions); + }).collect(Collectors.toList()); + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(base.getType()).ifPresent(resolutions::add); + } + return ExpandedResolvedType.of(of(base, resolutions), newExpansions); } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java new file mode 100644 index 000000000..e437e74f1 --- /dev/null +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java @@ -0,0 +1,39 @@ +package org.openzen.zenscript.codemodel.type.member; + +import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; +import org.openzen.zenscript.codemodel.type.TypeID; + +import java.util.List; +import java.util.stream.Collectors; + +public class ExpandedResolvingType implements ResolvingType { + + private final ResolvingType base; + private final List expansions; + + private ExpandedResolvingType(ResolvingType base, List expansions) { + this.base = base; + this.expansions = expansions; + } + + public static ResolvingType of(ResolvingType base, List expansions) { + if (expansions.isEmpty()) { + return base; + } + + return new ExpandedResolvingType(base, expansions); + } + + @Override + public TypeID getType() { + return base.getType(); + } + + @Override + public ResolvedType withExpansions(List expansions) { + List newExpansions = this.expansions.stream().map(expansion -> expansion.withExpansions(expansions)).collect(Collectors.toList()); + return ExpandedResolvedType.of(base.withExpansions(expansions), newExpansions); + } +} diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java index 9131cd0e3..f1b4a2047 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java @@ -10,9 +10,7 @@ import org.openzen.zenscript.codemodel.member.ref.ImplementationMemberInstance; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.Collection; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.function.BinaryOperator; import java.util.function.Function; import java.util.stream.Collectors; @@ -23,10 +21,10 @@ public class InterfaceResolvedType implements ResolvedType { private final List implementations; private final Collection implementedInterfaces; - public InterfaceResolvedType(ResolvedType baseType, Collection implementedInterfaces) { + public InterfaceResolvedType(ResolvedType baseType, Collection implementedInterfaces) { this.baseType = baseType; - implementations = Stream.concat(Stream.of(baseType), implementedInterfaces.stream().map(TypeID::resolve)).collect(Collectors.toList()); - this.implementedInterfaces = implementedInterfaces; + implementations = Stream.concat(Stream.of(baseType), implementedInterfaces.stream()).collect(Collectors.toList()); + this.implementedInterfaces = implementedInterfaces.stream().map(ResolvedType::getType).collect(Collectors.toList()); } @Override @@ -130,17 +128,20 @@ public Optional findStaticOperator(OperatorType operator) { return baseType.findStaticOperator(operator); } - @Override public ResolvedType withExpansions(List expansions) { List interfaceExpansions = implementedInterfaces.stream() .flatMap(iface -> expansions.stream().map(expansion -> expansion.resolve(iface)).filter(Optional::isPresent).map(Optional::get)) .collect(Collectors.toList()); + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(baseType.getType()).ifPresent(resolutions::add); + } return new InterfaceResolvedType( ExpandedResolvedType.of( - baseType.withExpansions(expansions), + ExpandedResolvedType.of(baseType, resolutions), interfaceExpansions), - implementedInterfaces); + Collections.emptyList()); } private Optional findFirstInLocalOrImplementedInterfaces(Function> mapper) { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java new file mode 100644 index 000000000..32533217a --- /dev/null +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java @@ -0,0 +1,41 @@ +package org.openzen.zenscript.codemodel.type.member; + +import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; +import org.openzen.zenscript.codemodel.type.TypeID; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +public class InterfaceResolvingType implements ResolvingType { + private final ResolvingType baseType; + private final Collection implementedInterfaces; + + public InterfaceResolvingType(ResolvingType baseType, Collection implementedInterfaces) { + this.baseType = baseType; + this.implementedInterfaces = implementedInterfaces; + } + + @Override + public TypeID getType() { + return baseType.getType(); + } + + @Override + public ResolvedType withExpansions(List expansions) { + List resolvedInterfaces = implementedInterfaces.stream().map(iface -> iface.resolve().withExpansions(expansions)).collect(Collectors.toList()); + + List interfaceExpansions = implementedInterfaces.stream() + .flatMap(iface -> expansions.stream().map(expansion -> expansion.resolve(iface)).filter(Optional::isPresent).map(Optional::get)) + .collect(Collectors.toList()); + + return new InterfaceResolvedType( + ExpandedResolvedType.of( + baseType.withExpansions(expansions), + interfaceExpansions), + resolvedInterfaces); + } +} diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java index 2f144a0c9..7191d7618 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java @@ -2,6 +2,7 @@ import org.openzen.zenscript.codemodel.OperatorType; import org.openzen.zenscript.codemodel.compilation.*; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.MethodID; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance; @@ -12,7 +13,7 @@ import java.util.*; import java.util.stream.Collectors; -public class MemberSet implements ResolvedType { +public class MemberSet implements ResolvedType, ResolvingType { public static Builder create(TypeID type) { MemberSet members = new MemberSet(type); return new Builder(members); @@ -114,6 +115,15 @@ public Optional findStaticOperator(OperatorType operator) { return findStatic(MethodID.staticOperator(operator)); } + @Override + public ResolvedType withExpansions(List expansions) { + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(getType()).ifPresent(resolutions::add); + } + return ExpandedResolvedType.of(this, resolutions); + } + @Override public Optional findField(String name) { return Optional.ofNullable(fields.get(name)); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java new file mode 100644 index 000000000..161a12459 --- /dev/null +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java @@ -0,0 +1,29 @@ +package org.openzen.zenscript.codemodel.type.member; + +import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; +import org.openzen.zenscript.codemodel.type.OptionalTypeID; +import org.openzen.zenscript.codemodel.type.TypeID; + +import java.util.List; + +public class OptionalResolvingType implements ResolvingType { + private final OptionalTypeID type; + private final ResolvingType baseType; + + public OptionalResolvingType(OptionalTypeID type, ResolvingType baseType) { + this.type = type; + this.baseType = baseType; + } + + @Override + public TypeID getType() { + return type; + } + + @Override + public ResolvedType withExpansions(List expansions) { + return new OptionalResolvedType(type, baseType.withExpansions(expansions)); + } +} diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java index 1e7b3722b..a9e54536f 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java @@ -130,9 +130,16 @@ public Optional findIterator(int variables) { return or(resolved.findIterator(variables), () -> superclass.findIterator(variables)); } - @Override public ResolvedType withExpansions(List expansions) { - return new SubclassResolvedType(superclass.withExpansions(expansions), resolved.withExpansions(expansions), supertype); + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(resolved.getType()).ifPresent(resolutions::add); + } + List resolutions1 = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(superclass.getType()).ifPresent(resolutions1::add); + } + return new SubclassResolvedType(ExpandedResolvedType.of(superclass, resolutions1), ExpandedResolvedType.of(resolved, resolutions), supertype); } private static Optional or(Optional value, Supplier> other) { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvingType.java new file mode 100644 index 000000000..0c98a17a3 --- /dev/null +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvingType.java @@ -0,0 +1,30 @@ +package org.openzen.zenscript.codemodel.type.member; + +import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; +import org.openzen.zenscript.codemodel.type.TypeID; + +import java.util.List; + +public class SubclassResolvingType implements ResolvingType { + private final ResolvingType superclass; + private final ResolvingType base; + private final TypeID supertype; + + public SubclassResolvingType(ResolvingType superclass, ResolvingType base, TypeID supertype) { + this.superclass = superclass; + this.base = base; + this.supertype = supertype; + } + + @Override + public TypeID getType() { + return base.getType(); + } + + @Override + public ResolvedType withExpansions(List expansions) { + return new SubclassResolvedType(superclass.withExpansions(expansions), base.withExpansions(expansions), supertype); + } +} diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java new file mode 100644 index 000000000..d0bcf5214 --- /dev/null +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java @@ -0,0 +1,33 @@ +package org.openzen.zencode.java; + +import org.openzen.zencode.java.module.JavaNativeTypeMembers; +import org.openzen.zencode.java.module.JavaNativeTypeTemplate; +import org.openzen.zenscript.codemodel.GenericMapper; +import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; +import org.openzen.zenscript.codemodel.type.TypeID; + +import java.util.List; + +public class JavaNativeTypeMembersResolving implements ResolvingType { + private final JavaNativeTypeTemplate template; + private final TypeID type; + private final GenericMapper mapper; + + public JavaNativeTypeMembersResolving(JavaNativeTypeTemplate template, TypeID type, GenericMapper mapper) { + this.template = template; + this.type = type; + this.mapper = mapper; + } + + @Override + public TypeID getType() { + return type; + } + + @Override + public ResolvedType withExpansions(List expansions) { + return new JavaNativeTypeMembers(template, type, mapper); + } +} diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java index 9d53577d4..f63241e67 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java @@ -1,21 +1,22 @@ package org.openzen.zencode.java.module; +import org.openzen.zencode.java.JavaNativeTypeMembersResolving; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.TypeMatcher; -import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; import org.openzen.zenscript.codemodel.type.member.InterfaceResolvedType; +import org.openzen.zenscript.codemodel.type.member.InterfaceResolvingType; import org.openzen.zenscript.codemodel.type.member.SubclassResolvedType; +import org.openzen.zenscript.codemodel.type.member.SubclassResolvingType; import org.openzen.zenscript.javashared.JavaClass; -import java.util.List; import java.util.Map; import java.util.Collection; import java.util.Optional; -import java.util.stream.Collectors; import java.util.stream.Stream; public class JavaAnnotatedRuntimeClass extends JavaRuntimeClass { @@ -29,16 +30,16 @@ public JavaAnnotatedRuntimeClass(JavaNativeModule module, Class cls, String n } @Override - public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { - ResolvedType resolved = new JavaNativeTypeMembers(getTemplate(), type, GenericMapper.create(getTypeParameters(), typeArguments)); + public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { + ResolvingType resolved = new JavaNativeTypeMembersResolving(getTemplate(), type, GenericMapper.create(getTypeParameters(), typeArguments)); Optional superType = getSupertype(typeArguments); if (superType.isPresent()) { - resolved = new SubclassResolvedType(superType.get().resolve(), resolved, superType.get()); + resolved = new SubclassResolvingType(superType.get().resolve(), resolved, superType.get()); } Collection interfaces = getInterfaces(typeArguments); if (!interfaces.isEmpty()) { - resolved = new InterfaceResolvedType(resolved, interfaces); + resolved = new InterfaceResolvingType(resolved, interfaces); } return resolved; diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java index 1d0884466..e447a6bc4 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java @@ -5,9 +5,9 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.Modifiers; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.ParameterTypeBound; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; @@ -83,7 +83,7 @@ public String getName() { } @Override - public abstract ResolvedType resolve(TypeID type, TypeID[] typeArguments); + public abstract ResolvingType resolve(TypeID type, TypeID[] typeArguments); @Override public TypeParameter[] getTypeParameters() { diff --git a/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java b/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java index 6e086b0d4..e3fa8fe64 100644 --- a/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java +++ b/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java @@ -73,7 +73,7 @@ public Optional resolve(CodePosition position, List name) { if (imports.containsKey(name.get(0).name)) { TypeID type = DefinitionTypeID.create(imports.get(name.get(0).name), name.get(0).arguments); for (int i = 1; i < name.size(); i++) { - Optional inner = type.resolve().findInnerType(name.get(i).name); + Optional inner = type.resolveIgnoringExpansions().findInnerType(name.get(i).name); if (inner.isPresent()) { type = DefinitionTypeID.create(inner.get(), name.get(i).arguments); } else { diff --git a/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java b/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java index e9221b269..9ee3d5a20 100644 --- a/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java +++ b/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java @@ -116,7 +116,7 @@ private void checkConstructorForwarded(ConstructorMember member) { validateThrow(member, member.header, body); if (member.definition.getSuperType() != null && !statementValidator.constructorForwarded) { - if (member.definition.getSuperType().resolve().getConstructor().acceptsZeroArguments()) { + if (member.definition.getSuperType().resolveIgnoringExpansions().getConstructor().acceptsZeroArguments()) { return; } From 11b43295402d7a2b0266139a8773ce233de9b36c Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 01:22:05 +0200 Subject: [PATCH 02/13] Remove unused withExpansion methods --- .../type/member/ExpandedResolvedType.java | 16 ---------------- .../type/member/InterfaceResolvedType.java | 17 ----------------- .../type/member/SubclassResolvedType.java | 13 ------------- 3 files changed, 46 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java index 7047e3831..f895e06c1 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.OperatorType; import org.openzen.zenscript.codemodel.compilation.*; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; @@ -12,7 +11,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; public class ExpandedResolvedType implements ResolvedType { public static ResolvedType of(ResolvedType base, List resolutions) { @@ -198,18 +196,4 @@ public Optional findIterator(int variables) { return base.findIterator(variables); } - public ResolvedType withExpansions(List expansions) { - List newExpansions = this.expansions.stream().map(expansion -> { - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion1 : expansions) { - expansion1.resolve(expansion.getType()).ifPresent(resolutions::add); - } - return of(expansion, resolutions); - }).collect(Collectors.toList()); - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(base.getType()).ifPresent(resolutions::add); - } - return ExpandedResolvedType.of(of(base, resolutions), newExpansions); - } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java index f1b4a2047..2538981d2 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.OperatorType; import org.openzen.zenscript.codemodel.compilation.*; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.member.InterfaceCaster; @@ -128,22 +127,6 @@ public Optional findStaticOperator(OperatorType operator) { return baseType.findStaticOperator(operator); } - public ResolvedType withExpansions(List expansions) { - List interfaceExpansions = implementedInterfaces.stream() - .flatMap(iface -> expansions.stream().map(expansion -> expansion.resolve(iface)).filter(Optional::isPresent).map(Optional::get)) - .collect(Collectors.toList()); - - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(baseType.getType()).ifPresent(resolutions::add); - } - return new InterfaceResolvedType( - ExpandedResolvedType.of( - ExpandedResolvedType.of(baseType, resolutions), - interfaceExpansions), - Collections.emptyList()); - } - private Optional findFirstInLocalOrImplementedInterfaces(Function> mapper) { return implementations.stream() .map(mapper) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java index a9e54536f..66a6d8067 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/SubclassResolvedType.java @@ -8,7 +8,6 @@ import org.openzen.zenscript.codemodel.expression.CallArguments; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.expression.SupertypeCastExpression; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; @@ -130,18 +129,6 @@ public Optional findIterator(int variables) { return or(resolved.findIterator(variables), () -> superclass.findIterator(variables)); } - public ResolvedType withExpansions(List expansions) { - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(resolved.getType()).ifPresent(resolutions::add); - } - List resolutions1 = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(superclass.getType()).ifPresent(resolutions1::add); - } - return new SubclassResolvedType(ExpandedResolvedType.of(superclass, resolutions1), ExpandedResolvedType.of(resolved, resolutions), supertype); - } - private static Optional or(Optional value, Supplier> other) { if (value.isPresent()) return value; From c92b8fad2c596ecd0547b270837d9c499eea5e17 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 01:31:30 +0200 Subject: [PATCH 03/13] Split MemberSet into Resolving and Resolved --- .../definition/ExpansionDefinition.java | 3 +- .../zenscript/codemodel/type/BasicTypeID.java | 2 +- .../codemodel/type/GenericTypeID.java | 2 +- .../codemodel/type/InvalidTypeID.java | 2 +- .../codemodel/type/IteratorTypeID.java | 2 +- .../type/builtin/BasicTypeMembers.java | 4 +- .../type/builtin/ResultTypeSymbol.java | 4 +- .../codemodel/type/member/MemberSet.java | 37 +++++++++++++------ .../type/member/OptionalResolvedType.java | 10 ++--- 9 files changed, 40 insertions(+), 26 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java index ce63f3960..3fac95301 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java @@ -13,6 +13,7 @@ import org.openzen.zenscript.codemodel.type.TypeMatcher; import org.openzen.zenscript.codemodel.type.member.MemberSet; +import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.stream.Stream; @@ -54,6 +55,6 @@ public Optional resolve(TypeID expandingType) { for (IDefinitionMember member : members) member.registerTo(expandingType, resolution, mapper); - return Optional.of(resolution.build()); + return Optional.of(resolution.build().withExpansions(Collections.emptyList())); } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java index 3d18a807e..cf1b7c0a1 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java @@ -36,7 +36,7 @@ public enum BasicTypeID implements TypeID, TypeSymbol { INVALID("invalid"); public final String name; - private MemberSet members; + private ResolvingType members; private Expression defaultValue = null; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java index eea4870f6..8b2a540de 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java @@ -76,7 +76,7 @@ public ResolvingType resolve() { for (TypeParameterBound bound : parameter.bounds) { bound.resolveMembers().ifPresent(fromBounds::add); } - return ExpandedResolvingType.of(new MemberSet(this), fromBounds); + return ExpandedResolvingType.of(MemberSet.create(this).build(), fromBounds); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java index 1fdc87864..b40e8bb8c 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/InvalidTypeID.java @@ -45,7 +45,7 @@ public boolean isInvalid() { @Override public ResolvingType resolve() { - return new MemberSet(this); + return MemberSet.create(this).build(); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java index 20e0b168f..30631b04b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/IteratorTypeID.java @@ -37,7 +37,7 @@ public boolean isValueType() { @Override public ResolvingType resolve() { - return new MemberSet(this); // no members yet + return MemberSet.create(this).build(); // no members yet } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java index 6bd1ffd2a..d515b1d74 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java @@ -21,13 +21,13 @@ public class BasicTypeMembers { private static final MethodID CONSTRUCTOR = MethodID.staticOperator(OperatorType.CONSTRUCTOR); private static final MethodID COMPARE = MethodID.operator(OperatorType.COMPARE); - public static MemberSet get(BasicTypeID type) { + public static MemberSet.Resolving get(BasicTypeID type) { switch (type) { case VOID: case NULL: case UNDETERMINED: case INVALID: - return new MemberSet(type); + return MemberSet.create(type).build(); default: MemberSet.Builder builder = MemberSet.create(type); setup(builder, type); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java index 23a55095e..a2678cb66 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java @@ -65,9 +65,9 @@ public String getName() { @Override public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { - MemberSet members = new MemberSet(type); + MemberSet.Builder members = MemberSet.create(type); // TODO - return members; + return members.build(); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java index 7191d7618..745de8822 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java @@ -13,7 +13,7 @@ import java.util.*; import java.util.stream.Collectors; -public class MemberSet implements ResolvedType, ResolvingType { +public class MemberSet implements ResolvedType { public static Builder create(TypeID type) { MemberSet members = new MemberSet(type); return new Builder(members); @@ -115,15 +115,6 @@ public Optional findStaticOperator(OperatorType operator) { return findStatic(MethodID.staticOperator(operator)); } - @Override - public ResolvedType withExpansions(List expansions) { - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(getType()).ifPresent(resolutions::add); - } - return ExpandedResolvedType.of(this, resolutions); - } - @Override public Optional findField(String name) { return Optional.ofNullable(fields.get(name)); @@ -239,8 +230,30 @@ public boolean hasNoConstructor() { return target.constructors.isEmpty(); } - public MemberSet build() { - return target; + public Resolving build() { + return new Resolving(target); + } + } + + public static class Resolving implements ResolvingType { + private final MemberSet target; + public Resolving(MemberSet target) { + this.target = target; + } + + + @Override + public TypeID getType() { + return target.getType(); + } + + @Override + public ResolvedType withExpansions(List expansions) { + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(getType()).ifPresent(resolutions::add); + } + return ExpandedResolvedType.of(target, resolutions); } } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java index 70c78d951..4e6d31d58 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java @@ -19,22 +19,22 @@ public class OptionalResolvedType implements ResolvedType { private final OptionalTypeID type; private final ResolvedType baseType; - private final MemberSet optionalMembers; + private final ResolvedType optionalMembers; public OptionalResolvedType(OptionalTypeID type, ResolvedType baseType) { this.type = type; this.baseType = baseType; - MemberSet.Builder optionalMembers = MemberSet.create(type); - optionalMembers.method(new MethodInstance( + MemberSet.Builder optionalMembersBuilder = MemberSet.create(type); + optionalMembersBuilder.method(new MethodInstance( BuiltinMethodSymbol.OPTIONAL_IS_NULL, new FunctionHeader(BasicTypeID.BOOL, BasicTypeID.NULL), type)); - optionalMembers.method(new MethodInstance( + optionalMembersBuilder.method(new MethodInstance( BuiltinMethodSymbol.OPTIONAL_IS_NOT_NULL, new FunctionHeader(BasicTypeID.BOOL, BasicTypeID.NULL), type)); - this.optionalMembers = optionalMembers.build(); + this.optionalMembers = optionalMembersBuilder.build().withExpansions(Collections.emptyList()); } @Override From 6f34027fbd1ba146f142fe53bd7b218d0ef7f405 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 01:49:11 +0200 Subject: [PATCH 04/13] Extract Matching of ExpandedResolvedType matching into static method --- .../type/member/ExpandedResolvedType.java | 9 +++++ .../codemodel/type/member/MemberSet.java | 6 +--- .../type/member/OptionalResolvingType.java | 3 +- .../java/JavaNativeTypeMembersResolving.java | 33 ------------------- .../module/JavaAnnotatedRuntimeClass.java | 5 +-- .../java/module/JavaNativeTypeMembers.java | 24 ++++++++++++++ 6 files changed, 37 insertions(+), 43 deletions(-) delete mode 100644 JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java index f895e06c1..a1d69225c 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java @@ -3,6 +3,7 @@ import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.OperatorType; import org.openzen.zenscript.codemodel.compilation.*; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; @@ -13,6 +14,14 @@ import java.util.Optional; public class ExpandedResolvedType implements ResolvedType { + public static ResolvedType resolve(ResolvedType base, List expansions) { + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(base.getType()).ifPresent(resolutions::add); + } + return ExpandedResolvedType.of(base, resolutions); + } + public static ResolvedType of(ResolvedType base, List resolutions) { if (resolutions.isEmpty()) { return base; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java index 745de8822..c1f067b5d 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java @@ -249,11 +249,7 @@ public TypeID getType() { @Override public ResolvedType withExpansions(List expansions) { - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(getType()).ifPresent(resolutions::add); - } - return ExpandedResolvedType.of(target, resolutions); + return ExpandedResolvedType.resolve(target, expansions); } } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java index 161a12459..a2e813ff2 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvingType.java @@ -24,6 +24,7 @@ public TypeID getType() { @Override public ResolvedType withExpansions(List expansions) { - return new OptionalResolvedType(type, baseType.withExpansions(expansions)); + OptionalResolvedType baseWithExpansions = new OptionalResolvedType(type, baseType.withExpansions(expansions)); + return ExpandedResolvedType.resolve(baseWithExpansions, expansions); } } diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java deleted file mode 100644 index d0bcf5214..000000000 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/JavaNativeTypeMembersResolving.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.openzen.zencode.java; - -import org.openzen.zencode.java.module.JavaNativeTypeMembers; -import org.openzen.zencode.java.module.JavaNativeTypeTemplate; -import org.openzen.zenscript.codemodel.GenericMapper; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; -import org.openzen.zenscript.codemodel.compilation.ResolvingType; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; -import org.openzen.zenscript.codemodel.type.TypeID; - -import java.util.List; - -public class JavaNativeTypeMembersResolving implements ResolvingType { - private final JavaNativeTypeTemplate template; - private final TypeID type; - private final GenericMapper mapper; - - public JavaNativeTypeMembersResolving(JavaNativeTypeTemplate template, TypeID type, GenericMapper mapper) { - this.template = template; - this.type = type; - this.mapper = mapper; - } - - @Override - public TypeID getType() { - return type; - } - - @Override - public ResolvedType withExpansions(List expansions) { - return new JavaNativeTypeMembers(template, type, mapper); - } -} diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java index f63241e67..9296d978b 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java @@ -1,6 +1,5 @@ package org.openzen.zencode.java.module; -import org.openzen.zencode.java.JavaNativeTypeMembersResolving; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.compilation.ResolvingType; @@ -8,9 +7,7 @@ import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.TypeMatcher; -import org.openzen.zenscript.codemodel.type.member.InterfaceResolvedType; import org.openzen.zenscript.codemodel.type.member.InterfaceResolvingType; -import org.openzen.zenscript.codemodel.type.member.SubclassResolvedType; import org.openzen.zenscript.codemodel.type.member.SubclassResolvingType; import org.openzen.zenscript.javashared.JavaClass; @@ -31,7 +28,7 @@ public JavaAnnotatedRuntimeClass(JavaNativeModule module, Class cls, String n @Override public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { - ResolvingType resolved = new JavaNativeTypeMembersResolving(getTemplate(), type, GenericMapper.create(getTypeParameters(), typeArguments)); + ResolvingType resolved = new JavaNativeTypeMembers.Resolving(getTemplate(), type, GenericMapper.create(getTypeParameters(), typeArguments)); Optional superType = getSupertype(typeArguments); if (superType.isPresent()) { resolved = new SubclassResolvingType(superType.get().resolve(), resolved, superType.get()); diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaNativeTypeMembers.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaNativeTypeMembers.java index 84c94c562..f0a0cac59 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaNativeTypeMembers.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaNativeTypeMembers.java @@ -4,11 +4,13 @@ import org.openzen.zenscript.codemodel.OperatorType; import org.openzen.zenscript.codemodel.compilation.*; import org.openzen.zenscript.codemodel.expression.Expression; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.MethodID; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.type.TypeID; +import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; import java.util.Collections; import java.util.List; @@ -184,4 +186,26 @@ public Expression setStatic(ExpressionBuilder builder, Expression value) { return builder.setStaticField(field, value); } } + + public static class Resolving implements ResolvingType { + private final JavaNativeTypeTemplate template; + private final TypeID type; + private final GenericMapper mapper; + + public Resolving(JavaNativeTypeTemplate template, TypeID type, GenericMapper mapper) { + this.template = template; + this.type = type; + this.mapper = mapper; + } + + @Override + public TypeID getType() { + return type; + } + + @Override + public ResolvedType withExpansions(List expansions) { + return ExpandedResolvedType.resolve(new JavaNativeTypeMembers(template, type, mapper), expansions); + } + } } From b6ca6f5712205e0727c1d41c5bb467a123b99ffb Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 01:51:25 +0200 Subject: [PATCH 05/13] Update gitignore to exclode xml and xslt Just os that I dont check in test exports --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7c0453250..8ecf1a2f1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ build *.class out/ -.idea \ No newline at end of file +.idea + +*.xslt +*.xml From 92c9bbd1a1c46a7309493edb5e4b83321d2e9ef4 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 01:56:01 +0200 Subject: [PATCH 06/13] Cleanup: Rename method to resolve without expansions --- .../zenscript/codemodel/compilation/CompileContext.java | 2 +- .../codemodel/compilation/impl/compiler/LocalTypeImpl.java | 3 +-- .../zenscript/codemodel/definition/ClassDefinition.java | 2 +- .../zenscript/codemodel/definition/ExpansionDefinition.java | 2 +- .../java/org/openzen/zenscript/codemodel/type/TypeID.java | 2 +- .../openzen/zenscript/codemodel/type/member/MemberSet.java | 4 ++++ .../zenscript/codemodel/type/member/OptionalResolvedType.java | 2 +- .../java/org/openzen/zenscript/parser/ParsedFileCompiler.java | 2 +- .../validator/visitors/DefinitionMemberValidator.java | 2 +- 9 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java index fe7a40b9c..34c3a1de4 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java @@ -102,7 +102,7 @@ public Optional resolve(CodePosition position, List name) { .flatMap(t -> t.getType(position, this, name.get(0).arguments)) .flatMap(t -> { for (int i = 1; i < name.size(); i++) { - Optional inner = t.resolveIgnoringExpansions().findInnerType(name.get(i).name); + Optional inner = t.resolveWithoutExpansions().findInnerType(name.get(i).name); if (inner.isPresent()) { t = DefinitionTypeID.create(inner.get(), name.get(i).arguments); } else { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java index cd66f09c6..bb62ce52b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java @@ -8,7 +8,6 @@ import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.Collections; import java.util.Optional; public class LocalTypeImpl implements LocalType { @@ -46,7 +45,7 @@ public Optional superCall() { return Optional.empty(); if (resolvedSuper == null) - resolvedSuper = thisType.getSuperType().resolveIgnoringExpansions(); + resolvedSuper = thisType.getSuperType().resolveWithoutExpansions(); return Optional.ofNullable(resolvedSuper) .map(super_ -> super_.getConstructor().map(constructor -> new SuperCallable(thisType.getSuperType(), constructor))); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java index 2a2f17ae5..0f16e172a 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java @@ -43,7 +43,7 @@ public void addDefaultMembers() { boolean hasNoConstructor = members.stream().noneMatch(IDefinitionMember::isConstructor); Optional superConstructor = Optional.ofNullable(getSuperType()) - .flatMap(t -> t.resolveIgnoringExpansions().getConstructor().getSingleOverload()) + .flatMap(t -> t.resolveWithoutExpansions().getConstructor().getSingleOverload()) .flatMap(AnyMethod::asMethod); if (hasNoConstructor) { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java index 3fac95301..8511ddab2 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java @@ -55,6 +55,6 @@ public Optional resolve(TypeID expandingType) { for (IDefinitionMember member : members) member.registerTo(expandingType, resolution, mapper); - return Optional.of(resolution.build().withExpansions(Collections.emptyList())); + return Optional.of(resolution.buildWithoutExpansions()); } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java index be97c0d6a..de8dddb79 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/TypeID.java @@ -154,7 +154,7 @@ default Optional asGeneric() { ResolvingType resolve(); - default ResolvedType resolveIgnoringExpansions() { + default ResolvedType resolveWithoutExpansions() { return this.resolve().withExpansions(Collections.emptyList()); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java index c1f067b5d..627950ff3 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java @@ -233,6 +233,10 @@ public boolean hasNoConstructor() { public Resolving build() { return new Resolving(target); } + + public ResolvedType buildWithoutExpansions() { + return build().withExpansions(Collections.emptyList()); + } } public static class Resolving implements ResolvingType { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java index 4e6d31d58..d10c5580c 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/OptionalResolvedType.java @@ -34,7 +34,7 @@ public OptionalResolvedType(OptionalTypeID type, ResolvedType baseType) { BuiltinMethodSymbol.OPTIONAL_IS_NOT_NULL, new FunctionHeader(BasicTypeID.BOOL, BasicTypeID.NULL), type)); - this.optionalMembers = optionalMembersBuilder.build().withExpansions(Collections.emptyList()); + this.optionalMembers = optionalMembersBuilder.buildWithoutExpansions(); } @Override diff --git a/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java b/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java index e3fa8fe64..77f4f6104 100644 --- a/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java +++ b/Parser/src/main/java/org/openzen/zenscript/parser/ParsedFileCompiler.java @@ -73,7 +73,7 @@ public Optional resolve(CodePosition position, List name) { if (imports.containsKey(name.get(0).name)) { TypeID type = DefinitionTypeID.create(imports.get(name.get(0).name), name.get(0).arguments); for (int i = 1; i < name.size(); i++) { - Optional inner = type.resolveIgnoringExpansions().findInnerType(name.get(i).name); + Optional inner = type.resolveWithoutExpansions().findInnerType(name.get(i).name); if (inner.isPresent()) { type = DefinitionTypeID.create(inner.get(), name.get(i).arguments); } else { diff --git a/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java b/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java index 9ee3d5a20..eaf149200 100644 --- a/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java +++ b/Validator/src/main/java/org/openzen/zenscript/validator/visitors/DefinitionMemberValidator.java @@ -116,7 +116,7 @@ private void checkConstructorForwarded(ConstructorMember member) { validateThrow(member, member.header, body); if (member.definition.getSuperType() != null && !statementValidator.constructorForwarded) { - if (member.definition.getSuperType().resolveIgnoringExpansions().getConstructor().acceptsZeroArguments()) { + if (member.definition.getSuperType().resolveWithoutExpansions().getConstructor().acceptsZeroArguments()) { return; } From 7c0bb7233c1a4eed8973fc83569f1b05e782efd9 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 02:07:35 +0200 Subject: [PATCH 07/13] refactor: use anonymous class instead --- .../codemodel/type/GenericTypeID.java | 18 ++++++++- .../type/member/ExpandedResolvingType.java | 39 ------------------- 2 files changed, 16 insertions(+), 41 deletions(-) delete mode 100644 CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java index 8b2a540de..7bd9e38be 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericTypeID.java @@ -5,13 +5,14 @@ import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.generic.TypeParameterBound; +import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; -import org.openzen.zenscript.codemodel.type.member.ExpandedResolvingType; import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public class GenericTypeID implements TypeID { public final TypeParameter parameter; @@ -76,7 +77,20 @@ public ResolvingType resolve() { for (TypeParameterBound bound : parameter.bounds) { bound.resolveMembers().ifPresent(fromBounds::add); } - return ExpandedResolvingType.of(MemberSet.create(this).build(), fromBounds); + + return new ResolvingType() { + @Override + public TypeID getType() { + return GenericTypeID.this; + } + + @Override + public ResolvedType withExpansions(List expansions) { + List newExpansions = fromBounds.stream().map(expansion -> expansion.withExpansions(expansions)).collect(Collectors.toList()); + ResolvedType base = MemberSet.create(getType()).build().withExpansions(expansions); + return ExpandedResolvedType.of(base, newExpansions); + } + }; } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java deleted file mode 100644 index e437e74f1..000000000 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvingType.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.openzen.zenscript.codemodel.type.member; - -import org.openzen.zenscript.codemodel.compilation.ResolvedType; -import org.openzen.zenscript.codemodel.compilation.ResolvingType; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; -import org.openzen.zenscript.codemodel.type.TypeID; - -import java.util.List; -import java.util.stream.Collectors; - -public class ExpandedResolvingType implements ResolvingType { - - private final ResolvingType base; - private final List expansions; - - private ExpandedResolvingType(ResolvingType base, List expansions) { - this.base = base; - this.expansions = expansions; - } - - public static ResolvingType of(ResolvingType base, List expansions) { - if (expansions.isEmpty()) { - return base; - } - - return new ExpandedResolvingType(base, expansions); - } - - @Override - public TypeID getType() { - return base.getType(); - } - - @Override - public ResolvedType withExpansions(List expansions) { - List newExpansions = this.expansions.stream().map(expansion -> expansion.withExpansions(expansions)).collect(Collectors.toList()); - return ExpandedResolvedType.of(base.withExpansions(expansions), newExpansions); - } -} From 411a8b7a56b59e8424f5148b3b39917defa54ea8 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 09:25:53 +0200 Subject: [PATCH 08/13] refactor: remove unecessary type arguments from resolve method Resolving types know their TypeID by now --- .../zenscript/codemodel/HighLevelDefinition.java | 6 ++++-- .../codemodel/definition/ExpansionDefinition.java | 1 - .../zenscript/codemodel/identifiers/TypeSymbol.java | 2 +- .../zenscript/codemodel/type/ArrayTypeID.java | 2 +- .../zenscript/codemodel/type/AssocTypeID.java | 2 +- .../zenscript/codemodel/type/BasicTypeID.java | 3 +-- .../zenscript/codemodel/type/DefinitionTypeID.java | 2 +- .../zenscript/codemodel/type/FunctionTypeID.java | 2 +- .../zenscript/codemodel/type/GenericMapTypeID.java | 13 ++++++------- .../zenscript/codemodel/type/OptionalTypeID.java | 2 +- .../zenscript/codemodel/type/RangeTypeID.java | 4 ++-- .../codemodel/type/builtin/ArrayTypeSymbol.java | 3 ++- .../codemodel/type/builtin/FunctionTypeSymbol.java | 3 ++- .../type/builtin/GenericMapTypeSymbol.java | 3 ++- .../codemodel/type/builtin/MapTypeSymbol.java | 5 +++-- .../codemodel/type/builtin/OptionalTypeSymbol.java | 8 +++----- .../codemodel/type/builtin/RangeTypeSymbol.java | 4 ++-- .../codemodel/type/builtin/ResultTypeSymbol.java | 4 +++- .../java/module/JavaAnnotatedRuntimeClass.java | 3 ++- .../zencode/java/module/JavaRuntimeClass.java | 2 +- 20 files changed, 39 insertions(+), 35 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java index c425f8241..c7d2154e2 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java @@ -3,7 +3,6 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zencode.shared.Taggable; import org.openzen.zenscript.codemodel.annotations.DefinitionAnnotation; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.definition.*; import org.openzen.zenscript.codemodel.generic.TypeParameter; @@ -12,6 +11,7 @@ import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; import org.openzen.zenscript.codemodel.member.*; import org.openzen.zenscript.codemodel.type.BasicTypeID; +import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol; import org.openzen.zenscript.codemodel.type.member.*; @@ -142,7 +142,9 @@ public String getName() { } @Override - public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID[] typeArguments) { + TypeID type = DefinitionTypeID.create(this, typeArguments); + MemberSet.Builder members = MemberSet.create(type); GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); for (IDefinitionMember member : this.members) { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java index 8511ddab2..862847944 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java @@ -13,7 +13,6 @@ import org.openzen.zenscript.codemodel.type.TypeMatcher; import org.openzen.zenscript.codemodel.type.member.MemberSet; -import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.stream.Stream; diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java index 6903417c2..8978d12aa 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/identifiers/TypeSymbol.java @@ -19,7 +19,7 @@ public interface TypeSymbol extends DefinitionSymbol { String getName(); - ResolvingType resolve(TypeID type, TypeID[] typeArguments); + ResolvingType resolve(TypeID[] typeArguments); Optional getOuter(); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java index f464336ad..e9188e094 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/ArrayTypeID.java @@ -73,7 +73,7 @@ public Optional asArray() { @Override public ResolvingType resolve() { - return type.resolve(this, new TypeID[] { elementType }); + return type.resolve(new TypeID[] { elementType }); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java index eacba9b97..5ec2f5e4a 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/AssocTypeID.java @@ -54,7 +54,7 @@ public Optional asAssoc() { @Override public ResolvingType resolve() { - return MapTypeSymbol.INSTANCE.resolve(this, new TypeID[] { keyType, valueType }); + return MapTypeSymbol.INSTANCE.resolve(new TypeID[] { keyType, valueType }); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java index cf1b7c0a1..652af866c 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java @@ -9,7 +9,6 @@ import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.type.builtin.BasicTypeMembers; -import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.List; import java.util.Optional; @@ -183,7 +182,7 @@ public String getName() { } @Override - public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID[] typeArguments) { if (members == null) members = BasicTypeMembers.get(this); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java index 2f3b9d4cc..f2387e999 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java @@ -54,7 +54,7 @@ public DefinitionTypeID(TypeSymbol definition, TypeID[] typeArguments, Definitio * @return base type members */ public ResolvingType resolve() { - return definition.resolve(this, typeArguments); + return definition.resolve(typeArguments); } public boolean hasTypeParameters() { diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java index 8af16469d..cb9f467e6 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/FunctionTypeID.java @@ -57,7 +57,7 @@ public Optional asFunction() { @Override public ResolvingType resolve() { - return type.resolve(this, TypeID.NONE); + return type.resolve(TypeID.NONE); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java index 3baf0f3d7..8601703a3 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/GenericMapTypeID.java @@ -43,7 +43,7 @@ public Optional asGenericMap() { @Override public ResolvingType resolve() { - return GenericMapTypeSymbol.INSTANCE.resolve(this, new TypeID[] { new GenericTypeID(key), value }); + return GenericMapTypeSymbol.INSTANCE.resolve(new TypeID[] { new GenericTypeID(key), value }); } @Override @@ -64,12 +64,11 @@ public void extractTypeParameters(List typeParameters) { @Override public String toString() { - StringBuilder result = new StringBuilder(); - result.append(value.toStringSuffixed()); - result.append("[<"); - result.append(key.toString()); - result.append(">]"); - return result.toString(); + String result = value.toStringSuffixed() + + "[<" + + key.toString() + + ">]"; + return result; } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java index 57d90f660..39dc04e37 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java @@ -35,7 +35,7 @@ public Optional asOptional() { @Override public ResolvingType resolve() { - return OptionalTypeSymbol.INSTANCE.resolve(this, new TypeID[] { baseType }); + return OptionalTypeSymbol.INSTANCE.resolve(new TypeID[] { baseType }); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java index 55db04f6b..5ec3c8efa 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/RangeTypeID.java @@ -38,7 +38,7 @@ public R accept(C context, TypeVisitorWithContext parameterFilled = new HashMap<>(); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java index 26fc56921..cf9c3ff07 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/MapTypeSymbol.java @@ -9,6 +9,7 @@ import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; import org.openzen.zenscript.codemodel.type.AssocTypeID; +import org.openzen.zenscript.codemodel.type.GenericMapTypeID; import org.openzen.zenscript.codemodel.type.GenericTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.MemberSet; @@ -70,9 +71,9 @@ public String getName() { } @Override - public ResolvingType resolve(TypeID type_, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID[] typeArguments) { GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); - AssocTypeID type = (AssocTypeID) type_; + AssocTypeID type = new AssocTypeID(typeArguments[0], typeArguments[1]); MemberSet.Builder members = MemberSet.create(type); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java index 4fb8a1ffa..93ee2e3f7 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/OptionalTypeSymbol.java @@ -3,12 +3,10 @@ import org.openzen.zenscript.codemodel.Modifiers; import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; -import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.type.OptionalTypeID; import org.openzen.zenscript.codemodel.type.TypeID; -import org.openzen.zenscript.codemodel.type.member.OptionalResolvedType; import org.openzen.zenscript.codemodel.type.member.OptionalResolvingType; import java.util.Optional; @@ -59,15 +57,15 @@ public String getName() { } @Override - public ResolvingType resolve(TypeID type_, TypeID[] typeArguments) { - OptionalTypeID type = (OptionalTypeID) type_; + public ResolvingType resolve(TypeID[] typeArguments) { + OptionalTypeID type = new OptionalTypeID(typeArguments[0]); ResolvingType baseMembers = typeArguments[0].resolve(); return new OptionalResolvingType(type, baseMembers); } @Override public TypeParameter[] getTypeParameters() { - return new TypeParameter[0]; + return TypeParameter.NONE; } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java index 62bd17d15..83fe726a6 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/RangeTypeSymbol.java @@ -65,10 +65,10 @@ public String getName() { } @Override - public ResolvingType resolve(TypeID type_, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID[] typeArguments) { TypeID baseType = typeArguments[0]; GenericMapper mapper = GenericMapper.single(PARAMETER, baseType); - RangeTypeID type = (RangeTypeID) type_; + RangeTypeID type = new RangeTypeID(baseType); MemberSet.Builder members = MemberSet.create(type); members.method(mapper.map(type, BuiltinMethodSymbol.RANGE_FROM)); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java index a2678cb66..5197eb8d1 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ResultTypeSymbol.java @@ -6,6 +6,7 @@ import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.generic.TypeParameter; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; +import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.MemberSet; @@ -64,7 +65,8 @@ public String getName() { } @Override - public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID[] typeArguments) { + TypeID type = DefinitionTypeID.create(this, typeArguments); MemberSet.Builder members = MemberSet.create(type); // TODO return members.build(); diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java index 9296d978b..9f2a2fbf5 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java @@ -27,7 +27,8 @@ public JavaAnnotatedRuntimeClass(JavaNativeModule module, Class cls, String n } @Override - public ResolvingType resolve(TypeID type, TypeID[] typeArguments) { + public ResolvingType resolve(TypeID[] typeArguments) { + TypeID type = DefinitionTypeID.create(this, typeArguments); ResolvingType resolved = new JavaNativeTypeMembers.Resolving(getTemplate(), type, GenericMapper.create(getTypeParameters(), typeArguments)); Optional superType = getSupertype(typeArguments); if (superType.isPresent()) { diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java index e447a6bc4..5c6bd108f 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaRuntimeClass.java @@ -83,7 +83,7 @@ public String getName() { } @Override - public abstract ResolvingType resolve(TypeID type, TypeID[] typeArguments); + public abstract ResolvingType resolve(TypeID[] typeArguments); @Override public TypeParameter[] getTypeParameters() { From a8b7a13a359c02436891d19eafe5730c61d1bdb0 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 09:35:12 +0200 Subject: [PATCH 09/13] Refactor extract `InterfaceResolvingType.of` --- .../zenscript/codemodel/HighLevelDefinition.java | 6 +----- .../codemodel/type/member/InterfaceResolvingType.java | 10 +++++++++- .../zencode/java/module/JavaAnnotatedRuntimeClass.java | 6 +----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java index c7d2154e2..1748248dc 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java @@ -163,11 +163,7 @@ public ResolvingType resolve(TypeID[] typeArguments) { resolved = new SubclassResolvingType(superResolved, resolved, superType); } - if (interfaces.isEmpty()) { - return resolved; - } else { - return new InterfaceResolvingType(resolved, interfaces); - } + return InterfaceResolvingType.of(resolved, interfaces); } protected void resolveAdditional(TypeID type, MemberSet.Builder members, GenericMapper mapper) {} diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java index 32533217a..c588d701b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java @@ -14,7 +14,15 @@ public class InterfaceResolvingType implements ResolvingType { private final ResolvingType baseType; private final Collection implementedInterfaces; - public InterfaceResolvingType(ResolvingType baseType, Collection implementedInterfaces) { + public static ResolvingType of(ResolvingType baseType, Collection implementedInterfaces) { + if(implementedInterfaces.isEmpty()){ + return baseType; + } + + return new InterfaceResolvingType(baseType, implementedInterfaces); + } + + private InterfaceResolvingType(ResolvingType baseType, Collection implementedInterfaces) { this.baseType = baseType; this.implementedInterfaces = implementedInterfaces; } diff --git a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java index 9f2a2fbf5..110fcd618 100644 --- a/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java +++ b/JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaAnnotatedRuntimeClass.java @@ -36,11 +36,7 @@ public ResolvingType resolve(TypeID[] typeArguments) { } Collection interfaces = getInterfaces(typeArguments); - if (!interfaces.isEmpty()) { - resolved = new InterfaceResolvingType(resolved, interfaces); - } - - return resolved; + return InterfaceResolvingType.of(resolved, interfaces); } @Override From 1bc4e819a788f4a8eab2f0e9dbe6e013644a05c6 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 09:43:57 +0200 Subject: [PATCH 10/13] cleanup: BasicTypeId --- .../zenscript/formatter/TypeFormatter.java | 2 +- .../zenscript/codemodel/type/BasicTypeID.java | 16 ++++++---------- .../codemodel/type/builtin/BasicTypeMembers.java | 7 +++---- .../zenscript/javabytecode/JavaMangler.java | 2 +- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java b/CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java index c28d49006..4627f3f4a 100644 --- a/CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java +++ b/CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java @@ -23,7 +23,7 @@ public String format(TypeID type) { @Override public String visitBasic(BasicTypeID basic) { - return basic.name; + return basic.getName(); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java index 652af866c..dd2a342dd 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/BasicTypeID.java @@ -34,7 +34,7 @@ public enum BasicTypeID implements TypeID, TypeSymbol { UNDETERMINED("undetermined"), INVALID("invalid"); - public final String name; + private final String name; private ResolvingType members; private Expression defaultValue = null; @@ -63,11 +63,6 @@ public R accept(C context, TypeVisitorWithContext typeParameters) { - + // BasicTypeIDs don't have type parameters } private Expression generateDefaultValue() { @@ -183,10 +178,11 @@ public String getName() { @Override public ResolvingType resolve(TypeID[] typeArguments) { - if (members == null) - members = BasicTypeMembers.get(this); + if(typeArguments.length > 0) { + throw new IllegalArgumentException(this + " cannot have type arguments"); + } - return members; + return this.resolve(); } @Override diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java index d515b1d74..389baa854 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java @@ -6,6 +6,7 @@ import org.openzen.zenscript.codemodel.compilation.CastedEval; import org.openzen.zenscript.codemodel.compilation.CastedExpression; import org.openzen.zenscript.codemodel.compilation.CompileErrors; +import org.openzen.zenscript.codemodel.compilation.ResolvingType; import org.openzen.zenscript.codemodel.expression.CompareExpression; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.identifiers.MethodID; @@ -21,7 +22,7 @@ public class BasicTypeMembers { private static final MethodID CONSTRUCTOR = MethodID.staticOperator(OperatorType.CONSTRUCTOR); private static final MethodID COMPARE = MethodID.operator(OperatorType.COMPARE); - public static MemberSet.Resolving get(BasicTypeID type) { + public static ResolvingType get(BasicTypeID type) { switch (type) { case VOID: case NULL: @@ -600,9 +601,7 @@ private static void comparator(MemberSet.Builder builder) { new MethodInstance(getComparator((BasicTypeID) typeID)), type); return new CastedExpression(CastedExpression.Level.EXACT, value); - }).orElseGet(() -> { - return new CastedExpression(CastedExpression.Level.INVALID, compiler.at(position).invalid(CompileErrors.cannotCompare(left.type, rightCompiled.type))); - }); + }).orElseGet(() -> new CastedExpression(CastedExpression.Level.INVALID, compiler.at(position).invalid(CompileErrors.cannotCompare(left.type, rightCompiled.type)))); })); } diff --git a/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaMangler.java b/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaMangler.java index f68bbafd9..fe3edbc75 100644 --- a/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaMangler.java +++ b/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaMangler.java @@ -171,7 +171,7 @@ private String mangleExpansionTarget(final TypeID target) { return this.oneOf( () -> this.mangleIf(EXP_TAR_MANGLE_ARRAY_ID, target::asArray, it -> Character.toString('D') + it.dimension + 'C' + this.mangleExpansionTarget(it.elementType)), () -> this.mangleIf(EXP_TAR_MANGLE_ASSOC_ID, target::asAssoc, it -> this.mangleKv(this.mangleExpansionTarget(it.keyType), this.mangleExpansionTarget(it.valueType))), - () -> this.mangleIf(EXP_TAR_MANGLE_BASIC_ID, target, BasicTypeID.class, it -> it.name), + () -> this.mangleIf(EXP_TAR_MANGLE_BASIC_ID, target, BasicTypeID.class, BasicTypeID::getName), () -> this.mangleIf(EXP_TAR_MANGLE_DEFINITION_ID, target::asDefinition, it -> { final String name = it.definition.getName(); final int simpleNameBegin = name.lastIndexOf('.'); From 2dc4ef21292af7de44298f215713008be57730ff Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 31 Aug 2024 10:07:39 +0200 Subject: [PATCH 11/13] Cleanup: Extract InterfaceResolvedType.of and some reformattings --- .../codemodel/type/builtin/ArrayTypeSymbol.java | 16 ++++++++-------- .../type/member/InterfaceResolvedType.java | 9 ++++++++- .../type/member/InterfaceResolvingType.java | 7 ++++--- .../codemodel/type/member/MemberSet.java | 15 ++++++++------- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java index 046d541d9..8678bc463 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/ArrayTypeSymbol.java @@ -20,7 +20,7 @@ import static org.openzen.zenscript.codemodel.type.BasicTypeID.USIZE; public class ArrayTypeSymbol implements TypeSymbol { - private final Modifiers MODIFIERS = Modifiers.PUBLIC; + private static final Modifiers MODIFIERS = Modifiers.PUBLIC; public static final TypeParameter ELEMENT = new TypeParameter(BUILTIN, "E"); public static final GenericTypeID ELEMENT_TYPE = new GenericTypeID(ELEMENT); @@ -114,19 +114,19 @@ public ResolvingType resolve(TypeID[] typeArguments) { if (baseType == BYTE) members.method(new MethodInstance(BuiltinMethodSymbol.BYTE_ARRAY_AS_SBYTE_ARRAY)); - if (baseType == SBYTE) + else if (baseType == SBYTE) members.method(new MethodInstance(BuiltinMethodSymbol.SBYTE_ARRAY_AS_BYTE_ARRAY)); - if (baseType == SHORT) + else if (baseType == SHORT) members.method(new MethodInstance(BuiltinMethodSymbol.SHORT_ARRAY_AS_USHORT_ARRAY)); - if (baseType == USHORT) + else if (baseType == USHORT) members.method(new MethodInstance(BuiltinMethodSymbol.USHORT_ARRAY_AS_SHORT_ARRAY)); - if (baseType == INT) + else if (baseType == INT) members.method(new MethodInstance(BuiltinMethodSymbol.INT_ARRAY_AS_UINT_ARRAY)); - if (baseType == UINT) + else if (baseType == UINT) members.method(new MethodInstance(BuiltinMethodSymbol.UINT_ARRAY_AS_INT_ARRAY)); - if (baseType == LONG) + else if (baseType == LONG) members.method(new MethodInstance(BuiltinMethodSymbol.LONG_ARRAY_AS_ULONG_ARRAY)); - if (baseType == ULONG) + else if (baseType == ULONG) members.method(new MethodInstance(BuiltinMethodSymbol.ULONG_ARRAY_AS_LONG_ARRAY)); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java index 2538981d2..8e060d57f 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvedType.java @@ -20,12 +20,19 @@ public class InterfaceResolvedType implements ResolvedType { private final List implementations; private final Collection implementedInterfaces; - public InterfaceResolvedType(ResolvedType baseType, Collection implementedInterfaces) { + private InterfaceResolvedType(ResolvedType baseType, Collection implementedInterfaces) { this.baseType = baseType; implementations = Stream.concat(Stream.of(baseType), implementedInterfaces.stream()).collect(Collectors.toList()); this.implementedInterfaces = implementedInterfaces.stream().map(ResolvedType::getType).collect(Collectors.toList()); } + public static ResolvedType of(ResolvedType baseType, Collection implementedInterfaces) { + if(implementedInterfaces.isEmpty()) + return baseType; + + return new InterfaceResolvedType(baseType, implementedInterfaces); + } + @Override public TypeID getType() { return baseType.getType(); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java index c588d701b..33d27334b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/InterfaceResolvingType.java @@ -15,7 +15,7 @@ public class InterfaceResolvingType implements ResolvingType { private final Collection implementedInterfaces; public static ResolvingType of(ResolvingType baseType, Collection implementedInterfaces) { - if(implementedInterfaces.isEmpty()){ + if (implementedInterfaces.isEmpty()) { return baseType; } @@ -40,10 +40,11 @@ public ResolvedType withExpansions(List expansions) { .flatMap(iface -> expansions.stream().map(expansion -> expansion.resolve(iface)).filter(Optional::isPresent).map(Optional::get)) .collect(Collectors.toList()); - return new InterfaceResolvedType( + return InterfaceResolvedType.of( ExpandedResolvedType.of( baseType.withExpansions(expansions), interfaceExpansions), - resolvedInterfaces); + resolvedInterfaces + ); } } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java index 627950ff3..1a76fb2d0 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java @@ -14,10 +14,6 @@ import java.util.stream.Collectors; public class MemberSet implements ResolvedType { - public static Builder create(TypeID type) { - MemberSet members = new MemberSet(type); - return new Builder(members); - } private final TypeID type; private final List constructors = new ArrayList<>(); @@ -30,10 +26,15 @@ public static Builder create(TypeID type) { private final List iterators = new ArrayList<>(); private final List comparators = new ArrayList<>(); - public MemberSet(TypeID type) { + private MemberSet(TypeID type) { this.type = type; } + public static Builder create(TypeID type) { + MemberSet members = new MemberSet(type); + return new Builder(members); + } + @Override public TypeID getType() { return type; @@ -230,7 +231,7 @@ public boolean hasNoConstructor() { return target.constructors.isEmpty(); } - public Resolving build() { + public ResolvingType build() { return new Resolving(target); } @@ -239,7 +240,7 @@ public ResolvedType buildWithoutExpansions() { } } - public static class Resolving implements ResolvingType { + private static class Resolving implements ResolvingType { private final MemberSet target; public Resolving(MemberSet target) { this.target = target; From 4be62264fa662045ff44b0a75bc70b5a95493b87 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sun, 1 Sep 2024 21:08:12 +0200 Subject: [PATCH 12/13] test: add two more tests for setters --- .../resources/zencode_tests/classes/setter_2.zc | 15 +++++++++++++++ .../resources/zencode_tests/classes/setter_3.zc | 13 +++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_2.zc create mode 100644 ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_3.zc diff --git a/ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_2.zc b/ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_2.zc new file mode 100644 index 000000000..91e52e939 --- /dev/null +++ b/ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_2.zc @@ -0,0 +1,15 @@ +#output: Something Else + +public class MyClass { + + public var value as string = "Hello World"; + + public set valueSetter as string { + this.value = $; + } +} + + +var instance as MyClass = new MyClass(); +instance.valueSetter = "Something else"; +println(instance.value); diff --git a/ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_3.zc b/ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_3.zc new file mode 100644 index 000000000..bb1a73d82 --- /dev/null +++ b/ScriptingEngineTester/src/main/resources/zencode_tests/classes/setter_3.zc @@ -0,0 +1,13 @@ +#output: set value to 'Something else' + +public class MyClass { + + public this() { + } + + public set valueSetter as string { + println("set value to ''" + $ + "'"); + } +} + +new MyClass().valueSetter = "Something else"; From 74ccdbe3f104499dc6ee677cfa3c5f3fe8d12cc5 Mon Sep 17 00:00:00 2001 From: Stan Hebben Date: Tue, 3 Sep 2024 20:20:54 +0200 Subject: [PATCH 13/13] Remove unnecessary expansions when resolving inner types --- .../org/openzen/zenscript/codemodel/GenericName.java | 4 ++-- .../codemodel/compilation/CompileContext.java | 2 +- .../zenscript/codemodel/definition/ZSPackage.java | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java index 8f861e81a..068ab0b8b 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/GenericName.java @@ -24,10 +24,10 @@ public GenericName(String name, TypeID[] arguments) { this.arguments = arguments; } - public static Optional getInnerType(TypeID type, List name, int index, List expansions) { + public static Optional getInnerType(TypeID type, List name, int index) { while (index < name.size()) { GenericName innerName = name.get(index++); - ResolvedType members = type.resolve().withExpansions(expansions); + ResolvedType members = type.resolveWithoutExpansions(); Optional inner = members .findInnerType(innerName.name) .map(t -> DefinitionTypeID.create(t, innerName.arguments)); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java index 34c3a1de4..20c2ac5d2 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/CompileContext.java @@ -95,7 +95,7 @@ public Optional resolve(CodePosition position, List name) { } return Optional.of(DefinitionTypeID.create(definition.getDefinition(), name.get(name.size() - 1).arguments)); } else if (rootPackage.contains(name.get(0).name)) { - return rootPackage.getType(name, expansions); + return rootPackage.getType(name); } return Optional.ofNullable(globals.get(name.get(0).name)) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java index 060c66f28..08786def7 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java @@ -52,8 +52,8 @@ public TypeSymbol getImport(List name, int depth) { return null; } - public Optional getType(List nameParts, List expansions) { - return getType(nameParts, 0, expansions); + public Optional getType(List nameParts) { + return getType(nameParts, 0); } public Optional getType(GenericName name) { @@ -64,17 +64,17 @@ public Optional getType(GenericName name) { } } - private Optional getType(List nameParts, int depth, List expansions) { + private Optional getType(List nameParts, int depth) { if (depth >= nameParts.size()) return Optional.empty(); GenericName name = nameParts.get(depth); if (subPackages.containsKey(name.name) && name.hasNoArguments()) - return subPackages.get(name.name).getType(nameParts, depth + 1, expansions); + return subPackages.get(name.name).getType(nameParts, depth + 1); if (types.containsKey(name.name)) { TypeID type = DefinitionTypeID.create(types.get(name.name), name.arguments); - return GenericName.getInnerType(type, nameParts, depth + 1, expansions); + return GenericName.getInnerType(type, nameParts, depth + 1); } return Optional.empty();