[m-rev.] diff: fix name mangling in java construct

Peter Wang novalazy at gmail.com
Thu Sep 30 13:17:26 AEST 2010


Branches: main, 10.04

On the Java backend, a functor with the same name and arity as its type is
given an extra "Mr_" prefix to distinguish it.  The Java implementation of
construct needs to account for the extra prefix; it does now.

library/rtti_implementation.m:
        As above.

tests/hard_coded/construct_mangle.exp:
tests/hard_coded/construct_mangle.m:
        Extend this test case.

        Add another name mangling problem, to be fixed some other time.

diff --git a/library/rtti_implementation.m b/library/rtti_implementation.m
index 804c081..a1287b9 100644
--- a/library/rtti_implementation.m
+++ b/library/rtti_implementation.m
@@ -2035,24 +2035,36 @@ is_exist_pseudo_type_info(_, _) :-
             IllegalAccessException, InstantiationException,
             InvocationTargetException
     {
-        Class<?> cls;
-
+        String clsname;
         if (tc.type_ctor_num_functors == 1) {
-            cls = Class.forName(
-                ""jmercury."" + ML_name_mangle(tc.type_ctor_module_name)
+            clsname = ""jmercury."" + ML_name_mangle(tc.type_ctor_module_name)
                 + ""$"" + ML_flipInitialCase(ML_name_mangle(tc.type_ctor_name))
-                + ""_"" + tc.arity);
+                + ""_"" + tc.arity;
         } else {
-            cls = Class.forName(
-                ""jmercury."" + ML_name_mangle(tc.type_ctor_module_name)
-                + ""$"" + ML_flipInitialCase(
-                            ML_name_mangle(tc.type_ctor_name))
-                + ""_"" + tc.arity
-                + ""$"" + ML_flipInitialCase(
-                            ML_name_mangle(functor_desc.du_functor_name))
-                + ""_"" + functor_desc.du_functor_orig_arity);
+            String mangled_mod_name = ML_name_mangle(tc.type_ctor_module_name);
+            String mangled_ctor_name = ML_name_mangle(tc.type_ctor_name) +
+                ""_"" + tc.arity;
+
+            String mangled_functor_name;
+            if (tc.type_ctor_name.equals(functor_desc.du_functor_name) &&
+                tc.arity == functor_desc.du_functor_orig_arity)
+            {
+                mangled_functor_name =
+                    ML_name_mangle(""mr_"" + functor_desc.du_functor_name) +
+                    ""_"" + functor_desc.du_functor_orig_arity;
+            } else {
+                mangled_functor_name =
+                    ML_name_mangle(functor_desc.du_functor_name) +
+                    ""_"" + functor_desc.du_functor_orig_arity;
+            }
+
+            clsname = ""jmercury."" + mangled_mod_name + ""$"" +
+                ML_flipInitialCase(mangled_ctor_name) + ""$"" +
+                ML_flipInitialCase(mangled_functor_name);
         }
 
+        final Class<?> cls = Class.forName(clsname);
+
         final int arity = functor_desc.du_functor_orig_arity;
         final Object[] args = ML_univ_list_to_array(arg_list, arity);
 
diff --git a/tests/hard_coded/construct_mangle.exp b/tests/hard_coded/construct_mangle.exp
index dbab897..bc6bce4 100644
--- a/tests/hard_coded/construct_mangle.exp
+++ b/tests/hard_coded/construct_mangle.exp
@@ -44,8 +44,14 @@ univ_cons('_')
 univ_cons('`')
 univ_cons('abc~!@#$%^&*()_+|xyz')
 univ_cons(f_this_also_requires_mangling)
+univ_cons(force_non_enum(1))
+univ_cons(requires_mangling)
+univ_cons(requires_mangling(1))
 univ_cons('{')
 univ_cons({})
 univ_cons('|')
 univ_cons('}')
 univ_cons((~))
+----
+univ_cons('$singleton'(1))
+----
diff --git a/tests/hard_coded/construct_mangle.m b/tests/hard_coded/construct_mangle.m
index 1e6c1ab..756975a 100644
--- a/tests/hard_coded/construct_mangle.m
+++ b/tests/hard_coded/construct_mangle.m
@@ -23,12 +23,8 @@
 %-----------------------------------------------------------------------------%
 
 main(!IO) :-
-    Type = type_of(_ : requires_mangling),
-    ( NumFunctors = num_functors(Type) ->
-        list.foldl(test_functor(Type), 0 .. NumFunctors - 1, !IO)
-    ;
-        io.write_string("failed\n", !IO)
-    ).
+    test_type(type_of(_ : requires_mangling), !IO),
+    test_type(type_of(_ : '$singleton'(int)), !IO).
 
     % This must not be an enumeration as enumerations on Java are represented
     % in a way such that name mangling doesn't apply.
@@ -85,27 +81,44 @@ main(!IO) :-
     ;       ('[]')
     ;       ('abc~!@#$%^&*()_+|xyz')
     ;       f_this_also_requires_mangling
-    ;       force_non_enum(int).
+    ;       force_non_enum(int)
+    ;       requires_mangling       % functor has same name/arity as type
+    ;       requires_mangling(int). % same name, different arity
+
+:- type '$singleton'(T)
+    --->    '$singleton'(T).
+
+% XXX the Java backend currently generates invalid code for this type
+% :- type '$blah'
+%     --->    '$blah'(int)
+%     ;       '$blah2'.
+
+:- pred test_type(type_desc::in, io::di, io::uo) is det.
+
+test_type(Type, !IO) :-
+    ( NumFunctors = num_functors(Type) ->
+        list.foldl(test_functor(Type), 0 .. NumFunctors - 1, !IO)
+    ;
+        io.write_string("failed\n", !IO)
+    ),
+    io.write_string("----\n", !IO).
 
 :- pred test_functor(type_desc::in, int::in, io::di, io::uo) is det.
 
 test_functor(Type, FunctorNumber, !IO) :-
-    ( get_functor(Type, FunctorNumber, Name, Arity, ArgTypes) ->
+    ( get_functor(Type, FunctorNumber, Name, Arity, _ArgTypes) ->
+        % Assume that any arguments are ints.
+        ArgUnivs = list.map(int_univ, 1 .. Arity),
         (
-            ArgTypes = [],
-            (
-                find_functor(Type, Name, Arity, FunctorNumber, _),
-                Univ = construct(Type, FunctorNumber, [])
-            ->
-                io.write(Univ, !IO),
-                io.nl(!IO)
-            ;
-                io.write_string("failed FunctorNumber = ", !IO),
-                io.write_int(FunctorNumber, !IO),
-                io.nl(!IO)
-            )
+            find_functor(Type, Name, Arity, FunctorNumber, _),
+            Univ = construct(Type, FunctorNumber, ArgUnivs)
+        ->
+            io.write(Univ, !IO),
+            io.nl(!IO)
         ;
-            ArgTypes = [_ | _]
+            io.write_string("failed FunctorNumber = ", !IO),
+            io.write_int(FunctorNumber, !IO),
+            io.nl(!IO)
         )
     ;
         io.write_string("failed FunctorNumber = ", !IO),
@@ -113,5 +126,9 @@ test_functor(Type, FunctorNumber, !IO) :-
         io.nl(!IO)
     ).
 
+:- func int_univ(int) = univ.
+
+int_univ(I) = univ(I).
+
 %-----------------------------------------------------------------------------%
 % vim: ft=mercury ts=8 sts=4 sw=4 et

--------------------------------------------------------------------------
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