Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for named and bodyless reactions in C++ #1933

Merged
merged 6 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public boolean supportsParameterizedWidths() {
* this target.
*/
public boolean supportsReactionDeclarations() {
if (this.equals(Target.C)) return true;
if (this.equals(Target.C) || this.equals(Target.CPP)) return true;
else return false;
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/main/kotlin/org/lflang/ast/AstExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,11 @@ val Resource.model: Model get() = this.allContents.asSequence().filterIsInstance

/** Get a label representing the receiving reaction.
*
* If the reaction is annotated with a label, then the label is returned. Otherwise, a reaction name
* is generated based on its priority.
* If the reaction is named, then the name is returned.
* If it is not named but annotated with a label, then the label is returned.
* Otherwise, a reaction name is generated based on its priority.
*/
val Reaction.label get(): String = AttributeUtils.getLabel(this) ?: "reaction_$priority"
val Reaction.label get(): String = name ?: AttributeUtils.getLabel(this) ?: "reaction_$priority"

/** Get the priority of a receiving reaction */
val Reaction.priority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import org.lflang.lf.WidthSpec

/** Get the "name" a reaction is represented with in target code.*/
val Reaction.codeName
get(): String = "r$indexInContainer"
get(): String = name ?: "r$indexInContainer"

/* **********************************************************************************************
* C++ specific extensions shared across classes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ import org.lflang.toText
/** A C++ code generator for reactions and their function bodies */
class CppReactionGenerator(
private val reactor: Reactor,
private val portGenerator: CppPortGenerator,
private val instanceGenerator: CppInstanceGenerator
private val portGenerator: CppPortGenerator
) {

private val reactionsWithDeadlines = reactor.reactions.filter { it.deadline != null }
Expand Down Expand Up @@ -149,8 +148,9 @@ class CppReactionGenerator(
}
}

private fun generateBodyDefinition(reaction: Reaction): String {
return with(PrependOperator) {
private fun generateBodyDefinition(reaction: Reaction): String? {
return if (reaction.code == null) null
else with(PrependOperator) {
"""
|// reaction ${reaction.label}
|${reactor.templateLine}
Expand Down Expand Up @@ -253,7 +253,7 @@ class CppReactionGenerator(

/** Get all definitions of reaction bodies. */
fun generateBodyDefinitions() =
reactor.reactions.joinToString(separator = "\n", postfix = "\n") { generateBodyDefinition(it) }
reactor.reactions.mapNotNull { generateBodyDefinition(it) }.joinToString(separator = "\n", postfix = "\n")

/** Get all declarations of deadline handlers. */
fun generateDeadlineHandlerDeclarations(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CppReactorGenerator(private val reactor: Reactor, fileConfig: CppFileConfi
private val timers = CppTimerGenerator(reactor)
private val actions = CppActionGenerator(reactor, messageReporter)
private val ports = CppPortGenerator(reactor)
private val reactions = CppReactionGenerator(reactor, ports, instances)
private val reactions = CppReactionGenerator(reactor, ports)
private val assemble = CppAssembleMethodGenerator(reactor)

private fun publicPreamble() =
Expand Down
7 changes: 7 additions & 0 deletions test/Cpp/src/HelloBodylessWorld.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
target Cpp {
cmake-include: hello_bodyless_world.cmake
}

main reactor {
reaction hello(startup)
}
5 changes: 5 additions & 0 deletions test/Cpp/src/hello_bodyless_world.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "HelloBodylessWorld/HelloBodylessWorld.hh"

void HelloBodylessWorld::Inner::hello_body([[maybe_unused]] const reactor::StartupTrigger& startup) {
cmnrd marked this conversation as resolved.
Show resolved Hide resolved
std::cout << "Hello World." << std::endl;
}
3 changes: 3 additions & 0 deletions test/Cpp/src/hello_bodyless_world.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target_sources(${LF_MAIN_TARGET} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/hello_bodyless_world.cc)