Skip to content

Commit

Permalink
Make ResolvedType remember its type (and simplified the withExpansion…
Browse files Browse the repository at this point in the history
…s method)
  • Loading branch information
stanhebben committed Aug 30, 2024
1 parent 05f88af commit c27ca1a
Show file tree
Hide file tree
Showing 22 changed files with 63 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Optional<IGlobal> findGlobal(String name) {

@Override
public ResolvedType resolve(TypeID type) {
return type.resolve().withExpansions(type, expansions);
return type.resolve().withExpansions(expansions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Optional;

public interface ResolvedType {
TypeID getType();

StaticCallable getConstructor();

Optional<StaticCallable> findImplicitConstructor();
Expand Down Expand Up @@ -76,10 +78,10 @@ default boolean canCastImplicitlyTo(TypeID target) {

Optional<StaticCallable> findStaticOperator(OperatorType operator);

default ResolvedType withExpansions(TypeID type, List<ExpansionSymbol> expansions) {
default ResolvedType withExpansions(List<ExpansionSymbol> expansions) {
List<ResolvedType> resolutions = new ArrayList<>();
for (ExpansionSymbol expansion : expansions) {
expansion.resolve(type).ifPresent(resolutions::add);
expansion.resolve(getType()).ifPresent(resolutions::add);
}
return ExpandedResolvedType.of(this, resolutions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Optional<ResolvedType> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public interface IDefinitionMember {

void registerTo(TypeID targetType, MemberSet.Builder members, GenericMapper mapper);

default List<ResolvedType> resolveExpansions(List<ExpansionSymbol> expansions) {
return Collections.emptyList();
}

default Optional<TypeID> asImplementation() {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ public void registerTo(TypeID targetType, MemberSet.Builder members, GenericMapp
}
}

@Override
public List<ResolvedType> resolveExpansions(List<ExpansionSymbol> expansions) {
return expansions.stream()
.map(expansion -> expansion.resolve(type))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
}

@Override
public Optional<TypeID> asImplementation() {
return Optional.of(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public boolean isInvalid() {

@Override
public ResolvedType resolve() {
return new MemberSet();
return new MemberSet(this);
}

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

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TypeParameter, TypeID> parameterFilled = new HashMap<>();
parameterFilled.put(PARAMETER, typeArguments[0]);
parameterFilled.put(VALUE, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ private ExpandedResolvedType(ResolvedType base, List<ResolvedType> expansions) {
this.expansions = expansions;
}

@Override
public TypeID getType() {
return base.getType();
}

@Override
public StaticCallable getConstructor() {
return base.getConstructor();
Expand Down Expand Up @@ -194,8 +199,8 @@ public Optional<IteratorInstance> findIterator(int variables) {
}

@Override
public ResolvedType withExpansions(TypeID type, List<ExpansionSymbol> expansions) {
List<ResolvedType> 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<ExpansionSymbol> expansions) {
List<ResolvedType> newExpansions = this.expansions.stream().map(expansion -> expansion.withExpansions(expansions)).collect(Collectors.toList());
return ExpandedResolvedType.of(base.withExpansions(expansions), newExpansions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public InterfaceResolvedType(ResolvedType baseType, Collection<TypeID> implement
this.implementedInterfaces = implementedInterfaces;
}

@Override
public TypeID getType() {
return baseType.getType();
}

@Override
public StaticCallable getConstructor() {
return baseType.getConstructor();
Expand Down Expand Up @@ -126,14 +131,14 @@ public Optional<StaticCallable> findStaticOperator(OperatorType operator) {
}

@Override
public ResolvedType withExpansions(TypeID type, List<ExpansionSymbol> expansions) {
public ResolvedType withExpansions(List<ExpansionSymbol> expansions) {
List<ResolvedType> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StaticCallableMethod> constructors = new ArrayList<>();
private final Map<MethodID, List<InstanceCallableMethod>> instanceMethods = new HashMap<>();
private final Map<MethodID, List<StaticCallableMethod>> staticMethods = new HashMap<>();
Expand All @@ -28,6 +29,15 @@ public static Builder create() {
private final List<IteratorInstance> iterators = new ArrayList<>();
private final List<Comparator> comparators = new ArrayList<>();

public MemberSet(TypeID type) {
this.type = type;
}

@Override
public TypeID getType() {
return type;
}

@Override
public StaticCallable getConstructor() {
return new StaticCallable(constructors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -126,8 +131,8 @@ public Optional<IteratorInstance> findIterator(int variables) {
}

@Override
public ResolvedType withExpansions(TypeID type, List<ExpansionSymbol> expansions) {
return new SubclassResolvedType(superclass.withExpansions(supertype, expansions), resolved.withExpansions(type, expansions), supertype);
public ResolvedType withExpansions(List<ExpansionSymbol> expansions) {
return new SubclassResolvedType(superclass.withExpansions(expansions), resolved.withExpansions(expansions), supertype);
}

private static <T> Optional<T> or(Optional<T> value, Supplier<Optional<T>> other) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c27ca1a

Please sign in to comment.