From 0fa0028daf69bc80a7e3b3ff9c6491fd12b9cc0b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Mar 2025 19:07:30 +0000 Subject: Generate Shid for native type method call --- .../org/shedlang/compiler/shid/ShidCodeGen.java | 21 +++++++++++++++++++-- .../org/shedlang/compiler/shid/ShidSerializer.java | 17 +++++++---------- .../compiler/shid/instructions/ShidMethodLoad.java | 4 ++-- .../compiler/shidtopatio/ShidToPatioMethods.java | 10 +++++----- .../shedlang/compiler/types/SimpleConcreteType.java | 9 +++++++++ .../shedlang/compiler/types/SimpleNativeType.java | 2 +- .../shedlang/compiler/types/SimpleStructType.java | 2 +- src/main/java/org/shedlang/compiler/types/Type.java | 2 +- 8 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/types/SimpleConcreteType.java (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java index b158de08..5f229ebc 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -118,13 +118,30 @@ public class ShidCodeGen { receiverNode.memberKind() == MemberKind.METHOD ) { extraArgumentCount = 1; - var methodCallReceiver = receiverTypeToReceiver(receiverNode.receiver().type()); - var methodCallReceiverType = ((ShidFieldLoad.Receiver.Struct) methodCallReceiver).type(); + // TODO: handle not concrete type + var methodCallReceiverType = switch (receiverNode.receiver().type()) { + case SimpleConcreteType concreteType -> + concreteType; + + case ConstructedType constructedType -> { + if (constructedType.genericType() instanceof SimpleConcreteType concreteType) { + yield concreteType; + } else { + throw new UnsupportedOperationException("TODO"); + } + } + + default -> { + throw new UnsupportedOperationException("TODO"); + } + }; + context = context.addInstruction(new ShidMethodLoad( methodCallReceiverType, receiverNode.memberName(), receiverNode.source() )); + context = compileExpression(receiverNode.receiver(), context); } else { context = compileExpression(node.receiver(), context); diff --git a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java index 6a60ccc8..6dc622f3 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidSerializer.java @@ -2,10 +2,7 @@ package org.shedlang.compiler.shid; import org.shedlang.compiler.shid.instructions.*; import org.shedlang.compiler.treetext.Tree; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.SimpleNativeType; -import org.shedlang.compiler.types.SimpleStructType; -import org.shedlang.compiler.types.UserDefinedEffect; +import org.shedlang.compiler.types.*; import java.util.List; @@ -117,7 +114,7 @@ public class ShidSerializer { case ShidDefineStruct defineStructInstruction -> treeListIndented( treeIdentifier("define-struct"), - treeNamed("type", structTypeToTree(defineStructInstruction.type())), + treeNamed("type", concreteTypeToTree(defineStructInstruction.type())), treeNamed("fields", treeList( defineStructInstruction.fields().stream() .map(field -> treeString(field.name())) @@ -160,7 +157,7 @@ public class ShidSerializer { case ShidFieldLoad.Receiver.Struct struct -> treeList( treeIdentifier("struct"), - structTypeToTree(struct.type()) + concreteTypeToTree(struct.type()) ); }, treeString(fieldLoad.fieldName()) @@ -234,14 +231,14 @@ public class ShidSerializer { case ShidMethodLoad methodLoad -> treeList( treeIdentifier("method-load"), - structTypeToTree(methodLoad.type()), + concreteTypeToTree(methodLoad.type()), treeString(methodLoad.methodName()) ); case ShidMethodStore methodStore -> treeList( treeIdentifier("method-store"), - structTypeToTree(methodStore.type()), + concreteTypeToTree(methodStore.type()), treeString(methodStore.methodName()) ); @@ -300,7 +297,7 @@ public class ShidSerializer { treeListIndented( switchInstruction.cases().stream() .map(switchCase -> treeListIndented( - structTypeToTree(switchCase.caseType()), + concreteTypeToTree(switchCase.caseType()), blockToTree(switchCase.body()) )) .toList() @@ -374,7 +371,7 @@ public class ShidSerializer { return treeString(type.describe()); } - private static Tree structTypeToTree(SimpleStructType type) { + private static Tree concreteTypeToTree(SimpleConcreteType type) { return treeString(type.describe()); } } diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java index 79f2307f..15a3ce43 100644 --- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidMethodLoad.java @@ -1,14 +1,14 @@ package org.shedlang.compiler.shid.instructions; import org.shedlang.compiler.sources.Source; -import org.shedlang.compiler.types.SimpleStructType; +import org.shedlang.compiler.types.SimpleConcreteType; /** * Load the method with the given name for the given type and push it onto the * temporary value stack. */ public record ShidMethodLoad( - SimpleStructType type, + SimpleConcreteType type, String methodName, Source source ) implements ShidInstruction { diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java index c621c6cb..3edd3d35 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java @@ -10,7 +10,7 @@ import org.shedlang.compiler.patio.operands.PatioOperand; import org.shedlang.compiler.patio.types.PatioDataLayouts; import org.shedlang.compiler.patio.types.PatioDataValues; import org.shedlang.compiler.sources.Source; -import org.shedlang.compiler.types.SimpleStructType; +import org.shedlang.compiler.types.SimpleConcreteType; import java.text.MessageFormat; import java.util.List; @@ -23,7 +23,7 @@ public class ShidToPatioMethods { public static ShidToPatioContext methodLoad( PatioLocalRef destination, - SimpleStructType type, + SimpleConcreteType type, String methodName, Source source, ShidToPatioContext context @@ -40,7 +40,7 @@ public class ShidToPatioMethods { } public static ShidToPatioContext methodStore( - SimpleStructType type, + SimpleConcreteType type, String methodName, PatioOperand methodValue, Source source, @@ -64,11 +64,11 @@ public class ShidToPatioMethods { return context; } - private static PatioOperand globalOperandMethod(SimpleStructType type, String methodName) { + private static PatioOperand globalOperandMethod(SimpleConcreteType type, String methodName) { return PatioDataPtrConst.dataElement(globalNameMethod(type, methodName)); } - private static String globalNameMethod(SimpleStructType type, String methodName) { + private static String globalNameMethod(SimpleConcreteType type, String methodName) { return MessageFormat.format( "shed__method__{0}__{1}__{2}", namespaceNameToPatioIdentifier(type.namespaceName()), diff --git a/src/main/java/org/shedlang/compiler/types/SimpleConcreteType.java b/src/main/java/org/shedlang/compiler/types/SimpleConcreteType.java new file mode 100644 index 00000000..147acb36 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/types/SimpleConcreteType.java @@ -0,0 +1,9 @@ +package org.shedlang.compiler.types; + +/** + * A specific type that can directly instantiated. + */ +public sealed interface SimpleConcreteType extends Type permits SimpleNativeType, SimpleStructType { + NamespaceName namespaceName(); + String name(); +} diff --git a/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java b/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java index a2acde62..0f6ca56c 100644 --- a/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java +++ b/src/main/java/org/shedlang/compiler/types/SimpleNativeType.java @@ -1,6 +1,6 @@ package org.shedlang.compiler.types; -public record SimpleNativeType(NamespaceName namespaceName, String name) implements ConstructibleType, NativeType { +public record SimpleNativeType(NamespaceName namespaceName, String name) implements ConstructibleType, NativeType, SimpleConcreteType { @Override public String describe() { return namespaceName.toString() + "." + name; diff --git a/src/main/java/org/shedlang/compiler/types/SimpleStructType.java b/src/main/java/org/shedlang/compiler/types/SimpleStructType.java index a43afb80..d781e537 100644 --- a/src/main/java/org/shedlang/compiler/types/SimpleStructType.java +++ b/src/main/java/org/shedlang/compiler/types/SimpleStructType.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.types; /** * A set of named fields. */ -public record SimpleStructType(NamespaceName namespaceName, String name) implements ConstructibleType, StructType { +public record SimpleStructType(NamespaceName namespaceName, String name) implements ConstructibleType, SimpleConcreteType, StructType { @Override public String describe() { return namespaceName.toString() + "." + name; diff --git a/src/main/java/org/shedlang/compiler/types/Type.java b/src/main/java/org/shedlang/compiler/types/Type.java index a05304c8..970a4023 100644 --- a/src/main/java/org/shedlang/compiler/types/Type.java +++ b/src/main/java/org/shedlang/compiler/types/Type.java @@ -6,5 +6,5 @@ package org.shedlang.compiler.types; * found through auxiliary data. */ public sealed interface Type extends TypeLevelValue - permits BoolType, BottomType, ConstructedType, ConstructibleType, FunctionType, IntType, NamespaceType, NativeType, StringType, StructType, SumType, TopType, TupleType, TypeArg, TypeLevelValueType, TypeParam, UnitType { + permits BoolType, BottomType, ConstructedType, ConstructibleType, FunctionType, IntType, NamespaceType, NativeType, SimpleConcreteType, StringType, StructType, SumType, TopType, TupleType, TypeArg, TypeLevelValueType, TypeParam, UnitType { } -- cgit v1.2.3