[m-rev.] for review: mangle symbols with a leading digit in java

Peter Wang novalazy at gmail.com
Fri Jun 19 17:55:21 AEST 2009


Branches: main

Mangle symbols which would begin with a digit in generated Java code.

compiler/prog_foreign.m:
        Add a version of name_mangle which mangles symbols beginning with a
        digit, in the same way as if it contained a character not allowed in C
        identifiers.

compiler/java_names.m:
compiler/mlds_to_java.m:
        Call the new function.

        Don't need to mangle a string in a spot.

compiler/ml_util.m:
        Unrelated: make a simple change to defns_contain_main to use less
        nondet stack space.  I ran out of stack space when testing this change
        on a particular module.  The compiler was built in a debugging grade.

diff --git a/compiler/java_names.m b/compiler/java_names.m
index 59153f2..a7cafd4 100644
--- a/compiler/java_names.m
+++ b/compiler/java_names.m
@@ -103,15 +103,15 @@ mangle_sym_name_for_java_2(SymName, QualKind,
MangledSymName) :-
 :- func java_safe_name_component(java_qual_kind, string) = string.

 java_safe_name_component(QualKind, Name) = JavaSafeName :-
+    MangledName = name_mangle_no_leading_digit(Name),
     (
         QualKind = module_qual,
-        FlippedName = Name
+        FlippedName = MangledName
     ;
         QualKind = type_qual,
-        FlippedName = flip_initial_case(Name)
+        FlippedName = flip_initial_case(MangledName)
     ),
-    MangledName = name_mangle(FlippedName),
-    JavaSafeName = valid_java_symbol_name(MangledName).
+    JavaSafeName = valid_java_symbol_name(FlippedName).

 %-----------------------------------------------------------------------------%

diff --git a/compiler/ml_util.m b/compiler/ml_util.m
index 716aa22..56fdeaf 100644
--- a/compiler/ml_util.m
+++ b/compiler/ml_util.m
@@ -203,11 +203,14 @@

 %-----------------------------------------------------------------------------%

