I wonder about what to think about it, while I could not imagine a
better solution :
Say a C structure is to be interfaced with Ada. The structure has a
"count" member and a variable length array, commonly defined in C as
something like elements[1].
I.e. a C structure like
typedef struct {
int count;
my_element_type elements[1];
} my_struct;
I'm tempted to use something like
type Element_Type is <Something>;
for Element_Type'Size use <Something>;
type Array_Type is array (Index_Type range <>) of aliased
Element_Type;
pragma Convention (C, Entity => Array_Type);
type Record_Type (Length : Index_Type) is record
Elements : Array_Type (1..Length);
end record;
for Record_Type use record
Length at 0 range 0 .. Something; -- Tell the Ada compiler where
the C
-- compiler has
placed the “ discriminant ”
end record;
pragma Convention (C, Entity => Record_Type);
Though the trouble may be that it is not possible to specify the bits
address of Elements, beceause it is not possible to have something
like “ Elements at 0 range <Something> .. <Something> * Length - 1; ”
What to think about it ?
Is it portable through most compilers ?
** Is it trustable ? **
http://www.adapower.com/index.php?Command=Class&ClassID=Advanced&CID=228
says (at the end):
> You need rep specs to handle tricky and unusual cases such as:
>
> * Mapping Ada record types with a discriminant and a variant part onto
> C unions. This can be made to work as long as the C type has a member
> that can serve as the discriminant; most sensibly designed C code does.
>
> [...]
>
> If you use rep specs, you should not need pragma Convention, and vice versa.
So this suggest that representation specs and pragma convention should
not be used simultaneously, as I've done due to some doubts.
Using representation specs is Ok, but the pragma Convention is to be
removed (will be done in a minute).