[m-rev.] for review: fix foreign_types and pragma export

Peter Ross pro at missioncriticalit.com
Tue Jul 23 20:11:48 AEST 2002


On Sun, Jul 21, 2002 at 12:52:57PM +1000, Simon Taylor wrote:
> Where is the relative diff for the change you committed?
> 
from export.m

                ( { MaybeForeignDecls = yes(ForeignDecls) } ->
-                       ( { HighLevelCode = yes } ->
-                               module_name_to_file_name(ModuleName,
-                                       ".mih", no, MIHName),
-                               io__write_strings(
-                                       ["#include """, MIHName, """\n"])
-                       ;
-                               list__foldl(output_foreign_decl, ForeignDecls)
-                       )
+                       list__foldl(output_foreign_decl, ForeignDecls)
                ;
                        []
                ),
> A much better way to deal with this would be to write the
> foreign_decls to both the `.mh' file (without line numbers) and
> the C or `.mih' file (with line numbers). That would allow errors
> to be reported with the correct line numbers when compiling the
> module containing the foreign_decl, but wouldn't cause unnecessary
> recompilation of the C code for importing modules.
> 

===================================================================


Estimated hours taken: 2
Branches: main

Place the decls both in the .mh file and in either the .c (LLDS) or .mih (HLDS) file protected with a #ifndef/#define block.

compiler/export.m:
	Don't output #line blocks around the decls so as to avoid unnecessary
	changes to the .mh file and redundant compilations.

compiler/foreign.m:
	Add the function decl_guard which returns the string that is used to
	protect the declarations from being defined twice.

compiler/export.m:
compiler/mercury_compile.m:
compiler/mlds_to_c.m:
	Protect all the declarations with a #ifndef/#define block.


Index: compiler/export.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/export.m,v
retrieving revision 1.58
diff -u -r1.58 export.m
--- compiler/export.m	19 Jul 2002 10:40:17 -0000	1.58
+++ compiler/export.m	23 Jul 2002 08:51:13 -0000
@@ -70,7 +70,7 @@
 :- import_module parse_tree__modules.
 :- import_module hlds__hlds_pred, check_hlds__type_util.
 :- import_module hlds__error_util.
-:- import_module backend_libs__code_model, backend_libs__c_util.
+:- import_module backend_libs__code_model.
 :- import_module ll_backend__code_gen, ll_backend__code_util.
 :- import_module ll_backend__llds_out, ll_backend__arg_info.
 :- import_module libs__globals, libs__options.
@@ -677,7 +677,10 @@
 			"\n"]),
 
 		( { MaybeForeignDecls = yes(ForeignDecls) } ->
-			list__foldl(output_foreign_decl, ForeignDecls)
+			io__write_strings(["#ifndef ", decl_guard(ModuleName),
+				"\n#define", decl_guard(ModuleName), "\n"]),
+			list__foldl(output_foreign_decl, ForeignDecls),
+			io__write_string("#endif\n")
 		;
 			[]
 		),
@@ -726,14 +729,10 @@
 
 :- pred output_foreign_decl(foreign_decl_code::in, io::di, io::uo) is det.
 
-export__output_foreign_decl(foreign_decl_code(Lang, Code, Context)) -->
+export__output_foreign_decl(foreign_decl_code(Lang, Code, _Context)) -->
 	( { Lang = c } ->
-		{ term__context_file(Context, FileName) },
-		{ term__context_line(Context, LineNumber) },
-		c_util__set_line_num(FileName, LineNumber),
 		io__write_string(Code),
-		io__nl,
-		c_util__reset_line_num
+		io__nl
 	;
 		[]
 	).
Index: compiler/foreign.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/foreign.m,v
retrieving revision 1.18
diff -u -r1.18 foreign.m
--- compiler/foreign.m	19 Jul 2002 10:40:17 -0000	1.18
+++ compiler/foreign.m	23 Jul 2002 08:51:13 -0000
@@ -193,6 +193,10 @@
 :- mode foreign_language_module_name(in, in) = out is semidet.
 :- mode foreign_language_module_name(in, in(lang_gen_ext_file)) = out is det.
 
+	% The name of the #define which can be used to guard declarations with
+	% to prevent entities being declared twice.
+:- func decl_guard(sym_name) = string.
+
 :- implementation.
 
 :- import_module list, map, assoc_list, std_util, string, varset, int, term.
@@ -686,6 +690,14 @@
 	).
 to_type_string(il, mercury(_Type)) = _ :-
 	sorry(this_file, "to_type_string for il").
+
+%-----------------------------------------------------------------------------%
+
+:- import_module ll_backend__llds_out.
+
+decl_guard(ModuleName) = UppercaseModuleName ++ "_DECL_GUARD" :-
+	llds_out__sym_name_mangle(ModuleName, MangledModuleName),
+	string__to_upper(MangledModuleName, UppercaseModuleName).
 
 %-----------------------------------------------------------------------------%
 
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.259
diff -u -r1.259 mercury_compile.m
--- compiler/mercury_compile.m	22 Jul 2002 07:12:58 -0000	1.259
+++ compiler/mercury_compile.m	23 Jul 2002 08:51:14 -0000
@@ -3283,7 +3283,7 @@
 :- mode mercury_compile__construct_c_file(in, in, in, in, in, out, out, di, uo)
 	is det.
 
-mercury_compile__construct_c_file(Module,
+mercury_compile__construct_c_file(_Module,
 		C_InterfaceInfo, Procedures, GlobalVars,
 		AllData, CFile, ComponentCount) -->
 	{ C_InterfaceInfo = foreign_interface_info(ModuleSymName,
@@ -3306,14 +3306,8 @@
 	list__map_foldl(make_foreign_import_header_code, C_Includes,
 		C_HeaderCode1),
 
-		% If the current module contains a foreign_type then all the
-		% declarations will be placed into the header (.mh) file, so
-		% only keep the foreign imports.
-	{ module_info_contains_foreign_type(Module) ->
-		C_HeaderCode = C_HeaderCode1
-	;
-		C_HeaderCode = C_HeaderCode0 ++ C_HeaderCode1
-	},
+	{ make_decl_guards(ModuleSymName, Start, End) },
+	{ C_HeaderCode = [End | C_HeaderCode0] ++ [Start | C_HeaderCode1] },
 
 	{ CFile = c_file(ModuleSymName, C_HeaderCode, C_BodyCode,
 		C_ExportDefns, GlobalVars, AllData, ChunkedModules) },
@@ -3324,6 +3318,16 @@
 	{ list__length(ChunkedModules, CompGenCodeCount) },
 	{ ComponentCount is UserCCodeCount + ExportCount
 		+ CompGenVarCount + CompGenDataCount + CompGenCodeCount }.
+
+:- pred make_decl_guards(sym_name::in,
+		foreign_decl_code::out, foreign_decl_code::out) is det.
+
+make_decl_guards(ModuleName, StartGuard, EndGuard) :-
+	Define = decl_guard(ModuleName),
+	Start = "#ifndef " ++ Define ++ "\n#define " ++ Define ++ "\n",
+	End = "#endif",
+	StartGuard = foreign_decl_code(c, Start, term__context_init),
+	EndGuard = foreign_decl_code(c, End, term__context_init).
 
 :- pred make_foreign_import_header_code(foreign_import_module,
 		foreign_decl_code, io__state, io__state).
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.132
diff -u -r1.132 mlds_to_c.m
--- compiler/mlds_to_c.m	21 Jun 2002 13:26:42 -0000	1.132
+++ compiler/mlds_to_c.m	23 Jul 2002 08:51:19 -0000
@@ -551,7 +551,10 @@
 	{ ForeignCode = mlds__foreign_code(RevHeaderCode, _RevImports,
 		_RevBodyCode, _ExportDefns) },
 	{ HeaderCode = list__reverse(RevHeaderCode) },
-	io__write_list(HeaderCode, "\n", mlds_output_c_hdr_decl(Indent)).
+	io__write_strings(["#ifndef ", decl_guard(ModuleName),
+		"\n#define", decl_guard(ModuleName), "\n"]),
+	io__write_list(HeaderCode, "\n", mlds_output_c_hdr_decl(Indent)),
+	io__write_string("#endif\n").
 
 :- pred mlds_output_c_hdr_decl(indent,
 	foreign_decl_code, io__state, io__state).

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list