Skip to content

Commit

Permalink
Merge pull request #169 from ZenCodeLang/feature/refactor-simplify-ty…
Browse files Browse the repository at this point in the history
…pe-resolution

Feature/refactor introduce ResolvingType
  • Loading branch information
stanhebben authored Sep 3, 2024
2 parents 4014692 + 74ccdbe commit 301ba5b
Show file tree
Hide file tree
Showing 51 changed files with 387 additions and 203 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ build
*.class

out/
.idea
.idea

*.xslt
*.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String format(TypeID type) {

@Override
public String visitBasic(BasicTypeID basic) {
return basic.name;
return basic.getName();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public GenericName(String name, TypeID[] arguments) {
this.arguments = arguments;
}

public static Optional<TypeID> getInnerType(TypeID type, List<GenericName> name, int index, List<ExpansionSymbol> expansions) {
public static Optional<TypeID> getInnerType(TypeID type, List<GenericName> name, int index) {
while (index < name.size()) {
GenericName innerName = name.get(index++);
ResolvedType members = type.resolve();
ResolvedType members = type.resolveWithoutExpansions();
Optional<TypeID> inner = members
.findInnerType(innerName.name)
.map(t -> DefinitionTypeID.create(t, innerName.arguments));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
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;
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 org.openzen.zenscript.codemodel.type.member.*;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -144,7 +142,9 @@ public String getName() {
}

@Override
public ResolvedType 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) {
Expand All @@ -155,19 +155,15 @@ 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 InterfaceResolvingType.of(resolved, interfaces);
}

protected void resolveAdditional(TypeID type, MemberSet.Builder members, GenericMapper mapper) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public Optional<TypeID> resolve(CodePosition position, List<GenericName> 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))
.flatMap(t -> t.getType(position, this, name.get(0).arguments))
.flatMap(t -> {
for (int i = 1; i < name.size(); i++) {
Optional<TypeSymbol> inner = t.resolve().findInnerType(name.get(i).name);
Optional<TypeSymbol> inner = t.resolveWithoutExpansions().findInnerType(name.get(i).name);
if (inner.isPresent()) {
t = DefinitionTypeID.create(inner.get(), name.get(i).arguments);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,14 +75,6 @@ default boolean canCastImplicitlyTo(TypeID target) {

Optional<StaticCallable> findStaticOperator(OperatorType operator);

default ResolvedType withExpansions(List<ExpansionSymbol> expansions) {
List<ResolvedType> resolutions = new ArrayList<>();
for (ExpansionSymbol expansion : expansions) {
expansion.resolve(getType()).ifPresent(resolutions::add);
}
return ExpandedResolvedType.of(this, resolutions);
}

interface SwitchMember {
SwitchValue toSwitchValue(List<CompilingVariable> bindings);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ExpansionSymbol> expansions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Optional<StaticCallable> superCall() {
return Optional.empty();

if (resolvedSuper == null)
resolvedSuper = thisType.getSuperType().resolve();
resolvedSuper = thisType.getSuperType().resolveWithoutExpansions();

return Optional.ofNullable(resolvedSuper)
.map(super_ -> super_.getConstructor().map(constructor -> new SuperCallable(thisType.getSuperType(), constructor)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void addDefaultMembers() {
boolean hasNoConstructor = members.stream().noneMatch(IDefinitionMember::isConstructor);

Optional<MethodInstance> superConstructor = Optional.ofNullable(getSuperType())
.flatMap(t -> t.resolve().getConstructor().getSingleOverload())
.flatMap(t -> t.resolveWithoutExpansions().getConstructor().getSingleOverload())
.flatMap(AnyMethod::asMethod);

if (hasNoConstructor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public Optional<ResolvedType> resolve(TypeID expandingType) {
for (IDefinitionMember member : members)
member.registerTo(expandingType, resolution, mapper);

return Optional.of(resolution.build());
return Optional.of(resolution.buildWithoutExpansions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public TypeSymbol getImport(List<String> name, int depth) {
return null;
}

public Optional<TypeID> getType(List<GenericName> nameParts, List<ExpansionSymbol> expansions) {
return getType(nameParts, 0, expansions);
public Optional<TypeID> getType(List<GenericName> nameParts) {
return getType(nameParts, 0);
}

public Optional<TypeID> getType(GenericName name) {
Expand All @@ -64,17 +64,17 @@ public Optional<TypeID> getType(GenericName name) {
}
}

private Optional<TypeID> getType(List<GenericName> nameParts, int depth, List<ExpansionSymbol> expansions) {
private Optional<TypeID> getType(List<GenericName> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +19,7 @@ public String getCanonical() {
}

@Override
public Optional<ResolvedType> resolveMembers() {
public Optional<ResolvingType> resolveMembers() {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +22,7 @@ public String getCanonical() {
}

@Override
public Optional<ResolvedType> resolveMembers() {
public Optional<ResolvingType> resolveMembers() {
return Optional.of(type.resolve());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +13,7 @@ public interface TypeParameterBound {

<C, R> R accept(C context, GenericParameterBoundVisitorWithContext<C, R> visitor);

Optional<ResolvedType> resolveMembers();
Optional<ResolvingType> resolveMembers();

boolean matches(TypeID type);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,7 +19,7 @@ public interface TypeSymbol extends DefinitionSymbol {

String getName();

ResolvedType resolve(TypeID type, TypeID[] typeArguments);
ResolvingType resolve(TypeID[] typeArguments);

Optional<TypeSymbol> getOuter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,8 +72,8 @@ public Optional<ArrayTypeID> asArray() {
}

@Override
public ResolvedType resolve() {
return type.resolve(this, new TypeID[] { elementType });
public ResolvingType resolve() {
return type.resolve(new TypeID[] { elementType });
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -53,8 +53,8 @@ public Optional<AssocTypeID> asAssoc() {
}

@Override
public ResolvedType resolve() {
return MapTypeSymbol.INSTANCE.resolve(this, new TypeID[] { keyType, valueType });
public ResolvingType resolve() {
return MapTypeSymbol.INSTANCE.resolve(new TypeID[] { keyType, valueType });
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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;
import org.openzen.zenscript.codemodel.type.builtin.BasicTypeMembers;
import org.openzen.zenscript.codemodel.type.member.MemberSet;

import java.util.List;
import java.util.Optional;
Expand All @@ -35,8 +34,8 @@ public enum BasicTypeID implements TypeID, TypeSymbol {
UNDETERMINED("undetermined"),
INVALID("invalid");

public final String name;
private MemberSet members;
private final String name;
private ResolvingType members;

private Expression defaultValue = null;

Expand Down Expand Up @@ -64,18 +63,13 @@ public <C, R, E extends Exception> R accept(C context, TypeVisitorWithContext<C,
return visitor.visitBasic(context, this);
}

@Override
public boolean isOptional() {
return false;
}

@Override
public boolean isValueType() {
return true;
}

@Override
public ResolvedType resolve() {
public ResolvingType resolve() {
if (members == null)
members = BasicTypeMembers.get(this);

Expand All @@ -97,7 +91,7 @@ public Expression getDefaultValue() {

@Override
public void extractTypeParameters(List<TypeParameter> typeParameters) {

// BasicTypeIDs don't have type parameters
}

private Expression generateDefaultValue() {
Expand Down Expand Up @@ -183,11 +177,12 @@ public String getName() {
}

@Override
public ResolvedType resolve(TypeID type, TypeID[] typeArguments) {
if (members == null)
members = BasicTypeMembers.get(this);
public ResolvingType resolve(TypeID[] typeArguments) {
if(typeArguments.length > 0) {
throw new IllegalArgumentException(this + " cannot have type arguments");
}

return members;
return this.resolve();
}

@Override
Expand Down
Loading

0 comments on commit 301ba5b

Please sign in to comment.