13
14:- module(metafile_referencer, [file_references_for_metacall/3, 15 file_references_for_call/3, 16 is_metaterm/3 17 ]). 18
19:- use_module(pdt_prolog_library(utils4modules)). 20
21:- use_module(library(lists)). 22
23:- dynamic user_defined_meta_pred/4. 24
26
27file_references_for_metacall(Module,MetaTerm,References):-
28 is_metaterm(Module,MetaTerm,MetaArgs),
29 length(MetaArgs,Length),
30 length(References,Length),
31 nth1(N,MetaArgs,MArg),
32 MArg=(ArgNr,Term),
33 file_references_for_term(Module, Term, FileSet),
34 nth1(N,References,(ArgNr,Term,FileSet)).
35
36
44is_metaterm(Module, Literal, MetaArguments) :-
45 \+(var(Literal)), !,
46 functor(Literal,Functor,Arity),
47 ( Module = user
48 -> visible_in_module(CurrentModule, Functor, Arity)
49 ; ( visible_in_module(Module,Functor,Arity),
50 CurrentModule = Module
51 )
52 ),
53 54 is_meta_pred(CurrentModule, Literal, MetaTerm),
55 Literal =.. [Functor|Args],
56 MetaTerm =.. [Functor|MetaArgs],
57 collect_meta_args(Args,MetaArgs, MetaArguments ).
58is_metaterm(Module, Literal, MetaArguments) :-
59 visible_in_module(Module,Functor,Arity),
60 functor(Literal,Functor,Arity),
61 62 is_meta_pred(Module, Literal, MetaTerm),
63 Literal =.. [Functor|Args],
64 MetaTerm =.. [Functor|MetaArgs],
65 collect_meta_args(Args,MetaArgs, MetaArguments ).
66
67
71is_meta_pred(Module, Literal, MetaTerm):- 72 predicate_property(Module:Literal,meta_predicate(MetaTerm)).
73is_meta_pred(Module, Literal, MetaTerm):-
74 functor(Literal, Functor, Arity),
75 user_defined_meta_pred(Functor, Arity, Module, MetaTerm).
76
86collect_meta_args(Args,MetaArgs, MetaArguments ) :-
87 bagof(
88 Meta,
89 extract_meta_argument(Args,MetaArgs, Meta),
90 MetaArguments
91 ).
92
(Args,MetaArgs, (N,NewArg) ) :-
94 nth1(N,MetaArgs,MArg),
95 nth1(N,Args,Arg),
96 additonal_parameters(MArg,Arg,NewArg).
97
104additonal_parameters(0,Arg,Arg):- !.
105additonal_parameters(N,Arg,Arg):-
106 integer(N),
107 var(Arg),!.
108additonal_parameters(N,Arg,NewArg) :-
109 integer(N),
110 Arg =.. [Functor | Params],
111 length(N_Elems,N),
112 append(Params,N_Elems,NewParams),
113 NewArg =.. [Functor | NewParams].
114
115
116file_references_for_call(Module, Term, [(Module, 'any')]):-
117 var(Term), !.
118file_references_for_call(Module, Term, FileSet):-
119 functor(Term,Name,Arity),
120 findall( ContextFile,
121 ( defined_in_module(Module,Name,Arity),
122 defined_in_file(Module,Name,Arity,_Nth,File,_Line),
123 ContextFile = (Module,File)
124 ),
125 Files
126 ),
127 not(Files = []), !,
128 list_to_set(Files,FileSet).
129file_references_for_call(Module, Term, FileSet):-
130 functor(Term,Name,Arity),
131 findall( ContextFile,
132 ( (Module,Name,Arity,DeclModule),
133 module_property(DeclModule,file(File)),
134 predicate_property(DeclModule:Term,dynamic),
135 ContextFile = (Module,File)
136 ),
137 Files
138 ),
139 not(Files = []), !,
140 list_to_set(Files,FileSet).
141file_references_for_call(Module, Term, [(Module, 'undefined')]):-
142 functor(Term,Name,Arity),
143 visible_in_module(Module,Name,Arity).
144
145
146
147
148:- dynamic(new_found_meta_predicate/1).
149
150find_unknown_meta_predicates:-
151 retractall(new_found_meta_predicate(_)),
152 current_predicate(RefModule:F/A), 153 functor(RefHead,F,A),
154 \+ predicate_property(RefModule:RefHead, built_in),
155 \+ predicate_property(RefModule:RefHead, autoload(_)),
156 nth_clause(RefModule:RefHead,_,Ref), 157 '$xr_member'(Ref, Module:Meta_Head), 158
159 functor(Meta_Head,Name,Arity),
160 known_meta_predicate(Module,Name,Arity,_Definition),
161
162
163 164 165 166 assert(new_found_meta_predicate(RefModule:F/A)),
167 fail.
168find_unknown_meta_predicates.
169
170
171
172
173
174
175
176
184known_meta_predicate(Module,Name,Arity,Definition):-
185 predicate_property(Module:Head,meta_predicate(Definition)),
186 functor(Head,Name,Arity),
187 visible_in_module(Module,Name,Arity)