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] New support for interfacing with C++ non-default constructors


This patch adds new support for interfacing with C++ non-default
constructors. It provides the basic functionality required for
object declaration but also provides support for record components
that are CPP_Class types and are initialized by means of limited
aggregates or through the Ada 2005 extended return statement. The
following test is an example of this new support.

//root.h
class Root {
public:
  Root();              // Default constructor
  Root(int v);         // Non-default constructor
};

//root.cpp
#include "root.h"
#include <iostream>
using namespace std;
Root::Root() {
  cout << "C++ default constructor" << endl;
}
Root::Root(int v) {
  cout << "C++ non-default constructor.Value=" << v << endl;
}

with Interfaces.C; use Interfaces.C;
package Pkg_CPP_Class is
   type Root is tagged limited null record;
   pragma Import (CPP, Root);

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

   function Constructor (v : Integer) return Root'Class;
   pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
end;

with Pkg_CPP_Class; use Pkg_CPP_Class;
procedure Test_Obj_Decl is
   pragma Warnings (Off);
   Obj : Root := Constructor (v => 10);
begin
   null;
end;

project Test_Obj_Decl is
   for Languages use ("Ada", "C++");
   for Main use ("test_obj_decl.adb");
   package Naming is
      for Specification_Suffix ("c++") use ".h";
      for Implementation_Suffix ("c++") use ".cpp";
   end Naming;
end Test_Obj_Decl;

Build with e.g. gprbuild -q -Ptest_obj_decl.gpr
Output:  C++ non-default constructor. Value=10

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

2009-04-29  Javier Miranda  <miranda@adacore.com>

	* sem_ch3.adb (Analyze_Object_Declaration): Disable error message
	associated with dyamically tagged expressions if the expression
	initializing a tagged type corresponds with a non default CPP
	constructor.
	(OK_For_Limited_Init): CPP constructor calls are OK for initialization
	of limited type objects.

	* sem_ch5.adb (Analyze_Assignment): Improve the error message reported
	when a CPP constructor is called in an assignment. Disable also the
	error message associated with dyamically tagged expressions if the
	exporession initializing a tagged type corresponds with a non default
	CPP constructor.

	* sem_prag.adb (Analyze_Pragma): Remove code disabling the use of
	non-default C++ constructors.

	* sem_util.ads, sem_util.adb (Is_CPP_Constructor_Call): New subprogram.

	* exp_tss.ads, exp_tss.adb (Base_Init_Proc): Add support for
	non-default constructors.
	(Init_Proc): Add support for non-default constructors.

	* exp_disp.adb (Set_Default_Constructor): Removed.
	(Set_CPP_Constructors): Code based in removed Set_Default_Constructor
	but extending its functionality to handle non-default constructors.

	* exp_aggr.adb (Build_Record_Aggr_Code): Add support for non-default
	constructors. Minor code cleanup removing unrequired label and goto
	statement.

	* exp_ch3.adb (Build_Initialization_Call): Add support for non-default
	constructors.
	(Build_Init_Statements): Add support for non-default constructors.
	(Expand_N_Object_Declaration): Add support for non-default constructors.
	(Freeze_Record_Type): Replace call to Set_Default_Constructor by call
	to Set_CPP_Constructors.

	* exp_ch5.adb (Expand_N_Assignment_Statement): Add support for
	non-default constructors.
	Required to handle its use in build-in-place statements.

	* gnat_rm.texi (CPP_Constructor): Document new extended use of this
	pragma for non-default C++ constructors and the new compiler support
	that allows the use of these constructors in record components, limited
	aggregates, and extended return statements.

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]