Skip to content

Commit

Permalink
cpp: Emit Bit Position (#648)
Browse files Browse the repository at this point in the history
* cpp: Add opt. source-position field to compounds

Also improve contructor member intialization by
adding a new freemarker helper marker that takes
care of commas, formatting etc. and should make
the templates easier to read.

* cpp: Add getSourceRegion member function

* cpp: Add source region to choice and union

* cpp: Prefer casting unused arguments to void

* cpp: Add missing sourceRegion accessors

For union an choice types.

* cpp: Update tidy suppressions

* cpp: Change getSourceRegion to bitPosition

* test: Add with_bit_position test

* tidy: Suppress generated-code warnings

* cpp: Rename withBitPosition to withBitPositionCode
  • Loading branch information
johannes-wolf authored Jul 30, 2024
1 parent c0a961a commit fbaeb0c
Show file tree
Hide file tree
Showing 18 changed files with 388 additions and 42 deletions.
14 changes: 13 additions & 1 deletion compiler/extensions/cpp/freemarker/Choice.cpp.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@ ${I}return {};
}
</#if>

<#if withBitPositionCode>
size_t bitPosition() const override
{
return m_object.bitPosition();
}
</#if>

private:
<#if isConst>const </#if>${fullName}& m_object;
};
Expand All @@ -292,7 +299,6 @@ ${I}return {};

<#if withWriterCode>
<@choice_reflectable false/>

</#if>
</#if>
</#if>
Expand Down Expand Up @@ -576,6 +582,12 @@ void ${name}::write(${name}::ZserioPackingContext&<#if uses_packing_context(fiel
}
</#if>
</#if>
<#if withBitPositionCode>
size_t ${name}::bitPosition() const
{
return m_bitPosition;
}
</#if>
<#if fieldList?has_content>

<@inner_classes_definition name, fieldList/>
Expand Down
15 changes: 15 additions & 0 deletions compiler/extensions/cpp/freemarker/Choice.h.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,16 @@ public:
</#if>
</#if>

<#if withBitPositionCode>
/**
* Get the source offset and size of the structure in bits.
* For objects not loaded from a blob, the offest is 0.
*
* \return Tuple of the objects offset and size in bits.
*/
size_t bitPosition() const;
</#if>

private:
<@private_section_declarations name, fieldList/>
<#if fieldList?has_content>
Expand All @@ -339,6 +349,11 @@ private:
</#if>
${types.anyHolder.name} copyObject(const allocator_type& allocator) const;

