Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada overlaps tag field

Tags:

record

ada

This is my type :

package MyPackage is
    Type T_MyType is record
        Field1 : Uint16;
        Field2 : Uint32;
        Field3 : Uint8;
        Field4 : Uint8;
    end record;
private
    for T_MyType'Alignment use 4;
    for T_MyType'Size use 64;
    for T_MyType use record
        Field1 at 16#00# range 0 .. 15;
        Field2 at 16#02# range 0 .. 31;
        Field3 at 16#06# range 0 .. 7;
        Field4 at 16#06# range 8 .. 15:
    end record
end package

I have no error, but if I change my type to Type T_MyType is tagged record at the first line, I have the error:

component overlaps tag field of "T_MyType"

Are there hidden fields for a tagged record? How can I keep my addresses with a tagged record?

like image 795
A.Pissicat Avatar asked Dec 22 '22 20:12

A.Pissicat


1 Answers

Assuming you're using GNAT, this chapter of the GNAT RM is relevant:

The tag field of a tagged type always occupies an address sized field at the start of the record. No component clause may attempt to overlay this tag.

So you need to save storage for the tag field, like this:

-- Standard'Address_Size is GNAT-specific and needed here since
-- System.Address'Size is not static. This length is in storage units.
Tag_Length : constant := Standard'Address_Size / System.Storage_Unit;

for T_MyType'Size use Standard'Address_Size + 64;
for T_MyType use record
   Field1 at Tag_Length + 16#00# range 0..15;
   Field2 at Tag_Length + 16#02# range 0..31;
   Field3 at Tag_Length + 16#06# range 0..7;
   Field4 at Tag_Length + 16#06# range 8..15;
end record;
like image 170
flyx Avatar answered Feb 04 '23 23:02

flyx