[m-dev.] diff: fix MLDS back-end C++ interface bug

Fergus Henderson fjh at cs.mu.OZ.AU
Sun Nov 19 16:18:50 AEDT 2000


Estimated hours taken: 0.75

compiler/mlds_to_c.m:
	Add a `extern "C"' wrapper around the generated header file,
	so that it can more easily be used from C++ programs.
	This change is needed to make the example in
	samples/c_interface/cplusplus_calls_mercury work in MLDS grades.
	Also output an "automatically generated ..." comment in the
	header file.

Workspace: /home/pgrad/fjh/ws/hg3
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.68
diff -u -d -r1.68 mlds_to_c.m
--- compiler/mlds_to_c.m	2000/11/18 20:13:01	1.68
+++ compiler/mlds_to_c.m	2000/11/19 05:17:50
@@ -40,7 +40,7 @@
 :- import_module rtti_to_mlds.	% for mlds_rtti_type_name.
 :- import_module hlds_pred.	% for pred_proc_id.
 :- import_module ml_code_util.	% for ml_gen_mlds_var_decl, which is used by
-				% the code that handles tail recursion.
+				% the code that handles derived classes
 :- import_module ml_type_gen.	% for ml_gen_type_name
 :- import_module export.	% for export__type_to_type_string
 :- import_module globals, options, passes_aux.
@@ -224,9 +224,6 @@
 
 mlds_output_src_file(Indent, MLDS) -->
 	{ MLDS = mlds(ModuleName, ForeignCode, Imports, Defns) },
-	{ library__version(Version) },
-	module_name_to_file_name(ModuleName, ".m", no, OrigFileName),
-	output_c_file_intro_and_grade(OrigFileName, Version),
 	mlds_output_src_start(Indent, ModuleName), io__nl,
 	mlds_output_src_imports(Indent, Imports), io__nl,
 	mlds_output_c_decls(Indent, ForeignCode), io__nl,
@@ -271,7 +268,7 @@
 :- mode mlds_output_hdr_start(in, in, di, uo) is det.
 
 mlds_output_hdr_start(Indent, ModuleName) -->
-	% XXX should spit out an "automatically generated by ..." comment
+	mlds_output_auto_gen_comment(ModuleName),
 	mlds_indent(Indent),
 	io__write_string("/* :- module "),
 	prog_out__write_sym_name(ModuleName),
@@ -289,6 +286,23 @@
 	io__write_string(MangledModuleName),
 	io__nl,
 	io__nl,
+	% 
+	% If we're outputting C (rather than C++), then add a
+	% conditional `extern "C"' wrapper around the header file,
+	% so that the header file can be #included by C++ programs.
+	% 
+	globals__io_get_target(Target),
+	( { Target = c } ->
+		mlds_indent(Indent),
+		io__write_string("#ifdef __cplusplus\n"),
+		mlds_indent(Indent),
+		io__write_string("extern ""C"" {\n"),
+		mlds_indent(Indent),
+		io__write_string("#endif\n"),
+		io__nl
+	;
+		[]
+	),
 	mlds_indent(Indent),
 	io__write_string("#include ""mercury.h""\n").
 
@@ -297,7 +311,7 @@
 :- mode mlds_output_src_start(in, in, di, uo) is det.
 
 mlds_output_src_start(Indent, ModuleName) -->
-	% XXX should spit out an "automatically generated by ..." comment
+	mlds_output_auto_gen_comment(ModuleName),
 	mlds_indent(Indent),
 	io__write_string("/* :- module "),
 	prog_out__write_sym_name(ModuleName),
@@ -323,6 +337,19 @@
 :- mode mlds_output_hdr_end(in, in, di, uo) is det.
 
 mlds_output_hdr_end(Indent, ModuleName) -->
+	globals__io_get_target(Target),
+	( { Target = c } ->
+		% terminate the `extern "C"' wrapper
+		mlds_indent(Indent),
+		io__write_string("#ifdef __cplusplus\n"),
+		mlds_indent(Indent),
+		io__write_string("}\n"),
+		mlds_indent(Indent),
+		io__write_string("#endif\n"),
+		io__nl
+	;
+		[]
+	),
 	mlds_indent(Indent),
 	io__write_string("#endif /* MR_HEADER_GUARD_"),
 	prog_out__write_sym_name(ModuleName),
@@ -342,6 +369,19 @@
 	io__write_string("/* :- end_module "),
 	prog_out__write_sym_name(ModuleName),
 	io__write_string(". */\n").
+
+	%
+	% Output a C comment saying that the file was automatically
+	% generated (and giving details such as the compiler version).
+	%
+:- pred mlds_output_auto_gen_comment(module_name::in,
+		io__state::di, io__state::uo) is det.
+
+mlds_output_auto_gen_comment(ModuleName) -->
+	{ library__version(Version) },
+	module_name_to_file_name(ModuleName, ".m", no, SourceFileName),
+	output_c_file_intro_and_grade(SourceFileName, Version),
+	io__nl.
 
 %-----------------------------------------------------------------------------%
 

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list