Skip to content

Commit

Permalink
Merge pull request #83 from dinkelk/u_c-conversions
Browse files Browse the repository at this point in the history
Update C packed type conversions
  • Loading branch information
dinkelk authored Oct 7, 2024
2 parents c2a0f78 + a6fa2e5 commit a52fa38
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 21 deletions.
2 changes: 2 additions & 0 deletions gen/generators/packed_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"record/name-assertion.ads",
"record/name-assertion.adb",
"record/name-c.ads",
"record/name-c.adb",
"record/name.py",
"record/name_type_ranges.adb",
]
Expand All @@ -27,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;
95 changes: 95 additions & 0 deletions gen/templates/record/name-c.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
--------------------------------------------------------------------------------
-- {{ 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
return (
{% for field in fields.values() %}
{% if field.is_packed_type %}
{{ field.name }} => {{ field.type_package }}.C.To_Ada (Src.{{ field.name }}){{ "," if not loop.last }}
{% else %}
{{ field.name }} => Src.{{ field.name }}{{ "," if not loop.last }}
{% endif %}
{% endfor %}
);
end To_Ada;

function To_C (Src : in U) return U_C is
begin
return (
{% for field in fields.values() %}
{% if field.is_packed_type %}
{{ field.name }} => {{ field.type_package }}.C.To_C (Src.{{ field.name }}){{ "," if not loop.last }}
{% else %}
{{ field.name }} => Src.{{ field.name }}{{ "," if not loop.last }}
{% endif %}
{% endfor %}
);
end To_C;

{% if endianness in ["either", "big"] %}
function Pack (Src : in U_C) return T is
begin
return (
{% for field in fields.values() %}
{% if field.is_packed_type %}
{{ field.name }} => {{ field.type_package }}.C.Pack (Src.{{ field.name }}){{ "," if not loop.last }}
{% else %}
{{ field.name }} => Src.{{ field.name }}{{ "," if not loop.last }}
{% endif %}
{% endfor %}
);
end Pack;

{% endif %}
{% if endianness in ["either", "little"] %}
function Pack (Src : in U_C) return T_Le is
begin
return (
{% for field in fields.values() %}
{% if field.is_packed_type %}
{{ field.name }} => {{ field.type_package }}.C.Pack (Src.{{ field.name }}){{ "," if not loop.last }}
{% else %}
{{ field.name }} => Src.{{ field.name }}{{ "," if not loop.last }}
{% endif %}
{% endfor %}
);
end Pack;

{% endif %}
{% if endianness in ["either", "big"] %}
function Unpack (Src : in T) return U_C is
begin
return (
{% for field in fields.values() %}
{% if field.is_packed_type %}
{{ field.name }} => {{ field.type_package }}.C.Unpack (Src.{{ field.name }}){{ "," if not loop.last }}
{% else %}
{{ field.name }} => Src.{{ field.name }}{{ "," if not loop.last }}
{% endif %}
{% endfor %}
);
end Unpack;

{% endif %}
{% if endianness in ["either", "little"] %}
function Unpack (Src : in T_Le) return U_C is
begin
return (
{% for field in fields.values() %}
{% if field.is_packed_type %}
{{ field.name }} => {{ field.type_package }}.C.Unpack (Src.{{ field.name }}){{ "," if not loop.last }}
{% else %}
{{ field.name }} => Src.{{ field.name }}{{ "," if not loop.last }}
{% endif %}
{% endfor %}
);
end Unpack;
{% endif %}

end {{ name }}.C;
22 changes: 12 additions & 10 deletions gen/templates/record/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 Down Expand Up @@ -37,12 +34,17 @@ package {{ name }}.C is
-- Access type for U_C.
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 a52fa38

Please sign in to comment.