Bug 18085 - [Ada] - C++ interoperability sample program fails, 3.4.2, Linux 2.4.20-8, Red Hat 9.0
Summary: [Ada] - C++ interoperability sample program fails, 3.4.2, Linux 2.4.20-8, Red...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: ada (show other bugs)
Version: 4.0.0
: P2 minor
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-10-20 16:53 UTC by Karl Nyberg
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Karl Nyberg 2004-10-20 16:53:29 UTC
This program source is modified (additional print statements) from:
       
http://gcc.gnu.org/onlinedocs/gcc-3.4.2/gnat_ugn_unw/A-Simple-Example.html#A-Simple-Example

An example of interoperating C++ and Ada code.  The results seem to
indicate that the calls between the two languages are working OK, but
that the parameter passing isn't.  I guess that indicates something in the ABI,
perhaps?

The final value in A::method2 should be 3030, and the intermediate
values in the Ada calls are wrong.

$ gcc -v
Reading specs from /opt/lib/gcc/i686-pc-linux-gnu/3.4.2/specs
Configured with: ./configure --prefix=/opt --enable-languages=ada,c,c++
Thread model: posix
gcc version 3.4.2
$ uname -a
Linux ezekiel 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux
$ cat /etc/issue
Red Hat Linux release 9 (Shrike)
Kernel \r on an \m

$./cpp_main
in A::A, a_value = 1010
=== in A::method2, a_value was = 1010
in Ada_Method2, O_Value = 134582632
in Ada_Method2, A_Value =-1073749612
in A::method1, a_value = 2020
in Ada_Method2, O_Value = 134582632
in Ada_Method2, A_Value =-1073749612
in Ada_Method2, O_Value = 134582632
in Ada_Method2, A_Value = 3030
=== in A::method2, a_value = 2020
$ cat compile
#! /bin/csh
# compilation script to evidence the bug
gnatmake -c simple_cpp_interface
c++ -c cpp_main.C
c++ -c ex7.C
gnatbind -n simple_cpp_interface
gnatlink simple_cpp_interface -o cpp_main --LINK=c++  -lstdc++ ex7.o cpp_main.o
$ cat *.C *.h *.ad?
// cpp_main.C start

#include "ex7.h"
#include <stdio.h>

extern "C" {
  void adainit (void);
  void adafinal (void);
  void method1 (A *t);
}

void method1 (A *t)
{
t->method1 ();
}

int main ()
{
  A obj;
  adainit ();
  obj.method2 (3030);
  adafinal ();
}

// cpp_main.C end
//ex7.C start

#include "ex7.h"
#include <stdio.h>

extern "C" { void ada_method2 (A *t, int v);}

void A::method1 (void)
{
  a_value = 2020;
  printf ("in A::method1, a_value = %d \n",a_value);

}

void A::method2 (int v)
{
  printf ("=== in A::method2, a_value was = %d \n",a_value);
  ada_method2 (this, v);
  printf ("=== in A::method2, a_value = %d \n",a_value);

}

A::A(void)
{
  a_value = 1010;
  printf ("in A::A, a_value = %d \n",a_value);
}

//ex7.C end
// ex7.h start

class Origin {
 public:
  int o_value;
};

class A : public Origin {
 public:
  void method1 (void);
  virtual void method2 (int v);
  A();
  int a_value;
};

// ex7.h end
-- simple_cpp_interface.adb start
with Ada.Text_Io; use Ada.Text_Io;
package body Simple_Cpp_Interface is

procedure Ada_Method2 (This : in out A; V : Integer) is
   begin
      Ada.Text_Io.Put_Line ("in Ada_Method2, O_Value =" & Integer'Image
(This.O_Value));
      Ada.Text_Io.Put_Line ("in Ada_Method2, A_Value =" & Integer'Image
(This.A_Value));
      Method1 (This);
      Ada.Text_Io.Put_Line ("in Ada_Method2, O_Value =" & Integer'Image
(This.O_Value));
      Ada.Text_Io.Put_Line ("in Ada_Method2, A_Value =" & Integer'Image
(This.A_Value));
      This.A_Value := V;
      Ada.Text_Io.Put_Line ("in Ada_Method2, O_Value =" & Integer'Image
(This.O_Value));
      Ada.Text_Io.Put_Line ("in Ada_Method2, A_Value =" & Integer'Image
(This.A_Value));
   end Ada_Method2;

