From 24845227e494b0b52bbe4c375909e9bf7f5d0f95 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 24 Aug 2024 00:21:17 +0200 Subject: [PATCH 1/8] JavaNative: Allow JavaAnnotatedRuntimeClass' nativeTypeTemplate to be provided by subclasses --- .../java/module/JavaAnnotatedRuntimeClass.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) 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 a26f100f..395e279c 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 @@ -66,11 +66,18 @@ public Optional resolve(TypeID expandingType) { private JavaNativeTypeTemplate getTemplate() { if (this.template == null) { - TypeID target = this.target; - if (target == null) - target = DefinitionTypeID.createThis(this); - this.template = new JavaNativeTypeTemplate(target, this, context, isExpansion()); + this.template = buildTemplate(); } return template; } + + /** + * Called to build a new template to be cached + */ + protected JavaNativeTypeTemplate buildTemplate() { + TypeID target = this.target; + if (target == null) + target = DefinitionTypeID.createThis(this); + return new JavaNativeTypeTemplate(target, this, context, isExpansion()); + } } From b5ab9d23dd5e9f93087481f04de6ad89e966ac11 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 24 Aug 2024 00:21:58 +0200 Subject: [PATCH 2/8] Method Matching: Also cast default values to their parameter type --- .../compilation/MatchedCallArguments.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java index 62f87bde..b9949c85 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java @@ -4,6 +4,7 @@ import org.openzen.zencode.shared.CompileError; import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.GenericMapper; +import org.openzen.zenscript.codemodel.compilation.expression.WrappedCompilingExpression; import org.openzen.zenscript.codemodel.expression.ArrayExpression; import org.openzen.zenscript.codemodel.expression.CallArguments; import org.openzen.zenscript.codemodel.expression.Expression; @@ -225,24 +226,31 @@ private static MatchedCallArguments matchNormal( new CallArguments(CastedExpression.Level.INVALID, expansionTypeArguments, typeArguments, Expression.NONE)); } - CastedExpression[] providedArguments = IntStream.range(0, arguments.length) - .mapToObj(i -> arguments[i].cast(CastedEval.implicit(compiler, position, header.getParameterType(false, i)))) + CastedExpression[] providedArguments = IntStream.range(0, header.parameters.length) + .mapToObj(i -> { + CompilingExpression argument; + if (i < arguments.length) { + // parameter provided + argument = arguments[i]; + } else if (header.getParameter(false, i).defaultValue != null) { + // default value + argument = new WrappedCompilingExpression(compiler, header.getParameter(false, i).defaultValue); + } else { + // invalid + return CastedExpression.invalid(compiler.at(position).invalid(CompileErrors.missingParameter(header.getParameter(false, i).name))); + } + return argument.cast(CastedEval.implicit(compiler, position, header.getParameterType(false, i))); + }) .toArray(CastedExpression[]::new); - Expression[] expressions = new Expression[header.parameters.length]; - IntStream.range(0, providedArguments.length).forEach(i -> expressions[i] = providedArguments[i].value); - IntStream.range(providedArguments.length, header.parameters.length).forEach(i -> expressions[i] = header.getParameter(false, i).defaultValue); - CastedExpression.Level level = Stream.of(providedArguments) .map(e -> e.level) .max(Comparator.naturalOrder()) .orElse(CastedExpression.Level.EXACT); - boolean containsNull = Stream.of(expressions).anyMatch(Objects::isNull); - return new MatchedCallArguments<>( instancedMethod, - new CallArguments(containsNull ? CastedExpression.Level.INVALID : level, expansionTypeArguments, typeArguments, expressions) + new CallArguments(level, expansionTypeArguments, typeArguments, Stream.of(providedArguments).map(casted -> casted.value).toArray(Expression[]::new)) ); } From 2eb3334ba9932ebdd37cfc9881654ae14f216710 Mon Sep 17 00:00:00 2001 From: kindlich Date: Sat, 24 Aug 2024 00:22:22 +0200 Subject: [PATCH 3/8] Fix: Use provided type arguments again --- .../compilation/MatchedCallArguments.java | 8 +++---- .../classes/generic_members_3.zc | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 ScriptingEngineTester/src/main/resources/zencode_tests/classes/generic_members_3.zc diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java index b9949c85..7b0cc1b0 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java @@ -307,12 +307,12 @@ private static Optional inferTypeArguments( ) { int providedTypeArguments = typeArguments == null ? 0 : typeArguments.length; - if (providedTypeArguments == 0 && method.getHeader().typeParameters.length == 0) { - return Optional.of(TypeID.NONE); + if (providedTypeArguments == method.getHeader().typeParameters.length) { + return Optional.of(typeArguments != null ? typeArguments : TypeID.NONE); } - if (providedTypeArguments != 0 || method.getHeader().typeParameters.length == 0) { - return Optional.of(typeArguments == null ? TypeID.NONE : typeArguments); + if(providedTypeArguments != 0 && providedTypeArguments != method.getHeader().typeParameters.length) { + return Optional.empty(); } // attempt to infer type arguments from the return type diff --git a/ScriptingEngineTester/src/main/resources/zencode_tests/classes/generic_members_3.zc b/ScriptingEngineTester/src/main/resources/zencode_tests/classes/generic_members_3.zc new file mode 100644 index 00000000..21fb6151 --- /dev/null +++ b/ScriptingEngineTester/src/main/resources/zencode_tests/classes/generic_members_3.zc @@ -0,0 +1,22 @@ +// Overloaded method with and without generic arguments can coexist +#output: printMe(string) was called +#output: printMe() vas called +#output: printMe(long) vas called + +public class MyClass { + public static printMe(test: string): void { + println("printMe(string) was called"); + } + + public static printMe(): void { + println("printMe() vas called"); + } + + public static printMe(x: long): void { + println("printMe(long) vas called"); + } +} + +MyClass.printMe("Some String"); +MyClass.printMe(); +MyClass.printMe(123L); From 285535a82afcef30f3956c1f6c06fbf21f23df85 Mon Sep 17 00:00:00 2001 From: kindlich Date: Thu, 29 Aug 2024 22:45:31 +0200 Subject: [PATCH 4/8] fix: Don't resolve java expansions twice --- .../zenscript/codemodel/compilation/CompileContext.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 8fa60daf..addd1e4d 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 @@ -80,6 +80,12 @@ public Optional findGlobal(String name) { @Override public ResolvedType resolve(TypeID type) { ResolvedType base = type.resolve(expansions); + if(base instanceof ExpandedResolvedType) { + // ToDo: If the type already resolved its expansions, then we shouldn't add them a 2nd time + // JavaAnnotatedRuntimeClass already resolves them (and we need that to inherit expansions on parent classes) + return base; + } + List resolutions = new ArrayList<>(); for (ExpansionSymbol expansion : expansions) { expansion.resolve(type).ifPresent(resolutions::add); From ff9fcff270db19086275fe6a2b8d92e6a0bded24 Mon Sep 17 00:00:00 2001 From: kindlich Date: Thu, 29 Aug 2024 23:05:52 +0200 Subject: [PATCH 5/8] fix: search for an implicit caster from double for a floating-point literal where another type is expected Fixes `FloatingPointLiteralTests#implicitCastWorksForDoubleTypeEvenIfNoSuffixWasGiven` --- .../parser/expression/ParsedExpressionFloat.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFloat.java b/Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFloat.java index 27c618d0..69c085c3 100644 --- a/Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFloat.java +++ b/Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionFloat.java @@ -71,7 +71,10 @@ public CastedExpression cast(CastedEval cast) { if (suffix.isEmpty()) { return resolvedType.findImplicitConstructor() .map(constructor -> constructor.casted(compiler, position, cast, null, this)) - .orElseGet(() -> cast.invalid(CompileErrors.cannotCompileFloatLiteralAs(cast.type))); + .orElseGet(() -> { + CastedExpression castedExpression = cast.of(eval()); + return castedExpression.isFailed() ? cast.invalid(CompileErrors.cannotCompileFloatLiteralAs(cast.type)) : castedExpression; + }); } else { return resolvedType.findSuffixConstructor(suffix) .map(method -> method.casted(compiler, position, cast, null, this)) @@ -81,12 +84,12 @@ public CastedExpression cast(CastedEval cast) { @Override public void collect(SSAVariableCollector collector) { - + // No vars to collect in a literal } @Override public void linkVariables(CodeBlockStatement.VariableLinker linker) { - + // No vars to link in a literal } } } From 9b081889df970ad020d6b9b840301553e4f121df Mon Sep 17 00:00:00 2001 From: kindlich Date: Thu, 29 Aug 2024 23:16:13 +0200 Subject: [PATCH 6/8] fix: evaluate size argument for copy-with-resize --- .../javabytecode/compiler/JavaMethodBytecodeCompiler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaMethodBytecodeCompiler.java b/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaMethodBytecodeCompiler.java index 68d8cb49..546dc589 100644 --- a/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaMethodBytecodeCompiler.java +++ b/JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaMethodBytecodeCompiler.java @@ -1517,7 +1517,8 @@ public Void specialStaticMethod(JavaSpecialMethod method, CallArguments args) { } case ARRAY_COPY_RESIZE: { Expression value = arguments[0]; - value.accept(expressionVisitor); + value.accept(expressionVisitor); // array + arguments[1].accept(expressionVisitor); // new size final TypeID elementType = ((ArrayTypeID) value.type).elementType; final boolean primitive = CompilerUtils.isPrimitive(elementType); From 05f88afc49f138ca1a21330225048accfa47747a Mon Sep 17 00:00:00 2001 From: Stan Hebben Date: Fri, 30 Aug 2024 22:59:48 +0200 Subject: [PATCH 7/8] Changed the way expansions are resolved --- .../zenscript/codemodel/GenericName.java | 2 +- .../codemodel/HighLevelDefinition.java | 20 ++++----- .../codemodel/compilation/CompileContext.java | 16 +------ .../codemodel/compilation/ResolvedType.java | 11 +++++ .../impl/compiler/LocalTypeImpl.java | 3 +- .../codemodel/definition/ClassDefinition.java | 2 +- .../generic/ParameterSuperBound.java | 9 ++-- .../codemodel/generic/ParameterTypeBound.java | 11 +++-- .../codemodel/generic/TypeParameterBound.java | 6 +-- .../codemodel/identifiers/TypeSymbol.java | 3 +- .../codemodel/member/IDefinitionMember.java | 5 +++ .../member/ImplementationMember.java | 5 +++ .../zenscript/codemodel/type/ArrayTypeID.java | 5 +-- .../zenscript/codemodel/type/AssocTypeID.java | 5 +-- .../zenscript/codemodel/type/BasicTypeID.java | 5 +-- .../codemodel/type/DefinitionTypeID.java | 5 +-- .../codemodel/type/FunctionTypeID.java | 5 +-- .../codemodel/type/GenericMapTypeID.java | 5 +-- .../codemodel/type/GenericTypeID.java | 7 ++- .../codemodel/type/InvalidTypeID.java | 3 +- .../codemodel/type/IteratorTypeID.java | 3 +- .../codemodel/type/OptionalTypeID.java | 5 +-- .../zenscript/codemodel/type/RangeTypeID.java | 5 +-- .../zenscript/codemodel/type/TypeID.java | 3 +- .../type/builtin/ArrayTypeSymbol.java | 4 +- .../type/builtin/FunctionTypeSymbol.java | 4 +- .../type/builtin/GenericMapTypeSymbol.java | 5 +-- .../codemodel/type/builtin/MapTypeSymbol.java | 6 +-- .../type/builtin/OptionalTypeSymbol.java | 8 ++-- .../type/builtin/RangeTypeSymbol.java | 6 +-- .../type/builtin/ResultTypeSymbol.java | 4 +- .../type/member/ExpandedResolvedType.java | 8 ++++ .../type/member/InterfaceResolvedType.java | 43 ++++++++++--------- .../type/member/SubclassResolvedType.java | 6 +++ .../module/JavaAnnotatedRuntimeClass.java | 11 ++--- .../zencode/java/module/JavaRuntimeClass.java | 3 +- .../zenscript/parser/ParsedFileCompiler.java | 2 +- .../interfaces/interface_definition_7.zc | 28 ++++++++++++ .../visitors/DefinitionMemberValidator.java | 2 +- 39 files changed, 158 insertions(+), 131 deletions(-) create mode 100644 ScriptingEngineTester/src/main/resources/zencode_tests/interfaces/interface_definition_7.zc 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 61c9eb5e..2308d55a 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(expansions); + ResolvedType members = type.resolve(); 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 6b0cc2df..3a1606b9 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java @@ -6,22 +6,22 @@ import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.definition.*; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; 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.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 java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public abstract class HighLevelDefinition extends Taggable implements TypeSymbol { public final CodePosition position; @@ -144,17 +144,13 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { MemberSet.Builder members = MemberSet.create(); GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); - TypeID type = DefinitionTypeID.create(this, typeArguments); for (IDefinitionMember member : this.members) { member.registerTo(type, members, mapper); } - List interfaceExpansions = new ArrayList<>(); - for (IDefinitionMember member : this.members) { - interfaceExpansions.addAll(member.resolveExpansions(expansions)); - } + List interfaces = this.members.stream().map(IDefinitionMember::asImplementation).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()); 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)); @@ -163,11 +159,15 @@ public ResolvedType resolve(TypeID[] typeArguments, List expans if (superType != null) { TypeID instancedSuperType = mapper.map(superType); - ResolvedType superResolved = instancedSuperType.resolve(expansions); + ResolvedType superResolved = instancedSuperType.resolve(); resolved = new SubclassResolvedType(superResolved, resolved, superType); } - return ExpandedResolvedType.of(resolved, interfaceExpansions); + if (interfaces.isEmpty()) { + return resolved; + } else { + return new InterfaceResolvedType(resolved, interfaces); + } } protected void resolveAdditional(TypeID type, MemberSet.Builder members, GenericMapper mapper) {} 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 addd1e4d..d595b542 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 @@ -14,7 +14,6 @@ import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.globals.IGlobal; import org.openzen.zenscript.codemodel.type.TypeID; -import org.openzen.zenscript.codemodel.type.member.ExpandedResolvedType; import java.util.*; @@ -79,18 +78,7 @@ public Optional findGlobal(String name) { @Override public ResolvedType resolve(TypeID type) { - ResolvedType base = type.resolve(expansions); - if(base instanceof ExpandedResolvedType) { - // ToDo: If the type already resolved its expansions, then we shouldn't add them a 2nd time - // JavaAnnotatedRuntimeClass already resolves them (and we need that to inherit expansions on parent classes) - return base; - } - - List resolutions = new ArrayList<>(); - for (ExpansionSymbol expansion : expansions) { - expansion.resolve(type).ifPresent(resolutions::add); - } - return ExpandedResolvedType.of(base, resolutions); + return type.resolve().withExpansions(type, expansions); } @Override @@ -114,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(expansions).findInnerType(name.get(i).name); + Optional inner = t.resolve().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 6cd7e203..eee41c36 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,10 +6,13 @@ 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; @@ -73,6 +76,14 @@ default boolean canCastImplicitlyTo(TypeID target) { Optional findStaticOperator(OperatorType operator); + default ResolvedType withExpansions(TypeID type, List expansions) { + List resolutions = new ArrayList<>(); + for (ExpansionSymbol expansion : expansions) { + expansion.resolve(type).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/impl/compiler/LocalTypeImpl.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/impl/compiler/LocalTypeImpl.java index 576274a5..1351bfaa 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().resolve(Collections.emptyList()); + resolvedSuper = thisType.getSuperType().resolve(); 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 105c9213..19cc9b14 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(Collections.emptyList()).getConstructor().getSingleOverload()) + .flatMap(t -> t.resolve().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 fe4e2f57..dea56bfa 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 @@ -2,10 +2,8 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.List; import java.util.Optional; public final class ParameterSuperBound implements TypeParameterBound { @@ -21,7 +19,7 @@ public String getCanonical() { } @Override - public Optional resolveMembers(List expansions) { + public Optional resolveMembers() { return Optional.empty(); } @@ -39,6 +37,11 @@ public TypeParameterBound instance(GenericMapper mapper) { return new ParameterSuperBound(translated); } + @Override + public Optional asType() { + return Optional.empty(); + } + @Override public T accept(GenericParameterBoundVisitor visitor) { return visitor.visitSuper(this); 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 77bf3535..132816a8 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 @@ -3,10 +3,8 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.List; import java.util.Optional; public final class ParameterTypeBound implements TypeParameterBound { @@ -24,8 +22,8 @@ public String getCanonical() { } @Override - public Optional resolveMembers(List expansions) { - return Optional.of(type.resolve(expansions)); + public Optional resolveMembers() { + return Optional.of(type.resolve()); } @Override @@ -38,6 +36,11 @@ public TypeParameterBound instance(GenericMapper mapper) { return new ParameterTypeBound(position, type.instance(mapper)); } + @Override + public Optional asType() { + return Optional.of(type); + } + @Override public T accept(GenericParameterBoundVisitor visitor) { return visitor.visitType(this); 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 b329dca8..a6b88084 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 @@ -2,10 +2,8 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.List; import java.util.Optional; public interface TypeParameterBound { @@ -15,11 +13,13 @@ public interface TypeParameterBound { R accept(C context, GenericParameterBoundVisitorWithContext visitor); - Optional resolveMembers(List expansions); + Optional resolveMembers(); boolean matches(TypeID type); TypeParameterBound instance(GenericMapper mapper); + Optional asType(); + String getCanonical(); } 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 732b56ce..04928beb 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 @@ -5,7 +5,6 @@ import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; -import java.util.List; import java.util.Optional; /** @@ -20,7 +19,7 @@ public interface TypeSymbol extends DefinitionSymbol { String getName(); - ResolvedType resolve(TypeID[] typeArguments, List expansions); + ResolvedType resolve(TypeID type, TypeID[] typeArguments); Optional getOuter(); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java index 2706990d..f4956ed3 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java @@ -11,6 +11,7 @@ import java.util.Collections; import java.util.List; +import java.util.Optional; public interface IDefinitionMember { CodePosition getPosition(); @@ -31,6 +32,10 @@ default List resolveExpansions(List expansions) { return Collections.emptyList(); } + default Optional asImplementation() { + return Optional.empty(); + } + T accept(MemberVisitor visitor); R accept(C context, MemberVisitorWithContext visitor); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java index c5ea497b..7983b734 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java @@ -56,6 +56,11 @@ public List resolveExpansions(List expansions) { .collect(Collectors.toList()); } + @Override + public Optional asImplementation() { + return Optional.of(type); + } + @Override public T accept(MemberVisitor visitor) { return visitor.visitImplementation(this); 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 61344682..72af0d37 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 @@ -6,7 +6,6 @@ import org.openzen.zenscript.codemodel.expression.ArrayExpression; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.type.builtin.ArrayTypeSymbol; @@ -73,8 +72,8 @@ public Optional asArray() { } @Override - public ResolvedType resolve(List expansions) { - return type.resolve(new TypeID[] { elementType }, expansions); + public ResolvedType resolve() { + return type.resolve(this, 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 9f397097..5ad5d68c 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 @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.builtin.MapTypeSymbol; import java.util.List; @@ -54,8 +53,8 @@ public Optional asAssoc() { } @Override - public ResolvedType resolve(List expansions) { - return MapTypeSymbol.INSTANCE.resolve(new TypeID[] { keyType, valueType }, expansions); + public ResolvedType resolve() { + return MapTypeSymbol.INSTANCE.resolve(this, 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 9fced421..bee54a3a 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,7 +3,6 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.Modifiers; -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.expression.*; @@ -76,7 +75,7 @@ public boolean isValueType() { } @Override - public ResolvedType resolve(List expansions) { + public ResolvedType resolve() { if (members == null) members = BasicTypeMembers.get(this); @@ -184,7 +183,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType 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 159b6c91..71963499 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 @@ -6,7 +6,6 @@ import org.openzen.zenscript.codemodel.definition.StructDefinition; import org.openzen.zenscript.codemodel.definition.VariantDefinition; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import java.util.*; @@ -54,8 +53,8 @@ public DefinitionTypeID(TypeSymbol definition, TypeID[] typeArguments, Definitio * * @return base type members */ - public ResolvedType resolve(List expansions) { - return definition.resolve(typeArguments, expansions); + public ResolvedType resolve() { + return definition.resolve(this, 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 3d859d95..14759d73 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 @@ -5,7 +5,6 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.builtin.FunctionTypeSymbol; import java.util.Arrays; @@ -57,8 +56,8 @@ public Optional asFunction() { } @Override - public ResolvedType resolve(List expansions) { - return type.resolve(TypeID.NONE, expansions); + public ResolvedType resolve() { + return type.resolve(this, 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 071b0e31..04bf4fa9 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 @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.*; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.builtin.GenericMapTypeSymbol; import java.util.*; @@ -43,8 +42,8 @@ public Optional asGenericMap() { } @Override - public ResolvedType resolve(List expansions) { - return GenericMapTypeSymbol.INSTANCE.resolve(new TypeID[] { new GenericTypeID(key), value }, expansions); + public ResolvedType resolve() { + return GenericMapTypeSymbol.INSTANCE.resolve(this, new TypeID[] { new GenericTypeID(key), value }); } @Override 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 390a3de7..bdfed47d 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 @@ -4,8 +4,8 @@ import org.openzen.zenscript.codemodel.compilation.ResolvedType; 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.InterfaceResolvedType; import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.ArrayList; @@ -70,11 +70,10 @@ public Optional asGeneric() { } @Override - public ResolvedType resolve(List expansions) { + public ResolvedType resolve() { List fromBounds = new ArrayList<>(); for (TypeParameterBound bound : parameter.bounds) { - // TODO: local expansions will not be applied here - bound.resolveMembers(expansions).ifPresent(fromBounds::add); + bound.resolveMembers().ifPresent(fromBounds::add); } return ExpandedResolvedType.of(new MemberSet(), fromBounds); } 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 726006fa..6d2d85c3 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 @@ -5,7 +5,6 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.List; @@ -45,7 +44,7 @@ public boolean isInvalid() { } @Override - public ResolvedType resolve(List expansions) { + public ResolvedType resolve() { return new MemberSet(); } 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 51a34e83..8b63530b 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 @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.Arrays; @@ -37,7 +36,7 @@ public boolean isValueType() { } @Override - public ResolvedType resolve(List expansions) { + public ResolvedType resolve() { return new MemberSet(); // 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 67909193..0d83b456 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 @@ -6,7 +6,6 @@ import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.expression.NullExpression; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.builtin.OptionalTypeSymbol; import java.util.*; @@ -35,8 +34,8 @@ public Optional asOptional() { } @Override - public ResolvedType resolve(List expansions) { - return OptionalTypeSymbol.INSTANCE.resolve(new TypeID[] { baseType }, expansions); + public ResolvedType resolve() { + return OptionalTypeSymbol.INSTANCE.resolve(this, 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 873ff46a..722a3683 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 @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.builtin.RangeTypeSymbol; import java.util.List; @@ -38,8 +37,8 @@ public R accept(C context, TypeVisitorWithContext asRange() { return Optional.of(this); } @Override - public ResolvedType resolve(List expansions) { - return RangeTypeSymbol.INSTANCE.resolve(new TypeID[] { baseType }, expansions); + public ResolvedType resolve() { + return RangeTypeSymbol.INSTANCE.resolve(this, new TypeID[] { baseType }); } @Override 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 7ce66201..1093068c 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 @@ -5,7 +5,6 @@ import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.expression.Expression; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import java.util.HashMap; import java.util.List; @@ -155,7 +154,7 @@ default Optional asGeneric() { return Optional.empty(); } - ResolvedType resolve(List expansions); + ResolvedType resolve(); 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 0a397fd3..e651dedb 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 @@ -4,7 +4,6 @@ import org.openzen.zenscript.codemodel.*; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; @@ -90,9 +89,8 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { TypeID baseType = typeArguments[0]; - TypeID type = new ArrayTypeID(baseType, dimension); GenericMapper mapper = GenericMapper.single(parameters[0], baseType); MemberSet.Builder members = MemberSet.create(); 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 55107bb5..42aef0dc 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,7 +2,6 @@ import org.openzen.zenscript.codemodel.FunctionHeader; import org.openzen.zenscript.codemodel.Modifiers; -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.TypeParameter; @@ -12,7 +11,6 @@ import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.MemberSet; -import java.util.List; import java.util.Optional; public class FunctionTypeSymbol implements TypeSymbol { @@ -67,7 +65,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { return MemberSet.create().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 8738cf3e..3292e3b8 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 @@ -4,7 +4,6 @@ import org.openzen.zenscript.codemodel.*; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance; @@ -15,7 +14,6 @@ import org.openzen.zenscript.codemodel.type.member.MemberSet; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Optional; @@ -75,10 +73,9 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { TypeParameter parameter = ((GenericTypeID)typeArguments[0]).parameter; TypeID value = typeArguments[1]; - GenericMapTypeID type = new GenericMapTypeID(value, parameter); MemberSet.Builder members = MemberSet.create(); Map 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 e471e5df..78f57d76 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 @@ -4,7 +4,6 @@ import org.openzen.zenscript.codemodel.*; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.identifiers.ModuleSymbol; import org.openzen.zenscript.codemodel.identifiers.TypeSymbol; import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance; @@ -14,7 +13,6 @@ import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.MemberSet; -import java.util.List; import java.util.Optional; public class MapTypeSymbol implements TypeSymbol { @@ -72,9 +70,9 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); - AssocTypeID type = new AssocTypeID(typeArguments[0], typeArguments[1]); + AssocTypeID type = (AssocTypeID) type_; MemberSet.Builder members = MemberSet.create(); 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 4567295d..06893d11 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,7 +1,6 @@ package org.openzen.zenscript.codemodel.type.builtin; import org.openzen.zenscript.codemodel.Modifiers; -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.TypeParameter; @@ -10,7 +9,6 @@ import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.OptionalResolvedType; -import java.util.List; import java.util.Optional; public class OptionalTypeSymbol implements TypeSymbol { @@ -59,9 +57,9 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { - OptionalTypeID type = new OptionalTypeID(typeArguments[0]); - ResolvedType baseMembers = typeArguments[0].resolve(expansions); + public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { + OptionalTypeID type = (OptionalTypeID) type_; + ResolvedType baseMembers = typeArguments[0].resolve(); return new OptionalResolvedType(type, baseMembers); } 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 717ac016..159e2545 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,7 +3,6 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.Modifiers; -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.TypeParameter; @@ -13,7 +12,6 @@ import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.MemberSet; -import java.util.List; import java.util.Optional; import static org.openzen.zenscript.codemodel.type.BasicTypeID.*; @@ -67,10 +65,10 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { TypeID baseType = typeArguments[0]; GenericMapper mapper = GenericMapper.single(PARAMETER, baseType); - RangeTypeID type = new RangeTypeID(baseType); + RangeTypeID type = (RangeTypeID) type_; MemberSet.Builder members = MemberSet.create(); 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 61d4bc6f..99e5a9e4 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,7 +2,6 @@ import org.openzen.zencode.shared.CodePosition; import org.openzen.zenscript.codemodel.Modifiers; -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.TypeParameter; @@ -10,7 +9,6 @@ import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.member.MemberSet; -import java.util.List; import java.util.Optional; public class ResultTypeSymbol implements TypeSymbol { @@ -66,7 +64,7 @@ public String getName() { } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { + public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { MemberSet members = new MemberSet(); // 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 9548f144..b8502940 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; @@ -11,6 +12,7 @@ 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) { @@ -190,4 +192,10 @@ public List comparators() { public Optional findIterator(int variables) { return base.findIterator(variables); } + + @Override + public ResolvedType withExpansions(TypeID type, List expansions) { + List newExpansions = this.expansions.stream().map(expansion -> expansion.withExpansions(type, expansions)).collect(Collectors.toList()); + return ExpandedResolvedType.of(base.withExpansions(type, 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 1660ec8e..5753a989 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,16 +20,15 @@ public class InterfaceResolvedType implements ResolvedType { private final ResolvedType baseType; + private final List implementations; private final Collection implementedInterfaces; - private final List expansions; - public InterfaceResolvedType(ResolvedType baseType, Collection implementedInterfaces, List expansions) { + 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; - this.expansions = expansions; } - @Override public StaticCallable getConstructor() { return baseType.getConstructor(); @@ -72,22 +71,22 @@ public Optional findStaticSetter(String name) { @Override public Optional findMethod(String name) { - return mergeLocalWithImplementedInterfaces(type -> type.findMethod(name), InstanceCallable::union); + return mergeLocalWithImplementedInterfaces(type -> type.findMethod(name), InstanceCallable::merge); } @Override public Optional findGetter(String name) { - return mergeLocalWithImplementedInterfaces(type -> type.findGetter(name), InstanceCallable::union); + return mergeLocalWithImplementedInterfaces(type -> type.findGetter(name), InstanceCallable::merge); } @Override public Optional findSetter(String name) { - return mergeLocalWithImplementedInterfaces(type -> type.findSetter(name), InstanceCallable::union); + return mergeLocalWithImplementedInterfaces(type -> type.findSetter(name), InstanceCallable::merge); } @Override public Optional findOperator(OperatorType operator) { - return mergeLocalWithImplementedInterfaces(type -> type.findOperator(operator), InstanceCallable::union); + return mergeLocalWithImplementedInterfaces(type -> type.findOperator(operator), InstanceCallable::merge); } @Override @@ -112,10 +111,7 @@ public Optional findSwitchMember(String name) { @Override public List comparators() { - return Stream.concat( - Stream.of(baseType), - implementedInterfaces.stream().map(typeID -> typeID.resolve(expansions)) - ).flatMap(type -> type.comparators().stream()) + return implementations.stream().flatMap(type -> type.comparators().stream()) .collect(Collectors.toList()); } @@ -129,11 +125,21 @@ public Optional findStaticOperator(OperatorType operator) { return baseType.findStaticOperator(operator); } + @Override + public ResolvedType withExpansions(TypeID type, List expansions) { + 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(type, expansions), + interfaceExpansions), + implementedInterfaces); + } + private Optional findFirstInLocalOrImplementedInterfaces(Function> mapper) { - return Stream.concat( - Stream.of(baseType), - implementedInterfaces.stream().map(typeID -> typeID.resolve(expansions)) - ) + return implementations.stream() .map(mapper) .filter(Optional::isPresent) .map(Optional::get) @@ -141,10 +147,7 @@ private Optional findFirstInLocalOrImplementedInterfaces(Function Optional mergeLocalWithImplementedInterfaces(Function> mapper, BinaryOperator combiner) { - return Stream.concat( - Stream.of(baseType), - implementedInterfaces.stream().map(typeID -> typeID.resolve(expansions)) - ) + return implementations.stream() .map(mapper) .filter(Optional::isPresent) .map(Optional::get) 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 42916090..07c4a50d 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,6 +8,7 @@ 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; @@ -124,6 +125,11 @@ public Optional findIterator(int variables) { return or(resolved.findIterator(variables), () -> superclass.findIterator(variables)); } + @Override + public ResolvedType withExpansions(TypeID type, List expansions) { + return new SubclassResolvedType(superclass.withExpansions(supertype, expansions), resolved.withExpansions(type, expansions), supertype); + } + private static Optional or(Optional value, Supplier> other) { if (value.isPresent()) return value; 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 395e279c..9d53577d 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 @@ -3,7 +3,6 @@ import org.openzen.zenscript.codemodel.GenericMapper; import org.openzen.zenscript.codemodel.compilation.ResolvedType; import org.openzen.zenscript.codemodel.generic.TypeParameter; -import org.openzen.zenscript.codemodel.identifiers.ExpansionSymbol; import org.openzen.zenscript.codemodel.type.DefinitionTypeID; import org.openzen.zenscript.codemodel.type.TypeID; import org.openzen.zenscript.codemodel.type.TypeMatcher; @@ -30,21 +29,19 @@ public JavaAnnotatedRuntimeClass(JavaNativeModule module, Class cls, String n } @Override - public ResolvedType resolve(TypeID[] typeArguments, List expansions) { - TypeID type = DefinitionTypeID.create(this, typeArguments); + public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { ResolvedType resolved = new JavaNativeTypeMembers(getTemplate(), type, GenericMapper.create(getTypeParameters(), typeArguments)); Optional superType = getSupertype(typeArguments); if (superType.isPresent()) { - resolved = new SubclassResolvedType(superType.get().resolve(expansions), resolved, superType.get()); + resolved = new SubclassResolvedType(superType.get().resolve(), resolved, superType.get()); } Collection interfaces = getInterfaces(typeArguments); if (!interfaces.isEmpty()) { - resolved = new InterfaceResolvedType(resolved, interfaces, expansions); + resolved = new InterfaceResolvedType(resolved, interfaces); } - List expansionsResolved = expansions.stream().map(expansion -> expansion.resolve(type)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()); - return ExpandedResolvedType.of(resolved, expansionsResolved); + return resolved; } @Override 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 e124b29c..1d088446 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 @@ -19,7 +19,6 @@ import java.lang.reflect.TypeVariable; import java.util.Arrays; import java.util.Collection; -import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -84,7 +83,7 @@ public String getName() { } @Override - public abstract ResolvedType resolve(TypeID[] typeArguments, List expansions); + public abstract ResolvedType 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 3c2004f5..6e086b0d 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(Collections.emptyList()).findInnerType(name.get(i).name); + Optional inner = type.resolve().findInnerType(name.get(i).name); if (inner.isPresent()) { type = DefinitionTypeID.create(inner.get(), name.get(i).arguments); } else { diff --git a/ScriptingEngineTester/src/main/resources/zencode_tests/interfaces/interface_definition_7.zc b/ScriptingEngineTester/src/main/resources/zencode_tests/interfaces/interface_definition_7.zc new file mode 100644 index 00000000..490b736f --- /dev/null +++ b/ScriptingEngineTester/src/main/resources/zencode_tests/interfaces/interface_definition_7.zc @@ -0,0 +1,28 @@ +#output: hello +#output: hello2 + +interface Test { + print(): void; +} + +interface Test2 { + print(): void; +} + +class TestClass { + implements Test { + print() { + println("hello"); + } + } + + implements Test2 { + print() { + println("hello2"); + } + } +} + +val testInstance = new TestClass(); +(testInstance as Test).print(); +(testInstance as Test2).print(); 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 ae09672e..e9221b26 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(Collections.emptyList()).getConstructor().acceptsZeroArguments()) { + if (member.definition.getSuperType().resolve().getConstructor().acceptsZeroArguments()) { return; } From c27ca1af94906ac45ef74132827d4be88a267210 Mon Sep 17 00:00:00 2001 From: Stan Hebben Date: Fri, 30 Aug 2024 23:06:22 +0200 Subject: [PATCH 8/8] Make ResolvedType remember its type (and simplified the withExpansions method) --- .../zenscript/codemodel/HighLevelDefinition.java | 2 +- .../codemodel/compilation/CompileContext.java | 2 +- .../codemodel/compilation/ResolvedType.java | 6 ++++-- .../codemodel/definition/ExpansionDefinition.java | 2 +- .../codemodel/member/IDefinitionMember.java | 4 ---- .../codemodel/member/ImplementationMember.java | 9 --------- .../zenscript/codemodel/type/GenericTypeID.java | 2 +- .../zenscript/codemodel/type/InvalidTypeID.java | 2 +- .../zenscript/codemodel/type/IteratorTypeID.java | 2 +- .../codemodel/type/builtin/ArrayTypeSymbol.java | 2 +- .../codemodel/type/builtin/BasicTypeMembers.java | 5 ++--- .../codemodel/type/builtin/FunctionTypeSymbol.java | 2 +- .../type/builtin/GenericMapTypeSymbol.java | 2 +- .../codemodel/type/builtin/MapTypeSymbol.java | 2 +- .../codemodel/type/builtin/RangeTypeSymbol.java | 2 +- .../codemodel/type/builtin/ResultTypeSymbol.java | 2 +- .../type/member/ExpandedResolvedType.java | 11 ++++++++--- .../type/member/InterfaceResolvedType.java | 9 +++++++-- .../zenscript/codemodel/type/member/MemberSet.java | 14 ++++++++++++-- .../type/member/OptionalResolvedType.java | 7 ++++++- .../type/member/SubclassResolvedType.java | 9 +++++++-- .../zencode/java/module/JavaNativeTypeMembers.java | 5 +++++ 22 files changed, 63 insertions(+), 40 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 3a1606b9..b9d074c5 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java @@ -145,7 +145,7 @@ public String getName() { @Override public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { - MemberSet.Builder members = MemberSet.create(); + MemberSet.Builder members = MemberSet.create(type); GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); for (IDefinitionMember member : this.members) { member.registerTo(type, members, mapper); 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 d595b542..41ea854e 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 @@ -78,7 +78,7 @@ public Optional findGlobal(String name) { @Override public ResolvedType resolve(TypeID type) { - return type.resolve().withExpansions(type, expansions); + return type.resolve().withExpansions(expansions); } @Override 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 eee41c36..aece8702 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 @@ -17,6 +17,8 @@ import java.util.Optional; public interface ResolvedType { + TypeID getType(); + StaticCallable getConstructor(); Optional findImplicitConstructor(); @@ -76,10 +78,10 @@ default boolean canCastImplicitlyTo(TypeID target) { Optional findStaticOperator(OperatorType operator); - default ResolvedType withExpansions(TypeID type, List expansions) { + default ResolvedType withExpansions(List expansions) { List resolutions = new ArrayList<>(); for (ExpansionSymbol expansion : expansions) { - expansion.resolve(type).ifPresent(resolutions::add); + expansion.resolve(getType()).ifPresent(resolutions::add); } return ExpandedResolvedType.of(this, resolutions); } 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 8b207dce..ce63f396 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 @@ -49,7 +49,7 @@ public Optional resolve(TypeID expandingType) { return Optional.empty(); TypeID[] expansionTypeArguments = Stream.of(typeParameters).map(mapping::get).toArray(TypeID[]::new); - MemberSet.Builder resolution = MemberSet.create(); + MemberSet.Builder resolution = MemberSet.create(expandingType); GenericMapper mapper = new GenericMapper(mapping, expansionTypeArguments); for (IDefinitionMember member : members) member.registerTo(expandingType, resolution, mapper); diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java index f4956ed3..09df3077 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java @@ -28,10 +28,6 @@ public interface IDefinitionMember { void registerTo(TypeID targetType, MemberSet.Builder members, GenericMapper mapper); - default List resolveExpansions(List expansions) { - return Collections.emptyList(); - } - default Optional asImplementation() { return Optional.empty(); } diff --git a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java index 7983b734..8b1f3869 100644 --- a/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java +++ b/CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java @@ -47,15 +47,6 @@ public void registerTo(TypeID targetType, MemberSet.Builder members, GenericMapp } } - @Override - public List resolveExpansions(List expansions) { - return expansions.stream() - .map(expansion -> expansion.resolve(type)) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.toList()); - } - @Override public Optional asImplementation() { return Optional.of(type); 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 bdfed47d..2589db79 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 @@ -75,7 +75,7 @@ public ResolvedType resolve() { for (TypeParameterBound bound : parameter.bounds) { bound.resolveMembers().ifPresent(fromBounds::add); } - return ExpandedResolvedType.of(new MemberSet(), fromBounds); + return ExpandedResolvedType.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 6d2d85c3..96c62b94 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 ResolvedType resolve() { - return new MemberSet(); + return new MemberSet(this); } @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 8b63530b..20a29a2d 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 ResolvedType resolve() { - return new MemberSet(); // no members yet + return new MemberSet(this); // no members yet } @Override 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 e651dedb..5f496112 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 @@ -93,7 +93,7 @@ public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { TypeID baseType = typeArguments[0]; GenericMapper mapper = GenericMapper.single(parameters[0], baseType); - MemberSet.Builder members = MemberSet.create(); + MemberSet.Builder members = MemberSet.create(type); FunctionParameter[] indexGetParameters = new FunctionParameter[dimension]; for (int i = 0; i < indexGetParameters.length; i++) 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 31866407..6bd1ffd2 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 @@ -18,7 +18,6 @@ import java.util.Optional; public class BasicTypeMembers { - private static final MemberSet NO_MEMBERS = new MemberSet(); private static final MethodID CONSTRUCTOR = MethodID.staticOperator(OperatorType.CONSTRUCTOR); private static final MethodID COMPARE = MethodID.operator(OperatorType.COMPARE); @@ -28,9 +27,9 @@ public static MemberSet get(BasicTypeID type) { case NULL: case UNDETERMINED: case INVALID: - return NO_MEMBERS; + return new MemberSet(type); default: - MemberSet.Builder builder = MemberSet.create(); + MemberSet.Builder builder = MemberSet.create(type); setup(builder, type); return builder.build(); } 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 42aef0dc..a0244178 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 @@ -66,7 +66,7 @@ public String getName() { @Override public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { - return MemberSet.create().method(new MethodInstance(caller)).build(); + return MemberSet.create(type).method(new MethodInstance(caller)).build(); } @Override 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 3292e3b8..3ea50a27 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 @@ -77,7 +77,7 @@ public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { TypeParameter parameter = ((GenericTypeID)typeArguments[0]).parameter; TypeID value = typeArguments[1]; - MemberSet.Builder members = MemberSet.create(); + MemberSet.Builder members = MemberSet.create(type); Map parameterFilled = new HashMap<>(); parameterFilled.put(PARAMETER, typeArguments[0]); parameterFilled.put(VALUE, value); 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 78f57d76..77ac3b5c 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 @@ -74,7 +74,7 @@ public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { GenericMapper mapper = GenericMapper.create(typeParameters, typeArguments); AssocTypeID type = (AssocTypeID) type_; - MemberSet.Builder members = MemberSet.create(); + MemberSet.Builder members = MemberSet.create(type); members.constructor(new MethodInstance(BuiltinMethodSymbol.ASSOC_CONSTRUCTOR)); members.method(mapper.map(type, BuiltinMethodSymbol.ASSOC_INDEXGET)); 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 159e2545..f2e84c81 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 @@ -70,7 +70,7 @@ public ResolvedType resolve(TypeID type_, TypeID[] typeArguments) { GenericMapper mapper = GenericMapper.single(PARAMETER, baseType); RangeTypeID type = (RangeTypeID) type_; - MemberSet.Builder members = MemberSet.create(); + MemberSet.Builder members = MemberSet.create(type); members.method(mapper.map(type, BuiltinMethodSymbol.RANGE_FROM)); members.method(mapper.map(type, BuiltinMethodSymbol.RANGE_TO)); 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 99e5a9e4..91c4fa2f 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,7 +65,7 @@ public String getName() { @Override public ResolvedType resolve(TypeID type, TypeID[] typeArguments) { - MemberSet members = new MemberSet(); + 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 b8502940..0a77f188 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 @@ -31,6 +31,11 @@ private ExpandedResolvedType(ResolvedType base, List expansions) { this.expansions = expansions; } + @Override + public TypeID getType() { + return base.getType(); + } + @Override public StaticCallable getConstructor() { return base.getConstructor(); @@ -194,8 +199,8 @@ public Optional findIterator(int variables) { } @Override - public ResolvedType withExpansions(TypeID type, List expansions) { - List newExpansions = this.expansions.stream().map(expansion -> expansion.withExpansions(type, expansions)).collect(Collectors.toList()); - return ExpandedResolvedType.of(base.withExpansions(type, expansions), newExpansions); + 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 5753a989..9131cd0e 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 @@ -29,6 +29,11 @@ public InterfaceResolvedType(ResolvedType baseType, Collection implement this.implementedInterfaces = implementedInterfaces; } + @Override + public TypeID getType() { + return baseType.getType(); + } + @Override public StaticCallable getConstructor() { return baseType.getConstructor(); @@ -126,14 +131,14 @@ public Optional findStaticOperator(OperatorType operator) { } @Override - public ResolvedType withExpansions(TypeID type, List expansions) { + 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()); return new InterfaceResolvedType( ExpandedResolvedType.of( - baseType.withExpansions(type, expansions), + baseType.withExpansions(expansions), interfaceExpansions), implementedInterfaces); } 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 07bfc901..2f144a0c 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,11 +13,12 @@ import java.util.stream.Collectors; public class MemberSet implements ResolvedType { - public static Builder create() { - MemberSet members = new MemberSet(); + 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<>(); private final Map> instanceMethods = new HashMap<>(); private final Map> staticMethods = new HashMap<>(); @@ -28,6 +29,15 @@ public static Builder create() { private final List iterators = new ArrayList<>(); private final List comparators = new ArrayList<>(); + public MemberSet(TypeID type) { + this.type = type; + } + + @Override + public TypeID getType() { + return type; + } + @Override public StaticCallable getConstructor() { return new StaticCallable(constructors); 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 0f1e5a48..70c78d95 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 @@ -25,7 +25,7 @@ public OptionalResolvedType(OptionalTypeID type, ResolvedType baseType) { this.type = type; this.baseType = baseType; - MemberSet.Builder optionalMembers = MemberSet.create(); + MemberSet.Builder optionalMembers = MemberSet.create(type); optionalMembers.method(new MethodInstance( BuiltinMethodSymbol.OPTIONAL_IS_NULL, new FunctionHeader(BasicTypeID.BOOL, BasicTypeID.NULL), @@ -37,6 +37,11 @@ public OptionalResolvedType(OptionalTypeID type, ResolvedType baseType) { this.optionalMembers = optionalMembers.build(); } + @Override + public TypeID getType() { + return type; + } + @Override public StaticCallable getConstructor() { return baseType.getConstructor(); 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 07c4a50d..1e7b3722 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 @@ -30,6 +30,11 @@ public SubclassResolvedType(ResolvedType superclass, ResolvedType resolved, Type this.supertype = supertype; } + @Override + public TypeID getType() { + return resolved.getType(); + } + @Override public StaticCallable getConstructor() { return resolved.getConstructor(); @@ -126,8 +131,8 @@ public Optional findIterator(int variables) { } @Override - public ResolvedType withExpansions(TypeID type, List expansions) { - return new SubclassResolvedType(superclass.withExpansions(supertype, expansions), resolved.withExpansions(type, expansions), supertype); + public ResolvedType withExpansions(List expansions) { + return new SubclassResolvedType(superclass.withExpansions(expansions), resolved.withExpansions(expansions), supertype); } private static Optional or(Optional value, Supplier> other) { 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 1e6632f2..84c94c56 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 @@ -27,6 +27,11 @@ public JavaNativeTypeMembers(JavaNativeTypeTemplate template, TypeID type, Gener this.mapper = mapper; } + @Override + public TypeID getType() { + return type; + } + @Override public StaticCallable getConstructor() { return new StaticCallable(template.getConstructors().stream()