[m-rev.] diff: support foreign_decls in erlang backend

Peter Wang wangp at students.csse.unimelb.edu.au
Tue Jun 19 11:12:26 AEST 2007


Estimated hours taken: 0.25
Branches: main

Add support for :- pragma foreign_decl for Erlang backend.  This is needed for
writing -export() annotations for Erlang functions defined in :- pragma
foreign_code blocks.

compiler/elds.m:
	Add slot in elds structure to hold foreign_decls.

compiler/elds_to_erlang.m:
	Output foreign_decls.

compiler/erl_code_gen.m:
	Extract foreign_decls for Erlang.

compiler/mercury_compile.m:
	Conform to change in elds type.


Index: compiler/elds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/elds.m,v
retrieving revision 1.12
diff -u -r1.12 elds.m
--- compiler/elds.m	14 Jun 2007 01:50:28 -0000	1.12
+++ compiler/elds.m	19 Jun 2007 01:05:37 -0000
@@ -45,6 +45,9 @@
                 % The original Mercury module name.
                 elds_name           :: module_name,
 
+                % Foreign code declarations.
+                elds_foreign_decls  :: list(foreign_decl_code),
+
                 % Code defined in Erlang.
                 elds_foreign_bodies :: list(foreign_body_code),
 
Index: compiler/elds_to_erlang.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/elds_to_erlang.m,v
retrieving revision 1.19
diff -u -r1.19 elds_to_erlang.m
--- compiler/elds_to_erlang.m	18 Jun 2007 04:41:27 -0000	1.19
+++ compiler/elds_to_erlang.m	19 Jun 2007 01:05:37 -0000
@@ -79,8 +79,8 @@
     io::di, io::uo) is det.
 
 output_erl_file(ModuleInfo, ELDS, SourceFileName, !IO) :-
-    ELDS = elds(ModuleName, ForeignBodies, ProcDefns, ForeignExportDefns,
-        RttiDefns, InitPreds, FinalPreds),
+    ELDS = elds(ModuleName, ForeignDecls, ForeignBodies, ProcDefns,
+        ForeignExportDefns, RttiDefns, InitPreds, FinalPreds),
     AddMainWrapper = should_add_main_wrapper(ModuleInfo),
 
     % Output intro.
@@ -113,6 +113,9 @@
     % Useful for debugging.
     io.write_string("% -compile(export_all).\n", !IO),
 
