Ada 重叠标签域

Ada overlaps tag field

这是我喜欢的类型:

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

我没有错误,但是如果我在第一行将我的类型更改为 Type T_MyType is tagged record,我就会出现错误:

component overlaps tag field of "T_MyType"

标记记录是否有隐藏字段?如何使用标记记录保存我的地址?

假设您使用的是 GNAT,this chapter of the GNAT RM 是相关的:

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.

所以你需要为标签字段保存存储空间,像这样:

-- 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;