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
  342last_modified(Source, Modified) :-
  343    prolog:xref_source_time(Source, Modified),
  344    !.
  345last_modified(Source, Modified) :-
  346    atom(Source),
  347    \+ is_global_url(Source),
  348    exists_file(Source),
  349    time_file(Source, Modified).
  350
  351is_global_url(File) :-
  352    sub_atom(File, B, _, _, '://'),
  353    !,
  354    B > 1,
  355    sub_atom(File, 0, B, _, Scheme),
  356    atom_codes(Scheme, Codes),
  357    maplist(between(0'a, 0'z), Codes).
  358
  359xref_setup(Src, In, Options, state(CleanIn, Dialect, Xref, [SRef|HRefs])) :-
  360    maplist(assert_option(Src), Options),
  361    assert_default_options(Src),
  362    current_prolog_flag(emulated_dialect, Dialect),
  363    (   option(stream(Stream), Options)
  364    ->  In = Stream,
  365        CleanIn = true
  366    ;   prolog_open_source(Src, In),
  367        CleanIn = prolog_close_source(In)
  368    ),
  369    set_initial_mode(In, Options),
  370    asserta(xref_input(Src, In), SRef),
  371    set_xref(Xref),
  372    (   verbose(Src)
  373    ->  HRefs = []
  374    ;   asserta((user:thread_message_hook(_,Level,_) :-
  375                     hide_message(Level)),
  376                Ref),
  377        HRefs = [Ref]
  378    ).
  379
  380hide_message(warning).
  381hide_message(error).
  382hide_message(informational).
  383
  384assert_option(_, Var) :-
  385    var(Var),
  386    !,
  387    instantiation_error(Var).
  388assert_option(Src, silent(Boolean)) :-
  389    !,
  390    must_be(boolean, Boolean),
  391    assert(xoption(Src, silent(Boolean))).
  392assert_option(Src, register_called(Which)) :-
  393    !,
  394    must_be(oneof([all,non_iso,non_built_in]), Which),
  395    assert(xoption(Src, register_called(Which))).
  396assert_option(Src, comments(CommentHandling)) :-
  397    !,
  398    must_be(oneof([store,collect,ignore]), CommentHandling),
  399    assert(xoption(Src, comments(CommentHandling))).
  400assert_option(Src, module(Module)) :-
  401    !,
  402    must_be(atom, Module),
  403    assert(xoption(Src, module(Module))).
  404assert_option(Src, process_include(Boolean)) :-
  405    !,
  406    must_be(boolean, Boolean),
  407    assert(xoption(Src, process_include(Boolean))).
  408assert_option(_, _).
  409
  410assert_default_options(Src) :-
  411    (   xref_option_default(Opt),
  412        generalise_term(Opt, Gen),
  413        (   xoption(Src, Gen)
  414        ->  true
  415        ;   assertz(xoption(Src, Opt))
  416        ),
  417        fail
  418    ;   true
  419    ).
  420
  421xref_option_default(silent(false)).
  422xref_option_default(register_called(non_built_in)).
  423xref_option_default(comments(collect)).
  424xref_option_default(process_include(true)).
 xref_cleanup(+State) is det
Restore processing state according to the saved State.
  430xref_cleanup(state(CleanIn, Dialect, Xref, Refs)) :-
  431    call(CleanIn),
  432    set_prolog_flag(emulated_dialect, Dialect),
  433    set_prolog_flag(xref, Xref),
  434    maplist(erase, Refs).
  435
  436set_xref(Xref) :-
  437    current_prolog_flag(xref, Xref),
  438    set_prolog_flag(xref, true).
  439
  440:- meta_predicate
  441    with_xref(0).  442
  443with_xref(Goal) :-
  444    setup_call_cleanup(
  445        push_prolog_flag(xref, true),
  446        Goal,
  447        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.
  457set_initial_mode(_Stream, Options) :-
  458    option(module(Module), Options),
  459    !,
  460    '$set_source_module'(Module).
  461set_initial_mode(Stream, _) :-
  462    stream_property(Stream, file_name(Path)),
  463    source_file_property(Path, load_context(M, _, Opts)),
  464    !,
  465    '$set_source_module'(M),
  466    (   option(dialect(Dialect), Opts)
  467    ->  expects_dialect(Dialect)
  468    ;   true
  469    ).
  470set_initial_mode(_, _) :-
  471    '$set_source_module'(user).
 xref_input_stream(-Stream) is det
Current input stream for cross-referencer.
  477xref_input_stream(Stream) :-
  478    xref_input(_, Var),
  479    !,
  480    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.
  487xref_push_op(Src, P, T, N0) :-
  488    '$current_source_module'(M0),
  489    strip_module(M0:N0, M, N),
  490    (   is_list(N),
  491        N \== []
  492    ->  maplist(push_op(Src, P, T, M), N)
  493    ;   push_op(Src, P, T, M, N)
  494    ).
  495
  496push_op(Src, P, T, M0, N0) :-
  497    strip_module(M0:N0, M, N),
  498    Name = M:N,
  499    valid_op(op(P,T,Name)),
  500    push_op(P, T, Name),
  501    assert_op(Src, op(P,T,Name)),
  502    debug(xref(op), ':- ~w.', [op(P,T,Name)]).
  503
  504valid_op(op(P,T,M:N)) :-
  505    atom(M),
  506    valid_op_name(N),
  507    integer(P),
  508    between(0, 1200, P),
  509    atom(T),
  510    op_type(T).
  511
  512valid_op_name(N) :-
  513    atom(N),
  514    !.
  515valid_op_name(N) :-
  516    N == [].
  517
  518op_type(xf).
  519op_type(yf).
  520op_type(fx).
  521op_type(fy).
  522op_type(xfx).
  523op_type(xfy).
  524op_type(yfx).
 xref_set_prolog_flag(+Flag, +Value, +Src, +Line)
Called when a directive sets a Prolog flag.
  530xref_set_prolog_flag(Flag, Value, Src, Line) :-
  531    atom(Flag),
  532    !,
  533    assertz(xflag(Flag, Value, Src, Line)).
  534xref_set_prolog_flag(_, _, _, _).
 xref_clean(+Source) is det
Reset the database for the given source.
  540xref_clean(Source) :-
  541    prolog_canonical_source(Source, Src),
  542    retractall(called(_, Src, _Origin, _Cond, _Line)),
  543    retractall(dynamic(_, Src, Line)),
  544    retractall(multifile(_, Src, Line)),
  545    retractall(public(_, Src, Line)),
  546    retractall(declared(_, _, Src, Line)),
  547    retractall(defined(_, Src, Line)),
  548    retractall(meta_goal(_, _, Src)),
  549    retractall(foreign(_, Src, Line)),
  550    retractall(constraint(_, Src, Line)),
  551    retractall(imported(_, Src, _From)),
  552    retractall(exported(_, Src)),
  553    retractall(uses_file(_, Src, _)),
  554    retractall(xmodule(_, Src)),
  555    retractall(xop(Src, _)),
  556    retractall(grammar_rule(_, Src)),
  557    retractall(xoption(Src, _)),
  558    retractall(xflag(_Name, _Value, Src, Line)),
  559    retractall(source(Src, _)),
  560    retractall(used_class(_, Src)),
  561    retractall(defined_class(_, _, _, Src, _)),
  562    retractall(mode(_, Src)),
  563    retractall(module_comment(Src, _, _)),
  564    retractall(pred_comment(_, Src, _, _)),
  565    retractall(pred_comment_link(_, Src, _)),
  566    retractall(pred_mode(_, Src, _)),
  567    retractall(object_comment(_, Src, _, _)),
  568    retractall(object_signature(_, Src, _)).
  569
  570
  571                 /*******************************
  572                 *          READ RESULTS        *
  573                 *******************************/
 xref_current_source(?Source)
Check what sources have been analysed.
  579xref_current_source(Source) :-
  580    source(Source, _Time).
 xref_done(+Source, -Time) is det
Cross-reference executed at Time
  587xref_done(Source, Time) :-
  588    prolog_canonical_source(Source, Src),
  589    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.
  611xref_called(Source, Called, By) :-
  612    xref_called(Source, Called, By, _).
  613
  614xref_called(Source, Called, By, Cond) :-
  615    canonical_source(Source, Src),
  616    distinct(Called-By, called(Called, Src, By, Cond, _)).
  617
  618xref_called(Source, Called, By, Cond, Line) :-
  619    canonical_source(Source, Src),
  620    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.
  642xref_defined(Source, Called, How) :-
  643    nonvar(Source),
  644    !,
  645    canonical_source(Source, Src),
  646    xref_defined2(How, Src, Called).
  647xref_defined(Source, Called, How) :-
  648    xref_defined2(How, Src, Called),
  649    canonical_source(Source, Src).
  650
  651xref_defined2(dynamic(Line), Src, Called) :-
  652    dynamic(Called, Src, Line).
  653xref_defined2(thread_local(Line), Src, Called) :-
  654    thread_local(Called, Src, Line).
  655xref_defined2(multifile(Line), Src, Called) :-
  656    multifile(Called, Src, Line).
  657xref_defined2(public(Line), Src, Called) :-
  658    public(Called, Src, Line).
  659xref_defined2(local(Line), Src, Called) :-
  660    defined(Called, Src, Line).
  661xref_defined2(foreign(Line), Src, Called) :-
  662    foreign(Called, Src, Line).
  663xref_defined2(constraint(Line), Src, Called) :-
  664    (   constraint(Called, Src, Line)
  665    ->  true
  666    ;   declared(Called, chr_constraint, Src, Line)
  667    ).
  668xref_defined2(imported(From), Src, Called) :-
  669    imported(Called, Src, From).
  670xref_defined2(dcg, Src, Called) :-
  671    grammar_rule(Called, Src).
 xref_definition_line(+How, -Line)
If the 3th argument of xref_defined contains line info, return this in Line.
  679xref_definition_line(local(Line),        Line).
  680xref_definition_line(dynamic(Line),      Line).
  681xref_definition_line(thread_local(Line), Line).
  682xref_definition_line(multifile(Line),    Line).
  683xref_definition_line(public(Line),       Line).
  684xref_definition_line(constraint(Line),   Line).
  685xref_definition_line(foreign(Line),      Line).
 xref_exported(?Source, ?Head) is nondet
True when Source exports Head.
  692xref_exported(Source, Called) :-
  693    prolog_canonical_source(Source, Src),
  694    exported(Called, Src).
 xref_module(?Source, ?Module) is nondet
True if Module is defined in Source.
  700xref_module(Source, Module) :-
  701    nonvar(Source),
  702    !,
  703    prolog_canonical_source(Source, Src),
  704    xmodule(Module, Src).
  705xref_module(Source, Module) :-
  706    xmodule(Module, Src),
  707    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>.
  717xref_uses_file(Source, Spec, Path) :-
  718    prolog_canonical_source(Source, Src),
  719    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)
  729xref_op(Source, Op) :-
  730    prolog_canonical_source(Source, Src),
  731    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.
  739xref_prolog_flag(Source, Flag, Value, Line) :-
  740    prolog_canonical_source(Source, Src),
  741    xflag(Flag, Value, Src, Line).
  742
  743xref_built_in(Head) :-
  744    system_predicate(Head).
  745
  746xref_used_class(Source, Class) :-
  747    prolog_canonical_source(Source, Src),
  748    used_class(Class, Src).
  749
  750xref_defined_class(Source, Class, local(Line, Super, Summary)) :-
  751    prolog_canonical_source(Source, Src),
  752    defined_class(Class, Super, Summary, Src, Line),
  753    integer(Line).
  754xref_defined_class(Source, Class, file(File)) :-
  755    prolog_canonical_source(Source, Src),
  756    defined_class(Class, _, _, Src, file(File)).
  757
  758:- thread_local
  759    current_cond/1,
  760    source_line/1,
  761    current_test_unit/2.  762
  763current_source_line(Line) :-
  764    source_line(Var),
  765    !,
  766    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.
  774collect(Src, File, In, Options) :-
  775    (   Src == File
  776    ->  SrcSpec = Line
  777    ;   SrcSpec = (File:Line)
  778    ),
  779    (   current_prolog_flag(xref_store_comments, OldStore)
  780    ->  true
  781    ;   OldStore = false
  782    ),
  783    option(comments(CommentHandling), Options, collect),
  784    (   CommentHandling == ignore
  785    ->  CommentOptions = [],
  786        Comments = []
  787    ;   CommentHandling == store
  788    ->  CommentOptions = [ process_comment(true) ],
  789        Comments = [],
  790	set_prolog_flag(xref_store_comments, true)
  791    ;   CommentOptions = [ comments(Comments) ]
  792    ),
  793    repeat,
  794        E = error(_,_),
  795        catch(prolog_read_source_term(
  796                  In, Term, Expanded,
  797                  [ term_position(TermPos),
  798                    variable_names(VarNames)
  799                  | CommentOptions
  800                  ]),
  801              E, report_syntax_error(E, Src, [])),
  802        update_condition(Term),
  803        stream_position_data(line_count, TermPos, Line),
  804        setup_call_cleanup(
  805            asserta(source_line(SrcSpec), Ref),
  806            catch(process(Expanded, Comments, Term, VarNames, TermPos,
  807                          Src, EOF),
  808                  E, print_message(error, E)),
  809            erase(Ref)),
  810        EOF == true,
  811    !,
  812    set_prolog_flag(xref_store_comments, OldStore).
  813
  814report_syntax_error(_, _, Options) :-
  815    option(silent(true), Options),
  816    !,
  817    fail.
  818report_syntax_error(E, Src, _Options) :-
  819    (   verbose(Src)
  820    ->  print_message(error, E)
  821    ;   true
  822    ),
  823    fail.
 update_condition(+Term) is det
