I declare a union type like this:
type Access_Kind is (Named, Indexed);
type Array_Type is array (0 .. 1) of Integer;
type Record_Type (Kind : Access_Kind := Access_Kind'First) is record
case Kind is
when Named =>
X, Y : Integer;
when Indexed =>
S : Array_Type;
end case;
end record;
pragma Unchecked_Union (Record_Type);
pragma Convention (C_Pass_By_Copy, Record_Type);
function Create (X, Y : Integer) return Record_Type;
Now when I try to create a derived type:
type Integer2 is new Record_Type;
GNAT gives me the following warning:
warning: in instantiation at line [pragma Convention...]
warning: variant record has no direct equivalent in C
warning: use of convention for type "Integer2" is dubious
So it looks like the pragma Convention is applied to the derived type, but Unchecked_Union is not. I cannot apply it to the derived type again because Record_Type already has primitive operations defined (Integer2 is defined in another package).
Is this correct behavior or a GNAT bug? How can I correctly derive from Unchecked_Union types so that the new type inherits the Unchecked_Union pragma?
GNAT Version: GNAT GPL 2012 (20120509).
One possibility would be to declare the operations of
Record_Typein a nested package, sayOps, so that they aren’t primitive:Using
-gnatRto display the chosen representation, I getThat said, I think GNAT’s behaviour is wrong.
ARM 13.1(0.1) says that there are representational and operational aspects, and (1) defines the representational aspects. (10) is why we need to avoid primitive operations. (15) says that representational aspects are inherited by derived types; but (15.1) says that operational aspects are not, “unless specified” for the specific aspect. I guess that “specified” means “in the ARM for language-defined aspects, or by the vendor for vendor-defined aspects”.
B3.3(3.1) states that
Unchecked_Unionis a representation aspect. It should therefore be inherited.B3.1(0.1) states that interfacing aspects, including
Convention, are representation aspects.C_Pass_By_Copyshould therefore be inherited.I’ll work up a bug report.