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): 2017, 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(codewalk_source, []).   36
   37:- use_module(library(prolog_source)).   38:- use_module(library(prolog_xref), []).   39:- use_module(library(context_values)).   40:- use_module(library(option_utils)).   41:- use_module(library(extend_args)).   42
   43:- multifile
   44    codewalk:walk_code/2.   45
   46codewalk:walk_code(source, Options) :-
   47    do_source_walk_code(Options).
   48
   49head_caller(MHead, M:Head) :-
   50    '$current_source_module'(CM),
   51    strip_module(CM:MHead, M, Head).
   52
   53determine_caller((Head :-  _), Caller) :- !, head_caller(Head, Caller).
   54determine_caller((Head --> _), Caller) :-
   55    !,
   56    extend_args(Head, [_, _], EHead),
   57    head_caller(EHead, Caller).
   58determine_caller((:- Decl), Caller) :- !, decl_caller(Decl, Caller).
   59determine_caller(Head, Caller) :- head_caller(Head, Caller).
   60
   61decl_caller(initialization(_), '<initialization>').
   62decl_caller(_,                 '<declaration>').
   63
   64:- public
   65    check_trace_reference/3,
   66    do_term_expansion/1,
   67    do_goal_expansion/3,
   68    determine_caller/2.   69
   70prepare(To, Undefined, p(TRef, GRef)) :-
   71    ( To \== (-)
   72    ->( var(To)
   73      ->assertz((system:goal_expansion(G, P, _, _) :-
   74                       '$current_source_module'(M),
   75                       once(do_goal_expansion(M, G, P)),
   76                       fail), GRef)
   77      ; To = _:H
   78      ->functor(H, F, A),
   79        functor(G, F, A), % speed up goal expansion
   80        assertz((system:goal_expansion(G, P, _, _) :-
   81                     '$current_source_module'(M),
   82                     check_trace_reference(To, M, G),
   83                     once(do_goal_expansion(M, G, P)),
   84                     fail), GRef)
   85      ; true
   86      )
   87    ; Undefined = ignore
   88    ->true
   89    ; Undefined = trace
   90    ->assertz((system:goal_expansion(G, P, _, _) :-
   91                   '$current_source_module'(M),
   92                   \+ '$get_predicate_attribute'(M:G, defined, 1),
   93                   \+ predicate_property(M:G, autoload(_)),
   94                   once(do_goal_expansion(M, G, P)),
   95                   fail), GRef)
   96    ; true
   97    ),
   98    ( nonvar(GRef)
   99    ->assertz((system:term_expansion(T, P, T, P) :-
  100               do_term_expansion(T)), TRef)
  101    ; true
  102    ).
  103
  104cleanup(p(TRef, GRef)) :-
  105    ( nonvar(TRef)
  106    ->erase(TRef)
  107    ; true
  108    ),
  109    ( nonvar(GRef)
  110    ->erase(GRef)
  111    ; true
  112    ).
  113
  114skip((_,_)).
  115skip((_;_)).
  116skip((_->_)).
  117skip((_*->_)).
  118skip(\+(_)).
  119skip(module(_, _)).
  120skip(module(_, _, _)).
  121skip(_:_).
  122
  123check_file(File) :-
  124    current_context_value(file, File),
  125    prolog_load_context(source, File).
  126
  127do_term_expansion(Term) :-
  128    check_file(_),
  129    determine_caller(Term, Caller),
  130    set_context_value(caller, Caller).
  131
  132check_trace_reference(To, M, Goal) :-
  133    (   subsumes_term(To, M:Goal)
  134    ->  true
  135    ;   predicate_property(M:Goal, imported_from(M2)),
  136        subsumes_term(To, M2:Goal)
  137    ).
  138
  139do_goal_expansion(M, Goal, TermPos) :-
  140    check_file(File),
  141    \+ skip(Goal),
  142    ( TermPos \= none
  143    ->From = file_term_position(File, TermPos)
  144    ; prolog_load_context(term_position, Pos),
  145      stream_position_data(line_count, Pos, Line),
  146      From = file(File, Line, -1, _)
  147    ),
  148    current_context_value(on_trace, OnTrace),
  149    current_context_value(caller,   Caller),
  150    call(OnTrace, M:Goal, Caller, From).
  151
  152do_source_walk_code(Options1) :-
  153    foldl(select_option_default,
  154          [on_trace(OnTrace)-(codewalk:true_3),
  155           trace_reference(To)-To,
  156           undefined(Undefined)-ignore,
  157           variable_names(VNL)-VNL],
  158          Options1, Options),
  159    freeze(VNL, b_setval('$variable_names', VNL)),
  160    with_context_values(
  161        setup_call_cleanup(
  162            ( '$current_source_module'(OldM),
  163              freeze(M, '$set_source_module'(M)),
  164              prepare(To, Undefined, Ref)
  165            ),
  166            walk_source(M, File, [variable_names(VNL)|Options]),
  167            ( '$set_source_module'(OldM),
  168              cleanup(Ref)
  169            )),
  170        [file, on_trace],
  171        [File, OnTrace]).
  172
  173walk_source(M, File, Options) :-
  174    option_module_files(Options, MFileD),
  175    forall(( get_dict(M, MFileD, FileD),
  176             get_dict(File, FileD, _)
  177           ),
  178           setup_call_cleanup(
  179               prolog_open_source(File, In),
  180               fetch_term(In, Options),
  181               prolog_close_source(In))).
  182
  183fetch_term(In, Options1) :-
  184    foldl(select_option_default,
  185          [subterm_positons(TermPos)-TermPos,
  186           term_position(Pos)-Pos,
  187           syntax_errors(SE)-dec10,
  188           process_comment(PC)-false,
  189           comments(C)-C
  190          ], Options1, Options2),
  191    Options = [subterm_positions(TermPos),
  192               syntax_errors(SE),
  193               term_position(Pos),
  194               process_comment(PC),
  195               comments(C)
  196               |Options2
  197              ],
  198    repeat,
  199      read_clause(In, Term, Options),
  200      prolog_xref:update_condition(Term),
  201      '$current_source_module'(M),
  202      prolog_xref:current_condition(Cond),
  203      ( M:Cond
  204      ->prolog_source:expand(Term, TermPos, In, Expanded),
  205        prolog_source:update_state(Term, Expanded, M)
  206      ; true
  207      ),
  208      Term == end_of_file,
  209    !