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(codewalk_prolog, []).   36
   37:- use_module(library(lists)).   38:- use_module(library(option)).   39:- use_module(library(prolog_codewalk)).   40:- use_module(library(assertions)).   41:- use_module(library(extra_location)).   42:- use_module(library(option_utils)).   43:- use_module(library(from_utils)).   44
   45:- thread_local
   46    issues/1.
 extra_walk_module_body(-Module, +Options) is det
   50extra_walk_module_body(Options) :-
   51    ( option(module(M), Options)
   52    ->findall(Ref, current_clause_module_body(M, Ref), RefU),
   53      sort(RefU, RefL),
   54      prolog_walk_code([source(false), clauses(RefL)|Options])
   55    ; true
   56    ).
   57
   58:- multifile
   59    codewalk:walk_code/2.   60
   61codewalk:walk_code(prolog, Options1) :-
   62    extra_wcsetup(Options1, Options2, MFileD),
   63    foldl(select_option_default,
   64          [source(S)-false,
   65           walkextras(Extras)-[declaration, asrparts([body])],
   66           on_trace(ETracer)-ETracer
   67          ], Options2, Options3),
   68    Options = [on_trace(codewalk_prolog:pcw_trace(1, ETracer, MFileD))|Options3],
   69    extra_walk_module_body(Options),
   70    optimized_walk_code(S, Stage, codewalk_prolog:pcw_trace(Stage, ETracer, MFileD), Options3),
   71    prolog_codewalk:make_walk_option(Options, OTerm),
   72    maplist(walk_extras_p(OTerm, MFileD), Extras).
   73
   74:- public pcw_trace/6.   75:- meta_predicate pcw_trace(+,3,+,+,+,+).   76pcw_trace(1, ETracer, MFileD, M:Goal, Caller, From) :-
   77    get_dict(M, MFileD, FileD),
   78    from_to_file(From, File),
   79    get_dict(File, FileD, _),
   80    '$set_source_module'(M),
   81    call(ETracer, M:Goal, Caller, From),
   82    ( From = clause(CRef)
   83    ->record_issues(CRef)
   84    ; true
   85    ).
   86pcw_trace(2, ETracer, _, Goal, Caller, From) :-
   87    call(ETracer, Goal, Caller, From).
   88
   89walk_extras_p(OTerm, MFileD, Extra) :- walk_extras_(Extra, OTerm, MFileD).
   90
   91walk_extras_(declaration, OTerm, MFileD) :- walk_from_loc_declaration(OTerm, MFileD).
   92walk_extras_(asrparts(L), OTerm, MFileD) :- walk_from_assertion(OTerm, MFileD, L).
   93
   94current_clause_module_body(CM, Ref) :-
   95    MH = M:_,
   96    current_predicate(_, MH),
   97    M \= CM,
   98    \+ predicate_property(MH, imported_from(_)),
   99    ( catch(clause(MH, Body, Ref), _, fail),
  100      clause_property(Ref, module(HM)),
  101      strip_module(HM:Body, CM, _)
  102    ).
  103
  104optimized_walk_code(false, 1, Tracer, Options) :-
  105    prolog_walk_code([source(false), on_trace(Tracer)|Options]).
  106optimized_walk_code(true, Stage, Tracer, Options) :-
  107    optimized_walk_code_true(Stage, Tracer, Options).
  108
  109optimized_walk_code_true(1, Tracer, Options) :-
  110    prolog_walk_code([source(false), on_trace(Tracer)|Options]),
  111    fail.
  112optimized_walk_code_true(2, Tracer, Options) :-
  113    findall(CRef, retract(issues(CRef)), ClausesU),
  114    sort(ClausesU, Clauses),
  115    ( Clauses==[]
  116    ->true
  117    ; prolog_walk_code([clauses(Clauses), on_trace(Tracer)|Options])
  118    ).
  119
  120extra_wcsetup(Options1, Options, MFileD) :-
  121    option_module_files(MFileD, Options1, Options2),
  122    merge_options(Options2,
  123                  [infer_meta_predicates(false),
  124                   autoload(false),
  125                   evaluate(false),
  126                   trace_reference(_),
  127                   module_class([user, system, library])
  128                  ], Options).
  129
  130walk_from_loc_declaration(OTerm, MFileD) :-
  131    forall(( prolog_codewalk:walk_option_caller(OTerm, '<declaration>'),
  132             clause(loc_declaration(Head, M, goal, From), _, Ref),
  133             get_dict(M, MFileD, FileD),
  134             from_to_file(From, File),
  135             get_dict(File, FileD, _)
  136           ),
  137           walk_from_goal(Head, M, Ref, OTerm)).
  138
  139walk_from_goal(Head, M, Ref, OTerm) :-
  140    prolog_codewalk:( scan_module(M, OTerm),
  141                      walk_option_clause(OTerm, Ref),
  142                      walk_called_by_body(no_positions, Head, M, OTerm)
  143                    ).
  144
  145walk_from_assertion(OTerm, MFileD, AsrPartL) :-
  146    option_files([module_files(MFileD)], FileD),
  147    forall(( AHead = assertions:asr_head_prop(Asr, HM, Head, _, _, _, _, From),
  148             clause(AHead, Body, Ref),
  149             module_property(Ref, module(M)),
  150             call(M:Body),
  151             from_to_file(From, File),
  152             get_dict(File, FileD, _),
  153             predicate_property(HM:Head, implementation_module(M)),
  154             prolog_codewalk:walk_option_caller(OTerm, '<assertion>'(M:Head)),
  155             member(AsrPart, AsrPartL),
  156             assertion_goal(AsrPart, Head, HM, Asr, Goal, CM)
  157           ),
  158           walk_from_goal(Goal, CM, Ref, OTerm)).
  159
  160assertion_goal(head, Head, HM, _, Head, HM).
  161assertion_goal(body, _, _, Asr, Prop, PM) :-
  162    member(Part, [comp, call, succ, glob]),
  163    % For glob, actually is arg(1, Prop, HM:Head), but we keep it uninstantiated for optimization
  164    curr_prop_asr(Part, PM:Prop, _, Asr).
  165
  166record_issues(CRef) :-
  167    assertz(issues(CRef))