[m-rev.] for review: refactor mlds_to_il.m

Peter Ross peter.ross at miscrit.be
Wed Jul 11 20:32:38 AEST 2001


On Tue, Jul 10, 2001 at 03:09:53AM +1000, Fergus Henderson wrote:
> On 10-Jul-2001, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> > [... to be continued ...]
> 
> The rest of the diff looked fine, except for the string "mercury_code"
> being hard-coded in too many places.  I suggest you define a function
> for it:
> 
> 		% When targetting languages such as IL, C#, and Java,
> 		% which don't support global methods or global variables,
> 		% we need to wrap all the generated global functions and
> 		% global data inside a wrapper class. 
> 		% This function returns the name to use for the wrapper class.
> 	:- func wrapper_class_name = string.
> 	wrapper_class_name = "mercury_code".
> 


===================================================================


Estimated hours taken: 0.25
Branches: main

compiler/mlds_to_il.m:
    For decl_flags_to_classattrs calculate the access flags correctly.

compiler/mlds.m:
    Add a new function wrapper_class_name, which returns the name of the
    class to wrap global functions and data inside for backends which
    require this.

compiler/mlds_to_csharp.m:
compiler/mlds_to_mcpp.m:
    Use the new function, and update the comments.


Index: mlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds.m,v
retrieving revision 1.59
diff -u -r1.59 mlds.m
--- mlds.m	10 Jul 2001 14:49:13 -0000	1.59
+++ mlds.m	11 Jul 2001 10:21:54 -0000
@@ -357,14 +357,20 @@
 :- func mlds__append_class_qualifier(mlds_module_name, mlds__class_name, arity) =
 	mlds_module_name.
 
-% Append a mercury_code qualifier to the module name and leave the
+% Append a wrapper class qualifier to the module name and leave the
 % package name unchanged.
-:- func mlds__append_mercury_code(mlds_module_name) = mlds_module_name.
+:- func mlds__append_wrapper_class(mlds_module_name) = mlds_module_name.
 
 % Append an arbitarty qualifier to the module name and leave the package
 % name unchanged.
 :- func mlds__append_name(mlds_module_name, string) = mlds_module_name.
 
+% When targetting languages such as IL, C#, and Java, which don't
+% support global methods or global variables, we need to wrap all the
+% generated global functions and global data inside a wrapper class.
+% This function returns the name to use for the wrapper class.
+:- func wrapper_class_name = string.
+
 :- type mlds__defns == list(mlds__defn).
 :- type mlds__defn
 	---> mlds__defn(
@@ -1505,10 +1511,12 @@
 	string__format("%s_%d", [s(ClassName), i(ClassArity)],
 		ClassQualifier).
 
-mlds__append_mercury_code(Name) = mlds__append_name(Name, "mercury_code").
+mlds__append_wrapper_class(Name) = mlds__append_name(Name, wrapper_class_name).
 
 mlds__append_name(name(Package, Module), Name)
 	= name(Package, qualified(Module, Name)).
+
+wrapper_class_name = "mercury_code".
 
 %-----------------------------------------------------------------------------%
 
Index: mlds_to_il.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.41
diff -u -r1.41 mlds_to_il.m
--- mlds_to_il.m	11 Jul 2001 09:10:34 -0000	1.41
+++ mlds_to_il.m	11 Jul 2001 10:21:56 -0000
@@ -233,7 +233,7 @@
 %-----------------------------------------------------------------------------%
 
 	% Move all the top level methods and data definitions into the
-	% mercury_code class, and then fix all the references so that
+	% wrapper class, and then fix all the references so that
 	% they refer to their new names.
 :- func transform_mlds(mlds) = mlds.
 
@@ -243,15 +243,15 @@
 			; D = mlds__defn(_, _, _, mlds__data(_, _))
 			)
 		), MLDS0 ^ defns, MercuryCodeMembers, Others),
