[m-rev.] for review: new foreign_type syntax

Tyson Dowd trd at cs.mu.OZ.AU
Fri Dec 7 18:22:02 AEDT 2001


Here's the incremental diff from the last round of changes. 
I think I have got everything (is it the right time of year to make
lists and check them twice?).


--- zzlog.6	Thu Dec  6 13:18:08 2001
+++ zzlog.7	Fri Dec  7 18:17:38 2001
@@ -1,5 +1,5 @@
 
-Estimated hours taken: 8
+Estimated hours taken: 10
 Branches: main
 
 Fix and enable the new foreign_type syntax.  We now accept declarations
@@ -9,8 +9,8 @@
 
 compiler/prog_data.m:
 	Remove the foreign type location from the foreign_type pragma,
-	it is better to make this part of the backend type.
-	Rename backend as backend_type as it specifies a type in a
+	it is better to make this part of the foreign_language_type.
+	Rename backend as foreign_language_type as it specifies a type in a
 	particular backend.
 
 compiler/prog_io_pragma.m:

diff -u compiler/make_hlds.m compiler/make_hlds.m
--- compiler/make_hlds.m
+++ compiler/make_hlds.m
@@ -395,13 +395,13 @@
 		{ Pragma = foreign_proc(_, _, _, _, _, _) },
 		{ Module = Module0 }
 	;	
-		{ Pragma = foreign_type(Backend, _MercuryType, Name) },
+		{ Pragma = foreign_type(ForeignType, _MercuryType, Name) },
 
-		{ Backend = il(ForeignTypeLocation, ForeignType) },
+		{ ForeignType = il(ForeignTypeLocation, ForeignTypeName) },
 
 		{ varset__init(VarSet) },
 		{ Args = [] },
-		{ Body = foreign_type(ForeignType, ForeignTypeLocation) },
+		{ Body = foreign_type(ForeignTypeName, ForeignTypeLocation) },
 		{ Cond = true },
 
 		{ TypeId = Name - 0 },
diff -u compiler/mercury_to_mercury.m compiler/mercury_to_mercury.m
--- compiler/mercury_to_mercury.m
+++ compiler/mercury_to_mercury.m
@@ -466,18 +466,18 @@
 		mercury_output_pragma_foreign_code(Attributes, Pred,
 			PredOrFunc, Vars, VarSet, PragmaCode)
 	;
