Skip to content

Commit

Permalink
Merge pull request #4 from nasa/refactor-issue-349
Browse files Browse the repository at this point in the history
Refactor topology code gen
  • Loading branch information
jwest115 authored Aug 1, 2024
2 parents 0a8cd3d + d368418 commit 732f04c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,45 @@ case class TopComponentInstances(
aNode: Ast.Annotated[AstNode[Ast.DefTopology]]
) extends TopologyCppWriterUtils(s, aNode) {

private val bannerComment = "Component instances"
def getMembers: List[CppDoc.Member] = {
val instanceMembers = getInstanceMembers
List.concat(
guardedList (!instanceMembers.isEmpty) (List(getCommentMember)),
instanceMembers
)
}

def getHppLines: List[Line] = addBannerComment(
bannerComment,
getDeclLines
)
private val bannerComment = "Component instances"

def getCppLines: List[Line] = addBannerComment(
bannerComment,
getDefLines
private def getCommentMember = linesMember(
CppDocWriter.writeBannerComment(bannerComment),
CppDoc.Lines.Both
)

private def getDeclLines = {
def getCode(ci: ComponentInstance): List[Line] = {
val implType = getImplType(ci)
val instanceName = ci.getUnqualifiedName
val instLines = lines(
s"""|//! $instanceName
|extern $implType $instanceName;"""
)
wrapInNamespaceLines(ci.qualifiedName.qualifier, instLines)
}
flattenWithBlankPrefix(instances.map(getCode))
}

private def getDefLines = {
def getCode(ci: ComponentInstance): List[Line] = {
private def getInstanceMembers = {
def getMembers(ci: ComponentInstance): List[CppDoc.Member] = {
val implType = getImplType(ci)
val instanceName = ci.getUnqualifiedName
val instLines = getCodeLinesForPhase (CppWriter.Phases.instances) (ci).getOrElse(
val hppMember = linesMember(
lines(
s"$implType $instanceName(FW_OPTIONAL_NAME($q$instanceName$q));"
)
s"""|
|//! $instanceName
|extern $implType $instanceName;"""
),
CppDoc.Lines.Hpp
)
wrapInNamespaceLines(ci.qualifiedName.qualifier, instLines)
val cppMember = {
val instLines = getCodeLinesForPhase (CppWriter.Phases.instances) (ci).getOrElse(
lines(
s"""|
|$implType $instanceName(FW_OPTIONAL_NAME($q$instanceName$q));"""
)
)
linesMember(instLines, CppDoc.Lines.Cpp)
}
wrapInNamespaces(ci.qualifiedName.qualifier, List(hppMember, cppMember))
}
flattenWithBlankPrefix(instances.map(getCode))
instances.flatMap(getMembers)
}

private def getImplType(ci: ComponentInstance) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ case class TopologyCppWriter(
getTopologyMembers
)

private def getComponentInstanceMembers =
TopComponentInstances(s, aNode).getMembers

private def getIncludeMembers: List[CppDoc.Member] = {
val hpp = {
val strings = (
Expand All @@ -54,15 +57,6 @@ case class TopologyCppWriter(
List(hpp, cpp)
}

private def getComponentInstanceMembers: List[CppDoc.Member] = {
val hpp = linesMember(TopComponentInstances(s, aNode).getHppLines)
val cpp = linesMember(
TopComponentInstances(s, aNode).getCppLines,
CppDoc.Lines.Cpp
)
List(hpp, cpp)
}

private def getTopologyMembers: List[CppDoc.Member] = {
val hppLines = linesMember(
TopConstants(s, aNode).getLines ++
Expand Down
9 changes: 9 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/BasicTopologyAc.ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@
// ----------------------------------------------------------------------

namespace M {

Active active1(FW_OPTIONAL_NAME("active1"));

}

namespace M {
Active active2;

}

namespace M {

Active active3(FW_OPTIONAL_NAME("active3"));

}

namespace M {

Passive passive1(FW_OPTIONAL_NAME("passive1"));

}

namespace M {

ConcretePassive passive2(FW_OPTIONAL_NAME("passive2"));

}

namespace M {
Expand Down
10 changes: 10 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/BasicTopologyAc.ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,38 @@
// ----------------------------------------------------------------------

namespace M {

//! active1
extern Active active1;

}

namespace M {

//! active2
extern Active active2;

}

namespace M {

//! active3
extern Active active3;

}

namespace M {

//! passive1
extern Passive passive1;

}

namespace M {

//! passive2
extern ConcretePassive passive2;

}

namespace M {
Expand Down
4 changes: 4 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/CommandsTopologyAc.ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
// ----------------------------------------------------------------------

namespace M {

C c1(FW_OPTIONAL_NAME("c1"));

}

namespace M {

C c2(FW_OPTIONAL_NAME("c2"));

}

namespace M {
Expand Down
4 changes: 4 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/CommandsTopologyAc.ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
// ----------------------------------------------------------------------

namespace M {

//! c1
extern C c1;

}

namespace M {

//! c2
extern C c2;

}

namespace M {
Expand Down
6 changes: 6 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/HealthTopologyAc.ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@
// ----------------------------------------------------------------------

namespace M {

C c1(FW_OPTIONAL_NAME("c1"));

}

namespace M {

C c2(FW_OPTIONAL_NAME("c2"));

}

namespace M {

Svc::Health health(FW_OPTIONAL_NAME("health"));

}

namespace M {
Expand Down
6 changes: 6 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/HealthTopologyAc.ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@
// ----------------------------------------------------------------------

namespace M {

//! c1
extern C c1;

}

namespace M {

//! c2
extern C c2;

}

namespace M {

//! health
extern Svc::Health health;

}

namespace M {
Expand Down
4 changes: 4 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/ParamsTopologyAc.ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
// ----------------------------------------------------------------------

namespace M {

C c1(FW_OPTIONAL_NAME("c1"));

}

namespace M {

C c2(FW_OPTIONAL_NAME("c2"));

}

namespace M {
Expand Down
4 changes: 4 additions & 0 deletions compiler/tools/fpp-to-cpp/test/top/ParamsTopologyAc.ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
// ----------------------------------------------------------------------

namespace M {

//! c1
extern C c1;

}

namespace M {

//! c2
extern C c2;

}

namespace M {
Expand Down

0 comments on commit 732f04c

Please sign in to comment.