<commit_before>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2010, University of Amsterdam, VU University Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(qp_foreign,
	  [ load_foreign_files/0,		%
	    load_foreign_files/2,		% +Files, +Libs
	    load_foreign_files/3,		% +Object, +Files, +Libs
	    make_shared_object/3,		% +Object, +Files, +Libs
	    make_foreign_wrapper_file/1,	% +OutBase
	    make_foreign_wrapper_file/2,	% +OFiles, +OutBase
						% SICStus stuff
	    make_foreign_resource_wrapper/3,    % +Resource, +ResBase, +FileBase
	    load_foreign_resource/2		% +Resource, +Dir
	  ]).

:- use_module(library(shlib)).
:- use_module(library(gensym)).
:- use_module(library(lists)).
:- use_module(library(apply)).
:- use_module(library(error)).

/** <module> Quintus compatible foreign loader

This module defines a  Quintus   compatible  foreign  language interface
based on the foreign_file/2 and foreign/3 declarations.

Predicates:

	* load_foreign_files
	Load all foreign files defined with foreign_file/2 statement
	in the current module.

	* load_foreign_files(+Files, +Libs)
	Load specified foreign files, linking them with the given
	libraries

	* load_foreign_files(+SharedObject, +Files, +Libs)
	As load_foreign_files/2, but first tries to load `SharedObject'.
	If the SharedObject exists it is loaded using load_foreign_library/1.
	Otherwise it is first created with make_shared_object/3.

	* make_shared_object(+SharedObject, +Files, +Libs)
	Generate a wrapper and link it using plld to the given SharedObject.

	* make_foreign_wrapper_file(+Files, +OutBase)
	Generate wrapper for the named object files in OutBase.c.

	* make_foreign_wrapper_file(+Files)
	Generate wrapper for all declared object files in OutBase.c.

Example:

	==
	foreign_file('-lm', [sin/2]).
	foreign(sin, c, sin(+float, [-float])).
	:- load_foreign_files,
	   abolish(foreign_file, 2),
	   abolish(foreign, 3).
	==

Supported types:

	| *Spec*	| *Prolog*	| *C*                           |
	| integer	| integer	| long                          |
	| float		| float,integer	| double                        |
	| single	| float,integer	| single                        |
	| string	| atom,string	| char *			|
	| atom		| atom		| atom identifier (type atomic) |

*NOTE*	This modules requires a correctly functioning plld and
	load_foreign_library/1 on your system.  If this isn't the
	case use make_foreign_wrapper_file/[1,2] to generate a
	wrapper and use static embedding.

@bug	Only supports C-interface
@bug	Insufficient checking for misusage.
@bug	Documentation is too short and very outdated
*/

:- module_transparent
	load_foreign_files/0.

:- meta_predicate
	load_foreign_files(:, +),
	load_foreign_files(+, :, +),
	make_shared_object(+, :, +),
	make_foreign_wrapper_file(:),
	make_foreign_wrapper_file(:, +),
					% SICStus
	make_foreign_resource_wrapper(:, +),
	load_foreign_resource(:, +).

setting(linker, 'swipl-ld').

hook(M:Goal) :-
	M:Goal.

%%	make_wrappers(+PrologHeads, +Module, +OutStream)

make_wrappers([], _, _).
make_wrappers([H|T], M, Out) :-
	make_wrapper(Out, M:H),
	make_wrappers(T, M, Out).

%%	make_wrapper(+Stream, :PrologHead)
%
%	Generates a C-wrapper function for the given foreign defined
%	Prolog predicate.  The wrapper is called _plw_<predname><arity>.

