This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Adding implicit call to C++ constructor in aggregates


This patch adds support for C++ constructors in extension aggregates.
When the ancestor part of the aggregate is a call to a C++ constructor
the GNAT frontend generates an implicit call to the corresponding C++
constructor to ensure its correct initialization. After this patch
the following test compiles and executes silently.

//root.h
class Root {
public:
  int  o_value;
  int  a_value;
  virtual void prim();
  Root();
};

//root.cpp
#include "root.h"
using namespace std;
Root::Root() {
  o_value = 998;
  a_value = 999;
}
void Root::prim() {
}

with Interfaces.C; use Interfaces.C;
package Pkg_CPP_Class is
   type Root is tagged limited record
      O_Value : int;
      A_Value : int;
   end record;
   pragma Import (CPP, Root);

   procedure Prim (Obj : Root);
   pragma Import (CPP, Prim);

   function Constructor return Root'Class;
   pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
end Pkg_CPP_Class;

package Pkg_CPP_Class.Child is
   type DT is new Pkg_CPP_Class.Root with record
      B_Value : Natural;
   end record;
end Pkg_CPP_Class.Child;

with Interfaces.C;        use Interfaces.C;
with Pkg_CPP_Class;       use Pkg_CPP_Class;
with Pkg_CPP_Class.Child; use Pkg_CPP_Class.Child;
with Ada.Text_IO;         use Ada.Text_IO;

procedure Do_Test is
  Obj : DT := (Constructor with B_Value => 1002);
begin
  if Obj.O_Value /= 998
     or Obj.A_Value /= 999
     or Obj.B_Value /= 1002
  then
     Put_Line ("Test failed");
  end if;
end;

project Do_Test is
   for Languages use ("Ada", "C++");
   for Main use ("do_test.adb");

   package Compiler is
      for Default_Switches ("ada") use ("-gnat05");
   end Compiler;

   package Naming is
      for Specification_Suffix ("c++") use ".h";
      for Implementation_Suffix ("c++") use ".cpp";
   end Naming;

   for Exec_Dir use ".";
end Do_Test;

Tested on x86_64-pc-linux-gnu, committed on trunk

2009-05-06  Javier Miranda  <miranda@adacore.com>

	* sem_aggr.adb (Valid_Ancestor_Type): Add support for C++ constructors.
	(Resolve_Extension_Aggregate): Do not reject C++ constructors in
	extension aggregates.
	(Resolve_Record_Aggregate): Add support for C++ constructors in
	extension aggregates.

	* exp_aggr.adb (Build_Record_Aggr_Code): Add support for C++
	constructors in extension aggregates.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]