From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Convert between C "void*" pointer and an access Date: Thu, 12 Oct 2017 09:05:00 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="beb157ce27c63db07f25ae034f4baf42"; logging-data="4138"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5Fw+fG3szeCofLDYkpmoxLdIWO+WecLo=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (darwin) Cancel-Lock: sha1:rECj7Ru7TRUg/P8lYq4JuNEeAyo= sha1:qd89Ro8j1Voq+9c7CCnfqlrG7OM= Xref: news.eternal-september.org comp.lang.ada:48439 Date: 2017-10-12T09:05:00+01:00 List-Id: Victor Porton writes: > I NEED to convert between access to tagged records (and access to > class-wide types) and C void* pointers. > > I need this because it is C's way to pass objects by using void* and I > need to pass Ada objects to C functions (not just C objects). What we think you need to do is to pass a class-wide pointer to a C function so that it can do something with it. One would hope that you could say e.g. type T is tagged null record; type T_P is access all T'Class with Convention => C; procedure Put (Obj : T_P) -- <<< warning here with Import, Convention => C, External_Name => "demo_put"; function Get return T_P -- <<< warning here with Import, Convention => C, External_Name => "demo_get"; type U is new T with null record; O : aliased U; begin Put (O'Access); declare Ret : constant T_P := Get; begin Put_Line ("ret's class is " & Ada.Tags.Expanded_Name (Ret'Tag)); end; and that the compiler would refuse to compile the code if it couldn't map the type's conventions as specified. Trying this on macOS results in warnings class_pointers.adb:6:19: warning: "Put.Obj" involves a tagged type which does not correspond to any C type class_pointers.adb:11:13: warning: return type of "Get" does not correspond to C type but the resulting executable works as expected. (You would have expected a similar warning on the type definition; perhaps this is a bug?) In the past I've had success by specifying the size of the classwide type (this doesn't suppress the warnings).