View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        jan@swi-prolog.org
    5    WWW:           https://www.swi-prolog.org
    6    Copyright (c)  2006-2026, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9                              SWI-Prolog Solutions b.v.
   10    All rights reserved.
   11
   12    Redistribution and use in source and binary forms, with or without
   13    modification, are permitted provided that the following conditions
   14    are met:
   15
   16    1. Redistributions of source code must retain the above copyright
   17       notice, this list of conditions and the following disclaimer.
   18
   19    2. Redistributions in binary form must reproduce the above copyright
   20       notice, this list of conditions and the following disclaimer in
   21       the documentation and/or other materials provided with the
   22       distribution.
   23
   24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   25    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   27    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   28    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   29    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   30    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   31    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   32    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   34    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   35    POSSIBILITY OF SUCH DAMAGE.
   36*/
   37
   38:- module(prolog_xref,
   39          [ xref_source/1,              % +Source
   40            xref_source/2,              % +Source, +Options
   41            xref_called/3,              % ?Source, ?Callable, ?By
   42            xref_called/4,              % ?Source, ?Callable, ?By, ?Cond
   43            xref_called/5,              % ?Source, ?Callable, ?By, ?Cond, ?Line
   44            xref_defined/3,             % ?Source. ?Callable, -How
   45            xref_definition_line/2,     % +How, -Line
   46            xref_exported/2,            % ?Source, ?Callable
   47            xref_module/2,              % ?Source, ?Module
   48            xref_uses_file/3,           % ?Source, ?Spec, ?Path
   49            xref_op/2,                  % ?Source, ?Op
   50            xref_prolog_flag/4,         % ?Source, ?Flag, ?Value, ?Line
   51            xref_comment/3,             % ?Source, ?Title, ?Comment
   52            xref_comment/4,             % ?Source, ?Head, ?Summary, ?Comment
   53            xref_object_comment/4,      % ?Source, ?Object, ?Summary, ?Comment
   54            xref_object_signature/3,    % ?Source, ?Object, ?Signature
   55            xref_mode/3,                % ?Source, ?Mode, ?Det
   56            xref_option/2,              % ?Source, ?Option
   57            xref_clean/1,               % +Source
   58            xref_current_source/1,      % ?Source
   59            xref_done/2,                % +Source, -When
   60            xref_built_in/1,            % ?Callable
   61            xref_source_file/3,         % +Spec, -Path, +Source
   62            xref_source_file/4,         % +Spec, -Path, +Source, +Options
   63            xref_public_list/3,         % +File, +Src, +Options
   64            xref_public_list/4,         % +File, -Path, -Export, +Src
   65            xref_public_list/6,         % +File, -Path, -Module, -Export, -Meta, +Src
   66            xref_public_list/7,         % +File, -Path, -Module, -Export, -Public, -Meta, +Src
   67            xref_meta/3,                % +Source, +Goal, -Called
   68            xref_meta/2,                % +Goal, -Called
   69            xref_hook/1,                % ?Callable
   70                                        % XPCE class references
   71            xref_used_class/2,          % ?Source, ?ClassName
   72            xref_defined_class/3        % ?Source, ?ClassName, -How
   73          ]).   74:- autoload(library(apply),[maplist/2,partition/4,maplist/3]).   75:- use_module(library(debug),[debug/3]).   76:- autoload(library(dialect),[expects_dialect/1]).   77:- autoload(library(error),[must_be/2,instantiation_error/1]).   78:- autoload(library(lists),[member/2,append/2,append/3,select/3]).   79:- autoload(library(operators),[push_op/3]).   80:- autoload(library(option),[option/2,option/3]).   81:- autoload(library(ordsets),[ord_intersect/2,ord_intersection/3]).   82:- autoload(library(prolog_code), [pi_head/2]).   83:- autoload(library(prolog_source),
   84	    [ prolog_canonical_source/2,
   85	      prolog_open_source/2,
   86	      prolog_close_source/1,
   87	      prolog_read_source_term/4,
   88              prolog_file_directives/3
   89	    ]).   90
   91:- if(exists_source(library(shlib))).   92:- autoload(library(shlib),[current_foreign_library/2]).   93:- endif.   94:- autoload(library(solution_sequences),[distinct/2,limit/2]).   95
   96:- if(exists_source(library(pldoc))).   97:- use_module(library(pldoc), []).      % Must be loaded before doc_process
   98:- use_module(library(pldoc/doc_process)).   99
  100:- endif.  101
  102:- predicate_options(xref_source/2, 2,
  103                     [ silent(boolean),
  104                       module(atom),
  105                       register_called(oneof([all,non_iso,non_built_in])),
  106                       comments(oneof([store,collect,ignore])),
  107                       process_include(boolean),
  108                       stream(stream)
  109                     ]).  110
  111
  112:- dynamic
  113    called/5,                       % Head, Src, From, Cond, Line
  114    (dynamic)/3,                    % Head, Src, Line
  115    (thread_local)/3,               % Head, Src, Line
  116    (multifile)/3,                  % Head, Src, Line
  117    (public)/3,                     % Head, Src, Line
  118    (declared)/4,	            % Head, How, Src, Line
  119    defined/3,                      % Head, Src, Line
  120    meta_goal/3,                    % Head, Called, Src
  121    foreign/3,                      % Head, Src, Line
  122    constraint/3,                   % Head, Src, Line
  123    imported/3,                     % Head, Src, From
  124    exported/2,                     % Head, Src
  125    xmodule/2,                      % Module, Src
  126    uses_file/3,                    % Spec, Src, Path
  127    xop/2,                          % Src, Op
  128    source/2,                       % Src, Time
  129    used_class/2,                   % Name, Src
  130    defined_class/5,                % Name, Super, Summary, Src, Line
  131    (mode)/2,                       % Mode, Src
  132    xoption/2,                      % Src, Option
  133    xflag/4,                        % Name, Value, Src, Line
  134    grammar_rule/2,                 % Head, Src
  135    module_comment/3,               % Src, Title, Comment
  136    pred_comment/4,                 % Head, Src, Summary, Comment
  137    pred_comment_link/3,            % Head, Src, HeadTo
  138    pred_mode/3,                    % Head, Src, Det
  139    object_comment/4,               % Object, Src, Summary, Comment
  140    object_signature/3.             % Object, Src, Signature
  141
  142:- create_prolog_flag(xref, false, [type(boolean)]).

Prolog cross-referencer data collection

This library collects information on defined and used objects in Prolog source files. Typically these are predicates, but we expect the library to deal with other types of objects in the future. The library is a building block for tools doing dependency tracking in applications. Dependency tracking is useful to reveal the structure of an unknown program or detect missing components at compile time, but also for program transformation or minimising a program saved state by only saving the reachable objects.

The library is exploited by two graphical tools in the SWI-Prolog environment: the XPCE front-end started by gxref/0, and library(prolog_colour), which exploits this library for its syntax highlighting.

For all predicates described below, Source is the source that is processed. This is normally a filename in any notation acceptable to the file loading predicates (see load_files/2). Input handling is done by the library(prolog_source), which may be hooked to process any source that can be translated into a Prolog stream holding Prolog source text. Callable is a callable term (see callable/1). Callables do not carry a module qualifier unless the referred predicate is not in the module defined by Source.

See also
- Where this library analyses source text, library(prolog_codewalk) may be used to analyse loaded code. The library(check) exploits library(prolog_codewalk) to report on e.g., undefined predicates.
bug
- meta_predicate/1 declarations take the module into consideration. Predicates that are both available as meta-predicate and normal (in different modules) are handled as meta-predicate in all places.
  179:- predicate_options(xref_source_file/4, 4,
  180                     [ file_type(oneof([txt,prolog,directory])),
  181                       silent(boolean)
  182                     ]).  183:- predicate_options(xref_public_list/3, 3,
  184                     [ path(-atom),
  185                       module(-atom),
  186                       exports(-list(any)),
  187                       public(-list(any)),
  188                       meta(-list(any)),
  189                       silent(boolean)
  190                     ]).  191
  192
  193                 /*******************************
  194                 *            HOOKS             *
  195                 *******************************/
 prolog:called_by(+Goal, +Module, +Context, -Called) is semidet
True when Called is a list of callable terms called from Goal, handled by the predicate Module:Goal and executed in the context of the module Context. Elements of Called may be qualified. If not, they are called in the context of the module Context.
 prolog:called_by(+Goal, -ListOfCalled)
If this succeeds, the cross-referencer assumes Goal may call any of the goals in ListOfCalled. If this call fails, default meta-goal analysis is used to determine additional called goals.
deprecated
- New code should use prolog:called_by/4
 prolog:meta_goal(+Goal, -Pattern)
Define meta-predicates. See the examples in this file for details.
 prolog:hook(Goal)
True if Goal is a hook that is called spontaneously (e.g., from foreign code).
  222:- multifile
  223    prolog:called_by/4,             % +Goal, +Module, +Context, -Called
  224    prolog:called_by/2,             % +Goal, -Called
  225    prolog:meta_goal/2,             % +Goal, -Pattern
  226    prolog:hook/1,                  % +Callable
  227    prolog:generated_predicate/1,   % :PI
  228    prolog:no_autoload_module/1,    % Module is not suitable for autoloading.
  229    prolog:xref_source_time/2.      % +Source, =Modified
  230
  231:- meta_predicate
  232    prolog:generated_predicate(:).  233
  234:- meta_predicate
  235    process_predicates(2, +, +).  236
  237                 /*******************************
  238                 *           BUILT-INS          *
  239                 *******************************/
 hide_called(:Callable, +Src) is semidet
True when the cross-referencer should not include Callable as being called. This is determined by the option register_called.
  247hide_called(Callable, Src) :-
  248    xoption(Src, register_called(Which)),
  249    !,
  250    mode_hide_called(Which, Callable).
  251hide_called(Callable, _) :-
  252    mode_hide_called(non_built_in, Callable).
  253
  254mode_hide_called(all, _) :- !, fail.
  255mode_hide_called(non_iso, _:Goal) :-
  256    goal_name_arity(Goal, Name, Arity),
  257    current_predicate(system:Name/Arity),
  258    predicate_property(system:Goal, iso).
  259mode_hide_called(non_built_in, _:Goal) :-
  260    goal_name_arity(Goal, Name, Arity),
  261    current_predicate(system:Name/Arity),
  262    predicate_property(system:Goal, built_in).
  263mode_hide_called(non_built_in, M:Goal) :-
  264    goal_name_arity(Goal, Name, Arity),
  265    current_predicate(M:Name/Arity),
  266    predicate_property(M:Goal, built_in).
 built_in_predicate(+Callable)
True if Callable is a built-in
  272system_predicate(Goal) :-
  273    goal_name_arity(Goal, Name, Arity),
  274    current_predicate(system:Name/Arity),   % avoid autoloading
  275    predicate_property(system:Goal, built_in),
  276    !.
  277
  278
  279                /********************************
  280                *            TOPLEVEL           *
  281                ********************************/
  282
  283verbose(Src) :-
  284    \+ xoption(Src, silent(true)).
  285
  286:- thread_local
  287    xref_input/2.                   % File, Stream
 xref_source(+Source) is det
 xref_source(+Source, +Options) is det
Generate the cross-reference data for Source if not already done and the source is not modified. Checking for modifications is only done for files. Options processed:
silent(+Boolean)
If true (default false), emit warning messages.
module(+Module)
Define the initial context module to work in.
register_called(+Which)
Determines which calls are registerd. Which is one of all, non_iso or non_built_in (default).
comments(+CommentHandling)
How to handle comments. If store, comments are stored into the database as if the file was compiled. If collect, comments are entered to the xref database and made available through xref_mode/2 and xref_comment/4. If ignore, comments are simply ignored. Default is to collect comments.
process_include(+Boolean)
Process the content of included files (default is true).
stream(+Stream)
Process the input from Stream rather than opening Source.
Arguments:
Source- File specification or XPCE buffer
  317xref_source(Source) :-
  318    xref_source(Source, []).
  319
  320xref_source(Source, Options) :-
  321    prolog_canonical_source(Source, Src),
  322    (   last_modified(Source, Modified)
  323    ->  (   source(Src, Modified)
  324        ->  true
  325        ;   xref_clean(Src),
  326            assert(source(Src, Modified)),
  327            do_xref(Src, Options)
  328        )
  329    ;   xref_clean(Src),
  330        get_time(Now),
  331        assert(source(Src, Now)),
  332        do_xref(Src, Options)
  333    ).
  334
  335do_xref(Src, Options) :-
  336    must_be(list, Options),
  337    setup_call_cleanup(
  338        xref_setup(Src, In, Options, State),
  339        collect(Src, Src, In, Options),
  340        ( xref_cleanup(State),
  341          retractall(deferred_meta_call(_, _, Src, _)),
  342          retractall(qp_foreign_head(_, Src, _))
  343        )).
  344
  345last_modified(Source, Modified) :-
  346    prolog:xref_source_time(Source, Modified),
  347    !.
  348last_modified(Source, Modified) :-
  349    atom(Source),
  350    \+ is_global_url(Source),
  351    exists_file(Source),
  352    time_file(Source, Modified).
  353
  354is_global_url(File) :-
  355    sub_atom(File, B, _, _, '://'),
  356    !,
  357    B > 1,
  358    sub_atom(File, 0, B, _, Scheme),
  359    atom_codes(Scheme, Codes),
  360    maplist(between(0'a, 0'z), Codes).
  361
  362xref_setup(Src, In, Options, state(CleanIn, Dialect, Xref, [SRef|HRefs])) :-
  363    maplist(assert_option(Src), Options),
  364    assert_default_options(Src),
  365    current_prolog_flag(emulated_dialect, Dialect),
  366    (   option(stream(Stream), Options)
  367    ->  In = Stream,
  368        CleanIn = true
  369    ;   prolog_open_source(Src, In),
  370        CleanIn = prolog_close_source(In)
  371    ),
  372    set_initial_mode(In, Options),
  373    asserta(xref_input(Src, In), SRef),
  374    set_xref(Xref),
  375    (   verbose(Src)
  376    ->  HRefs = []
  377    ;   asserta((user:thread_message_hook(_,Level,_) :-
  378                     hide_message(Level)),
  379                Ref),
  380        HRefs = [Ref]
  381    ).
  382
  383hide_message(warning).
  384hide_message(error).
  385hide_message(informational).
  386
  387assert_option(_, Var) :-
  388    var(Var),
  389    !,
  390    instantiation_error(Var).
  391assert_option(Src, silent(Boolean)) :-
  392    !,
  393    must_be(boolean, Boolean),
  394    assert(xoption(Src, silent(Boolean))).
  395assert_option(Src, register_called(Which)) :-
  396    !,
  397    must_be(oneof([all,non_iso,non_built_in]), Which),
  398    assert(xoption(Src, register_called(Which))).
  399assert_option(Src, comments(CommentHandling)) :-
  400    !,
  401    must_be(oneof([store,collect,ignore]), CommentHandling),
  402    assert(xoption(Src, comments(CommentHandling))).
  403assert_option(Src, module(Module)) :-
  404    !,
  405    must_be(atom, Module),
  406    assert(xoption(Src, module(Module))).
  407assert_option(Src, process_include(Boolean)) :-
  408    !,
  409    must_be(boolean, Boolean),
  410    assert(xoption(Src, process_include(Boolean))).
  411assert_option(_, _).
  412
  413assert_default_options(Src) :-
  414    (   xref_option_default(Opt),
  415        generalise_term(Opt, Gen),
  416        (   xoption(Src, Gen)
  417        ->  true
  418        ;   assertz(xoption(Src, Opt))
  419        ),
  420        fail
  421    ;   true
  422    ).
  423
  424xref_option_default(silent(false)).
  425xref_option_default(register_called(non_built_in)).
  426xref_option_default(comments(collect)).
  427xref_option_default(process_include(true)).
 xref_cleanup(+State) is det