</#if>
<#-- Data members -->
<#if withBitPositionCode>
<#-- Source position must be the first member in order to get initialized first. -->
size_t m_bitPosition = 0;
</#if>
<@compound_parameter_members compoundParametersData/>
<@compound_constructor_members compoundConstructorsData/>
Expand Down
128 changes: 91 additions & 37 deletions compiler/extensions/cpp/freemarker/CompoundConstructor.inc.ftl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<#include "CppUtility.inc.ftl">
<#include "CompoundField.inc.ftl">
<#include "CompoundParameter.inc.ftl">
<#macro compound_default_constructor compoundConstructorsData>
Expand Down Expand Up @@ -29,19 +30,20 @@
<#local numExtendedFields=num_extended_fields(compoundConstructorsData.fieldList)/>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>const allocator_type&<#rt>
<#lt><#if empty_constructor_needs_allocator(compoundConstructorsData.fieldList)> allocator</#if>) <#rt>
<#lt>noexcept<#if hasInitializers> :</#if>
<#lt><#if empty_constructor_needs_allocator(compoundConstructorsData.fieldList)> allocator</#if>) noexcept<#t>
<@cpp_initializer_list>
<#if needs_compound_initialization(compoundConstructorsData)>
m_isInitialized(false)<#if memberInitializationMacroName != "">,</#if>
m_isInitialized(false)
<#elseif has_field_with_initialization(compoundConstructorsData.fieldList)>
m_areChildrenInitialized(false)<#if memberInitializationMacroName != "">,</#if>
m_areChildrenInitialized(false)
</#if>
<#if memberInitializationMacroName != "">
<#if (numExtendedFields > 0)>
m_numExtendedFields(${numExtendedFields}),
m_numExtendedFields(${numExtendedFields})
</#if>
<@.vars[memberInitializationMacroName]/>
</#if>
</@cpp_initializer_list>
{
}
</#macro>
Expand Down Expand Up @@ -83,36 +85,41 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam
needs_compound_initialization(compoundConstructorsData) ||
has_field_with_initialization(compoundConstructorsData.fieldList) ||
memberInitializationMacroName != ""/>
<#local wantsBitStreamReader = compoundConstructorsData.fieldList?has_content || withBitPositionCode>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#if packed>
${compoundConstructorsData.compoundName}::ZserioPackingContext& context, <#t>
</#if>
::zserio::BitStreamReader&<#if compoundConstructorsData.fieldList?has_content> in</#if><#t>
::zserio::BitStreamReader&<#if wantsBitStreamReader> in</#if><#t>
<#if constructorArgumentTypeList?has_content>
<#lt>,
${constructorArgumentTypeList}<#t>
</#if>
, const allocator_type&<#if read_constructor_needs_allocator(compoundConstructorsData.fieldList)> allocator</#if><#t>
<#if hasInitializers>
<#lt>) :
<#else>
<#lt>)
, const allocator_type&<#if read_constructor_needs_allocator(compoundConstructorsData.fieldList)> allocator</#if>)<#t>
<@cpp_initializer_list>
<#-- Store reader bit position (option -withBitPositionCode) -->
<#if withBitPositionCode>
m_bitPosition(<#if wantsBitStreamReader>in.getBitPosition()<#else>0u</#if>)
</#if>

<#if constructorArgumentTypeList?has_content>
<@compound_parameter_constructor_initializers compoundConstructorsData.compoundParametersData, 2,
needs_compound_initialization(compoundConstructorsData)/>
</#if>

<#if needs_compound_initialization(compoundConstructorsData)>
m_isInitialized(true)<#if memberInitializationMacroName != "">,</#if>
m_isInitialized(true)
<#elseif has_field_with_initialization(compoundConstructorsData.fieldList)>
m_areChildrenInitialized(true)<#if memberInitializationMacroName != "">,</#if>
m_areChildrenInitialized(true)
</#if>

<#if memberInitializationMacroName != "">
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(0),
m_numExtendedFields(0)
</#if>
<@.vars[memberInitializationMacroName] packed/>
</#if>
</@cpp_initializer_list>
{
}
</#macro>
Expand Down Expand Up @@ -167,35 +174,46 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam

<#macro compound_copy_constructor_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>const ${compoundConstructorsData.compoundName}& other)<#if compoundConstructorsData.fieldList?has_content> :</#if>
<#lt>const ${compoundConstructorsData.compoundName}& other)<#rt>
<@cpp_initializer_list>
<#if withBitPositionCode>
m_bitPosition(other.m_bitPosition)
</#if>

<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(other.m_numExtendedFields),
m_numExtendedFields(other.m_numExtendedFields)
</#if>

<#list compoundConstructorsData.fieldList as field>
<@compound_copy_constructor_initializer_field field, field?has_next, 2/>
<#-- hasNext is set to false because joining lines by comma is handled by the cpp_initializer_list macro -->
<@compound_copy_constructor_initializer_field field, false, 2/>
<#if field.usesAnyHolder>
<#break>
</#if>
</#list>
</@cpp_initializer_list>
{
<@compound_copy_initialization compoundConstructorsData/>
}
</#macro>

<#macro compound_copy_constructor_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(::zserio::NoInitT,
const ${compoundConstructorsData.compoundName}&<#if compoundConstructorsData.fieldList?has_content> other</#if>) :
m_isInitialized(false)<#if compoundConstructorsData.fieldList?has_content>,</#if>
const ${compoundConstructorsData.compoundName}& other)
<@cpp_initializer_list>
m_isInitialized(false)
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(other.m_numExtendedFields),
m_numExtendedFields(other.m_numExtendedFields)
</#if>
<#list compoundConstructorsData.fieldList as field>
<@compound_copy_constructor_initializer_field field, field?has_next, 2/>
<#if field.usesAnyHolder>
<#break>
</#if>
</#list>
</@cpp_initializer_list>
{
(void)other;
}
</#macro>

Expand Down Expand Up @@ -245,7 +263,7 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam

<#macro compound_assignment_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundName}::assign(::zserio::NoInitT,
const ${compoundConstructorsData.compoundName}&<#if compoundConstructorsData.fieldList?has_content> other</#if>)
const ${compoundConstructorsData.compoundName}& other)
{
m_isInitialized = false;
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
Expand All @@ -258,6 +276,7 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam
</#if>
</#list>

(void)other;
return *this;
}
</#macro>
Expand Down Expand Up @@ -289,35 +308,49 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam

<#macro compound_move_constructor_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>${compoundConstructorsData.compoundName}&& other)<#if compoundConstructorsData.fieldList?has_content> :</#if>
<#lt>${compoundConstructorsData.compoundName}&& other)<#rt>
<@cpp_initializer_list>
<#if withBitPositionCode>
m_bitPosition(::std::move(other.m_bitPosition))
</#if>
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(other.m_numExtendedFields),
m_numExtendedFields(other.m_numExtendedFields)
</#if>
<#list compoundConstructorsData.fieldList as field>
<@compound_move_constructor_initializer_field field, field?has_next, 2/>
<#if field.usesAnyHolder>
<#break>
</#if>
</#list>
</@cpp_initializer_list>
{
<@compound_copy_initialization compoundConstructorsData/>
}
</#macro>

<#macro compound_move_constructor_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(::zserio::NoInitT,
${compoundConstructorsData.compoundName}&&<#if compoundConstructorsData.fieldList?has_content> other</#if>) :
m_isInitialized(false)<#if compoundConstructorsData.fieldList?has_content>,</#if>
<#lt>${compoundConstructorsData.compoundName}&& other)<#rt>
<@cpp_initializer_list>
<#if withBitPositionCode>
m_bitPosition(::std::move(other.m_bitPosition))
</#if>

m_isInitialized(false)

<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(other.m_numExtendedFields),
m_numExtendedFields(other.m_numExtendedFields)
</#if>

<#list compoundConstructorsData.fieldList as field>
<@compound_move_constructor_initializer_field field, field?has_next, 2/>
<#if field.usesAnyHolder>
<#break>
</#if>
</#list>
</@cpp_initializer_list>
{
(void)other;
}
</#macro>

Expand Down Expand Up @@ -351,6 +384,9 @@ ${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundNam
${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundName}::operator=(<#rt>
<#lt>${compoundConstructorsData.compoundName}&& other)
{
<#if withBitPositionCode>
m_bitPosition = ::std::move(other.m_bitPosition);
</#if>
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields = other.m_numExtendedFields;
</#if>
Expand All @@ -368,8 +404,11 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam

<#macro compound_move_assignment_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundName}::assign(::zserio::NoInitT,
${compoundConstructorsData.compoundName}&&<#if compoundConstructorsData.fieldList?has_content> other</#if>)
<#lt>${compoundConstructorsData.compoundName}&& other)
{
<#if withBitPositionCode>
m_bitPosition = other.m_bitPosition;
</#if>
m_isInitialized = false;
<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields = other.m_numExtendedFields;
Expand All @@ -381,6 +420,7 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam
</#if>
</#list>

(void)other;
return *this;
}
</#macro>
Expand Down Expand Up @@ -415,43 +455,57 @@ ${compoundConstructorsData.compoundName}& ${compoundConstructorsData.compoundNam
<#local initialization><@compound_copy_initialization compoundConstructorsData/></#local>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>::zserio::PropagateAllocatorT,
const ${compoundConstructorsData.compoundName}&<#rt>
<#lt><#if compoundConstructorsData.fieldList?has_content || initialization?has_content> other</#if>,<#rt>
<#lt> const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<#lt><#if compoundConstructorsData.fieldList?has_content> :</#if>
<#lt>const ${compoundConstructorsData.compoundName}& other,
<#lt>const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<@cpp_initializer_list>
<#if withBitPositionCode>
m_bitPosition(other.m_bitPosition)
</#if>

<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(other.m_numExtendedFields),
m_numExtendedFields(other.m_numExtendedFields)
</#if>

<#list compoundConstructorsData.fieldList as field>
<@compound_allocator_propagating_copy_constructor_initializer_field field, field?has_next, 2/>
<#if field.usesAnyHolder>
<#break>
</#if>
</#list>
</@cpp_initializer_list>
{
<#if initialization?has_content>
${initialization}<#t>
</#if>
(void)other;
}
</#macro>

<#macro compound_allocator_propagating_copy_constructor_no_init_definition compoundConstructorsData>
${compoundConstructorsData.compoundName}::${compoundConstructorsData.compoundName}(<#rt>
<#lt>::zserio::PropagateAllocatorT, ::zserio::NoInitT,
const ${compoundConstructorsData.compoundName}&<#rt>
<#lt><#if compoundConstructorsData.fieldList?has_content || initialization?has_content> other</#if>,<#rt>
<#lt> const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>) :
m_isInitialized(false)<#if compoundConstructorsData.fieldList?has_content>,</#if>
<#lt>const ${compoundConstructorsData.compoundName}& other,
<#lt>const allocator_type&<#if compoundConstructorsData.fieldList?has_content> allocator</#if>)<#rt>
<@cpp_initializer_list>
<#if withBitPositionCode>
m_bitPosition(other.m_bitPosition)
</#if>

m_isInitialized(false)

<#if (num_extended_fields(compoundConstructorsData.fieldList) > 0)>
m_numExtendedFields(other.m_numExtendedFields),
m_numExtendedFields(other.m_numExtendedFields)
</#if>

<#list compoundConstructorsData.fieldList as field>
<@compound_allocator_propagating_copy_constructor_initializer_field field, field?has_next, 2/>
<#if field.usesAnyHolder>
<#break>
</#if>
</#list>
</@cpp_initializer_list>
{
(void)other;
}
</#macro>

Expand Down
Loading

0 comments on commit fbaeb0c

Please sign in to comment.