+    % Output foreign declarations.
+    list.foldl(output_foreign_decl_code, ForeignDecls, !IO),
+
     % Write directives for mkinit_erl.
     ErlangModuleNameStr = erlang_module_name_to_str(ModuleName),
     (
@@ -350,6 +353,13 @@
 
 %-----------------------------------------------------------------------------%
 
+:- pred output_foreign_decl_code(foreign_decl_code::in, io::di, io::uo) is det.
+
+output_foreign_decl_code(foreign_decl_code(_Lang, _IsLocal, Code, _Context),
+        !IO) :-
+    io.write_string(Code, !IO),
+    io.nl(!IO).
+
 :- pred output_foreign_body_code(foreign_body_code::in, io::di, io::uo) is det.
 
 output_foreign_body_code(foreign_body_code(_Lang, Code, _Context), !IO) :-
Index: compiler/erl_code_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/erl_code_gen.m,v
retrieving revision 1.13
diff -u -r1.13 erl_code_gen.m
--- compiler/erl_code_gen.m	14 Jun 2007 03:52:48 -0000	1.13
+++ compiler/erl_code_gen.m	19 Jun 2007 01:05:37 -0000
@@ -23,8 +23,7 @@
 % TODO: (this is incomplete)
 % - contexts are ignored at the moment
 % - RTTI
-% - some scope types not yet supported
-% - trace runtime conditions
+% - support non-local foreign code declarations (e.g. for macros)
 %
 %-----------------------------------------------------------------------------%
 
@@ -81,19 +80,22 @@
 erl_code_gen(ModuleInfo, ELDS, !IO) :-
     module_info_get_name(ModuleInfo, ModuleName),
     erl_gen_preds(ModuleInfo, ProcDefns, !IO),
-    filter_erlang_foreigns(ModuleInfo, ForeignBodies, PragmaExports, !IO),
+    filter_erlang_foreigns(ModuleInfo, ForeignDecls, ForeignBodies,
+        PragmaExports, !IO),
     erl_gen_foreign_exports(ProcDefns, PragmaExports, ForeignExportDefns),
     % RTTI function definitions are added later by rtti_data_list_to_elds.
     RttiDefns = [],
     module_info_user_init_pred_procs(ModuleInfo, InitPredProcs),
     module_info_user_final_pred_procs(ModuleInfo, FinalPredProcs),
-    ELDS = elds(ModuleName, ForeignBodies, ProcDefns, ForeignExportDefns,
-        RttiDefns, InitPredProcs, FinalPredProcs).
+    ELDS = elds(ModuleName, ForeignDecls, ForeignBodies, ProcDefns,
+        ForeignExportDefns, RttiDefns, InitPredProcs, FinalPredProcs).
 
-:- pred filter_erlang_foreigns(module_info::in, list(foreign_body_code)::out,
-    list(pragma_exported_proc)::out, io::di, io::uo) is det.
+:- pred filter_erlang_foreigns(module_info::in, list(foreign_decl_code)::out,
+    list(foreign_body_code)::out, list(pragma_exported_proc)::out,
+    io::di, io::uo) is det.
 
-filter_erlang_foreigns(ModuleInfo, ForeignBodies, PragmaExports, !IO) :-
+filter_erlang_foreigns(ModuleInfo, ForeignDecls, ForeignBodies, PragmaExports,
+        !IO) :-
     globals.io_get_backend_foreign_languages(BackendForeignLanguages, !IO),
     ( BackendForeignLanguages = [lang_erlang] ->
         true
@@ -101,12 +103,16 @@
         unexpected(this_file,
             "erl_gen_foreign_code: foreign language other than Erlang")
     ),
+    module_info_get_foreign_decl(ModuleInfo, AllForeignDecls),
     module_info_get_foreign_body_code(ModuleInfo, AllForeignBodys),
     module_info_get_pragma_exported_procs(ModuleInfo, AllPragmaExports),
+    foreign.filter_decls(lang_erlang, AllForeignDecls, RevForeignDecls,
+        _OtherForeignDecls),
     foreign.filter_bodys(lang_erlang, AllForeignBodys, RevForeignBodies,
         _OtherForeignBodys),
     foreign.filter_exports(lang_erlang, AllPragmaExports, RevPragmaExports,
         _OtherForeignExports),
+    ForeignDecls = list.reverse(RevForeignDecls),
     ForeignBodies = list.reverse(RevForeignBodies),
     PragmaExports = list.reverse(RevPragmaExports).
 
Index: compiler/mercury_compile.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/mercury_compile.m,v
retrieving revision 1.441
diff -u -r1.441 mercury_compile.m
--- compiler/mercury_compile.m	8 Jun 2007 06:45:10 -0000	1.441
+++ compiler/mercury_compile.m	19 Jun 2007 01:05:37 -0000
@@ -5224,11 +5224,9 @@
     RttiDatas = TypeCtorRttiData ++ TypeClassInfoRttiData,
     ErlangRttiDatas = list.map(erlang_rtti_data(HLDS), RttiDatas),
 
-    ELDS0 = elds(ModuleName, ForeignBodies, Defns, FEDefns, RttiDefns0,
-        InitPreds, FinalPreds),
+    RttiDefns0 = ELDS0 ^ elds_rtti_funcs,
     rtti_data_list_to_elds(HLDS, ErlangRttiDatas, RttiDefns),
-    ELDS = elds(ModuleName, ForeignBodies, Defns, FEDefns,
-        RttiDefns0 ++ RttiDefns, InitPreds, FinalPreds).
+    ELDS = ELDS0 ^ elds_rtti_funcs := RttiDefns0 ++ RttiDefns.
 
 :- pred elds_to_erlang(module_info::in, elds::in, io::di, io::uo) is det.
 
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list