end Simple_Cpp_Interface;

-- simple_cpp_interface.adb end
-- simple_cpp_interface.ads start

package Simple_Cpp_Interface is
   type A is limited
      record
         O_Value : Integer;
         A_Value : Integer;
      end record;

pragma Convention (CPP, A);

procedure Method1 (This : in out A);
pragma Import (C, Method1);

procedure Ada_Method2 (This : in out A; V : Integer);
pragma Export (C, Ada_Method2);

end Simple_Cpp_Interface;

-- simple_cpp_interface.ads end
Comment 1 Karl Nyberg 2004-10-22 12:21:25 UTC
Confirmed still present in current snapshot for gcc 4.0

Reading specs from /home/karl/gnat/lib/gcc/i686-pc-linux-gnu/4.0.0/specs
Configured with: ../gcc/gcc-4.0-20041017/./configure --prefix=/home/karl/gnat
--enable-languages=c,c++,ada
Thread model: posix
gcc version 4.0.0 20041017 (experimental)
Comment 2 Karl Nyberg 2004-10-24 22:39:40 UTC
Problem is that the documentation has an incorrect specification of the interface.

By changing ex7.h to the following, the program works.
     
     //ex7.h
     
     class Origin {
      public:
       int a_value;
     };
     class A : public Origin {
      public:
       void method1 (void);
       virtual void method2 (int v);
       A();
       //       int   a_value;
     };

I leave it to the language lawyers to determine why some sort of error wasn't
raised before...
Comment 3 Arnaud Charlet 2004-10-29 13:28:02 UTC
I believe recent changes in Interfaces.CPP may have fixed things, if
there's indeed something to fix.

Could you verify ?

Arno
Comment 4 Arnaud Charlet 2004-11-25 13:38:55 UTC
No feedback received, although it's pretty clear that this is working
"as expected" on mainline, so closing.

Arno
Comment 5 Karl Nyberg 2004-11-25 14:28:57 UTC
Subject: Re:  [Ada] - C++ interoperability sample program fails, 3.4.2, Linux 2.4.20-8, Red Hat 9.0

I thought I updated that it appears to be a documentation issue...

-- Karl --
Comment 6 Arnaud Charlet 2004-11-25 14:51:05 UTC
<<I thought I updated that it appears to be a documentation issue...>>

Not the one you raised: you seemed to have confused o_value and
a_value, which are two different names and variables.

Now, I thought that recent changes to Interfaces.CPP may have
fixed this issue, although I am no longer sure this is the case,
so it would be interesting to have a confirmation, and re-open this
PR if there's still an issue.

Of course, it might still be the case that the example in the documentation
is wrong, but in this case, the error would be on the Ada code, not on the
C++ code as far as I can see.

Arno
Comment 7 Karl Nyberg 2004-11-25 14:59:18 UTC
Subject: Re:  [Ada] - C++ interoperability sample program fails, 3.4.2, Linux 2.4.20-8, Red Hat 9.0

I will download the latest 4.0 snapshot and take another look.

-- Karl --
Comment 8 Karl Nyberg 2004-11-25 15:10:25 UTC
Subject: Re:  [Ada] - C++ interoperability sample program fails, 3.4.2, Linux 2.4.20-8, Red Hat 9.0

The latest 4.0 snapshot won't build thanks to a breakage in the Ada.  But
the example in the documentation works correctly now.  Maybe I goofed up the
o_ and the a_ by manually copying.  I saved straight from the html page and
it did indeed work (with gcc 3.4.3).

-- Karl --