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(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(Declaration, _, M:PI,
  155                       term_position(_, _, _, _, [_, Pos])) -->
  156    !,
  157    assert_declaration_one(Declaration, M, PI, Pos).
  158assert_declaration_one(Declaration, M, F/A, Pos) -->
  159    { atom(F),
  160      integer(A)
  161    },
  162    !,
  163    {functor(H, F, A)},
  164    assert_position(H, M, Declaration, Pos).
  165assert_declaration_one(Declaration, M, F//A1, Pos) -->
  166    { atom(F),
  167      integer(A1)
  168    },
  169    !,
  170    { A is A1+2,
  171      functor(H, F, A)
  172    },
  173    assert_position(H, M, Declaration, Pos).
  174assert_declaration_one(Declaration, M, H, Pos) -->
  175    assert_position(H, M, Declaration, Pos).
  176
  177assert_reexport_declaration_2((F/A as G), U, Pos, M) -->
  178    {functor(H, G, A)},
  179    assert_position(H, M, reexport(U, [F/A as G]), Pos).
  180assert_reexport_declaration_2(F/A, U, Pos, M) -->
  181    {functor(H, F, A)},
  182    assert_position(H, M, reexport(U, [F/A]), Pos).
  183assert_reexport_declaration_2(op(_, _, _), _, _, _) --> [].
  184assert_reexport_declaration_2(except(_),   _, _, _) --> [].
  185
  186assert_position(H, M, Type, TermPos) :-
  187    assert_position(H, M, Type, TermPos, Clauses, []),
  188    compile_aux_clauses(Clauses).
  189
  190assert_position(H, M, Type, TermPos) -->
  191    { source_location(File, Line1),
  192      ( nonvar(TermPos)
  193      ->arg(1, TermPos, Chars),
  194        filepos_line(File, Chars, Line, LinePos)
  195        % Meld TermPos because later the source code will not be available and
  196        % therefore we will not be able to get LinePos
  197      ; Line = Line1,
  198        LinePos = -1
  199      )
  200    },
  201    assert_location(H, M, Type, File, Line, file(File, Line, LinePos, Chars)).
  202
  203assert_location(H, M, Type, File, Line, From) -->
  204    ( {\+ have_extra_location(From, H, M, Type)}
  205    ->['$source_location'(File, Line):extra_location:loc_declaration(H, M, Type, From)]
  206    ; []
  207    ).
  208
  209/*
  210have_extra_location(file(File, Line, _, _), H, M, Type) :- !,
  211    extra_location(H, M, Type, From),
  212    from_to_file(From, File),
  213    from_to_line(From, Line).
  214have_extra_location(From, H, M, Type) :-
  215    extra_location(H, M, Type, From).
  216*/
  217
  218have_extra_location(From1, H, M, Type) :-
  219    extra_location(H, M, Type, From),
  220    subsumes_from(From1, From).
  221
  222system:term_expansion(Term, Pos, [Term|Clauses], Pos) :-
  223    record_location,
  224    source_location(File, Line),
  225    ( rl_tmp(File, Line, _)
  226    ->fail
  227    ; retractall(rl_tmp(_, _, _)),
  228      asserta(rl_tmp(File, Line, 0 )),
  229      record_extra_location(Term, Pos, Clauses, []),
  230      Clauses \= []
  231    ).
  232
  233redundant((_,_)).
  234redundant((_;_)).
  235redundant((_:_)).
  236redundant(true).
  237redundant(!).
  238
  239assert_position(G, Pos, T) :-
  240    '$current_source_module'(M),
  241    assert_position(G, M, T, Pos).
  242
  243:- public rl_goal_expansion/2.  244rl_goal_expansion(Goal, Pos) :-
  245    callable(Goal),
  246    \+ redundant(Goal),
  247    source_location(File, Line),
  248    ( rl_tmp(File, Line, Flag)
  249    ->Flag == 1
  250    ; true
  251    ),
  252    b_getval('$term', Term),
  253    memberchk(Term, [(:-_), []]),
  254    \+ clause(declaration_pos(Goal, _, _, _, _, _, _), _),
  255    \+ skip_record_decl(Goal),
  256    assert_position(Goal, Pos, goal),
  257    !.
  258
  259system:goal_expansion(Goal, Pos, _, _) :-
  260    record_location,
  261    rl_goal_expansion(Goal, Pos),
  262    fail