make_wrapper(Out, Spec) :-
	get_foreign_head(Spec, Func, Head), !,
	(   check_head(Head)
	->  wrapper_name(Head, WrapName, ArgN),
	    make_C_header(Out, WrapName, ArgN),
	    make_C_decls(Out, Head),
	    make_C_prototype(Out, Head),
	    make_C_input_conversions(Out, Head),<commit_msg>Fixed incorrect meta_predicate declaration.<commit_after>/*  $Id$

    Part of SWI-Prolog

    Author:        <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2010, University of Amsterdam, VU University Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(qp_foreign,
	  [ load_foreign_files/0,		%
	    load_foreign_files/2,		% +Files, +Libs
	    load_foreign_files/3,		% +Object, +Files, +Libs
	    make_shared_object/3,		% +Object, +Files, +Libs
	    make_foreign_wrapper_file/1,	% +OutBase
	    make_foreign_wrapper_file/2,	% +OFiles, +OutBase
						% SICStus stuff
	    make_foreign_resource_wrapper/3,    % +Resource, +ResBase, +FileBase
	    load_foreign_resource/2		% +Resource, +Dir
	  ]).

:- use_module(library(shlib)).
:- use_module(library(gensym)).
:- use_module(library(lists)).
:- use_module(library(apply)).
:- use_module(library(error)).

/** <module> Quintus compatible foreign loader

This module defines a  Quintus   compatible  foreign  language interface
based on the foreign_file/2 and foreign/3 declarations.

Predicates:

	* load_foreign_files
	Load all foreign files defined with foreign_file/2 statement
	in the current module.

	* load_foreign_files(+Files, +Libs)
	Load specified foreign files, linking them with the given
	libraries

	* load_foreign_files(+SharedObject, +Files, +Libs)
	As load_foreign_files/2, but first tries to load `SharedObject'.
	If the SharedObject exists it is loaded using load_foreign_library/1.
	Otherwise it is first created with make_shared_object/3.

	* make_shared_object(+SharedObject, +Files, +Libs)
	Generate a wrapper and link it using plld to the given SharedObject.

	* make_foreign_wrapper_file(+Files, +OutBase)
	Generate wrapper for the named object files in OutBase.c.

	* make_foreign_wrapper_file(+Files)
	Generate wrapper for all declared object files in OutBase.c.

Example:

	==
	foreign_file('-lm', [sin/2]).
	foreign(sin, c, sin(+float, [-float])).
	:- load_foreign_files,
	   abolish(foreign_file, 2),
	   abolish(foreign, 3).
	==

Supported types:

	| *Spec*	| *Prolog*	| *C*                           |
	| integer	| integer	| long                          |
	| float		| float,integer	| double                        |
	| single	| float,integer	| single                        |
	| string	| atom,string	| char *			|
	| atom		| atom		| atom identifier (type atomic) |

*NOTE*	This modules requires a correctly functioning plld and
	load_foreign_library/1 on your system.  If this isn't the
	case use make_foreign_wrapper_file/[1,2] to generate a
	wrapper and use static embedding.

@bug	Only supports C-interface
@bug	Insufficient checking for misusage.
@bug	Documentation is too short and very outdated
*/

:- module_transparent
	load_foreign_files/0.

:- meta_predicate
	load_foreign_files(:, +),
	load_foreign_files(+, :, +),
	make_shared_object(+, :, +),
	make_foreign_wrapper_file(:),
	make_foreign_wrapper_file(:, +),
					% SICStus
	make_foreign_resource_wrapper(:, +, +),
	load_foreign_resource(:, +).

setting(linker, 'swipl-ld').

hook(M:Goal) :-
	M:Goal.

%%	make_wrappers(+PrologHeads, +Module, +OutStream)

make_wrappers([], _, _).
make_wrappers([H|T], M, Out) :-
	make_wrapper(Out, M:H),
	make_wrappers(T, M, Out).

%%	make_wrapper(+Stream, :PrologHead)
%
%	Generates a C-wrapper function for the given foreign defined
%	Prolog predicate.  The wrapper is called _plw_<predname><arity>.

make_wrapper(Out, Spec) :-
	get_foreign_head(Spec, Func, Head), !,
	(   check_head(Head)
	->  wrapper_name(Head, WrapName, ArgN),
	    make_C_header(Out, WrapName, ArgN),
	    make_C_decls(Out, Head),
	    make_C_prototype(Out, Head),
	    make_C_input_conversions(Out, Head),
{'fraction_non_alphanumeric': 0.09497555393737647, 'fraction_numerical': 0.008530115468636221, 'mean_word_length': 3.6265640038498557, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5354982', 'n_tokens_mistral': 3201, 'n_tokens_neox': 2976, 'n_words': 966}