Update the condition under which the current code is compiled.
  829update_condition((:-Directive)) :-
  830    !,
  831    update_cond(Directive).
  832update_condition(_).
  833
  834update_cond(if(Cond)) :-
  835    !,
  836    asserta(current_cond(Cond)).
  837update_cond(else) :-
  838    retract(current_cond(C0)),
  839    !,
  840    assert(current_cond(\+C0)).
  841update_cond(elif(Cond)) :-
  842    retract(current_cond(C0)),
  843    !,
  844    assert(current_cond((\+C0,Cond))).
  845update_cond(endif) :-
  846    retract(current_cond(_)),
  847    !.
  848update_cond(_).
 current_condition(-Condition) is det
Condition is the current compilation condition as defined by the :- if/1 directive and friends.
  855current_condition(Condition) :-
  856    \+ current_cond(_),
  857    !,
  858    Condition = true.
  859current_condition(Condition) :-
  860    findall(C, current_cond(C), List),
  861    list_to_conj(List, Condition).
  862
  863list_to_conj([], true).
  864list_to_conj([C], C) :- !.
  865list_to_conj([H|T], (H,C)) :-
  866    list_to_conj(T, C).
  867
  868
  869                 /*******************************
  870                 *           PROCESS            *
  871                 *******************************/
 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.
  886process([], Comments, Term, VarNames, TermPos, Src, false) :-
  887    !,
  888    process_nil_expanded(Term, Src),
  889    xref_comments(Comments, TermPos, Term, VarNames, Src).
  890process(Expanded, Comments, Term0, VarNames, TermPos, Src, EOF) :-
  891    is_list(Expanded),                          % term_expansion into list.
  892    !,
  893    (   member(Term, Expanded),
  894        process(Term, Term0, Src),
  895        Term == end_of_file
  896    ->  EOF = true
  897    ;   EOF = false
  898    ),
  899    xref_comments(Comments, TermPos, Term0, VarNames, Src).
  900process(end_of_file, _, _, _, _, _, true) :-
  901    !.
  902process(Term, Comments, Term0, VarNames, TermPos, Src, false) :-
  903    process(Term, Term0, Src),
  904    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.
  910process_nil_expanded((:- pce_class_directive(Goal)), Src) :-
  911    !,
  912    current_source_line(Line),
  913    assert_called(Src, '<directive>'(Line), Goal, Line).
  914process_nil_expanded(_, _).
 process(+Term, +Term0, +Src) is det
  918process(_, Term0, _) :-
  919    ignore_raw_term(Term0),
  920    !.
  921process(Head :- Body, Head0 --> _, Src) :-
  922    pi_head(F/A, Head),
  923    pi_head(F/A0, Head0),
  924    A =:= A0 + 2,
  925    !,
  926    assert_grammar_rule(Src, Head),
  927    process((Head :- Body), Src).
  928process(Term, _Term0, Src) :-
  929    process(Term, Src).
  930
  931ignore_raw_term((:- predicate_options(_,_,_))).
 process(+Term, +Src) is det
  935process(Var, _) :-
  936    var(Var),
  937    !.                    % Warn?
  938process(end_of_file, _) :- !.
  939process((:- Directive), Src) :-
  940    !,
  941    process_directive(Directive, Src),
  942    !.
  943process((?- Directive), Src) :-
  944    !,
  945    process_directive(Directive, Src),
  946    !.
  947process((Head :- Body), Src) :-
  948    !,
  949    assert_defined(Src, Head),
  950    process_body(Body, Head, Src).
  951process((Left => Body), Src) :-
  952    !,
  953    (   nonvar(Left),
  954        Left = (Head, Guard)
  955    ->  assert_defined(Src, Head),
  956        process_body(Guard, Head, Src),
  957        process_body(Body, Head, Src)
  958    ;   assert_defined(Src, Left),
  959        process_body(Body, Left, Src)
  960    ).
  961process(?=>(Head, Body), Src) :-
  962    !,
  963    assert_defined(Src, Head),
  964    process_body(Body, Head, Src).
  965process('$source_location'(_File, _Line):Clause, Src) :-
  966    !,
  967    process(Clause, Src).
  968process(Term, Src) :-
  969    process_chr(Term, Src),
  970    !.
  971process(M:(Head :- Body), Src) :-
  972    !,
  973    process((M:Head :- M:Body), Src).
  974process(Head, Src) :-
  975    assert_defined(Src, Head).
  976
  977
  978                 /*******************************
  979                 *            COMMENTS          *
  980                 *******************************/
 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.
  988xref_comments([], _Pos, _Term, _VarNames, _Src).
  989:- if(current_predicate(parse_comment/5)).  990xref_comments([Pos-Comment|T], TermPos, Term, VarNames, Src) :-
  991    (   Pos @> TermPos              % comments inside term
  992    ->  true
  993    ;   stream_position_data(line_count, Pos, Line),
  994        FilePos = Src:Line,
  995        (   last_comment(T, TermPos)
  996        ->  CTerm = Term
  997        ;   true
  998        ),
  999        (   parse_comment(Comment, FilePos, CTerm, VarNames, Parsed)
 1000        ->  assert_comments(Parsed, Src)
 1001        ;   true
 1002        ),
 1003        xref_comments(T, TermPos, Term, VarNames, Src)
 1004    ).
 1005
 1006last_comment([], _).
 1007last_comment([Pos-_|_], TermPos) :-
 1008    Pos @> TermPos.
 1009
 1010assert_comments([], _).
 1011assert_comments([H|T], Src) :-
 1012    assert_comment(H, Src),
 1013    assert_comments(T, Src).
 1014
 1015assert_comment(section(Id, Title, Comment), Src) :-
 1016    (   Id = module(_)
 1017    ->  assertz(module_comment(Src, Title, Comment))
 1018    ;   true
 1019    ).
 1020assert_comment(predicate(PI, Summary, Comment), Src) :-
 1021    pi_to_head(PI, Src, Head),
 1022    assertz(pred_comment(Head, Src, Summary, Comment)).
 1023assert_comment(link(PI, PITo), Src) :-
 1024    pi_to_head(PI, Src, Head),
 1025    pi_to_head(PITo, Src, HeadTo),
 1026    assertz(pred_comment_link(Head, Src, HeadTo)).
 1027assert_comment(mode(Head, Det), Src) :-
 1028    assertz(pred_mode(Head, Src, Det)).
 1029assert_comment(object(Object, Summary, Comment), Src) :-
 1030    assertz(object_comment(Object, Src, Summary, Comment)).
 1031assert_comment(signature(Object, Signature), Src) :-
 1032    assertz(object_signature(Object, Src, Signature)).
 1033
 1034pi_to_head(PI, Src, Head) :-
 1035    pi_to_head(PI, Head0),
 1036    (   Head0 = _:_
 1037    ->  strip_module(Head0, M, Plain),
 1038        (   xmodule(M, Src)
 1039        ->  Head = Plain
 1040        ;   Head = M:Plain
 1041        )
 1042    ;   Head = Head0
 1043    ).
 1044:- endif.
 xref_comment(?Source, ?Title, ?Comment) is nondet
Is true when Source has a section comment with Title and Comment
 1050xref_comment(Source, Title, Comment) :-
 1051    canonical_source(Source, Src),
 1052    module_comment(Src, Title, Comment).
 xref_comment(?Source, ?Head, ?Summary, ?Comment) is nondet
Is true when Head in Source has the given PlDoc comment.
 1058xref_comment(Source, Head, Summary, Comment) :-
 1059    canonical_source(Source, Src),
 1060    (   pred_comment(Head, Src, Summary, Comment)
 1061    ;   pred_comment_link(Head, Src, HeadTo),
 1062        pred_comment(HeadTo, Src, Summary, Comment)
 1063    ).
 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.
 1071xref_object_comment(Source, Object, Summary, Comment) :-
 1072    canonical_source(Source, Src),
 1073    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.
 1080xref_object_signature(Source, Object, Signature) :-
 1081    canonical_source(Source, Src),
 1082    object_signature(Object, Src, Signature).
 xref_mode(?Source, ?Mode, ?Det) is nondet
Is true when Source provides a predicate with Mode and determinism.
 1089xref_mode(Source, Mode, Det) :-
 1090    canonical_source(Source, Src),
 1091    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.
 1098xref_option(Source, Option) :-
 1099    canonical_source(Source, Src),
 1100    xoption(Src, Option).
 1101
 1102
 1103                 /********************************
 1104                 *           DIRECTIVES         *
 1105                 ********************************/
 1106
 1107process_directive(Var, _) :-
 1108    var(Var),
 1109    !.                    % error, but that isn't our business
 1110process_directive(Dir, _Src) :-
 1111    debug(xref(directive), 'Processing :- ~q', [Dir]),
 1112    fail.
 1113process_directive((A,B), Src) :-       % TBD: what about other control
 1114    !,
 1115    process_directive(A, Src),      % structures?
 1116    process_directive(B, Src).
 1117process_directive(List, Src) :-
 1118    is_list(List),
 1119    !,
 1120    process_directive(consult(List), Src).
 1121process_directive(use_module(File, Import), Src) :-
 1122    process_use_module2(File, Import, Src, false).
 1123process_directive(autoload(File, Import), Src) :-
 1124    process_use_module2(File, Import, Src, false).
 1125process_directive(require(Import), Src) :-
 1126    process_requires(Import, Src).
 1127process_directive(expects_dialect(Dialect), Src) :-
 1128    process_directive(use_module(library(dialect/Dialect)), Src),
 1129    expects_dialect(Dialect).
 1130process_directive(reexport(File, Import), Src) :-
 1131    process_use_module2(File, Import, Src, true).
 1132process_directive(reexport(Modules), Src) :-
 1133    process_use_module(Modules, Src, true).
 1134process_directive(autoload(Modules), Src) :-
 1135    process_use_module(Modules, Src, false).
 1136process_directive(use_module(Modules), Src) :-
 1137    process_use_module(Modules, Src, false).
 1138process_directive(consult(Modules), Src) :-
 1139    process_use_module(Modules, Src, false).
 1140process_directive(ensure_loaded(Modules), Src) :-
 1141    process_use_module(Modules, Src, false).
 1142process_directive(load_files(Files, _Options), Src) :-
 1143    process_use_module(Files, Src, false).
 1144process_directive(include(Files), Src) :-
 1145    process_include(Files, Src).
 1146process_directive(dynamic(Dynamic), Src) :-
 1147    process_predicates(assert_dynamic, Dynamic, Src).
 1148process_directive(dynamic(Dynamic, _Options), Src) :-
 1149    process_predicates(assert_dynamic, Dynamic, Src).
 1150process_directive(thread_local(Dynamic), Src) :-
 1151    process_predicates(assert_thread_local, Dynamic, Src).
 1152process_directive(multifile(Dynamic), Src) :-
 1153    process_predicates(assert_multifile, Dynamic, Src).
 1154process_directive(public(Public), Src) :-
 1155    process_predicates(assert_public, Public, Src).
 1156process_directive(export(Export), Src) :-
 1157    process_predicates(assert_export, Export, Src).
 1158process_directive(import(Import), Src) :-
 1159    process_import(Import, Src).
 1160process_directive(module(Module, Export), Src) :-
 1161    assert_module(Src, Module),
 1162    assert_module_export(Src, Export).
 1163process_directive(module(Module, Export, Import), Src) :-
 1164    assert_module(Src, Module),
 1165    assert_module_export(Src, Export),
 1166    assert_module3(Import, Src).
 1167process_directive(begin_tests(Unit, _Options), Src) :-
 1168    enter_test_unit(Unit, Src).
 1169process_directive(begin_tests(Unit), Src) :-
 1170    enter_test_unit(Unit, Src).
 1171process_directive(end_tests(Unit), Src) :-
 1172    leave_test_unit(Unit, Src).
 1173process_directive('$set_source_module'(system), Src) :-
 1174    assert_module(Src, system).     % hack for handling boot/init.pl
 1175process_directive(pce_begin_class_definition(Name, Meta, Super, Doc), Src) :-
 1176    assert_defined_class(Src, Name, Meta, Super, Doc).
 1177process_directive(pce_autoload(Name, From), Src) :-
 1178    assert_defined_class(Src, Name, imported_from(From)).
 1179
 1180process_directive(op(P, A, N), Src) :-
 1181    xref_push_op(Src, P, A, N).
 1182process_directive(set_prolog_flag(Flag, Value), Src) :-
 1183    (   Flag == character_escapes
 1184    ->  set_prolog_flag(character_escapes, Value)
 1185    ;   true
 1186    ),
 1187    current_source_line(Line),
 1188    xref_set_prolog_flag(Flag, Value, Src, Line).
 1189process_directive(style_check(X), _) :-
 1190    style_check(X).
 1191process_directive(encoding(Enc), _) :-
 1192    (   xref_input_stream(Stream)
 1193    ->  catch(set_stream(Stream, encoding(Enc)), error(_,_), true)
 1194    ;   true                        % can this happen?
 1195    ).
 1196process_directive(pce_expansion:push_compile_operators, _) :-
 1197    '$current_source_module'(SM),
 1198    call(pce_expansion:push_compile_operators(SM)). % call to avoid xref
 1199process_directive(pce_expansion:pop_compile_operators, _) :-
 1200    call(pce_expansion:pop_compile_operators).
 1201process_directive(meta_predicate(Meta), Src) :-
 1202    process_meta_predicate(Meta, Src).
 1203process_directive(arithmetic_function(FSpec), Src) :-
 1204    arith_callable(FSpec, Goal),
 1205    !,
 1206    current_source_line(Line),
 1207    assert_called(Src, '<directive>'(Line), Goal, Line).
 1208process_directive(format_predicate(_, Goal), Src) :-
 1209    !,
 1210    current_source_line(Line),
 1211    assert_called(Src, '<directive>'(Line), Goal, Line).
 1212process_directive(if(Cond), Src) :-
 1213    !,
 1214    current_source_line(Line),
 1215    assert_called(Src, '<directive>'(Line), Cond, Line).
 1216process_directive(elif(Cond), Src) :-
 1217    !,
 1218    current_source_line(Line),
 1219    assert_called(Src, '<directive>'(Line), Cond, Line).
 1220process_directive(else, _) :- !.
 1221process_directive(endif, _) :- !.
 1222process_directive(Goal, Src) :-
 1223    current_source_line(Line),
 1224    process_body(Goal, '<directive>'(Line), Src).
 process_meta_predicate(+Decl, +Src)
