[m-rev.] for review: merge foreign_type pragma on to the main branch

Peter Ross peter.ross at miscrit.be
Wed Oct 24 00:44:22 AEST 2001


On Tue, Oct 23, 2001 at 05:06:38PM +1000, Tyson Dowd wrote:
> > 
> > compiler/mlds.m:
> >     Table the result of export__type_to_type_string so as to avoid
> >     passing the module_info around the MLDS backend.
> 
> I'm not really keen on this change, and I never have been.
> The real thing I dislike is that we have to run
> export__type_to_type_string every time we create a mercury_type in the
> MLDS.
> 
> I would prefer 
> 	- that export__type_to_type_string can work without the module_info.
> or
> 	- that we don't call export__type_to_type_string in the MLDS
> 	  backend.
> or
> 	- that we call export__type_to_type_string much later in the
> 	  MLDS, when we actually have to output a type.
> 
> Why can't the MLDS contain a map of all the foreign types?  Or the type
> can contain maybe(type_defn_body) and be yes/1 for foreign types?
> 

How about adding the following interface to foreign.m and then using it
everywhere?

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


foreign.m:
    Implement a new interface for export__type_to_type_string.  The
    interface now consists of two steps.  The first step is creating an
    instance of the abstract type, export.  This requires a module info
    to do this safely, however for the case where you know that the
    type is not a foreign type, then you can call non_foreign_type.

    Then later call to_type_string on the abstract type, export, to get
    the actual type representation.

Index: foreign.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/foreign.m,v
retrieving revision 1.7
diff -u -r1.7 foreign.m
--- foreign.m	23 Jul 2001 12:22:04 -0000	1.7
+++ foreign.m	23 Oct 2001 14:31:56 -0000
@@ -496,9 +496,82 @@
 		FM = qualified(Module, Name ++ Ending)
 	).
 
+%-----------------------------------------------------------------------------%
 
+:- import_module hlds_data.
 
+:- type export
+	--->	foreign(sym_name)
+	;	mercury((type)).
+
+	% Given a type which is not defined as a foreign type, get the
+	% export representation of that type.
+:- func non_foreign_type((type)) = export.
+
+non_foreign_type(Type) = mercury(Type).
+
+	% Given an arbitary mercury type, get the export representation
+	% of that type.
+:- func to_export(module_info, (type)) = export.
+
+to_export(ModuleInfo, Type) = ExportType :-
+	module_info_types(ModuleInfo, Types),
+	(
+		type_to_type_id(Type, TypeId, _),
+		map__search(Types, TypeId, TypeDefn)
+	->
+		hlds_data__get_type_defn_body(TypeDefn, Body),
+		( Body = foreign_type(ForeignType, _) ->
+			ExportType = foreign(ForeignType)
+		;
+			ExportType = mercury(Type)
+		)
+	;
+		ExportType = mercury(Type)
+	).
+
+:- func to_type_string(foreign_language, export_type) = string.
+
+to_type_string(c, foreign(Foreign)) = 
+	error("to_type_string: c NYI").
+to_type_string(csharp, foreign(Foreign)) = _ :-
+	Result = sym_name_to_string(ForeignType, ".").
+to_type_string(managed_cplusplus, foreign(Foreign)) = _ :-
+	Result = sym_name_to_string(ForeignType, ":") ++ " *".
+to_type_string(il, foreign(Foreign)) = _ :-
+	error("to_type_string: il NYI").
+
+to_type_string(c, mercury(Type)) = Result :-
+	( Type = term__functor(term__atom("int"), [], _) ->
+		Result = "MR_Integer"
+	; Type = term__functor(term__atom("float"), [], _) ->
+		Result = "MR_Float"
+	; Type = term__functor(term__atom("string"), [], _) ->
+		Result = "MR_String"
+	; Type = term__functor(term__atom("character"), [], _) ->
+		Result = "MR_Char"
+	;
+		Result = "MR_Word"
+	).
+to_type_string(csharp, mercury(Type)) = _ :-
+	error("to_type_string: NYI").
+to_type_string(managed_cplusplus, mercury(Type)) = TypeString :-
+	( 
+		type_util__var(Type, _),
+		Lang = managed_cplusplus
+	->
+		TypeString = "MR_Box"
+	;
+		TypeString = to_type_string(c, mercury(Type)
+	).
+to_type_string(il, mercury(Type)) = _ :-
+	error("to_type_string: il NYI").
+	
+%-----------------------------------------------------------------------------%
 
 :- func this_file = string.
+
 this_file = "foreign.m".
 
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%

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