-		{ Pragma = foreign_type(Backend, _MercuryType,
+		{ Pragma = foreign_type(ForeignType, _MercuryType,
 				MercuryTypeSymName) },
 		io__write_string(":- pragma foreign_type("),
 		% output_type(varset__init, no, MercuryType),
 		mercury_output_sym_name(MercuryTypeSymName),
 		io__write_string(", "),
 
-		{ Backend = il(ForeignLocStr, ForeignType) },
+		{ ForeignType = il(ForeignLocStr, ForeignTypeName) },
 		io__write_string("il(\""),
 		io__write_string(ForeignLocStr),
 		io__write_string("\", "),
-		mercury_output_sym_name(ForeignType),
+		mercury_output_sym_name(ForeignTypeName),
 		io__write_string("\").\n")
 	;
 		{ Pragma = import(Pred, PredOrFunc, ModeList, Attributes,
diff -u compiler/module_qual.m compiler/module_qual.m
--- compiler/module_qual.m
+++ compiler/module_qual.m
@@ -893,8 +893,8 @@
 qualify_pragma(source_file(File), source_file(File), Info, Info) --> [].
 qualify_pragma(foreign_decl(L, Code), foreign_decl(L, Code), Info, Info) --> [].
 qualify_pragma(foreign_code(L, C), foreign_code(L, C), Info, Info) --> [].
-qualify_pragma(foreign_type(Backend, Type0, SymName),
-		foreign_type(Backend, Type, SymName), Info0, Info) -->
+qualify_pragma(foreign_type(ForeignType, Type0, SymName),
+		foreign_type(ForeignType, Type, SymName), Info0, Info) -->
 	qualify_type(Type0, Type, Info0, Info).
 qualify_pragma(foreign_import_module(L, M), foreign_import_module(L, M),
 		Info, Info) --> [].
diff -u compiler/prog_data.m compiler/prog_data.m
--- compiler/prog_data.m
+++ compiler/prog_data.m
@@ -157,8 +157,8 @@
 			% PredName, Predicate or Function, Vars/Mode, 
 			% VarNames, Foreign Code Implementation Info
 
-	;	foreign_type(backend_type, (type), sym_name)
-			% BackendType, MercuryType, MercuryTypeName
+	;	foreign_type(foreign_language_type, (type), sym_name)
+			% ForeignType, MercuryType, MercuryTypeName
 
 	;	foreign_import_module(foreign_language, module_name)
 			% Equivalent to
@@ -287,9 +287,16 @@
 % Stuff for the foreign interfacing pragmas.
 %
 
-:- type backend_type
-			% The location of the il name, and the .NET type name
-			% (represented as a sym_name)
+	% 
+	% A foreign_language_type represents a type that is defined in a
+	% foreign language and accessed in Mercury (most likely through 
+	% pragma foreign_type).
+	% Currently we only support foreign_language_types for IL.
+	%
+
+:- type foreign_language_type
+			% The location of the .NET name (the assembly),
+			% and the .NET type name (represented as a sym_name)
 	--->	il(string, sym_name).
 
 %
diff -u compiler/prog_io_pragma.m compiler/prog_io_pragma.m
--- compiler/prog_io_pragma.m
+++ compiler/prog_io_pragma.m
@@ -72,11 +72,12 @@
 
 parse_pragma_type(ModuleName, "foreign_type", PragmaTerms,
             ErrorTerm, _VarSet, Result) :-
-    ( PragmaTerms = [LangTerm, MercuryName, BackendTerm] ->
+    ( PragmaTerms = [LangTerm, MercuryName, ForeignTypeTerm] ->
 	( parse_foreign_language(LangTerm, Language) ->
-	    parse_backend(BackendTerm, Language, MaybeBackend),
+	    parse_foreign_language_type(ForeignTypeTerm, Language,
+	    	MaybeForeignType),
 	    (
-		MaybeBackend = ok(Backend),
+		MaybeForeignType = ok(ForeignType),
 		parse_implicitly_qualified_term(ModuleName, MercuryName,
 		    ErrorTerm, "`:- pragma foreign_type' declaration",
 		    MaybeMercuryType),
@@ -84,7 +85,7 @@
 		    MaybeMercuryType = ok(MercuryTypeSymName, MercuryArgs),
 		    ( MercuryArgs = [] ->
 			term__coerce(MercuryName, MercuryType),
-			Result = ok(pragma(foreign_type(Backend,
+			Result = ok(pragma(foreign_type(ForeignType,
 			    MercuryType, MercuryTypeSymName)))
 		    ;
 			Result = error("foreign type arity not 0", ErrorTerm)
@@ -94,7 +95,7 @@
 		    Result = error(String, Term)
 		)
 	    ;
-		MaybeBackend = error(String, Term),
+		MaybeForeignType = error(String, Term),
 		Result = error(String, Term)
 	    )
 	;   
@@ -205,10 +206,11 @@
 parse_foreign_language(term__functor(term__atom(String), _, _), Lang) :-
 	globals__convert_foreign_language(String, Lang).
 
-:- pred parse_backend(term, foreign_language, maybe1(backend_type)).
-:- mode parse_backend(in, in, out) is det.
+:- pred parse_foreign_language_type(term, foreign_language,
+		maybe1(foreign_language_type)).
+:- mode parse_foreign_language_type(in, in, out) is det.
 
-parse_backend(InputTerm, Language, Result) :-
+parse_foreign_language_type(InputTerm, Language, Result) :-
 	( 
 		Language = il
 	->
@@ -225,10 +227,10 @@
 		Result = error("unsupported language specified, unable to parse backend type", InputTerm)
 	).
 
-:- pred parse_il_type_name(string, term, maybe1(backend_type)).
+:- pred parse_il_type_name(string, term, maybe1(foreign_language_type)).
 :- mode parse_il_type_name(in, in, out) is det.
 
-parse_il_type_name(String0, ErrorTerm, BackendType) :-
+parse_il_type_name(String0, ErrorTerm, ForeignType) :-
 	( 
 		string__append("class [", String1, String0),
 		string__sub_string_search(String1, "]", Index)
@@ -236,10 +238,10 @@
 		string__left(String1, Index, AssemblyName),
 		string__split(String1, Index + 1, _, TypeNameStr),
 		string_to_sym_name(TypeNameStr, ".", TypeSymName),
-		BackendType = ok(il(AssemblyName, TypeSymName))
+		ForeignType = ok(il(AssemblyName, TypeSymName))
 	;
-		BackendType = error("invalid backend type description", 
-			ErrorTerm)
+		ForeignType = error(
+			"invalid foreign language type description", ErrorTerm)
 	).
 
 	% This predicate parses both c_header_code and foreign_decl pragmas.
diff -u doc/reference_manual.texi doc/reference_manual.texi
--- doc/reference_manual.texi
+++ doc/reference_manual.texi
@@ -5010,7 +5010,7 @@
 @node Using foreign types from Mercury
 @section Using foreign types from Mercury
 
-Types defined in foreign language can be defined in Mercury using 
+Types defined in a foreign language can be accessed in Mercury using 
 a declaration of the form
 
 @example
@@ -5033,9 +5033,16 @@
 However, if @var{MercuryTypeName} is one of the parameters of a
 foreign_proc for @var{Lang}, it will be passed to that foreign_proc as
 specified by @var{ForeignTypeDescriptor}.
+
 Multiple foreign languages definitions may be given for the same type ---
 the appropriate definition will be used for the appropriate language (see the
-language specific inforation below for details).
+language specific information below for details).
+
+You can use Mercury foreign language interfacing declarations           
+which specify language X to interface to types that are actually          
+written in a different language Y provided that X and Y                 
+have compatible interface conventions.  Support for this kind of
+compatibility is described in the language specific information below.
 
 @c -----------------------------------------------------------------------
 
@@ -5120,7 +5127,6 @@
 
 @menu
 * Interfacing with C 		:: How to write code to interface with C
-* Interfacing with .NET CLR	:: How to write code to interface with .NET CLR
 * Interfacing with C# 		:: How to write code to interface with C#
 * Interfacing with IL 		:: How to write code to interface with IL
 * Interfacing with Managed C++ 	:: How to write code to interface with
@@ -5289,29 +5295,72 @@
 @node Using pragma foreign_type for C
 @subsubsection Using pragma foreign_type for C
 
-This pragma is currently not implemented for C backends.
+This pragma is currently not supported for C.
+
+See the section on using C pointers (@pxref{Using C pointers}) for
+information on how to use the c_pointer type with the C interface.
+ at c XXX we should eventually just move that section to here,
+ at c presenting it as an alternative to pragma foreign_type.
 
 @c ----------------------------------------------------------------------------
 
- at node Interfacing with .NET CLR
- at subsection Interfacing with .NET CLR
+ at node Interfacing with C#
+
+
+
+Currently undocumented, sorry.
+
+ at menu
+* Using pragma foreign_type for C#	:: Declaring C# types in Mercury
+* Using pragma foreign_proc for C# 	:: Calling C# code from Mercury
+* Using pragma foreign_decl for C# 	:: Including C# declarations in Mercury
+* Using pragma foreign_code for C# 	:: Including C# code in Mercury
+ at end menu
+
+ at node Using pragma foreign_type for C#
+ at subsubsection Using pragma foreign_type for C#
+
+There is direct support for using C# types from Mercury, however the
+types for IL are compatible with C#, and so the foreign_type
+support for IL can be used instead.
+See the section on using pragma foreign_type for IL
+(@pxref{Using pragma foreign_type for IL}).
+
+ at node Using pragma foreign_proc for C#
+ at subsubsection Using pragma foreign_proc for C#
+
+
+
 
-This section describes how to interface with the .NET Common Language
-Runtime, as specified by the ECMA Common Language Infrastructure (ECMA
-TC39/TG3) standards.
 
- at c XXX
-Currently much of this section is still undocumented, sorry.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ at subsection Interfacing with IL
 
 @menu
-* Using pragma foreign_type for .NET CLR :: Declaring .NET CLR types in Mercury
+* Using pragma foreign_type for IL	:: Declaring IL types in Mercury
+* Using pragma foreign_proc for IL 	:: Calling IL code from Mercury
+* Using pragma foreign_decl for IL 	:: Including IL declarations in Mercury
+* Using pragma foreign_code for IL 	:: Including IL code in Mercury
 @end menu
 
- at node Using pragma foreign_type for .NET CLR
- at subsubsection Using pragma foreign_type for .NET CLR
+ at node Using pragma foreign_type for IL
+ at subsubsection Using pragma foreign_type for IL
 
-The .NET CLR @samp{pragma foreign_type} declaration is of
-the form:
+The IL @samp{pragma foreign_type} declaration is of the form:
 
 @example
 :- pragma foreign_type(il, @var{MercuryTypeName}, @var{DotNetForeignType}).
@@ -5319,13 +5368,17 @@
 
 If the @var{MercuryTypeName} is the type of a parameter of a procedure
 defined using @samp{pragma foreign_proc} for any of the .NET CLR
-languages, it will be passed to user code as @var{DotNetForeignType}.
-Futhermore, any exported (with @samp{pragma export}) Mercury
-procedure will use @var{DotNetForeignType} as the parameter type for
+languages, it will be passed to the foreign_proc's foreign language code
+as @var{DotNetForeignType}.
+
+Furthermore, any Mercury procedure exported with @samp{pragma export}
+will use @var{DotNetForeignType} as the .NET CLR parameter type for
 parameters whose Mercury type is @var{MercuryTypeName}.
 
-The CIL assembler syntax is used to specify type names for the .NET CLR
-backend (this syntax is documented in the ECMA specifications for .NET).
+The CIL assembler syntax is used to specify type names for all the .NET CLR
+languages (IL, C# and Managed C++) supported by Mercury's
+ at samp{foreign_proc} mechanism.  
+This syntax is documented in the ECMA specifications for .NET).
 Currently on the .NET CLR backend only reference classes are supported using
 @samp{pragma foreign_type}, and hence the only supported syntax for
 DotNetForeignType is @samp{"class [AssemblyName]ClassName"}.
@@ -5357,50 +5410,6 @@
 ").
 @end example
 
- at c ----------------------------------------------------------------------------
-
- at node Interfacing with C#
-
-
-
-Currently undocumented, sorry.
-
- at menu
-* Using pragma foreign_proc for C# 	:: Calling C# code from Mercury
-* Using pragma foreign_decl for C# 	:: Including C# declarations in Mercury
-* Using pragma foreign_code for C# 	:: Including C# code in Mercury
- at end menu
-
- at node Using pragma foreign_proc for C#
- at subsubsection Using pragma foreign_proc for C#
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- at subsection Interfacing with IL
-
- at menu
-* Using pragma foreign_proc for IL 	:: Calling IL code from Mercury
-* Using pragma foreign_decl for IL 	:: Including IL declarations in Mercury
-* Using pragma foreign_code for IL 	:: Including IL code in Mercury
- at end menu
-
 @node Using pragma foreign_proc for IL
 @subsubsection Using pragma foreign_proc for IL
 
@@ -5481,10 +5490,20 @@
 Currently undocumented, sorry.
 
 @menu
+* Using pragma foreign_type for MC++::  Declaring MC++ types in Mercury
 * Using pragma foreign_proc for MC++::  Calling MC++ code from Mercury
 * Using pragma foreign_decl for MC++::  Including MC++ declarations in Mercury
 * Using pragma foreign_code for MC++::  Including MC++ code in Mercury
 @end menu
+
+ at node Using pragma foreign_type for MC++
+ at subsubsection Using pragma foreign_type for MC++
+
+There is direct support for using MC++ types from Mercury, however the
+types for IL are compatible with MC++, and so the foreign_type
+support for IL can be used instead.
+See the section on using pragma foreign_type for IL
+(@pxref{Using pragma foreign_type for IL}).
 
 @node Using pragma foreign_proc for MC++
 @subsubsection Using pragma foreign_proc for MC++

-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
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