[m-dev.] for review: pragma foreign_code for MC++ (part 1/2)

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Nov 13 19:32:21 AEDT 2000


On 10-Nov-2000, Tyson Dowd <trd at cs.mu.OZ.AU> wrote:
> compiler/make_hlds.m:
> 	Add foriegn language information to the bodys and decls when
> 	creating them.

s/foriegn/foreign/

> compiler/prog_data.m:
> 	Add managedcplusplus as a new alternative for the
> 	foreign_language type.

I suggest s/managedcplusplus/managed_cplusplus/

[... to be continued ...]

> +%-----------------------------------------------------------------------------%
> +
> +% Utilities for generating C code which interfaces with Mercury.  
> +% The MLDS->C backend and fact tables use this code.
>  
> -	% Convert the type, to a string corresponding to its C type.
> +	% Convert the type to a string corresponding to its C type.
>  	% (Defaults to MR_Word).
>  :- pred export__type_to_type_string(type, string).
>  :- mode export__type_to_type_string(in, out) is det.
> @@ -72,30 +79,31 @@

The comment there gives incomplete information:
export__type_to_type_string is also used by the LLDS->C back-end.

> @@ -555,16 +563,19 @@
>  :- mode export__produce_header_file_2(in, di, uo) is det.
>  export__produce_header_file_2([]) --> [].
>  export__produce_header_file_2([E|ExportedProcs]) -->
> -	{ E = foreign_export_decl(c, C_RetType, C_Function, ArgDecls) },
> -
> -		% output the function header
> -	io__write_string(C_RetType),
> -	io__write_string(" "),
> -	io__write_string(C_Function),
> -	io__write_string("("),
> -	io__write_string(ArgDecls),
> -	io__write_string(");\n"),
> -
> +	( 
> +		{ E = foreign_export_decl(c, C_RetType, C_Function, ArgDecls) }
> +	->
> +			% output the function header
> +		io__write_string(C_RetType),
> +		io__write_string(" "),
> +		io__write_string(C_Function),
> +		io__write_string("("),
> +		io__write_string(ArgDecls),
> +		io__write_string(");\n")
> +	;
> +		{ error("export__produce_header_file_2: foreign languages other than C unimplemented") }
> +	),

It would be better to move the unification with foreign_export_decl/4
out of the if-then-else:

	{ E = foreign_export_decl(Language, RetType, Function, ArgDecls) },
	( { Language = c } ->
		...
	;
		{ error(...) }
	).

> +++ foreign.m	Fri Nov 10 19:35:21 2000
...
> +	% Filter the decls for the given foreign language. 
> +:- pred foreign__filter_decls(foreign_language, foreign_decl_info,
> +		foreign_decl_info, foreign_decl_info).
> +:- mode foreign__filter_decls(in, in, out, out) is det.

The comment here is not clear; does this return just those that
match the specified language, or just those that don't match?

> +	% Filter the bodys for the given foreign language. 
> +:- pred foreign__filter_bodys(foreign_language, foreign_body_info,

Likewise.

> +	% Given some foreign code, generate some suitable proxy code for 
> +	% calling the code via the given language. 
> +	% This might mean, for example, generating a call to a
> +	% forwarding function in C.
> +:- pred foreign__extrude_pragma_implementation(foreign_language,
> +		list(pragma_var), sym_name, pred_or_func, prog_context,
> +		module_info, pragma_foreign_code_attributes,
> +		pragma_foreign_code_impl, 
> +		module_info, pragma_foreign_code_attributes,
> +		pragma_foreign_code_impl).
> +:- mode foreign__extrude_pragma_implementation(in, in, in, in, in,
> +		in, in, in, out, out, out) is det.

What do the arguments of this predicate mean?

> +:- pred foreign__make_pragma_import(pred_info, proc_info, string, prog_context,
> +	module_info, pragma_foreign_code_impl, prog_varset, 
> +	list(pragma_var), list(type), arity, pred_or_func).
> +:- mode foreign__make_pragma_import(in, in, in, in, in,
> +	out, out, out, out, out, out) is det.

What does this predicate do?
And what is the meaning of its arguments?

> +:- func make_pred_name_rest(foreign_language, sym_name) = string.
> +make_pred_name_rest(c, _SymName) = "some_c_name".

Hmm, that looks a bit odd... perhaps it is missing an XXX comment
and explanation?

> +make_pragma_import(PredInfo, ProcInfo, C_Function, Context,
> +		ModuleInfo, PragmaImpl, VarSet, PragmaVars, ArgTypes, 
> +		Arity, PredOrFunc) :-
...
> +	%
> +	% Construct parts of the C_Code string for calling C_Function.
> +	% This C code fragment invokes the specified C function
> +	% with the appropriate arguments from the list constructed
> +	% above, passed in the appropriate manner (by value, or by
> +	% passing the address to simulate pass-by-reference), and
> +	% assigns the return value (if any) to the appropriate place.
> +	% As this phase occurs before polymorphism, we don't know about
> +	% the type-infos yet.  polymorphism.m is responsible for adding
> +	% the type-info arguments to the list of variables.
> +	%
> +	handle_return_value(CodeModel, PredOrFunc, PragmaVarsAndTypes,
> +			ModuleInfo, ArgPragmaVarsAndTypes, Return),
> +	assoc_list__keys(ArgPragmaVarsAndTypes, ArgPragmaVars),
> +	create_pragma_import_c_code(ArgPragmaVars, ModuleInfo,
> +			"", Variables),
> +
> +	%
> +	% Add the C_Code for this `pragma import' to the clauses_info
> +	%
> +	PragmaImpl = import(C_Function, Return, Variables, yes(Context)).

The comment here appears to have suffered documentation rot.
It needs at least "s/C_Code/C code/g", and perhaps more serious revision.

[... to be continued ...]

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list