From d57c29478ba4e234e5dc266e797912c603495fad Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Wed, 24 Jan 2024 17:07:21 -0800 Subject: [PATCH 1/3] cpp: fix parameter forwarding for non-trivial types --- .../org/lflang/generator/cpp/CppTypes.kt | 2 +- test/Cpp/src/ParameterHierarchy.lf | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/core/src/main/kotlin/org/lflang/generator/cpp/CppTypes.kt b/core/src/main/kotlin/org/lflang/generator/cpp/CppTypes.kt index 6a908392ab..c31366c784 100644 --- a/core/src/main/kotlin/org/lflang/generator/cpp/CppTypes.kt +++ b/core/src/main/kotlin/org/lflang/generator/cpp/CppTypes.kt @@ -51,7 +51,7 @@ object CppTypes : TargetTypes { } override fun getTargetParamRef(expr: ParameterReference, type: InferredType?): String { - return "__lf_parameters.${expr.parameter.name}" + return "__lf_inner.${expr.parameter.name}" } } diff --git a/test/Cpp/src/ParameterHierarchy.lf b/test/Cpp/src/ParameterHierarchy.lf index e2d1432d10..ffdc8e7ce4 100644 --- a/test/Cpp/src/ParameterHierarchy.lf +++ b/test/Cpp/src/ParameterHierarchy.lf @@ -7,25 +7,29 @@ */ target Cpp -reactor Deep(p: int = 0) { +reactor Deep(p: int = 0, s: std::string = "foo") { reaction(startup) {= if(p != 42) { - reactor::log::Error() << "Parameter value is: " << p << ". Should have been 42."; + reactor::log::Error() << "Parameter value p is: " << p << ". Should have been 42."; exit(1); - } else { - reactor::log::Info() << "Success."; } + if(s != "bar") { + reactor::log::Error() << "Parameter value s is: " << s << ". Should have been bar."; + exit(2); + } + + reactor::log::Info() << "Success."; =} } -reactor Intermediate(p: int = 10) { - a = new Deep(p=p) +reactor Intermediate(p: int = 10, s: std::string = "default") { + a = new Deep(p=p, s=s) } -reactor Another(p: int = 20) { - a = new Intermediate(p = {= p =}) // also test forwarding parameters via target code blocks +reactor Another(p: int = 20, s: std::string = "default") { + a = new Intermediate(p = {= p =}, s = {= s =}) // also test forwarding parameters via target code blocks } main reactor ParameterHierarchy { - a = new Intermediate(p=42) + a = new Intermediate(p=42, s="bar") } From e54830da1374acbb87080756f987f8af9feb94f9 Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Wed, 24 Jan 2024 17:49:47 -0800 Subject: [PATCH 2/3] ensure that __lf_inner is always defined --- .../main/kotlin/org/lflang/generator/cpp/CppReactorGenerator.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/kotlin/org/lflang/generator/cpp/CppReactorGenerator.kt b/core/src/main/kotlin/org/lflang/generator/cpp/CppReactorGenerator.kt index a7d8f0e4ae..e6cf197e06 100644 --- a/core/src/main/kotlin/org/lflang/generator/cpp/CppReactorGenerator.kt +++ b/core/src/main/kotlin/org/lflang/generator/cpp/CppReactorGenerator.kt @@ -94,6 +94,7 @@ class CppReactorGenerator(private val reactor: Reactor, fileConfig: CppFileConfi ${" | "..reactions.generateReactionViewForwardDeclarations()} | | class Inner: public lfutil::LFScope { + | const Inner& __lf_inner = *this; | const Parameters __lf_parameters; ${" | "..parameters.generateInnerAliasDeclarations()} ${" | "..state.generateDeclarations()} From bcbaccc5bf85dd65c707a7942efcc9435a6acd40 Mon Sep 17 00:00:00 2001 From: Christian Menard Date: Wed, 24 Jan 2024 18:12:57 -0800 Subject: [PATCH 3/3] formatting --- test/Cpp/src/ParameterHierarchy.lf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Cpp/src/ParameterHierarchy.lf b/test/Cpp/src/ParameterHierarchy.lf index ffdc8e7ce4..2789a13c87 100644 --- a/test/Cpp/src/ParameterHierarchy.lf +++ b/test/Cpp/src/ParameterHierarchy.lf @@ -27,7 +27,8 @@ reactor Intermediate(p: int = 10, s: std::string = "default") { } reactor Another(p: int = 20, s: std::string = "default") { - a = new Intermediate(p = {= p =}, s = {= s =}) // also test forwarding parameters via target code blocks + // also test forwarding parameters via target code blocks + a = new Intermediate(p = {= p =}, s = {= s =}) } main reactor ParameterHierarchy {