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

-- 
           Summary: [Ada] - C++ interoperability sample program fails,
                    3.4.2, Linux 2.4.20-8, Red Hat 9.0
           Product: gcc
           Version: 3.4.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: ada
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: karl at grebyn dot com
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18085

Reply via email to