comp.lang.ada
 help / color / mirror / Atom feed
* How to bind this properly, C ** which is an array
@ 2019-03-01 13:48 Lucretia
  2019-03-01 13:54 ` Björn Lundin
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Lucretia @ 2019-03-01 13:48 UTC (permalink / raw)


Hi,

I'm trying to bind the following struct:

typedef struct mpc_ast_t {
  char *tag;
  char *contents;
  mpc_state_t state;
  int children_num;
  struct mpc_ast_t** children;
} mpc_ast_t;

I have this:

   type ASTs is
   record
      Tag                : C.Strings.chars_ptr;
      Contents           : C.Strings.chars_ptr;
      State              : States;
      Number_Of_Children : C.int;
      Children           : System.Address; --  Pointer to a pointer to an array of ASTs.
   end record with
     Convention => C;

Children needs to be accessed in code as a pointer to a pointer to an array of ASTs.

How do I best bind this thing? It's likely going to have to be converted from Address to a particular type using Unchecked_Conversion most likely.

Thanks,
Luke.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 13:48 How to bind this properly, C ** which is an array Lucretia
@ 2019-03-01 13:54 ` Björn Lundin
  2019-03-01 13:59   ` Lucretia
  2019-03-01 14:02 ` Dmitry A. Kazakov
  2019-03-01 19:57 ` Per Sandberg
  2 siblings, 1 reply; 9+ messages in thread
From: Björn Lundin @ 2019-03-01 13:54 UTC (permalink / raw)


On 2019-03-01 14:48, Lucretia wrote:
> Hi,
> 
> I'm trying to bind the following struct:
> 
> typedef struct mpc_ast_t {
>   char *tag;
>   char *contents;
>   mpc_state_t state;
>   int children_num;
>   struct mpc_ast_t** children;
> } mpc_ast_t;
> 
> I have this:
> 
>    type ASTs is
>    record
>       Tag                : C.Strings.chars_ptr;
>       Contents           : C.Strings.chars_ptr;
>       State              : States;
>       Number_Of_Children : C.int;
>       Children           : System.Address; --  Pointer to a pointer to an array of ASTs.
>    end record with
>      Convention => C;
> 
> Children needs to be accessed in code as a pointer to a pointer to an array of ASTs.
> 
> How do I best bind this thing? It's likely going to have to be converted from Address to a particular type using Unchecked_Conversion most likely.
> 
> Thanks,
> Luke.
> 

naïvly I would have tried something like

 type Ptr_To_Chars_Ptr is access all C.Strings.chars_ptr;

    type ASTs is
    record
       Tag                : C.Strings.chars_ptr;
       Contents           : C.Strings.chars_ptr;
       State              : States;
       Number_Of_Children : C.int;
       Children           : Ptr_To_Chars_Ptr;
    end record with
      Convention => C;


-- 
--
Björn


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 13:54 ` Björn Lundin
@ 2019-03-01 13:59   ` Lucretia
  0 siblings, 0 replies; 9+ messages in thread
From: Lucretia @ 2019-03-01 13:59 UTC (permalink / raw)


On Friday, 1 March 2019 13:54:32 UTC, björn lundin  wrote:
> On 2019-03-01 14:48, Lucretia wrote:
> > Hi,
> > 
> > I'm trying to bind the following struct:
> > 
> > typedef struct mpc_ast_t {
> >   char *tag;
> >   char *contents;
> >   mpc_state_t state;
> >   int children_num;
> >   struct mpc_ast_t** children;
> > } mpc_ast_t;
> > 
> > I have this:
> > 
> >    type ASTs is
> >    record
> >       Tag                : C.Strings.chars_ptr;
> >       Contents           : C.Strings.chars_ptr;
> >       State              : States;
> >       Number_Of_Children : C.int;
> >       Children           : System.Address; --  Pointer to a pointer to an array of ASTs.
> >    end record with
> >      Convention => C;
> > 
> > Children needs to be accessed in code as a pointer to a pointer to an array of ASTs.
> > 
> > How do I best bind this thing? It's likely going to have to be converted from Address to a particular type using Unchecked_Conversion most likely.
> > 
> > Thanks,
> > Luke.
> > 
> 
> naïvly I would have tried something like
> 
>  type Ptr_To_Chars_Ptr is access all C.Strings.chars_ptr;
> 
>     type ASTs is
>     record
>        Tag                : C.Strings.chars_ptr;
>        Contents           : C.Strings.chars_ptr;
>        State              : States;
>        Number_Of_Children : C.int;
>        Children           : Ptr_To_Chars_Ptr;
>     end record with
>       Convention => C;

But it's not a chars_ptr, it's an ASTs_Ptr_Ptr, effecitvely.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 13:48 How to bind this properly, C ** which is an array Lucretia
  2019-03-01 13:54 ` Björn Lundin
