/
prolog_xref.pl
2893 lines (2562 loc) · 90 KB
/
prolog_xref.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org/projects/xpce/
Copyright (c) 2006-2020, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(prolog_xref,
[ xref_source/1, % +Source
xref_source/2, % +Source, +Options
xref_called/3, % ?Source, ?Callable, ?By
xref_called/4, % ?Source, ?Callable, ?By, ?Cond
xref_called/5, % ?Source, ?Callable, ?By, ?Cond, ?Line
xref_defined/3, % ?Source. ?Callable, -How
xref_definition_line/2, % +How, -Line
xref_exported/2, % ?Source, ?Callable
xref_module/2, % ?Source, ?Module
xref_uses_file/3, % ?Source, ?Spec, ?Path
xref_op/2, % ?Source, ?Op
xref_prolog_flag/4, % ?Source, ?Flag, ?Value, ?Line
xref_comment/3, % ?Source, ?Title, ?Comment
xref_comment/4, % ?Source, ?Head, ?Summary, ?Comment
xref_mode/3, % ?Source, ?Mode, ?Det
xref_option/2, % ?Source, ?Option
xref_clean/1, % +Source
xref_current_source/1, % ?Source
xref_done/2, % +Source, -When
xref_built_in/1, % ?Callable
xref_source_file/3, % +Spec, -Path, +Source
xref_source_file/4, % +Spec, -Path, +Source, +Options
xref_public_list/3, % +File, +Src, +Options
xref_public_list/4, % +File, -Path, -Export, +Src
xref_public_list/6, % +File, -Path, -Module, -Export, -Meta, +Src
xref_public_list/7, % +File, -Path, -Module, -Export, -Public, -Meta, +Src
xref_meta/3, % +Source, +Goal, -Called
xref_meta/2, % +Goal, -Called
xref_hook/1, % ?Callable
% XPCE class references
xref_used_class/2, % ?Source, ?ClassName
xref_defined_class/3 % ?Source, ?ClassName, -How
]).
:- autoload(library(apply),[maplist/2,partition/4,maplist/3]).
:- autoload(library(debug),[debug/3]).
:- autoload(library(dialect),[expects_dialect/1]).
:- autoload(library(error),[must_be/2,instantiation_error/1]).
:- autoload(library(lists),[member/2,append/2,append/3,select/3]).
:- autoload(library(modules),[in_temporary_module/3]).
:- autoload(library(operators),[push_op/3]).
:- autoload(library(option),[option/2,option/3]).
:- autoload(library(ordsets),[ord_intersect/2,ord_intersection/3]).
:- autoload(library(prolog_source),
[ prolog_canonical_source/2,
prolog_open_source/2,
prolog_close_source/1,
prolog_read_source_term/4
]).
:- autoload(library(shlib),[current_foreign_library/2]).
:- autoload(library(solution_sequences),[distinct/2,limit/2]).
:- if(exists_source(library(pldoc))).
:- use_module(library(pldoc), []). % Must be loaded before doc_process
:- use_module(library(pldoc/doc_process)).
:- endif.
:- predicate_options(xref_source/2, 2,
[ silent(boolean),
module(atom),
register_called(oneof([all,non_iso,non_built_in])),
comments(oneof([store,collect,ignore])),
process_include(boolean)
]).
:- dynamic
called/5, % Head, Src, From, Cond, Line
(dynamic)/3, % Head, Src, Line
(thread_local)/3, % Head, Src, Line
(multifile)/3, % Head, Src, Line
(public)/3, % Head, Src, Line
defined/3, % Head, Src, Line
meta_goal/3, % Head, Called, Src
foreign/3, % Head, Src, Line
constraint/3, % Head, Src, Line
imported/3, % Head, Src, From
exported/2, % Head, Src
xmodule/2, % Module, Src
uses_file/3, % Spec, Src, Path
xop/2, % Src, Op
source/2, % Src, Time
used_class/2, % Name, Src
defined_class/5, % Name, Super, Summary, Src, Line
(mode)/2, % Mode, Src
xoption/2, % Src, Option
xflag/4, % Name, Value, Src, Line
module_comment/3, % Src, Title, Comment
pred_comment/4, % Head, Src, Summary, Comment
pred_comment_link/3, % Head, Src, HeadTo
pred_mode/3. % Head, Src, Det
:- create_prolog_flag(xref, false, [type(boolean)]).
/** <module> Prolog cross-referencer data collection
This library collects information on defined and used objects in Prolog
source files. Typically these are predicates, but we expect the library
to deal with other types of objects in the future. The library is a
building block for tools doing dependency tracking in applications.
Dependency tracking is useful to reveal the structure of an unknown
program or detect missing components at compile time, but also for
program transformation or minimising a program saved state by only
saving the reachable objects.
The library is exploited by two graphical tools in the SWI-Prolog
environment: the XPCE front-end started by gxref/0, and
library(prolog_colour), which exploits this library for its syntax
highlighting.
For all predicates described below, `Source` is the source that is
processed. This is normally a filename in any notation acceptable to the
file loading predicates (see load_files/2). Input handling is done by
the library(prolog_source), which may be hooked to process any source
that can be translated into a Prolog stream holding Prolog source text.
`Callable` is a callable term (see callable/1). Callables do not
carry a module qualifier unless the referred predicate is not in the
module defined by `Source`.
@bug meta_predicate/1 declarations take the module into consideration.
Predicates that are both available as meta-predicate and normal
(in different modules) are handled as meta-predicate in all
places.
@see Where this library analyses _source text_, library(prolog_codewalk)
may be used to analyse _loaded code_. The library(check) exploits
library(prolog_codewalk) to report on e.g., undefined
predicates.
*/
:- predicate_options(xref_source_file/4, 4,
[ file_type(oneof([txt,prolog,directory])),
silent(boolean)
]).
:- predicate_options(xref_public_list/3, 3,
[ path(-atom),
module(-atom),
exports(-list(any)),
public(-list(any)),
meta(-list(any)),
silent(boolean)
]).
/*******************************
* HOOKS *
*******************************/
%! prolog:called_by(+Goal, +Module, +Context, -Called) is semidet.
%
% True when Called is a list of callable terms called from Goal,
% handled by the predicate Module:Goal and executed in the context
% of the module Context. Elements of Called may be qualified. If
% not, they are called in the context of the module Context.
%! prolog:called_by(+Goal, -ListOfCalled)
%
% If this succeeds, the cross-referencer assumes Goal may call any
% of the goals in ListOfCalled. If this call fails, default
% meta-goal analysis is used to determine additional called goals.
%
% @deprecated New code should use prolog:called_by/4
%! prolog:meta_goal(+Goal, -Pattern)
%
% Define meta-predicates. See the examples in this file for
% details.
%! prolog:hook(Goal)
%
% True if Goal is a hook that is called spontaneously (e.g., from
% foreign code).
:- multifile
prolog:called_by/4, % +Goal, +Module, +Context, -Called
prolog:called_by/2, % +Goal, -Called
prolog:meta_goal/2, % +Goal, -Pattern
prolog:hook/1, % +Callable
prolog:generated_predicate/1, % :PI
prolog:no_autoload_module/1. % Module is not suitable for autoloading.
:- meta_predicate
prolog:generated_predicate(:).
:- dynamic
meta_goal/2.
:- meta_predicate
process_predicates(2, +, +).
/*******************************
* BUILT-INS *
*******************************/
%! hide_called(:Callable, +Src) is semidet.
%
% True when the cross-referencer should not include Callable as
% being called. This is determined by the option
% =register_called=.
hide_called(Callable, Src) :-
xoption(Src, register_called(Which)),
!,
mode_hide_called(Which, Callable).
hide_called(Callable, _) :-
mode_hide_called(non_built_in, Callable).
mode_hide_called(all, _) :- !, fail.
mode_hide_called(non_iso, _:Goal) :-
goal_name_arity(Goal, Name, Arity),
current_predicate(system:Name/Arity),
predicate_property(system:Goal, iso).
mode_hide_called(non_built_in, _:Goal) :-
goal_name_arity(Goal, Name, Arity),
current_predicate(system:Name/Arity),
predicate_property(system:Goal, built_in).
mode_hide_called(non_built_in, M:Goal) :-
goal_name_arity(Goal, Name, Arity),
current_predicate(M:Name/Arity),
predicate_property(M:Goal, built_in).
%! built_in_predicate(+Callable)
%
% True if Callable is a built-in
system_predicate(Goal) :-
goal_name_arity(Goal, Name, Arity),
current_predicate(system:Name/Arity), % avoid autoloading
predicate_property(system:Goal, built_in),
!.
/********************************
* TOPLEVEL *
********************************/
verbose(Src) :-
\+ xoption(Src, silent(true)).
:- thread_local
xref_input/2. % File, Stream
%! xref_source(+Source) is det.
%! xref_source(+Source, +Options) is det.
%
% Generate the cross-reference data for Source if not already
% done and the source is not modified. Checking for modifications
% is only done for files. Options processed:
%
% * silent(+Boolean)
% If =true= (default =false=), emit warning messages.
% * module(+Module)
% Define the initial context module to work in.
% * register_called(+Which)
% Determines which calls are registerd. Which is one of
% =all=, =non_iso= or =non_built_in=.
% * comments(+CommentHandling)
% How to handle comments. If =store=, comments are stored into
% the database as if the file was compiled. If =collect=,
% comments are entered to the xref database and made available
% through xref_mode/2 and xref_comment/4. If =ignore=,
% comments are simply ignored. Default is to =collect= comments.
% * process_include(+Boolean)
% Process the content of included files (default is `true`).
%
% @param Source File specification or XPCE buffer
xref_source(Source) :-
xref_source(Source, []).
xref_source(Source, Options) :-
prolog_canonical_source(Source, Src),
( last_modified(Source, Modified)
-> ( source(Src, Modified)
-> true
; xref_clean(Src),
assert(source(Src, Modified)),
do_xref(Src, Options)
)
; xref_clean(Src),
get_time(Now),
assert(source(Src, Now)),
do_xref(Src, Options)
).
do_xref(Src, Options) :-
must_be(list, Options),
setup_call_cleanup(
xref_setup(Src, In, Options, State),
collect(Src, Src, In, Options),
xref_cleanup(State)).
last_modified(Source, Modified) :-
prolog:xref_source_time(Source, Modified),
!.
last_modified(Source, Modified) :-
atom(Source),
\+ is_global_url(Source),
exists_file(Source),
time_file(Source, Modified).
is_global_url(File) :-
sub_atom(File, B, _, _, '://'),
!,
B > 1,
sub_atom(File, 0, B, _, Scheme),
atom_codes(Scheme, Codes),
maplist(between(0'a, 0'z), Codes).
xref_setup(Src, In, Options, state(In, Dialect, Xref, [SRef|HRefs])) :-
maplist(assert_option(Src), Options),
assert_default_options(Src),
current_prolog_flag(emulated_dialect, Dialect),
prolog_open_source(Src, In),
set_initial_mode(In, Options),
asserta(xref_input(Src, In), SRef),
set_xref(Xref),
( verbose(Src)
-> HRefs = []
; asserta((user:thread_message_hook(_,Level,_) :-
hide_message(Level)),
Ref),
HRefs = [Ref]
).
hide_message(warning).
hide_message(error).
hide_message(informational).
assert_option(_, Var) :-
var(Var),
!,
instantiation_error(Var).
assert_option(Src, silent(Boolean)) :-
!,
must_be(boolean, Boolean),
assert(xoption(Src, silent(Boolean))).
assert_option(Src, register_called(Which)) :-
!,
must_be(oneof([all,non_iso,non_built_in]), Which),
assert(xoption(Src, register_called(Which))).
assert_option(Src, comments(CommentHandling)) :-
!,
must_be(oneof([store,collect,ignore]), CommentHandling),
assert(xoption(Src, comments(CommentHandling))).
assert_option(Src, module(Module)) :-
!,
must_be(atom, Module),
assert(xoption(Src, module(Module))).
assert_option(Src, process_include(Boolean)) :-
!,
must_be(boolean, Boolean),
assert(xoption(Src, process_include(Boolean))).
assert_default_options(Src) :-
( xref_option_default(Opt),
generalise_term(Opt, Gen),
( xoption(Src, Gen)
-> true
; assertz(xoption(Src, Opt))
),
fail
; true
).
xref_option_default(silent(false)).
xref_option_default(register_called(non_built_in)).
xref_option_default(comments(collect)).
xref_option_default(process_include(true)).
%! xref_cleanup(+State) is det.
%
% Restore processing state according to the saved State.
xref_cleanup(state(In, Dialect, Xref, Refs)) :-
prolog_close_source(In),
set_prolog_flag(emulated_dialect, Dialect),
set_prolog_flag(xref, Xref),
maplist(erase, Refs).
set_xref(Xref) :-
current_prolog_flag(xref, Xref),
set_prolog_flag(xref, true).
%! set_initial_mode(+Stream, +Options) is det.
%
% Set the initial mode for processing this file in the
% cross-referencer. If the file is loaded, we use information from
% the previous load context, setting the appropriate module and
% dialect.
set_initial_mode(_Stream, Options) :-
option(module(Module), Options),
!,
'$set_source_module'(Module).
set_initial_mode(Stream, _) :-
stream_property(Stream, file_name(Path)),
source_file_property(Path, load_context(M, _, Opts)),
!,
'$set_source_module'(M),
( option(dialect(Dialect), Opts)
-> expects_dialect(Dialect)
; true
).
set_initial_mode(_, _) :-
'$set_source_module'(user).
%! xref_input_stream(-Stream) is det.
%
% Current input stream for cross-referencer.
xref_input_stream(Stream) :-
xref_input(_, Var),
!,
Stream = Var.
%! xref_push_op(Source, +Prec, +Type, :Name)
%
% Define operators into the default source module and register
% them to be undone by pop_operators/0.
xref_push_op(Src, P, T, N0) :-
'$current_source_module'(M0),
strip_module(M0:N0, M, N),
( is_list(N),
N \== []
-> maplist(push_op(Src, P, T, M), N)
; push_op(Src, P, T, M, N)
).
push_op(Src, P, T, M0, N0) :-
strip_module(M0:N0, M, N),
Name = M:N,
valid_op(op(P,T,Name)),
push_op(P, T, Name),
assert_op(Src, op(P,T,Name)),
debug(xref(op), ':- ~w.', [op(P,T,Name)]).
valid_op(op(P,T,M:N)) :-
atom(M),
valid_op_name(N),
integer(P),
between(0, 1200, P),
atom(T),
op_type(T).
valid_op_name(N) :-
atom(N),
!.
valid_op_name(N) :-
N == [].
op_type(xf).
op_type(yf).
op_type(fx).
op_type(fy).
op_type(xfx).
op_type(xfy).
op_type(yfx).
%! xref_set_prolog_flag(+Flag, +Value, +Src, +Line)
%
% Called when a directive sets a Prolog flag.
xref_set_prolog_flag(Flag, Value, Src, Line) :-
atom(Flag),
!,
assertz(xflag(Flag, Value, Src, Line)).
xref_set_prolog_flag(_, _, _, _).
%! xref_clean(+Source) is det.
%
% Reset the database for the given source.
xref_clean(Source) :-
prolog_canonical_source(Source, Src),
retractall(called(_, Src, _Origin, _Cond, _Line)),
retractall(dynamic(_, Src, Line)),
retractall(multifile(_, Src, Line)),
retractall(public(_, Src, Line)),
retractall(defined(_, Src, Line)),
retractall(meta_goal(_, _, Src)),
retractall(foreign(_, Src, Line)),
retractall(constraint(_, Src, Line)),
retractall(imported(_, Src, _From)),
retractall(exported(_, Src)),
retractall(uses_file(_, Src, _)),
retractall(xmodule(_, Src)),
retractall(xop(Src, _)),
retractall(xoption(Src, _)),
retractall(xflag(_Name, _Value, Src, Line)),
retractall(source(Src, _)),
retractall(used_class(_, Src)),
retractall(defined_class(_, _, _, Src, _)),
retractall(mode(_, Src)),
retractall(module_comment(Src, _, _)),
retractall(pred_comment(_, Src, _, _)),
retractall(pred_comment_link(_, Src, _)),
retractall(pred_mode(_, Src, _)).
/*******************************
* READ RESULTS *
*******************************/
%! xref_current_source(?Source)
%
% Check what sources have been analysed.
xref_current_source(Source) :-
source(Source, _Time).
%! xref_done(+Source, -Time) is det.
%
% Cross-reference executed at Time
xref_done(Source, Time) :-
prolog_canonical_source(Source, Src),
source(Src, Time).
%! xref_called(?Source, ?Called, ?By) is nondet.
%! xref_called(?Source, ?Called, ?By, ?Cond) is nondet.
%! xref_called(?Source, ?Called, ?By, ?Cond, ?Line) is nondet.
%
% True when By is called from Called in Source. Note that
% xref_called/3 and xref_called/4 use distinct/2 to return only
% distinct `Called-By` pairs. The xref_called/5 version may return
% duplicate `Called-By` if Called is called from multiple clauses in
% By, but at most one call per clause.
%
% @arg By is a head term or one of the reserved terms
% `'<directive>'(Line)` or `'<public>'(Line)`, indicating the call
% is from an (often initialization/1) directive or there is a public/1
% directive that claims the predicate is called from in some
% untractable way.
% @arg Cond is the (accumulated) condition as defined by
% ``:- if(Cond)`` under which the calling code is compiled.
% @arg Line is the _start line_ of the calling clause.
xref_called(Source, Called, By) :-
xref_called(Source, Called, By, _).
xref_called(Source, Called, By, Cond) :-
canonical_source(Source, Src),
distinct(Called-By, called(Called, Src, By, Cond, _)).
xref_called(Source, Called, By, Cond, Line) :-
canonical_source(Source, Src),
called(Called, Src, By, Cond, Line).
%! xref_defined(?Source, +Goal, ?How) is nondet.
%
% Test if Goal is accessible in Source. If this is the case, How
% specifies the reason why the predicate is accessible. Note that
% this predicate does not deal with built-in or global predicates,
% just locally defined and imported ones. How is one of of the
% terms below. Location is one of Line (an integer) or File:Line
% if the definition comes from an included (using :-
% include(File)) directive.
%
% * dynamic(Location)
% * thread_local(Location)
% * multifile(Location)
% * public(Location)
% * local(Location)
% * foreign(Location)
% * constraint(Location)
% * imported(From)
xref_defined(Source, Called, How) :-
nonvar(Source),
!,
canonical_source(Source, Src),
xref_defined2(How, Src, Called).
xref_defined(Source, Called, How) :-
xref_defined2(How, Src, Called),
canonical_source(Source, Src).
xref_defined2(dynamic(Line), Src, Called) :-
dynamic(Called, Src, Line).
xref_defined2(thread_local(Line), Src, Called) :-
thread_local(Called, Src, Line).
xref_defined2(multifile(Line), Src, Called) :-
multifile(Called, Src, Line).
xref_defined2(public(Line), Src, Called) :-
public(Called, Src, Line).
xref_defined2(local(Line), Src, Called) :-
defined(Called, Src, Line).
xref_defined2(foreign(Line), Src, Called) :-
foreign(Called, Src, Line).
xref_defined2(constraint(Line), Src, Called) :-
constraint(Called, Src, Line).
xref_defined2(imported(From), Src, Called) :-
imported(Called, Src, From).
%! xref_definition_line(+How, -Line)
%
% If the 3th argument of xref_defined contains line info, return
% this in Line.
xref_definition_line(local(Line), Line).
xref_definition_line(dynamic(Line), Line).
xref_definition_line(thread_local(Line), Line).
xref_definition_line(multifile(Line), Line).
xref_definition_line(public(Line), Line).
xref_definition_line(constraint(Line), Line).
xref_definition_line(foreign(Line), Line).
%! xref_exported(?Source, ?Head) is nondet.
%
% True when Source exports Head.
xref_exported(Source, Called) :-
prolog_canonical_source(Source, Src),
exported(Called, Src).
%! xref_module(?Source, ?Module) is nondet.
%
% True if Module is defined in Source.
xref_module(Source, Module) :-
nonvar(Source),
!,
prolog_canonical_source(Source, Src),
xmodule(Module, Src).
xref_module(Source, Module) :-
xmodule(Module, Src),
prolog_canonical_source(Source, Src).
%! xref_uses_file(?Source, ?Spec, ?Path) is nondet.
%
% True when Source tries to load a file using Spec.
%
% @param Spec is a specification for absolute_file_name/3
% @param Path is either an absolute file name of the target
% file or the atom =|<not_found>|=.
xref_uses_file(Source, Spec, Path) :-
prolog_canonical_source(Source, Src),
uses_file(Spec, Src, Path).
%! xref_op(?Source, Op) is nondet.
%
% Give the operators active inside the module. This is intended to
% setup the environment for incremental parsing of a term from the
% source-file.
%
% @param Op Term of the form op(Priority, Type, Name)
xref_op(Source, Op) :-
prolog_canonical_source(Source, Src),
xop(Src, Op).
%! xref_prolog_flag(?Source, ?Flag, ?Value, ?Line) is nondet.
%
% True when Flag is set to Value at Line in Source. This is
% intended to support incremental parsing of a term from the
% source-file.
xref_prolog_flag(Source, Flag, Value, Line) :-
prolog_canonical_source(Source, Src),
xflag(Flag, Value, Src, Line).
xref_built_in(Head) :-
system_predicate(Head).
xref_used_class(Source, Class) :-
prolog_canonical_source(Source, Src),
used_class(Class, Src).
xref_defined_class(Source, Class, local(Line, Super, Summary)) :-
prolog_canonical_source(Source, Src),
defined_class(Class, Super, Summary, Src, Line),
integer(Line),
!.
xref_defined_class(Source, Class, file(File)) :-
prolog_canonical_source(Source, Src),
defined_class(Class, _, _, Src, file(File)).
:- thread_local
current_cond/1,
source_line/1.
current_source_line(Line) :-
source_line(Var),
!,
Line = Var.
%! collect(+Source, +File, +Stream, +Options)
%
% Process data from Source. If File \== Source, we are processing
% an included file. Stream is the stream from shich we read the
% program.
collect(Src, File, In, Options) :-
( Src == File
-> SrcSpec = Line
; SrcSpec = (File:Line)
),
option(comments(CommentHandling), Options, collect),
( CommentHandling == ignore
-> CommentOptions = [],
Comments = []
; CommentHandling == store
-> CommentOptions = [ process_comment(true) ],
Comments = []
; CommentOptions = [ comments(Comments) ]
),
repeat,
catch(prolog_read_source_term(
In, Term, Expanded,
[ term_position(TermPos)
| CommentOptions
]),
E, report_syntax_error(E, Src, [])),
update_condition(Term),
stream_position_data(line_count, TermPos, Line),
setup_call_cleanup(
asserta(source_line(SrcSpec), Ref),
catch(process(Expanded, Comments, Term, TermPos, Src, EOF),
E, print_message(error, E)),
erase(Ref)),
EOF == true,
!.
report_syntax_error(E, _, _) :-
fatal_error(E),
throw(E).
report_syntax_error(_, _, Options) :-
option(silent(true), Options),
!,
fail.
report_syntax_error(E, Src, _Options) :-
( verbose(Src)
-> print_message(error, E)
; true
),
fail.
fatal_error(time_limit_exceeded).
fatal_error(error(resource_error(_),_)).
%! update_condition(+Term) is det.
%
% Update the condition under which the current code is compiled.
update_condition((:-Directive)) :-
!,
update_cond(Directive).
update_condition(_).
update_cond(if(Cond)) :-
!,
asserta(current_cond(Cond)).
update_cond(else) :-
retract(current_cond(C0)),
!,
assert(current_cond(\+C0)).
update_cond(elif(Cond)) :-
retract(current_cond(C0)),
!,
assert(current_cond((\+C0,Cond))).
update_cond(endif) :-
retract(current_cond(_)),
!.
update_cond(_).
%! current_condition(-Condition) is det.
%
% Condition is the current compilation condition as defined by the
% :- if/1 directive and friends.
current_condition(Condition) :-
\+ current_cond(_),
!,
Condition = true.
current_condition(Condition) :-
findall(C, current_cond(C), List),
list_to_conj(List, Condition).
list_to_conj([], true).
list_to_conj([C], C) :- !.
list_to_conj([H|T], (H,C)) :-
list_to_conj(T, C).
/*******************************
* PROCESS *
*******************************/
%! process(+Expanded, +Comments, +Term, +TermPos, +Src, -EOF) is det.
%
% Process a source term that has been subject to term expansion as
% well as its optional leading structured comments.
%
% @arg TermPos is the term position that describes the start of the
% term. We need this to find _leading_ comments.
% @arg EOF is unified with a boolean to indicate whether or not
% processing was stopped because `end_of_file` was processed.
process(Expanded, Comments, Term0, TermPos, Src, EOF) :-
is_list(Expanded), % term_expansion into list.
!,
( member(Term, Expanded),
process(Term, Term0, Src),
Term == end_of_file
-> EOF = true
; EOF = false
),
xref_comments(Comments, TermPos, Src).
process(end_of_file, _, _, _, _, true) :-
!.
process(Term, Comments, Term0, TermPos, Src, false) :-
process(Term, Term0, Src),
xref_comments(Comments, TermPos, Src).
%! process(+Term, +Term0, +Src) is det.
process(_, Term0, _) :-
ignore_raw_term(Term0),
!.
process(Term, _Term0, Src) :-
process(Term, Src).
ignore_raw_term((:- predicate_options(_,_,_))).
%! process(+Term, +Src) is det.
process(Var, _) :-
var(Var),
!. % Warn?
process(end_of_file, _) :- !.
process((:- Directive), Src) :-
!,
process_directive(Directive, Src),
!.
process((?- Directive), Src) :-
!,
process_directive(Directive, Src),
!.
process((Head :- Body), Src) :-
!,
assert_defined(Src, Head),
process_body(Body, Head, Src).
process((Left => Body), Src) :-
!,
( nonvar(Left),
Left = (Head, Guard)
-> assert_defined(Src, Head),
process_body(Guard, Head, Src),
process_body(Body, Head, Src)
; assert_defined(Src, Left),
process_body(Body, Left, Src)
).
process(?=>(Head, Body), Src) :-
!,
assert_defined(Src, Head),
process_body(Body, Head, Src).
process('$source_location'(_File, _Line):Clause, Src) :-
!,
process(Clause, Src).
process(Term, Src) :-
process_chr(Term, Src),
!.
process(M:(Head :- Body), Src) :-
!,
process((M:Head :- M:Body), Src).
process(Head, Src) :-
assert_defined(Src, Head).
/*******************************
* COMMENTS *
*******************************/
%! xref_comments(+Comments, +FilePos, +Src) is det.
xref_comments([], _Pos, _Src).
:- if(current_predicate(parse_comment/3)).
xref_comments([Pos-Comment|T], TermPos, Src) :-
( Pos @> TermPos % comments inside term
-> true
; stream_position_data(line_count, Pos, Line),
FilePos = Src:Line,
( parse_comment(Comment, FilePos, Parsed)
-> assert_comments(Parsed, Src)
; true
),
xref_comments(T, TermPos, Src)
).
assert_comments([], _).
assert_comments([H|T], Src) :-
assert_comment(H, Src),
assert_comments(T, Src).
assert_comment(section(_Id, Title, Comment), Src) :-
assertz(module_comment(Src, Title, Comment)).
assert_comment(predicate(PI, Summary, Comment), Src) :-
pi_to_head(PI, Src, Head),
assertz(pred_comment(Head, Src, Summary, Comment)).
assert_comment(link(PI, PITo), Src) :-
pi_to_head(PI, Src, Head),
pi_to_head(PITo, Src, HeadTo),
assertz(pred_comment_link(Head, Src, HeadTo)).
assert_comment(mode(Head, Det), Src) :-
assertz(pred_mode(Head, Src, Det)).
pi_to_head(PI, Src, Head) :-
pi_to_head(PI, Head0),
( Head0 = _:_
-> strip_module(Head0, M, Plain),
( xmodule(M, Src)
-> Head = Plain
; Head = M:Plain
)
; Head = Head0
).
:- endif.
%! xref_comment(?Source, ?Title, ?Comment) is nondet.
%
% Is true when Source has a section comment with Title and Comment
xref_comment(Source, Title, Comment) :-
canonical_source(Source, Src),
module_comment(Src, Title, Comment).
%! xref_comment(?Source, ?Head, ?Summary, ?Comment) is nondet.
%
% Is true when Head in Source has the given PlDoc comment.
xref_comment(Source, Head, Summary, Comment) :-
canonical_source(Source, Src),
( pred_comment(Head, Src, Summary, Comment)
; pred_comment_link(Head, Src, HeadTo),
pred_comment(HeadTo, Src, Summary, Comment)
).
%! xref_mode(?Source, ?Mode, ?Det) is nondet.
%
% Is true when Source provides a predicate with Mode and
% determinism.
xref_mode(Source, Mode, Det) :-
canonical_source(Source, Src),
pred_mode(Mode, Src, Det).
%! xref_option(?Source, ?Option) is nondet.
%
% True when Source was processed using Option. Options are defined
% with xref_source/2.
xref_option(Source, Option) :-
canonical_source(Source, Src),
xoption(Src, Option).
/********************************