Skip to content

Commit

Permalink
finish implementing the logic in StaticModuleScopeAnalysis, start pha…
Browse files Browse the repository at this point in the history
…sing out AtomTypeInterface
  • Loading branch information
radeusgd committed Dec 31, 2024
1 parent 968af64 commit 31934b9
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* <p>This interface declares what methods can be called on instances of that type (or statically)
* and what constructors may be called on it to create new instances.
*/
@Deprecated
interface AtomTypeInterface {
List<? extends Constructor> constructors();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import scala.jdk.javaapi.CollectionConverters$;

/** Implementation of {@link AtomTypeInterface} that is built from a {@link BindingsMap.Type}. */
@Deprecated
public final class AtomTypeInterfaceFromBindingsMap implements AtomTypeInterface {
// TODO this probably is no longer needed since we have StaticModuleScope
private final BindingsMap.Type type;
Expand Down Expand Up @@ -38,8 +39,6 @@ public String name() {
return constructor.name();
}

private transient List<? extends Argument> arguments = null;

@Override
public List<? extends Argument> arguments() {
return new ProxyList<>(
Expand All @@ -65,14 +64,10 @@ public boolean hasDefaultValue() {
return arg.hasDefaultValue();
}

@Deprecated
@Override
public TypeRepresentation getType(TypeResolver resolver) {
if (arg.typ().isEmpty()) {
return null;
} else {
Expression expression = arg.typ().get();
return resolver.resolveTypeExpression(expression);
}
throw new IllegalStateException("No longer supported - this class will be removed soon.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ protected void encounteredNoSuchMethod(
.getDiagnostics()
.add(new Warning.NoSuchMethod(relatedIr.identifiedLocation(), methodDescription));
}

@Override
protected void encounteredNoSuchConstructor(IR relatedIr, TypeRepresentation type, String constructorName) {
// TODO make sure if NoSuchMethod is right or we need a separate type here
String methodDescription = "constructor `" + constructorName + "` on type " + type;
relatedIr
.getDiagnostics()
.add(new Warning.NoSuchMethod(relatedIr.identifiedLocation(), methodDescription));
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ protected TypeRepresentation resolveGlobalName(BindingsMap.ResolvedName resolved
return switch (resolvedName) {
// TODO investigate when do these appear?? I did not yet see them in the wild
case BindingsMap.ResolvedConstructor ctor -> {
// TODO can we replace this with querying StaticModuleScope?
var constructorInterface =
new AtomTypeInterfaceFromBindingsMap.ConstructorFromBindingsMap(ctor.cons());
yield typeResolver.buildAtomConstructorType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ private TypeRepresentation getResolvedTypeFromBindingsMap(Name name) {
}

TypeRepresentation.TypeObject resolvedTypeAsTypeObject(BindingsMap.ResolvedType resolvedType) {
var iface = new AtomTypeInterfaceFromBindingsMap(resolvedType.tp());
return new TypeRepresentation.TypeObject(resolvedType.qualifiedName(), iface);
return new TypeRepresentation.TypeObject(resolvedType.qualifiedName());
}

TypeRepresentation resolvedTypeAsAtomType(BindingsMap.ResolvedType resolvedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.enso.compiler.pass.resolve.FullyQualifiedNames$;
import org.enso.compiler.pass.resolve.GlobalNames$;
import org.enso.compiler.pass.resolve.TypeNames$;
import org.enso.pkg.QualifiedName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.Option;
import scala.collection.immutable.Seq;
import scala.jdk.javaapi.CollectionConverters;
import scala.jdk.javaapi.CollectionConverters$;
Expand Down Expand Up @@ -136,24 +138,26 @@ protected void processMethodDefinition(Method.Explicit method) {

@Override
protected void processTypeDefinition(Definition.Type typ) {
QualifiedName qualifiedName = scopeBuilder.getModuleName().createChild(typ.name().name());
TypeRepresentation.TypeObject typeObject = new TypeRepresentation.TypeObject(qualifiedName);
List<AtomType.Constructor> constructors =
CollectionConverters$.MODULE$.asJava(typ.members()).stream()
.map(
constructorDef -> {
TypeRepresentation type = buildAtomConstructorType(constructorDef);
TypeRepresentation type = buildAtomConstructorType(typeObject, constructorDef);
return new AtomType.Constructor(
constructorDef.name().name(), constructorDef.isPrivate(), type);
})
.toList();

AtomType atomType = new AtomType(typ.name().name(), constructors);
var qualifiedName = scopeBuilder.getModuleName().createChild(typ.name().name());
var atomTypeScope = TypeScopeReference.atomType(qualifiedName);
scopeBuilder.registerType(atomType);
registerFieldGetters(scopeBuilder, atomTypeScope, typ);
}

private TypeRepresentation buildAtomConstructorType(Definition.Data constructorDef) {
private TypeRepresentation buildAtomConstructorType(
TypeRepresentation.TypeObject associatedType, Definition.Data constructorDef) {
boolean hasDefaults = constructorDef.arguments().exists(a -> a.defaultValue().isDefined());
if (hasDefaults) {
// TODO implement handling of default arguments - not only ctors will need this!
Expand All @@ -165,13 +169,18 @@ private TypeRepresentation buildAtomConstructorType(Definition.Data constructorD
.arguments()
.map(
(arg) -> {
var typ = arg.ascribedType();
// TODO
return typ != null ? typ : TypeRepresentation.UNKNOWN;
Option<Expression> typ = arg.ascribedType();
if (typ.isEmpty()) {
return TypeRepresentation.UNKNOWN;
}

var resolvedType = typeResolver.resolveTypeExpression(typ.get());
assert resolvedType != null;
return resolvedType;
})
.toList();
var resultType = parentType.instanceType();
return TypeRepresentation.buildFunction(arguments, resultType);
var resultType = associatedType.instanceType();
return TypeRepresentation.buildFunction(CollectionConverters.asJava(arguments), resultType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.enso.compiler.data.BindingsMap.{
import org.enso.compiler.pass.analyse.BindingAnalysis
import org.enso.compiler.pass.{PassConfiguration, PassGroup, PassManager}
import org.enso.compiler.test.CompilerTest
import org.enso.persist.Persistance

class BindingAnalysisTest extends CompilerTest {

Expand Down Expand Up @@ -168,18 +167,15 @@ class BindingAnalysisTest extends CompilerTest {
List(
Argument(
"a",
hasDefaultValue = false,
Persistance.Reference.none()
hasDefaultValue = false
),
Argument(
"b",
hasDefaultValue = false,
Persistance.Reference.none()
hasDefaultValue = false
),
Argument(
"c",
hasDefaultValue = false,
Persistance.Reference.none()
hasDefaultValue = false
)
),
isProjectPrivate = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import java.io.IOException;
import java.util.List;
import org.enso.compiler.data.BindingsMap;
import org.enso.compiler.pass.analyse.types.AtomTypeInterfaceFromBindingsMap;
import org.enso.compiler.pass.analyse.types.InferredType;
import org.enso.compiler.pass.analyse.types.TypeRepresentation;
import org.enso.persist.Persistance;
Expand Down Expand Up @@ -42,16 +40,6 @@ public void writeSomeInferredType() throws Exception {

private TypeRepresentation.TypeObject mockObject() {
var fqn = new QualifiedName(makeScalaList(List.of("mod")), "Test");
return new TypeRepresentation.TypeObject(fqn, mockAtomType());
}

private AtomTypeInterfaceFromBindingsMap mockAtomType() {
scala.collection.immutable.List<String> params = makeScalaList(List.of());
var ctorArgs =
makeScalaList(
List.of(new BindingsMap.Argument("arg", false, Persistance.Reference.none())));
var constructors = makeScalaList(List.of(new BindingsMap.Cons("ctor", ctorArgs, false)));
return new AtomTypeInterfaceFromBindingsMap(
new BindingsMap.Type("Test", params, constructors, false));
return new TypeRepresentation.TypeObject(fqn);
}
}

0 comments on commit 31934b9

Please sign in to comment.