Skip to content

Commit

Permalink
Update C packed array conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
dinkelk committed Oct 7, 2024
1 parent 3f497c5 commit a6fa2e5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
1 change: 1 addition & 0 deletions gen/generators/packed_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"array/name-validation.adb",
"array/name-assertion.ads",
"array/name-c.ads",
"array/name-c.adb",
"array/name.py",
"array/name_type_ranges.adb",
]
Expand Down
72 changes: 72 additions & 0 deletions gen/templates/array/name-c.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--------------------------------------------------------------------------------
-- {{ formatType(model_name) }} {{ formatType(model_type) }} C/C++ Interface Body
--
-- Generated from {{ filename }} on {{ time }}.
--------------------------------------------------------------------------------

package body {{ name }}.C is

function To_Ada (Src : in U_C) return U is
begin
{% if element.is_packed_type %}
return [for J in Src'Range => {{ element.type_package }}.C.To_Ada (Src (J))];
{% else %}
return [for J in Src'Range => Src (J)];
{% endif %}
end To_Ada;

function To_C (Src : in U) return U_C is
begin
{% if element.is_packed_type %}
return [for J in Src'Range => {{ element.type_package }}.C.To_C (Src (J))];
{% else %}
return [for J in Src'Range => Src (J)];
{% endif %}
end To_C;

{% if endianness in ["either", "big"] %}
function Pack (Src : in U_C) return T is
begin
{% if element.is_packed_type %}
return [for J in Src'Range => {{ element.type_package }}.C.Pack (Src (J))];
{% else %}
return [for J in Src'Range => Src (J)];
{% endif %}
end Pack;

{% endif %}
{% if endianness in ["either", "little"] %}
function Pack (Src : in U_C) return T_Le is
begin
{% if element.is_packed_type %}
return [for J in Src'Range => {{ element.type_package }}.C.Pack (Src (J))];
{% else %}
return [for J in Src'Range => Src (J)];
{% endif %}
end Pack;

{% endif %}
{% if endianness in ["either", "big"] %}
function Unpack (Src : in T) return U_C is
begin
{% if element.is_packed_type %}
return [for J in Src'Range => {{ element.type_package }}.C.Unpack (Src (J))];
{% else %}
return [for J in Src'Range => Src (J)];
{% endif %}
end Unpack;

{% endif %}
{% if endianness in ["either", "little"] %}
function Unpack (Src : in T_Le) return U_C is
begin
{% if element.is_packed_type %}
return [for J in Src'Range => {{ element.type_package }}.C.Unpack (Src (J))];
{% else %}
return [for J in Src'Range => Src (J)];
{% endif %}
end Unpack;

{% endif %}

end {{ name }}.C;
24 changes: 13 additions & 11 deletions gen/templates/array/name-c.ads
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
--
-- Generated from {{ filename }} on {{ time }}.
--------------------------------------------------------------------------------

-- Standard Includes:
with Ada.Unchecked_Conversion;
{% if type_includes %}

-- Record Component Includes:
Expand All @@ -29,17 +26,22 @@ package {{ name }}.C is
{% endif %}

-- Unpacked array type:
subtype U_C is Unconstrained{% if length %} (Constrained_Index_Type){% endif %};
subtype U_C is Unconstrained_C{% if length %} (Constrained_Index_Type){% endif %};

-- Access type for U
type U_C_Access is access all U_C;

-- Functions for converting between the Ada and C version of the packed type:
function To_Ada is new Ada.Unchecked_Conversion (Source => U_C, Target => U);
function To_C is new Ada.Unchecked_Conversion (Source => U, Target => U_C);

-- The .C package is not supported for all Adamant packed records. We do not allow compilation in
-- these cases.
pragma Compile_Time_Error ({{ name }}.U'Size /= U_C'Size, "C type size not compatible with Ada type size.");
-- Functions for converting between the Ada and C version of the
-- unpacked and packed types:
function To_Ada (Src : in U_C) return U;
function To_C (Src : in U) return U_C;
{% if endianness in ["either", "big"] %}
function Pack (Src : in U_C) return T;
function Unpack (Src : in T) return U_C;
{% endif %}
{% if endianness in ["either", "little"] %}
function Pack (Src : in U_C) return T_Le;
function Unpack (Src : in T_Le) return U_C;
{% endif %}

end {{ name }}.C;

0 comments on commit a6fa2e5

Please sign in to comment.