Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor introduce ResolvingType #169

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -27,7 +27,7 @@ public GenericName(String name, TypeID[] arguments) {
public static Optional<TypeID> getInnerType(TypeID type, List<GenericName> name, int index, List<ExpansionSymbol> expansions) {
while (index < name.size()) {
GenericName innerName = name.get(index++);
ResolvedType members = type.resolve();
ResolvedType members = type.resolve().withExpansions(expansions);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Question: Do we even need the expansions here or could we remove that argument altogether and type.resolveWithoutExpansions()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we may not need it here.

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 @@ -102,7 +102,7 @@ public Optional<TypeID> resolve(CodePosition position, List<GenericName> 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
@@ -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
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.definition.EnumDefinition;
import org.openzen.zenscript.codemodel.definition.StructDefinition;
import org.openzen.zenscript.codemodel.definition.VariantDefinition;
Expand Down Expand Up @@ -53,8 +53,8 @@ public DefinitionTypeID(TypeSymbol definition, TypeID[] typeArguments, Definitio
*
* @return base type members
*/
public ResolvedType resolve() {
return definition.resolve(this, typeArguments);
public ResolvingType resolve() {
return definition.resolve(typeArguments);
}

public boolean hasTypeParameters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.openzen.zenscript.codemodel.FunctionHeader;
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.GenericMapper;
import org.openzen.zenscript.codemodel.compilation.ResolvedType;
import org.openzen.zenscript.codemodel.compilation.ResolvingType;
import org.openzen.zenscript.codemodel.generic.TypeParameter;
import org.openzen.zenscript.codemodel.type.builtin.FunctionTypeSymbol;

Expand Down Expand Up @@ -56,8 +56,8 @@ public Optional<FunctionTypeID> asFunction() {
}

@Override
public ResolvedType resolve() {
return type.resolve(this, TypeID.NONE);
public ResolvingType resolve() {
return type.resolve(TypeID.NONE);
}

@Override
Expand Down
Loading
Loading