Complete the update of mlds_to_cs_*.m.

compiler/mlds_to_cs_file.m:
compiler/mlds_to_cs_func.m:
compiler/mlds_to_cs_stmt.m:
    Switch to using indent.m for indentation.

    Do not generate #line directives, even if their generation is enabled,
    in places where its scope contains nothing derived from the original
    Mercury source file.

    Include as many things as possible in calls to io.format, because
    doing so allows people reading the code can see how those items
    fit together.

compiler/mlds_to_cs_data.m:
    Delete an unneeded ZZZ.

compiler/mlds_to_cs_util.m:
    Delete the indent_line_after_context predicate, after the changes above
    have replaced all calls to it.

compiler/mlds_to_target_util.m:
    Add an indent string argument to maybe_output_pred_proc_id_comment,
    because without it, you can't make the comment line up with the code
    it is commenting on.

compiler/mlds_to_java_func.m:
    Conform to the change in mlds_to_target_util.m, and move the generated
    comment to a more logical place.
This commit is contained in:
Zoltan Somogyi
2023-05-12 13:15:44 +10:00
parent d4f3713227
commit 10cf2dcf5b
8 changed files with 210 additions and 241 deletions

View File

@@ -1047,7 +1047,6 @@ output_initializer_body_list_for_csharp(Info, Stream, Indent,
Indent, HeadInit, no, Suffix, !IO)
;
TailInits = [HeadTailInit | TailTailInits],
% ZZZ
output_initializer_body_for_csharp(Info, Stream, at_start_of_line,
Indent, HeadInit, no, ",", !IO),
output_initializer_body_list_for_csharp(Info, Stream,

View File

@@ -38,6 +38,7 @@
:- import_module libs.compiler_util.
:- import_module libs.file_util.
:- import_module libs.globals.
:- import_module libs.indent.
:- import_module mdbcomp.
:- import_module mdbcomp.sym_name.
:- import_module ml_backend.ml_global_data.
@@ -218,11 +219,11 @@ make_code_addr_map_for_csharp([SeqNum - CodeAddr | SeqNumsCodeAddrs],
list(mlds_import)::in, list(foreign_decl_code)::in,
list(mlds_function_defn)::in, list(string)::out, io::di, io::uo) is det.
output_src_start_for_csharp(Info, Stream, MercuryModuleName, _Imports,
output_src_start_for_csharp(Info, Stream, ModuleName, _Imports,
ForeignDecls, FuncDefns, Errors, !IO) :-
output_auto_gen_comment(Stream, Info ^ csoi_source_filename, !IO),
io.format(Stream, "// :- module %s.\n\n",
[s(sym_name_to_escaped_string(MercuryModuleName))], !IO),
[s(sym_name_to_escaped_string(ModuleName))], !IO),
% The close parenthesis for this open parenthesis is written out
% by output_src_end_for_csharp.
io.write_string(Stream, "namespace mercury {\n\n", !IO),
@@ -231,8 +232,7 @@ output_src_start_for_csharp(Info, Stream, MercuryModuleName, _Imports,
ForeignDecls, ForeignDeclResults, !IO),
list.filter_map(maybe_is_error, ForeignDeclResults, Errors),
mangle_sym_name_for_csharp(MercuryModuleName, module_qual, "__",
ClassName),
mangle_sym_name_for_csharp(ModuleName, module_qual, "__", ClassName),
% The close parenthesis for this open parenthesis is written out
% by output_src_end_for_csharp.
io.format(Stream, "public static class %s {\n", [s(ClassName)], !IO),
@@ -252,38 +252,39 @@ output_src_start_for_csharp(Info, Stream, MercuryModuleName, _Imports,
mercury_module_name::in, indent::in, list(string)::in, list(string)::in,
io::di, io::uo) is det.
output_static_constructor(Stream, MercuryModuleName, Indent,
output_static_constructor(Stream, ModuleName, Indent,
StaticConstructors, FinalPreds, !IO) :-
mangle_sym_name_for_csharp(MercuryModuleName, module_qual, "__",
ClassName),
output_n_indents(Stream, Indent, !IO),
io.format(Stream, "static %s() {\n", [s(ClassName)], !IO),
IndentStr = indent2_string(Indent),
Indent1Str = indent2_string(Indent + 1),
mangle_sym_name_for_csharp(ModuleName, module_qual, "__", ClassName),
io.format(Stream, "%sstatic %s() {\n", [s(IndentStr), s(ClassName)], !IO),
WriteCall =
( pred(MethodName::in, !.IO::di, !:IO::uo) is det :-
output_n_indents(Stream, Indent + 1, !IO),
io.format(Stream, "%s();\n", [s(MethodName)], !IO)
io.format(Stream, "%s%s();\n",
[s(Indent1Str), s(MethodName)], !IO)
),
list.foldl(WriteCall, StaticConstructors, !IO),
WriteFinal =
( pred(FinalPred::in, !.IO::di, !:IO::uo) is det :-
output_n_indents(Stream, Indent + 1, !IO),
list.foldl(io.write_string(Stream), [
"System.AppDomain.CurrentDomain.ProcessExit += ",
"(sender, ev) => ", FinalPred, "();\n"
], !IO)
io.format(Stream,
"%sSystem.AppDomain.CurrentDomain.ProcessExit +=\n",
[s(Indent1Str)], !IO),
io.format(Stream,
"%s (sender, ev) => %s();\n",
[s(Indent1Str), s(FinalPred)], !IO)
),
list.foldl(WriteFinal, FinalPreds, !IO),
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, "}\n", !IO).
io.format(Stream, "%s}\n", [s(IndentStr)], !IO).
:- pred write_main_driver_for_csharp(io.text_output_stream::in, indent::in,
string::in, io::di, io::uo) is det.
write_main_driver_for_csharp(Stream, Indent, ClassName, !IO) :-
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, "public static void Main(string[] args)\n", !IO),
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, "{\n", !IO),
IndentStr = indent2_string(Indent),
Indent1Str = indent2_string(Indent + 1),
io.format(Stream, "%spublic static void Main(string[] args)\n",
[s(IndentStr)], !IO),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
Body = [
"try {",
" library.ML_std_library_init();",
@@ -300,9 +301,8 @@ write_main_driver_for_csharp(Stream, Indent, ClassName, !IO) :-
" }",
"}"
],
list.foldl(write_indented_line(Stream, Indent + 1), Body, !IO),
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, "}\n", !IO).
list.foldl(write_indentstr_line(Stream, Indent1Str), Body, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO).
:- pred output_src_end_for_csharp(io.text_output_stream::in,
mercury_module_name::in, io::di, io::uo) is det.
@@ -359,22 +359,22 @@ output_csharp_body_code(Info, Stream, ForeignBodyCode, Res, !IO) :-
output_csharp_foreign_literal_or_include(Info, Stream, LiteralOrInclude,
Context, Res, !IO) :-
PrintLineNumbers = Info ^ csoi_foreign_line_numbers,
(
LiteralOrInclude = floi_literal(Code),
indent_line_after_context(Stream, Info ^ csoi_foreign_line_numbers,
Context, 0, !IO),
cs_output_context(Stream, PrintLineNumbers, Context, !IO),
io.write_string(Stream, Code, !IO),
Res = ok
;
LiteralOrInclude = floi_include_file(IncludeFileName),
SourceFileName = Info ^ csoi_source_filename,
make_include_file_path(SourceFileName, IncludeFileName, IncludePath),
cs_output_context(Stream, Info ^ csoi_foreign_line_numbers,
context(IncludePath, 1), !IO),
InitialFileContext = context(IncludePath, 1),
cs_output_context(Stream, PrintLineNumbers, InitialFileContext, !IO),
write_include_file_contents(Stream, IncludePath, Res, !IO)
),
io.nl(Stream, !IO),
cs_output_default_context(Stream, Info ^ csoi_foreign_line_numbers, !IO).
cs_output_default_context(Stream, PrintLineNumbers, !IO).
%---------------------------------------------------------------------------%
@@ -383,20 +383,20 @@ output_csharp_foreign_literal_or_include(Info, Stream, LiteralOrInclude,
io::di, io::uo) is det.
output_method_ptr_constants(Info, Stream, Indent, CodeAddrs, !IO) :-
map.foldl(output_method_ptr_constant(Info, Stream, Indent),
IndentStr = indent2_string(Indent),
map.foldl(output_method_ptr_constant(Info, Stream, IndentStr),
CodeAddrs, !IO).
:- pred output_method_ptr_constant(csharp_out_info::in,
io.text_output_stream::in, indent::in, mlds_code_addr::in, string::in,
io.text_output_stream::in, string::in, mlds_code_addr::in, string::in,
io::di, io::uo) is det.
output_method_ptr_constant(Info, Stream, Indent, CodeAddr, Name, !IO) :-
output_method_ptr_constant(Info, Stream, IndentStr, CodeAddr, Name, !IO) :-
CodeAddr = mlds_code_addr(_QualFuncLabel, Signature),
Signature = mlds_func_signature(ArgTypes, RetTypes),
TypeString = method_ptr_type_to_string(Info, ArgTypes, RetTypes),
output_n_indents(Stream, Indent, !IO),
io.format(Stream, "private static readonly %s %s = ",
[s(TypeString), s(Name)], !IO),
TypeNameStr = method_ptr_type_to_string(Info, ArgTypes, RetTypes),
io.format(Stream, "%sprivate static readonly %s %s = ",
[s(IndentStr), s(TypeNameStr), s(Name)], !IO),
IsCall = no,
mlds_output_code_addr_for_csharp(Info, Stream, CodeAddr, IsCall, !IO),
io.write_string(Stream, ";\n", !IO).
@@ -410,15 +410,15 @@ output_method_ptr_constant(Info, Stream, Indent, CodeAddr, Name, !IO) :-
indent::in, string::in, io::di, io::uo) is det.
output_env_var_definition_for_csharp(Stream, Indent, EnvVarName, !IO) :-
IndentStr = indent2_string(Indent),
% We use int because the generated code compares against zero, and changing
% that is more trouble than it is worth, as it affects the C backends.
output_n_indents(Stream, Indent, !IO),
io.format(Stream, "private static int mercury_envvar_%s =\n",
[s(EnvVarName)], !IO),
output_n_indents(Stream, Indent + 1, !IO),
io.format(Stream, "%sprivate static int mercury_envvar_%s =\n",
[s(IndentStr), s(EnvVarName)], !IO),
io.format(Stream,
"System.Environment.GetEnvironmentVariable(\"%s\") == null ? 0 : 1;\n",
[s(EnvVarName)], !IO).
"%s System.Environment.GetEnvironmentVariable(\"%s\") == null" ++
" ? 0 : 1;\n",
[s(IndentStr), s(EnvVarName)], !IO).
%---------------------------------------------------------------------------%
:- end_module ml_backend.mlds_to_cs_file.