Create meta_goal/3 facts from the meta-goal declaration.
 1230process_meta_predicate((A,B), Src) :-
 1231    !,
 1232    process_meta_predicate(A, Src),
 1233    process_meta_predicate(B, Src).
 1234process_meta_predicate(Decl, Src) :-
 1235    process_meta_head(Src, Decl).
 1236
 1237process_meta_head(Src, Decl) :-         % swapped arguments for maplist
 1238    compound(Decl),
 1239    compound_name_arity(Decl, Name, Arity),
 1240    compound_name_arity(Head, Name, Arity),
 1241    meta_args(1, Arity, Decl, Head, Meta),
 1242    (   (   prolog:meta_goal(Head, _)
 1243        ;   prolog:called_by(Head, _, _, _)
 1244        ;   prolog:called_by(Head, _)
 1245        ;   meta_goal(Head, Meta, _Src)
 1246        )
 1247    ->  true
 1248    ;   warn_late_meta_predicate(Decl, Src),
 1249        retractall(meta_goal(Head, _, Src)),
 1250        assert(meta_goal(Head, Meta, Src))
 1251    ).
 1252
 1253meta_args(I, Arity, _, _, []) :-
 1254    I > Arity,
 1255    !.
 1256meta_args(I, Arity, Decl, Head, [H|T]) :-               % 0
 1257    arg(I, Decl, 0),
 1258    !,
 1259    arg(I, Head, H),
 1260    I2 is I + 1,
 1261    meta_args(I2, Arity, Decl, Head, T).
 1262meta_args(I, Arity, Decl, Head, [H|T]) :-               % ^
 1263    arg(I, Decl, ^),
 1264    !,
 1265    arg(I, Head, EH),
 1266    setof_goal(EH, H),
 1267    I2 is I + 1,
 1268    meta_args(I2, Arity, Decl, Head, T).
 1269meta_args(I, Arity, Decl, Head, [//(H)|T]) :-
 1270    arg(I, Decl, //),
 1271    !,
 1272    arg(I, Head, H),
 1273    I2 is I + 1,
 1274    meta_args(I2, Arity, Decl, Head, T).
 1275meta_args(I, Arity, Decl, Head, [H+A|T]) :-             % I --> H+I
 1276    arg(I, Decl, A),
 1277    integer(A), A > 0,
 1278    !,
 1279    arg(I, Head, H),
 1280    I2 is I + 1,
 1281    meta_args(I2, Arity, Decl, Head, T).
 1282meta_args(I, Arity, Decl, Head, Meta) :-
 1283    I2 is I + 1,
 1284    meta_args(I2, Arity, Decl, Head, Meta).
 1285
 1286
 1287warn_late_meta_predicate(Decl, Src) :-
 1288    xref_called(Src, Decl, By),
 1289    !,
 1290    print_message(warning, meta_predicate_after_call(Decl, By)).
 1291warn_late_meta_predicate(_, _).
 1292
 1293
 1294              /********************************
 1295              *             BODY              *
 1296              ********************************/
 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).
 1305xref_meta(Source, Head, Called) :-
 1306    canonical_source(Source, Src),
 1307    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.
 1322xref_meta_src(Head, Called, Src) :-
 1323    meta_goal(Head, Called, Src),
 1324    !.
 1325xref_meta_src(Head, Called, _) :-
 1326    xref_meta(Head, Called),
 1327    !.
 1328xref_meta_src(Head, Called, _) :-
 1329    compound(Head),
 1330    compound_name_arity(Head, Name, Arity),
 1331    apply_pred(Name),
 1332    Arity > 5,
 1333    !,
 1334    Extra is Arity - 1,
 1335    arg(1, Head, G),
 1336    Called = [G+Extra].
 1337xref_meta_src(Head, Called, _) :-
 1338    with_xref(predicate_property('$xref_tmp':Head, meta_predicate(Meta))),
 1339    !,
 1340    Meta =.. [_|Args],
 1341    meta_args(Args, 1, Head, Called).
 1342
 1343meta_args([], _, _, []).
 1344meta_args([H0|T0], I, Head, [H|T]) :-
 1345    xargs(H0, N),
 1346    !,
 1347    arg(I, Head, A),
 1348    (   N == 0
 1349    ->  H = A
 1350    ;   H = (A+N)
 1351    ),
 1352    I2 is I+1,
 1353    meta_args(T0, I2, Head, T).
 1354meta_args([_|T0], I, Head, T) :-
 1355    I2 is I+1,
 1356    meta_args(T0, I2, Head, T).
 1357
 1358xargs(N, N) :- integer(N), !.
 1359xargs(//, 2).
 1360xargs(^, 0).
 1361
 1362apply_pred(call).                               % built-in
 1363apply_pred(maplist).                            % library(apply_macros)
 1364
 1365xref_meta((A, B),               [A, B]).
 1366xref_meta((A; B),               [A, B]).
 1367xref_meta((A| B),               [A, B]).
 1368xref_meta((A -> B),             [A, B]).
 1369xref_meta((A *-> B),            [A, B]).
 1370xref_meta(findall(_V,G,_L),     [G]).
 1371xref_meta(findall(_V,G,_L,_T),  [G]).
 1372xref_meta(findnsols(_N,_V,G,_L),    [G]).
 1373xref_meta(findnsols(_N,_V,G,_L,_T), [G]).
 1374xref_meta(setof(_V, EG, _L),    [G]) :-
 1375    setof_goal(EG, G).
 1376xref_meta(bagof(_V, EG, _L),    [G]) :-
 1377    setof_goal(EG, G).
 1378xref_meta(aggregate(_V, EG, _L), [G]) :-
 1379    setof_goal(EG, G).
 1380xref_meta(aggregate(_V, _D, EG, _L), [G]) :-
 1381    setof_goal(EG, G).
 1382xref_meta(forall(A, B),         [A, B]).
 1383xref_meta(maplist(G,_),         [G+1]).
 1384xref_meta(maplist(G,_,_),       [G+2]).
 1385xref_meta(maplist(G,_,_,_),     [G+3]).
 1386xref_meta(maplist(G,_,_,_,_),   [G+4]).
 1387xref_meta(map_list_to_pairs(G,_,_), [G+2]).
 1388xref_meta(map_assoc(G, _),      [G+1]).
 1389xref_meta(map_assoc(G, _, _),   [G+2]).
 1390xref_meta(checklist(G, _L),     [G+1]).
 1391xref_meta(sublist(G, _, _),     [G+1]).
 1392xref_meta(include(G, _, _),     [G+1]).
 1393xref_meta(exclude(G, _, _),     [G+1]).
 1394xref_meta(partition(G, _, _, _, _),     [G+2]).
 1395xref_meta(partition(G, _, _, _),[G+1]).
 1396xref_meta(call(G),              [G]).
 1397xref_meta(call(G, _),           [G+1]).
 1398xref_meta(call(G, _, _),        [G+2]).
 1399xref_meta(call(G, _, _, _),     [G+3]).
 1400xref_meta(call(G, _, _, _, _),  [G+4]).
 1401xref_meta(not(G),               [G]).
 1402xref_meta(notrace(G),           [G]).
 1403xref_meta('$notrace'(G),        [G]).
 1404xref_meta(\+(G),                [G]).
 1405xref_meta(ignore(G),            [G]).
 1406xref_meta(once(G),              [G]).
 1407xref_meta(initialization(G),    [G]).
 1408xref_meta(initialization(G,_),  [G]).
 1409xref_meta(retract(Rule),        [G]) :- head_of(Rule, G).
 1410xref_meta(clause(G, _),         [G]).
 1411xref_meta(clause(G, _, _),      [G]).
 1412xref_meta(phrase(G, _A),        [//(G)]).
 1413xref_meta(phrase(G, _A, _R),    [//(G)]).
 1414xref_meta(call_dcg(G, _A, _R),  [//(G)]).
 1415xref_meta(phrase_from_file(G,_),[//(G)]).
 1416xref_meta(catch(A, _, B),       [A, B]).
 1417xref_meta(catch_with_backtrace(A, _, B), [A, B]).
 1418xref_meta(thread_create(A,_,_), [A]).
 1419xref_meta(thread_create(A,_),   [A]).
 1420xref_meta(thread_signal(_,A),   [A]).
 1421xref_meta(thread_idle(A,_),     [A]).
 1422xref_meta(thread_at_exit(A),    [A]).
 1423xref_meta(thread_initialization(A), [A]).
 1424xref_meta(engine_create(_,A,_), [A]).
 1425xref_meta(engine_create(_,A,_,_), [A]).
 1426xref_meta(transaction(A),       [A]).
 1427xref_meta(transaction(A,B,_),   [A,B]).
 1428xref_meta(snapshot(A),          [A]).
 1429xref_meta(predsort(A,_,_),      [A+3]).
 1430xref_meta(call_cleanup(A, B),   [A, B]).
 1431xref_meta(call_cleanup(A, _, B),[A, B]).
 1432xref_meta(setup_call_cleanup(A, B, C),[A, B, C]).
 1433xref_meta(setup_call_catcher_cleanup(A, B, _, C),[A, B, C]).
 1434xref_meta(call_residue_vars(A,_), [A]).
 1435xref_meta(with_mutex(_,A),      [A]).
 1436xref_meta(assume(G),            [G]).   % library(debug)
 1437xref_meta(assertion(G),         [G]).   % library(debug)
 1438xref_meta(freeze(_, G),         [G]).
 1439xref_meta(when(C, A),           [C, A]).
 1440xref_meta(time(G),              [G]).   % development system
 1441xref_meta(call_time(G, _),      [G]).   % development system
 1442xref_meta(call_time(G, _, _),   [G]).   % development system
 1443xref_meta(profile(G),           [G]).
 1444xref_meta(at_halt(G),           [G]).
 1445xref_meta(call_with_time_limit(_, G), [G]).
 1446xref_meta(call_with_depth_limit(G, _, _), [G]).
 1447xref_meta(call_with_inference_limit(G, _, _), [G]).
 1448xref_meta(alarm(_, G, _),       [G]).
 1449xref_meta(alarm(_, G, _, _),    [G]).
 1450xref_meta('$add_directive_wic'(G), [G]).
 1451xref_meta(with_output_to(_, G), [G]).
 1452xref_meta(if(G),                [G]).
 1453xref_meta(elif(G),              [G]).
 1454xref_meta(meta_options(G,_,_),  [G+1]).
 1455xref_meta(on_signal(_,_,H),     [H+1]) :- H \== default.
 1456xref_meta(distinct(G),          [G]).   % library(solution_sequences)
 1457xref_meta(distinct(_, G),       [G]).
 1458xref_meta(order_by(_, G),       [G]).
 1459xref_meta(limit(_, G),          [G]).
 1460xref_meta(offset(_, G),         [G]).
 1461xref_meta(reset(G,_,_),         [G]).
 1462xref_meta(prolog_listen(Ev,G),  [G+N]) :- event_xargs(Ev, N).
 1463xref_meta(prolog_listen(Ev,G,_),[G+N]) :- event_xargs(Ev, N).
 1464xref_meta(tnot(G),		[G]).
 1465xref_meta(not_exists(G),	[G]).
 1466xref_meta(with_tty_raw(G),	[G]).
 1467xref_meta(residual_goals(G),    [G+2]).
 1468
 1469                                        % XPCE meta-predicates
 1470xref_meta(pce_global(_, new(_)), _) :- !, fail.
 1471xref_meta(pce_global(_, B),     [B+1]).
 1472xref_meta(ifmaintainer(G),      [G]).   % used in manual
 1473xref_meta(listen(_, G),         [G]).   % library(broadcast)
 1474xref_meta(listen(_, _, G),      [G]).
 1475xref_meta(in_pce_thread(G),     [G]).
 1476
 1477xref_meta(G, Meta) :-                   % call user extensions
 1478    prolog:meta_goal(G, Meta).
 1479xref_meta(G, Meta) :-                   % Generated from :- meta_predicate
 1480    meta_goal(G, Meta, _Src).
 1481
 1482setof_goal(EG, G) :-
 1483    var(EG), !, G = EG.
 1484setof_goal(_^EG, G) :-
 1485    !,
 1486    setof_goal(EG, G).
 1487setof_goal(G, G).
 1488
 1489event_xargs(abort,            0).
 1490event_xargs(erase,            1).
 1491event_xargs(break,            3).
 1492event_xargs(frame_finished,   1).
 1493event_xargs(thread_exit,      1).
 1494event_xargs(this_thread_exit, 0).
 1495event_xargs(PI,               2) :- pi_to_head(PI, _).
 head_of(+Rule, -Head)
Get the head for a retract call.
 1501head_of(Var, _) :-
 1502    var(Var), !, fail.
 1503head_of((Head :- _), Head).
 1504head_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.
 1512xref_hook(Hook) :-
 1513    prolog:hook(Hook).
 1514xref_hook(Hook) :-
 1515    hook(Hook).
 1516
 1517
 1518hook(attr_portray_hook(_,_)).
 1519hook(attr_unify_hook(_,_)).
 1520hook(attribute_goals(_,_,_)).
 1521hook(goal_expansion(_,_)).
 1522hook(term_expansion(_,_)).
 1523hook(goal_expansion(_,_,_,_)).
 1524hook(term_expansion(_,_,_,_)).
 1525hook(resource(_,_,_)).
 1526hook('$pred_option'(_,_,_,_)).
 1527hook('$nowarn_autoload'(_,_)).
 1528
 1529hook(emacs_prolog_colours:goal_classification(_,_)).
 1530hook(emacs_prolog_colours:goal_colours(_,_)).
 1531hook(emacs_prolog_colours:identify(_,_)).
 1532hook(emacs_prolog_colours:style(_,_)).
 1533hook(emacs_prolog_colours:term_colours(_,_)).
 1534hook(pce_principal:get_implementation(_,_,_,_)).
 1535hook(pce_principal:pce_class(_,_,_,_,_,_)).
 1536hook(pce_principal:pce_lazy_get_method(_,_,_)).
 1537hook(pce_principal:pce_lazy_send_method(_,_,_)).
 1538hook(pce_principal:pce_uses_template(_,_)).
 1539hook(pce_principal:send_implementation(_,_,_)).
 1540hook(predicate_options:option_decl(_,_,_)).
 1541hook(prolog:debug_control_hook(_)).
 1542hook(prolog:error_message(_,_,_)).
 1543hook(prolog:expand_answer(_,_,_)).
 1544hook(prolog:general_exception(_,_)).
 1545hook(prolog:help_hook(_)).
 1546hook(prolog:locate_clauses(_,_)).
 1547hook(prolog:message(_,_,_)).
 1548hook(prolog:message_context(_,_,_)).
 1549hook(prolog:message_line_element(_,_)).
 1550hook(prolog:message_location(_,_,_)).
 1551hook(prolog:predicate_summary(_,_)).
 1552hook(prolog:prolog_exception_hook(_,_,_,_,_)).
 1553hook(prolog:residual_goals(_,_)).
 1554hook(prolog_edit:load).
 1555hook(prolog_edit:locate(_,_,_)).
 1556hook(sandbox:safe_directive(_)).
 1557hook(sandbox:safe_global_variable(_)).
 1558hook(sandbox:safe_meta(_,_)).
 1559hook(sandbox:safe_meta_predicate(_)).
 1560hook(sandbox:safe_primitive(_)).
 1561hook(sandbox:safe_prolog_flag(_,_)).
 1562hook(shlib:unload_all_foreign_libraries).
 1563hook(system:'$foreign_registered'(_, _)).
 1564hook(user:exception(_,_,_)).
 1565hook(user:expand_answer(_,_)).
 1566hook(user:expand_query(_,_,_,_)).
 1567hook(user:file_search_path(_,_)).
 1568hook(user:library_directory(_)).
 1569hook(user:message_hook(_,_,_)).
 1570hook(prolog:message_action(_,_)).
 1571hook(user:portray(_)).
 1572hook(user:prolog_clause_name(_,_)).
 1573hook(user:prolog_list_goal(_)).
 1574hook(user:prolog_predicate_name(_,_)).
 1575hook(user:prolog_trace_interception(_,_,_,_)).
 arith_callable(+Spec, -Callable)
Translate argument of arithmetic_function/1 into a callable term
 1581arith_callable(Var, _) :-
 1582    var(Var), !, fail.
 1583arith_callable(Module:Spec, Module:Goal) :-
 1584    !,
 1585    arith_callable(Spec, Goal).
 1586arith_callable(Name/Arity, Goal) :-
 1587    PredArity is Arity + 1,
 1588    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.

 1599process_body(Body, Origin, Src) :-
 1600    forall(limit(100, process_goal(Body, Origin, Src, _Partial)),
 1601           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.
 1608process_goal(Var, _, _, _) :-
 1609    var(Var),
 1610    !.
 1611process_goal(_:Goal, _, _, _) :-
 1612    var(Goal),
 1613    !.
 1614process_goal(Goal, Origin, Src, P) :-
 1615    Goal = (_,_),                               % problems
 1616    !,
 1617    phrase(conjunction(Goal), Goals),
 1618    process_conjunction(Goals, Origin, Src, P).
 1619process_goal(Goal, Origin, Src, _) :-           % Final disjunction, no
 1620    Goal = (_;_),                               % problems
 1621    !,
 1622    phrase(disjunction(Goal), Goals),
 1623    forall(member(G, Goals),
 1624           process_body(G, Origin, Src)).
 1625process_goal(Goal, Origin, Src, P) :-
 1626    (   (   xmodule(M, Src)
 1627        ->  true
 1628        ;   M = user
 1629        ),
 1630        pi_head(PI, M:Goal),
 1631        (   current_predicate(PI),
 1632            predicate_property(M:Goal, imported_from(IM))
 1633        ->  true
 1634        ;   PI = M:Name/Arity,
 1635            '$find_library'(M, Name, Arity, IM, _Library)
 1636        ->  true
 1637        ;   IM = M
 1638        ),
 1639        prolog:called_by(Goal, IM, M, Called)
 1640    ;   prolog:called_by(Goal, Called)
 1641    ),
 1642    !,
 1643    must_be(list, Called),
 1644    current_source_line(Here),
 1645    assert_called(Src, Origin, Goal, Here),
 1646    process_called_list(Called, Origin, Src, P).
 1647process_goal(Goal, Origin, Src, _) :-
 1648    process_xpce_goal(Goal, Origin, Src),
 1649    !.
 1650process_goal(load_foreign_library(File), _Origin, Src, _) :-
 1651    process_foreign(File, Src).
 1652process_goal(load_foreign_library(File, _Init), _Origin, Src, _) :-
 1653    process_foreign(File, Src).
 1654process_goal(use_foreign_library(File), _Origin, Src, _) :-
 1655    process_foreign(File, Src).
 1656process_goal(use_foreign_library(File, _Init), _Origin, Src, _) :-
 1657    process_foreign(File, Src).
 1658process_goal(Goal, Origin, Src, P) :-
 1659    xref_meta_src(Goal, Metas, Src),
 1660    !,
 1661    current_source_line(Here),
 1662    assert_called(Src, Origin, Goal, Here),
 1663    process_called_list(Metas, Origin, Src, P).
 1664process_goal(Goal, Origin, Src, _) :-
 1665    asserting_goal(Goal, Rule),
 1666    !,
 1667    current_source_line(Here),
 1668    assert_called(Src, Origin, Goal, Here),
 1669    process_assert(Rule, Origin, Src).
 1670process_goal(Goal, Origin, Src, P) :-
 1671    partial_evaluate(Goal, P),
 1672    current_source_line(Here),
 1673    assert_called(Src, Origin, Goal, Here).
 1674
 1675disjunction(Var)   --> {var(Var), !}, [Var].
 1676disjunction((A;B)) --> !, disjunction(A), disjunction(B).
 1677disjunction(G)     --> [G].
 1678
 1679conjunction(Var)   --> {var(Var), !}, [Var].
 1680conjunction((A,B)) --> !, conjunction(A), conjunction(B).
 1681conjunction(G)     --> [G].
 1682
 1683shares_vars(RVars, T) :-
 1684    term_variables(T, TVars0),
 1685    sort(TVars0, TVars),
 1686    ord_intersect(RVars, TVars).
 1687
 1688process_conjunction([], _, _, _).
 1689process_conjunction([Disj|Rest], Origin, Src, P) :-
 1690    nonvar(Disj),
 1691    Disj = (_;_),
 1692    Rest \== [],
 1693    !,
 1694    phrase(disjunction(Disj), Goals),
 1695    term_variables(Rest, RVars0),
 1696    sort(RVars0, RVars),
 1697    partition(shares_vars(RVars), Goals, Sharing, NonSHaring),
 1698    forall(member(G, NonSHaring),
 1699           process_body(G, Origin, Src)),
 1700    (   Sharing == []
 1701    ->  true
 1702    ;   maplist(term_variables, Sharing, GVars0),
 1703        append(GVars0, GVars1),
 1704        sort(GVars1, GVars),
 1705        ord_intersection(GVars, RVars, SVars),
 1706        VT =.. [v|SVars],
 1707        findall(VT,
 1708                (   member(G, Sharing),
 1709                    process_goal(G, Origin, Src, PS),
 1710                    PS == true
 1711                ),
 1712                Alts0),
 1713        (   Alts0 == []
 1714        ->  true
 1715        ;   (   true
 1716            ;   P = true,
 1717                sort(Alts0, Alts1),
 1718                variants(Alts1, 10, Alts),
 1719                member(VT, Alts)
 1720            )
 1721        )
 1722    ),
 1723    process_conjunction(Rest, Origin, Src, P).
 1724process_conjunction([H|T], Origin, Src, P) :-
 1725    process_goal(H, Origin, Src, P),
 1726    process_conjunction(T, Origin, Src, P).
 1727
 1728
 1729process_called_list([], _, _, _).
 1730process_called_list([H|T], Origin, Src, P) :-
 1731    process_meta(H, Origin, Src, P),
 1732    process_called_list(T, Origin, Src, P).
 1733
 1734process_meta(A+N, Origin, Src, P) :-
 1735    !,
 1736    (   extend(A, N, AX)
 1737    ->  process_goal(AX, Origin, Src, P)
 1738    ;   true
 1739    ).
 1740process_meta(//(A), Origin, Src, P) :-
 1741    !,
 1742    process_dcg_goal(A, Origin, Src, P).
 1743process_meta(G, Origin, Src, P) :-
 1744    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.
 1751process_dcg_goal(Var, _, _, _) :-
 1752    var(Var),
 1753    !.
 1754process_dcg_goal((A,B), Origin, Src, P) :-
 1755    !,
 1756    process_dcg_goal(A, Origin, Src, P),
 1757    process_dcg_goal(B, Origin, Src, P).
 1758process_dcg_goal((A;B), Origin, Src, P) :-
 1759    !,
 1760    process_dcg_goal(A, Origin, Src, P),
 1761    process_dcg_goal(B, Origin, Src, P).
 1762process_dcg_goal((A|B), Origin, Src, P) :-
 1763    !,
 1764    process_dcg_goal(A, Origin, Src, P),
 1765    process_dcg_goal(B, Origin, Src, P).
 1766process_dcg_goal((A->B), Origin, Src, P) :-
 1767    !,
 1768    process_dcg_goal(A, Origin, Src, P),
 1769    process_dcg_goal(B, Origin, Src, P).
 1770process_dcg_goal((A*->B), Origin, Src, P) :-
 1771    !,
 1772    process_dcg_goal(A, Origin, Src, P),
 1773    process_dcg_goal(B, Origin, Src, P).
 1774process_dcg_goal({Goal}, Origin, Src, P) :-
 1775    !,
 1776    process_goal(Goal, Origin, Src, P).
 1777process_dcg_goal(List, _Origin, _Src, _) :-
 1778    is_list(List),
 1779    !.               % terminal
 1780process_dcg_goal(List, _Origin, _Src, _) :-
 1781    string(List),
 1782    !.                % terminal
 1783process_dcg_goal(Callable, Origin, Src, P) :-
 1784    extend(Callable, 2, Goal),
 1785    !,
 1786    process_goal(Goal, Origin, Src, P).
 1787process_dcg_goal(_, _, _, _).
 1788
 1789
 1790extend(Var, _, _) :-
 1791    var(Var), !, fail.
 1792extend(M:G, N, M:GX) :-
 1793    !,
 1794    callable(G),
 1795    extend(G, N, GX).
 1796extend(G, N, GX) :-
 1797    (   N =:= 0
 1798    ->  GX = G
 1799    ;   compound(G)
 1800    ->  compound_name_arguments(G, Name, Args),
 1801        length(Rest, N),
 1802        append(Args, Rest, NArgs),
 1803        compound_name_arguments(GX, Name, NArgs)
 1804    ;   atom(G)
 1805    ->  length(NArgs, N),
 1806        compound_name_arguments(GX, G, NArgs)
 1807    ).
 1808
 1809asserting_goal(assert(Rule), Rule).
 1810asserting_goal(asserta(Rule), Rule).
 1811asserting_goal(assertz(Rule), Rule).
 1812asserting_goal(assert(Rule,_), Rule).
 1813asserting_goal(asserta(Rule,_), Rule).
 1814asserting_goal(assertz(Rule,_), Rule).
 1815
 1816process_assert(0, _, _) :- !.           % catch variables
 1817process_assert((_:-Body), Origin, Src) :-
 1818    !,
 1819    process_body(Body, Origin, Src).
 1820process_assert(_, _, _).
 variants(+SortedList, +Max, -Variants) is det
 1824variants([], _, []).
 1825variants([H|T], Max, List) :-
 1826    variants(T, H, Max, List).
 1827
 1828variants([], H, _, [H]).
 1829variants(_, _, 0, []) :- !.
 1830variants([H|T], V, Max, List) :-
 1831    (   H =@= V
 1832    ->  variants(T, V, Max, List)
 1833    ;   List = [V|List2],
 1834        Max1 is Max-1,
 1835        variants(T, H, Max1, List2)
 1836    ).
 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?
 1850partial_evaluate(Goal, P) :-
 1851    eval(Goal),
 1852    !,
 1853    P = true.
 1854partial_evaluate(_, _).
 1855
 1856eval(X = Y) :-
 1857    unify_with_occurs_check(X, Y).
 1858
 1859		 /*******************************
 1860		 *        PLUNIT SUPPORT	*
 1861		 *******************************/
 1862
 1863enter_test_unit(Unit, _Src) :-
 1864    current_source_line(Line),
 1865    asserta(current_test_unit(Unit, Line)).
 1866
 1867leave_test_unit(Unit, _Src) :-
 1868    retractall(current_test_unit(Unit, _)).
 1869
 1870
 1871                 /*******************************
 1872                 *          XPCE STUFF          *
 1873                 *******************************/
 1874
 1875pce_goal(new(_,_), new(-, new)).
 1876pce_goal(send(_,_), send(arg, msg)).
 1877pce_goal(send_class(_,_,_), send_class(arg, arg, msg)).
 1878pce_goal(get(_,_,_), get(arg, msg, -)).
 1879pce_goal(get_class(_,_,_,_), get_class(arg, arg, msg, -)).
 1880pce_goal(get_chain(_,_,_), get_chain(arg, msg, -)).
 1881pce_goal(get_object(_,_,_), get_object(arg, msg, -)).
 1882
 1883process_xpce_goal(G, Origin, Src) :-
 1884    pce_goal(G, Process),
 1885    !,
 1886    current_source_line(Here),
 1887    assert_called(Src, Origin, G, Here),
 1888    (   arg(I, Process, How),
 1889        arg(I, G, Term),
 1890        process_xpce_arg(How, Term, Origin, Src),
 1891        fail
 1892    ;   true
 1893    ).
 1894
 1895process_xpce_arg(new, Term, Origin, Src) :-
 1896    callable(Term),
 1897    process_new(Term, Origin, Src).
 1898process_xpce_arg(arg, Term, Origin, Src) :-
 1899    compound(Term),
 1900    process_new(Term, Origin, Src).
 1901process_xpce_arg(msg, Term, Origin, Src) :-
 1902    compound(Term),
 1903    (   arg(_, Term, Arg),
 1904        process_xpce_arg(arg, Arg, Origin, Src),
 1905        fail
 1906    ;   true
 1907    ).
 1908
 1909process_new(_M:_Term, _, _) :- !.       % TBD: Calls on other modules!
 1910process_new(Term, Origin, Src) :-
 1911    assert_new(Src, Origin, Term),
 1912    (   compound(Term),
 1913        arg(_, Term, Arg),
 1914        process_xpce_arg(arg, Arg, Origin, Src),
 1915        fail
 1916    ;   true
 1917    ).
 1918
 1919assert_new(_, _, Term) :-
 1920    \+ callable(Term),
 1921    !.
 1922assert_new(Src, Origin, Control) :-
 1923    functor_name(Control, Class),
 1924    pce_control_class(Class),
 1925    !,
 1926    forall(arg(_, Control, Arg),
 1927           assert_new(Src, Origin, Arg)).
 1928assert_new(Src, Origin, Term) :-
 1929    compound(Term),
 1930    arg(1, Term, Prolog),
 1931    Prolog == @(prolog),
 1932    (   Term =.. [message, _, Selector | T],
 1933        atom(Selector)
 1934    ->  Called =.. [Selector|T],
 1935        process_body(Called, Origin, Src)
 1936    ;   Term =.. [?, _, Selector | T],
 1937        atom(Selector)
 1938    ->  append(T, [_R], T2),
 1939        Called =.. [Selector|T2],
 1940        process_body(Called, Origin, Src)
 1941    ),
 1942    fail.
 1943assert_new(_, _, @(_)) :- !.
 1944assert_new(Src, _, Term) :-
 1945    functor_name(Term, Name),
 1946    assert_used_class(Src, Name).
 1947
 1948
 1949pce_control_class(and).
 1950pce_control_class(or).
 1951pce_control_class(if).
 1952pce_control_class(not).
 1953
 1954
 1955                /********************************
 1956                *       INCLUDED MODULES        *
 1957                ********************************/
 process_use_module(+Modules, +Src, +Rexport) is det
 1961process_use_module(_Module:_Files, _, _) :- !.  % loaded in another module
 1962process_use_module([], _, _) :- !.
 1963process_use_module([H|T], Src, Reexport) :-
 1964    !,
 1965    process_use_module(H, Src, Reexport),
 1966    process_use_module(T, Src, Reexport).
 1967process_use_module(library(pce), Src, Reexport) :-     % bit special
 1968    !,
 1969    xref_public_list(library(pce), Path, Exports, Src),
 1970    forall(member(Import, Exports),
 1971           process_pce_import(Import, Src, Path, Reexport)).
 1972process_use_module(File, Src, Reexport) :-
 1973    load_module_if_needed(File),
 1974    (   xoption(Src, silent(Silent))
 1975    ->  Extra = [silent(Silent)]
 1976    ;   Extra = [silent(true)]
 1977    ),
 1978    (   xref_public_list(File, Src,
 1979                         [ path(Path),
 1980                           module(M),
 1981                           exports(Exports),
 1982                           public(Public),
 1983                           meta(Meta)
 1984                         | Extra
 1985                         ])
 1986    ->  assert(uses_file(File, Src, Path)),
 1987        assert_import(Src, Exports, _, Path, Reexport),
 1988        assert_xmodule_callable(Exports, M, Src, Path),
 1989        assert_xmodule_callable(Public, M, Src, Path),
 1990        maplist(process_meta_head(Src), Meta),
 1991        (   File = library(chr)     % hacky
 1992        ->  assert(mode(chr, Src))
 1993        ;   true
 1994        )
 1995    ;   assert(uses_file(File, Src, '<not_found>'))
 1996    ).
 1997
 1998process_pce_import(Name/Arity, Src, Path, Reexport) :-
 1999    atom(Name),
 2000    integer(Arity),
 2001    !,
 2002    functor(Term, Name, Arity),
 2003    (   \+ system_predicate(Term),
 2004        \+ Term = pce_error(_)      % hack!?
 2005    ->  assert_import(Src, [Name/Arity], _, Path, Reexport)
 2006    ;   true
 2007    ).
 2008process_pce_import(op(P,T,N), Src, _, _) :-
 2009    xref_push_op(Src, P, T, N).
 process_use_module2(+File, +Import, +Src, +Reexport) is det
Process use_module/2 and reexport/2.
 2015process_use_module2(File, Import, Src, Reexport) :-
 2016    load_module_if_needed(File),
 2017    (   catch(xref_public_list(File, Src,
 2018                               [ path(Path),
 2019                                 exports(Export),
 2020                                 meta(Meta)
 2021                               ]),
 2022              error(_,_),
 2023              fail)
 2024    ->  assertz(uses_file(File, Src, Path)),
 2025        assert_import(Src, Import, Export, Path, Reexport),
 2026        forall((  member(Head, Meta),
 2027                  imported(Head, _, Path)
 2028               ),
 2029               process_meta_head(Src, Head))
 2030    ;   assertz(uses_file(File, Src, '<not_found>'))
 2031    ).
 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.
 2040load_module_if_needed(File) :-
 2041    prolog:no_autoload_module(File),
 2042    !,
 2043    use_module(File, []).
 2044load_module_if_needed(_).
 2045
 2046prolog:no_autoload_module(library(apply_macros)).
 2047prolog:no_autoload_module(library(arithmetic)).
 2048prolog:no_autoload_module(library(record)).
 2049prolog:no_autoload_module(library(persistency)).
 2050prolog:no_autoload_module(library(pldoc)).
 2051prolog:no_autoload_module(library(settings)).
 2052prolog:no_autoload_module(library(debug)).
 2053prolog:no_autoload_module(library(plunit)).
 2054prolog:no_autoload_module(library(macros)).
 2055prolog:no_autoload_module(library(yall)).
 process_requires(+Import, +Src)
 2060process_requires(Import, Src) :-
 2061    is_list(Import),
 2062    !,
 2063    require_list(Import, Src).
 2064process_requires(Var, _Src) :-
 2065    var(Var),
 2066    !.
 2067process_requires((A,B), Src) :-
 2068    !,
 2069    process_requires(A, Src),
 2070    process_requires(B, Src).
 2071process_requires(PI, Src) :-
 2072    requires(PI, Src).
 2073
 2074require_list([], _).
 2075require_list([H|T], Src) :-
 2076    requires(H, Src),
 2077    require_list(T, Src).
 2078
 2079requires(PI, _Src) :-
 2080    '$pi_head'(PI, Head),
 2081    '$get_predicate_attribute'(system:Head, defined, 1),
 2082    !.
 2083requires(PI, Src) :-
 2084    '$pi_head'(PI, Head),
 2085    '$pi_head'(Name/Arity, Head),
 2086    '$find_library'(_Module, Name, Arity, _LoadModule, Library),
 2087    (   imported(Head, Src, Library)
 2088    ->  true
 2089    ;   assertz(imported(Head, Src, Library))
 2090    ).
 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.
 2126xref_public_list(File, Src, Options) :-
 2127    option(path(Source), Options, _),
 2128    option(module(Module), Options, _),
 2129    option(exports(Exports), Options, _),
 2130    option(public(Public), Options, _),
 2131    option(meta(Meta), Options, _),
 2132    xref_source_file(File, Path, Src, Options),
 2133    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.
 2157xref_public_list(File, Source, Export, Src) :-
 2158    xref_source_file(File, Path, Src),
 2159    public_list(Path, Source, _, _, Export, _, []).
 2160xref_public_list(File, Source, Module, Export, Meta, Src) :-
 2161    xref_source_file(File, Path, Src),
 2162    public_list(Path, Source, Module, Meta, Export, _, []).
 2163xref_public_list(File, Source, Module, Export, Public, Meta, Src) :-
 2164    xref_source_file(File, Path, Src),
 2165    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.
 2176:- dynamic  public_list_cache/7. 2177:- volatile public_list_cache/7. 2178
 2179public_list(Path, Source, Module, Meta, Export, Public, _Options) :-
 2180    \+ is_stream(Path),
 2181    public_list_cache(Path, Source, Modified,
 2182                      Module0, Meta0, Export0, Public0),
 2183    time_file(Path, ModifiedNow),
 2184    (   abs(Modified-ModifiedNow) < 0.0001
 2185    ->  !,
 2186        t(Module,Meta,Export,Public) = t(Module0,Meta0,Export0,Public0)
 2187    ;   retractall(public_list_cache(Path, _, _, _, _, _, _)),
 2188        fail
 2189    ).
 2190public_list(Path, Source, Module, Meta, Export, Public, Options) :-
 2191    public_list_nc(Path, Source, Module0, Meta0, Export0, Public0, Options),
 2192    (   Error = error(_,_),
 2193        catch(time_file(Path, Modified), Error, fail)
 2194    ->  asserta(public_list_cache(Path, Source, Modified,
 2195                                  Module0, Meta0, Export0, Public0))
 2196    ;   true
 2197    ),
 2198    t(Module,Meta,Export,Public) = t(Module0,Meta0,Export0,Public0).
 2199
 2200public_list_nc(Path, Source, Module, Meta, Export, Public, _Options) :-
 2201    \+ is_stream(Path),
 2202    public_list_from_index(Path, Module, Meta, Export, Public),
 2203    !,
 2204    qlf_pl_file(Path, Source).
 2205public_list_nc(Path, Source, Module, [], Export, [], _Options) :-
 2206    \+ is_stream(Path),
 2207    is_qlf_file(Path),
 2208    !,
 2209    '$qlf_module'(Path, Info),
 2210    _{module:Module, exports:Export, file:Source} :< Info.
 2211public_list_nc(Path, Path, Module, Meta, Export, Public, Options) :-
 2212    (   is_stream(Path)
 2213    ;   exists_file(Path)
 2214    ),
 2215    !,
 2216    prolog_file_directives(Path, Directives, Options),
 2217    public_list(Directives, Path, Module, Meta, [], Export, [], Public, []).
 2218public_list_nc(Path, Path, Module, [], Export, [], _Options) :-
 2219    \+ is_stream(Path),
 2220    qlf_pl_file(QlfFile, Path),
 2221    '$qlf_module'(QlfFile, Info),
 2222    _{module:Module, exports:Export} :< Info.
 2223
 2224public_list([(:- module(Module, Export0))|Decls], Path,
 2225            Module, Meta, MT, Export, Rest, Public, PT) :-
 2226    !,
 2227    (   is_list(Export0)
 2228    ->  append(Export0, Reexport, Export)
 2229    ;   Reexport = Export
 2230    ),
 2231    public_list_(Decls, Path, Meta, MT, Reexport, Rest, Public, PT).
 2232public_list([(:- encoding(_))|Decls], Path,
 2233            Module, Meta, MT, Export, Rest, Public, PT) :-
 2234    public_list(Decls, Path, Module, Meta, MT, Export, Rest, Public, PT).
 2235
 2236public_list_([], _, Meta, Meta, Export, Export, Public, Public).
 2237public_list_([(:-(Dir))|T], Path, Meta, MT, Export, Rest, Public, PT) :-
 2238    public_list_1(Dir, Path, Meta, MT0, Export, Rest0, Public, PT0),
 2239    !,
 2240    public_list_(T, Path, MT0, MT, Rest0, Rest, PT0, PT).
 2241public_list_([_|T], Path, Meta, MT, Export, Rest, Public, PT) :-
 2242    public_list_(T, Path, Meta, MT, Export, Rest, Public, PT).
 2243
 2244public_list_1(reexport(Spec), Path, Meta, MT, Reexport, Rest, Public, PT) :-
 2245    reexport_files(Spec, Path, Meta, MT, Reexport, Rest, Public, PT).
 2246public_list_1(reexport(Spec, Import), Path, Meta, Meta, Reexport, Rest, Public, Public) :-
 2247    public_from_import(Import, Spec, Path, Reexport, Rest).
 2248public_list_1(meta_predicate(Decl), _Path, Meta, MT, Export, Export, Public, Public) :-
 2249    phrase(meta_decls(Decl), Meta, MT).
 2250public_list_1(public(Decl), _Path, Meta, Meta, Export, Export, Public, PT) :-
 2251    phrase(public_decls(Decl), Public, PT).
 reexport_files(+Files, +Src, -Meta, ?MetaTail, -Exports, ?ExportsTail, -Public, ?PublicTail)
 2257reexport_files([], _, Meta, Meta, Export, Export, Public, Public) :- !.
 2258reexport_files([H|T], Src, Meta, MT, Export, ET, Public, PT) :-
 2259    !,
 2260    xref_source_file(H, Path, Src),
 2261    public_list(Path, _Source, _Module, Meta0, Export0, Public0, []),
 2262    append(Meta0, MT1, Meta),
 2263    append(Export0, ET1, Export),
 2264    append(Public0, PT1, Public),
 2265    reexport_files(T, Src, MT1, MT, ET1, ET, PT1, PT).
 2266reexport_files(Spec, Src, Meta, MT, Export, ET, Public, PT) :-
 2267    xref_source_file(Spec, Path, Src),
 2268    public_list(Path, _Source, _Module, Meta0, Export0, Public0, []),
 2269    append(Meta0, MT, Meta),
 2270    append(Export0, ET, Export),
 2271    append(Public0, PT, Public).
 2272
 2273public_from_import(except(Map), Path, Src, Export, Rest) :-
 2274    !,
 2275    xref_public_list(Path, _, AllExports, Src),
 2276    except(Map, AllExports, NewExports),
 2277    append(NewExports, Rest, Export).
 2278public_from_import(Import, _, _, Export, Rest) :-
 2279    import_name_map(Import, Export, Rest).
 except(+Remove, +AllExports, -Exports)
 2284except([], Exports, Exports).
 2285except([PI0 as NewName|Map], Exports0, Exports) :-
 2286    !,
 2287    canonical_pi(PI0, PI),
 2288    map_as(Exports0, PI, NewName, Exports1),
 2289    except(Map, Exports1, Exports).
 2290except([PI0|Map], Exports0, Exports) :-
 2291    canonical_pi(PI0, PI),
 2292    select(PI2, Exports0, Exports1),
 2293    same_pi(PI, PI2),
 2294    !,
 2295    except(Map, Exports1, Exports).
 2296
 2297
 2298map_as([PI|T], Repl, As, [PI2|T])  :-
 2299    same_pi(Repl, PI),
 2300    !,
 2301    pi_as(PI, As, PI2).
 2302map_as([H|T0], Repl, As, [H|T])  :-
 2303    map_as(T0, Repl, As, T).
 2304
 2305pi_as(_/Arity, Name, Name/Arity).
 2306pi_as(_//Arity, Name, Name//Arity).
 2307
 2308import_name_map([], L, L).
 2309import_name_map([_/Arity as NewName|T0], [NewName/Arity|T], Tail) :-
 2310    !,
 2311    import_name_map(T0, T, Tail).
 2312import_name_map([_//Arity as NewName|T0], [NewName//Arity|T], Tail) :-
 2313    !,
 2314    import_name_map(T0, T, Tail).
 2315import_name_map([H|T0], [H|T], Tail) :-
 2316    import_name_map(T0, T, Tail).
 2317
 2318canonical_pi(Name//Arity0, PI) :-
 2319    integer(Arity0),
 2320    !,
 2321    PI = Name/Arity,
 2322    Arity is Arity0 + 2.
 2323canonical_pi(PI, PI).
 2324
 2325same_pi(Canonical, PI2) :-
 2326    canonical_pi(PI2, Canonical).
 2327
 2328meta_decls(Var) -->
 2329    { var(Var) },
 2330    !.
 2331meta_decls((A,B)) -->
 2332    !,
 2333    meta_decls(A),
 2334    meta_decls(B).
 2335meta_decls(A) -->
 2336    [A].
 2337
 2338public_decls(Var) -->
 2339    { var(Var) },
 2340    !.
 2341public_decls((A,B)) -->
 2342    !,
 2343    public_decls(A),
 2344    public_decls(B).
 2345public_decls(A) -->
 2346    [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.
 2353public_list_from_index(Path, Module, Meta, Export, Public) :-
 2354    file_name_extension(BasePath, _Ext, Path),
 2355    file_directory_name(BasePath, Dir),
 2356    atom_concat(Dir, '/INDEX.pl', IndexFile),
 2357    exists_file(IndexFile),
 2358    file_base_name(BasePath, Base),
 2359    setup_call_cleanup(
 2360        '$push_input_context'(autoload_index),
 2361        setup_call_cleanup(
 2362            open(IndexFile, read, In),
 2363            index_public_list(In, Base, Module, Meta, Export, Public),
 2364            close(In)),
 2365        '$pop_input_context').
 2366
 2367index_public_list(In, Base, Module, Meta, Export, Public) :-
 2368    read_term(In, Term, []),
 2369    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2370
 2371index_public_list(end_of_file, _In, _Base, _Module, [], [], []).
 2372index_public_list(index(op:Op, Module, Base), In, Base, Module, Meta, [Op|Export], Public) :-
 2373    !,
 2374    read_term(In, Term, []),
 2375    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2376index_public_list(index((public):Head, Module, Base), In, Base, Module, Meta, Export, [PI|Public]) :-
 2377    !,
 2378    pi_head(PI, Head),
 2379    read_term(In, Term, []),
 2380    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2381index_public_list(index(Head, Module, Base), In, Base, Module, Meta, [PI|Export], Public) :-
 2382    !,
 2383    pi_head(PI, Head),
 2384    (   meta_mode(Head)
 2385    ->  Meta = [Head|MetaT]
 2386    ;   Meta = MetaT
 2387    ),
 2388    read_term(In, Term, []),
 2389    index_public_list(Term, In, Base, Module, MetaT, Export, Public).
 2390index_public_list(index(Name, Arity, Module, Base), In, Base, Module, Meta, [Name/Arity|Export], Public) :-
 2391    !,
 2392    read_term(In, Term, []),
 2393    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2394index_public_list(_, In, Base, Module, Meta, Export, Public) :-
 2395    read_term(In, Term, []),
 2396    index_public_list(Term, In, Base, Module, Meta, Export, Public).
 2397
 2398meta_mode(H) :-
 2399    compound(H),
 2400    arg(_, H, A),
 2401    meta_arg(A),
 2402    !.
 2403
 2404meta_arg(I) :-
 2405    integer(I),
 2406    !.
 2407meta_arg(:).
 2408meta_arg(^).
 2409meta_arg(//).
 2410
 2411                 /*******************************
 2412                 *             INCLUDE          *
 2413                 *******************************/
 2414
 2415process_include([], _) :- !.
 2416process_include([H|T], Src) :-
 2417    !,
 2418    process_include(H, Src),
 2419    process_include(T, Src).
 2420process_include(File, Src) :-
 2421    callable(File),
 2422    !,
 2423    (   once(xref_input(ParentSrc, _)),
 2424        xref_source_file(File, Path, ParentSrc)
 2425    ->  (   (   uses_file(_, Src, Path)
 2426            ;   Path == Src
 2427            )
 2428        ->  true
 2429        ;   assert(uses_file(File, Src, Path)),
 2430            (   xoption(Src, process_include(true))
 2431            ->  findall(O, xoption(Src, O), Options),
 2432                setup_call_cleanup(
 2433                    open_include_file(Path, In, Refs),
 2434                    collect(Src, Path, In, Options),
 2435                    close_include(In, Refs))
 2436            ;   true
 2437            )
 2438        )
 2439    ;   assert(uses_file(File, Src, '<not_found>'))
 2440    ).
 2441process_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.
 2449open_include_file(Path, In, [Ref]) :-
 2450    once(xref_input(_, Parent)),
 2451    stream_property(Parent, encoding(Enc)),
 2452    '$push_input_context'(xref_include),
 2453    catch((   prolog:xref_open_source(Path, In)
 2454          ->  catch(set_stream(In, encoding(Enc)),
 2455                    error(_,_), true)       % deal with non-file input
 2456          ;   include_encoding(Enc, Options),
 2457              open(Path, read, In, Options)
 2458          ), E,
 2459          ( '$pop_input_context', throw(E))),
 2460    catch((   peek_char(In, #)              % Deal with #! script
 2461          ->  skip(In, 10)
 2462          ;   true
 2463          ), E,
 2464          ( close_include(In, []), throw(E))),
 2465    asserta(xref_input(Path, In), Ref).
 2466
 2467include_encoding(wchar_t, []) :- !.
 2468include_encoding(Enc, [encoding(Enc)]).
 2469
 2470
 2471close_include(In, Refs) :-
 2472    maplist(erase, Refs),
 2473    close(In, [force(true)]),
 2474    '$pop_input_context'.
 process_foreign(+Spec, +Src)
Process a load_foreign_library/1 call.
 2480process_foreign(Spec, Src) :-
 2481    ground(Spec),
 2482    current_foreign_library(Spec, Defined),
 2483    !,
 2484    (   xmodule(Module, Src)
 2485    ->  true
 2486    ;   Module = user
 2487    ),
 2488    process_foreign_defined(Defined, Module, Src).
 2489process_foreign(_, _).
 2490
 2491process_foreign_defined([], _, _).
 2492process_foreign_defined([H|T], M, Src) :-
 2493    (   H = M:Head
 2494    ->  assert_foreign(Src, Head)
 2495    ;   assert_foreign(Src, H)
 2496    ),
 2497    process_foreign_defined(T, M, Src).
 2498
 2499
 2500                 /*******************************
 2501                 *          CHR SUPPORT         *
 2502                 *******************************/
 2503
 2504/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 2505This part of the file supports CHR. Our choice is between making special
 2506hooks to make CHR expansion work and  then handle the (complex) expanded
 2507code or process the  CHR  source   directly.  The  latter looks simpler,
 2508though I don't like the idea  of   adding  support for libraries to this
 2509module.  A  file  is  supposed  to  be  a    CHR   file  if  it  uses  a
 2510use_module(library(chr) or contains a :-   constraint/1 directive. As an
 2511extra bonus we get the source-locations right :-)
 2512- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 2513
 2514process_chr(@(_Name, Rule), Src) :-
 2515    mode(chr, Src),
 2516    process_chr(Rule, Src).
 2517process_chr(pragma(Rule, _Pragma), Src) :-
 2518    mode(chr, Src),
 2519    process_chr(Rule, Src).
 2520process_chr(<=>(Head, Body), Src) :-
 2521    mode(chr, Src),
 2522    chr_head(Head, Src, H),
 2523    chr_body(Body, H, Src).
 2524process_chr(==>(Head, Body), Src) :-
 2525    mode(chr, Src),
 2526    chr_head(Head, H, Src),
 2527    chr_body(Body, H, Src).
 2528process_chr((:- chr_constraint(Decls)), Src) :-
 2529    (   mode(chr, Src)
 2530    ->  true
 2531    ;   assert(mode(chr, Src))
 2532    ),
 2533    chr_decls(Decls, Src).
 2534
 2535chr_decls((A,B), Src) =>
 2536    chr_decls(A, Src),
 2537    chr_decls(B, Src).
 2538chr_decls(Head, Src) =>
 2539    generalise_term(Head, Gen),
 2540    (   declared(Gen, chr_constraint, Src, _)
 2541    ->  true
 2542    ;   current_source_line(Line),
 2543        assertz(declared(Gen, chr_constraint, Src, Line))
 2544    ).
 2545
 2546chr_head(X, _, _) :-
 2547    var(X),
 2548    !.                      % Illegal.  Warn?
 2549chr_head(\(A,B), Src, H) :-
 2550    chr_head(A, Src, H),
 2551    process_body(B, H, Src).
 2552chr_head((H0,B), Src, H) :-
 2553    chr_defined(H0, Src, H),
 2554    process_body(B, H, Src).
 2555chr_head(H0, Src, H) :-
 2556    chr_defined(H0, Src, H).
 2557
 2558chr_defined(X, _, _) :-
 2559    var(X),
 2560    !.
 2561chr_defined(#(C,_Id), Src, C) :-
 2562    !,
 2563    assert_constraint(Src, C).
 2564chr_defined(A, Src, A) :-
 2565    assert_constraint(Src, A).
 2566
 2567chr_body(X, From, Src) :-
 2568    var(X),
 2569    !,
 2570    process_body(X, From, Src).
 2571chr_body('|'(Guard, Goals), H, Src) :-
 2572    !,
 2573    chr_body(Guard, H, Src),
 2574    chr_body(Goals, H, Src).
 2575chr_body(G, From, Src) :-
 2576    process_body(G, From, Src).
 2577
 2578assert_constraint(_, Head) :-
 2579    var(Head),
 2580    !.
 2581assert_constraint(Src, Head) :-
 2582    constraint(Head, Src, _),
 2583    !.
 2584assert_constraint(Src, Head) :-
 2585    generalise_term(Head, Term),
 2586    current_source_line(Line),
 2587    assert(constraint(Term, Src, Line)).
 2588
 2589
 2590                /********************************
 2591                *       PHASE 1 ASSERTIONS      *
 2592                ********************************/
 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.
 2599assert_called(_, _, Var, _) :-
 2600    var(Var),
 2601    !.
 2602assert_called(Src, From, Goal, Line) :-
 2603    var(From),
 2604    !,
 2605    assert_called(Src, '<unknown>', Goal, Line).
 2606assert_called(_, _, Goal, _) :-
 2607    expand_hide_called(Goal),
 2608    !.
 2609assert_called(Src, Origin, M:G, Line) :-
 2610    !,
 2611    (   atom(M),
 2612        callable(G)
 2613    ->  current_condition(Cond),
 2614        (   xmodule(M, Src)         % explicit call to own module
 2615        ->  assert_called(Src, Origin, G, Line)
 2616        ;   called(M:G, Src, Origin, Cond, Line) % already registered
 2617        ->  true
 2618        ;   hide_called(M:G, Src)           % not interesting (now)
 2619        ->  true
 2620        ;   generalise(Origin, OTerm),
 2621            generalise(G, GTerm)
 2622        ->  assert(called(M:GTerm, Src, OTerm, Cond, Line))
 2623        ;   true
 2624        )
 2625    ;   true                        % call to variable module
 2626    ).
 2627assert_called(Src, _, Goal, _) :-
 2628    (   xmodule(M, Src)
 2629    ->  M \== system
 2630    ;   M = user
 2631    ),
 2632    hide_called(M:Goal, Src),
 2633    !.
 2634assert_called(Src, Origin, Goal, Line) :-
 2635    current_condition(Cond),
 2636    (   called(Goal, Src, Origin, Cond, Line)
 2637    ->  true
 2638    ;   generalise(Origin, OTerm),
 2639        generalise(Goal, Term)
 2640    ->  assert(called(Term, Src, OTerm, Cond, Line))
 2641    ;   true
 2642    ).
 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.
 2650expand_hide_called(pce_principal:send_implementation(_, _, _)).
 2651expand_hide_called(pce_principal:get_implementation(_, _, _, _)).
 2652expand_hide_called(pce_principal:pce_lazy_get_method(_,_,_)).
 2653expand_hide_called(pce_principal:pce_lazy_send_method(_,_,_)).
 2654
 2655assert_defined(Src, Goal) :-
 2656    Goal = test(_Test),
 2657    current_test_unit(Unit, Line),
 2658    assert_called(Src, '<test_unit>'(Unit), Goal, Line),
 2659    fail.
 2660assert_defined(Src, Goal) :-
 2661    Goal = test(_Test, _Options),
 2662    current_test_unit(Unit, Line),
 2663    assert_called(Src, '<test_unit>'(Unit), Goal, Line),
 2664    fail.
 2665assert_defined(Src, Goal) :-
 2666    defined(Goal, Src, _),
 2667    !.
 2668assert_defined(Src, Goal) :-
 2669    generalise(Goal, Term),
 2670    current_source_line(Line),
 2671    assert(defined(Term, Src, Line)).
 2672
 2673assert_foreign(Src, Goal) :-
 2674    foreign(Goal, Src, _),
 2675    !.
 2676assert_foreign(Src, Goal) :-
 2677    generalise(Goal, Term),
 2678    current_source_line(Line),
 2679    assert(foreign(Term, Src, Line)).
 2680
 2681assert_grammar_rule(Src, Goal) :-
 2682    grammar_rule(Goal, Src),
 2683    !.
 2684assert_grammar_rule(Src, Goal) :-
 2685    generalise(Goal, Term),
 2686    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.
 2699assert_import(_, [], _, _, _) :- !.
 2700assert_import(Src, [H|T], Export, From, Reexport) :-
 2701    !,
 2702    assert_import(Src, H, Export, From, Reexport),
 2703    assert_import(Src, T, Export, From, Reexport).
 2704assert_import(Src, except(Except), Export, From, Reexport) :-
 2705    !,
 2706    is_list(Export),
 2707    !,
 2708    except(Except, Export, Import),
 2709    assert_import(Src, Import, _All, From, Reexport).
 2710assert_import(Src, Import as Name, Export, From, Reexport) :-
 2711    !,
 2712    pi_to_head(Import, Term0),
 2713    rename_goal(Term0, Name, Term),
 2714    (   in_export_list(Term0, Export)
 2715    ->  assert(imported(Term, Src, From)),
 2716        assert_reexport(Reexport, Src, Term)
 2717    ;   current_source_line(Line),
 2718        assert_called(Src, '<directive>'(Line), Term0, Line)
 2719    ).
 2720assert_import(Src, Import, Export, From, Reexport) :-
 2721    pi_to_head(Import, Term),
 2722    !,
 2723    (   in_export_list(Term, Export)
 2724    ->  assert(imported(Term, Src, From)),
 2725        assert_reexport(Reexport, Src, Term)
 2726    ;   current_source_line(Line),
 2727        assert_called(Src, '<directive>'(Line), Term, Line)
 2728    ).
 2729assert_import(Src, op(P,T,N), _, _, _) :-
 2730    xref_push_op(Src, P,T,N).
 2731
 2732in_export_list(_Head, Export) :-
 2733    var(Export),
 2734    !.
 2735in_export_list(Head, Export) :-
 2736    member(PI, Export),
 2737    pi_to_head(PI, Head).
 2738
 2739assert_reexport(false, _, _) :- !.
 2740assert_reexport(true, Src, Term) :-
 2741    assert(exported(Term, Src)).
 process_import(:Import, +Src)
Process an import/1 directive
 2747process_import(M:PI, Src) :-
 2748    pi_to_head(PI, Head),
 2749    !,
 2750    (   atom(M),
 2751        current_module(M),
 2752        module_property(M, file(From))
 2753    ->  true
 2754    ;   From = '<unknown>'
 2755    ),
 2756    assert(imported(Head, Src, From)).
 2757process_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?
 2766assert_xmodule_callable([], _, _, _).
 2767assert_xmodule_callable([PI|T], M, Src, From) :-
 2768    (   pi_to_head(M:PI, Head)
 2769    ->  assert(imported(Head, Src, From))
 2770    ;   true
 2771    ),
 2772    assert_xmodule_callable(T, M, Src, From).
 assert_op(+Src, +Op) is det
Arguments:
Op- Ground term op(Priority, Type, Name).
 2779assert_op(Src, op(P,T,M:N)) :-
 2780    (   '$current_source_module'(M)
 2781    ->  Name = N
 2782    ;   Name = M:N
 2783    ),
 2784    (   xop(Src, op(P,T,Name))
 2785    ->  true
 2786    ;   assert(xop(Src, op(P,T,Name)))
 2787    ).
 assert_module(+Src, +Module)
Assert we are loading code into Module. This is also used to exploit local term-expansion and other rules.
 2794assert_module(Src, Module) :-
 2795    xmodule(Module, Src),
 2796    !.
 2797assert_module(Src, Module) :-
 2798    '$set_source_module'(Module),
 2799    assert(xmodule(Module, Src)),
 2800    (   module_property(Module, class(system))
 2801    ->  retractall(xoption(Src, register_called(_))),
 2802        assert(xoption(Src, register_called(all)))
 2803    ;   true
 2804    ).
 2805
 2806assert_module_export(_, []) :- !.
 2807assert_module_export(Src, [H|T]) :-
 2808    !,
 2809    assert_module_export(Src, H),
 2810    assert_module_export(Src, T).
 2811assert_module_export(Src, PI) :-
 2812    pi_to_head(PI, Term),
 2813    !,
 2814    assert(exported(Term, Src)).
 2815assert_module_export(Src, op(P, A, N)) :-
 2816    xref_push_op(Src, P, A, N).
 assert_module3(+Import, +Src)
Handle 3th argument of module/3 declaration.
 2822assert_module3([], _) :- !.
 2823assert_module3([H|T], Src) :-
 2824    !,
 2825    assert_module3(H, Src),
 2826    assert_module3(T, Src).
 2827assert_module3(Option, Src) :-
 2828    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.
 2837process_predicates(Closure, Preds, Src) :-
 2838    is_list(Preds),
 2839    !,
 2840    process_predicate_list(Preds, Closure, Src).
 2841process_predicates(Closure, as(Preds, _Options), Src) :-
 2842    !,
 2843    process_predicates(Closure, Preds, Src).
 2844process_predicates(Closure, Preds, Src) :-
 2845    process_predicate_comma(Preds, Closure, Src).
 2846
 2847process_predicate_list([], _, _).
 2848process_predicate_list([H|T], Closure, Src) :-
 2849    (   nonvar(H)
 2850    ->  call(Closure, H, Src)
 2851    ;   true
 2852    ),
 2853    process_predicate_list(T, Closure, Src).
 2854
 2855process_predicate_comma(Var, _, _) :-
 2856    var(Var),
 2857    !.
 2858process_predicate_comma(M:(A,B), Closure, Src) :-
 2859    !,
 2860    process_predicate_comma(M:A, Closure, Src),
 2861    process_predicate_comma(M:B, Closure, Src).
 2862process_predicate_comma((A,B), Closure, Src) :-
 2863    !,
 2864    process_predicate_comma(A, Closure, Src),
 2865    process_predicate_comma(B, Closure, Src).
 2866process_predicate_comma(as(Spec, _Options), Closure, Src) :-
 2867    !,
 2868    process_predicate_comma(Spec, Closure, Src).
 2869process_predicate_comma(A, Closure, Src) :-
 2870    call(Closure, A, Src).
 2871
 2872
 2873assert_dynamic(PI, Src) :-
 2874    pi_to_head(PI, Term),
 2875    (   thread_local(Term, Src, _)  % dynamic after thread_local has
 2876    ->  true                        % no effect
 2877    ;   current_source_line(Line),
 2878        assert(dynamic(Term, Src, Line))
 2879    ).
 2880
 2881assert_thread_local(PI, Src) :-
 2882    pi_to_head(PI, Term),
 2883    current_source_line(Line),
 2884    assert(thread_local(Term, Src, Line)).
 2885
 2886assert_multifile(PI, Src) :-                    % :- multifile(Spec)
 2887    pi_to_head(PI, Term),
 2888    current_source_line(Line),
 2889    assert(multifile(Term, Src, Line)).
 2890
 2891assert_public(PI, Src) :-                       % :- public(Spec)
 2892    pi_to_head(PI, Term),
 2893    current_source_line(Line),
 2894    assert_called(Src, '<public>'(Line), Term, Line),
 2895    assert(public(Term, Src, Line)).
 2896
 2897assert_export(PI, Src) :-                       % :- export(Spec)
 2898    pi_to_head(PI, Term),
 2899    !,
 2900    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.
 2907pi_to_head(Var, _) :-
 2908    var(Var), !, fail.
 2909pi_to_head(M:PI, M:Term) :-
 2910    !,
 2911    pi_to_head(PI, Term).
 2912pi_to_head(Name/Arity, Term) :-
 2913    functor(Term, Name, Arity).
 2914pi_to_head(Name//DCGArity, Term) :-
 2915    Arity is DCGArity+2,
 2916    functor(Term, Name, Arity).
 2917
 2918
 2919assert_used_class(Src, Name) :-
 2920    used_class(Name, Src),
 2921    !.
 2922assert_used_class(Src, Name) :-
 2923    assert(used_class(Name, Src)).
 2924
 2925assert_defined_class(Src, Name, _Meta, _Super, _) :-
 2926    defined_class(Name, _, _, Src, _),
 2927    !.
 2928assert_defined_class(_, _, _, -, _) :- !.               % :- pce_extend_class
 2929assert_defined_class(Src, Name, Meta, Super, Summary) :-
 2930    current_source_line(Line),
 2931    (   Summary == @(default)
 2932    ->  Atom = ''
 2933    ;   is_list(Summary)
 2934    ->  atom_codes(Atom, Summary)
 2935    ;   string(Summary)
 2936    ->  atom_concat(Summary, '', Atom)
 2937    ),
 2938    assert(defined_class(Name, Super, Atom, Src, Line)),
 2939    (   Meta = @(_)
 2940    ->  true
 2941    ;   assert_used_class(Src, Meta)
 2942    ),
 2943    assert_used_class(Src, Super).
 2944
 2945assert_defined_class(Src, Name, imported_from(_File)) :-
 2946    defined_class(Name, _, _, Src, _),
 2947    !.
 2948assert_defined_class(Src, Name, imported_from(File)) :-
 2949    assert(defined_class(Name, _, '', Src, file(File))).
 2950
 2951
 2952                /********************************
 2953                *            UTILITIES          *
 2954                ********************************/
 generalise(+Callable, -General)
Generalise a callable term.
 2960generalise(Var, Var) :-
 2961    var(Var),
 2962    !.                    % error?
 2963generalise(pce_principal:send_implementation(Id, _, _),
 2964           pce_principal:send_implementation(Id, _, _)) :-
 2965    atom(Id),
 2966    !.
 2967generalise(pce_principal:get_implementation(Id, _, _, _),
 2968           pce_principal:get_implementation(Id, _, _, _)) :-
 2969    atom(Id),
 2970    !.
 2971generalise('<directive>'(Line), '<directive>'(Line)) :- !.
 2972generalise(test(Test), test(Test)) :-
 2973    current_test_unit(_,_),
 2974    ground(Test),
 2975    !.
 2976generalise(test(Test, _), test(Test, _)) :-
 2977    current_test_unit(_,_),
 2978    ground(Test),
 2979    !.
 2980generalise('<test_unit>'(Line), '<test_unit>'(Line)) :- !.
 2981generalise(Module:Goal0, Module:Goal) :-
 2982    atom(Module),
 2983    !,
 2984    generalise(Goal0, Goal).
 2985generalise(Term0, Term) :-
 2986    callable(Term0),
 2987    generalise_term(Term0, Term).
 2988
 2989
 2990                 /*******************************
 2991                 *      SOURCE MANAGEMENT       *
 2992                 *******************************/
 2993
 2994/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 2995This section of the file contains   hookable  predicates to reason about
 2996sources. The built-in code here  can  only   deal  with  files. The XPCE
 2997library(pce_prolog_xref) provides hooks to deal with XPCE objects, so we
 2998can do cross-referencing on PceEmacs edit   buffers.  Other examples for
 2999hooking can be databases, (HTTP) URIs, etc.
 3000- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 3001
 3002:- multifile
 3003    prolog:xref_source_directory/2, % +Source, -Dir
 3004    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.
 3012xref_source_file(Plain, File, Source) :-
 3013    xref_source_file(Plain, File, Source, []).
 3014
 3015xref_source_file(QSpec, File, Source, Options) :-
 3016    nonvar(QSpec), QSpec = _:Spec,
 3017    !,
 3018    must_be(acyclic, Spec),
 3019    xref_source_file(Spec, File, Source, Options).
 3020xref_source_file(Spec, File, _Source, _Options) :-
 3021    is_stream(Spec), !,
 3022    File = Spec.
 3023xref_source_file(Spec, File, Source, Options) :-
 3024    nonvar(Spec),
 3025    prolog:xref_source_file(Spec, File,
 3026                            [ relative_to(Source)
 3027                            | Options
 3028                            ]),
 3029    !.
 3030xref_source_file(Plain, File, Source, Options) :-
 3031    atom(Plain),
 3032    \+ is_absolute_file_name(Plain),
 3033    (   prolog:xref_source_directory(Source, Dir)
 3034    ->  true
 3035    ;   atom(Source),
 3036        file_directory_name(Source, Dir)
 3037    ),
 3038    atomic_list_concat([Dir, /, Plain], Spec0),
 3039    absolute_file_name(Spec0, Spec),
 3040    do_xref_source_file(Spec, File, Options),
 3041    !.
 3042xref_source_file(Spec, File, Source, Options) :-
 3043    do_xref_source_file(Spec, File,
 3044                        [ relative_to(Source)
 3045                        | Options
 3046                        ]),
 3047    !.
 3048xref_source_file(_, _, _, Options) :-
 3049    option(silent(true), Options),
 3050    !,
 3051    fail.
 3052xref_source_file(Spec, _, Src, _Options) :-
 3053    verbose(Src),
 3054    \+ current_cond(_),             % may be guarded by exists_source/1
 3055    print_message(warning, error(existence_error(file, Spec), _)),
 3056    fail.
 3057
 3058do_xref_source_file(Spec, File, Options) :-
 3059    nonvar(Spec),
 3060    option(file_type(Type), Options, prolog),
 3061    absolute_file_name(Spec, File0,
 3062                       [ file_type(Type),
 3063                         access(read),
 3064                         file_errors(fail)
 3065                       ]),
 3066    !,
 3067    qlf_pl_file(File0, File).
 3068do_xref_source_file(Spec, File, Options) :-
 3069    atom(Spec), % handle absolute /file/to/source.pl without sources
 3070    file_name_extension(Base, Ext, Spec),
 3071    user:prolog_file_type(Ext, source),
 3072    option(file_type(prolog), Options, prolog),
 3073    absolute_file_name(Base, File0,
 3074                       [ file_type(prolog),
 3075                         access(read),
 3076                         file_errors(fail)
 3077                       ]),
 3078    qlf_pl_file(File0, File).
 qlf_pl_file(?QlfFile, ?PlFile) is semidet
 3082qlf_pl_file(QlfFile, PlFile) :-
 3083    nonvar(QlfFile),
 3084    is_qlf_file(QlfFile),
 3085    !,
 3086    '$qlf_module'(QlfFile, Info),
 3087    #{file:PlFile} :< Info.
 3088qlf_pl_file(QlfFile, PlFile) :-
 3089    nonvar(PlFile),
 3090    !,
 3091    (   file_name_extension(Base, Ext, PlFile),
 3092        user:prolog_file_type(Ext, source)
 3093    ->  true
 3094    ),
 3095    (   user:prolog_file_type(QlfExt, qlf),
 3096        file_name_extension(Base, QlfExt, QlfFile),
 3097        exists_file(QlfFile)
 3098    ->  true
 3099    ),
 3100    '$qlf_module'(QlfFile, Info),
 3101    #{file:PlFile} :< Info,
 3102    !.
 3103qlf_pl_file(PlFile, PlFile).
 3104
 3105is_qlf_file(QlfFile) :-
 3106    file_name_extension(_, Ext, QlfFile),
 3107    user:prolog_file_type(Ext, qlf),
 3108    !.
 canonical_source(?Source, ?Src) is det
Src is the canonical version of Source if Source is given.
 3114canonical_source(Source, Src) :-
 3115    (   ground(Source)
 3116    ->  prolog_canonical_source(Source, Src)
 3117    ;   Source = Src
 3118    ).
 goal_name_arity(+Goal, -Name, -Arity)
Generalized version of functor/3 that can deal with name() goals.
 3125goal_name_arity(Goal, Name, Arity) :-
 3126    (   compound(Goal)
 3127    ->  compound_name_arity(Goal, Name, Arity)
 3128    ;   atom(Goal)
 3129    ->  Name = Goal, Arity = 0
 3130    ).
 3131
 3132generalise_term(Specific, General) :-
 3133    (   compound(Specific)
 3134    ->  compound_name_arity(Specific, Name, Arity),
 3135        compound_name_arity(General, Name, Arity)
 3136    ;   General = Specific
 3137    ).
 3138
 3139functor_name(Term, Name) :-
 3140    (   compound(Term)
 3141    ->  compound_name_arity(Term, Name, _)
 3142    ;   atom(Term)
 3143    ->  Name = Term
 3144    ).
 3145
 3146rename_goal(Goal0, Name, Goal) :-
 3147    (   compound(Goal0)
 3148    ->  compound_name_arity(Goal0, _, Arity),
 3149        compound_name_arity(Goal, Name, Arity)
 3150    ;   Goal = Name
 3151    ).
 3152
 3153
 3154                /*******************************
 3155                *           MESSAGES           *
 3156                *******************************/<
 3157
 3158:- multifile prolog:message//1.
 3159
 3160prolog:message(meta_predicate_after_call(Decl, By)) -->
 3161    { pi_head(ByPI, By) },
 3162    [ ansi(code, ':- meta_predicate(~p)', [Decl]),
 3163      ' declaration appears after call from '-[],
 3164      ansi(code, '~p', [ByPI])
 3165    ]