Skip to content

Commit 45ccdd1

Browse files
authored
Merge pull request #843 from nasa/issue-839-get-num-ports
Revise C++ code gen for getting port array sizes
2 parents 30543a6 + 1a211fe commit 45ccdd1

File tree

166 files changed

+7251
-17806
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+7251
-17806
lines changed

compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/ComponentCppWriterUtils.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,12 @@ abstract class ComponentCppWriterUtils(
956956

957957
object ComponentCppWriterUtils {
958958

959+
/** Whether code generation is internal or external to the component */
960+
enum InternalOrExternal {
961+
case Internal
962+
case External
963+
}
964+
959965
/** ( parameter name, parameter type name, parameter type ) **/
960966
type ParamTypeMapInfo = (String, String, Type)
961967
type CmdParamTypeMap = Map[Command.Opcode, List[ParamTypeMapInfo]]

compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/ComponentPorts.scala

Lines changed: 116 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -14,167 +14,145 @@ case class ComponentPorts(
1414

1515
private val outputPortWriter = ComponentOutputPorts(s, aNode)
1616

17-
def getConstantMembers: List[CppDoc.Class.Member] = {
18-
List(
19-
getConstants(specialInputPorts),
20-
getConstants(typedInputPorts),
21-
getConstants(serialInputPorts),
22-
getConstants(specialOutputPorts),
23-
getConstants(typedOutputPorts),
24-
getConstants(serialOutputPorts),
25-
).flatten
26-
}
27-
28-
def getPublicFunctionMembers: List[CppDoc.Class.Member] = {
29-
List(
30-
inputPortWriter.getGetters(specialInputPorts),
31-
inputPortWriter.getGetters(typedInputPorts),
32-
inputPortWriter.getGetters(serialInputPorts),
33-
outputPortWriter.getTypedConnectors(specialOutputPorts),
34-
outputPortWriter.getTypedConnectors(typedOutputPorts),
35-
outputPortWriter.getSerialConnectors(specialOutputPorts),
36-
outputPortWriter.getSerialConnectors(typedOutputPorts),
37-
outputPortWriter.getSerialConnectors(serialOutputPorts),
38-
).flatten
39-
}
40-
41-
def getProtectedFunctionMembers: List[CppDoc.Class.Member] = {
42-
List(
43-
getNumGetters(specialInputPorts),
44-
getNumGetters(typedInputPorts),
45-
getNumGetters(serialInputPorts),
46-
getNumGetters(specialOutputPorts),
47-
getNumGetters(typedOutputPorts),
48-
getNumGetters(serialOutputPorts),
49-
outputPortWriter.getConnectionStatusQueries(specialOutputPorts),
50-
outputPortWriter.getConnectionStatusQueries(typedOutputPorts),
51-
outputPortWriter.getConnectionStatusQueries(serialOutputPorts),
52-
inputPortWriter.getHandlerBases(dataProductInputPorts),
53-
inputPortWriter.getHandlers(typedInputPorts),
54-
inputPortWriter.getHandlerBases(typedInputPorts),
55-
inputPortWriter.getHandlers(serialInputPorts),
56-
inputPortWriter.getHandlerBases(serialInputPorts),
57-
inputPortWriter.getPreMsgHooks(dataProductAsyncInputPorts),
58-
inputPortWriter.getPreMsgHooks(typedAsyncInputPorts),
59-
inputPortWriter.getPreMsgHooks(serialAsyncInputPorts),
60-
inputPortWriter.getOverflowHooks(dataProductHookPorts),
61-
inputPortWriter.getOverflowHooks(typedHookPorts),
62-
inputPortWriter.getOverflowHooks(serialHookPorts),
63-
outputPortWriter.getInvokers(dataProductOutputPorts),
64-
outputPortWriter.getInvokers(typedOutputPorts),
65-
outputPortWriter.getInvokers(serialOutputPorts),
66-
).flatten
67-
}
68-
69-
def getPrivateFunctionMembers: List[CppDoc.Class.Member] = {
70-
List(
71-
inputPortWriter.getCallbacks(specialInputPorts),
72-
inputPortWriter.getCallbacks(typedInputPorts),
73-
inputPortWriter.getCallbacks(serialInputPorts)
74-
).flatten
75-
}
76-
77-
def getVariableMembers: List[CppDoc.Class.Member] = {
78-
List(
79-
getVariables(specialInputPorts),
80-
getVariables(typedInputPorts),
81-
getVariables(serialInputPorts),
82-
getVariables(specialOutputPorts),
83-
getVariables(typedOutputPorts),
84-
getVariables(serialOutputPorts),
85-
).flatten
86-
}
17+
def getConstantMembers: List[CppDoc.Class.Member] = List.concat(
18+
getConstants(specialInputPorts),
19+
getConstants(typedInputPorts),
20+
getConstants(serialInputPorts),
21+
getConstants(specialOutputPorts),
22+
getConstants(typedOutputPorts),
23+
getConstants(serialOutputPorts),
24+
)
25+
26+
def getPublicFunctionMembers: List[CppDoc.Class.Member] = List.concat(
27+
inputPortWriter.getGetters(specialInputPorts),
28+
inputPortWriter.getGetters(typedInputPorts),
29+
inputPortWriter.getGetters(serialInputPorts),
30+
outputPortWriter.getTypedConnectors(specialOutputPorts),
31+
outputPortWriter.getTypedConnectors(typedOutputPorts),
32+
outputPortWriter.getSerialConnectors(specialOutputPorts),
33+
outputPortWriter.getSerialConnectors(typedOutputPorts),
34+
outputPortWriter.getSerialConnectors(serialOutputPorts),
35+
)
36+
37+
def getProtectedFunctionMembers: List[CppDoc.Class.Member] = List.concat(
38+
getNumGetters(specialInputPorts),
39+
getNumGetters(typedInputPorts),
40+
getNumGetters(serialInputPorts),
41+
getNumGetters(specialOutputPorts),
42+
getNumGetters(typedOutputPorts),
43+
getNumGetters(serialOutputPorts),
44+
outputPortWriter.getConnectionStatusQueries(specialOutputPorts),
45+
outputPortWriter.getConnectionStatusQueries(typedOutputPorts),
46+
outputPortWriter.getConnectionStatusQueries(serialOutputPorts),
47+
inputPortWriter.getHandlerBases(dataProductInputPorts),
48+
inputPortWriter.getHandlers(typedInputPorts),
49+
inputPortWriter.getHandlerBases(typedInputPorts),
50+
inputPortWriter.getHandlers(serialInputPorts),
51+
inputPortWriter.getHandlerBases(serialInputPorts),
52+
inputPortWriter.getPreMsgHooks(dataProductAsyncInputPorts),
53+
inputPortWriter.getPreMsgHooks(typedAsyncInputPorts),
54+
inputPortWriter.getPreMsgHooks(serialAsyncInputPorts),
55+
inputPortWriter.getOverflowHooks(dataProductHookPorts),
56+
inputPortWriter.getOverflowHooks(typedHookPorts),
57+
inputPortWriter.getOverflowHooks(serialHookPorts),
58+
outputPortWriter.getInvokers(dataProductOutputPorts),
59+
outputPortWriter.getInvokers(typedOutputPorts),
60+
outputPortWriter.getInvokers(serialOutputPorts),
61+
)
62+
63+
def getPrivateFunctionMembers: List[CppDoc.Class.Member] = List.concat(
64+
inputPortWriter.getCallbacks(specialInputPorts),
65+
inputPortWriter.getCallbacks(typedInputPorts),
66+
inputPortWriter.getCallbacks(serialInputPorts)
67+
)
68+
69+
def getVariableMembers: List[CppDoc.Class.Member] = List.concat(
70+
getVariables(specialInputPorts),
71+
getVariables(typedInputPorts),
72+
getVariables(serialInputPorts),
73+
getVariables(specialOutputPorts),
74+
getVariables(typedOutputPorts),
75+
getVariables(serialOutputPorts),
76+
)
8777

8878
private def getConstants(ports: List[PortInstance]): List[CppDoc.Class.Member] = {
89-
if ports.isEmpty then Nil
90-
else List(
79+
lazy val member = {
80+
val kind = getPortListTypeString(ports)
81+
val direction = ports.head.getDirection.get.toString
82+
def enumConstant(p: PortInstance) =
83+
writeEnumConstant(portConstantName(p), p.getArraySize)
9184
linesClassMember(
92-
List(
93-
Line.blank :: lines(
94-
s"//! Enumerations for numbers of ${getPortListTypeString(ports)} ${ports.head.getDirection.get.toString} ports"
95-
),
96-
wrapInEnum(
97-
ports.flatMap(p =>
98-
writeEnumConstant(
99-
portConstantName(p),
100-
p.getArraySize,
101-
)
102-
)
103-
)
104-
).flatten
85+
Line.blank ::
86+
line(s"//! Enumerations for numbers of $kind $direction ports") ::
87+
wrapInEnum(ports.flatMap(enumConstant))
10588
)
106-
)
89+
}
90+
guardedList (!ports.isEmpty) (List(member))
10791
}
10892

10993
def generateNumGetters(
11094
ports: List[PortInstance],
11195
portName: PortInstance => String,
11296
numGetterName: PortInstance => String,
113-
variableName: PortInstance => String
97+
ioe: ComponentCppWriterUtils.InternalOrExternal =
98+
ComponentCppWriterUtils.InternalOrExternal.Internal
11499
) = {
115-
mapPorts(ports, p => List(
116-
functionClassMember(
117-
Some(
118-
s"""|Get the number of ${portName(p)} ports
119-
|
120-
|\\return The number of ${portName(p)} ports
121-
|"""
122-
),
123-
numGetterName(p),
124-
Nil,
125-
CppDoc.Type("FwIndexType"),
126-
lines(
127-
s"return static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->${variableName(p)}));"
128-
),
129-
CppDoc.Function.NonSV,
130-
CppDoc.Function.Const
131-
)
132-
))
100+
lazy val constantPrefix = ioe match {
101+
case ComponentCppWriterUtils.InternalOrExternal.Internal => ""
102+
case ComponentCppWriterUtils.InternalOrExternal.External =>
103+
s"$componentClassName::"
104+
}
105+
def generateNumGetter(p: PortInstance) = lines(
106+
s"""|
107+
|//! Get the number of ${portName(p)} ports
108+
|//!
109+
|//! \\return The number of ${portName(p)} ports
110+
|static constexpr FwIndexType ${numGetterName(p)}() {
111+
| return ${constantPrefix}${portConstantName(p)};
112+
|}
113+
|"""
114+
)
115+
mapPorts(
116+
ports,
117+
p => List(linesClassMember(generateNumGetter(p))),
118+
CppDoc.Lines.Hpp
119+
)
133120
}
134121

135122
private def getNumGetters(ports: List[PortInstance]): List[CppDoc.Class.Member] = {
136-
val dirStr = ports match {
137-
case Nil => ""
138-
case _ => ports.head.getDirection.get.toString
139-
}
140-
123+
lazy val direction = ports.headOption.map(_.getDirection.get.toString).getOrElse("")
124+
lazy val kind = getPortListTypeString(ports)
125+
def portName(p: PortInstance) =
126+
s"${p.getUnqualifiedName} ${p.getDirection.get.toString}"
141127
addAccessTagAndComment(
142128
"protected",
143-
s"Getters for numbers of ${getPortListTypeString(ports)} $dirStr ports",
144-
generateNumGetters(
145-
ports,
146-
(p: PortInstance) => s"${p.getUnqualifiedName} ${p.getDirection.get.toString}",
147-
portNumGetterName,
148-
portVariableName
149-
)
129+
s"Getters for numbers of $kind $direction ports",
130+
generateNumGetters(ports, portName, portNumGetterName),
131+
CppDoc.Lines.Hpp
150132
)
151133
}
152134

153135
private def getVariables(ports: List[PortInstance]): List[CppDoc.Class.Member] = {
154-
val dirStr = ports match {
155-
case Nil => ""
156-
case _ => ports.head.getDirection.get.toString
136+
lazy val direction = ports.headOption.map(_.getDirection.get.toString).getOrElse("")
137+
def variable(p: PortInstance) = {
138+
val typeName = getQualifiedPortTypeName(p, p.getDirection.get)
139+
val name = portVariableName(p)
140+
val num = portConstantName(p)
141+
lines(
142+
s"""|
143+
|//! ${p.getDirection.get.toString.capitalize} port ${p.getUnqualifiedName}
144+
|$typeName $name[$num];
145+
|"""
146+
)
157147
}
158-
159148
addAccessTagAndComment(
160149
"private",
161-
s"${getPortListTypeString(ports).capitalize} $dirStr ports",
162-
mapPorts(ports, p => {
163-
val typeName = getQualifiedPortTypeName(p, p.getDirection.get)
164-
val name = portVariableName(p)
165-
val num = portConstantName(p)
166-
167-
List(
168-
linesClassMember(
169-
lines(
170-
s"""|
171-
|//! ${p.getDirection.get.toString.capitalize} port ${p.getUnqualifiedName}
172-
|$typeName $name[$num];
173-
|"""
174-
)
175-
)
176-
)
177-
}, CppDoc.Lines.Hpp),
150+
s"${getPortListTypeString(ports).capitalize} $direction ports",
151+
mapPorts(
152+
ports,
153+
p => List(linesClassMember(variable(p))),
154+
CppDoc.Lines.Hpp
155+
),
178156
CppDoc.Lines.Hpp
179157
)
180158
}

compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/TestWriter/ComponentTestUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ abstract class ComponentTestUtils(
167167
def outputPortName(name: String) =
168168
s"to_$name"
169169

170-
/** Get the corresponding tester port name for a port in the component under test */
170+
/** Get the corresponding tester port name for a port in the component under test */
171171
def testerPortName(p: PortInstance) =
172172
p.getDirection.get match {
173173
case PortInstance.Direction.Input => outputPortName(p.getUnqualifiedName)

compiler/lib/src/main/scala/codegen/CppWriter/ComponentCppWriter/TestWriter/ComponentTesterBaseWriter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,9 @@ case class ComponentTesterBaseWriter(
505505
inputPorts ++ outputPorts,
506506
testerPortName,
507507
testerPortNumGetterName,
508-
testerPortVariableName
509-
)
508+
ComponentCppWriterUtils.InternalOrExternal.External
509+
),
510+
CppDoc.Lines.Hpp
510511
)
511512
}
512513

compiler/tools/fpp-to-cpp/test/component/base/ActiveAsyncProductPortsOnlyComponentAc.ref.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -245,32 +245,6 @@ ActiveAsyncProductPortsOnlyComponentBase ::
245245

246246
}
247247

248-
// ----------------------------------------------------------------------
249-
// Getters for numbers of special input ports
250-
// ----------------------------------------------------------------------
251-
252-
FwIndexType ActiveAsyncProductPortsOnlyComponentBase ::
253-
getNum_productRecvIn_InputPorts() const
254-
{
255-
return static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->m_productRecvIn_InputPort));
256-
}
257-
258-
// ----------------------------------------------------------------------
259-
// Getters for numbers of special output ports
260-
// ----------------------------------------------------------------------
261-
262-
FwIndexType ActiveAsyncProductPortsOnlyComponentBase ::
263-
getNum_productRequestOut_OutputPorts() const
264-
{
265-
return static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->m_productRequestOut_OutputPort));
266-
}
267-
268-
FwIndexType ActiveAsyncProductPortsOnlyComponentBase ::
269-
getNum_productSendOut_OutputPorts() const
270-
{
271-
return static_cast<FwIndexType>(FW_NUM_ARRAY_ELEMENTS(this->m_productSendOut_OutputPort));
272-
}
273-
274248
// ----------------------------------------------------------------------
275249
// Connection status queries for special output ports
276250
// ----------------------------------------------------------------------

