Skip to content

Commit 5853a1e

Browse files
committed
WIP replacing BindingsMap access and AtomTypeInterface with StaticModuleScope
1 parent 2a3c789 commit 5853a1e

File tree

9 files changed

+76
-35
lines changed

9 files changed

+76
-35
lines changed

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/BuiltinTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private BuiltinTypes() {}
2020

2121
private static TypeRepresentation fromQualifiedName(String qualifiedName) {
2222
var fqn = QualifiedName$.MODULE$.fromString(qualifiedName);
23-
return new TypeRepresentation.AtomType(fqn, null);
23+
return new TypeRepresentation.AtomType(fqn);
2424
}
2525

2626
public static boolean isAny(QualifiedName qualifiedName) {

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/MethodTypeResolver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.enso.compiler.pass.analyse.types;
22

3+
import org.enso.compiler.pass.analyse.types.scope.AtomType;
34
import org.enso.compiler.pass.analyse.types.scope.BuiltinsFallbackScope;
45
import org.enso.compiler.pass.analyse.types.scope.ModuleResolver;
56
import org.enso.compiler.pass.analyse.types.scope.StaticMethodResolution;

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/TypePropagation.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import org.enso.compiler.pass.analyse.alias.AliasMetadata;
2222
import org.enso.compiler.pass.analyse.alias.graph.Graph;
2323
import org.enso.compiler.pass.analyse.alias.graph.GraphOccurrence;
24+
import org.enso.compiler.pass.analyse.types.scope.AtomType;
2425
import org.enso.compiler.pass.analyse.types.scope.ModuleResolver;
2526
import org.enso.compiler.pass.analyse.types.scope.StaticModuleScope;
2627
import org.enso.compiler.pass.analyse.types.scope.TypeScopeReference;
28+
import org.enso.pkg.QualifiedName;
2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
2931
import scala.Option;
@@ -41,6 +43,7 @@ abstract class TypePropagation {
4143
private static final Logger logger = LoggerFactory.getLogger(TypePropagation.class);
4244
private final TypeResolver typeResolver;
4345
private final TypeCompatibility compatibilityChecker;
46+
private final ModuleResolver moduleResolver;
4447
private final MethodTypeResolver methodTypeResolver;
4548

4649
TypePropagation(
@@ -50,6 +53,7 @@ abstract class TypePropagation {
5053
ModuleResolver moduleResolver) {
5154
this.typeResolver = typeResolver;
5255
this.compatibilityChecker = compatibilityChecker;
56+
this.moduleResolver = moduleResolver;
5357

5458
var currentModuleScope = StaticModuleScope.forIR(currentModule);
5559
this.methodTypeResolver = new MethodTypeResolver(moduleResolver, currentModuleScope);
@@ -81,6 +85,11 @@ protected abstract void encounteredInvocationOfNonFunctionType(
8185
protected abstract void encounteredNoSuchMethod(
8286
IR relatedIr, TypeRepresentation type, String methodName, MethodCallKind kind);
8387

88+
/**
89+
* The callback that is called when a constructor is being invoked on a type that does not have such a constructor.
90+
*/
91+
protected abstract void encounteredNoSuchConstructor(IR relatedIr, TypeRepresentation type, String constructorName);
92+
8493
enum MethodCallKind {
8594
MEMBER,
8695
STATIC,
@@ -323,6 +332,12 @@ private TypeRepresentation processSingleApplication(
323332
return null;
324333
}
325334

335+
private AtomType findTypeDefinition(QualifiedName name) {
336+
var module = moduleResolver.findContainingModule(TypeScopeReference.atomType(name));
337+
var moduleScope = StaticModuleScope.forIR(module);
338+
return moduleScope.getType(name.item());
339+
}
340+
326341
private TypeRepresentation processUnresolvedSymbolApplication(
327342
TypeRepresentation.UnresolvedSymbol function,
328343
Expression argument,
@@ -336,14 +351,26 @@ private TypeRepresentation processUnresolvedSymbolApplication(
336351
switch (argumentType) {
337352
case TypeRepresentation.TypeObject typeObject -> {
338353
if (isConstructorOrType(function.name())) {
339-
var ctorCandidate =
340-
typeObject.typeInterface().constructors().stream()
341-
.filter(ctor -> ctor.name().equals(function.name()))
342-
.findFirst();
343-
if (ctorCandidate.isPresent()) {
344-
return typeResolver.buildAtomConstructorType(typeObject, ctorCandidate.get());
354+
var typeDefinition = findTypeDefinition(typeObject.name());
355+
if (typeDefinition == null) {
356+
logger.warn(
357+
"processUnresolvedSymbolApplication: {} - no type definition found for {}",
358+
relatedWholeApplicationIR.showCode(),
359+
typeObject.name());
360+
return null;
361+
}
362+
363+
var constructor = typeDefinition.getConstructor(function.name());
364+
if (constructor != null) {
365+
if (constructor.type() == null) {
366+
// type is unknown due to default arguments
367+
// TODO later on this should be assert != null because all constructors should have a type (once we can deal with default arguments)
368+
return null;
369+
}
370+
371+
return constructor.type();
345372
} else {
346-
// TODO we could report that no valid constructor was found
373+
encounteredNoSuchConstructor(relatedWholeApplicationIR, argumentType, function.name());
347374
return null;
348375
}
349376
} else {

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/TypeRepresentation.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public QualifiedName getAssociatedType() {
5151
* <p>Instances that are assigned this type are built with one of the available constructors, but
5252
* statically we do not necessarily know which one.
5353
*/
54-
record AtomType(QualifiedName fqn, AtomTypeInterface typeInterface)
54+
record AtomType(QualifiedName fqn)
5555
implements TypeRepresentation {
5656
@Override
5757
public String toString() {
@@ -157,10 +157,8 @@ public String toString() {
157157
* using its constructors, which will be assigned the corresponding AtomType.
158158
*
159159
* @param name the qualified name of the type
160-
* @param typeInterface the declared interface of the type
161160
*/
162-
record TypeObject(QualifiedName name, AtomTypeInterface typeInterface)
163-
implements TypeRepresentation {
161+
record TypeObject(QualifiedName name) implements TypeRepresentation {
164162
@Override
165163
public String toString() {
166164
return "(type " + name.item() + ")";
@@ -179,7 +177,7 @@ public TypeRepresentation instanceType() {
179177
return new ArrowType(TypeRepresentation.ANY, TypeRepresentation.ANY);
180178
}
181179

182-
return new AtomType(name, typeInterface);
180+
return new AtomType(name);
183181
}
184182

185183
@Override

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/TypeResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ TypeRepresentation resolvedTypeAsAtomType(BindingsMap.ResolvedType resolvedType)
129129
return resolvedTypeAsTypeObject(resolvedType).instanceType();
130130
}
131131

132-
TypeRepresentation buildAtomConstructorType(
132+
public TypeRepresentation buildAtomConstructorType(
133133
TypeRepresentation.TypeObject parentType, AtomTypeInterface.Constructor constructor) {
134134
boolean hasAnyDefaults =
135135
constructor.arguments().stream().anyMatch(AtomTypeInterface.Argument::hasDefaultValue);

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/scope/AtomType.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@
66

77
public final class AtomType {
88
private final String name;
9+
private final List<Constructor> constructors;
910

1011
public AtomType(String name, List<Constructor> constructors) {
1112
this.name = name;
13+
this.constructors = constructors;
1214
}
1315

1416
public String getName() {
1517
return name;
1618
}
1719

18-
// TODO mark type in constructor only if no default arguments, treat same as function?
19-
// TODO this should replace AtomTypeInterface
20+
public Constructor getConstructor(String name) {
21+
return constructors.stream().filter(c -> c.name().equals(name)).findFirst().orElse(null);
22+
}
2023

2124
/**
2225
* Represents a constructor of the atom type.
2326
*
2427
* @param name the name of the constructor
2528
* @param isProjectPrivate whether the constructor is project private
2629
* @param type the type ascribed to the constructor, it may be null if it is unknown
30+
* TODO the type will soon be always non-null - once we can handle default arguments
2731
*/
2832
public record Constructor(String name, boolean isProjectPrivate, TypeRepresentation type) {}
2933
}

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/scope/StaticModuleScope.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,8 @@ public TypeRepresentation getConversionFor(TypeScopeReference target, TypeScopeR
167167
// TODO conversions in static analysis
168168
return null;
169169
}
170+
171+
public AtomType getType(String name) {
172+
return typesDefinedHere.get(name);
173+
}
170174
}

engine/runtime-compiler/src/main/java/org/enso/compiler/pass/analyse/types/scope/StaticModuleScopeAnalysis.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ protected void processTypeDefinition(Definition.Type typ) {
139139
List<AtomType.Constructor> constructors =
140140
CollectionConverters$.MODULE$.asJava(typ.members()).stream()
141141
.map(
142-
constructorDef ->
143-
new AtomType.Constructor(
144-
constructorDef.name().name(), constructorDef.isPrivate()))
142+
constructorDef -> {
143+
TypeRepresentation type = buildAtomConstructorType(constructorDef);
144+
return new AtomType.Constructor(
145+
constructorDef.name().name(), constructorDef.isPrivate(), type);
146+
})
145147
.toList();
146148

147149
AtomType atomType = new AtomType(typ.name().name(), constructors);
@@ -151,6 +153,25 @@ protected void processTypeDefinition(Definition.Type typ) {
151153
registerFieldGetters(scopeBuilder, atomTypeScope, typ);
152154
}
153155

156+
private TypeRepresentation buildAtomConstructorType(Definition.Data constructorDef) {
157+
boolean hasDefaults = constructorDef.arguments().exists(a -> a.defaultValue().isDefined());
158+
if (hasDefaults) {
159+
// TODO implement handling of default arguments - not only ctors will need this!
160+
return null;
161+
}
162+
163+
var arguments =
164+
constructorDef.arguments().map(
165+
(arg) -> {
166+
var typ = arg.ascribedType();
167+
// TODO
168+
return typ != null ? typ : TypeRepresentation.UNKNOWN;
169+
})
170+
.toList();
171+
var resultType = parentType.instanceType();
172+
return TypeRepresentation.buildFunction(arguments, resultType);
173+
}
174+
154175
@Override
155176
protected TypeScopeReference associatedTypeFromResolvedModule(
156177
BindingsMap.ResolvedModule module) {

engine/runtime-compiler/src/main/scala/org/enso/compiler/data/BindingsMap.scala

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -595,15 +595,7 @@ object BindingsMap {
595595
def allFieldsDefaulted: Boolean = arguments.forall(_.hasDefaultValue)
596596
}
597597

598-
case class Argument(
599-
name: String,
600-
hasDefaultValue: Boolean,
601-
typReference: Reference[Expression]
602-
) {
603-
def typ(): Option[Expression] = Option(
604-
typReference.get(classOf[Expression])
605-
)
606-
}
598+
case class Argument(name: String, hasDefaultValue: Boolean)
607599

608600
/** A representation of a sum type
609601
*
@@ -629,15 +621,9 @@ object BindingsMap {
629621
Cons(
630622
m.name.name,
631623
m.arguments.map { arg =>
632-
val ascribedType: Reference[Expression] =
633-
arg.ascribedType match {
634-
case Some(value) => Reference.of(value, true)
635-
case None => Reference.none()
636-
}
637624
BindingsMap.Argument(
638625
arg.name.name,
639-
arg.defaultValue.isDefined,
640-
ascribedType
626+
arg.defaultValue.isDefined
641627
)
642628
},
643629
m.isPrivate

0 commit comments

Comments
 (0)