Restore processing state according to the saved State.
  433xref_cleanup(state(CleanIn, Dialect, Xref, Refs)) :-
  434    call(CleanIn),
  435    set_prolog_flag(emulated_dialect, Dialect),
  436    set_prolog_flag(xref, Xref),
  437    maplist(erase, Refs).
  438
  439set_xref(Xref) :-
  440    current_prolog_flag(xref, Xref),
  441    set_prolog_flag(xref, true).
  442
  443:- meta_predicate
  444    with_xref(0).  445
  446with_xref(Goal) :-
  447    setup_call_cleanup(
  448        push_prolog_flag(xref, true),
  449        Goal,
  450        pop_prolog_flag(xref)).
 set_initial_mode(+Stream, +Options) is det
Set the initial mode for processing this file in the cross-referencer. If the file is loaded, we use information from the previous load context, setting the appropriate module and dialect.
  460set_initial_mode(_Stream, Options) :-
  461    option(module(Module), Options),
  462    !,
  463    '$set_source_module'(Module).
  464set_initial_mode(Stream, _) :-
  465    stream_property(Stream, file_name(Path)),
  466    source_file_property(Path, load_context(M, _, Opts)),
  467    !,
  468    '$set_source_module'(M),
  469    (   option(dialect(Dialect), Opts)
  470    ->  expects_dialect(Dialect)
  471    ;   true
  472    ).
  473set_initial_mode(_, _) :-
  474    '$set_source_module'(user).
 xref_input_stream(-Stream) is det
Current input stream for cross-referencer.
  480xref_input_stream(Stream) :-
  481    xref_input(_, Var),
  482    !,
  483    Stream = Var.
 xref_push_op(Source, +Prec, +Type, :Name)
Define operators into the default source module and register them to be undone by pop_operators/0.
  490xref_push_op(Src, P, T, N0) :-
  491    '$current_source_module'(M0),
  492    strip_module(M0:N0, M, N),
  493    (   is_list(N),
  494        N \== []
  495    ->  maplist(push_op(Src, P, T, M), N)
  496    ;   push_op(Src, P, T, M, N)
  497    ).
  498
  499push_op(Src, P, T, M0, N0) :-
  500    strip_module(M0:N0, M, N),
  501    Name = M:N,
  502    valid_op(op(P,T,Name)),
  503    push_op(P, T, Name),
  504    assert_op(Src, op(P,T,Name)),
  505    debug(xref(op), ':- ~w.', [op(P,T,Name)]).
  506
  507valid_op(op(P,T,M:N)) :-
  508    atom(M),
  509    valid_op_name(N),
  510    integer(P),
  511    between(0, 1200, P),
  512    atom(T),
  513    op_type(T).
  514
  515valid_op_name(N) :-
  516    atom(N),
  517    !.
  518valid_op_name(N) :-
  519    N == [].
  520
  521op_type(xf).
  522op_type(yf).
  523op_type(fx).
  524op_type(fy).
  525op_type(xfx).
  526op_type(xfy).
  527op_type(yfx).
 xref_set_prolog_flag(+Flag, +Value, +Src, +Line)
Called when a directive sets a Prolog flag.
  533xref_set_prolog_flag(Flag, Value, Src, Line) :-
  534    atom(Flag),
  535    !,
  536    assertz(xflag(Flag, Value, Src, Line)).
  537xref_set_prolog_flag(_, _, _, _).
 xref_clean(+Source) is det
Reset the database for the given source.
  543xref_clean(Source) :-
  544    prolog_canonical_source(Source, Src),
  545    retractall(called(_, Src, _Origin, _Cond, _Line)),
  546    retractall(dynamic(_, Src, Line)),
  547    retractall(multifile(_, Src, Line)),
  548    retractall(public(_, Src, Line)),
  549    retractall(declared(_, _, Src, Line)),
  550    retractall(defined(_, Src, Line)),
  551    retractall(meta_goal(_, _, Src)),
  552    retractall(foreign(_, Src, Line)),
  553    retractall(constraint(_, Src, Line)),
  554    retractall(imported(_, Src, _From)),
  555    retractall(exported(_, Src)),
  556    retractall(uses_file(_, Src, _)),
  557    retractall(xmodule(_, Src)),
  558    retractall(xop(Src, _)),
  559    retractall(grammar_rule(_, Src)),
  560    retractall(xoption(Src, _)),
  561    retractall(xflag(_Name, _Value, Src, Line)),
  562    retractall(source(Src, _)),
  563    retractall(used_class(_, Src)),
  564    retractall(defined_class(_, _, _, Src, _)),
  565    retractall(mode(_, Src)),
  566    retractall(module_comment(Src, _, _)),
  567    retractall(pred_comment(_, Src, _, _)),
  568    retractall(pred_comment_link(_, Src, _)),
  569    retractall(pred_mode(_, Src, _)),
  570    retractall(object_comment(_, Src, _, _)),
  571    retractall(object_signature(_, Src, _)).
  572
  573
  574                 /*******************************
  575                 *          READ RESULTS        *
  576                 *******************************/
 xref_current_source(?Source)
Check what sources have been analysed.
  582xref_current_source(Source) :-
  583    source(Source, _Time).
 xref_done(+Source, -Time) is det
Cross-reference executed at Time
  590xref_done(Source, Time) :-
  591    prolog_canonical_source(Source, Src),
  592    source(Src, Time).
 xref_called(?Source, ?Called, ?By) is nondet
 xref_called(?Source, ?Called, ?By, ?Cond) is nondet
 xref_called(?Source, ?Called, ?By, ?Cond, ?Line) is nondet
True when By is called from Called in Source. Note that xref_called/3 and xref_called/4 use distinct/2 to return only distinct Called-By pairs. The xref_called/5 version may return duplicate Called-By if Called is called from multiple clauses in By, but at most one call per clause.
Arguments:
By- is a head term or one of the reserved terms '<directive>'(Line) or '<public>'(Line), indicating the call is from an (often initialization/1) directive or there is a public/1 directive that claims the predicate is called from in some untractable way.
Cond- is the (accumulated) condition as defined by :- if(Cond) under which the calling code is compiled.
Line- is the start line of the calling clause.
  614xref_called(Source, Called, By) :-
  615    xref_called(Source, Called, By, _).
  616
  617xref_called(Source, Called, By, Cond) :-
  618    canonical_source(Source, Src),
  619    distinct(Called-By, called(Called, Src, By, Cond, _)).
  620
  621xref_called(Source, Called, By, Cond, Line) :-
  622    canonical_source(Source, Src),
  623    called(Called, Src, By, Cond, Line).
 xref_defined(?Source, +Goal, ?How) is nondet
Test if Goal is accessible in Source. If this is the case, How specifies the reason why the predicate is accessible. Note that this predicate does not deal with built-in or global predicates, just locally defined and imported ones. How is one of of the terms below. Location is one of Line (an integer) or File:Line if the definition comes from an included (using :- include(File)) directive.
  645xref_defined(Source, Called, How) :-
  646    nonvar(Source),
  647    !,
  648    canonical_source(Source, Src),
  649    xref_defined2(How, Src, Called).
  650xref_defined(Source, Called, How) :-
  651    xref_defined2(How, Src, Called),
  652    canonical_source(Source, Src).
  653
  654xref_defined2(dynamic(Line), Src, Called) :-
  655    dynamic(Called, Src, Line).
  656xref_defined2(thread_local(Line), Src, Called) :-
  657    thread_local(Called, Src, Line).
  658xref_defined2(multifile(Line), Src, Called) :-
  659    multifile(Called, Src, Line).
  660xref_defined2(public(Line), Src, Called) :-
  661    public(Called, Src, Line).
  662xref_defined2(local(Line), Src, Called) :-
  663    defined(Called, Src, Line).
  664xref_defined2(foreign(Line), Src, Called) :-
  665    foreign(Called, Src, Line).
  666xref_defined2(constraint(Line), Src, Called) :-
  667    (   constraint(Called, Src, Line)
  668    ->  true
  669    ;   declared(Called, chr_constraint, Src, Line)
  670    ).
  671xref_defined2(imported(From), Src, Called) :-
  672    imported(Called, Src, From).
  673xref_defined2(dcg, Src, Called) :-
  674    grammar_rule(Called, Src).
 xref_definition_line(+How, -Line)
If the 3th argument of xref_defined contains line info, return this in Line.
  682xref_definition_line(local(Line),        Line).
  683xref_definition_line(dynamic(Line),      Line).
  684xref_definition_line(thread_local(Line), Line).
  685xref_definition_line(multifile(Line),    Line).
  686xref_definition_line(public(Line),       Line).
  687xref_definition_line(constraint(Line),   Line).
  688xref_definition_line(foreign(Line),      Line).
 xref_exported(?Source, ?Head) is nondet
True when Source exports Head.
  695xref_exported(Source, Called) :-
  696    prolog_canonical_source(Source, Src),
  697    exported(Called, Src).
 xref_module(?Source, ?Module) is nondet
True if Module is defined in Source.
  703xref_module(Source, Module) :-
  704    nonvar(Source),
  705    !,
  706    prolog_canonical_source(Source, Src),
  707    xmodule(Module, Src).
  708xref_module(Source, Module) :-
  709    xmodule(Module, Src),
  710    prolog_canonical_source(Source, Src).
 xref_uses_file(?Source, ?Spec, ?Path) is nondet
True when Source tries to load a file using Spec.
Arguments:
Spec- is a specification for absolute_file_name/3
Path- is either an absolute file name of the target file or the atom <not_found>.
  720xref_uses_file(Source, Spec, Path) :-
  721    prolog_canonical_source(Source, Src),
  722    uses_file(Spec, Src, Path).
 xref_op(?Source, Op) is nondet
Give the operators active inside the module. This is intended to setup the environment for incremental parsing of a term from the source-file.
Arguments:
Op- Term of the form op(Priority, Type, Name)
  732xref_op(Source, Op) :-
  733    prolog_canonical_source(Source, Src),
  734    xop(Src, Op).
 xref_prolog_flag(?Source, ?Flag, ?Value, ?Line) is nondet
True when Flag is set to Value at Line in Source. This is intended to support incremental parsing of a term from the source-file.
  742xref_prolog_flag(Source, Flag, Value, Line) :-
  743    prolog_canonical_source(Source, Src),
  744    xflag(Flag, Value, Src, Line).
  745
  746xref_built_in(Head) :-
  747    system_predicate(Head).
  748
  749xref_used_class(Source, Class) :-
  750    prolog_canonical_source(Source, Src),
  751    used_class(Class, Src).
  752
  753xref_defined_class(Source, Class, local(Line, Super, Summary)) :-
  754    prolog_canonical_source(Source, Src),
  755    defined_class(Class, Super, Summary, Src, Line),
  756    integer(Line).
  757xref_defined_class(Source, Class, file(File)) :-
  758    prolog_canonical_source(Source, Src),
  759    defined_class(Class, _, _, Src, file(File)).
  760
  761:- thread_local
  762    current_cond/1,
  763    source_line/1,
  764    current_test_unit/2,
  765    deferred_meta_call/4,           % Goal, Origin, Src, Line
  766    qp_foreign_head/3.              % Head, Src, Line
  767
  768current_source_line(Line) :-
  769    source_line(Var),
  770    !,
  771    Line = Var.
 collect(+Source, +File, +Stream, +Options)
Process data from Source. If File \== Source, we are processing an included file. Stream is the stream from which we read the program.
  779collect(Src, File, In, Options) :-
  780    (   Src == File
  781    ->  SrcSpec = Line
  782    ;   SrcSpec = (File:Line)
  783    ),
  784    (   current_prolog_flag(xref_store_comments, OldStore)
  785    ->  true
  786    ;   OldStore = false
  787    ),
  788    option(comments(CommentHandling), Options, collect),
  789    (   CommentHandling == ignore
  790    ->  CommentOptions = [],
  791        Comments = []
  792    ;   CommentHandling == store
  793    ->  CommentOptions = [ process_comment(true) ],
  794        Comments = [],
  795	set_prolog_flag(xref_store_comments, true)
  796    ;   CommentOptions = [ comments(Comments) ]
  797    ),
  798    repeat,
  799        E = error(_,_),
  800        catch(prolog_read_source_term(
  801                  In, Term, Expanded,
  802                  [ term_position(TermPos),
  803                    variable_names(VarNames)
  804                  | CommentOptions
  805                  ]),
  806              E, report_syntax_error(E, Src, [])),
  807        update_condition(Term),
  808        stream_position_data(line_count, TermPos, Line),
  809        setup_call_cleanup(
  810            asserta(source_line(SrcSpec), Ref),
  811            catch(process(Expanded, Comments, Term, VarNames, TermPos,
  812                          Src, EOF),
  813                  E, print_message(error, E)),
  814            erase(Ref)),
  815        EOF == true,
  816    !,
  817    set_prolog_flag(xref_store_comments, OldStore).
  818
  819report_syntax_error(_, _, Options) :-
  820    option(silent(true), Options),
  821    !,
  822    fail.
  823report_syntax_error(E, Src, _Options) :-
  824    (   verbose(Src)
  825    ->  print_message(error, E)
  826    ;   true
  827    ),
  828    fail.
 update_condition(+Term) is det
Update the condition under which the current code is compiled.
  834update_condition((:-Directive)) :-
  835    !,
  836    update_cond(Directive).
  837update_condition(_).
  838
  839update_cond(if(Cond)) :-
  840    !,
  841    asserta(current_cond(Cond)).
  842update_cond(else) :-
  843    retract(current_cond(C0)),
  844    !,
  845    assert(current_cond(\+C0)).
  846update_cond(elif(Cond)) :-
  847    retract(current_cond(C0)),
  848    !,
  849    assert(current_cond((\+C0,Cond))).
  850update_cond(endif) :-
  851    retract(current_cond(_)),
  852    !.
  853update_cond(_).
 current_condition(-Condition) is det
Condition is the current compilation condition as defined by the :- if/1 directive and friends.
  860current_condition(Condition) :-
  861    \+ current_cond(_),
  862    !,
  863    Condition = true.
  864current_condition(Condition) :-
  865    findall(C, current_cond(C), List),
  866    list_to_conj(List, Condition).
  867
  868list_to_conj([], true).
  869list_to_conj([C], C) :- !.
  870list_to_conj([H|T], (H,C)) :-
  871    list_to_conj(T, C).
  872
  873
  874                 /*******************************
  875                 *           PROCESS            *
  876                 *******************************/
 process(+Expanded, +Comments, +Term, +VarNames, +TermPos, +Src, -EOF) is det
Process a source term that has been subject to term expansion as well as its optional leading structured comments.
Arguments:
VarNames- is a list Name=Var for the variables of Term. It is passed to the comment processing.
TermPos- is the term position that describes the start of the term. We need this to find leading comments.
EOF- is unified with a boolean to indicate whether or not processing was stopped because end_of_file was processed.
  891process([], Comments, Term, VarNames, TermPos, Src, false) :-
  892    !,
  893    process_nil_expanded(Term, Src),
  894    xref_comments(Comments, TermPos, Term, VarNames, Src).
  895process(Expanded, Comments, Term0, VarNames, TermPos, Src, EOF) :-
  896    is_list(Expanded),                          % term_expansion into list.
  897    !,
  898    (   member(Term, Expanded),
  899        process(Term, Term0, Src),
  900        Term == end_of_file
  901    ->  EOF = true
  902    ;   EOF = false
  903    ),
  904    xref_comments(Comments, TermPos, Term0, VarNames, Src).
  905process(end_of_file, _, _, _, _, _, true) :-
  906    !.
  907process(Term, Comments, Term0, VarNames, TermPos, Src, false) :-
  908    process(Term, Term0, Src),
  909    xref_comments(Comments, TermPos, Term0, VarNames, Src).
 process_nil_expanded(+Term, +Src) is det