compiler/tools/fpp-to-cpp/test/component/base/ActiveAsyncProductPortsOnlyComponentAc.ref.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ class ActiveAsyncProductPortsOnlyComponentBase :
137137
//! Get the number of productRecvIn input ports
138138
//!
139139
//! \return The number of productRecvIn input ports
140-
FwIndexType getNum_productRecvIn_InputPorts() const;
140+
static constexpr FwIndexType getNum_productRecvIn_InputPorts() {
141+
return NUM_PRODUCTRECVIN_INPUT_PORTS;
142+
}
141143

142144
protected:
143145

@@ -148,12 +150,16 @@ class ActiveAsyncProductPortsOnlyComponentBase :
148150
//! Get the number of productRequestOut output ports
149151
//!
150152
//! \return The number of productRequestOut output ports
151-
FwIndexType getNum_productRequestOut_OutputPorts() const;
153+
static constexpr FwIndexType getNum_productRequestOut_OutputPorts() {
154+
return NUM_PRODUCTREQUESTOUT_OUTPUT_PORTS;
155+
}
152156

153157
//! Get the number of productSendOut output ports
154158
//!
155159
//! \return The number of productSendOut output ports
156-
FwIndexType getNum_productSendOut_OutputPorts() const;
160+
static constexpr FwIndexType getNum_productSendOut_OutputPorts() {
161+
return NUM_PRODUCTSENDOUT_OUTPUT_PORTS;
162+
}
157163

158164
protected:
159165

0 commit comments

Comments
 (0)