View File

@@ -58,7 +58,7 @@ output_function_defn_for_csharp(Info, Stream, Indent, OutputAux,
FunctionDefn, !IO) :-
% Put a blank line before each function definition.
io.nl(Stream, !IO),
write_indent2(Stream, Indent, !IO),
IndentStr = indent2_string(Indent),
FunctionDefn = mlds_function_defn(FuncName, Context, Flags,
MaybePredProcId, Params, MaybeBody, _EnvVarNames,
_MaybeRequireTailrecInfo),
@@ -69,62 +69,36 @@ output_function_defn_for_csharp(Info, Stream, Indent, OutputAux,
% so just output the declaration as a comment.
% (Note that the actual definition of an external procedure
% must be given in `pragma foreign_code' in the same module.)
PreStr = "/* external:\n",
PostStr = "*/\n"
PreStr = IndentStr ++ "/* external:\n",
PostStr = IndentStr ++ "*/\n"
;
MaybeBody = body_defined_here(_),
PreStr = "",
PostStr = ""
),
io.write_string(Stream, PreStr, !IO),
output_function_decl_flags_for_csharp(Info, Stream, Flags, !IO),
(
MaybePredProcId = no
;
MaybePredProcId = yes(PredProcId),
maybe_output_pred_proc_id_comment(Stream, Info ^ csoi_auto_comments,
PredProcId, !IO)
IndentStr, PredProcId, !IO)
),
Flags = mlds_function_decl_flags(Access, PerInstance),
( Access = func_public, AccessPrefix = "public "
; Access = func_private, AccessPrefix = "private "
),
( PerInstance = per_instance, PerInstancePrefix = ""
; PerInstance = one_copy, PerInstancePrefix = "static "
),
io.format(Stream, "%s%s%s",
[s(IndentStr), s(AccessPrefix), s(PerInstancePrefix)], !IO),
output_func_for_csharp(Info, Stream, Indent, FuncName, OutputAux, Context,
Params, MaybeBody, !IO),
io.write_string(Stream, PostStr, !IO).
%---------------------------------------------------------------------------%
:- pred output_function_decl_flags_for_csharp(csharp_out_info::in,
io.text_output_stream::in, mlds_function_decl_flags::in,
io::di, io::uo) is det.
output_function_decl_flags_for_csharp(Info, Stream, Flags, !IO) :-
Flags = mlds_function_decl_flags(Access, PerInstance),
output_access_for_csharp(Info, Stream, Access, !IO),
output_per_instance_for_csharp(Stream, PerInstance, !IO).
:- pred output_access_for_csharp(csharp_out_info::in,
io.text_output_stream::in, function_access::in, io::di, io::uo) is det.
output_access_for_csharp(_Info, Stream, Access, !IO) :-
(
Access = func_public,
io.write_string(Stream, "public ", !IO)
;
Access = func_private,
io.write_string(Stream, "private ", !IO)
).
:- pred output_per_instance_for_csharp(io.text_output_stream::in,
per_instance::in, io::di, io::uo) is det.
output_per_instance_for_csharp(Stream, PerInstance, !IO) :-
(
PerInstance = per_instance
;
PerInstance = one_copy,
io.write_string(Stream, "static ", !IO)
).
%---------------------------------------------------------------------------%
:- pred output_func_for_csharp(csharp_out_info::in, io.text_output_stream::in,
indent::in, mlds_function_name::in, output_aux::in, prog_context::in,
mlds_func_params::in, mlds_function_body::in, io::di, io::uo) is det.
@@ -133,19 +107,20 @@ output_func_for_csharp(Info, Stream, Indent, FuncName, OutputAux, Context,
Signature, MaybeBody, !IO) :-
(
MaybeBody = body_defined_here(Body),
PrintLineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
output_func_decl_for_csharp(Info, Stream, Indent, FuncName, OutputAux,
Signature, !IO),
io.write_string(Stream, "\n", !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "{\n", !IO),
cs_output_context(Stream, PrintLineNumbers, Context, !IO),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
FuncInfo = func_info_csj(Signature),
output_stmt_for_csharp(Info, Stream, Indent + 1, FuncInfo, Body,
_ExitMethods, !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "}\n", !IO), % end the function
cs_output_default_context(Stream, Info ^ csoi_line_numbers, !IO)
% XXX What is this printed context for? Its scope is limited
% to just one close brace.
cs_output_context(Stream, PrintLineNumbers, Context, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO), % end the function
cs_output_default_context(Stream, PrintLineNumbers, !IO)
;
MaybeBody = body_external
).
@@ -164,7 +139,7 @@ output_func_decl_for_csharp(Info, Stream, Indent, FuncName, OutputAux,
ClassNameStr =
unqual_class_name_to_ll_string_for_csharp(ClassName, ClassArity),
ParamsStr = params_to_string_for_csharp(Info, Indent, Parameters),
io.format(Stream, "%s%s", [s(ClassNameStr), s(ParamsStr)], !IO)
io.format(Stream, "%s%s\n", [s(ClassNameStr), s(ParamsStr)], !IO)
else
FuncNameStr = function_name_to_ll_string_for_csharp(FuncName),
get_return_type_and_out_params_for_csharp(Info, RetTypes,
@@ -172,7 +147,7 @@ output_func_decl_for_csharp(Info, Stream, Indent, FuncName, OutputAux,
list.map_foldl(make_out_param, OutParamTypes, OutParams, 2, _),
ParamsStr = params_to_string_for_csharp(Info, Indent,
Parameters ++ OutParams),
io.format(Stream, "%s %s%s",
io.format(Stream, "%s %s%s\n",
[s(RetTypeStr), s(FuncNameStr), s(ParamsStr)], !IO)
).

View File

@@ -82,7 +82,6 @@ output_global_var_decls_for_csharp(Info, Stream, Indent,
[GlobalVarDefn | GlobalVarDefns], !IO) :-
GlobalVarDefn = mlds_global_var_defn(GlobalVarName, _Context, Flags0,
Type, _Initializer, _GCStmt),
% ZZZ move to caller
IndentStr = indent2_string(Indent),
% We can't honour _Constness here as the variable is assigned separately.
Flags0 = mlds_global_var_decl_flags(Access, _Constness),

View File

@@ -131,9 +131,11 @@ output_stmt_block_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
Stmt = ml_stmt_block(LocalVarDefns, FuncDefns, Stmts, Context),
BraceIndent = Indent,
BlockIndent = Indent + 1,
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
BraceIndent, !IO),
io.write_string(Stream, "{\n", !IO),
LineNumbers = Info ^ csoi_line_numbers,
BraceIndentStr = indent2_string(BraceIndent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s{\n", [s(BraceIndentStr)], !IO),
(
LocalVarDefns = [_ | _],
list.foldl(
@@ -155,9 +157,8 @@ output_stmt_block_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
),
output_stmts_for_csharp(Info, Stream, BlockIndent, FuncInfo, Stmts,
ExitMethods, !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
BraceIndent, !IO),
io.write_string(Stream, "}\n", !IO).
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s}\n", [s(BraceIndentStr)], !IO).
:- pred output_local_var_defn_for_csharp(csharp_out_info::in,
io.text_output_stream::in, indent::in, mlds_local_var_defn::in,
@@ -211,25 +212,24 @@ output_stmts_for_csharp(Info, Stream, Indent, FuncInfo, [Stmt | Stmts],
output_stmt_while_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
ExitMethods, !IO) :-
Stmt = ml_stmt_while(Kind, Cond, BodyStmt, _LoopLocalVars, Context),
LineNumbers = Info ^ csoi_line_numbers,
scope_indent(BodyStmt, Indent, ScopeIndent),
IndentStr = indent2_string(Indent),
(
Kind = may_loop_zero_times,
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "while (", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%swhile (", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Cond, Stream, !IO),
io.write_string(Stream, ")\n", !IO),
% The contained statement is reachable iff the while statement is
% reachable and the condition expression is not a constant
% expression whose value is false.
( if Cond = ml_const(mlconst_false) then
indent_line_after_context(Stream, Info ^ csoi_line_numbers,
Context, Indent, !IO),
io.write_string(Stream, "{\n", !IO),
output_n_indents(Stream, Indent + 1, !IO),
io.write_string(Stream, "/* Unreachable code */\n", !IO),
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, "}\n", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
io.format(Stream, "%s /* Unreachable code */\n",
[s(IndentStr)], !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO),
ExitMethods = set.make_singleton_set(can_fall_through)
else
BodyInfo = Info ^ csoi_break_context := bc_loop,
@@ -239,15 +239,13 @@ output_stmt_while_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
)
;
Kind = loop_at_least_once,
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "do\n", !IO),
BodyInfo = Info ^ csoi_break_context := bc_loop,
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sdo\n", [s(IndentStr)], !IO),
output_stmt_for_csharp(BodyInfo, Stream, ScopeIndent, FuncInfo,
BodyStmt, StmtExitMethods, !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "while (", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%swhile (", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Cond, Stream, !IO),
io.write_string(Stream, ");\n", !IO),
ExitMethods = while_exit_methods_for_csharp(Cond, StmtExitMethods)
@@ -286,6 +284,8 @@ while_exit_methods_for_csharp(Cond, BlockExitMethods) = ExitMethods :-
output_stmt_if_then_else_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
ExitMethods, !IO) :-
Stmt = ml_stmt_if_then_else(Cond, Then0, MaybeElse, Context),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
% We need to take care to avoid problems caused by the dangling else
% ambiguity.
( if
@@ -308,20 +308,18 @@ output_stmt_if_then_else_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
Then = Then0
),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "if (", !IO),
scope_indent(Then, Indent, ThenScopeIndent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sif (", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Cond, Stream, !IO),
io.write_string(Stream, ")\n", !IO),
scope_indent(Then, Indent, ThenScopeIndent),
output_stmt_for_csharp(Info, Stream, ThenScopeIndent, FuncInfo, Then,
ThenExitMethods, !IO),
(
MaybeElse = yes(Else),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "else\n", !IO),
scope_indent(Else, Indent, ElseScopeIndent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%selse\n", [s(IndentStr)], !IO),
output_stmt_for_csharp(Info, Stream, ElseScopeIndent, FuncInfo, Else,
ElseExitMethods, !IO),
% An if-then-else statement can complete normally iff the
@@ -345,17 +343,17 @@ output_stmt_if_then_else_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
output_stmt_switch_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
ExitMethods, !IO) :-
Stmt = ml_stmt_switch(_Type, Val, _Range, Cases, Default, Context),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "switch (", !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sswitch (", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Val, Stream, !IO),
io.write_string(Stream, ") {\n", !IO),
CaseInfo = Info ^ csoi_break_context := bc_switch,
output_switch_cases_for_csharp(CaseInfo, Stream, Indent + 1, FuncInfo,
Context, Cases, Default, ExitMethods, !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "}\n", !IO).
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO).
:- pred output_switch_cases_for_csharp(csharp_out_info::in,
io.text_output_stream::in, indent::in, func_info_csj::in, prog_context::in,
@@ -394,9 +392,10 @@ output_switch_case_for_csharp(Info, Stream, Indent, FuncInfo, Context, Case,
output_stmt_for_csharp(Info, Stream, Indent + 1, FuncInfo, Statement,
StmtExitMethods, !IO),
( if set.member(can_fall_through, StmtExitMethods) then
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent + 1, !IO),
io.write_string(Stream, "break;\n", !IO),
LineNumbers = Info ^ csoi_line_numbers,
Indent1Str = indent2_string(Indent + 1),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sbreak;\n", [s(Indent1Str)], !IO),
ExitMethods = set.delete(set.insert(StmtExitMethods, can_break),
can_fall_through)
else
@@ -411,9 +410,10 @@ output_switch_case_for_csharp(Info, Stream, Indent, FuncInfo, Context, Case,
output_case_cond_for_csharp(Info, Stream, Indent, Context, Match, !IO) :-
(
Match = match_value(Val),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "case ", !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%scase ", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Val, Stream, !IO),
io.write_string(Stream, ":\n", !IO)
;
@@ -432,23 +432,24 @@ output_switch_default_for_csharp(Info, Stream, Indent, FuncInfo, Context,
ExitMethods = set.make_singleton_set(can_fall_through)
;
Default = default_case(Statement),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "default:\n", !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sdefault:\n", [s(IndentStr)], !IO),
output_stmt_for_csharp(Info, Stream, Indent + 1, FuncInfo, Statement,
ExitMethods, !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "break;\n", !IO)
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sbreak;\n", [s(IndentStr)], !IO)
;
Default = default_is_unreachable,
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "default: /*NOTREACHED*/\n", !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent + 1, !IO),
io.write_string(Stream,
"throw new runtime.UnreachableDefault();\n", !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
io.format(Stream,
"%sdefault: /*NOTREACHED*/\n", [s(IndentStr)], !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream,
"%s throw new runtime.UnreachableDefault();\n",
[s(IndentStr)], !IO),
ExitMethods = set.make_singleton_set(can_throw)
).
@@ -462,6 +463,8 @@ output_switch_default_for_csharp(Info, Stream, Indent, FuncInfo, Context,
output_stmt_goto_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
ExitMethods, !IO) :-
Stmt = ml_stmt_goto(Target, Context),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
(
Target = goto_label(_),
unexpected($pred, "gotos not supported in C#.")
@@ -470,9 +473,8 @@ output_stmt_goto_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
BreakContext = Info ^ csoi_break_context,
(
BreakContext = bc_switch,
indent_line_after_context(Stream, Info ^ csoi_line_numbers,
Context, Indent, !IO),
io.write_string(Stream, "break;\n", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sbreak;\n", [s(IndentStr)], !IO),
ExitMethods = set.make_singleton_set(can_break)
;
( BreakContext = bc_none
@@ -485,9 +487,8 @@ output_stmt_goto_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
BreakContext = Info ^ csoi_break_context,
(
BreakContext = bc_loop,
indent_line_after_context(Stream, Info ^ csoi_line_numbers,
Context, Indent, !IO),
io.write_string(Stream, "break;\n", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sbreak;\n", [s(IndentStr)], !IO),
ExitMethods = set.make_singleton_set(can_break)
;
( BreakContext = bc_none
@@ -497,9 +498,8 @@ output_stmt_goto_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
)
;
Target = goto_continue_loop,
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "continue;\n", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%scontinue;\n", [s(IndentStr)], !IO),
ExitMethods = set.make_singleton_set(can_continue)
).
@@ -515,11 +515,13 @@ output_stmt_call_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
Stmt = ml_stmt_call(Signature, FuncRval, CallArgs, Results,
IsTailCall, Context),
Signature = mlds_func_signature(ArgTypes, RetTypes),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "{\n", !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent + 1, !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
Indent1Str = indent2_string(Indent + 1),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.write_string(Stream, Indent1Str, !IO),
(
Results = [],
OutArgs = []
@@ -534,11 +536,12 @@ output_stmt_call_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
output_call_rval_for_csharp(Info, FuncRval, Stream, !IO)
else
% This is a call using a method pointer.
TypeString = method_ptr_type_to_string(Info, ArgTypes, RetTypes),
io.format(Stream, "((%s) ", [s(TypeString)], !IO),
PtrTypeName = method_ptr_type_to_string(Info, ArgTypes, RetTypes),
io.format(Stream, "((%s) ", [s(PtrTypeName)], !IO),
output_call_rval_for_csharp(Info, FuncRval, Stream, !IO),
io.write_string(Stream, ")", !IO)
),
% XXX This list of rvals on one line can result in *very* long lines.
io.write_string(Stream, "(", !IO),
write_out_list(output_rval_for_csharp(Info), ", ", CallArgs ++ OutArgs,
Stream, !IO),
@@ -552,15 +555,13 @@ output_stmt_call_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
IsTailCall = tail_call
;
IsTailCall = no_return_call,
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent + 1, !IO),
io.write_string(Stream,
"throw new runtime.UnreachableDefault();\n", !IO)
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sthrow new runtime.UnreachableDefault();\n",
[s(Indent1Str)], !IO)
),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "}\n", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO),
ExitMethods = set.make_singleton_set(can_fall_through).
%---------------------------------------------------------------------------%
@@ -574,20 +575,20 @@ output_stmt_call_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
output_stmt_return_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
ExitMethods, !IO) :-
Stmt = ml_stmt_return(Results, Context),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
(
Results = [],
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "return;\n", !IO)
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sreturn;\n", [s(IndentStr)], !IO)
;
Results = [Rval | Rvals],
% The first return value is returned directly.
% Subsequent return values are assigned to out parameters.
list.foldl2(output_assign_out_params(Info, Stream, Indent),
Rvals, 2, _, !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "return ", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sreturn ", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Rval, Stream, !IO),
io.write_string(Stream, ";\n", !IO)
),
@@ -598,8 +599,8 @@ output_stmt_return_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
io::di, io::uo) is det.
output_assign_out_params(Info, Stream, Indent, Rval, Num, Num + 1, !IO) :-
output_n_indents(Stream, Indent, !IO),
io.format(Stream, "out_param_%d = ", [i(Num)], !IO),
IndentStr = indent2_string(Indent),
io.format(Stream, "%sout_param_%d = ", [s(IndentStr), i(Num)], !IO),
output_rval_for_csharp(Info, Rval, Stream, !IO),
io.write_string(Stream, ";\n", !IO).
@@ -614,13 +615,14 @@ output_assign_out_params(Info, Stream, Indent, Rval, Num, Num + 1, !IO) :-
output_stmt_do_commit_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
ExitMethods, !IO) :-
Stmt = ml_stmt_do_commit(Ref, Context),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.write_string(Stream, IndentStr, !IO),
output_rval_for_csharp(Info, Ref, Stream, !IO),
io.write_string(Stream, " = new runtime.Commit();\n", !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers,
Context, Indent, !IO),
io.write_string(Stream, "throw ", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%sthrow ", [s(IndentStr)], !IO),
output_rval_for_csharp(Info, Ref, Stream, !IO),
io.write_string(Stream, ";\n", !IO),
ExitMethods = set.make_singleton_set(can_throw).
@@ -635,25 +637,28 @@ output_stmt_do_commit_for_csharp(Info, Stream, Indent, _FuncInfo, Stmt,
output_stmt_try_commit_for_csharp(Info, Stream, Indent, FuncInfo, Stmt,
ExitMethods, !IO) :-
% XXX I (zs) reduced the number of times this code writes out the
% context (if LineNumbers = yes) from seven to four, but I think
% even the remaining set is probably more than is needed.
Stmt = ml_stmt_try_commit(_Ref, BodyStmt, HandlerStmt, Context),
LineNumbers = Info ^ csoi_line_numbers,
indent_line_after_context(Stream, LineNumbers, Context, Indent, !IO),
io.write_string(Stream, "try\n", !IO),
indent_line_after_context(Stream, LineNumbers, Context, Indent, !IO),
io.write_string(Stream, "{\n", !IO),
IndentStr = indent2_string(Indent),
io.format(Stream, "%stry\n", [s(IndentStr)], !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
output_stmt_for_csharp(Info, Stream, Indent + 1, FuncInfo, BodyStmt,
TryExitMethods0, !IO),
indent_line_after_context(Stream, LineNumbers, Context, Indent, !IO),
io.write_string(Stream, "}\n", !IO),
indent_line_after_context(Stream, LineNumbers, Context, Indent, !IO),
io.write_string(Stream, "catch (runtime.Commit commit_variable)\n", !IO),
indent_line_after_context(Stream, LineNumbers, Context, Indent, !IO),
io.write_string(Stream, "{\n", !IO),
indent_line_after_context(Stream, LineNumbers, Context, Indent + 1, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%scatch (runtime.Commit commit_variable)\n",
[s(IndentStr)], !IO),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s ", [s(IndentStr)], !IO),
output_stmt_for_csharp(Info, Stream, Indent + 1, FuncInfo, HandlerStmt,
CatchExitMethods, !IO),
indent_line_after_context(Stream, LineNumbers, Context, Indent, !IO),
io.write_string(Stream, "}\n", !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO),
ExitMethods = set.union(set.delete(TryExitMethods0, can_throw),
CatchExitMethods).
@@ -670,17 +675,17 @@ output_atomic_stmt_for_csharp(Info, Stream, Indent, AtomicStmt,
( if Comment = "" then
io.nl(Stream, !IO)
else
IndentStr = indent2_string(Indent),
% XXX We should escape any "*/"'s in the Comment. We should also
% split the comment into lines and indent each line appropriately.
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, "/* ", !IO),
io.write_string(Stream, Comment, !IO),
io.write_string(Stream, " */\n", !IO)
io.format(Stream, "%s/* %s */\n", [s(IndentStr), s(Comment)], !IO)
)
;
AtomicStmt = assign(Lval, Rval),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.write_string(Stream, IndentStr, !IO),
output_lval_for_csharp(Info, Lval, Stream, !IO),
io.write_string(Stream, " = ", !IO),
output_rval_for_csharp(Info, Rval, Stream, !IO),
@@ -700,15 +705,14 @@ output_atomic_stmt_for_csharp(Info, Stream, Indent, AtomicStmt,
;
ExplicitSecTag = no
),
LineNumbers = Info ^ csoi_line_numbers,
IndentStr = indent2_string(Indent),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "{\n", !IO),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent + 1, !IO),
io.format(Stream, "%s{\n", [s(IndentStr)], !IO),
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s ", [s(IndentStr)], !IO),
output_lval_for_csharp(Info, Target, Stream, !IO),
% Generate class constructor name.
% ZZZ
type_to_string_and_dims_for_csharp(Info, Type, TypeStr0, ArrayDims),
TypeStr = add_array_dimensions(TypeStr0, ArrayDims),
( if
@@ -735,14 +739,13 @@ output_atomic_stmt_for_csharp(Info, Stream, Indent, AtomicStmt,
;
ArgRvalsTypes = [HeadArgRvalType | TailArgRvalsTypes],
io.format(Stream, "%s\n", [s(LParen)], !IO),
output_init_args_for_csharp(Info, Stream, Indent + 2,
Indent2Str = indent2_string(Indent + 1),
output_init_args_for_csharp(Info, Stream, Indent2Str,
HeadArgRvalType, TailArgRvalsTypes, !IO),
output_n_indents(Stream, Indent + 1, !IO),
io.format(Stream, "%s;\n", [s(RParen)], !IO)
io.format(Stream, "%s %s;\n", [s(IndentStr), s(RParen)], !IO)
),
indent_line_after_context(Stream, Info ^ csoi_line_numbers, Context,
Indent, !IO),
io.write_string(Stream, "}\n", !IO)
cs_output_context(Stream, LineNumbers, Context, !IO),
io.format(Stream, "%s}\n", [s(IndentStr)], !IO)
;
AtomicStmt = gc_check,
unexpected($pred, "gc_check not implemented.")
@@ -778,12 +781,12 @@ output_atomic_stmt_for_csharp(Info, Stream, Indent, AtomicStmt,
% object's class constructor.
%
:- pred output_init_args_for_csharp(csharp_out_info::in,
io.text_output_stream::in, int::in, mlds_typed_rval::in,
io.text_output_stream::in, string::in, mlds_typed_rval::in,
list(mlds_typed_rval)::in, io::di, io::uo) is det.
output_init_args_for_csharp(Info, Stream, Indent, HeadArg, TailArgs, !IO) :-
output_init_args_for_csharp(Info, Stream, IndentStr, HeadArg, TailArgs, !IO) :-
HeadArg = ml_typed_rval(HeadArgRval, _HeadArgType),
output_n_indents(Stream, Indent, !IO),
io.write_string(Stream, IndentStr, !IO),
output_rval_for_csharp(Info, HeadArgRval, Stream, !IO),
(
TailArgs = [],
@@ -791,7 +794,7 @@ output_init_args_for_csharp(Info, Stream, Indent, HeadArg, TailArgs, !IO) :-
;
TailArgs = [HeadTailArg | TailTailArgs],
io.write_string(Stream, ",\n", !IO),
output_init_args_for_csharp(Info, Stream, Indent,
output_init_args_for_csharp(Info, Stream, IndentStr,
HeadTailArg, TailTailArgs, !IO)
).
@@ -804,19 +807,17 @@ output_init_args_for_csharp(Info, Stream, Indent, HeadArg, TailArgs, !IO) :-
output_target_code_component_for_csharp(Info, Stream, TargetCode, !IO) :-
(
TargetCode = user_target_code(CodeString, MaybeUserContext),
ForeignLineNumbers = Info ^ csoi_foreign_line_numbers,
output_pragma_warning_restore(Stream, !IO),
io.write_string(Stream, "{\n", !IO),
(
MaybeUserContext = yes(ProgContext),
cs_output_context(Stream, Info ^ csoi_foreign_line_numbers,
ProgContext, !IO)
cs_output_context(Stream, ForeignLineNumbers, ProgContext, !IO)
;
MaybeUserContext = no
),
io.write_string(Stream, CodeString, !IO),
io.write_string(Stream, "}\n", !IO),
cs_output_default_context(Stream,
Info ^ csoi_foreign_line_numbers, !IO),
io.format(Stream, "%s}\n", [s(CodeString)], !IO),
cs_output_default_context(Stream, ForeignLineNumbers, !IO),
output_pragma_warning_disable(Stream, !IO)
;
TargetCode = raw_target_code(CodeString),

View File

@@ -54,9 +54,6 @@
:- pred cs_output_context(io.text_output_stream::in, bool::in,
prog_context::in, io::di, io::uo) is det.
:- pred indent_line_after_context(io.text_output_stream::in, bool::in,
prog_context::in, indent::in, io::di, io::uo) is det.
:- pred cs_output_default_context(io.text_output_stream::in, bool::in,
io::di, io::uo) is det.
@@ -116,10 +113,6 @@ cs_output_context(Stream, OutputLineNumbers, Context, !IO) :-
OutputLineNumbers = no
).
indent_line_after_context(Stream, OutputLineNumbers, Context, N, !IO) :-
cs_output_context(Stream, OutputLineNumbers, Context, !IO),
output_n_indents(Stream, N, !IO).
cs_output_default_context(Stream, OutputLineNumbers, !IO) :-
(
OutputLineNumbers = yes,

View File

@@ -140,7 +140,7 @@ output_function_defn_for_java(Info, Stream, Indent, OutputAux,
% Java doesn't support separate declarations and definitions,
% so just output the declaration as a comment.
% (Note that the actual definition of an external procedure
% must be given in `pragma java_code' in the same module.)
% must be given in `pragma foreign_code' in the same module.)
%
% XXX For now, we print only the name of the function.
% We would like to print the whole declaration in a comment,
@@ -154,16 +154,17 @@ output_function_defn_for_java(Info, Stream, Indent, OutputAux,
io.format(Stream, "// external: %s\n", [s(FuncNameStr)], !IO)
;
MaybeBody = body_defined_here(_),
indent_line_after_context(Stream, Info ^ joi_line_numbers,
marker_comment, Context, Indent, !IO),
output_function_decl_flags_for_java(Info, Stream, Flags, !IO),
(
MaybePredProcId = no
;
MaybePredProcId = yes(PredProcid),
IndentStr = indent2_string(Indent),
maybe_output_pred_proc_id_comment(Stream, Info ^ joi_auto_comments,
PredProcid, !IO)
IndentStr, PredProcid, !IO)
),
indent_line_after_context(Stream, Info ^ joi_line_numbers,
marker_comment, Context, Indent, !IO),
output_function_decl_flags_for_java(Info, Stream, Flags, !IO),
output_func_for_java(Info, Stream, Indent, FuncName, OutputAux,
Context, Params, MaybeBody, !IO)
).

View File

@@ -200,7 +200,7 @@
%---------------------------------------------------------------------------%
:- pred maybe_output_pred_proc_id_comment(io.text_output_stream::in, bool::in,
pred_proc_id::in, io::di, io::uo) is det.
string::in, pred_proc_id::in, io::di, io::uo) is det.
%---------------------------------------------------------------------------%
@@ -430,14 +430,15 @@ output_generic_tvars(Stream, Vars, !IO) :-
%---------------------------------------------------------------------------%
maybe_output_pred_proc_id_comment(Stream, AutoComments, PredProcId, !IO) :-
maybe_output_pred_proc_id_comment(Stream, AutoComments, IndentStr,
PredProcId, !IO) :-
(
AutoComments = yes,
PredProcId = proc(PredId, ProcId),
pred_id_to_int(PredId, PredIdNum),
proc_id_to_int(ProcId, ProcIdNum),
io.format(Stream, "// pred_id: %d, proc_id: %d\n",
[i(PredIdNum), i(ProcIdNum)], !IO)
io.format(Stream, "%s// pred_id: %d, proc_id: %d\n",
[s(IndentStr), i(PredIdNum), i(ProcIdNum)], !IO)
;
AutoComments = no
).