[m-rev.] diff: avoid clutter in generated Java code

Julien Fischer jfischer at opturion.com
Fri Aug 2 13:30:36 AEST 2019


Avoid clutter in generated Java code.

compiler/mlds_to_java_file.m:
     Do not output section headers or empty static initializers
     for classes of entity that are not present in the module being
     compiled.

Julien.

diff --git a/compiler/mlds_to_java_file.m b/compiler/mlds_to_java_file.m
index e405528..88cbe50 100644
--- a/compiler/mlds_to_java_file.m
+++ b/compiler/mlds_to_java_file.m
@@ -231,58 +231,113 @@ output_java_src_file(ModuleInfo, Indent, MLDS, Errors, !IO) :-
          ForeignBodyCodes, ForeignCodeResults, !IO),
      list.filter_map(maybe_is_error, ForeignCodeResults, ForeignCodeErrors),

-    io.write_string("\n// RttiDefns\n", !IO),
-    list.foldl(
-        output_global_var_defn_for_java(Info, Indent + 1, oa_alloc_only),
-        RttiDefns, !IO),
-    output_rtti_assignments_for_java(Info, Indent + 1, RttiDefns, !IO),
-
-    io.write_string("\n// Cell and tabling definitions\n", !IO),
-    output_global_var_decls_for_java(Info, Indent + 1, CellDefns, !IO),
-    output_global_var_decls_for_java(Info, Indent + 1, TableStructDefns, !IO),
-    output_global_var_assignments_for_java(Info, Indent + 1,
-        CellDefns ++ TableStructDefns, !IO),
-
-    % Scalar common data must appear after the previous data definitions,
-    % and the vector common data after that.
-    io.write_string("\n// Scalar common data\n", !IO),
-    output_scalar_common_data_for_java(Info, Indent + 1,
-        ScalarCellGroupMap, !IO),
-
-    io.write_string("\n// Vector common data\n", !IO),
-    output_vector_common_data_for_java(Info, Indent + 1,
-        VectorCellGroupMap, !IO),
-
-    io.write_string("\n// Function definitions\n", !IO),
+    (
+        RttiDefns = []
+    ;
+        RttiDefns = [_ | _],
+        io.write_string("\n// RttiDefns\n", !IO),
+        list.foldl(
+            output_global_var_defn_for_java(Info, Indent + 1, oa_alloc_only),
+            RttiDefns, !IO),
+        output_rtti_assignments_for_java(Info, Indent + 1, RttiDefns, !IO)
+    ),
+
+    ( if
+        CellDefns = [],
+        TableStructDefns = []
+    then
+        true
+    else
+        io.write_string("\n// Cell and tabling definitions\n", !IO),
+        output_global_var_decls_for_java(Info, Indent + 1, CellDefns, !IO),
+        output_global_var_decls_for_java(Info, Indent + 1, TableStructDefns,
+            !IO),
+        output_global_var_assignments_for_java(Info, Indent + 1,
+            CellDefns ++ TableStructDefns, !IO)
+    ),
+
+    ( if map.is_empty(ScalarCellGroupMap) then
+        true
+    else
+        % Scalar common data must appear after the previous data definitions,
+        % and the vector common data after that.
+        io.write_string("\n// Scalar common data\n", !IO),
+        output_scalar_common_data_for_java(Info, Indent + 1,
+            ScalarCellGroupMap, !IO)
+    ),
+
+    ( if map.is_empty(VectorCellGroupMap) then
+        true
+    else
+        io.write_string("\n// Vector common data\n", !IO),
+        output_vector_common_data_for_java(Info, Indent + 1,
+            VectorCellGroupMap, !IO)
+    ),
+
      list.sort(ClosureWrapperFuncDefns ++ ProcDefns, SortedFuncDefns),
-    list.foldl(output_function_defn_for_java(Info, Indent + 1, oa_none),
-        SortedFuncDefns, !IO),
+    (
+        SortedFuncDefns = []
+    ;
+        SortedFuncDefns = [_ | _],
+        io.write_string("\n// Function definitions\n", !IO),
+        list.foldl(output_function_defn_for_java(Info, Indent + 1, oa_none),
+            SortedFuncDefns, !IO)
+    ),

-    io.write_string("\n// Class definitions\n", !IO),
      list.sort(WrapperClassDefns ++ TypeDefns, SortedClassDefns),
-    list.foldl(output_class_defn_for_java(Info, Indent + 1),
-        SortedClassDefns, !IO),
+    (
+        SortedClassDefns = []
+    ;
+        SortedClassDefns = [_ | _],
+        io.write_string("\n// Class definitions\n", !IO),
+        list.foldl(output_class_defn_for_java(Info, Indent + 1),
+            SortedClassDefns, !IO)
+    ),

-    io.write_string("\n// ExportDefns\n", !IO),
-    output_exports_for_java(Info, Indent + 1, ExportDefns, !IO),
+    (
+        ExportDefns = []
+    ;
+        ExportDefns = [_ | _],
+        io.write_string("\n// ExportDefns\n", !IO),
+        output_exports_for_java(Info, Indent + 1, ExportDefns, !IO)
+    ),

-    io.write_string("\n// ExportedEnums\n", !IO),
-    output_exported_enums_for_java(Info, Indent + 1, ExportedEnums, !IO),
+    (
+        ExportedEnums = []
+    ;
+        ExportedEnums = [_ | _],
+        io.write_string("\n// ExportedEnums\n", !IO),
+        output_exported_enums_for_java(Info, Indent + 1, ExportedEnums, !IO)
+    ),

-    io.write_string("\n// InitPreds\n", !IO),
-    output_inits_for_java(Indent + 1, InitPreds, !IO),
+    (
+        InitPreds = []
+    ;
+        InitPreds = [_ | _],
+        io.write_string("\n// InitPreds\n", !IO),
+        output_inits_for_java(Indent + 1, InitPreds, !IO)
+    ),

-    io.write_string("\n// FinalPreds\n", !IO),
-    output_finals_for_java(Indent + 1, FinalPreds, !IO),
+    (
+        FinalPreds = []
+    ;
+        FinalPreds = [_  | _],
+        io.write_string("\n// FinalPreds\n", !IO),
+        output_finals_for_java(Indent + 1, FinalPreds, !IO)
+    ),

-    io.write_string("\n// EnvVarNames\n", !IO),
      set.init(EnvVarNamesSet0),
      list.foldl(accumulate_env_var_names, ProcDefns,
          EnvVarNamesSet0, EnvVarNamesSet1),
      list.foldl(accumulate_env_var_names, ClosureWrapperFuncDefns,
          EnvVarNamesSet1, EnvVarNamesSet),
-    set.foldl(output_env_var_definition_for_java(Indent + 1),
-        EnvVarNamesSet, !IO),
+    ( if set.is_empty(EnvVarNamesSet) then
+        true
+    else
+        io.write_string("\n// EnvVarNames\n", !IO),
+        set.foldl(output_env_var_definition_for_java(Indent + 1),
+            EnvVarNamesSet, !IO)
+    ),

      output_src_end_for_java(Indent, ModuleName, !IO),
      % XXX Need to handle non-Java foreign code at this point.


More information about the reviews mailing list