Skip to content

Commit

Permalink
Merge pull request #2164 from lf-lang/cpp-parameter-forwarding
Browse files Browse the repository at this point in the history
Fixed parameter forwarding for non-trivial types in C++
  • Loading branch information
cmnrd authored Jan 25, 2024
2 parents 9f1b6b1 + bcbaccc commit e2e36ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/kotlin/org/lflang/generator/cpp/CppTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
}

Expand Down
23 changes: 14 additions & 9 deletions test/Cpp/src/ParameterHierarchy.lf
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@
*/
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") {
// also test forwarding parameters via target code blocks
a = new Intermediate(p = {= p =}, s = {= s =})
}

main reactor ParameterHierarchy {
a = new Intermediate(p=42)
a = new Intermediate(p=42, s="bar")
}

0 comments on commit e2e36ea

Please sign in to comment.