1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera Menendez
    4    E-mail:        efmera@gmail.com
    5    WWW:           https://github.com/edisonm/xtools
    6    Copyright (C): 2015, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(called_from, [called_from/1,
   36                        called_from/2,
   37                        called_from/5,
   38                        collect_called_from/5,
   39                        collect_called_from/6,
   40                        current_called_from/5,
   41                        current_used_from/6,
   42                        used_predicates/2,
   43                        used_predicates/3
   44                       ]).   45
   46:- use_module(library(implementation_module)).   47:- use_module(library(assrt_lib)).   48:- use_module(library(normalize_head)).   49:- use_module(library(normalize_pi)).   50:- use_module(library(codewalk)).   51:- use_module(library(extra_location)).   52:- use_module(library(location_utils)).   53:- use_module(library(from_utils)).   54
   55:- multifile
   56    prolog:message//1.   57
   58:- dynamic called_from_db/5.   59
   60prolog:message(acheck(called_from(MsgLoc, Args))) -->
   61        MsgLoc,
   62        ['~w called from ~w'-Args].
   63
   64called_from(Ref) :-
   65        called_from(Ref, _).
   66
   67called_from(Ref, Caller) :-
   68    ( called_from(Ref, _CM, Caller, [], Sorted),
   69      maplist(print_call_point, Sorted),
   70      fail
   71    ; cleanup_loc_dynamic(_, _, dynamic(_, _, _), _),
   72      retractall(called_from_db(_, _, _, _, _))
   73    ).
   74
   75called_from(Ref, CM, Caller, Options, Pairs) :-
   76    normalize_head(Ref, M:H),
   77    collect_called_from(H, M, CM, Caller, Options, Pairs).
   78
   79collect_called_from(H, M, CM, Caller, Options, Sorted) :-
   80    collect_called_from(H, M, CM, Caller, Options),
   81    findall(Loc-[M:F/A, CPI], ( current_called_from(H, M, CM, From, C),
   82                                functor(H, F, A),
   83                                normalize_pi(C, CPI),
   84                                from_location(From, Loc)
   85                              ), Pairs),
   86    keysort(Pairs, Sorted).
   87
   88collect_called_from(Ref, M, CM, Caller, Options1) :-
   89    cleanup_loc_dynamic(_, _, dynamic(_, _, _), _),
   90    retractall(called_from_db(_, _, _, _, _)),
   91    merge_options([source(true),
   92                   infer_meta_predicates(false),
   93                   autoload(false),
   94                   evaluate(false),
   95                   method(prolog),
   96                   trace_reference(_:Ref),
   97                   module_class([user, system, library]),
   98                   on_trace(collect_call_point(M, CM, Caller))],
   99                  Options1, Options),
  100    walk_code(Options).
  101
  102current_called_from(H, M, CM, From, Caller) :-
  103    current_used_from([retract, query], H, M, CM, From, Caller).
  104
  105current_used_from(DynTypes, H, M, CM, From, Caller) :-
  106    ( called_from_db(H, M, CM, Caller, From)
  107    ; loc_dynamic(H, M, dynamic(Type, CM, Caller), From),
  108      memberchk(Type, DynTypes)
  109    ; loc_declaration(H, CM, goal, From),
  110      implementation_module(CM:H, M)
  111    ; curr_prop_asr(head, CM:H, From, _),
  112      implementation_module(CM:H, M),
  113      Caller = '<assertion>'(M:H)
  114    ).
  115
  116:- public collect_call_point/6.  117:- meta_predicate collect_call_point(?, ?, ?, +, +, +).  118collect_call_point(IM, M, Caller, MGoal, Caller, From) :-
  119    ignore(record_location_dynamic(MGoal, IM, From)),
  120    MGoal = M:Goal,
  121    implementation_module(MGoal, IM),
  122    update_fact_from(called_from_db(Goal, IM, M, Caller), From).
  123
  124print_call_point(L-A) :-
  125    print_message(information, acheck(called_from(L, A))).
  126
  127% used_predicates(+Module, +Context, -PIL) is det
  128%
  129% Unifies PIL with a list of predicates implemented in the module Module,
  130% actually being used in the context Context.  Note that this would be different
  131% than the imported predicates.
  132%
  133used_predicates(Module, Context, PIL) :-
  134    collect_called_from(_, Module, Context, _, [source(false)]),
  135    findall(F/A,
  136            ( current_called_from(H, Module, Context, _, _),
  137              functor(H, F, A)
  138            ), PIU),
  139    sort(PIU, PIL).
  140
  141used_predicates(Module, Groups) :-
  142    collect_called_from(_, Module, _, _, [source(false)]),
  143    findall(Context-(F/A),
  144            ( current_called_from(H, Module, Context, _, _),
  145              functor(H, F, A)
  146            ), Pairs),
  147    sort(Pairs, Sorted),
  148    group_pairs_by_key(Sorted, Groups)