View source with raw comments or as raw
    1/*  Part of XPCE --- The SWI-Prolog GUI toolkit
    2
    3    Author:        Jan Wielemaker and Anjo Anjewierden
    4    E-mail:        jan@swi.psy.uva.nl
    5    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    6    Copyright (c)  1999-2011, University of Amsterdam
    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(pce_portray, []).   36:- use_module(pce_boot(pce_principal)).   37
   38:- multifile
   39    user:portray/1,
   40    user:prolog_list_goal/1,
   41    user:prolog_predicate_name/2,
   42    user:prolog_clause_name/2.   43:- dynamic
   44    user:portray/1.   45
   46/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   47XPCE portray rules. These rules print object references indicating their
   48class-name and the goal to the implementation of methods more readable
   49- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
   50
   51user:portray(Obj) :-
   52    Obj = @Ref,
   53    object(Obj),
   54    !,
   55    (   send(Obj, '_instance_of', var)
   56    ->  get(Obj, '_value', Value),
   57        format('@~w(= ~p)', [Ref, Value])
   58    ;   get(Obj, '_class_name', CN),
   59        format('@~w/~w', [Ref, CN])
   60    ).
   61user:portray(pce_principal:send_implementation(Id, Args, Receiver)) :-
   62    object(Receiver),
   63    !,
   64    method_from_id(Id, (Class->_Method)),
   65    format('Send-method on ~p: ~w->~p', [Receiver, Class, Args]).
   66user:portray(pce_principal:get_implementation(Id, Args, Receiver, RVal)) :-
   67    object(Receiver),
   68    !,
   69    method_from_id(Id, <-(Class, _Method)),
   70    format('Get-method on ~p: ~w<-~p --> ~p',
   71           [Receiver, Class, Args, RVal]).
   72
   73%       user:prolog_list_goal(:Goal)
   74%
   75%       Called from the SWI-Prolog debugger 'L' command to show the
   76%       relevant sourcecode given the current Goal.
   77
   78user:prolog_list_goal(pce_principal:send_implementation(Id, _Args, _Ref)) :-
   79    !,
   80    (   Head = pce_principal:send_implementation(Id, Args, Ref),
   81        method_from_id(Id, ->(Class, Sel)),
   82        clause(Head, Body)
   83    ->  format('~N~n% XPCE Method ~w->~w:~n~n', [Class, Sel]),
   84        portray_clause(((Ref->Args) :- Body)),
   85        nl
   86    ;   format('No XPCE method implementation for id=~p~n', [Id])
   87    ).
   88user:prolog_list_goal(pce_principal:get_implementation(Id, _Args, _Ref, _Rval)) :-
   89    !,
   90    (   Head = pce_principal:get_implementation(Id, Args, Ref, Rval),
   91        method_from_id(Id, <-(Class, Sel)),
   92        clause(Head, Body)
   93    ->  format('~N~n% XPCE Method ~w<-~w:~n~n', [Class, Sel]),
   94        portray_clause(((Rval = <-(Ref,Args)) :- Body)),
   95        nl
   96    ;   format('No XPCE method implementation for id=~p~n', [Id])
   97    ).
 user:prolog_predicate_name(:Goal, -Name:atom) is semidet
Hook used by the Prolog graphical tracer to display the frames in the stack.
  104user:prolog_predicate_name(pce_principal:send_implementation(Id0, _, _),
  105                           Id) :-
  106    method_from_id(Id0, SG),
  107    atom_from_method(SG, Id).
  108user:prolog_predicate_name(pce_principal:get_implementation(Id0, _, _, _),
  109                           Id) :-
  110    method_from_id(Id0, SG),
  111    atom_from_method(SG, Id).
 user:prolog_clause_name(+ClauseRef, -Name)
Translate the reference to a method-clause into the corresponding method.
  118user:prolog_clause_name(Ref, Name) :-
  119    clause(Head, _, Ref),
  120    user:prolog_predicate_name(Head, Name).
  121
  122
  123                 /*******************************
  124                 *             UTIL             *
  125                 *******************************/
  126
  127%       Get the type, class and selector from a method identifier.
  128%       Should we cache this info for proper performance on the tracer?
  129
  130method_from_id(Id, Method) :-
  131    compound(Id),
  132    !,
  133    arg(1, Id, Id2),
  134    method_from_id(Id2, Method).
  135method_from_id(Id, ->(Class, Selector)) :-
  136    pce_principal:pce_lazy_send_method(Selector, Class, Binder),
  137    arg(1, Binder, Id),
  138    !.
  139method_from_id(Id, <-(Class, Selector)) :-
  140    pce_principal:pce_lazy_get_method(Selector, Class, Binder),
  141    arg(1, Binder, Id).
  142
  143atom_from_method(->(Class, Selector), Atom) :-
  144    atomic_list_concat([Class, (->), Selector], Atom).
  145atom_from_method(<-(Class, Selector), Atom) :-
  146    atomic_list_concat([Class, (<-), Selector], Atom)