From 8cfdfc92fcf41f5db6d30015b20457ca64f0c3ce Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 13:39:40 +0000 Subject: Support method calls from other namespaces (without Wasm support) --- .../40-imported-struct-methods/shed-examples.json5 | 9 +++++ examples/40-imported-struct-methods/shed.json5 | 8 +++++ .../40-imported-struct-methods/src/Counting.shed | 11 ++++++ .../40-imported-struct-methods/src/HelloWorld.shed | 7 ++++ .../shed-examples.json5 | 9 ----- examples/40-struct-method-references/shed.json5 | 8 ----- .../src/HelloWorld.shed | 25 ------------- .../41-generic-struct-methods/shed-examples.json5 | 9 ----- examples/41-generic-struct-methods/shed.json5 | 8 ----- .../41-generic-struct-methods/src/HelloWorld.shed | 11 ------ .../shed-examples.json5 | 9 +++++ examples/41-struct-method-references/shed.json5 | 8 +++++ .../src/HelloWorld.shed | 25 +++++++++++++ .../shed-examples.json5 | 9 ----- examples/42-generic-struct-methods-this/shed.json5 | 8 ----- .../src/HelloWorld.shed | 15 -------- .../42-generic-struct-methods/shed-examples.json5 | 9 +++++ examples/42-generic-struct-methods/shed.json5 | 8 +++++ .../42-generic-struct-methods/src/HelloWorld.shed | 11 ++++++ .../shed-examples.json5 | 9 +++++ examples/43-generic-struct-methods-this/shed.json5 | 8 +++++ .../src/HelloWorld.shed | 15 ++++++++ examples/43-native-code/shed-examples.json5 | 9 ----- examples/43-native-code/shed.json5 | 11 ------ examples/43-native-code/src/False.shed | 5 --- examples/44-native-code/shed-examples.json5 | 9 +++++ examples/44-native-code/shed.json5 | 11 ++++++ examples/44-native-code/src/False.shed | 5 +++ ...mpiler.testing.snapshots.Snapshotter)].expected | 21 ----------- ...mpiler.testing.snapshots.Snapshotter)].expected | 22 ++++++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 21 +++++++++++ ...mpiler.testing.snapshots.Snapshotter)].expected | 2 +- .../shedlang/compiler/backends/PatioBackends.java | 20 ++++++++--- .../shedlang/compiler/shidtopatio/ShidToPatio.java | 2 +- .../compiler/shidtopatio/ShidToPatioContext.java | 41 +++++++++++++++------- .../compiler/shidtopatio/ShidToPatioMethods.java | 13 ++++++- .../compiler/backends/wasm/Wasm32ExampleTests.java | 1 + .../compiler/backends/wasm/Wasm64ExampleTests.java | 1 + .../shidtopatio/ShidToPatioDefineEffectTests.java | 2 +- .../shidtopatio/ShidToPatioDefineStructTests.java | 6 ++-- .../shidtopatio/ShidToPatioFieldLoadTests.java | 6 ++-- .../shidtopatio/ShidToPatioMethodLoadTests.java | 22 ++++++++++-- .../shidtopatio/ShidToPatioMethodStoreTests.java | 2 +- .../ShidToPatioNamespaceFieldStoreTests.java | 2 +- .../shidtopatio/ShidToPatioNamespaceTests.java | 2 +- .../shidtopatio/ShidToPatioSwitchTests.java | 2 +- .../compiler/shidtopatio/ShidToPatioTesting.java | 8 +++++ .../compiler/shidtopatio/ShidToPatioTryTests.java | 4 +-- 48 files changed, 306 insertions(+), 183 deletions(-) create mode 100644 examples/40-imported-struct-methods/shed-examples.json5 create mode 100644 examples/40-imported-struct-methods/shed.json5 create mode 100644 examples/40-imported-struct-methods/src/Counting.shed create mode 100644 examples/40-imported-struct-methods/src/HelloWorld.shed delete mode 100644 examples/40-struct-method-references/shed-examples.json5 delete mode 100644 examples/40-struct-method-references/shed.json5 delete mode 100644 examples/40-struct-method-references/src/HelloWorld.shed delete mode 100644 examples/41-generic-struct-methods/shed-examples.json5 delete mode 100644 examples/41-generic-struct-methods/shed.json5 delete mode 100644 examples/41-generic-struct-methods/src/HelloWorld.shed create mode 100644 examples/41-struct-method-references/shed-examples.json5 create mode 100644 examples/41-struct-method-references/shed.json5 create mode 100644 examples/41-struct-method-references/src/HelloWorld.shed delete mode 100644 examples/42-generic-struct-methods-this/shed-examples.json5 delete mode 100644 examples/42-generic-struct-methods-this/shed.json5 delete mode 100644 examples/42-generic-struct-methods-this/src/HelloWorld.shed create mode 100644 examples/42-generic-struct-methods/shed-examples.json5 create mode 100644 examples/42-generic-struct-methods/shed.json5 create mode 100644 examples/42-generic-struct-methods/src/HelloWorld.shed create mode 100644 examples/43-generic-struct-methods-this/shed-examples.json5 create mode 100644 examples/43-generic-struct-methods-this/shed.json5 create mode 100644 examples/43-generic-struct-methods-this/src/HelloWorld.shed delete mode 100644 examples/43-native-code/shed-examples.json5 delete mode 100644 examples/43-native-code/shed.json5 delete mode 100644 examples/43-native-code/src/False.shed create mode 100644 examples/44-native-code/shed-examples.json5 create mode 100644 examples/44-native-code/shed.json5 create mode 100644 examples/44-native-code/src/False.shed delete mode 100644 snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected create mode 100644 snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInDifferentNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected create mode 100644 snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInSameNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected diff --git a/examples/40-imported-struct-methods/shed-examples.json5 b/examples/40-imported-struct-methods/shed-examples.json5 new file mode 100644 index 00000000..95fb0a97 --- /dev/null +++ b/examples/40-imported-struct-methods/shed-examples.json5 @@ -0,0 +1,9 @@ +{ + examples: [ + { + project: ".", + bin: "hello-world", + expectedOutput: "one\ntwo\nmany", + }, + ], +} diff --git a/examples/40-imported-struct-methods/shed.json5 b/examples/40-imported-struct-methods/shed.json5 new file mode 100644 index 00000000..a6c8e563 --- /dev/null +++ b/examples/40-imported-struct-methods/shed.json5 @@ -0,0 +1,8 @@ +{ + bins: [ + { + name: "hello-world", + mainNamespace: "HelloWorld", + }, + ], +} diff --git a/examples/40-imported-struct-methods/src/Counting.shed b/examples/40-imported-struct-methods/src/Counting.shed new file mode 100644 index 00000000..ac97df14 --- /dev/null +++ b/examples/40-imported-struct-methods/src/Counting.shed @@ -0,0 +1,11 @@ +pub struct Count(value: Int) { + fun message(this) -> String { + if (this.value == 1) { + "one\n" + } else if (this.value == 2) { + "two\n" + } else { + "many\n" + } + } +} diff --git a/examples/40-imported-struct-methods/src/HelloWorld.shed b/examples/40-imported-struct-methods/src/HelloWorld.shed new file mode 100644 index 00000000..6d9de22c --- /dev/null +++ b/examples/40-imported-struct-methods/src/HelloWorld.shed @@ -0,0 +1,7 @@ +import Counting; + +pub fun main() ! Io -> Unit { + print!(Counting.Count(1).message()); + print!(Counting.Count(2).message()); + print!(Counting.Count(3).message()); +} diff --git a/examples/40-struct-method-references/shed-examples.json5 b/examples/40-struct-method-references/shed-examples.json5 deleted file mode 100644 index a57c4749..00000000 --- a/examples/40-struct-method-references/shed-examples.json5 +++ /dev/null @@ -1,9 +0,0 @@ -{ - examples: [ - { - project: ".", - bin: "hello-world", - expectedOutput: "one apple\ntwo bananas\nmany coconuts!", - }, - ], -} diff --git a/examples/40-struct-method-references/shed.json5 b/examples/40-struct-method-references/shed.json5 deleted file mode 100644 index a6c8e563..00000000 --- a/examples/40-struct-method-references/shed.json5 +++ /dev/null @@ -1,8 +0,0 @@ -{ - bins: [ - { - name: "hello-world", - mainNamespace: "HelloWorld", - }, - ], -} diff --git a/examples/40-struct-method-references/src/HelloWorld.shed b/examples/40-struct-method-references/src/HelloWorld.shed deleted file mode 100644 index 5bfd3631..00000000 --- a/examples/40-struct-method-references/src/HelloWorld.shed +++ /dev/null @@ -1,25 +0,0 @@ -pub fun main() ! Io -> Unit { - val one = Count(1).message; - val two = Count(2).message; - val three = Count(3).message; - - print!(one("apple", .exclaim = false)); - print!(two("bananas", .exclaim = false)); - print!(three("coconuts", .exclaim = true)); -} - -struct Count(value: Int) { - fun message(this, noun: String, .exclaim: Bool) -> String { - val prefix = if (this.value == 1) { - "one" - } else if (this.value == 2) { - "two" - } else { - "many" - }; - - val exclamation = if (exclaim) { "!" } else { "" }; - - prefix + " " + noun + exclamation + "\n" - } -} diff --git a/examples/41-generic-struct-methods/shed-examples.json5 b/examples/41-generic-struct-methods/shed-examples.json5 deleted file mode 100644 index 531c665f..00000000 --- a/examples/41-generic-struct-methods/shed-examples.json5 +++ /dev/null @@ -1,9 +0,0 @@ -{ - examples: [ - { - project: ".", - bin: "hello-world", - expectedOutput: "hello, world", - }, - ], -} diff --git a/examples/41-generic-struct-methods/shed.json5 b/examples/41-generic-struct-methods/shed.json5 deleted file mode 100644 index a6c8e563..00000000 --- a/examples/41-generic-struct-methods/shed.json5 +++ /dev/null @@ -1,8 +0,0 @@ -{ - bins: [ - { - name: "hello-world", - mainNamespace: "HelloWorld", - }, - ], -} diff --git a/examples/41-generic-struct-methods/src/HelloWorld.shed b/examples/41-generic-struct-methods/src/HelloWorld.shed deleted file mode 100644 index a4e808e1..00000000 --- a/examples/41-generic-struct-methods/src/HelloWorld.shed +++ /dev/null @@ -1,11 +0,0 @@ -pub fun main() ! Io -> Unit { - val messageBox = Box("hello, world"); - - print!(messageBox.get()); -} - -struct Box[T](value: T) { - fun get(this) -> T { - this.value - } -} diff --git a/examples/41-struct-method-references/shed-examples.json5 b/examples/41-struct-method-references/shed-examples.json5 new file mode 100644 index 00000000..a57c4749 --- /dev/null +++ b/examples/41-struct-method-references/shed-examples.json5 @@ -0,0 +1,9 @@ +{ + examples: [ + { + project: ".", + bin: "hello-world", + expectedOutput: "one apple\ntwo bananas\nmany coconuts!", + }, + ], +} diff --git a/examples/41-struct-method-references/shed.json5 b/examples/41-struct-method-references/shed.json5 new file mode 100644 index 00000000..a6c8e563 --- /dev/null +++ b/examples/41-struct-method-references/shed.json5 @@ -0,0 +1,8 @@ +{ + bins: [ + { + name: "hello-world", + mainNamespace: "HelloWorld", + }, + ], +} diff --git a/examples/41-struct-method-references/src/HelloWorld.shed b/examples/41-struct-method-references/src/HelloWorld.shed new file mode 100644 index 00000000..5bfd3631 --- /dev/null +++ b/examples/41-struct-method-references/src/HelloWorld.shed @@ -0,0 +1,25 @@ +pub fun main() ! Io -> Unit { + val one = Count(1).message; + val two = Count(2).message; + val three = Count(3).message; + + print!(one("apple", .exclaim = false)); + print!(two("bananas", .exclaim = false)); + print!(three("coconuts", .exclaim = true)); +} + +struct Count(value: Int) { + fun message(this, noun: String, .exclaim: Bool) -> String { + val prefix = if (this.value == 1) { + "one" + } else if (this.value == 2) { + "two" + } else { + "many" + }; + + val exclamation = if (exclaim) { "!" } else { "" }; + + prefix + " " + noun + exclamation + "\n" + } +} diff --git a/examples/42-generic-struct-methods-this/shed-examples.json5 b/examples/42-generic-struct-methods-this/shed-examples.json5 deleted file mode 100644 index 531c665f..00000000 --- a/examples/42-generic-struct-methods-this/shed-examples.json5 +++ /dev/null @@ -1,9 +0,0 @@ -{ - examples: [ - { - project: ".", - bin: "hello-world", - expectedOutput: "hello, world", - }, - ], -} diff --git a/examples/42-generic-struct-methods-this/shed.json5 b/examples/42-generic-struct-methods-this/shed.json5 deleted file mode 100644 index a6c8e563..00000000 --- a/examples/42-generic-struct-methods-this/shed.json5 +++ /dev/null @@ -1,8 +0,0 @@ -{ - bins: [ - { - name: "hello-world", - mainNamespace: "HelloWorld", - }, - ], -} diff --git a/examples/42-generic-struct-methods-this/src/HelloWorld.shed b/examples/42-generic-struct-methods-this/src/HelloWorld.shed deleted file mode 100644 index b114d85a..00000000 --- a/examples/42-generic-struct-methods-this/src/HelloWorld.shed +++ /dev/null @@ -1,15 +0,0 @@ -pub fun main() ! Io -> Unit { - val messageBox = Box("hello, world"); - - print!(messageBox.me().get()); -} - -struct Box[T](value: T) { - fun me(this) -> Box[T] { - this - } - - fun get(this) -> T { - this.value - } -} diff --git a/examples/42-generic-struct-methods/shed-examples.json5 b/examples/42-generic-struct-methods/shed-examples.json5 new file mode 100644 index 00000000..531c665f --- /dev/null +++ b/examples/42-generic-struct-methods/shed-examples.json5 @@ -0,0 +1,9 @@ +{ + examples: [ + { + project: ".", + bin: "hello-world", + expectedOutput: "hello, world", + }, + ], +} diff --git a/examples/42-generic-struct-methods/shed.json5 b/examples/42-generic-struct-methods/shed.json5 new file mode 100644 index 00000000..a6c8e563 --- /dev/null +++ b/examples/42-generic-struct-methods/shed.json5 @@ -0,0 +1,8 @@ +{ + bins: [ + { + name: "hello-world", + mainNamespace: "HelloWorld", + }, + ], +} diff --git a/examples/42-generic-struct-methods/src/HelloWorld.shed b/examples/42-generic-struct-methods/src/HelloWorld.shed new file mode 100644 index 00000000..a4e808e1 --- /dev/null +++ b/examples/42-generic-struct-methods/src/HelloWorld.shed @@ -0,0 +1,11 @@ +pub fun main() ! Io -> Unit { + val messageBox = Box("hello, world"); + + print!(messageBox.get()); +} + +struct Box[T](value: T) { + fun get(this) -> T { + this.value + } +} diff --git a/examples/43-generic-struct-methods-this/shed-examples.json5 b/examples/43-generic-struct-methods-this/shed-examples.json5 new file mode 100644 index 00000000..531c665f --- /dev/null +++ b/examples/43-generic-struct-methods-this/shed-examples.json5 @@ -0,0 +1,9 @@ +{ + examples: [ + { + project: ".", + bin: "hello-world", + expectedOutput: "hello, world", + }, + ], +} diff --git a/examples/43-generic-struct-methods-this/shed.json5 b/examples/43-generic-struct-methods-this/shed.json5 new file mode 100644 index 00000000..a6c8e563 --- /dev/null +++ b/examples/43-generic-struct-methods-this/shed.json5 @@ -0,0 +1,8 @@ +{ + bins: [ + { + name: "hello-world", + mainNamespace: "HelloWorld", + }, + ], +} diff --git a/examples/43-generic-struct-methods-this/src/HelloWorld.shed b/examples/43-generic-struct-methods-this/src/HelloWorld.shed new file mode 100644 index 00000000..b114d85a --- /dev/null +++ b/examples/43-generic-struct-methods-this/src/HelloWorld.shed @@ -0,0 +1,15 @@ +pub fun main() ! Io -> Unit { + val messageBox = Box("hello, world"); + + print!(messageBox.me().get()); +} + +struct Box[T](value: T) { + fun me(this) -> Box[T] { + this + } + + fun get(this) -> T { + this.value + } +} diff --git a/examples/43-native-code/shed-examples.json5 b/examples/43-native-code/shed-examples.json5 deleted file mode 100644 index 1b45c0b5..00000000 --- a/examples/43-native-code/shed-examples.json5 +++ /dev/null @@ -1,9 +0,0 @@ -{ - examples: [ - { - project: ".", - bin: "false", - expectedExitCode: 42, - }, - ], -} diff --git a/examples/43-native-code/shed.json5 b/examples/43-native-code/shed.json5 deleted file mode 100644 index b07dcaed..00000000 --- a/examples/43-native-code/shed.json5 +++ /dev/null @@ -1,11 +0,0 @@ -{ - bins: [ - { - name: "false", - mainNamespace: "False", - }, - ], - dependencies: [ - {path: "../../projects/std"}, - ], -} diff --git a/examples/43-native-code/src/False.shed b/examples/43-native-code/src/False.shed deleted file mode 100644 index 75dc7a89..00000000 --- a/examples/43-native-code/src/False.shed +++ /dev/null @@ -1,5 +0,0 @@ -import Std/Process; - -pub fun main() ! Io -> Unit { - Process.exit!(42); -} diff --git a/examples/44-native-code/shed-examples.json5 b/examples/44-native-code/shed-examples.json5 new file mode 100644 index 00000000..1b45c0b5 --- /dev/null +++ b/examples/44-native-code/shed-examples.json5 @@ -0,0 +1,9 @@ +{ + examples: [ + { + project: ".", + bin: "false", + expectedExitCode: 42, + }, + ], +} diff --git a/examples/44-native-code/shed.json5 b/examples/44-native-code/shed.json5 new file mode 100644 index 00000000..b07dcaed --- /dev/null +++ b/examples/44-native-code/shed.json5 @@ -0,0 +1,11 @@ +{ + bins: [ + { + name: "false", + mainNamespace: "False", + }, + ], + dependencies: [ + {path: "../../projects/std"}, + ], +} diff --git a/examples/44-native-code/src/False.shed b/examples/44-native-code/src/False.shed new file mode 100644 index 00000000..75dc7a89 --- /dev/null +++ b/examples/44-native-code/src/False.shed @@ -0,0 +1,5 @@ +import Std/Process; + +pub fun main() ! Io -> Unit { + Process.exit!(42); +} diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected deleted file mode 100644 index 7b048f63..00000000 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoad(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ /dev/null @@ -1,21 +0,0 @@ -Compilation unit elements: - - -Locals: - * (val "tmp__name__0" int) - -Instructions: - -( - ( - memory-load - (local "tmp__name__0") - pointer=(data-ptr-const "shed__method__Example_Project__User__name") - layout=int - offset=() - ) -) - -Temporary stack: - * PatioLocalRef[name=tmp__name__0] - * PatioLocalRef[name=a] diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInDifferentNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInDifferentNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..05f2af25 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInDifferentNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,22 @@ +Compilation unit elements: + +(data-mutable-decl "shed__method__Example_Project__User__name" int) + +Locals: + * (val "tmp__name__0" int) + +Instructions: + +( + ( + memory-load + (local "tmp__name__0") + pointer=(data-ptr-const "shed__method__Example_Project__User__name") + layout=int + offset=() + ) +) + +Temporary stack: + * PatioLocalRef[name=tmp__name__0] + * PatioLocalRef[name=a] diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInSameNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInSameNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected new file mode 100644 index 00000000..7b048f63 --- /dev/null +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodLoadTests]/[method:canGenerateCodeForMethodLoadInSameNamespace(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -0,0 +1,21 @@ +Compilation unit elements: + + +Locals: + * (val "tmp__name__0" int) + +Instructions: + +( + ( + memory-load + (local "tmp__name__0") + pointer=(data-ptr-const "shed__method__Example_Project__User__name") + layout=int + offset=() + ) +) + +Temporary stack: + * PatioLocalRef[name=tmp__name__0] + * PatioLocalRef[name=a] diff --git a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodStoreTests]/[method:canGenerateCodeForMethodStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodStoreTests]/[method:canGenerateCodeForMethodStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected index ff2b3924..8979075b 100644 --- a/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodStoreTests]/[method:canGenerateCodeForMethodStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected +++ b/snapshots/[engine:junit-jupiter]/[class:org.shedlang.compiler.shidtopatio.ShidToPatioMethodStoreTests]/[method:canGenerateCodeForMethodStore(org.shedlang.compiler.testing.snapshots.Snapshotter)].expected @@ -1,6 +1,6 @@ Compilation unit elements: -(data-mutable "shed__method__Example_Project__User__name" private (int 0)) +(data-mutable "shed__method__Example_Project__User__name" public (int 0)) Locals: diff --git a/src/main/java/org/shedlang/compiler/backends/PatioBackends.java b/src/main/java/org/shedlang/compiler/backends/PatioBackends.java index bc90c6b4..b8f93ca3 100644 --- a/src/main/java/org/shedlang/compiler/backends/PatioBackends.java +++ b/src/main/java/org/shedlang/compiler/backends/PatioBackends.java @@ -13,6 +13,7 @@ import org.shedlang.compiler.types.NamespaceName; import java.io.IOException; import java.util.List; +import java.util.Optional; public class PatioBackends { public interface PatioToTarget { @@ -36,30 +37,39 @@ public class PatioBackends { TypeInfos typeInfos, PatioToTarget patioToTarget ) throws IOException, InterruptedException { - var shidToPatioContext = ShidToPatioContext.forTypeInfos(typeInfos); + var shidToPatioGlobalContext = ShidToPatioContext.GlobalContext.forTypeInfos(typeInfos); for (var namespace : namespaces) { + var shidToPatioContext = ShidToPatioContext.initialForCompilationUnit( + Optional.of(namespace.namespaceName()), + shidToPatioGlobalContext + ); var patioCompilationUnitResult = ShidToPatio.generateNamespace( namespace, shidToPatioContext ); var patioCompilationUnit = patioCompilationUnitResult.value(); - shidToPatioContext = patioCompilationUnitResult.context(); + shidToPatioGlobalContext = patioCompilationUnitResult.context(); patioToTarget = patioToTarget.namespace(namespace.namespaceName(), patioCompilationUnit); } for (var bin : config.bins()) { - generateMain(profileConfig, bin, shidToPatioContext, patioToTarget); + generateMain(profileConfig, bin, shidToPatioGlobalContext, patioToTarget); } } private static void generateMain( ProfileConfig profileConfig, BinConfig binConfig, - ShidToPatioContext shidToPatioContext, + ShidToPatioContext.GlobalContext shidToPatioGlobalContext, PatioToTarget patioToTarget ) throws IOException, InterruptedException { + var shidToPatioContext = ShidToPatioContext.initialForCompilationUnit( + Optional.empty(), + shidToPatioGlobalContext + ); + // TODO: set appropriate source var source = Sources.NULL; @@ -78,7 +88,7 @@ public class PatioBackends { var patioCompilationUnitResult = shidToPatioContext.toCompilationUnit(source); var patioCompilationUnit = patioCompilationUnitResult.value(); - shidToPatioContext = patioCompilationUnitResult.context(); + shidToPatioGlobalContext = patioCompilationUnitResult.context(); patioToTarget.bin( binConfig.name(), diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java index a27168c4..97f5d4cd 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatio.java @@ -108,7 +108,7 @@ public class ShidToPatio { return context.toResult(mainFunctionName); } - public static ShidToPatioResult generateNamespace( + public static ShidToPatioContext.GlobalContext.Result generateNamespace( ShidNamespace shidNamespace, ShidToPatioContext context ) { diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java index ac8f0094..397ce996 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioContext.java @@ -172,14 +172,28 @@ public record ShidToPatioContext( } public record CompilationUnitContext( + Optional namespaceName, OrderedPMap elementsByName ) { - public static CompilationUnitContext EMPTY = new CompilationUnitContext(OrderedPMap.empty()); + public static CompilationUnitContext EMPTY = initial(Optional.empty()); + + public static CompilationUnitContext initial(Optional namespaceName) { + return new CompilationUnitContext( + namespaceName, + OrderedPMap.empty() + ); + } + + @CheckReturnValue + public Optional namespaceName() { + return this.namespaceName; + } @CheckReturnValue public CompilationUnitContext addElement(PatioCompilationUnitElement element) { // TODO: handle existing element with name. return new CompilationUnitContext( + this.namespaceName, this.elementsByName.plus(element.name(), element) ); } @@ -212,6 +226,7 @@ public record ShidToPatioContext( } return new CompilationUnitContext( + this.namespaceName, this.elementsByName.plus(element.name(), element) ); } @@ -224,6 +239,7 @@ public record ShidToPatioContext( if (existingElement == null) { return new CompilationUnitContext( + this.namespaceName, this.elementsByName.plus(element.name(), element) ); } else if (existingElement.equals(element)) { @@ -528,17 +544,13 @@ public record ShidToPatioContext( ); @CheckReturnValue - public static ShidToPatioContext forTypeInfos(TypeInfos typeInfos) { - return initialForCompilationUnit( - GlobalContext.forTypeInfos(typeInfos) - ); - } - - @CheckReturnValue - private static ShidToPatioContext initialForCompilationUnit(GlobalContext globalContext) { + public static ShidToPatioContext initialForCompilationUnit( + Optional namespaceName, + GlobalContext globalContext + ) { return new ShidToPatioContext( globalContext, - CompilationUnitContext.EMPTY, + CompilationUnitContext.initial(namespaceName), P.stack(), FunctionContext.EMPTY ); @@ -579,6 +591,11 @@ public record ShidToPatioContext( return this.globalContext.runtimeTypeTags.keySet(); } + @CheckReturnValue + public Optional namespaceName() { + return this.compilationUnitContext.namespaceName(); + } + @CheckReturnValue public ShidToPatioResult allocateCompilationUnitElementName( ShidToPatioCompilationUnitElementSpace space, @@ -957,8 +974,8 @@ public record ShidToPatioContext( } @CheckReturnValue - public ShidToPatioResult toCompilationUnit(Source source) { - return initialForCompilationUnit(this.globalContext).toResult(new PatioCompilationUnit( + public GlobalContext.Result toCompilationUnit(Source source) { + return this.globalContext.toResult(new PatioCompilationUnit( new ArrayList<>(this.compilationUnitContext.elementsByName.values()), source )); diff --git a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java index 96f3a83c..6dfaf6a4 100644 --- a/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java +++ b/src/main/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethods.java @@ -1,5 +1,6 @@ package org.shedlang.compiler.shidtopatio; +import org.shedlang.compiler.patio.PatioDataElementMutableDeclaration; import org.shedlang.compiler.patio.PatioDataElementMutableDefinition; import org.shedlang.compiler.patio.PatioVisibility; import org.shedlang.compiler.patio.instructions.PatioMemoryLoad; @@ -10,10 +11,12 @@ 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.sources.Sources; import org.shedlang.compiler.types.SimpleConcreteType; import java.text.MessageFormat; import java.util.List; +import java.util.Optional; import static org.shedlang.compiler.shidtopatio.ShidToPatioNamespaces.namespaceNameToPatioIdentifier; @@ -28,6 +31,14 @@ public class ShidToPatioMethods { Source source, ShidToPatioContext context ) { + if (!context.namespaceName().equals(Optional.of(type.namespaceName()))) { + context = context.ensureCompilationUnitElement(new PatioDataElementMutableDeclaration( + globalNameMethod(type, methodName), + PatioDataLayouts.fromType(ShidToPatioTypes.VALUE_TYPE), + Sources.NULL + )); + } + context = context.addInstruction(new PatioMemoryLoad( destination, globalOperandMethod(type, methodName), @@ -48,7 +59,7 @@ public class ShidToPatioMethods { ) { context = context.addCompilationUnitElement(new PatioDataElementMutableDefinition( globalNameMethod(type, methodName), - PatioVisibility.PRIVATE, + PatioVisibility.PUBLIC, PatioDataValues.int_(0), source )); diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java index 7b0ab151..f1dbee2c 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm32ExampleTests.java @@ -23,6 +23,7 @@ public class Wasm32ExampleTests extends ExampleTests { } private static final Set DISABLED = Set.of( + "40-imported-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java index 71d5f17b..0a4cfd7b 100644 --- a/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/wasm/Wasm64ExampleTests.java @@ -23,6 +23,7 @@ public class Wasm64ExampleTests extends ExampleTests { } private static final Set DISABLED = Set.of( + "40-imported-struct-methods" ); @Override diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java index 686f708c..f3ca07a0 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineEffectTests.java @@ -38,7 +38,7 @@ public class ShidToPatioDefineEffectTests { ) .build() ); - var context = ShidToPatioContext.forTypeInfos(typeInfos); + var context = ShidToPatioTesting.createContext(typeInfos); var result = ShidToPatio.generateInstruction(instruction, context); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java index b363cbb8..cb9268fb 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java @@ -38,7 +38,7 @@ public class ShidToPatioDefineStructTests { .withFieldsArePositional(false) .build() ); - var context = ShidToPatioContext.forTypeInfos(typeInfos); + var context = ShidToPatioTesting.createContext(typeInfos); var result = ShidToPatio.generateInstruction(instruction, context); @@ -64,7 +64,7 @@ public class ShidToPatioDefineStructTests { .withFieldsArePositional(true) .build() ); - var context = ShidToPatioContext.forTypeInfos(typeInfos); + var context = ShidToPatioTesting.createContext(typeInfos); var result = ShidToPatio.generateInstruction(instruction, context); @@ -83,7 +83,7 @@ public class ShidToPatioDefineStructTests { var typeInfos = TypeInfos.EMPTY .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); - var context = ShidToPatioContext.forTypeInfos(typeInfos); + var context = ShidToPatioTesting.createContext(typeInfos); context = ShidToPatio.generateInstruction(instruction1, context); context = ShidToPatio.generateInstruction(instruction2, context); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java index 3f5fd284..10644e07 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java @@ -31,7 +31,7 @@ public class ShidToPatioFieldLoadTests { .build() ) .toTypeInfos(); - var context = ShidToPatioContext.forTypeInfos(typeInfos) + var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("a")) .temporaryPush(new PatioLocalRef("b")); @@ -54,7 +54,7 @@ public class ShidToPatioFieldLoadTests { .addField("isActive", Types.BOOL) .build() ); - var context = ShidToPatioContext.forTypeInfos(typeInfos) + var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("a")) .temporaryPush(new PatioLocalRef("b")); @@ -77,7 +77,7 @@ public class ShidToPatioFieldLoadTests { .addMethod("read", FunctionType.builder().build()) .build() ); - var context = ShidToPatioContext.forTypeInfos(typeInfos) + var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("a")) .temporaryPush(new PatioLocalRef("b")); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java index 6fc4abc2..f7d24389 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodLoadTests.java @@ -11,18 +11,36 @@ import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import java.io.IOException; +import java.util.Optional; import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize; @ExtendWith({SnapshotResolver.class}) public class ShidToPatioMethodLoadTests { @Test - public void canGenerateCodeForMethodLoad(Snapshotter snapshotter) throws IOException { + public void canGenerateCodeForMethodLoadInSameNamespace(Snapshotter snapshotter) throws IOException { var namespaceName = NamespaceName.of("Example", "Project"); var structType = Types.struct(namespaceName, "User"); var shidInstruction = Shid.methodLoad(structType, "name"); var typeInfo = TypeCheckerContext.EMPTY.toTypeInfos(); - var context = ShidToPatioContext.forTypeInfos(typeInfo) + var globalContext = ShidToPatioContext.GlobalContext.forTypeInfos(typeInfo); + var context = ShidToPatioContext.initialForCompilationUnit(Optional.of(namespaceName), globalContext) + .temporaryPush(new PatioLocalRef("a")); + + var result = ShidToPatio.generateInstruction(shidInstruction, context); + + snapshotter.assertSnapshot(serialize(result)); + } + + @Test + public void canGenerateCodeForMethodLoadInDifferentNamespace(Snapshotter snapshotter) throws IOException { + var namespaceName = NamespaceName.of("Example", "Project"); + var otherNamespaceName = NamespaceName.of("Example", "Project2"); + var structType = Types.struct(namespaceName, "User"); + var shidInstruction = Shid.methodLoad(structType, "name"); + var typeInfo = TypeCheckerContext.EMPTY.toTypeInfos(); + var globalContext = ShidToPatioContext.GlobalContext.forTypeInfos(typeInfo); + var context = ShidToPatioContext.initialForCompilationUnit(Optional.of(otherNamespaceName), globalContext) .temporaryPush(new PatioLocalRef("a")); var result = ShidToPatio.generateInstruction(shidInstruction, context); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java index 447a925a..65386f61 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioMethodStoreTests.java @@ -22,7 +22,7 @@ public class ShidToPatioMethodStoreTests { var structType = Types.struct(namespaceName, "User"); var shidInstruction = Shid.methodStore(structType, "name"); var typeInfo = TypeCheckerContext.EMPTY.toTypeInfos(); - var context = ShidToPatioContext.forTypeInfos(typeInfo) + var context = ShidToPatioTesting.createContext(typeInfo) .temporaryPush(new PatioLocalRef("a")) .temporaryPush(new PatioLocalRef("b")); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java index c57f9572..436bd92c 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceFieldStoreTests.java @@ -32,7 +32,7 @@ public class ShidToPatioNamespaceFieldStoreTests { .build() ) .toTypeInfos(); - var context = ShidToPatioContext.forTypeInfos(typeInfo) + var context = ShidToPatioTesting.createContext(typeInfo) .temporaryPush(new PatioLocalRef("a")) .temporaryPush(new PatioLocalRef("b")); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java index 561a420f..67a97d96 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java @@ -45,7 +45,7 @@ public class ShidToPatioNamespaceTests { .addField("y", Types.STRING) .build() ); - var context = ShidToPatioContext.forTypeInfos(typeInfos); + var context = ShidToPatioTesting.createContext(typeInfos); var shidNamespace = new ShidNamespace(namespaceName, instructions, Sources.NULL); var result = ShidToPatio.generateNamespace(shidNamespace, context); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java index b22e7572..d40da05b 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioSwitchTests.java @@ -46,7 +46,7 @@ public class ShidToPatioSwitchTests { var typeInfos = TypeInfos.EMPTY .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); - var context = ShidToPatioContext.forTypeInfos(typeInfos) + var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("condition")); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java index 644371eb..c384d7c4 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTesting.java @@ -1,10 +1,18 @@ package org.shedlang.compiler.shidtopatio; import org.shedlang.compiler.patio.PatioSerializer; +import org.shedlang.compiler.typechecker.TypeInfos; + +import java.util.Optional; class ShidToPatioTesting { private ShidToPatioTesting() {} + static ShidToPatioContext createContext(TypeInfos typeInfos) { + var globalContext = ShidToPatioContext.GlobalContext.forTypeInfos(typeInfos); + return ShidToPatioContext.initialForCompilationUnit(Optional.empty(), globalContext); + } + static String serialize(ShidToPatioContext context) { var builder = new StringBuilder(); diff --git a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java index 5166449d..f7cfb141 100644 --- a/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java +++ b/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioTryTests.java @@ -37,7 +37,7 @@ public class ShidToPatioTryTests { .build() ) .toTypeInfos(); - var context = ShidToPatioContext.forTypeInfos(typeInfos) + var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("handlerA")) .temporaryPush(new PatioLocalRef("handlerC")) @@ -68,7 +68,7 @@ public class ShidToPatioTryTests { .build() ) .toTypeInfos(); - var context = ShidToPatioContext.forTypeInfos(typeInfos) + var context = ShidToPatioTesting.createContext(typeInfos) .stashGcRootStackIndex(new PatioLocalRef("gcRootStackIndex")) .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("handlerA")); -- cgit v1.2.3