Skip to content

Commit

Permalink
Bitsets according to standard IDL to C++11 2021 (#145)
Browse files Browse the repository at this point in the history
* Refs #20853. Change behaviour of getAllBitfields

Signed-off-by: Ricardo González Moreno <[email protected]>

* Refs #20853. Change bitset generation according IDL to C++11 2021

Signed-off-by: Ricardo González Moreno <[email protected]>

* Refs #21026. Apply suggestion

Signed-off-by: Ricardo González Moreno <[email protected]>

---------

Signed-off-by: Ricardo González Moreno <[email protected]>
  • Loading branch information
richiware authored May 21, 2024
1 parent 752ae92 commit f1bc6cd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 55 deletions.
13 changes: 5 additions & 8 deletions src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1320,12 +1320,15 @@ bitset_type[Vector<Annotation> annotations] returns [Pair<Vector<TypeCode>, Temp
( COLON scoped_name
{
superType = ctx.getTypeCode($scoped_name.pair.first());
if (superType != null)
{
typecode.addInheritance(ctx, superType);
}

}
)?
LEFT_BRACE bitfield[typecode] RIGHT_BRACE
{
if (superType != null) typecode.addInheritance(ctx, superType);
if(ctx.isInScopedFile() || ctx.isScopeLimitToAll())
{
if(tmanager != null) {
Expand Down Expand Up @@ -1357,9 +1360,6 @@ bitfield [BitsetTypeCode owner]
bitfield = new Bitfield($owner, $bitfield_spec.bitfieldType, $simple_declarators.ret.get(count).first().first());
$owner.addBitfield(bitfield);
if(!$owner.addMember(bitfield))
throw new ParseException($simple_declarators.ret.get(count).first().second(), " was defined previously");
}
}
}
Expand All @@ -1376,9 +1376,6 @@ bitfield [BitsetTypeCode owner]
bitfield = new Bitfield($owner, $bitfield_spec.bitfieldType, "");
$owner.addBitfield(bitfield);
if(!$owner.addMember(bitfield))
System.out.println("Empty space failed to be inserted.");
}
}
)
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/eprosima/idl/parser/typecode/AliasTypeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,6 @@ public List<String> getEvaluatedDimensions() throws RuntimeGenerationException
": trying accessing dimensions for a non-array type");
}

public int getFullBitSize() // Function for alias when enclosed type is a BitsetTypeCode
{
int returnedValue = 0;

if (getContentTypeCode() instanceof BitsetTypeCode)
{
returnedValue = ((BitsetTypeCode)getContentTypeCode()).getFullBitSize();
}

return returnedValue;
}

public TypeCode getInheritance() // Function for alias when enclosed type is a StructTypeCode
{
TypeCode returnedValue = null;
Expand Down
51 changes: 46 additions & 5 deletions src/main/java/com/eprosima/idl/parser/typecode/Bitfield.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,55 @@ public BitfieldSpec getSpec()
return m_spec;
}

@Override
public String getName()
/*!
* @ingroup api_for_stg
* @brief This function check if the bitfield was defined by name.
* @return true if the bitfield was defined by name in the IDL file. false otherwise.
*/
public boolean getIsDefined()
{
return !getName().isEmpty();
}

/*!
* @ingroup api_for_stg
* @brief This function returns a string containing the mask in hexadecimal which can be used to make bitwise AND
* operation over the bitfield.
* @return The bit mask in hexadecimal.
*/
public String getBitmask()
{
if (super.getName().isEmpty())
String mask = "";
int mod = m_spec.getBitSize() / 4;
int rest = m_spec.getBitSize() % 4;

while (0 != mod)
{
mask += "F";
--mod;
}

switch(rest)
{
case 1:
mask = "1" + mask;
break;
case 2:
mask = "3" + mask;
break;
case 3:
mask = "7" + mask;
break;
default:
break;
}

if (mask.isEmpty())
{
return null;
mask = "0";
}
return super.getName();

return "0x" + mask;
}