-	MLDS = MLDS0 ^ defns := [mercury_code_class(
+	MLDS = MLDS0 ^ defns := [wrapper_class(
 			list__map(rename_defn, MercuryCodeMembers)) | Others].
 
 
-:- func mercury_code_class(mlds__defns) = mlds__defn.
+:- func wrapper_class(mlds__defns) = mlds__defn.
 
-mercury_code_class(Members)
+wrapper_class(Members)
 	= mlds__defn(
-		export("mercury_code"),
+		export(wrapper_class_name),
 		mlds__make_context(term__context_init),
 		ml_gen_type_decl_flags,
 		mlds__class(
@@ -387,7 +387,8 @@
 rename_code_addr(internal(Label, Seq, Signature))
 	= internal(rename_proc_label(Label), Seq, Signature).
 
-rename_proc_label(qual(Module, Name)) = qual(append_mercury_code(Module), Name).
+rename_proc_label(qual(Module, Name))
+	= qual(append_wrapper_class(Module), Name).
 
 :- func rename_lval(mlds__lval) = mlds__lval.
 
@@ -411,23 +412,23 @@
 	= init_array(list__map(rename_initializer, Inits)).
 rename_initializer(no_initializer) = no_initializer.
 
-	% We need to append a mercury_code so that we access the RTTI
-	% fields correctly.
+	% We need to append a wrapper class qualifier so that we access
+	% the RTTI fields correctly.
 :- func rename_data_addr(data_addr) = data_addr.
 
 rename_data_addr(data_addr(ModuleName, Name))
-	= data_addr(append_mercury_code(ModuleName), Name).
+	= data_addr(append_wrapper_class(ModuleName), Name).
 
-	% We need to append a mercury_code so that we refer to the
-	% methods of the mercury_code class.
+	% We need to append a wrapper class qualifier so that we refer to the
+	% methods of the wrapper class.
 :- func rename_proc_label(mlds__qualified_proc_label) =
 		mlds__qualified_proc_label.
 
-	% Again append a mercury_code to the var name.
+	% Again append a wrapper class qualifier to the var name.
 :- func rename_var(mlds__var, mlds__type) = mlds__var.
 
 rename_var(qual(ModuleName, Name), _Type)
-	= qual(append_mercury_code(ModuleName), Name).
+	= qual(append_wrapper_class(ModuleName), Name).
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
@@ -453,10 +454,10 @@
 	generate_class_body(Name, ClassDefn, ClassName, EntityName, Extends,
 			Interfaces, MethodsAndFieldsAndCtors, Info1, Info2),
 
-		% Only the mercury_code class needs to have the
+		% Only the wrapper class needs to have the
 		% initialization instructions executed by the class
 		% constructor.
-	( EntityName = "mercury_code" ->
+	( EntityName = wrapper_class_name ->
 		Imports = Info2 ^ imports,
 		InitInstrs = list__condense(tree__flatten(Info2 ^ init_instrs)),
 		AllocInstrs = list__condense(
@@ -549,11 +550,14 @@
 	( AccessFlag = public,
 		Access = [public]
 	; AccessFlag = protected,
-		Access = []
+		error("decl_flags_to_classattrs: protected access flag")
 	; AccessFlag = private,
 		Access = [private]
 	; AccessFlag = default,
-		error("decl_flags_to_classattrs: default access flag")
+			% To make members of the private class
+			% accessible to other types in the assembly, set
+			% their access to be default or public.
+		Access = [private]
 	; AccessFlag = local,
 		error("decl_flags_to_classattrs: local access flag")
 	).
@@ -2123,7 +2127,7 @@
 	test_rtti_initialization_field(DoneFieldRef, TestInstrs),
 	set_rtti_initialization_field(DoneFieldRef, SetInstrs),
 	{ CCtorCalls = list__map((func(X) = call_class_constructor(
-		class_name(X, "mercury_code"))), Imports) },
+		class_name(X, wrapper_class_name))), Imports) },
 	{ AllInstrs = list__condense([TestInstrs, AllocInstrs, SetInstrs,
 		CCtorCalls, InitInstrs, [ret]]) },
 	{ MethodDecls = [instrs(AllInstrs)] }.
@@ -2496,8 +2500,8 @@
 	),
 	SymName0 = mlds_module_name_to_sym_name(ModuleName0),
 		% Check to see whether or not the name has already been
-		% qualified with a mercury_code.  If not qualify it.
-	( SymName0 = qualified(SymName1, "mercury_code") ->
+		% qualified with the wrapper class.  If not qualify it.
+	( SymName0 = qualified(SymName1, wrapper_class_name) ->
 		( 
 			SymName1 = qualified(SQ, SM0),
 			SM = string__format("%s__%s_code",
@@ -2509,18 +2513,20 @@
 					[s(SM0), s(LangStr)]),
 			SymName2 = unqualified(SM)
 		),
-		SymName = qualified(SymName2, "mercury_code")
+		SymName = qualified(SymName2, wrapper_class_name)
 	;
 		( 
 			SymName0 = qualified(SQ, SM0),
 			SM = string__format("%s__%s_code",
 					[s(SM0), s(LangStr)]),
-			SymName = qualified(qualified(SQ, SM), "mercury_code")
+			SymName = qualified(qualified(SQ, SM),
+					wrapper_class_name)
 		; 
 			SymName0 = unqualified(SM0),
 			SM = string__format("%s__%s_code",
 					[s(SM0), s(LangStr)]),
-			SymName = qualified(unqualified(SM), "mercury_code")
+			SymName = qualified(unqualified(SM),
+					wrapper_class_name)
 		)
 	),
 	ModuleName = mercury_module_and_package_name_to_mlds(
@@ -2540,7 +2546,7 @@
 	( 
 		SymName = mlds_module_name_to_sym_name(ModuleName0),
 		SymName = qualified(qualified(unqualified("mercury"),
-			LibModuleName0), "mercury_code"),
+			LibModuleName0), wrapper_class_name),
 		DataName = rtti(rtti_type_id(_, Name, Arity),
 			_RttiName),
 		( LibModuleName0 = "builtin",
@@ -2575,7 +2581,7 @@
 			LibModuleName),
 		ModuleName = mercury_module_name_to_mlds(
 			qualified(qualified(unqualified("mercury"),
-			LibModuleName), "mercury_code"))
+			LibModuleName), wrapper_class_name))
 	;
 		ModuleName = ModuleName0
 	).
