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(option)).   47:- use_module(library(pairs)).   48:- use_module(library(assertions)).   49:- use_module(library(normalize_head)).   50:- use_module(library(normalize_pi)).   51:- use_module(library(codewalk)).   52:- use_module(library(extra_location)).   53:- use_module(library(location_utils)).   54:- use_module(library(dynamic_locations)).   55:- use_module(library(from_utils)).   56
   57:- multifile
   58    prolog:message//1.   59
   60:- dynamic called_from_db/5.   61
   62prolog:message(acheck(called_from(MsgLoc, Args))) -->
   63        MsgLoc,
   64        ['~w called from ~w'-Args].
   65
   66called_from(Ref) :-
   67        called_from(Ref, _).
   68
   69called_from(Ref, Caller) :-
   70    ( called_from(Ref, _CM, Caller, [], Sorted),
   71      maplist(print_call_point, Sorted),
   72      fail
   73    ; cleanup_dynl_db,
   74      retractall(called_from_db(_, _, _, _, _))
   75    ).
   76
   77called_from(Ref, CM, Caller, Options, Pairs) :-
   78    normalize_head(Ref, M:H),
   79    collect_called_from(H, M, CM, Caller, Options, Pairs).
   80
   81collect_called_from(H, M, CM, Caller, Options, Sorted) :-
   82    collect_called_from(H, M, CM, Caller, Options),
   83    findall(Loc-[M:F/A, CPI],
   84            ( current_called_from(H, M, CM, From, C),
   85              functor(H, F, A),
   86              normalize_pi(C, CPI),
   87              from_location(From, Loc)
   88            ), Pairs),
   89    sort(Pairs, Sorted).
   90
   91collect_called_from(Ref, M, CM, Caller, Options1) :-
   92    retractall(called_from_db(_, _, _, _, _)),
   93    merge_options([source(true),
   94                   infer_meta_predicates(false),
   95                   autoload(false),
   96                   evaluate(false),
   97                   method(prolog),
   98                   trace_reference(_:Ref),
   99                   module_class([user, system, library]),
  100                   on_trace(collect_call_point(M, CM, Caller))],
  101                  Options1, Options),
  102    walk_code(Options).
  103
  104current_called_from(H, M, CM, From, Caller) :-
  105    current_used_from([retract, query], H, M, CM, From, Caller).
  106
  107current_used_from(DynTypes, H, M, CM, From, Caller) :-
  108    ( called_from_db(H, M, CM, Caller, From)
  109    ; loc_dynamic(H, M, dynamic(Type, CM, Caller), From),
  110      memberchk(Type, DynTypes)
  111    ; loc_declaration(H, CM, goal, From),
  112      predicate_property(CM:H, implementation_module(M))
  113    ; curr_prop_asr(head, CM:H, From, _),
  114      predicate_property(CM:H, implementation_module(M)),
  115      Caller = '<assertion>'(M:H)
  116    ).
  117
  118:- public collect_call_point/6.  119:- meta_predicate collect_call_point(?, ?, ?, +, +, +).  120collect_call_point(IM, M, Caller, MGoal, Caller, From) :-
  121    ignore(record_location_dynamic(MGoal, IM, From)),
  122    MGoal = M:Goal,
  123    predicate_property(MGoal, implementation_module(IM)),
  124    update_fact_from(called_from_db(Goal, IM, M, Caller), From).
  125
  126print_call_point(L-A) :-
  127    print_message(information, acheck(called_from(L, A))).
  128
  129% used_predicates(+Module, +Context, -PIL) is det
  130%
  131% Unifies PIL with a list of predicates implemented in the module Module,
  132% actually being used in the context Context.  Note that this would be different
  133% than the imported predicates.
  134%
  135used_predicates(Module, Context, PIL) :-
  136    collect_called_from(_, Module, Context, _, [source(false)]),
  137    findall(F/A,
  138            ( current_called_from(H, Module, Context, _, _),
  139              functor(H, F, A)
  140            ), PIU),
  141    sort(PIU, PIL).
  142
  143used_predicates(Module, Groups) :-
  144    collect_called_from(_, Module, _, _, [source(false)]),
  145    findall(Context-(F/A),
  146            ( current_called_from(H, Module, Context, _, _),
  147              functor(H, F, A)
  148            ), Pairs),
  149    sort(Pairs, Sorted),
  150    group_pairs_by_key(Sorted, Groups)