@ 2019-03-01 14:02 ` Dmitry A. Kazakov
  2019-03-01 14:20   ` Lucretia
  2019-03-01 19:57 ` Per Sandberg
  2 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2019-03-01 14:02 UTC (permalink / raw)


On 2019-03-01 14:48, Lucretia wrote:
> Hi,
> 
> I'm trying to bind the following struct:
> 
> typedef struct mpc_ast_t {
>    char *tag;
>    char *contents;
>    mpc_state_t state;
>    int children_num;
>    struct mpc_ast_t** children;
> } mpc_ast_t;
> 
> I have this:
> 
>     type ASTs is
>     record
>        Tag                : C.Strings.chars_ptr;
>        Contents           : C.Strings.chars_ptr;
>        State              : States;
>        Number_Of_Children : C.int;
>        Children           : System.Address; --  Pointer to a pointer to an array of ASTs.
>     end record with
>       Convention => C;
> 
> Children needs to be accessed in code as a pointer to a pointer to an array of ASTs.
> 
> How do I best bind this thing? It's likely going to have to be converted from Address to a particular type using Unchecked_Conversion most likely.

I think you could do it without Unchecked_Conversion:

    -- Forward declaration
    type ASTs;
    type ASTs_Ptr is access all ASTs;
    pragma Convention (C, ASTs_Ptr);

    -- Flat array of pointers without bounds
    type ASTs_Ptr_Array is array (size_t) of ASTs_Ptr;
    pragma Convention (C, ASTs_Ptr_Array);
    type ASTs_Ptr_Array_Ptr is access all ASTs_Ptr_Array;
    pragma Convention (C, ASTs_Ptr_Array_Ptr);

    -- Now the type
    type ASTs is record
       Tag                : C.Strings.chars_ptr;
       Contents           : C.Strings.chars_ptr;
       State              : States;
       Number_Of_Children : C.int;
       Children           : ASTs_Ptr_Array_Ptr;
    end record;
    pragma Convention (C, ASTs);

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 14:02 ` Dmitry A. Kazakov
@ 2019-03-01 14:20   ` Lucretia
  0 siblings, 0 replies; 9+ messages in thread
From: Lucretia @ 2019-03-01 14:20 UTC (permalink / raw)


On Friday, 1 March 2019 14:02:50 UTC, Dmitry A. Kazakov  wrote:

> I think you could do it without Unchecked_Conversion:

Thanks, this worked! :)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 13:48 How to bind this properly, C ** which is an array Lucretia
  2019-03-01 13:54 ` Björn Lundin
  2019-03-01 14:02 ` Dmitry A. Kazakov
@ 2019-03-01 19:57 ` Per Sandberg
  2019-03-01 20:25   ` Lucretia
  2 siblings, 1 reply; 9+ messages in thread
From: Per Sandberg @ 2019-03-01 19:57 UTC (permalink / raw)


Well i would start with:

gcc -c -fdump-ada-spec ${headerfile} [-fada-spec-parent=${a-good-root}]

to have the compiler do all the tricky work, if the headers are "bad" 
you might need to do som touch-ups but that could usually be done with 
some simple sed-scripts.

/P

On 3/1/19 2:48 PM, Lucretia wrote:
> Hi,
> 
> I'm trying to bind the following struct:
> 
> typedef struct mpc_ast_t {
>    char *tag;
>    char *contents;
>    mpc_state_t state;
>    int children_num;
>    struct mpc_ast_t** children;
> } mpc_ast_t;
> 
> I have this:
> 
>     type ASTs is
>     record
>        Tag                : C.Strings.chars_ptr;
>        Contents           : C.Strings.chars_ptr;
>        State              : States;
>        Number_Of_Children : C.int;
>        Children           : System.Address; --  Pointer to a pointer to an array of ASTs.
>     end record with
>       Convention => C;
> 
> Children needs to be accessed in code as a pointer to a pointer to an array of ASTs.
> 
> How do I best bind this thing? It's likely going to have to be converted from Address to a particular type using Unchecked_Conversion most likely.
> 
> Thanks,
> Luke.
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 19:57 ` Per Sandberg
@ 2019-03-01 20:25   ` Lucretia
  2019-03-01 22:02     ` Per Sandberg
  0 siblings, 1 reply; 9+ messages in thread
