Skip to content

Commit

Permalink
Fix implementation of generic interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Oct 18, 2024
1 parent e852cde commit 5634b1c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ public ResolvingType resolve(TypeID[] typeArguments) {
for (IDefinitionMember member : this.members) {
member.registerTo(type, members, mapper);
}
List<TypeID> interfaces = this.members.stream().map(IDefinitionMember::asImplementation).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
List<TypeID> interfaces = this.members.stream()
.map(IDefinitionMember::asImplementation)
.filter(Optional::isPresent)
.map(Optional::get)
.map(mapper::map)
.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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public Void visitCustomIterator(IteratorMember member) {

@Override
public Void visitImplementation(ImplementationMember implementation) {
for (MemberAnnotation annotation : implementation.annotations)
annotation.apply(implementation);

for (IDefinitionMember member : implementation.members) {
member.accept(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ public Void visitCustomIterator(IteratorMember member) {

@Override
public Void visitImplementation(ImplementationMember member) {
if (member.getTag(NativeTag.class) != null) {
return null;
}

JavaImplementation implementation = context.getJavaImplementation(member);
if (implementation.inline) {
for (IDefinitionMember imember : member.members)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void addMethod(String key, JavaMethod method) {
}

public void addConstructor(String key, String descriptor) {
methods.put(key, createMethod("<init>", descriptor, JavaNativeMethod.Kind.CONSTRUCTOR));
methods.put(key, JavaNativeMethod.getNativeConstructor(cls, descriptor));
}

public void addInstanceMethod(String key, String name, String descriptor) {
Expand All @@ -41,12 +41,12 @@ public void addInstanceMethod(String key, String name, String descriptor, boolea
methods.put(key, createMethod(name, descriptor, JavaNativeMethod.Kind.INSTANCE, genericReturnType));
}

public JavaNativeMethod createMethod(String name, String descriptor, JavaNativeMethod.Kind instance) {
return createMethod(name, descriptor, instance, false);
public JavaNativeMethod createMethod(String name, String descriptor, JavaNativeMethod.Kind kind) {
return createMethod(name, descriptor, kind, false);
}

public JavaNativeMethod createMethod(String name, String descriptor, JavaNativeMethod.Kind instance, boolean genericReturnType) {
return new JavaNativeMethod(cls, instance, name, false, descriptor, JavaModifiers.PUBLIC, genericReturnType);
public JavaNativeMethod createMethod(String name, String descriptor, JavaNativeMethod.Kind kind, boolean genericReturnType) {
return new JavaNativeMethod(cls, kind, name, false, descriptor, JavaModifiers.PUBLIC, genericReturnType);
}

public JavaNativeMethod createInstanceMethod(String name, String descriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class JavaPrepareDefinitionVisitor implements DefinitionVisitor<JavaClass
}

{
JavaNativeClass cls = new JavaNativeClass(new JavaClass("java.util", "HashSet", JavaClass.Kind.INTERFACE));
JavaNativeClass cls = new JavaNativeClass(new JavaClass("java.util", "HashSet", JavaClass.Kind.CLASS));
cls.addConstructor("constructor", "()V");
nativeClasses.put("collections::HashSet", cls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void prepare(List<CompileException> errors) {

@Override
public void compile(List<CompileException> errors) {
compiled.annotations = ParsedAnnotation.compileForMember(annotations, compiled, compiler);
for (CompilingMember member : members) {
member.compile(errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Test {
class TestClass {
implements Test {
protected print() {
return "hello";
println("hello");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,21 @@ public Void visitImplementation(ImplementationMember implementation) {
}

private void checkImplementationComplete(ImplementationMember implementation) {
if (implementation.getEffectiveModifiers().isExtern()) {
return;
}

ImplementationCheckValidator implementationCheckValidator = new ImplementationCheckValidator(validator, implementation);
implementation.members.forEach(member -> member.accept(implementationCheckValidator));
implementation.members.forEach(member -> {
ValidationUtils.validateModifiers(
validator,
member.getSpecifiedModifiers(),
Modifiers.FLAG_PRIVATE | Modifiers.FLAG_PUBLIC,
member.getPosition(),
"Invalid modifier or implementation member"
);
member.accept(implementationCheckValidator);
});

List<MethodSymbol> unimplementedMembers = implementationCheckValidator.getUnimplementedMembers();
if (!unimplementedMembers.isEmpty()) {
Expand Down

0 comments on commit 5634b1c

Please sign in to comment.