-defns_contain_main(Defns) :-
-    list.member(Defn, Defns),
-    Defn = mlds_defn(Name, _, _, _),
-    Name = entity_function(FuncName, _, _, _),
-    FuncName = mlds_user_pred_label(pf_predicate, _, "main", 2, _, _).
+defns_contain_main([Defn | Defns]) :-
+    (
+        Defn = mlds_defn(Name, _, _, _),
+        Name = entity_function(FuncName, _, _, _),
+        FuncName = mlds_user_pred_label(pf_predicate, _, "main", 2, _, _)
+    ;
+        defns_contain_main(Defns)
+    ).

 can_optimize_tailcall(Name, Call) :-
     Call = ml_stmt_call(_Signature, FuncRval, MaybeObject, _CallArgs,
diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index 13f899f..64b5a51 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -976,7 +976,7 @@ pred_label_string(mlds_user_pred_label(PredOrFunc,
MaybeDefiningModule, Name,
         Suffix = "f",
         OrigArity = PredArity - 1
     ),
-    MangledName = name_mangle(Name),
+    MangledName = name_mangle_no_leading_digit(Name),
     PredLabelStr0 = MangledName ++ "_" ++ string.int_to_string(OrigArity)
         ++ "_" ++ Suffix,
     (
@@ -989,7 +989,7 @@ pred_label_string(mlds_user_pred_label(PredOrFunc,
MaybeDefiningModule, Name,
     ).
 pred_label_string(mlds_special_pred_label(PredName, MaybeTypeModule, TypeName,
         TypeArity)) = PredLabelStr :-
-    MangledPredName = name_mangle(PredName),
+    MangledPredName = name_mangle_no_leading_digit(PredName),
     MangledTypeName = name_mangle(TypeName),
     PredLabelStr0 = MangledPredName ++ "__",
     (
@@ -1972,7 +1972,7 @@ output_class_name_and_arity(Name, Arity, !IO) :-
 :- pred output_class_name(mlds_class_name::in, io::di, io::uo) is det.

 output_class_name(Name, !IO) :-
-    MangledName = name_mangle(Name),
+    MangledName = name_mangle_no_leading_digit(Name),
     % By convention, class names should start with a capital letter.
     UppercaseMangledName = flip_initial_case(MangledName),
     io.write_string(UppercaseMangledName, !IO).
@@ -2009,7 +2009,7 @@
output_pred_label(mlds_user_pred_label(PredOrFunc,
MaybeDefiningModule, Name,
         Suffix = "f",
         OrigArity = PredArity - 1
     ),
-    MangledName = name_mangle(Name),
+    MangledName = name_mangle_no_leading_digit(Name),
     io.format("%s_%d_%s", [s(MangledName), i(OrigArity), s(Suffix)], !IO),
     (
         MaybeDefiningModule = yes(DefiningModule),
@@ -2021,7 +2021,7 @@
output_pred_label(mlds_user_pred_label(PredOrFunc,
MaybeDefiningModule, Name,

 output_pred_label(mlds_special_pred_label(PredName, MaybeTypeModule, TypeName,
         TypeArity), !IO) :-
-    MangledPredName = name_mangle(PredName),
+    MangledPredName = name_mangle_no_leading_digit(PredName),
     MangledTypeName = name_mangle(TypeName),
     io.write_string(MangledPredName, !IO),
     io.write_string("__", !IO),
@@ -2077,7 +2077,7 @@ output_mlds_var_name(mlds_var_name(Name,
yes(Num)), !IO) :-
 :- func name_mangle_maybe_shorten(string) = string.

 name_mangle_maybe_shorten(Name) = MangledName :-
-    MangledName0 = name_mangle(Name),
+    MangledName0 = name_mangle_no_leading_digit(Name),
     ( string.length(MangledName0) < 100 ->
         MangledName = MangledName0
     ;
@@ -3244,8 +3244,7 @@ output_lval(ModuleInfo, Lval, ModuleName, !IO) :-
             FieldId = ml_field_named(FieldName, CtorType),
             (
                 FieldName = qual(_, _, UnqualFieldName),
-                MangledFieldName = name_mangle(UnqualFieldName),
-                MangledFieldName = "data_tag"
+                UnqualFieldName = "data_tag"
             ->
                 % If the field we are trying to access is just a `data_tag'
                 % then it is a member of the base class.
diff --git a/compiler/prog_foreign.m b/compiler/prog_foreign.m
index 347d2f1..d5ed6f6 100644
--- a/compiler/prog_foreign.m
+++ b/compiler/prog_foreign.m
@@ -172,9 +172,15 @@
 :- func sym_name_mangle(sym_name) = string.

     % Mangle an arbitrary name into a C etc identifier.
+    % Initial digits are allowed.
     %
 :- func name_mangle(string) = string.

+    % Mangle an arbitrary name into a C etc identifier.
+    % The resulting identifier will not begin with a digit.
+    %
+:- func name_mangle_no_leading_digit(string) = string.
+
     % Produces a string of the form Module__Name.
     %
 :- func qualify_name(string, string) = string.
@@ -373,12 +379,27 @@ sym_name_mangle(qualified(ModuleName,
PlainName)) = MangledName :-
     MangledPlainName = name_mangle(PlainName),
     MangledName = qualify_name(MangledModuleName, MangledPlainName).

-name_mangle(Name) = MangledName :-
+name_mangle(Name) = name_mangle_2(yes, Name).
+
+name_mangle_no_leading_digit(Name) = name_mangle_2(no, Name).
+
+:- func name_mangle_2(bool, string) = string.
+
+name_mangle_2(AllowLeadingDigit, Name) = MangledName :-
     % Warning: any changes to the name mangling algorithm here may also
     % require changes to extras/dynamic_linking/name_mangle.m,
     % profiler/demangle.m, util/mdemangle.c and compiler/name_mangle.m.

-    ( string.is_all_alnum_or_underscore(Name) ->
+    (
+        string.is_all_alnum_or_underscore(Name),
+        (
+            AllowLeadingDigit = yes
+        ;
+            AllowLeadingDigit = no,
+            string.index(Name, 0, FirstChar),
+            not char.is_digit(FirstChar)
+        )
+    ->
         % Any names that start with `f_' are changed so that they start with
         % `f__', so that we can use names starting with `f_' (followed by
         % anything except an underscore) without fear of name collisions.
--------------------------------------------------------------------------
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