@@ -2956,8 +2962,8 @@
 
 :- func mercury_string_class_name = ilds__class_name.
 mercury_string_class_name = mercury_library_name(StringClass) :-
-	sym_name_to_class_name(qualified(unqualified("string"), "mercury_code"),
-			StringClass).
+	sym_name_to_class_name(qualified(unqualified("string"),
+			wrapper_class_name), StringClass).
 
 %-----------------------------------------------------------------------------%
 %
@@ -3187,7 +3193,7 @@
 :- func runtime_init_module_name = ilds__class_name.
 runtime_init_module_name = 
 	structured_name("mercury",
-		["mercury", "private_builtin__cpp_code", "mercury_code"]).
+		["mercury", "private_builtin__cpp_code", wrapper_class_name]).
 
 :- func runtime_init_method_name = ilds__member_name.
 runtime_init_method_name = id("init_runtime").
Index: mlds_to_csharp.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_csharp.m,v
retrieving revision 1.8
diff -u -r1.8 mlds_to_csharp.m
--- mlds_to_csharp.m	9 Jul 2001 15:55:05 -0000	1.8
+++ mlds_to_csharp.m	11 Jul 2001 10:21:56 -0000
@@ -91,7 +91,7 @@
 
 	{ MLDS = mlds(ModuleName, ForeignCode, _Imports, Defns) },
 	{ ClassName = class_name(mercury_module_name_to_mlds(ModuleName), 
-			"mercury_code") },
+			wrapper_class_name) },
 
 	io__nl,
 	io__write_strings([
@@ -119,7 +119,7 @@
 	)),
 
 	io__write_strings([
-		"\npublic class mercury_code",
+		"\npublic class " ++ wrapper_class_name,
 		"{\n"]),
 
 		% Output the contents of pragma foreign_code declarations.
Index: mlds_to_mcpp.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_mcpp.m,v
retrieving revision 1.9
diff -u -r1.9 mlds_to_mcpp.m
--- mlds_to_mcpp.m	9 Jul 2001 15:55:07 -0000	1.9
+++ mlds_to_mcpp.m	11 Jul 2001 10:21:56 -0000
@@ -94,7 +94,7 @@
 	{ MLDS = mlds(ModuleName, ForeignCode, _Imports, Defns) },
 	{ prog_out__sym_name_to_string(ModuleName, ModuleNameStr) },
 	{ ClassName = class_name(mercury_module_name_to_mlds(ModuleName),
-			"mercury_code") },
+			wrapper_class_name) },
 
 	io__nl,
 	io__write_strings([
@@ -132,7 +132,7 @@
 		ForeignCode),
 
 	io__write_strings([
-		"\n__gc public class mercury_code",
+		"\n__gc public class " ++ wrapper_class_name,
 		"{\n",
 		"public:\n"]),
 

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list