aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-26 18:35:46 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-26 18:35:46 +0000
commit538d0c883cb5afaeef7a58a66a876b7c2cc80708 (patch)
tree2072c923999378dd117c12f64ce670ef40cfc0ab /src
parente0f232e9ad7fbe304e99bb4e880c761f06255791 (diff)
Support constructed type field accesses in Shid code gen
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java27
-rw-r--r--src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioExampleTests.java2
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioExampleTests.java2
-rw-r--r--src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java30
4 files changed, 49 insertions, 12 deletions
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
index 8c42982b..501e12e5 100644
--- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
+++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java
@@ -190,7 +190,22 @@ public class ShidCodeGen {
) {
context = compileExpression(node.receiver(), context);
// TODO: handle not a type with fields
- var receiver = switch (node.receiver().type()) {
+ var receiverType = node.receiver().type();
+ var receiver = receiverTypeToReceiver(receiverType);
+
+ context = context.addInstruction(new ShidFieldLoad(
+ receiver,
+ node.fieldName(),
+ node.source()
+ ));
+ return context;
+ }
+
+ private static ShidFieldLoad.Receiver receiverTypeToReceiver(Type receiverType) {
+ return switch (receiverType) {
+ case ConstructedType constructedType ->
+ receiverTypeToReceiver(constructedType.genericType());
+
case NamespaceType namespaceType ->
new ShidFieldLoad.Receiver.Namespace(namespaceType.namespaceName());
@@ -205,16 +220,8 @@ public class ShidCodeGen {
}
}
- default ->
- throw new RuntimeException("not a type with fields");
+ default -> throw new RuntimeException("not a type with fields");
};
-
- context = context.addInstruction(new ShidFieldLoad(
- receiver,
- node.fieldName(),
- node.source()
- ));
- return context;
}
private static Context compileFunction(
diff --git a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioExampleTests.java b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioExampleTests.java
index 193f7f74..ab8b19be 100644
--- a/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioExampleTests.java
+++ b/src/test/java/org/shedlang/compiler/backends/llvm/LlvmPatioExampleTests.java
@@ -23,6 +23,6 @@ public class LlvmPatioExampleTests extends ExampleTests {
@Override
protected boolean isDisabled(String testName) {
- return testName.equals("29-generic-structs");
+ return false;
}
}
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioExampleTests.java
index dfecd8bb..95585ee4 100644
--- a/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioExampleTests.java
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/WasmPatioExampleTests.java
@@ -23,6 +23,6 @@ public class WasmPatioExampleTests extends ExampleTests {
@Override
protected boolean isDisabled(String testName) {
- return testName.equals("29-generic-structs");
+ return false;
}
}
diff --git a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java
index 27559724..7dc159bd 100644
--- a/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java
+++ b/src/test/java/org/shedlang/compiler/shid/ShidCodeGenFieldAccessTests.java
@@ -6,8 +6,11 @@ import org.shedlang.compiler.shid.instructions.ShidFieldLoad;
import org.shedlang.compiler.shid.instructions.ShidMatchers;
import org.shedlang.compiler.types.Effects;
import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.TypeParam;
import org.shedlang.compiler.types.Types;
+import java.util.List;
+
import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidFieldLoad;
import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidLocalLoad;
import static org.zwobble.precisely.AssertThat.assertThat;
@@ -56,6 +59,33 @@ public class ShidCodeGenFieldAccessTests {
}
@Test
+ public void constructedTypeFieldAccessIsCompiledToFieldLoad() {
+ var structType = Types.struct(NamespaceName.of("Example", "Project"), "Box");
+ var typeConstructor = Types.typeConstructor(
+ List.of(TypeParam.forStructType(structType, "T")),
+ structType
+ );
+ var constructedType = Types.constructedType(
+ typeConstructor,
+ List.of(Types.STRING)
+ );
+ var node = Typed.fieldAccess(
+ Typed.reference("box", constructedType),
+ "value",
+ Types.STRING
+ );
+ var context = ShidCodeGen.Context.EMPTY
+ .addShedVal("box");
+
+ var result = ShidCodeGen.compileExpression(node, context);
+
+ assertThat(result.instructions(), isSequence(
+ isShidLocalLoad("box__0"),
+ isShidFieldLoad(structType, "value")
+ ));
+ }
+
+ @Test
public void userDefinedEffectFieldAccessIsCompiledToFieldLoad() {
var effect = Effects.userDefined(NamespaceName.of("Example", "Project"), "State");
var node = Typed.fieldAccess(