comp.lang.ada
 help / color / mirror / Atom feed
* Linking Ada to C++ Overloaded Functions and Methods
@ 2018-04-07 18:35 Ron Wills
  2018-04-07 19:01 ` Dan'l Miller
  2018-04-07 19:05 ` Niklas Holsti
  0 siblings, 2 replies; 5+ messages in thread
From: Ron Wills @ 2018-04-07 18:35 UTC (permalink / raw)


I know that both Ada and C++ can overload functions and methods. The problem I'm having is when I'm trying to link Ada to overload C++ functions. I know I could use different Ada names, but if it's possible to keep the same names in Ada it would save a lot of re-coding.

An example is from the FLTK library. The "draw_box" method from the Fl_Widget class is overloaded and I get the following from g++ -fdump-ada-spec.

procedure draw_box (this : access constant Fl_Widget'Class);
pragma Import (CPP, draw_box, "_ZNK9Fl_Widget8draw_boxEv");

procedure draw_box
  (this : access constant Fl_Widget'Class;
   t : Fl_Enumerations_H.Fl_Boxtype;
   c : Fl_Enumerations_H.Fl_Color);  -- /usr/include/Fl/Fl_Widget.H:181
pragma Import (CPP, draw_box, "_ZNK9Fl_Widget8draw_boxE10Fl_Boxtypej");

The C++ name mangling works to uniquely identify the C++ methods, but the compiler is choking on the Ada names as already being declared.

fl_fl_widget_h.ads:171:07: entity "draw_box" was previously imported
fl_fl_widget_h.ads:171:07: (pragma "Import" applies to all previous entities)
fl_fl_widget_h.ads:171:07: import not allowed for "draw_box" declared at line 164
fl_fl_widget_h.ads:181:07: entity "draw_box" was previously imported
fl_fl_widget_h.ads:181:07: (pragma "Import" applies to all previous entities)
fl_fl_widget_h.ads:181:07: import not allowed for "draw_box" declared at line 167

Is there a way to use overloaded Ada names with C++ or do I need to find a unique Ada name for every C++ overload method?


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

* Linking Ada to C++ Overloaded Functions and Methods
  2018-04-07 18:35 Linking Ada to C++ Overloaded Functions and Methods Ron Wills
@ 2018-04-07 19:01 ` Dan'l Miller
  2018-04-07 19:05 ` Niklas Holsti
  1 sibling, 0 replies; 5+ messages in thread
From: Dan'l Miller @ 2018-04-07 19:01 UTC (permalink / raw)


Which single compiler toolchain are you using?

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

* Re: Linking Ada to C++ Overloaded Functions and Methods
  2018-04-07 18:35 Linking Ada to C++ Overloaded Functions and Methods Ron Wills
  2018-04-07 19:01 ` Dan'l Miller
@ 2018-04-07 19:05 ` Niklas Holsti
  2018-04-07 20:07   ` Ron Wills
  1 sibling, 1 reply; 5+ messages in thread
From: Niklas Holsti @ 2018-04-07 19:05 UTC (permalink / raw)


On 18-04-07 21:35 , Ron Wills wrote:
> I know that both Ada and C++ can overload functions and methods. The problem I'm having is when I'm trying to link Ada to overload C++ functions. I know I could use different Ada names, but if it's possible to keep the same names in Ada it would save a lot of re-coding.
>
> An example is from the FLTK library. The "draw_box" method from the Fl_Widget class is overloaded and I get the following from g++ -fdump-ada-spec.
>
> procedure draw_box (this : access constant Fl_Widget'Class);
> pragma Import (CPP, draw_box, "_ZNK9Fl_Widget8draw_boxEv");
>
> procedure draw_box
>   (this : access constant Fl_Widget'Class;
>    t : Fl_Enumerations_H.Fl_Boxtype;
>    c : Fl_Enumerations_H.Fl_Color);  -- /usr/include/Fl/Fl_Widget.H:181
> pragma Import (CPP, draw_box, "_ZNK9Fl_Widget8draw_boxE10Fl_Boxtypej");
>
> The C++ name mangling works to uniquely identify the C++ methods, but the compiler is choking on the Ada names as already being declared.
>
> fl_fl_widget_h.ads:171:07: entity "draw_box" was previously imported
> fl_fl_widget_h.ads:171:07: (pragma "Import" applies to all previous entities)
> fl_fl_widget_h.ads:171:07: import not allowed for "draw_box" declared at line 164
> fl_fl_widget_h.ads:181:07: entity "draw_box" was previously imported
> fl_fl_widget_h.ads:181:07: (pragma "Import" applies to all previous entities)
> fl_fl_widget_h.ads:181:07: import not allowed for "draw_box" declared at line 167
>
> Is there a way to use overloaded Ada names with C++ or do I need to find a unique Ada name for every C++ overload method?
>

I would try to change the pragma Imports into aspects, something like this:

procedure draw_box (....)
with Import,
      Convention => CPP,
      Link_Name => "_ZNK9Fl_Widget8draw_boxE10Fl_Boxtypej";

Aspects are syntactically tied to the subprogram declaration in which 
they occur, so overloaded subprograms with the same identifier can be 
given different aspect values, unlike for pragma Import.

Note that I'm not fully sure if you should use the Link_Name aspect or 
the External_Name aspect. The C++ identifiers given in the pragma 
Imports are External_Name values, but I think they are also Link_Name 
values.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


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

* Re: Linking Ada to C++ Overloaded Functions and Methods
  2018-04-07 19:05 ` Niklas Holsti
@ 2018-04-07 20:07   ` Ron Wills
  2018-04-07 20:25     ` Jere
  0 siblings, 1 reply; 5+ messages in thread
From: Ron Wills @ 2018-04-07 20:07 UTC (permalink / raw)


On Saturday, April 7, 2018 at 1:05:24 PM UTC-6, Niklas Holsti wrote:
> On 18-04-07 21:35 , Ron Wills wrote:
> > I know that both Ada and C++ can overload functions and methods. The problem I'm having is when I'm trying to link Ada to overload C++ functions. I know I could use different Ada names, but if it's possible to keep the same names in Ada it would save a lot of re-coding.
> >
> > An example is from the FLTK library. The "draw_box" method from the Fl_Widget class is overloaded and I get the following from g++ -fdump-ada-spec.
> >
> > procedure draw_box (this : access constant Fl_Widget'Class);
> > pragma Import (CPP, draw_box, "_ZNK9Fl_Widget8draw_boxEv");
> >
> > procedure draw_box
> >   (this : access constant Fl_Widget'Class;
> >    t : Fl_Enumerations_H.Fl_Boxtype;
> >    c : Fl_Enumerations_H.Fl_Color);  -- /usr/include/Fl/Fl_Widget.H:181
> > pragma Import (CPP, draw_box, "_ZNK9Fl_Widget8draw_boxE10Fl_Boxtypej");
> >
> > The C++ name mangling works to uniquely identify the C++ methods, but the compiler is choking on the Ada names as already being declared.
> >
> > fl_fl_widget_h.ads:171:07: entity "draw_box" was previously imported
> > fl_fl_widget_h.ads:171:07: (pragma "Import" applies to all previous entities)
> > fl_fl_widget_h.ads:171:07: import not allowed for "draw_box" declared at line 164
> > fl_fl_widget_h.ads:181:07: entity "draw_box" was previously imported
> > fl_fl_widget_h.ads:181:07: (pragma "Import" applies to all previous entities)
> > fl_fl_widget_h.ads:181:07: import not allowed for "draw_box" declared at line 167
> >
> > Is there a way to use overloaded Ada names with C++ or do I need to find a unique Ada name for every C++ overload method?
> >
> 
> I would try to change the pragma Imports into aspects, something like this:
> 
> procedure draw_box (....)
> with Import,
>       Convention => CPP,
>       Link_Name => "_ZNK9Fl_Widget8draw_boxE10Fl_Boxtypej";

That worked perfectly! Thanks!

> Aspects are syntactically tied to the subprogram declaration in which 
> they occur, so overloaded subprograms with the same identifier can be 
> given different aspect values, unlike for pragma Import.
> 
> Note that I'm not fully sure if you should use the Link_Name aspect or 
> the External_Name aspect. The C++ identifiers given in the pragma 
> Imports are External_Name values, but I think they are also Link_Name 
> values.
> 
> -- 
> Niklas Holsti
> Tidorum Ltd
> niklas holsti tidorum fi
>        .      @       .


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

* Re: Linking Ada to C++ Overloaded Functions and Methods
  2018-04-07 20:07   ` Ron Wills
@ 2018-04-07 20:25     ` Jere
  0 siblings, 0 replies; 5+ messages in thread
From: Jere @ 2018-04-07 20:25 UTC (permalink / raw)


On Saturday, April 7, 2018 at 4:07:38 PM UTC-4, Ron Wills wrote:
> On Saturday, April 7, 2018 at 1:05:24 PM UTC-6, Niklas Holsti wrote:
> > 
> > I would try to change the pragma Imports into aspects, something like this:
> > 
> > procedure draw_box (....)
> > with Import,
> >       Convention => CPP,
> >       Link_Name => "_ZNK9Fl_Widget8draw_boxE10Fl_Boxtypej";
> 
> That worked perfectly! Thanks!
> 

One gotcha that you may already be aware of, but just bit me today is
that if you have overloaded functions in a base class, but don't override
all of them in an extension class, you will get "undefined" errors when
calling the versions that are not overridden.  See:
https://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the

I was trying out Niklas' suggestion in my ada/cpp scratch space and had it
happen because I extended the ada tagged type and didn't override all of
the imports.


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

end of thread, other threads:[~2018-04-07 20:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-07 18:35 Linking Ada to C++ Overloaded Functions and Methods Ron Wills
2018-04-07 19:01 ` Dan'l Miller
2018-04-07 19:05 ` Niklas Holsti
2018-04-07 20:07   ` Ron Wills
2018-04-07 20:25     ` Jere

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