rtiddsgen fails to evaluate const expression

7 posts / 0 new
Last post
Offline
Last seen: 10 months 1 week ago
Joined: 02/07/2020
Posts: 21
rtiddsgen fails to evaluate const expression

Connext Version: 7.2.0
rtiddsgen Version: 4.2.0

I have the following constants defined:

<const name="MAX_NUM_VALS" type="int16" value="372"/>
<const name="NUM_OCTETS_BITMASK" type="int16" value="MAX_NUM_VALS &gt;&gt; 3 + 1"/> 
In rtiddsgen, I get the following error:
ERROR com.rti.ndds.nddsgen.Main In member_337, NUM_OCTETS_BITMASK could not be resolved to an integer
However, if I wrap the referenced MAX_NUM_VALS const in parentheses (effectively making it a sub-expression), then the evaluation works as expected.
<const name="MAX_NUM_VALS" type="int16" value="372"/>
<const name="NUM_OCTETS_BITMASK" type="int16" value="(MAX_NUM_VALS) &gt;&gt; 3 + 1"/> 
 
 
Keywords:
Offline
Last seen: 10 months 1 week ago
Joined: 02/07/2020
Posts: 21

As a follow-up, I needed to add parentheses to change operator precedence, but the got an error:

<const name="NUM_OCTETS_BITMASK" type="int16" value="((MAX_NUM_VALS) &gt;&gt; 3) + 1"/> 

The extra set of parentheses results in the following error:

ERROR com.rti.ndds.nddsgen.Main String index out of range: 0

 

Offline
Last seen: 1 week 5 days ago
Joined: 04/23/2014
Posts: 58

Hi seairth,

I've done a quick test and this is working for me. I've called rtiddsgen like this:

rtiddsgen -language C++11 -example arm64Darwin20clang12.0 test.xml and this snippet is generated:

RTI_CONSTEXPR_OR_CONST_STRING int16_t NUM_OCTETS_BITMASK = (((MAX_NUM_VALS)) >> 3) + 1;  
 
In fact, it is working for me without the extra parenthesis you added around MAX_NUM_VALS
 
Can you put here how is your call to rtiddsgen?
Offline
Last seen: 10 months 1 week ago
Joined: 02/07/2020
Posts: 21

The issue is not with the <const> statement itself, it seems, but when the const is used in a struct:

<?xml version="1.0" standalone="yes"?>
<dds>
  <types>
    <const name="MAX_NUM_VALS" type="int16" value="372"/>
    <const name="NUM_OCTETS_BITMASK" type="int16" value="MAX_NUM_VALS / 8 + 1"/>
    <struct name="MyDataType">
      <member name="id" type="int64" key="true"/>
      <member name="mask" type="byte" sequenceMaxLength="NUM_OCTETS_BITMASK"/>
      <member name="num_samples" type="uint64"/>
    </struct>
  </types>
</dds>

with the following command:

~/rti_connext_dds-7.2.0/bin/rtiddsgen -language C++11 -update typefiles -unboundedSupport -d ./test -inputXml ./types_redux.xml

generates the following error:

ERROR com.rti.ndds.nddsgen.Main In member_5, NUM_OCTETS_BITMASK could not be resolved to an integer

If I change NUM_OCTETS_BITMASK to

      <const name="NUM_OCTETS_BITMASK" type="int16" value="(MAX_NUM_VALS) / 8 + 1"/>

then I do not get an error.

As for the bit-shift, this also seems related to using the const in a struct.  If I change NUM_OCTETS_BITMASK to:

      <const name="NUM_OCTETS_BITMASK" type="int16" value="((MAX_NUM_VALS) &gt;&gt; 3) + 1"/>

I get the following error:

ERROR com.rti.ndds.nddsgen.Main String index out of range: 0

The usage of "&gt;&gt;" vs ">>" makes no difference.  The additional parentheses are required for correct order of operations in C/C++, so are not optional.  Removing the parentheses around MAX_NUM_VALS just results in getting the original error.

 

Edit: I apologize for the repeated edits, but the editor keeps changing some of the greater-than/less-than symbols into their character reference format, and I'm changing them back to accurately show what is in my file.

 

Offline
Last seen: 10 months 1 week ago
Joined: 10/15/2018
Posts: 9

Hi seairth,

rtiddsgen does not resolve the constant value and is generated in the same way that is defined in the IDL/XML. Then, constant values defined as an expression cannot be used to defined the length of a sequence or any other value that expect an integer.

The rationale for not resolving the constant during the code generation are:

  • If the constant is defined as an expression, keep it like that will made it more readable in the generated code.
  • Depending on the generated language, the resolved value of the constant may be different.
  • If the constant is defined in B.idl and use a constant defined in A.idl, a change in the constant defined in A.idl will not force the regeneration of the code defined in B.idl.
    e.g.
    A.idl
       const A = 5;
    B.idl
    #include A.id
    const  B = A + 5
Offline
Last seen: 10 months 1 week ago
Joined: 02/07/2020
Posts: 21

I understand why a const expression wouldn't be resolved to a value during coegen, but that's not my problem.  I'm perfectly fine with the expression being kept as-is in the generated code.  But I need rtiddsgen to actually generate the code without throwing an error for valid expressions.  If the expression is not valid for some reason, please point that out.

Offline
Last seen: 10 months 1 week ago
Joined: 10/15/2018
Posts: 9

I agree we may improve the code generator. I have created the ticket to allow the use of constant expression to define the size of arrays, sequences and strings.

The code for the ticket is [CODEGENII-2041]