From: Lucretia @ 2019-03-01 20:25 UTC (permalink / raw)


On Friday, 1 March 2019 19:57:17 UTC, Per Sandberg  wrote:
> Well i would start with:
> 
> gcc -c -fdump-ada-spec ${headerfile} [-fada-spec-parent=${a-good-root}]
> 
> to have the compiler do all the tricky work, if the headers are "bad" 
> you might need to do som touch-ups but that could usually be done with 
> some simple sed-scripts.

I already did, it just generates what I originally had, Address.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 20:25   ` Lucretia
@ 2019-03-01 22:02     ` Per Sandberg
  2019-03-01 23:42       ` Lucretia
  0 siblings, 1 reply; 9+ messages in thread
From: Per Sandberg @ 2019-03-01 22:02 UTC (permalink / raw)


 From the source provided i would guss that the folowing is a correct 
spec-file.
------------------------------------------------------------
pragma Ada_2012;
pragma Style_Checks (Off);

with Interfaces.C; use Interfaces.C;
with Interfaces.C.Strings;

package xx_h is

    type mpc_state_ti is record
       null;
    end record
    with Convention => C_Pass_By_Copy;  -- xx.h:1

    subtype mpc_state_t is mpc_state_ti;  -- xx.h:1

    type mpc_ast_t;
    type mpc_ast_t_access is access all  mpc_ast_t;
    type mpc_ast_t is record
       tag : Interfaces.C.Strings.chars_ptr;  -- xx.h:4
       contents : Interfaces.C.Strings.chars_ptr;  -- xx.h:5
       state : aliased mpc_state_t;  -- xx.h:6
       children_num : aliased int;  -- xx.h:7
       children : access mpc_ast_t_access;  -- xx.h:8
    end record
    with Convention => C_Pass_By_Copy;  -- xx.h:3

end xx_h;
------------------------------------------------------------
/P

On 3/1/19 9:25 PM, Lucretia wrote:
> On Friday, 1 March 2019 19:57:17 UTC, Per Sandberg  wrote:
>> Well i would start with:
>>
>> gcc -c -fdump-ada-spec ${headerfile} [-fada-spec-parent=${a-good-root}]
>>
>> to have the compiler do all the tricky work, if the headers are "bad"
>> you might need to do som touch-ups but that could usually be done with
>> some simple sed-scripts.
> 
> I already did, it just generates what I originally had, Address.
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: How to bind this properly, C ** which is an array
  2019-03-01 22:02     ` Per Sandberg
@ 2019-03-01 23:42       ` Lucretia
  0 siblings, 0 replies; 9+ messages in thread
From: Lucretia @ 2019-03-01 23:42 UTC (permalink / raw)


On Friday, 1 March 2019 22:02:26 UTC, Per Sandberg  wrote:

Like I already told you, I ran it though gcc - http://dpaste.com/2R1ZWAP


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2019-03-01 23:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-01 13:48 How to bind this properly, C ** which is an array Lucretia
2019-03-01 13:54 ` Björn Lundin
2019-03-01 13:59   ` Lucretia
2019-03-01 14:02 ` Dmitry A. Kazakov
2019-03-01 14:20   ` Lucretia
2019-03-01 19:57 ` Per Sandberg
2019-03-01 20:25   ` Lucretia
2019-03-01 22:02     ` Per Sandberg
2019-03-01 23:42       ` Lucretia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox