diff --git a/compiler/mlds_to_ilasm.m b/compiler/mlds_to_ilasm.m index 32945f246..e3f5d21fe 100644 --- a/compiler/mlds_to_ilasm.m +++ b/compiler/mlds_to_ilasm.m @@ -34,6 +34,7 @@ :- import_module hlds__hlds_pred. % for `pred_proc_id'. :- import_module parse_tree__prog_data, parse_tree__prog_out. :- import_module backend_libs__rtti, check_hlds__type_util, hlds__error_util. +:- import_module backend_libs__foreign. :- import_module ml_backend__ilds, ml_backend__ilasm, ml_backend__il_peephole. :- import_module ml_backend__ml_util, ml_backend__ml_code_util. @@ -70,20 +71,30 @@ output_mlds(MLDS) --> output_foreign_file(MLDS, ForeignLang) --> { ModuleName = mlds__get_module_name(MLDS) }, - { handle_foreign_lang(ForeignLang, Extension, CodeGenerator) }, - module_name_to_file_name(ModuleName, Extension, yes, File), - output_to_file(File, (pred(di, uo) is det --> CodeGenerator(MLDS))). + ( + { ForeignModuleName = foreign_language_module_name(ModuleName, + ForeignLang) }, + { Extension = foreign_language_file_extension(ForeignLang) } + -> + { handle_foreign_lang(ForeignLang, CodeGenerator) }, + module_name_to_file_name(ForeignModuleName, Extension, + yes, File), + output_to_file(File, + (pred(di, uo) is det --> CodeGenerator(MLDS))) + ; + { error( + "mlds_to_ilasm__output_foreign_file: unexpected language") } + ). -:- pred handle_foreign_lang(foreign_language::in, string::out, +:- pred handle_foreign_lang(foreign_language::in, pred(mlds, io__state, io__state)::out(pred(in, di, uo) is det)) is det. -handle_foreign_lang(managed_cplusplus, "__cpp_code.cpp", - output_managed_code(managed_cplusplus)). -handle_foreign_lang(csharp, "__csharp_code.cs", output_managed_code(csharp)). -handle_foreign_lang(c, _, _) :- +handle_foreign_lang(managed_cplusplus, output_managed_code(managed_cplusplus)). +handle_foreign_lang(csharp, output_managed_code(csharp)). +handle_foreign_lang(c, _) :- sorry(this_file, "language C foreign code not supported"). -handle_foreign_lang(il, _, _) :- +handle_foreign_lang(il, _) :- sorry(this_file, "language IL foreign code not supported"). % diff --git a/tests/valid/Mmakefile b/tests/valid/Mmakefile index a10d7fd0f..cc05c7e9e 100644 --- a/tests/valid/Mmakefile +++ b/tests/valid/Mmakefile @@ -46,6 +46,7 @@ RLO_PROGS= \ # Tests for which we should only produce a `.il' file. IL_PROGS = \ + csharp_hello \ foreign_type_spec OTHER_PROGS= \ diff --git a/tests/valid/csharp_hello.m b/tests/valid/csharp_hello.m new file mode 100644 index 000000000..3597217a4 --- /dev/null +++ b/tests/valid/csharp_hello.m @@ -0,0 +1,12 @@ +:- module csharp_hello. +:- interface. +:- import_module io. +:- pred main(io::di, io::uo) is det. + +:- implementation. + +:- pragma foreign_proc("C#", main(_IO0::di,_IO::uo), + [will_not_call_mercury, promise_pure], +" + System.Console.WriteLine(""Hello Mercury/C# world""); +").