Process terms that are term-expanded to [], but have side effects.
  915process_nil_expanded((:- pce_class_directive(Goal)), Src) :-
  916    !,
  917    current_source_line(Line),
  918    assert_called(Src, '<directive>'(Line), Goal, Line).
  919process_nil_expanded(_, _).
 process(+Term, +Term0, +Src) is det
  923process(_, Term0, _) :-
  924    ignore_raw_term(Term0),
  925    !.
  926process(Head :- Body, Head0 --> _, Src) :-
  927    pi_head(F/A, Head),
  928    pi_head(F/A0, Head0),
  929    A =:= A0 + 2,
  930    !,
  931    assert_grammar_rule(Src, Head),
  932    process((Head :- Body), Src).
  933process(Term, _Term0, Src) :-
  934    process(Term, Src).
  935
  936ignore_raw_term((:- predicate_options(_,_,_))).
 process(+Term, +Src) is det
  940process(Var, _) :-
  941    var(Var),
  942    !.                    % Warn?
  943process(end_of_file, _) :- !.
  944process((:- Directive), Src) :-
  945    !,
  946    process_directive(Directive, Src),
  947    !.
  948process((?- Directive), Src) :-
  949    !,
  950    process_directive(Directive, Src),
  951    !.
  952process((Head :- Body), Src) :-
  953    !,
  954    assert_defined(Src, Head),
  955    process_body(Body, Head, Src).
  956process((Left => Body), Src) :-
  957    !,
  958    (   nonvar(Left),
  959        Left = (Head, Guard)
  960    ->  assert_defined(Src, Head),
  961        process_body(Guard, Head, Src),
  962        process_body(Body, Head, Src)
  963    ;   assert_defined(Src, Left),
  964        process_body(Body, Left, Src)
  965    ).
  966process(?=>(Head, Body), Src) :-
  967    !,
  968    assert_defined(Src, Head),
  969    process_body(Body, Head, Src).
  970process('$source_location'(_File, _Line):Clause, Src) :-
  971    !,
  972    process(Clause, Src).
  973process(Term, Src) :-
  974    process_chr(Term, Src),
  975    !.
  976process(M:(Head :- Body), Src) :-
  977    !,
  978    process((M:Head :- M:Body), Src).
  979process(Head, Src) :-
  980    assert_defined(Src, Head),
  981    qp_foreign_declaration(Head, Src).
 qp_foreign_declaration(+Head, +Src) is det
Record the predicate declared by a clause foreign/2,3 for library(qpforeign). It becomes a foreign predicate of Src if Src calls load_foreign_resource/1,2 (see process_qp_foreign/1).
  989qp_foreign_declaration(foreign(_Function, PredSpec), Src) :-
  990    !,
  991    qp_foreign_head(PredSpec, Src).
  992qp_foreign_declaration(foreign(_Function, _Lang, PredSpec), Src) :-
  993    !,
  994    qp_foreign_head(PredSpec, Src).
  995qp_foreign_declaration(_, _).
  996
  997qp_foreign_head(PredSpec, Src) :-
  998    callable(PredSpec),
  999    !,
 1000    generalise(PredSpec, Head),
 1001    current_source_line(Line),
 1002    assertz(qp_foreign_head(Head, Src, Line)).
 1003qp_foreign_head(_, _).
 1004
 1005process_qp_foreign(Src) :-
 1006    forall(retract(qp_foreign_head(Head, Src, Line)),
 1007           (   foreign(Head, Src, _)
 1008           ->  true
 1009           ;   assert(foreign(Head, Src, Line))
 1010           )).
 1011
 1012
 1013                 /*******************************
 1014                 *            COMMENTS          *
 1015                 *******************************/
 xref_comments(+Comments, +FilePos, +Term, +VarNames, +Src) is det
Process the structured comments before Term. The last comment before Term is parsed with Term and VarNames, a list Name=Var for the variables of Term.
 1023xref_comments([], _Pos, _Term, _VarNames, _Src).
 1024:- if(current_predicate(parse_comment/5)). 1025xref_comments([Pos-Comment|T], TermPos, Term, VarNames, Src) :-
 1026    (   Pos @> TermPos              % comments inside term
 1027    ->  true
 1028    ;   stream_position_data(line_count, Pos, Line),
 1029        FilePos = Src:Line,
 1030        (   last_comment(T, TermPos)
 1031        ->  CTerm = Term
 1032        ;   true
 1033        ),
 1034        (   parse_comment(Comment, FilePos, CTerm, VarNames, Parsed)
 1035        ->  assert_comments(Parsed, Src)
 1036        ;   true
 1037        ),
 1038        xref_comments(T, TermPos, Term, VarNames, Src)
 1039    ).
 1040
 1041last_comment([], _).
 1042last_comment([Pos-_|_], TermPos) :-
 1043    Pos @> TermPos.
 1044
 1045assert_comments([], _).
 1046assert_comments([H|T], Src) :-
 1047    assert_comment(H, Src),
 1048    assert_comments(T, Src).
 1049
 1050assert_comment(section(Id, Title, Comment), Src) :-
 1051    (   Id = module(_)
 1052    ->  assertz(module_comment(Src, Title, Comment))
 1053    ;   true
 1054    ).
 1055assert_comment(predicate(PI, Summary, Comment), Src) :-
 1056    pi_to_head(PI, Src, Head),
 1057    assertz(pred_comment(Head, Src, Summary, Comment)).
 1058assert_comment(link(PI, PITo), Src) :-
 1059    pi_to_head(PI, Src, Head),
 1060    pi_to_head(PITo, Src, HeadTo),
 1061    assertz(pred_comment_link(Head, Src, HeadTo)).
 1062assert_comment(mode(Head, Det), Src) :-
 1063    assertz(pred_mode(Head, Src, Det)).
 1064assert_comment(object(Object, Summary, Comment), Src) :-
 1065    assertz(object_comment(Object, Src, Summary, Comment)).
 1066assert_comment(signature(Object, Signature), Src) :-
 1067    assertz(object_signature(Object, Src, Signature)).
 1068
 1069pi_to_head(PI, Src, Head) :-
 1070    pi_to_head(PI, Head0),
 1071    (   Head0 = _:_
 1072    ->  strip_module(Head0, M, Plain),
 1073        (   xmodule(M, Src)
 1074        ->  Head = Plain
 1075        ;   Head = M:Plain
 1076        )
 1077    ;   Head = Head0
 1078    ).
 1079:- endif.
 xref_comment(?Source, ?Title, ?Comment) is nondet
Is true when Source has a section comment with Title and Comment
 1085xref_comment(Source, Title, Comment) :-
 1086    canonical_source(Source, Src),
 1087    module_comment(Src, Title, Comment).
 xref_comment(?Source, ?Head, ?Summary, ?Comment) is nondet
Is true when Head in Source has the given PlDoc comment.
 1093xref_comment(Source, Head, Summary, Comment) :-
 1094    canonical_source(Source, Src),
 1095    (   pred_comment(Head, Src, Summary, Comment)
 1096    ;   pred_comment_link(Head, Src, HeadTo),
 1097        pred_comment(HeadTo, Src, Summary, Comment)
 1098    ).
 xref_object_comment(?Source, ?Object, ?Summary, ?Comment) is nondet
Is true when Object in Source has the given PlDoc comment. Such objects are created by the hook doc_compile_comment/5, e.g., xpce(Class, Kind, Name) for the members of xpce classes.
 1106xref_object_comment(Source, Object, Summary, Comment) :-
 1107    canonical_source(Source, Src),
 1108    object_comment(Object, Src, Summary, Comment).
 xref_object_signature(?Source, ?Object, ?Signature) is nondet
Is true when Object in Source has Signature as provided by the hook doc_compile_comment/5.
 1115xref_object_signature(Source, Object, Signature) :-
 1116    canonical_source(Source, Src),
 1117    object_signature(Object, Src, Signature).
 xref_mode(?Source, ?Mode, ?Det) is nondet
Is true when Source provides a predicate with Mode and determinism.
 1124xref_mode(Source, Mode, Det) :-
 1125    canonical_source(Source, Src),
 1126    pred_mode(Mode, Src, Det).
 xref_option(?Source, ?Option) is nondet
True when Source was processed using Option. Options are defined with xref_source/2.
 1133xref_option(Source, Option) :-
 1134    canonical_source(Source, Src),
 1135    xoption(Src, Option).
 1136
 1137
 1138                 /********************************
 1139                 *           DIRECTIVES         *
 1140                 ********************************/
 1141
 1142process_directive(Var, _) :-
 1143    var(Var),
 1144    !.                    % error, but that isn't our business
 1145process_directive(Dir, _Src) :-
 1146    debug(xref(directive), 'Processing :- ~q', [Dir]),
 1147    fail.
 1148process_directive((A,B), Src) :-       % TBD: what about other control
 1149    !,
 1150    process_directive(A, Src),      % structures?
 1151    process_directive(B, Src).
 1152process_directive(List, Src) :-
 1153    is_list(List),
 1154    !,
 1155    process_directive(consult(List), Src).
 1156process_directive(use_module(File, Import), Src) :-
 1157    process_use_module2(File, Import, Src, false).
 1158process_directive(autoload(File, Import), Src) :-
 1159    process_use_module2(File, Import, Src, false).
 1160process_directive(require(Import), Src) :-
 1161    process_requires(Import, Src).
 1162process_directive(expects_dialect(Dialect), Src) :-
 1163    process_directive(use_module(library(dialect/Dialect)), Src),
 1164    expects_dialect(Dialect).
 1165process_directive(reexport(File, Import), Src) :-
 1166    process_use_module2(File, Import, Src, true).
 1167process_directive(reexport(Modules), Src) :-
 1168    process_use_module(Modules, Src, true).
 1169process_directive(autoload(Modules), Src) :-
 1170    process_use_module(Modules, Src, false).
 1171process_directive(use_module(Modules), Src) :-
 1172    process_use_module(Modules, Src, false).
 1173process_directive(consult(Modules), Src) :-
 1174    process_use_module(Modules, Src, false).
 1175process_directive(ensure_loaded(Modules), Src) :-
 1176    process_use_module(Modules, Src, false).
 1177process_directive(load_files(Files, _Options), Src) :-
 1178    process_use_module(Files, Src, false).
 1179process_directive(include(Files), Src) :-
 1180    process_include(Files, Src).
 1181process_directive(dynamic(Dynamic), Src) :-
 1182    process_predicates(assert_dynamic, Dynamic, Src).
 1183process_directive(dynamic(Dynamic, _Options), Src) :-
 1184    process_predicates(assert_dynamic, Dynamic, Src).
 1185process_directive(thread_local(Dynamic), Src) :-
 1186    process_predicates(assert_thread_local, Dynamic, Src).
 1187process_directive(multifile(Dynamic), Src) :-
 1188    process_predicates(assert_multifile, Dynamic, Src).
 1189process_directive(public(Public), Src) :-
 1190    process_predicates(assert_public, Public, Src).
 1191process_directive(export(Export), Src) :-
 1192    process_predicates(assert_export, Export, Src).
 1193process_directive(import(Import), Src) :-
 1194    process_import(Import, Src).
 1195process_directive(module(Module, Export), Src) :-
 1196    assert_module(Src, Module),
 1197    assert_module_export(Src, Export).
 1198process_directive(module(Module, Export, Import), Src) :-
 1199    assert_module(Src, Module),
 1200    assert_module_export(Src, Export),
 1201    assert_module3(Import, Src).
 1202process_directive(begin_tests(Unit, _Options), Src) :-
 1203    enter_test_unit(Unit, Src).
 1204process_directive(begin_tests(Unit), Src) :-
 1205    enter_test_unit(Unit, Src).
 1206process_directive(end_tests(Unit), Src) :-
 1207    leave_test_unit(Unit, Src).
 1208process_directive('$set_source_module'(system), Src) :-
 1209    assert_module(Src, system).     % hack for handling boot/init.pl
 1210process_directive(pce_begin_class_definition(Name, Meta, Super, Doc), Src) :-
 1211    assert_defined_class(Src, Name, Meta, Super, Doc).
 1212process_directive(pce_autoload(Name, From), Src) :-
 1213    assert_defined_class(Src, Name, imported_from(From)).
 1214
 1215process_directive(op(P, A, N), Src) :-
 1216    xref_push_op(Src, P, A, N).
 1217process_directive(set_prolog_flag(Flag, Value), Src) :-
 1218    (   Flag == character_escapes
 1219    ->  set_prolog_flag(character_escapes, Value)
 1220    ;   true
 1221    ),
 1222    current_source_line(Line),
 1223    xref_set_prolog_flag(Flag, Value, Src, Line).
 1224process_directive(style_check(X), _) :-
 1225    style_check(X).
 1226process_directive(encoding(Enc), _) :-
 1227    (   xref_input_stream(Stream)
 1228    ->  catch(set_stream(Stream, encoding(Enc)), error(_,_), true)
 1229    ;   true                        % can this happen?
 1230    ).
 1231process_directive(pce_expansion:push_compile_operators, _) :-
 1232    '$current_source_module'(SM),
 1233    call(pce_expansion:push_compile_operators(SM)). % call to avoid xref
 1234process_directive(pce_expansion:pop_compile_operators, _) :-
 1235    call(pce_expansion:pop_compile_operators).
 1236process_directive(meta_predicate(Meta), Src) :-
 1237    process_meta_predicate(Meta, Src).
 1238process_directive(arithmetic_function(FSpec), Src) :-
 1239    arith_callable(FSpec, Goal),
 1240    !,
 1241    current_source_line(Line),
 1242    assert_called(Src, '<directive>'(Line), Goal, Line).
 1243process_directive(format_predicate(_, Goal), Src) :-
 1244    !,
 1245    current_source_line(Line),
 1246    assert_called(Src, '<directive>'(Line), Goal, Line).
 1247process_directive(if(Cond), Src) :-
 1248    !,
 1249    current_source_line(Line),
 1250    assert_called(Src, '<directive>'(Line), Cond, Line).
 1251process_directive(elif(Cond), Src) :-
 1252    !,
 1253    current_source_line(Line),
 1254    assert_called(Src, '<directive>'(Line), Cond, Line).
 1255process_directive(else, _) :- !.
 1256process_directive(endif, _) :- !.
 1257process_directive(Goal, Src) :-
 1258    current_source_line(Line),
 1259    process_body(Goal, '<directive>'(Line), Src).
 process_meta_predicate(+Decl, +Src)
Create meta_goal/3 facts from the meta-goal declaration.
 1265process_meta_predicate((A,B), Src) :-
 1266    !,
 1267    process_meta_predicate(A, Src),
 1268    process_meta_predicate(B, Src).
 1269process_meta_predicate(Decl, Src) :-
 1270    process_meta_head(Src, Decl).
 1271
 1272process_meta_head(Src, Decl) :-         % swapped arguments for maplist
 1273    compound(Decl),
 1274    compound_name_arity(Decl, Name, Arity),
 1275    compound_name_arity(Head, Name, Arity),
 1276    meta_args(1, Arity, Decl, Head, Meta),
 1277    (   (   prolog:meta_goal(Head, _)
 1278        ;   prolog:called_by(Head, _, _, _)
 1279        ;   prolog:called_by(Head, _)
 1280        ;   meta_goal(Head, Meta, _Src)
 1281        )
 1282    ->  true
 1283    ;   warn_late_meta_predicate(Decl, Src),
 1284        retractall(meta_goal(Head, _, Src)),
 1285        assert(meta_goal(Head, Meta, Src)),
 1286        process_deferred_meta_calls(Head, Src)
 1287    ).
 defer_meta_call(+Goal, +Origin, +Src, +Line) is det
 process_deferred_meta_calls(+Head, +Src) is det
