From b6573783aa27ff764a4a94a4a539da2ec57e6137 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 29 Nov 2024 20:32:25 -0500 Subject: [PATCH] Fix returning enum structs not working. Bug: issue #1012 Test: new test case --- compiler/code-generator.cpp | 2 +- tests/enum-structs/return-enum-struct.out | 20 ++++++++++ tests/enum-structs/return-enum-struct.sp | 45 +++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/enum-structs/return-enum-struct.out create mode 100644 tests/enum-structs/return-enum-struct.sp diff --git a/compiler/code-generator.cpp b/compiler/code-generator.cpp index 7fba2d3a..e5f042fe 100644 --- a/compiler/code-generator.cpp +++ b/compiler/code-generator.cpp @@ -1403,7 +1403,7 @@ CodeGenerator::EmitReturnStmt(ReturnStmt* stmt) EmitExpr(stmt->expr()); const auto& v = stmt->expr()->val(); - if (v.type()->isArray()) + if (v.type()->isArray() || v.type()->isEnumStruct()) EmitReturnArrayStmt(stmt); } else { /* this return statement contains no expression */ diff --git a/tests/enum-structs/return-enum-struct.out b/tests/enum-structs/return-enum-struct.out new file mode 100644 index 00000000..30f666e5 --- /dev/null +++ b/tests/enum-structs/return-enum-struct.out @@ -0,0 +1,20 @@ +First Struct +1 +1.000000 +Hello +1 +1.000000 +Hello +1 +1.000000 +Hello +New Struct Below +1 +1.000000 +Hello +1 +1.000000 +Hello +1 +1.000000 +Hello diff --git a/tests/enum-structs/return-enum-struct.sp b/tests/enum-structs/return-enum-struct.sp new file mode 100644 index 00000000..dd2633cf --- /dev/null +++ b/tests/enum-structs/return-enum-struct.sp @@ -0,0 +1,45 @@ +#include + +enum struct MyStruct +{ + int v; + float f; + char sz[8]; +} + +MyStruct CreateStruct() +{ + MyStruct st; + st.v = 1; + st.f = 1.0; + st.sz = "Hello"; + return st; +} + +void PrintStruct(MyStruct st) +{ + printnums(st.v); + printfloat(st.f); + print(st.sz); + print("\n"); +} + +public void main() +{ + print("First Struct\n"); + MyStruct st; + st = CreateStruct(); + PrintStruct(st); + st = CreateStruct(); + PrintStruct(st); + st = CreateStruct(); + PrintStruct(st); + print("New Struct Below\n"); + MyStruct st2; + st2 = CreateStruct(); + PrintStruct(st2); + st2 = CreateStruct(); + PrintStruct(st2); + st2 = CreateStruct(); + PrintStruct(st2); +}