From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!aioe.org!5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Messing with access types... Date: Mon, 28 Dec 2020 14:56:51 +0100 Organization: Aioe.org NNTP Server Message-ID: References: NNTP-Posting-Host: 5WHqCw2XxjHb2npjM9GYbw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.9.2 Xref: reader02.eternal-september.org comp.lang.ada:60965 List-Id: On 2020-12-28 12:43, Marek wrote: > On 28.12.2020 11:14, Dmitry A. Kazakov wrote: >> >> What are you trying to achieve? Because the code does not make much >> sense to me. >> >> P.S. If you are trying to do some C bindings flat arrays are easier >> choice than Interfaces.C.Pointers. C has no arrays, but pointers. Ada's >> arrays are never compatible with any pointers, except when flat. >> Therefore using access to String would be wrong in most, if not all, cases. > > Code is a fragment some plugin framework for host written in C. When > plugin is initialized it receives pointer to host features. Then I can > scan them to find concrete feature (Id) and retrieve data. > Problem is with instantiation generic function Get_Data, more precisely > in line: > > return T_Access (ATA.To_Pointer (F.Data)); You should never convert access types, because to do so, you must first learn the accessibility rules. No man can do that while keeping sanity. So, just do this: return ATA.To_Pointer (F.Data).all'Unchecked_Access; -- Mind your business, COMPILER! > OK, some changes to code to clear view: > > with System; > > package Util is > > type Feature is record > Id : Integer; > Data : System.Address; > end record; type Feature is record Id : Interfaces.C.int; -- If that is int Data : System.Address; -- Could be a named access type [*] end record; pragma Convention (C, Feature); > type Feature_Access is access all Feature; > > Null_Feature : constant Feature := (0, System.Null_Address); > type Feature_Array is array (Natural range <>) of aliased Feature; type Feature_Array is array (size_t) of aliased Feature; -- Flat pragma Convention (C, Feature_Array); type Feature_Array_Ptr is access all Feature_Array; pragma Convention (C, Feature_Array_Ptr); function Get_Me_Features return Feature_Array_Ptr; pragma Import (C, Get_Me_features, "???"); That should be all necessary to do this: List : Feature_Array renames Get_Me_Features.all; begin for I in size_t'Range loop exit when List (I) = Null_Feature; declare Data : Whatever_C_Type; for Data'Address use List (I).Data; pragma Import (Ada, Data); begin ... end; end loop; ------------------------------------- * Here is a variant without using addresses type Whatever_C_Type is record ... end record; pragma Convention (C, Whatever_C_Type); type Whatever_C_Type_Ptr is access all Whatever_C_Type; pragma Convention (C, Whatever_C_Type_Ptr); type Feature is record Id : Interfaces.C.int; Data : Whatever_Ptr; end record; pragma Convention (C, Feature); ... for I in size_t'Range loop exit when List (I) = Null_Feature; declare Data : Whatever_C_Type renames List (I).Data.all; begin ... end; end loop; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de