@Override
Expand Down
92 changes: 63 additions & 29 deletions src/main/java/com/eprosima/idl/parser/typecode/BitsetTypeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,48 +77,76 @@ public String getIdlTypename()
return st.render();
}

/*!
* @ingroup api_for_stg
* @brief This function returns all bitfields including the inherited ones.
* @return A list of all bitfields.
*/
public List<Bitfield> getBitfields()
{
return getBitfields(false);
ArrayList<Bitfield> result = new ArrayList<Bitfield>();
result.addAll(m_bitfields.values());
return result;
}

public List<Bitfield> getBitfields(
boolean includeParents)
/*!
* @ingroup api_for_stg
* @brief This function returns all bitfields including the inherited ones which were defined by name.
* @return A list of all bitfields defined by name.
*/
public List<Bitfield> getDefinedBitfields()
{
ArrayList<Bitfield> result = new ArrayList<Bitfield>();

if (includeParents && enclosed_super_type_ != null)
for (Bitfield bitfield : m_bitfields.values())
{
result.addAll(enclosed_super_type_.getAllBitfields());
if (bitfield.getIsDefined())
{
result.add(bitfield);
}
}

result.addAll(m_bitfields.values());
return result;
}

public List<Bitfield> getAllBitfields() // Alias for getBitfields(true) for stg
{
return getBitfields(true);
}

public boolean addBitfield(
Bitfield bitfield)
{
if (null == bitfield.getName())
{
m_current_base += bitfield.getSpec().getBitSize();
return true;
bitfield.setName("");
}
if (!m_bitfields.containsKey(bitfield.getName()) )

String key = bitfield.getName();

if (key.isEmpty())
{
m_bitfields.put(bitfield.getName(), bitfield);
++annonymous_index_;
key = "_annonymous_" + Integer.toString(annonymous_index_);
}

if (!m_bitfields.containsKey(key))
{
m_bitfields.put(key, bitfield);
bitfield.setBasePosition(m_current_base);
m_current_base += bitfield.getSpec().getBitSize();

if (bitfield.getIsDefined())
{
addMember(bitfield);
}

return true;
}
return false;
}

/*!
* @ingroup api_for_g4
* @brief This function adds the inherited Bitset to this Bitset instance.
* Internally add the member from inherited Bitset to this instance, making a plain bitset as defined in IDL to
* C++11 2021.
* @param ctx Parser context.
* @param parent Inherited Bitset
*/
@Override
public void addInheritance(
Context ctx,
Expand Down Expand Up @@ -150,6 +178,23 @@ else if (super_type_ != null)
{
throw new ParseException(null, "Inheritance must correspond to the name of a previously defined bitset");
}

//{{{ Add members of inherited Bitset.
if (null != enclosed_super_type_)
{
enclosed_super_type_.m_bitfields.forEach((key, bitfield) ->
{
Bitfield new_bitfield = new Bitfield(this, bitfield.getSpec(), bitfield.getName());
m_bitfields.put(key, bitfield);
m_current_base += bitfield.getSpec().getBitSize();

if (bitfield.getIsDefined())
{
addMember(bitfield);
}
});
}
//}}}
}

@Override
Expand All @@ -169,18 +214,7 @@ public int getBitSize()
return m_current_base;
}

public int getFullBitSize()
{
int size = 0;

if (enclosed_super_type_ != null)
{
size += enclosed_super_type_.getFullBitSize();
}

return m_current_base + size;
}

private int annonymous_index_ = 0;
private BitsetTypeCode enclosed_super_type_ = null;
private TypeCode super_type_ = null;
private LinkedHashMap<String, Bitfield> m_bitfields = new LinkedHashMap<String, Bitfield>();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/eprosima/idl/test/TestIDLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public void parseUnion(UnionTypeCode union) {

public void parseBitset(BitsetTypeCode bitset) {
System.out.println("Start Bitset: " + bitset.getName());
for (Bitfield field : bitset.getBitfields(true)) {
for (Bitfield field : bitset.getBitfields()) {
parseBitfield(field);
}
System.out.println("End Bitset: \n");
Expand Down

0 comments on commit f1bc6cd

Please sign in to comment.