1/*  Part of Extended Tools for SWI-Prolog
    2
    3    Author:        Edison Mera
    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(record_locations, [record_location/0]).   36
   37:- use_module(library(extra_location)). % shold be the first
   38:- use_module(library(apply)).   39:- use_module(library(filepos_line)).   40:- use_module(library(from_utils)).   41
   42:- multifile
   43    system:term_expansion/4,
   44    system:goal_expansion/4.   45
   46:- dynamic record_location/0.   47record_location. % Enable recording of locations
   48
   49:- thread_local rl_tmp/3. % trick to detect if term_expansion was applied
   50
   51% Extra location for assertions of a given predicate
   52extra_location:loc_declaration(Head, M, assertion(Status, Type), From) :-
   53    assertions:asr_head_prop(_, CM, Head, Status, Type, _, _, From),
   54    predicate_property(CM:Head, implementation_module(M)).
   55
   56:- multifile skip_record_decl/1.   57
   58skip_record_decl(initialization(_)) :- !.
   59skip_record_decl(Decl) :-
   60    nonvar(Decl),
   61    '$current_source_module'(M),
   62    predicate_property(M:Decl, imported_from(assertions)),
   63    functor(Decl, Type, Arity),
   64    memberchk(Arity, [1, 2]),
   65    assertions:assrt_type(Type), !.
   66
   67:- public record_extra_location/4.   68
   69record_extra_location((:- Decl),
   70                      term_position(_, _, _, _, [DPos])) -->
   71    ( {\+ skip_record_decl(Decl)}
   72    ->record_extra_decl(Decl, DPos)
   73    ; []
   74    ).
   75
   76record_extra_decl(Decl, DPos) -->
   77    { '$current_source_module'(SM),
   78      declaration_pos(Decl, DPos, SM, M, IdL, ArgL, PosL)
   79    },
   80    foldl(assert_declaration(M), IdL, ArgL, PosL),
   81    !.
   82record_extra_decl(Goal, Pos) -->
   83    { nonvar(Goal),
   84      source_location(File, Line),
   85      retractall(rl_tmp(File, Line, _)),
   86      asserta(rl_tmp(File, Line, 1)),
   87      assert_position(Goal, Pos, body)
   88    }.
   89
   90declaration_pos(DM:Decl, term_position(_, _, _, _, [_, DPos]), _, M, ID, U, Pos) :-
   91    declaration_pos(Decl, DPos, DM, M, ID, U, Pos).
   92declaration_pos(module(M, L), DPos,
   93                _, M, [module_2, export], [module(M, L), L], [DPos, Pos]) :-
   94    DPos = term_position(_, _, _, _, [_, Pos]).
   95declaration_pos(volatile(L), term_position(_, _, _, _, PosL),
   96                M, M, [volatile], [L], PosL).
   97declaration_pos(dynamic(L), term_position(_, _, _, _, PosL),
   98                M, M, [dynamic], [L], PosL).
   99declaration_pos(thread_local(L), term_position(_, _, _, _, PosL),
  100                M, M, [thread_local], [L], PosL).
  101declaration_pos(public(L), term_position(_, _, _, _, PosL),
  102                M, M, [public], [L], PosL).
  103declaration_pos(export(L), term_position(_, _, _, _, PosL),
  104                M, M, [export], [L], PosL).
  105declaration_pos(multifile(L), term_position(_, _, _, _, PosL),
  106                M, M, [multifile], [L], PosL).
  107declaration_pos(discontiguous(L), term_position(_, _, _, _, PosL),
  108                M, M, [discontiguous], [L], PosL).
  109declaration_pos(meta_predicate(L), term_position(_, _, _, _, PosL),
  110                M, M, [meta_predicate], [L], PosL).
  111declaration_pos(reexport(SM:DU),  DPos,  _, M, ID, U, Pos) :- !,
  112    declaration_pos(reexport(DU), DPos, SM, M, ID, U, Pos).
  113declaration_pos(use_module(SM:DU),  DPos,  _, M, ID, U, Pos) :- !,
  114    declaration_pos(use_module(DU), DPos, SM, M, ID, U, Pos).
  115declaration_pos(use_module(SM:DU, L), DPos, ID, _, M, U, Pos) :- !,
  116    declaration_pos(use_module(DU, L), DPos, ID, SM, M, U, Pos).
  117declaration_pos(reexport(SM:DU, L), DPos, ID, _, M, U, Pos) :- !,
  118    declaration_pos(reexport(DU, L), DPos, ID, SM, M, U, Pos).
  119declaration_pos(include(U),    DPos, M, M, [include],    [U], [DPos]).
  120declaration_pos(use_module(U), DPos, M, M, [use_module], [U], [DPos]).
  121declaration_pos(reexport(U),   DPos, M, M, [reexport],   [U], [DPos]).
  122declaration_pos(consult(U),    DPos, M, M, [consult],    [U], [DPos]).
  123declaration_pos(reexport(U, L), DPos, M, M,
  124                [reexport_2, reexport(U)], [reexport(U, L), L], [DPos, Pos]) :-
  125    DPos = term_position(_, _, _, _, [_, Pos]).
  126declaration_pos(use_module(U, L), DPos, M, M,
  127                [use_module_2, import(U)], [use_module(U, L), L], [DPos, Pos]) :-
  128    DPos = term_position(_, _, _, _, [_, Pos]).
  129
  130:- meta_predicate foldsequence(4,?,?,?,?).  131
  132foldsequence(G, A, B) --> foldsequence_(A, G, B).
  133
  134foldsequence_(A, _, _) -->
  135    {var(A)},
  136    !.
  137    % call(G, A).
  138foldsequence_([], _, _) --> !.
  139foldsequence_([E|L], G, list_position(_, _, PosL, _)) -->
  140    !,
  141    foldl(foldsequence(G), [E|L], PosL).
  142foldsequence_((A, B), G, term_position(_, _, _, _, [PA, PB])) -->
  143    !,
  144    foldsequence_(A, G, PA),
  145    foldsequence_(B, G, PB).
  146foldsequence_(A, G, PA) --> call(G, A, PA).
  147
  148assert_declaration(M, Declaration, Sequence, Pos) -->
  149    foldsequence(assert_declaration_one(Declaration, M), Sequence, Pos).
  150
  151assert_declaration_one(reexport(U), M, PI, Pos) -->
  152    !,
  153    assert_reexport_declaration_2(PI, U, Pos, M).
  154assert_declaration_one(module_2, M, H, Pos) -->
  155    !,
  156    % [(:- discontiguous '$exported_op'/3)], % Not a good idea: commented out because it causes problems in apps that use this library
  157    assert_declaration_one(H, M, module_2, Pos).
  158assert_declaration_one(Declaration, _, M:PI,
  159                       term_position(_, _, _, _, [_, Pos])) -->
  160    !,
  161    assert_declaration_one(Declaration, M, PI, Pos).
  162assert_declaration_one(Declaration, M, F/A, Pos) -->
  163    { atom(F),
  164      integer(A)
  165    },
  166    !,
  167    {functor(H, F, A)},
  168    assert_position(H, M, Declaration, Pos).
  169assert_declaration_one(Declaration, M, F//A1, Pos) -->
  170    { atom(F),
  171      integer(A1)
  172    },
  173    !,
  174    { A is A1+2,
  175      functor(H, F, A)
  176    },
  177    assert_position(H, M, Declaration, Pos).
  178assert_declaration_one(Declaration, M, H, Pos) -->
  179    assert_position(H, M, Declaration, Pos).
  180
  181assert_reexport_declaration_2((F/A as G), U, Pos, M) -->
  182    {functor(H, G, A)},
  183    assert_position(H, M, reexport(U, [F/A as G]), Pos).
  184assert_reexport_declaration_2(F/A, U, Pos, M) -->
  185    {functor(H, F, A)},
  186    assert_position(H, M, reexport(U, [F/A]), Pos).
  187assert_reexport_declaration_2(op(_, _, _), _, _, _) --> [].
  188assert_reexport_declaration_2(except(_),   _, _, _) --> [].
  189
  190assert_position(H, M, Type, TermPos) :-
  191    assert_position(H, M, Type, TermPos, Clauses, []),
  192    compile_aux_clauses(Clauses).
  193
  194assert_position(H, M, Type, TermPos) -->
  195    { source_location(File, Line1),
  196      ( nonvar(TermPos)
  197      ->arg(1, TermPos, Chars),
  198        filepos_line(File, Chars, Line, LinePos)
  199        % Meld TermPos because later the source code will not be available and
  200        % therefore we will not be able to get LinePos
  201      ; Line = Line1,
  202        LinePos = -1
  203      )
  204    },
  205    assert_location(H, M, Type, File, Line, file(File, Line, LinePos, Chars)).
  206
  207assert_location(H, M, Type, File, Line, From) -->
  208    ( {\+ have_extra_location(From, H, M, Type)}
  209    ->['$source_location'(File, Line):extra_location:loc_declaration(H, M, Type, From)]
  210    ; []
  211    ).
  212
  213/*
  214have_extra_location(file(File, Line, _, _), H, M, Type) :- !,
  215    extra_location(H, M, Type, From),
  216    from_to_file(From, File),
  217    from_to_line(From, Line).
  218have_extra_location(From, H, M, Type) :-
  219    extra_location(H, M, Type, From).
  220*/
  221
  222have_extra_location(From1, H, M, Type) :-
  223    extra_location(H, M, Type, From),
  224    subsumes_from(From1, From).
  225
  226system:term_expansion(Term, Pos, [Term|Clauses], Pos) :-
  227    record_location,
  228    source_location(File, Line),
  229    ( rl_tmp(File, Line, _)
  230    ->fail
  231    ; retractall(rl_tmp(_, _, _)),
  232      asserta(rl_tmp(File, Line, 0 )),
  233      record_extra_location(Term, Pos, Clauses, []),
  234      Clauses \= []
  235    ).
  236
  237redundant((_,_)).
  238redundant((_;_)).
  239redundant((_:_)).
  240redundant(true).
  241redundant(!).
  242
  243assert_position(G, Pos, T) :-
  244    '$current_source_module'(M),
  245    assert_position(G, M, T, Pos).
  246
  247:- public rl_goal_expansion/2.  248rl_goal_expansion(Goal, Pos) :-
  249    callable(Goal),
  250    \+ redundant(Goal),
  251    source_location(File, Line),
  252    ( rl_tmp(File, Line, Flag)
  253    ->Flag == 1
  254    ; true
  255    ),
  256    nb_current('$term', Term),
  257    memberchk(Term, [(:-_), []]),
  258    \+ clause(declaration_pos(Goal, _, _, _, _, _, _), _),
  259    \+ skip_record_decl(Goal),
  260    assert_position(Goal, Pos, goal),
  261    !.
  262
  263system:goal_expansion(Goal, Pos, _, _) :-
  264    record_location,
  265    rl_goal_expansion(Goal, Pos),
  266    fail