A goal that is not known to be a meta-predicate may become one by a later meta_predicate/1 declaration. If such a goal has an argument that may be a goal or closure, we remember it. If a later declaration makes it a meta-predicate, we process its meta-arguments as if the declaration preceded the goal.
 1298defer_meta_call(Goal, Origin, Src, Line) :-
 1299    compound(Goal),
 1300    Goal \= _:_,
 1301    arg(_, Goal, A),
 1302    nonvar(A),
 1303    callable(A),
 1304    !,
 1305    assertz(deferred_meta_call(Goal, Origin, Src, Line)).
 1306defer_meta_call(_, _, _, _).
 1307
 1308process_deferred_meta_calls(Head, Src) :-
 1309    compound_name_arity(Head, Name, Arity),
 1310    compound_name_arity(Goal, Name, Arity),
 1311    forall(retract(deferred_meta_call(Goal, Origin, Src, Line)),
 1312           setup_call_cleanup(
 1313               asserta(source_line(Line), Ref),
 1314               ignore(process_deferred_meta_call(Goal, Origin, Src)),
 1315               erase(Ref))).
 1316
 1317process_deferred_meta_call(Goal, Origin, Src) :-
 1318    xref_meta_src(Goal, Metas, Src),
 1319    forall(limit(100, process_called_list(Metas, Origin, Src, _)),
 1320           true).
 1321
 1322meta_args(I, Arity, _, _, []) :-
 1323    I > Arity,
 1324    !.
 1325meta_args(I, Arity, Decl, Head, [H|T]) :-               % 0
 1326    arg(I, Decl, 0),
 1327    !,
 1328    arg(I, Head, H),
 1329    I2 is I + 1,
 1330    meta_args(I2, Arity, Decl, Head, T).
 1331meta_args(I, Arity, Decl, Head, [H|T]) :-               % ^
 1332    arg(I, Decl, ^),
 1333    !,
 1334    arg(I, Head, EH),
 1335    setof_goal(EH, H),
 1336    I2 is I + 1,
 1337    meta_args(I2, Arity, Decl, Head, T).
 1338meta_args(I, Arity, Decl, Head, [//(H)|T]) :-
 1339    arg(I, Decl, //),
 1340    !,
 1341    arg(I, Head, H),
 1342    I2 is I + 1,
 1343    meta_args(I2, Arity, Decl, Head, T).
 1344meta_args(I, Arity, Decl, Head, [H+A|T]) :-             % I --> H+I
 1345    arg(I, Decl, A),
 1346    integer(A), A > 0,
 1347    !,
 1348    arg(I, Head, H),
 1349    I2 is I + 1,
 1350    meta_args(I2, Arity, Decl, Head, T).
 1351meta_args(I, Arity, Decl, Head, Meta) :-
 1352    I2 is I + 1,
 1353    meta_args(I2, Arity, Decl, Head, Meta).
 1354
 1355
 1356warn_late_meta_predicate(Decl, Src) :-
 1357    xref_called(Src, Decl, By),
 1358    !,
 1359    print_message(warning, meta_predicate_after_call(Decl, By)).
 1360warn_late_meta_predicate(_, _).
 1361
 1362
 1363              /********************************
 1364              *             BODY              *
 1365              ********************************/
 xref_meta(+Source, +Head, -Called) is semidet
True when Head calls Called in Source.
Arguments:
Called- is a list of called terms, terms of the form Term+Extra or terms of the form //(Term).
 1374xref_meta(Source, Head, Called) :-
 1375    canonical_source(Source, Src),
 1376    xref_meta_src(Head, Called, Src).
 xref_meta(+Head, -Called) is semidet
 xref_meta_src(+Head, -Called, +Src) is semidet
True when Called is a list of terms called from Head. Each element in Called can be of the form Term+Int, which means that Term must be extended with Int additional arguments. The variant xref_meta/3 first queries the local context.
deprecated
- New code should use xref_meta/3.
To be done
- Split predifined in several categories. E.g., the ISO predicates cannot be redefined.
- Rely on the meta_predicate property for many predicates.
 1391xref_meta_src(Head, Called, Src) :-
 1392    meta_goal(Head, Called, Src),
 1393    !.
 1394xref_meta_src(Head, Called, _) :-
 1395    xref_meta(Head, Called),
 1396    !.
 1397xref_meta_src(Head, Called, _) :-
 1398    compound(Head),
 1399    compound_name_arity(Head, Name, Arity),
 1400    apply_pred(Name),
 1401    Arity > 5,
 1402    !,
 1403    Extra is Arity - 1,
 1404    arg(1, Head, G),
 1405    Called = [G+Extra].
 1406xref_meta_src(Head, Called, _) :-
 1407    with_xref(predicate_property('$xref_tmp':Head, meta_predicate(Meta))),
 1408    !,
 1409    Meta =.. [_|Args],
 1410    meta_args(Args, 1, Head, Called).
 1411
 1412meta_args([], _, _, []).
 1413meta_args([H0|T0], I, Head, [H|T]) :-
 1414    xargs(H0, N),
 1415    !,
 1416    arg(I, Head, A),
 1417    (   N == 0
 1418    ->  H = A
 1419    ;   H = (A+N)
 1420    ),
 1421    I2 is I+1,
 1422    meta_args(T0, I2, Head, T).
 1423meta_args([_|T0], I, Head, T) :-
 1424    I2 is I+1,
 1425    meta_args(T0, I2, Head, T).
 1426
 1427xargs(N, N) :- integer(N), !.
 1428xargs(//, 2).
 1429xargs(^, 0).
 1430
 1431apply_pred(call).                               % built-in
 1432apply_pred(maplist).                            % library(apply_macros)
 1433
 1434xref_meta((A, B),               [A, B]).
 1435xref_meta((A; B),               [A, B]).
 1436xref_meta((A| B),               [A, B]).
 1437xref_meta((A -> B),             [A, B]).
 1438xref_meta((A *-> B),            [A, B]).
 1439xref_meta(findall(_V,G,_L),     [G]).
 1440xref_meta(findall(_V,G,_L,_T),  [G]).
 1441xref_meta(findnsols(_N,_V,G,_L),    [G]).
 1442xref_meta(findnsols(_N,_V,G,_L,_T), [G]).
 1443xref_meta(setof(_V, EG, _L),    [G]) :-
 1444    setof_goal(EG, G).
 1445xref_meta(bagof(_V, EG, _L),    [G]) :-
 1446    setof_goal(EG, G).
 1447xref_meta(aggregate(_V, EG, _L), [G]) :-
 1448    setof_goal(EG, G).
 1449xref_meta(aggregate(_V, _D, EG, _L), [G]) :-
 1450    setof_goal(EG, G).
 1451xref_meta(forall(A, B),         [A, B]).
 1452xref_meta(maplist(G,_),         [G+1]).
 1453xref_meta(maplist(G,_,_),       [G+2]).
 1454xref_meta(maplist(G,_,_,_),     [G+3]).
 1455xref_meta(maplist(G,_,_,_,_),   [G+4]).
 1456xref_meta(map_list_to_pairs(G,_,_), [G+2]).
 1457xref_meta(map_assoc(G, _),      [G+1]).
 1458xref_meta(map_assoc(G, _, _),   [G+2]).
 1459xref_meta(checklist(G, _L),     [G+1]).
 1460xref_meta(sublist(G, _, _),     [G+1]).
 1461xref_meta(include(G, _, _),     [G+1]).
 1462xref_meta(exclude(G, _, _),     [G+1]).
 1463xref_meta(partition(G, _, _, _, _),     [G+2]).
 1464xref_meta(partition(G, _, _, _),[G+1]).
 1465xref_meta(call(G),              [G]).
 1466xref_meta(call(G, _),           [G+1]).
 1467xref_meta(call(G, _, _),        [G+2]).
 1468xref_meta(call(G, _, _, _),     [G+3]).
 1469xref_meta(call(G, _, _, _, _),  [G+4]).
 1470xref_meta(not(G),               [G]).
 1471xref_meta(notrace(G),           [G]).
 1472xref_meta('$notrace'(G),        [G]).
 1473xref_meta(\+(G),                [G]).
 1474xref_meta(ignore(G),            [G]).
 1475xref_meta(once(G),              [G]).
 1476xref_meta(initialization(G),    [G]).
 1477xref_meta(initialization(G,_),  [G]).
 1478xref_meta(retract(Rule),        [G]) :- head_of(Rule, G).
 1479xref_meta(clause(G, _),         [G]).
 1480xref_meta(clause(G, _, _),      [G]).
 1481xref_meta(phrase(G, _A),        [//(G)]).
 1482xref_meta(phrase(G, _A, _R),    [//(G)]).
 1483xref_meta(call_dcg(G, _A, _R),  [//(G)]).
 1484xref_meta(phrase_from_file(G,_),[//(G)]).
 1485xref_meta(catch(A, _, B),       [A, B]).
 1486xref_meta(catch_with_backtrace(A, _, B), [A, B]).
 1487xref_meta(thread_create(A,_,_), [A]).
 1488xref_meta(thread_create(A,_),   [A]).
 1489xref_meta(thread_signal(_,A),   [A]).
 1490xref_meta(thread_idle(A,_),     [A]).
 1491xref_meta(thread_at_exit(A),    [A]).
 1492xref_meta(thread_initialization(A), [A]).
 1493xref_meta(engine_create(_,A,_), [A]).
 1494xref_meta(engine_create(_,A,_,_), [A]).
 1495xref_meta(transaction(A),       [A]).
 1496xref_meta(transaction(A,B,_),   [A,B]).
 1497xref_meta(snapshot(A),          [A]).
 1498xref_meta(predsort(A,_,_),      [A+3]).
 1499xref_meta(call_cleanup(A, B),   [A, B]).
 1500xref_meta(call_cleanup(A, _, B),[A, B]).
 1501xref_meta(setup_call_cleanup(A, B, C),[A, B, C]).
 1502xref_meta(setup_call_catcher_cleanup(A, B, _, C),[A, B, C]).
 1503xref_meta(call_residue_vars(A,_), [A]).
 1504xref_meta(with_mutex(_,A),      [A]).
 1505xref_meta(assume(G),            [G]).   % library(debug)
 1506xref_meta(assertion(G),         [G]).   % library(debug)
 1507xref_meta(freeze(_, G),         [G]).
 1508xref_meta(when(C, A),           [C, A]).
 1509xref_meta(time(G),              [G]).   % development system
 1510xref_meta(call_time(G, _),      [G]).   % development system
 1511xref_meta(call_time(G, _, _),   [G]).   % development system
 1512xref_meta(profile(G),           [G]).
 1513xref_meta(at_halt(G),           [G]).
 1514xref_meta(call_with_time_limit(_, G), [G]).
 1515xref_meta(call_with_depth_limit(G, _, _), [G]).
 1516xref_meta(call_with_inference_limit(G, _, _), [G]).
 1517xref_meta(alarm(_, G, _),       [G]).
 1518xref_meta(alarm(_, G, _, _),    [G]).
 1519xref_meta('$add_directive_wic'(G), [G]).
 1520xref_meta(with_output_to(_, G), [G]).
 1521xref_meta(if(G),                [G]).
 1522xref_meta(elif(G),              [G]).
 1523xref_meta(meta_options(G,_,_),  [G+1]).
 1524xref_meta(on_signal(_,_,H),     [H+1]) :- H \== default.
 1525xref_meta(distinct(G),          [G]).   % library(solution_sequences)
 1526xref_meta(distinct(_, G),       [G]).
 1527xref_meta(order_by(_, G),       [G]).
 1528xref_meta(limit(_, G),          [G]).
 1529xref_meta(offset(_, G),         [G]).
 1530xref_meta(reset(G,_,_),         [G]).
 1531xref_meta(prolog_listen(Ev,G),  [G+N]) :- event_xargs(Ev, N).
 1532xref_meta(prolog_listen(Ev,G,_),[G+N]) :- event_xargs(Ev, N).
 1533xref_meta(tnot(G),		[G]).
 1534xref_meta(not_exists(G),	[G]).
 1535xref_meta(with_tty_raw(G),	[G]).
 1536xref_meta(residual_goals(G),    [G+2]).
 1537
 1538                                        % XPCE meta-predicates
 1539xref_meta(pce_global(_, new(_)), _) :- !, fail.
 1540xref_meta(pce_global(_, B),     [B+1]).
 1541xref_meta(ifmaintainer(G),      [G]).   % used in manual
 1542xref_meta(listen(_, G),         [G]).   % library(broadcast)
 1543xref_meta(listen(_, _, G),      [G]).
 1544xref_meta(in_pce_thread(G),     [G]).
 1545
 1546xref_meta(G, Meta) :-                   % call user extensions
 1547    prolog:meta_goal(G, Meta).
 1548xref_meta(G, Meta) :-                   % Generated from :- meta_predicate
 1549    meta_goal(G, Meta, _Src).
 1550
 1551setof_goal(EG, G) :-
 1552    var(EG), !, G = EG.
 1553setof_goal(_^EG, G) :-
 1554    !,
 1555    setof_goal(EG, G).
 1556setof_goal(G, G).
 1557
 1558event_xargs(abort,            0).
 1559event_xargs(erase,            1).
 1560event_xargs(break,            3).
 1561event_xargs(frame_finished,   1).
 1562event_xargs(thread_exit,      1).
 1563event_xargs(this_thread_exit, 0).
 1564event_xargs(PI,               2) :- pi_to_head(PI, _).
 head_of(+Rule, -Head)
Get the head for a retract call.
 1570head_of(Var, _) :-
 1571    var(Var), !, fail.
 1572head_of((Head :- _), Head).
 1573head_of(Head, Head).
 xref_hook(?Callable)
Definition of known hooks. Hooks that can be called in any module are unqualified. Other hooks are qualified with the module where they are called.
 1581xref_hook(Hook) :-
 1582    prolog:hook(Hook).
 1583xref_hook(Hook) :-
 1584    hook(Hook).
 1585
 1586
 1587hook(attr_portray_hook(_,_)).
 1588hook(attr_unify_hook(_,_)).
 1589hook(attribute_goals(_,_,_)).
 1590hook(goal_expansion(_,_)).
 1591hook(term_expansion(_,_)).
 1592hook(goal_expansion(_,_,_,_)).
 1593hook(term_expansion(_,_,_,_)).
 1594hook(resource(_,_,_)).
 1595hook('$pred_option'(_,_,_,_)).
 1596hook('$nowarn_autoload'(_,_)).
 1597
 1598hook(emacs_prolog_colours:goal_classification(_,_)).
 1599hook(emacs_prolog_colours:goal_colours(_,_)).
 1600hook(emacs_prolog_colours:identify(_,_)).
 1601hook(emacs_prolog_colours:style(_,_)).
 1602hook(emacs_prolog_colours:term_colours(_,_)).
 1603hook(pce_principal:get_implementation(_,_,_,_)).
 1604hook(pce_principal:pce_class(_,_,_,_,_,_)).
 1605hook(pce_principal:pce_lazy_get_method(_,_,_)).
 1606hook(pce_principal:pce_lazy_send_method(_,_,_)).
 1607hook(pce_principal:pce_uses_template(_,_)).
 1608hook(pce_principal:send_implementation(_,_,_)).
 1609hook(predicate_options:option_decl(_,_,_)).
 1610hook(prolog:debug_control_hook(_)).
 1611hook(prolog:error_message(_,_,_)).
 1612hook(prolog:expand_answer(_,_,_)).
 1613hook(prolog:general_exception(_,_)).
 1614hook(prolog:help_hook(_)).
 1615hook(prolog:locate_clauses(_,_)).
 1616hook(prolog:message(_,_,_)).
 1617hook(prolog:message_context(_,_,_)).
 1618hook(prolog:message_line_element(_,_)).
 1619hook(prolog:message_location(_,_,_)).
 1620hook(prolog:predicate_summary(_,_)).
 1621hook(prolog:prolog_exception_hook(_,_,_,_,_)).
 1622hook(prolog:residual_goals(_,_)).
 1623hook(prolog_edit:load).
 1624hook(prolog_edit:locate(_,_,_)).
 1625hook(sandbox:safe_directive(_)).
 1626hook(sandbox:safe_global_variable(_)).
 1627hook(sandbox:safe_meta(_,_)).
 1628hook(sandbox:safe_meta_predicate(_)).
 1629hook(sandbox:safe_primitive(_)).
 1630hook(sandbox:safe_prolog_flag(_,_)).
 1631hook(shlib:unload_all_foreign_libraries).
 1632hook(system:'$foreign_registered'(_, _)).
 1633hook(user:exception(_,_,_)).
 1634hook(user:expand_answer(_,_)).
 1635hook(user:expand_query(_,_,_,_)).
 1636hook(user:file_search_path(_,_)).
 1637hook(user:library_directory(_)).
 1638hook(user:message_hook(_,_,_)).
 1639hook(prolog:message_action(_,_)).
 1640hook(user:portray(_)).
 1641hook(user:prolog_clause_name(_,_)).
 1642hook(user:prolog_list_goal(_)).
 1643hook(user:prolog_predicate_name(_,_)).
 1644hook(user:prolog_trace_interception(_,_,_,_)).
 arith_callable(+Spec, -Callable)
Translate argument of arithmetic_function/1 into a callable term
 1650arith_callable(Var, _) :-
 1651    var(Var), !, fail.
 1652arith_callable(Module:Spec, Module:Goal) :-
 1653    !,
 1654    arith_callable(Spec, Goal).
 1655arith_callable(Name/Arity, Goal) :-
 1656    PredArity is Arity + 1,
 1657    functor(Goal, Name, PredArity).
 process_body(+Body, +Origin, +Src) is det
Process a callable body (body of a clause or directive). Origin describes the origin of the call. Partial evaluation may lead to non-determinism, which is why we backtrack over process_goal/3.

We limit the number of explored paths to 100 to avoid getting trapped in this analysis.

 1668process_body(Body, Origin, Src) :-
 1669    forall(limit(100, process_goal(Body, Origin, Src, _Partial)),
 1670           true).
 process_goal(+Goal, +Origin, +Src, ?Partial) is multi
Xref Goal. The argument Partial is bound to true if there was a partial evalation inside Goal that has bound variables.
 1677process_goal(Var, _, _, _) :-
 1678    var(Var),
 1679    !.
 1680process_goal(_:Goal, _, _, _) :-
 1681    var(Goal),
 1682    !.
 1683process_goal(Goal, Origin, Src, P) :-
 1684    Goal = (_,_),                               % problems
 1685    !,
 1686    phrase(conjunction(Goal), Goals),
 1687    process_conjunction(Goals, Origin, Src, P).
 1688process_goal(Goal, Origin, Src, _) :-           % Final disjunction, no
 1689    Goal = (_;_),                               % problems
 1690    !,
 1691    phrase(disjunction(Goal), Goals),
 1692    forall(member(G, Goals),
 1693           process_body(G, Origin, Src)).
 1694process_goal(Goal, Origin, Src, P) :-
 1695    (   (   xmodule(M, Src)
 1696        ->  true
 1697        ;   M = user
 1698        ),
 1699        pi_head(PI, M:Goal),
 1700        (   current_predicate(PI),
 1701            predicate_property(M:Goal, imported_from(IM))
 1702        ->  true
 1703        ;   PI = M:Name/Arity,
 1704            '$find_library'(M, Name, Arity, IM, _Library)
 1705        ->  true
 1706        ;   IM = M
 1707        ),
 1708        prolog:called_by(Goal, IM, M, Called)
 1709    ;   prolog:called_by(Goal, Called)
 1710    ),
 1711    !,
 1712    must_be(list, Called),
 1713    current_source_line(Here),
 1714    assert_called(Src, Origin, Goal, Here),
 1715    process_called_list(Called, Origin, Src, P).
 1716process_goal(Goal, Origin, Src, _) :-
 1717    process_xpce_goal(Goal, Origin, Src),
 1718    !.
 1719process_goal(load_foreign_library(File), _Origin, Src, _) :-
 1720    process_foreign(File, Src).
 1721process_goal(load_foreign_library(File, _Init), _Origin, Src, _) :-
 1722    process_foreign(File, Src).
 1723process_goal(use_foreign_library(File), _Origin, Src, _) :-
 1724    process_foreign(File, Src).
 1725process_goal(use_foreign_library(File, _Init), _Origin, Src, _) :-
 1726    process_foreign(File, Src).
 1727process_goal(load_foreign_resource(_Resource), _Origin, Src, _) :-
 1728    process_qp_foreign(Src).
 1729process_goal(load_foreign_resource(_Resource, _Dir), _Origin, Src, _) :-
 1730    process_qp_foreign(Src).
 1731process_goal(Goal, Origin, Src, P) :-
 1732    xref_meta_src(Goal, Metas, Src),
 1733    !,
 1734    current_source_line(Here),
 1735    assert_called(Src, Origin, Goal, Here),
 1736    process_called_list(Metas, Origin, Src, P).
 1737process_goal(Goal, Origin, Src, _) :-
 1738    asserting_goal(Goal, Rule),
 1739    !,
 1740    current_source_line(Here),
 1741    assert_called(Src, Origin, Goal, Here),
 1742    process_assert(Rule, Origin, Src).
 1743process_goal(Goal, Origin, Src, P) :-
 1744    partial_evaluate(Goal, P),
 1745    current_source_line(Here),
 1746    assert_called(Src, Origin, Goal, Here),
 1747    defer_meta_call(Goal, Origin, Src, Here).
 1748
 1749disjunction(Var)   --> {var(Var), !}, [Var].
 1750disjunction((A;B)) --> !, disjunction(A), disjunction(B).
 1751disjunction(G)     --> [G].
 1752
 1753conjunction(Var)   --> {var(Var), !}, [Var].
 1754conjunction((A,B)) --> !, conjunction(A), conjunction(B).
 1755conjunction(G)     --> [G].
 1756
 1757shares_vars(RVars, T) :-
 1758    term_variables(T, TVars0),
 1759    sort(TVars0, TVars),
 1760    ord_intersect(RVars, TVars).
 1761
 1762process_conjunction([], _, _, _).
 1763process_conjunction([Disj|Rest], Origin, Src, P) :-
 1764    nonvar(Disj),
 1765    Disj = (_;_),
 1766    Rest \== [],
 1767    !,
 1768    phrase(disjunction(Disj), Goals),
 1769    term_variables(Rest, RVars0),
 1770    sort(RVars0, RVars),
 1771    partition(shares_vars(RVars), Goals, Sharing, NonSHaring),
 1772    forall(member(G, NonSHaring),
 1773           process_body(G, Origin, Src)),
 1774    (   Sharing == []
 1775    ->  true
 1776    ;   maplist(term_variables, Sharing, GVars0),
 1777        append(GVars0, GVars1),
 1778        sort(GVars1, GVars),
 1779        ord_intersection(GVars, RVars, SVars),
 1780        VT =.. [v|SVars],
 1781        findall(VT,
 1782                (   member(G, Sharing),
 1783                    process_goal(G, Origin, Src, PS),
 1784                    PS == true
 1785                ),
 1786                Alts0),
 1787        (   Alts0 == []
 1788        ->  true
 1789        ;   (   true
 1790            ;   P = true,
 1791                sort(Alts0, Alts1),
 1792                variants(Alts1, 10, Alts),
 1793                member(VT, Alts)
 1794            )
 1795        )
 1796    ),
 1797    process_conjunction(Rest, Origin, Src, P).
 1798process_conjunction([H|T], Origin, Src, P) :-
 1799    process_goal(H, Origin, Src, P),
 1800    process_conjunction(T, Origin, Src, P).
 1801
 1802
 1803process_called_list([], _, _, _).
 1804process_called_list([H|T], Origin, Src, P) :-
 1805    process_meta(H, Origin, Src, P),
 1806    process_called_list(T, Origin, Src, P).
 1807
 1808process_meta(A+N, Origin, Src, P) :-
 1809    !,
 1810    (   extend(A, N, AX)
 1811    ->  process_goal(AX, Origin, Src, P)
 1812    ;   true
 1813    ).
 1814process_meta(//(A), Origin, Src, P) :-
 1815    !,
 1816    process_dcg_goal(A, Origin, Src, P).
 1817process_meta(G, Origin, Src, P) :-
 1818    process_goal(G, Origin, Src, P).
 process_dcg_goal(+Grammar, +Origin, +Src, ?Partial) is det
Process meta-arguments that are tagged with //, such as phrase/3.
 1825process_dcg_goal(Var, _, _, _) :-
 1826    var(Var),
 1827    !.
 1828process_dcg_goal((A,B), Origin, Src, P) :-
 1829    !,
 1830    process_dcg_goal(A, Origin, Src, P),
 1831    process_dcg_goal(B, Origin, Src, P).
 1832process_dcg_goal((A;B), Origin, Src, P) :-
 1833    !,
 1834    process_dcg_goal(A, Origin, Src, P),
 1835    process_dcg_goal(B, Origin, Src, P).
 1836process_dcg_goal((A|B), Origin, Src, P) :-
 1837    !,
 1838    process_dcg_goal(A, Origin, Src, P),
 1839    process_dcg_goal(B, Origin, Src, P).
 1840process_dcg_goal((A->B), Origin, Src, P) :-
 1841    !,
 1842    process_dcg_goal(A, Origin, Src, P),
 1843    process_dcg_goal(B, Origin, Src, P).
 1844process_dcg_goal((A*->B), Origin, Src, P) :-
 1845    !,
 1846    process_dcg_goal(A, Origin, Src, P),
 1847    process_dcg_goal(B, Origin, Src, P).
 1848process_dcg_goal({Goal}, Origin, Src, P) :-
 1849    !,
 1850    process_goal(Goal, Origin, Src, P).
 1851process_dcg_goal(List, _Origin, _Src, _) :-
 1852    is_list(List),
 1853    !.               % terminal
 1854process_dcg_goal(List, _Origin, _Src, _) :-
 1855    string(List),
 1856    !.                % terminal
 1857process_dcg_goal(Callable, Origin, Src, P) :-
 1858    extend(Callable, 2, Goal),
 1859    !,
 1860    process_goal(Goal, Origin, Src, P).
 1861process_dcg_goal(_, _, _, _).
 1862
 1863
 1864extend(Var, _, _) :-
 1865    var(Var), !, fail.
 1866extend(M:G, N, M:GX) :-
 1867    !,
 1868    callable(G),
 1869    extend(G, N, GX).
 1870extend(G, N, GX) :-
 1871    (   N =:= 0
 1872    ->  GX = G
 1873    ;   compound(G)
 1874    ->  compound_name_arguments(G, Name, Args),
 1875        length(Rest, N),
 1876        append(Args, Rest, NArgs),
 1877        compound_name_arguments(GX, Name, NArgs)
 1878    ;   atom(G)
 1879    ->  length(NArgs, N),
 1880        compound_name_arguments(GX, G, NArgs)
 1881    ).
 1882
 1883asserting_goal(assert(Rule), Rule).
 1884asserting_goal(asserta(Rule), Rule).
 1885asserting_goal(assertz(Rule), Rule).
 1886asserting_goal(assert(Rule,_), Rule).
 1887asserting_goal(asserta(Rule,_), Rule).
 1888asserting_goal(assertz(Rule,_), Rule).
 1889
 1890process_assert(0, _, _) :- !.           % catch variables
 1891process_assert((_:-Body), Origin, Src) :-
 1892    !,
 1893    process_body(Body, Origin, Src).
 1894process_assert(_, _, _).
 variants(+SortedList, +Max, -Variants) is det
 1898variants([], _, []).
 1899variants([H|T], Max, List) :-
 1900    variants(T, H, Max, List).
 1901
 1902variants([], H, _, [H]).
 1903variants(_, _, 0, []) :- !.
 1904variants([H|T], V, Max, List) :-
 1905    (   H =@= V
 1906    ->  variants(T, V, Max, List)
 1907    ;   List = [V|List2],
 1908        Max1 is Max-1,
 1909        variants(T, H, Max1, List2)
 1910    ).
 partial_evaluate(+Goal, ?Parrial) is det
Perform partial evaluation on Goal to trap cases such as below.
      T = hello(X),
      findall(T, T, List),
To be done
- Make this user extensible? What about non-deterministic bindings?
 1924partial_evaluate(Goal, P) :-
 1925    eval(Goal),
 1926    !,
 1927    P = true.
 1928partial_evaluate(_, _).
 1929
 1930eval(X = Y) :-
 1931    unify_with_occurs_check(X, Y).
 1932
 1933		 /*******************************
 1934		 *        PLUNIT SUPPORT	*
 1935		 *******************************/
 1936
 1937enter_test_unit(Unit, _Src) :-
 1938    current_source_line(Line),
 1939    asserta(current_test_unit(Unit, Line)).
 1940
 1941leave_test_unit(Unit, _Src) :-
 1942    retractall(current_test_unit(Unit, _)).
 1943
 1944
 1945                 /*******************************
 1946                 *          XPCE STUFF          *
 1947                 *******************************/
 1948
 1949pce_goal(new(_,_), new(-, new)).
 1950pce_goal(send(_,_), send(arg, msg)).
 1951pce_goal(send_class(_,_,_), send_class(arg, arg, msg)).
 1952pce_goal(get(_,_,_), get(arg, msg, -)).
 1953pce_goal(get_class(_,_,_,_), get_class(arg, arg, msg, -)).
 1954pce_goal(get_chain(_,_,_), get_chain(arg, msg, -)).
 1955pce_goal(get_object(_,_,_), get_object(arg, msg, -)).
 1956
 1957process_xpce_goal(G, Origin, Src) :-
 1958    pce_goal(G, Process),
 1959    !,
 1960    current_source_line(Here),
 1961    assert_called(Src, Origin, G, Here),
 1962    (   arg(I, Process, How),
 1963        arg(I, G, Term),
 1964        process_xpce_arg(How, Term, Origin, Src),
 1965        fail
 1966    ;   true
 1967    ).
 1968
 1969process_xpce_arg(new, Term, Origin, Src) :-
 1970    callable(Term),
 1971    process_new(Term, Origin, Src).
 1972process_xpce_arg(arg, Term, Origin, Src) :-
 1973    compound(Term),
 1974    process_new(Term, Origin, Src).
 1975process_xpce_arg(msg, Term, Origin, Src) :-
 1976    compound(Term),
 1977    (   arg(_, Term, Arg),
 1978        process_xpce_arg(arg, Arg, Origin, Src),
 1979        fail
 1980    ;   true
 1981    ).
 1982
 1983process_new(_M:_Term, _, _) :- !.       % TBD: Calls on other modules!
 1984process_new(Term, Origin, Src) :-
 1985    assert_new(Src, Origin, Term),
 1986    (   compound(Term),
 1987        arg(_, Term, Arg),
 1988        process_xpce_arg(arg, Arg, Origin, Src),
 1989        fail
 1990    ;   true
 1991    ).
 1992
 1993assert_new(_, _, Term) :-
 1994    \+ callable(Term),
 1995    !.
 1996assert_new(Src, Origin, Control) :-
 1997    functor_name(Control, Class),
 1998    pce_control_class(Class),
 1999    !,
 2000    forall(arg(_, Control, Arg),
 2001           assert_new(Src, Origin, Arg)).
 2002assert_new(Src, Origin, Term) :-
 2003    compound(Term),
 2004    arg(1, Term, Prolog),
 2005    Prolog == @(prolog),
 2006    (   Term =.. [message, _, Selector | T],
 2007        atom(Selector)
 2008    ->  Called =.. [Selector|T],
 2009        process_body(Called, Origin, Src)
 2010    ;   Term =.. [?, _, Selector | T],
 2011        atom(Selector)
 2012    ->  append(T, [_R], T2),
 2013        Called =.. [Selector|T2],
 2014        process_body(Called, Origin, Src)
 2015    ),
 2016    fail.
 2017assert_new(_, _, @(_)) :- !.
 2018assert_new(Src, _, Term) :-
 2019    functor_name(Term, Name),
 2020    assert_used_class(Src, Name).
 2021
 2022
 2023pce_control_class(and).
 2024pce_control_class(or).
 2025pce_control_class(if).
 2026pce_control_class(not).
 2027
 2028
 2029                /********************************
 2030                *       INCLUDED MODULES        *
 2031                ********************************/
 process_use_module(+Modules, +Src, +Rexport) is det
 2035process_use_module(_Module:_Files, _, _) :- !.  % loaded in another module
 2036process_use_module([], _, _) :- !.
 2037process_use_module([H|T], Src, Reexport) :-
 2038    !,
 2039    process_use_module(H, Src, Reexport),
 2040    process_use_module(T, Src, Reexport).
 2041process_use_module(library(pce), Src, Reexport) :-     % bit special
 2042    !,
 2043    xref_public_list(library(pce), Path, Exports, Src),
 2044    forall(member(Import, Exports),
 2045           process_pce_import(Import, Src, Path, Reexport)).
 2046process_use_module(File, Src, Reexport) :-
 2047    load_module_if_needed(File),
 2048    (   xoption(Src, silent(Silent))
 2049    ->  Extra = [silent(Silent)]
 2050    ;   Extra = [silent(true)]
 2051    ),
 2052    (   xref_public_list(File, Src,
 2053                         [ path(Path),
 2054                           module(M),
 2055                           exports(Exports),
 2056                           public(Public),
 2057                           meta(Meta)
 2058                         | Extra
 2059                         ])
 2060    ->  assert(uses_file(File, Src, Path)),
 2061        assert_import(Src, Exports, _, Path, Reexport),
 2062        assert_xmodule_callable(Exports, M, Src, Path),
 2063        assert_xmodule_callable(Public, M, Src, Path),
 2064        maplist(process_meta_head(Src), Meta),
 2065        (   File = library(chr)     % hacky
 2066        ->  assert(mode(chr, Src))
 2067        ;   true
 2068        )
 2069    ;   assert(uses_file(File, Src, '<not_found>'))
 2070    ).
 2071
 2072process_pce_import(Name/Arity, Src, Path, Reexport) :-
 2073    atom(Name),
 2074    integer(Arity),
 2075    !,
 2076    functor(Term, Name, Arity),
 2077    (   \+ system_predicate(Term),
 2078        \+ Term = pce_error(_)      % hack!?
 2079    ->  assert_import(Src, [Name/Arity], _, Path, Reexport)
 2080    ;   true
 2081    ).
 2082process_pce_import(op(P,T,N), Src, _, _) :-
 2083    xref_push_op(Src, P, T, N).
 process_use_module2(+File, +Import, +Src, +Reexport) is det
Process use_module/2 and reexport/2.
 2089process_use_module2(File, Import, Src, Reexport) :-
 2090    load_module_if_needed(File),
 2091    (   catch(xref_public_list(File, Src,
 2092                               [ path(Path),
 2093                                 exports(Export),
 2094                                 meta(Meta)
 2095                               ]),
 2096              error(_,_),
 2097              fail)
 2098    ->  assertz(uses_file(File, Src, Path)),
 2099        assert_import(Src, Import, Export, Path, Reexport),
 2100        forall((  member(Head, Meta),
 2101                  imported(Head, _, Path)
 2102               ),
 2103               process_meta_head(Src, Head))
 2104    ;   assertz(uses_file(File, Src, '<not_found>'))
 2105    ).
 load_module_if_needed(+File)
Load a module explicitly if it is not suitable for autoloading. Typically this is the case if the module provides essential term and/or goal expansion rulses.
 2114load_module_if_needed(File) :-
 2115    prolog:no_autoload_module(File),
 2116    !,
 2117    use_module(File, []).
 2118load_module_if_needed(_).
 2119
 2120prolog:no_autoload_module(library(apply_macros)).
 2121prolog:no_autoload_module(library(arithmetic)).
 2122prolog:no_autoload_module(library(record)).
 2123prolog:no_autoload_module(library(persistency)).
 2124prolog:no_autoload_module(library(pldoc)).
 2125prolog:no_autoload_module(library(settings)).
 2126prolog:no_autoload_module(library(debug)).
 2127prolog:no_autoload_module(library(plunit)).
 2128prolog:no_autoload_module(library(macros)).
 2129prolog:no_autoload_module(library(yall)).
 process_requires(+Import, +Src)
 2134process_requires(Import, Src) :-
 2135    is_list(Import),
 2136    !,
 2137    require_list(Import, Src).
 2138process_requires(Var, _Src) :-
 2139    var(Var),
 2140    !.
 2141process_requires((A,B), Src) :-
 2142    !,
 2143    process_requires(A, Src),
 2144    process_requires(B, Src).
 2145process_requires(PI, Src) :-
 2146    requires(PI, Src).
 2147
 2148require_list([], _).
 2149require_list([H|T], Src) :-
 2150    requires(H, Src),
 2151    require_list(T, Src).
 2152
 2153requires(PI, _Src) :-
 2154    '$pi_head'(PI, Head),
 2155    '$get_predicate_attribute'(system:Head, defined, 1),
 2156    !.
 2157requires(PI, Src) :-
 2158    '$pi_head'(PI, Head),
 2159    '$pi_head'(Name/Arity, Head),
 2160    '$find_library'(_Module, Name, Arity, _LoadModule, Library),
 2161    (   imported(Head, Src, Library)
 2162    ->  true
 2163    ;   assertz(imported(Head, Src, Library))
 2164    ).
 xref_public_list(+Spec, +Source, +Options) is semidet
Find meta-information about File. If Spec resolves to a Prolog source file, this predicate reads all terms upto the first term that is not a directive. If Spec resolves to a SWI-Prolog `.qlf` file, it extracts part of the information from the QLF file. It uses the module and meta_predicate directives to assemble the information in Options. Options processed:
path(-Path)
Path is the full path name of the referenced file. If Spec resolves to a .qlf file, Path is the name of the embedded Prolog file.
module(-Module)
Module is the module defines in Spec.
exports(-Exports)
Exports is a list of predicate indicators and operators collected from the module/2 term and reexport declarations.
public -Public
Public declarations of the file. Currently always [] for .qlf files.
meta(-Meta)
Meta is a list of heads as they appear in meta_predicate/1 declarations. Currently always [] for .qlf files.
silent(+Boolean)
Do not print any messages or raise exceptions on errors.

The information collected by this predicate is cached. The cached data is considered valid as long as the modification time of the file does not change.

Arguments:
Source- is the file from which Spec is referenced.
 2200xref_public_list(File, Src, Options) :-
 2201    option(path(Source), Options, _),
 2202    option(module(Module), Options, _),
 2203    option(exports(Exports), Options, _),
 2204    option(public(Public), Options, _),
 2205    option(meta(Meta), Options, _),
 2206    xref_source_file(File, Path, Src, Options),
 2207    public_list(Path, Source, Module, Meta, Exports, Public, Options).
 xref_public_list(+File, -Path, -Export, +Src) is semidet
 xref_public_list(+File, -Path, -Module, -Export, -Meta, +Src) is semidet
 xref_public_list(+File, -Path, -Module, -Export, -Public, -Meta, +Src) is semidet
Find meta-information about File. This predicate reads all terms upto the first term that is not a directive. It uses the module and meta_predicate directives to assemble the information described below.

These predicates fail if File is not a module-file.

Arguments:
File- is a file speficiation for prolog_open_source/2 or a .qlf file name. Note this makes a stream a valid input.
Path- is the canonical path to File
Module- is the module defined in Path
Export- is a list of predicate indicators.
Meta- is a list of heads as they appear in meta_predicate/1 declarations.
Src- is the place from which File is referenced.
deprecated
- New code should use xref_public_list/3, which unifies all variations using an option list.
 2231xref_public_list(File, Source, Export, Src) :-
 2232    xref_source_file(File, Path, Src),
 2233    public_list(Path, Source, _, _, Export, _, []).
 2234xref_public_list(File, Source, Module, Export, Meta, Src) :-
 2235    xref_source_file(File, Path, Src),
 2236    public_list(Path, Source, Module, Meta, Export, _, []).
 2237xref_public_list(File, Source, Module, Export, Public, Meta, Src) :-
 2238    xref_source_file(File, Path, Src),
 2239    public_list(Path, Source, Module, Meta, Export, Public, []).
 public_list(+Path, -Source, -Module, -Meta, -Export, -Public, +Options) is det
Read the public information for Path. Options supported are:
silent(+Boolean)
If true, ignore (syntax) errors. If not specified the default is inherited from xref_source/2.
 2250:- dynamic  public_list_cache/7. 2251:- volatile public_list_cache/7. 2252
 2253public_list(Path, Source, Module, Meta, Export, Public, _Options) :-
 2254    \+ is_stream(Path),
 2255    public_list_cache(Path, Source, Modified,
 2256                      Module0, Meta0, Export0, Public0),
 2257    time_file(Path, ModifiedNow),
 2258    (   abs(Modified-ModifiedNow) < 0.0001
 2259    ->  !,
 2260        t(Module,Meta,Export,Public) = t(Module0,Meta0,Export0,Public0)
 2261    ;   retractall(public_list_cache(Path, _, _, _, _, _, _)),
 2262        fail
 2263    ).
 2264public_list(Path, Source, Module, Meta, Export, Public, Options) :-
 2265    public_list_nc(Path, Source, Module0, Meta0, Export0, Public0, Options),
 2266    (   Error = error(_,_),
 2267        catch(time_file(Path, Modified), Error, fail)
 2268    ->  asserta(public_list_cache(Path, Source, Modified,
 2269                                  Module0, Meta0, Export0, Public0))
 2270    ;   true
 2271    ),
 2272    t(Module,Meta,Export,Public) = t(Module0,Meta0,Export0,Public0).
 2273
 2274public_list_nc(Path, Source, Module, Meta, Export, Public, _Options) :-
 2275    \+ is_stream(Path),
 2276    public_list_from_index(Path, Module, Meta, Export, Public),
 2277    !,
 2278    qlf_pl_file(Path, Source).
 2279public_list_nc(Path, Source, Module, [], Export, [], _Options) :-
 2280    \+ is_stream(Path),
 2281    is_qlf_file(Path),
 2282    !,
 2283    '$qlf_module'(Path, Info),
 2284    _{module:Module, exports:Export, file:Source} :< Info.
 2285public_list_nc(Path, Path, Module, Meta, Export, Public, Options) :-
 2286    (   is_stream(Path)
 2287    ;   exists_file(Path)
 2288    ),
 2289    !,
 2290    prolog_file_directives(Path, Directives, Options),
 2291    public_list(Directives, Path, Module, Meta, [], Export, [], Public, []).
 2292public_list_nc(Path, Path, Module, [], Export, [], _Options) :-
 2293    \+ is_stream(Path),
 2294    qlf_pl_file(QlfFile, Path),
 2295    '$qlf_module'(QlfFile, Info),
 2296    _{module:Module, exports:Export} :< Info.
 2297
 2298public_list([(:- module(Module, Export0))|Decls], Path,
 2299            Module, Meta, MT, Export, Rest, Public, PT) :-
 2300    !,
 2301    (   is_list(Export0)
 2302    ->  append(Export0, Reexport, Export)
 2303    ;   Reexport = Export
 2304    ),
 2305    public_list_(Decls, Path, Meta, MT, Reexport, Rest, Public, PT).
 2306public_list([(:- encoding(_))|Decls], Path,
 2307            Module, Meta, MT, Export, Rest, Public, PT) :-
 2308    public_list(Decls, Path, Module, Meta, MT, Export, Rest, Public, PT).
 2309
 2310public_list_([], _, Meta, Meta, Export, Export, Public, Public).
 2311public_list_([(:-(Dir))|T], Path, Meta, MT, Export, Rest, Public, PT) :-
 2312    public_list_1(Dir, Path, Meta, MT0, Export, Rest0, Public, PT0),
 2313    !,
 2314    public_list_(T, Path, MT0, MT, Rest0, Rest, PT0, PT).
 2315public_list_([_|T], Path, Meta, MT, Export, Rest, Public, PT) :-
 2316    public_list_(T, Path, Meta, MT, Export, Rest, Public, PT).
 2317
 2318public_list_1(reexport(Spec), Path, Meta, MT, Reexport, Rest, Public, PT) :-
 2319    reexport_files(Spec, Path, Meta, MT, Reexport, Rest, Public, PT).
 2320public_list_1(reexport(Spec, Import), Path, Meta, Meta, Reexport, Rest, Public, Public) :-
 2321    public_from_import(Import, Spec, Path, Reexport, Rest).
 2322public_list_1(meta_predicate(Decl), _Path, Meta, MT, Export, Export, Public, Public) :-
 2323    phrase(meta_decls(Decl), Meta, MT).
 2324public_list_1(public(Decl), _Path, Meta, Meta, Export, Export, Public, PT) :-
 2325    phrase(public_decls(Decl), Public, PT).
 reexport_files(+Files, +Src, -Meta, ?MetaTail, -Exports, ?ExportsTail, -Public, ?PublicTail)
 2331reexport_files([], _, Meta, Meta, Export, Export, Public, Public) :- !.
 2332reexport_files([H|T], Src, Meta, MT, Export, ET, Public, PT) :-
 2333    !,
 2334    xref_source_file(H, Path, Src),
 2335    public_list(Path, _Source, _Module, Meta0, Export0, Public0, []),
 2336    append(Meta0, MT1, Meta),
 2337    append(Export0, ET1, Export),
 2338    append(Public0, PT1, Public),
 2339    reexport_files(T, Src, MT1, MT, ET1, ET, PT1, PT).
 2340reexport_files(Spec, Src, Meta, MT, Export, ET, Public, PT) :-
 2341    xref_source_file(Spec, Path, Src),
 2342    public_list(Path, _Source, _Module, Meta0, Export0, Public0, []),
 2343    append(Meta0, MT, Meta),
 2344    append(Export0, ET, Export),
 2345    append(Public0, PT, Public).
 2346
 2347public_from_import(except(Map), Path, Src, Export, Rest) :-
 2348    !,
 2349    xref_public_list(Path, _, AllExports, Src),
 2350    except(Map, AllExports, NewExports),
 2351    append(NewExports, Rest, Export).
 2352public_from_import(Import, _, _, Export, Rest) :-
 2353    import_name_map(Import, Export, Rest).
 except(+Remove, +AllExports, -Exports)
 2358except([], Exports, Exports).
 2359except([PI0 as NewName|Map], Exports0, Exports) :-
 2360    !,
 2361    canonical_pi(PI0, PI),
 2362    map_as(Exports0, PI, NewName, Exports1),
 2363    except(Map, Exports1, Exports).
 2364except([PI0|Map], Exports0, Exports) :-
 2365    canonical_pi(PI0, PI),
 2366    select(PI2, Exports0, Exports1),
 2367    same_pi(PI, PI2),
 2368    !,
 2369    except(Map, Exports1, Exports).
 2370
 2371
 2372map_as([PI|T], Repl, As, [PI2|T])  :-
 2373    same_pi(Repl, PI),
 2374    !,
 2375    pi_as(PI, As, PI2).
 2376map_as([H|T0], Repl, As, [H|T])  :-
 2377    map_as(T0, Repl, As, T).
 2378
 2379pi_as(_/Arity, Name, Name/Arity).
 2380pi_as(_//Arity, Name, Name//Arity).
 2381
 2382import_name_map([], L, L).
 2383import_name_map([_/Arity as NewName|T0], [NewName/Arity|T], Tail) :-
 2384    !,
 2385    import_name_map(T0, T, Tail).
 2386import_name_map([_//Arity as NewName|T0], [NewName//Arity|T], Tail) :-
 2387    !,
 2388    import_name_map(T0, T, Tail).
 2389import_name_map([H|T0], [H|T], Tail) :-
 2390    import_name_map(T0, T, Tail).
 2391
 2392canonical_pi(Name//Arity0, PI) :-
 2393    integer(Arity0),
 2394    !,
 2395    PI = Name/Arity,
 2396    Arity is Arity0 + 2.
 2397canonical_pi(PI, PI).
 2398
 2399same_pi(Canonical, PI2) :-
 2400    canonical_pi(PI2, Canonical).
 2401
 2402meta_decls(Var) -->
 2403    { var(Var) },
 2404    !.
 2405meta_decls((A,B)) -->
 2406    !,
 2407    meta_decls(A),
 2408    meta_decls(B).
 2409meta_decls(A) -->
 2410    [A].
 2411
 2412public_decls(Var) -->
 2413    { var(Var) },
 2414    !.
 2415public_decls((A,B)) -->
 2416    !,
 2417    public_decls(A),
 2418    public_decls(B).
 2419public_decls(A) -->
 2420    [A].
 public_list_from_index(+Path, -Module, -Meta, -Exports, -Public) is semidet
Read the exports for Path from the INDEX.pl file in the same directory.
 2427public_list_from_index(Path, Module, Meta, Export, Public) :-
 2428    file_name_extension(BasePath, _Ext, Path),
 2429    file_directory_name(BasePath, Dir),
 2430    atom_concat(Dir, '/INDEX.pl', IndexFile),
 2431    exists_file(IndexFile),
 2432    file_base_name(BasePath, Base),
 2433    setup_call_cleanup(
 2434        '$push_input_context'(autoload_index),
 2435        setup_call_cleanup(
 2436            open(IndexFile, read, In),
 2437            index_public_list(In, Base, Module, Meta, Export, Public),
 2438            close(In)),
 2439        '$pop_input_context').
 2440
 2441index_public_list(In, Base, Module, Meta, Export, Public) :-
 2442    read_term(In, Term, []),
 2443    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2444
 2445index_public_list(end_of_file, _In, _Base, _Module, [], [], []).
 2446index_public_list(index(op:Op, Module, Base), In, Base, Module, Meta, [Op|Export], Public) :-
 2447    !,
 2448    read_term(In, Term, []),
 2449    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2450index_public_list(index((public):Head, Module, Base), In, Base, Module, Meta, Export, [PI|Public]) :-
 2451    !,
 2452    pi_head(PI, Head),
 2453    read_term(In, Term, []),
 2454    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2455index_public_list(index(Head, Module, Base), In, Base, Module, Meta, [PI|Export], Public) :-
 2456    !,
 2457    pi_head(PI, Head),
 2458    (   meta_mode(Head)
 2459    ->  Meta = [Head|MetaT]
 2460    ;   Meta = MetaT
 2461    ),
 2462    read_term(In, Term, []),
 2463    index_public_list(Term, In, Base, Module, MetaT, Export, Public).
 2464index_public_list(index(Name, Arity, Module, Base), In, Base, Module, Meta, [Name/Arity|Export], Public) :-
 2465    !,
 2466    read_term(In, Term, []),
 2467    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2468index_public_list(_, In, Base, Module, Meta, Export, Public) :-
 2469    read_term(In, Term, []),
 2470    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2471
 2472meta_mode(H) :-
 2473    compound(H),
 2474    arg(_, H, A),
 2475    meta_arg(A),
 2476    !.
 2477
 2478meta_arg(I) :-
 2479    integer(I),
 2480    !.
 2481meta_arg(:).
 2482meta_arg(^).
 2483meta_arg(//).
 2484
 2485                 /*******************************
 2486                 *             INCLUDE          *
 2487                 *******************************/
 2488
 2489process_include([], _) :- !.
 2490process_include([H|T], Src) :-
 2491    !,
 2492    process_include(H, Src),
 2493    process_include(T, Src).
 2494process_include(File, Src) :-
 2495    callable(File),
 2496    !,
 2497    (   once(xref_input(ParentSrc, _)),
 2498        xref_source_file(File, Path, ParentSrc)
 2499    ->  (   (   uses_file(_, Src, Path)
 2500            ;   Path == Src
 2501            )
 2502        ->  true
 2503        ;   assert(uses_file(File, Src, Path)),
 2504            (   xoption(Src, process_include(true))
 2505            ->  findall(O, xoption(Src, O), Options),
 2506                setup_call_cleanup(
 2507                    open_include_file(Path, In, Refs),
 2508                    collect(Src, Path, In, Options),
 2509                    close_include(In, Refs))
 2510            ;   true
 2511            )
 2512        )
 2513    ;   assert(uses_file(File, Src, '<not_found>'))
 2514    ).
 2515process_include(_, _).
 open_include_file(+Path, -In, -Refs)
Opens an :- include(File) referenced file. Note that we cannot use prolog_open_source/2 because we should not safe/restore the lexical context.
 2523open_include_file(Path, In, [Ref]) :-
 2524    once(xref_input(_, Parent)),
 2525    stream_property(Parent, encoding(Enc)),
 2526    '$push_input_context'(xref_include),
 2527    catch((   prolog:xref_open_source(Path, In)
 2528          ->  catch(set_stream(In, encoding(Enc)),
 2529                    error(_,_), true)       % deal with non-file input
 2530          ;   include_encoding(Enc, Options),
 2531              open(Path, read, In, Options)
 2532          ), E,
 2533          ( '$pop_input_context', throw(E))),
 2534    catch((   peek_char(In, #)              % Deal with #! script
 2535          ->  skip(In, 10)
 2536          ;   true
 2537          ), E,
 2538          ( close_include(In, []), throw(E))),
 2539    asserta(xref_input(Path, In), Ref).
 2540
 2541include_encoding(wchar_t, []) :- !.
 2542include_encoding(Enc, [encoding(Enc)]).
 2543
 2544
 2545close_include(In, Refs) :-
 2546    maplist(erase, Refs),
 2547    close(In, [force(true)]),
 2548    '$pop_input_context'.
 process_foreign(+Spec, +Src)
Process a load_foreign_library/1 call.
 2554process_foreign(Spec, Src) :-
 2555    ground(Spec),
 2556    current_foreign_library(Spec, Defined),
 2557    !,
 2558    (   xmodule(Module, Src)
 2559    ->  true
 2560    ;   Module = user
 2561    ),
 2562    process_foreign_defined(Defined, Module, Src).
 2563process_foreign(_, _).
 2564
 2565process_foreign_defined([], _, _).
 2566process_foreign_defined([H|T], M, Src) :-
 2567    (   H = M:Head
 2568    ->  assert_foreign(Src, Head)
 2569    ;   assert_foreign(Src, H)
 2570    ),
 2571    process_foreign_defined(T, M, Src).
 2572
 2573
 2574                 /*******************************
 2575                 *          CHR SUPPORT         *
 2576                 *******************************/
 2577
 2578/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 2579This part of the file supports CHR. Our choice is between making special
 2580hooks to make CHR expansion work and  then handle the (complex) expanded
 2581code or process the  CHR  source   directly.  The  latter looks simpler,
 2582though I don't like the idea  of   adding  support for libraries to this
 2583module.  A  file  is  supposed  to  be  a    CHR   file  if  it  uses  a
 2584use_module(library(chr) or contains a :-   constraint/1 directive. As an
 2585extra bonus we get the source-locations right :-)
 2586- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 2587
 2588process_chr(@(_Name, Rule), Src) :-
 2589    mode(chr, Src),
 2590    process_chr(Rule, Src).
 2591process_chr(pragma(Rule, _Pragma), Src) :-
 2592    mode(chr, Src),
 2593    process_chr(Rule, Src).
 2594process_chr(<=>(Head, Body), Src) :-
 2595    mode(chr, Src),
 2596    chr_head(Head, Src, H),
 2597    chr_body(Body, H, Src).
 2598process_chr(==>(Head, Body), Src) :-
 2599    mode(chr, Src),
 2600    chr_head(Head, H, Src),
 2601    chr_body(Body, H, Src).
 2602process_chr((:- chr_constraint(Decls)), Src) :-
 2603    (   mode(chr, Src)
 2604    ->  true
 2605    ;   assert(mode(chr, Src))
 2606    ),
 2607    chr_decls(Decls, Src).
 2608
 2609chr_decls((A,B), Src) =>
 2610    chr_decls(A, Src),
 2611    chr_decls(B, Src).
 2612chr_decls(Head, Src) =>
 2613    generalise_term(Head, Gen),
 2614    (   declared(Gen, chr_constraint, Src, _)
 2615    ->  true
 2616    ;   current_source_line(Line),
 2617        assertz(declared(Gen, chr_constraint, Src, Line))
 2618    ).
 2619
 2620chr_head(X, _, _) :-
 2621    var(X),
 2622    !.                      % Illegal.  Warn?
 2623chr_head(\(A,B), Src, H) :-
 2624    chr_head(A, Src, H),
 2625    process_body(B, H, Src).
 2626chr_head((H0,B), Src, H) :-
 2627    chr_defined(H0, Src, H),
 2628    process_body(B, H, Src).
 2629chr_head(H0, Src, H) :-
 2630    chr_defined(H0, Src, H).
 2631
 2632chr_defined(X, _, _) :-
 2633    var(X),
 2634    !.
 2635chr_defined(#(C,_Id), Src, C) :-
 2636    !,
 2637    assert_constraint(Src, C).
 2638chr_defined(A, Src, A) :-
 2639    assert_constraint(Src, A).
 2640
 2641chr_body(X, From, Src) :-
 2642    var(X),
 2643    !,
 2644    process_body(X, From, Src).
 2645chr_body('|'(Guard, Goals), H, Src) :-
 2646    !,
 2647    chr_body(Guard, H, Src),
 2648    chr_body(Goals, H, Src).
 2649chr_body(G, From, Src) :-
 2650    process_body(G, From, Src).
 2651
 2652assert_constraint(_, Head) :-
 2653    var(Head),
 2654    !.
 2655assert_constraint(Src, Head) :-
 2656    constraint(Head, Src, _),
 2657    !.
 2658assert_constraint(Src, Head) :-
 2659    generalise_term(Head, Term),
 2660    current_source_line(Line),
 2661    assert(constraint(Term, Src, Line)).
 2662
 2663
 2664                /********************************
 2665                *       PHASE 1 ASSERTIONS      *
 2666                ********************************/
 assert_called(+Src, +From, +Head, +Line) is det
Assert the fact that Head is called by From in Src. We do not assert called system predicates.
 2673assert_called(_, _, Var, _) :-
 2674    var(Var),
 2675    !.
 2676assert_called(Src, From, Goal, Line) :-
 2677    var(From),
 2678    !,
 2679    assert_called(Src, '<unknown>', Goal, Line).
 2680assert_called(_, _, Goal, _) :-
 2681    expand_hide_called(Goal),
 2682    !.
 2683assert_called(Src, Origin, M:G, Line) :-
 2684    !,
 2685    (   atom(M),
 2686        callable(G)
 2687    ->  current_condition(Cond),
 2688        (   xmodule(M, Src)         % explicit call to own module
 2689        ->  assert_called(Src, Origin, G, Line)
 2690        ;   called(M:G, Src, Origin, Cond, Line) % already registered
 2691        ->  true
 2692        ;   hide_called(M:G, Src)           % not interesting (now)
 2693        ->  true
 2694        ;   generalise(Origin, OTerm),
 2695            generalise(G, GTerm)
 2696        ->  assert(called(M:GTerm, Src, OTerm, Cond, Line))
 2697        ;   true
 2698        )
 2699    ;   true                        % call to variable module
 2700    ).
 2701assert_called(Src, _, Goal, _) :-
 2702    (   xmodule(M, Src)
 2703    ->  M \== system
 2704    ;   M = user
 2705    ),
 2706    hide_called(M:Goal, Src),
 2707    !.
 2708assert_called(Src, Origin, Goal, Line) :-
 2709    current_condition(Cond),
 2710    (   called(Goal, Src, Origin, Cond, Line)
 2711    ->  true
 2712    ;   generalise(Origin, OTerm),
 2713        generalise(Goal, Term)
 2714    ->  assert(called(Term, Src, OTerm, Cond, Line))
 2715    ;   true
 2716    ).
 expand_hide_called(:Callable) is semidet
Goals that should not turn up as being called. Hack. Eventually we should deal with that using an XPCE plugin.
 2724expand_hide_called(pce_principal:send_implementation(_, _, _)).
 2725expand_hide_called(pce_principal:get_implementation(_, _, _, _)).
 2726expand_hide_called(pce_principal:pce_lazy_get_method(_,_,_)).
 2727expand_hide_called(pce_principal:pce_lazy_send_method(_,_,_)).
 2728
 2729assert_defined(Src, Goal) :-
 2730    Goal = test(_Test),
 2731    current_test_unit(Unit, Line),
 2732    assert_called(Src, '<test_unit>'(Unit), Goal, Line),
 2733    fail.
 2734assert_defined(Src, Goal) :-
 2735    Goal = test(_Test, _Options),
 2736    current_test_unit(Unit, Line),
 2737    assert_called(Src, '<test_unit>'(Unit), Goal, Line),
 2738    fail.
 2739assert_defined(Src, Goal) :-
 2740    defined(Goal, Src, _),
 2741    !.
 2742assert_defined(Src, Goal) :-
 2743    generalise(Goal, Term),
 2744    current_source_line(Line),
 2745    assert(defined(Term, Src, Line)).
 2746
 2747assert_foreign(Src, Goal) :-
 2748    foreign(Goal, Src, _),
 2749    !.
 2750assert_foreign(Src, Goal) :-
 2751    generalise(Goal, Term),
 2752    current_source_line(Line),
 2753    assert(foreign(Term, Src, Line)).
 2754
 2755assert_grammar_rule(Src, Goal) :-
 2756    grammar_rule(Goal, Src),
 2757    !.
 2758assert_grammar_rule(Src, Goal) :-
 2759    generalise(Goal, Term),
 2760    assert(grammar_rule(Term, Src)).
 assert_import(+Src, +Import, +ExportList, +From, +Reexport) is det
Asserts imports into Src. Import is the import specification, ExportList is the list of known exported predicates or unbound if this need not be checked and From is the file from which the public predicates come. If Reexport is true, re-export the imported predicates.
To be done
- Tighter type-checking on Import.
 2773assert_import(_, [], _, _, _) :- !.
 2774assert_import(Src, [H|T], Export, From, Reexport) :-
 2775    !,
 2776    assert_import(Src, H, Export, From, Reexport),
 2777    assert_import(Src, T, Export, From, Reexport).
 2778assert_import(Src, except(Except), Export, From, Reexport) :-
 2779    !,
 2780    is_list(Export),
 2781    !,
 2782    except(Except, Export, Import),
 2783    assert_import(Src, Import, _All, From, Reexport).
 2784assert_import(Src, Import as Name, Export, From, Reexport) :-
 2785    !,
 2786    pi_to_head(Import, Term0),
 2787    rename_goal(Term0, Name, Term),
 2788    (   in_export_list(Term0, Export)
 2789    ->  assert(imported(Term, Src, From)),
 2790        assert_reexport(Reexport, Src, Term)
 2791    ;   current_source_line(Line),
 2792        assert_called(Src, '<directive>'(Line), Term0, Line)
 2793    ).
 2794assert_import(Src, Import, Export, From, Reexport) :-
 2795    pi_to_head(Import, Term),
 2796    !,
 2797    (   in_export_list(Term, Export)
 2798    ->  assert(imported(Term, Src, From)),
 2799        assert_reexport(Reexport, Src, Term)
 2800    ;   current_source_line(Line),
 2801        assert_called(Src, '<directive>'(Line), Term, Line)
 2802    ).
 2803assert_import(Src, op(P,T,N), _, _, _) :-
 2804    xref_push_op(Src, P,T,N).
 2805
 2806in_export_list(_Head, Export) :-
 2807    var(Export),
 2808    !.
 2809in_export_list(Head, Export) :-
 2810    member(PI, Export),
 2811    pi_to_head(PI, Head).
 2812
 2813assert_reexport(false, _, _) :- !.
 2814assert_reexport(true, Src, Term) :-
 2815    assert(exported(Term, Src)).
 process_import(:Import, +Src)
Process an import/1 directive
 2821process_import(M:PI, Src) :-
 2822    pi_to_head(PI, Head),
 2823    !,
 2824    (   atom(M),
 2825        current_module(M),
 2826        module_property(M, file(From))
 2827    ->  true
 2828    ;   From = '<unknown>'
 2829    ),
 2830    assert(imported(Head, Src, From)).
 2831process_import(_, _).
 assert_xmodule_callable(PIs, Module, Src, From)
We can call all exports and public predicates of an imported module using Module:Goal.
To be done
- Should we distinguish this from normal imported?
 2840assert_xmodule_callable([], _, _, _).
 2841assert_xmodule_callable([PI|T], M, Src, From) :-
 2842    (   pi_to_head(M:PI, Head)
 2843    ->  assert(imported(Head, Src, From))
 2844    ;   true
 2845    ),
 2846    assert_xmodule_callable(T, M, Src, From).
 assert_op(+Src, +Op) is det
Arguments:
Op- Ground term op(Priority, Type, Name).
 2853assert_op(Src, op(P,T,M:N)) :-
 2854    (   '$current_source_module'(M)
 2855    ->  Name = N
 2856    ;   Name = M:N
 2857    ),
 2858    (   xop(Src, op(P,T,Name))
 2859    ->  true
 2860    ;   assert(xop(Src, op(P,T,Name)))
 2861    ).
 assert_module(+Src, +Module)
Assert we are loading code into Module. This is also used to exploit local term-expansion and other rules.
 2868assert_module(Src, Module) :-
 2869    xmodule(Module, Src),
 2870    !.
 2871assert_module(Src, Module) :-
 2872    '$set_source_module'(Module),
 2873    assert(xmodule(Module, Src)),
 2874    (   module_property(Module, class(system))
 2875    ->  retractall(xoption(Src, register_called(_))),
 2876        assert(xoption(Src, register_called(all)))
 2877    ;   true
 2878    ).
 2879
 2880assert_module_export(_, []) :- !.
 2881assert_module_export(Src, [H|T]) :-
 2882    !,
 2883    assert_module_export(Src, H),
 2884    assert_module_export(Src, T).
 2885assert_module_export(Src, PI) :-
 2886    pi_to_head(PI, Term),
 2887    !,
 2888    assert(exported(Term, Src)).
 2889assert_module_export(Src, op(P, A, N)) :-
 2890    xref_push_op(Src, P, A, N).
 assert_module3(+Import, +Src)
Handle 3th argument of module/3 declaration.
 2896assert_module3([], _) :- !.
 2897assert_module3([H|T], Src) :-
 2898    !,
 2899    assert_module3(H, Src),
 2900    assert_module3(T, Src).
 2901assert_module3(Option, Src) :-
 2902    process_use_module(library(dialect/Option), Src, false).
 process_predicates(:Closure, +Predicates, +Src)
Process areguments of dynamic, etc., using call(Closure, PI, Src). Handles both lists of specifications and (PI,...) specifications.
 2911process_predicates(Closure, Preds, Src) :-
 2912    is_list(Preds),
 2913    !,
 2914    process_predicate_list(Preds, Closure, Src).
 2915process_predicates(Closure, as(Preds, _Options), Src) :-
 2916    !,
 2917    process_predicates(Closure, Preds, Src).
 2918process_predicates(Closure, Preds, Src) :-
 2919    process_predicate_comma(Preds, Closure, Src).
 2920
 2921process_predicate_list([], _, _).
 2922process_predicate_list([H|T], Closure, Src) :-
 2923    (   nonvar(H)
 2924    ->  call(Closure, H, Src)
 2925    ;   true
 2926    ),
 2927    process_predicate_list(T, Closure, Src).
 2928
 2929process_predicate_comma(Var, _, _) :-
 2930    var(Var),
 2931    !.
 2932process_predicate_comma(M:(A,B), Closure, Src) :-
 2933    !,
 2934    process_predicate_comma(M:A, Closure, Src),
 2935    process_predicate_comma(M:B, Closure, Src).
 2936process_predicate_comma((A,B), Closure, Src) :-
 2937    !,
 2938    process_predicate_comma(A, Closure, Src),
 2939    process_predicate_comma(B, Closure, Src).
 2940process_predicate_comma(as(Spec, _Options), Closure, Src) :-
 2941    !,
 2942    process_predicate_comma(Spec, Closure, Src).
 2943process_predicate_comma(A, Closure, Src) :-
 2944    call(Closure, A, Src).
 2945
 2946
 2947assert_dynamic(PI, Src) :-
 2948    pi_to_head(PI, Term),
 2949    (   thread_local(Term, Src, _)  % dynamic after thread_local has
 2950    ->  true                        % no effect
 2951    ;   current_source_line(Line),
 2952        assert(dynamic(Term, Src, Line))
 2953    ).
 2954
 2955assert_thread_local(PI, Src) :-
 2956    pi_to_head(PI, Term),
 2957    current_source_line(Line),
 2958    assert(thread_local(Term, Src, Line)).
 2959
 2960assert_multifile(PI, Src) :-                    % :- multifile(Spec)
 2961    pi_to_head(PI, Term),
 2962    current_source_line(Line),
 2963    assert(multifile(Term, Src, Line)).
 2964
 2965assert_public(PI, Src) :-                       % :- public(Spec)
 2966    pi_to_head(PI, Term),
 2967    current_source_line(Line),
 2968    assert_called(Src, '<public>'(Line), Term, Line),
 2969    assert(public(Term, Src, Line)).
 2970
 2971assert_export(PI, Src) :-                       % :- export(Spec)
 2972    pi_to_head(PI, Term),
 2973    !,
 2974    assert(exported(Term, Src)).
 pi_to_head(+PI, -Head) is semidet
Translate Name/Arity or Name//Arity to a callable term. Fails if PI is not a predicate indicator.
 2981pi_to_head(Var, _) :-
 2982    var(Var), !, fail.
 2983pi_to_head(M:PI, M:Term) :-
 2984    !,
 2985    pi_to_head(PI, Term).
 2986pi_to_head(Name/Arity, Term) :-
 2987    functor(Term, Name, Arity).
 2988pi_to_head(Name//DCGArity, Term) :-
 2989    Arity is DCGArity+2,
 2990    functor(Term, Name, Arity).
 2991
 2992
 2993assert_used_class(Src, Name) :-
 2994    used_class(Name, Src),
 2995    !.
 2996assert_used_class(Src, Name) :-
 2997    assert(used_class(Name, Src)).
 2998
 2999assert_defined_class(Src, Name, _Meta, _Super, _) :-
 3000    defined_class(Name, _, _, Src, _),
 3001    !.
 3002assert_defined_class(_, _, _, -, _) :- !.               % :- pce_extend_class
 3003assert_defined_class(Src, Name, Meta, Super, Summary) :-
 3004    current_source_line(Line),
 3005    (   Summary == @(default)
 3006    ->  Atom = ''
 3007    ;   is_list(Summary)
 3008    ->  atom_codes(Atom, Summary)
 3009    ;   string(Summary)
 3010    ->  atom_concat(Summary, '', Atom)
 3011    ),
 3012    assert(defined_class(Name, Super, Atom, Src, Line)),
 3013    (   Meta = @(_)
 3014    ->  true
 3015    ;   assert_used_class(Src, Meta)
 3016    ),
 3017    assert_used_class(Src, Super).
 3018
 3019assert_defined_class(Src, Name, imported_from(_File)) :-
 3020    defined_class(Name, _, _, Src, _),
 3021    !.
 3022assert_defined_class(Src, Name, imported_from(File)) :-
 3023    assert(defined_class(Name, _, '', Src, file(File))).
 3024
 3025
 3026                /********************************
 3027                *            UTILITIES          *
 3028                ********************************/
 generalise(+Callable, -General)
Generalise a callable term.
 3034generalise(Var, Var) :-
 3035    var(Var),
 3036    !.                    % error?
 3037generalise(pce_principal:send_implementation(Id, _, _),
 3038           pce_principal:send_implementation(Id, _, _)) :-
 3039    atom(Id),
 3040    !.
 3041generalise(pce_principal:get_implementation(Id, _, _, _),
 3042           pce_principal:get_implementation(Id, _, _, _)) :-
 3043    atom(Id),
 3044    !.
 3045generalise('<directive>'(Line), '<directive>'(Line)) :- !.
 3046generalise(test(Test), test(Test)) :-
 3047    current_test_unit(_,_),
 3048    ground(Test),
 3049    !.
 3050generalise(test(Test, _), test(Test, _)) :-
 3051    current_test_unit(_,_),
 3052    ground(Test),
 3053    !.
 3054generalise('<test_unit>'(Line), '<test_unit>'(Line)) :- !.
 3055generalise(Module:Goal0, Module:Goal) :-
 3056    atom(Module),
 3057    !,
 3058    generalise(Goal0, Goal).
 3059generalise(Term0, Term) :-
 3060    callable(Term0),
 3061    generalise_term(Term0, Term).
 3062
 3063
 3064                 /*******************************
 3065                 *      SOURCE MANAGEMENT       *
 3066                 *******************************/
 3067
 3068/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 3069This section of the file contains   hookable  predicates to reason about
 3070sources. The built-in code here  can  only   deal  with  files. The XPCE
 3071library(pce_prolog_xref) provides hooks to deal with XPCE objects, so we
 3072can do cross-referencing on PceEmacs edit   buffers.  Other examples for
 3073hooking can be databases, (HTTP) URIs, etc.
 3074- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 3075
 3076:- multifile
 3077    prolog:xref_source_directory/2, % +Source, -Dir
 3078    prolog:xref_source_file/3.      % +Spec, -Path, +Options
 xref_source_file(+Spec, -File, +Src) is semidet
 xref_source_file(+Spec, -File, +Src, +Options) is semidet
Find named source file from Spec, relative to Src.
 3086xref_source_file(Plain, File, Source) :-
 3087    xref_source_file(Plain, File, Source, []).
 3088
 3089xref_source_file(QSpec, File, Source, Options) :-
 3090    nonvar(QSpec), QSpec = _:Spec,
 3091    !,
 3092    must_be(acyclic, Spec),
 3093    xref_source_file(Spec, File, Source, Options).
 3094xref_source_file(Spec, File, _Source, _Options) :-
 3095    is_stream(Spec), !,
 3096    File = Spec.
 3097xref_source_file(Spec, File, Source, Options) :-
 3098    nonvar(Spec),
 3099    prolog:xref_source_file(Spec, File,
 3100                            [ relative_to(Source)
 3101                            | Options
 3102                            ]),
 3103    !.
 3104xref_source_file(Plain, File, Source, Options) :-
 3105    atom(Plain),
 3106    \+ is_absolute_file_name(Plain),
 3107    (   prolog:xref_source_directory(Source, Dir)
 3108    ->  true
 3109    ;   atom(Source),
 3110        file_directory_name(Source, Dir)
 3111    ),
 3112    atomic_list_concat([Dir, /, Plain], Spec0),
 3113    absolute_file_name(Spec0, Spec),
 3114    do_xref_source_file(Spec, File, Options),
 3115    !.
 3116xref_source_file(Spec, File, Source, Options) :-
 3117    do_xref_source_file(Spec, File,
 3118                        [ relative_to(Source)
 3119                        | Options
 3120                        ]),
 3121    !.
 3122xref_source_file(_, _, _, Options) :-
 3123    option(silent(true), Options),
 3124    !,
 3125    fail.
 3126xref_source_file(Spec, _, Src, _Options) :-
 3127    verbose(Src),
 3128    \+ current_cond(_),             % may be guarded by exists_source/1
 3129    print_message(warning, error(existence_error(file, Spec), _)),
 3130    fail.
 3131
 3132do_xref_source_file(Spec, File, Options) :-
 3133    nonvar(Spec),
 3134    option(file_type(Type), Options, prolog),
 3135    absolute_file_name(Spec, File0,
 3136                       [ file_type(Type),
 3137                         access(read),
 3138                         file_errors(fail)
 3139                       ]),
 3140    !,
 3141    qlf_pl_file(File0, File).
 3142do_xref_source_file(Spec, File, Options) :-
 3143    atom(Spec), % handle absolute /file/to/source.pl without sources
 3144    file_name_extension(Base, Ext, Spec),
 3145    user:prolog_file_type(Ext, source),
 3146    option(file_type(prolog), Options, prolog),
 3147    absolute_file_name(Base, File0,
 3148                       [ file_type(prolog),
 3149                         access(read),
 3150                         file_errors(fail)
 3151                       ]),
 3152    qlf_pl_file(File0, File).
 qlf_pl_file(?QlfFile, ?PlFile) is semidet
 3156qlf_pl_file(QlfFile, PlFile) :-
 3157    nonvar(QlfFile),
 3158    is_qlf_file(QlfFile),
 3159    !,
 3160    '$qlf_module'(QlfFile, Info),
 3161    #{file:PlFile} :< Info.
 3162qlf_pl_file(QlfFile, PlFile) :-
 3163    nonvar(PlFile),
 3164    !,
 3165    (   file_name_extension(Base, Ext, PlFile),
 3166        user:prolog_file_type(Ext, source)
 3167    ->  true
 3168    ),
 3169    (   user:prolog_file_type(QlfExt, qlf),
 3170        file_name_extension(Base, QlfExt, QlfFile),
 3171        exists_file(QlfFile)
 3172    ->  true
 3173    ),
 3174    '$qlf_module'(QlfFile, Info),
 3175    #{file:PlFile} :< Info,
 3176    !.
 3177qlf_pl_file(PlFile, PlFile).
 3178
 3179is_qlf_file(QlfFile) :-
 3180    file_name_extension(_, Ext, QlfFile),
 3181    user:prolog_file_type(Ext, qlf),
 3182    !.
 canonical_source(?Source, ?Src) is det
Src is the canonical version of Source if Source is given.
 3188canonical_source(Source, Src) :-
 3189    (   ground(Source)
 3190    ->  prolog_canonical_source(Source, Src)
 3191    ;   Source = Src
 3192    ).
 goal_name_arity(+Goal, -Name, -Arity)
Generalized version of functor/3 that can deal with name() goals.
 3199goal_name_arity(Goal, Name, Arity) :-
 3200    (   compound(Goal)
 3201    ->  compound_name_arity(Goal, Name, Arity)
 3202    ;   atom(Goal)
 3203    ->  Name = Goal, Arity = 0
 3204    ).
 3205
 3206generalise_term(Specific, General) :-
 3207    (   compound(Specific)
 3208    ->  compound_name_arity(Specific, Name, Arity),
 3209        compound_name_arity(General, Name, Arity)
 3210    ;   General = Specific
 3211    ).
 3212
 3213functor_name(Term, Name) :-
 3214    (   compound(Term)
 3215    ->  compound_name_arity(Term, Name, _)
 3216    ;   atom(Term)
 3217    ->  Name = Term
 3218    ).
 3219
 3220rename_goal(Goal0, Name, Goal) :-
 3221    (   compound(Goal0)
 3222    ->  compound_name_arity(Goal0, _, Arity),
 3223        compound_name_arity(Goal, Name, Arity)
 3224    ;   Goal = Name
 3225    ).
 3226
 3227
 3228                /*******************************
 3229                *           MESSAGES           *
 3230                *******************************/<
 3231
 3232:- multifile prolog:message//1.
 3233
 3234prolog:message(meta_predicate_after_call(Decl, By)) -->
 3235    { pi_head(ByPI, By) },
 3236    [ ansi(code, ':- meta_predicate(~p)', [Decl]),
 3237      ' declaration appears after call from '-[],
 3238      ansi(code, '~p', [ByPI])
 3239    ]