1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words kernel sequences namespaces make assocs
4 hashtables definitions kernel.private classes classes.private
5 classes.algebra quotations arrays vocabs effects combinators
9 ! Method combination protocol
10 GENERIC: perform-combination ( word combination -- )
12 GENERIC: make-default-method ( generic combination -- method )
14 PREDICATE: generic < word
15 "combination" word-prop >boolean ;
17 M: generic definition drop f ;
19 : make-generic ( word -- )
20 [ { "unannotated-def" } reset-props ]
21 [ dup "combination" word-prop perform-combination ]
25 remake-generics get keys
26 [ generic? ] filter [ make-generic ] each
27 ] remake-generics-hook set-global
29 : method ( class generic -- method/f )
30 "methods" word-prop at ;
32 PREDICATE: method-spec < pair
33 first2 generic? swap class? and ;
35 : order ( generic -- seq )
36 "methods" word-prop keys sort-classes ;
38 : specific-method ( class generic -- method/f )
39 tuck order min-class dup [ swap method ] [ 2drop f ] if ;
41 GENERIC: effective-method ( generic -- method )
43 : next-method-class ( class generic -- class/f )
44 order [ class<= ] with filter reverse dup length 1 =
45 [ drop f ] [ second ] if ;
47 : next-method ( class generic -- method/f )
48 [ next-method-class ] keep method ;
50 GENERIC: next-method-quot* ( class generic combination -- quot )
52 : next-method-quot ( method -- quot )
53 next-method-quot-cache get [
54 [ "method-class" word-prop ]
56 "method-generic" word-prop
57 dup "combination" word-prop
58 ] bi next-method-quot*
61 ERROR: no-next-method method ;
63 : (call-next-method) ( method -- )
64 dup next-method-quot [ call ] [ no-next-method ] ?if ;
66 TUPLE: check-method class generic ;
68 : check-method ( class generic -- class generic )
69 2dup [ class? ] [ generic? ] bi* and [
70 \ check-method boa throw
73 : with-methods ( class generic quot -- )
74 [ drop changed-generic ]
75 [ [ "methods" word-prop ] dip call ]
76 [ drop remake-generic drop ]
79 : method-word-name ( class word -- string )
80 [ name>> ] bi@ "=>" glue ;
82 PREDICATE: method-body < word
83 "method-generic" word-prop >boolean ;
85 M: method-body stack-effect
86 "method-generic" word-prop stack-effect ;
88 M: method-body crossref?
89 "forgotten" word-prop not ;
91 : method-word-props ( class generic -- assoc )
97 : <method> ( class generic -- method )
99 [ method-word-props ] 2keep
100 method-word-name f <word>
103 : with-implementors ( class generic quot -- )
104 [ swap implementors-map get at ] dip call ; inline
106 : reveal-method ( method class generic -- )
107 [ [ conjoin ] with-implementors ]
108 [ [ set-at ] with-methods ]
111 : create-method ( class generic -- method )
112 2dup method dup [ 2nip ] [
114 [ <method> dup ] 2keep
119 PREDICATE: default-method < word "default" word-prop ;
121 M: default-method irrelevant? drop t ;
123 : <default-method> ( generic combination -- method )
124 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
125 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
127 : define-default-method ( generic combination -- )
128 dupd <default-method> "default-method" set-word-prop ;
130 ! Definition protocol
132 dup first2 method [ ] [ second ] ?if where ;
134 M: method-spec set-where
135 first2 method set-where ;
137 M: method-spec definer
138 first2 method definer ;
140 M: method-spec definition
141 first2 method definition ;
143 M: method-spec forget*
144 first2 method [ forgotten-definition ] [ forget* ] bi ;
146 M: method-spec smart-usage
149 M: method-body definer
152 M: method-body forget*
153 dup "forgotten" word-prop [ drop ] [
155 dup default-method? [ drop ] [
157 [ "method-class" word-prop ]
158 [ "method-generic" word-prop ] bi
162 [ [ delete-at ] with-methods ]
163 [ [ delete-at ] with-implementors ] 2bi
168 [ call-next-method ] bi
171 M: method-body smart-usage
172 "method-generic" word-prop smart-usage ;
174 M: sequence update-methods ( class seq -- )
176 [ changed-generic ] [ remake-generic drop ] 2bi
179 : define-generic ( word combination -- )
180 over "combination" word-prop over = [ drop ] [
181 2dup "combination" set-word-prop
182 over "methods" word-prop values forget-all
183 over H{ } clone "methods" set-word-prop
184 dupd define-default-method
185 ] if remake-generic ;
189 [ "default-method" word-prop , ]
190 [ "methods" word-prop values % ]
191 [ "engines" word-prop % ]
196 [ subwords forget-all ] [ call-next-method ] bi ;
198 : xref-generics ( -- )
199 all-words [ subwords [ xref ] each ] each ;