[m-rev.] for review: rename internal field for enum classes (java)

Peter Wang novalazy at gmail.com
Fri May 22 17:26:09 AEST 2009


Are there grades I should check? Grepping for "value" doesn't help.


Branches: main

In the Java grade, an enumeration is represented by a class with a `value'
field, alongside constants named by the user.  A constant named `value'
would conflict with that field, so we rename it to `MR_value'.

compiler/ml_type_gen.m:
compiler/mlds_to_java.m:
        As above.

tests/hard_coded/Mmakefile:
tests/hard_coded/value_enum.exp:
tests/hard_coded/value_enum.m:
        Add test case.

diff --git a/compiler/ml_type_gen.m b/compiler/ml_type_gen.m
index c906359..980e348 100644
--- a/compiler/ml_type_gen.m
+++ b/compiler/ml_type_gen.m
@@ -211,7 +211,7 @@ ml_gen_type_2(TypeBody, ModuleInfo, TypeCtor,
TypeDefn, !Defns) :-
     %       static final const int <ctor1> = 0;
     %       static final const int <ctor2> = 1;
     %       ...
-    %       int value;
+    %       int MR_value;
     %   };
     %
     % It is marked as an mlds_enum so that the MLDS -> target code
@@ -256,7 +256,7 @@ ml_gen_enum_type(TypeCtor, TypeDefn, Ctors, TagValues,
 :- func ml_gen_enum_value_member(prog_context) = mlds_defn.

 ml_gen_enum_value_member(Context) =
-    mlds_defn(entity_data(var(mlds_var_name("value", no))),
+    mlds_defn(entity_data(var(mlds_var_name("MR_value", no))),
         mlds_make_context(Context),
         ml_gen_member_decl_flags,
         mlds_data(mlds_native_int_type, no_initializer, gc_no_stmt)).
diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index 8e7a3bb..1d91b9c 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -1287,7 +1287,7 @@ output_class_body(Indent, ModuleInfo, mlds_enum,
Name, AllMembers, _, !IO) :-
     Name = qual(ModuleName, _QualKind, UnqualName),
     output_enum_constants(Indent + 1, ModuleInfo, ModuleName, EnumConsts, !IO),
     indent_line(Indent + 1, !IO),
-    io.write_string("public int value;\n\n", !IO),
+    io.write_string("public int MR_value;\n\n", !IO),
     output_enum_ctor(Indent + 1, UnqualName, !IO).

 %-----------------------------------------------------------------------------%
@@ -1317,9 +1317,9 @@ output_enum_ctor(Indent, UnqualName, !IO) :-
     io.write_string("(int val) {\n", !IO),
     indent_line(Indent + 1, !IO),

-    % The use of `value' is hardcoded into ml_type_gen.m. Any changes there
+    % The use of `MR_value' is hardcoded into ml_type_gen.m. Any changes there
     % should probably be reflected here.
-    io.write_string("this.value = val;\n", !IO),
+    io.write_string("this.MR_value = val;\n", !IO),
     indent_line(Indent + 1, !IO),
     io.write_string("return;\n", !IO),
     indent_line(Indent, !IO),
@@ -3446,12 +3446,12 @@ output_binop(ModuleInfo, Op, X, Y, ModuleName, !IO) :-
     ).

     % Output an Rval and if the Rval is an enumeration object append the string
-    % ".value", so we can access its value field.
+    % ".MR_value", so we can access its value field.
     %
     % XXX Note that this is necessary in some places, but not in others.
     % For example, it is important to do so for switch statements, as the
     % argument of a switch _must_ be an integer in Java. However, adding
-    % the .value to assignments breaks some casting... At some point, we
+    % the .MR_value to assignments breaks some casting... At some point, we
     % need to go through all the places where output_rval and
     % output_rval_maybe_with_enum are called and make sure the correct one
     % is being used.
@@ -3462,7 +3462,7 @@ output_binop(ModuleInfo, Op, X, Y, ModuleName, !IO) :-
 output_rval_maybe_with_enum(ModuleInfo, Rval, ModuleName, !IO) :-
     output_rval(ModuleInfo, Rval, ModuleName, !IO),
     ( rval_is_enum_object(Rval) ->
-        io.write_string(".value", !IO)
+        io.write_string(".MR_value", !IO)
     ;
         true
     ).
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 215a7eb..7a13a4b 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -262,6 +262,7 @@ ORDINARY_PROGS=	\
 	unusual_name_mutable \
 	user_compare \
 	user_defined_equality2 \
+	value_enum \
 	write \
 	write_reg1 \
 	write_reg2 \
@@ -313,7 +314,8 @@ JAVA_PASS_PROGS= \
 	string_suffix_bug \
 	system_sort \
 	target_mlobjs \
-	time_test
+	time_test \
+	value_enum

 # Solver types only work in C grades
 ifeq "$(filter il% java% erlang%,$(GRADE))" ""
diff --git a/tests/hard_coded/value_enum.exp b/tests/hard_coded/value_enum.exp
new file mode 100644
index 0000000..6d4e150
--- /dev/null
+++ b/tests/hard_coded/value_enum.exp
@@ -0,0 +1 @@
+value
diff --git a/tests/hard_coded/value_enum.m b/tests/hard_coded/value_enum.m
new file mode 100644
index 0000000..800d23e
--- /dev/null
+++ b/tests/hard_coded/value_enum.m
@@ -0,0 +1,25 @@
+% The Java backend was using `value' as a field name inside enumeration
+% classes, which conflicted with `value' function symbols in Mercury code.
+
+:- module value_enum.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- type ref_or_value
+    --->    value
+    ;       ref.
+
+main(!IO) :-
+    io.write(value, !IO),
+    io.nl(!IO).
+
+%-----------------------------------------------------------------------------%
+% 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