1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: words kernel sequences namespaces assocs hashtables
4 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 ]
24 : method ( class generic -- method/f )
25 "methods" word-prop at ;
27 PREDICATE: method-spec < pair
28 first2 generic? swap class? and ;
30 : order ( generic -- seq )
31 "methods" word-prop keys sort-classes ;
33 : specific-method ( class word -- class )
36 GENERIC: effective-method ( ... generic -- method )
38 : next-method-class ( class generic -- class/f )
39 order [ class<= ] with filter reverse dup length 1 =
40 [ drop f ] [ second ] if ;
42 : next-method ( class generic -- class/f )
43 [ next-method-class ] keep method ;
45 GENERIC: next-method-quot* ( class generic -- quot )
47 : next-method-quot ( class generic -- quot )
48 dup "combination" word-prop next-method-quot* ;
50 : (call-next-method) ( class generic -- )
51 next-method-quot call ;
53 TUPLE: check-method class generic ;
55 : check-method ( class generic -- class generic )
56 over class? over generic? and [
57 \ check-method boa throw
60 : affected-methods ( class generic -- seq )
61 "methods" word-prop swap
62 [ nip [ classes-intersect? ] [ class<= ] 2bi or ] curry assoc-filter
65 : update-generic ( class generic -- )
66 affected-methods [ +called+ changed-definition ] each ;
68 : with-methods ( class generic quot -- )
69 [ drop update-generic ]
70 [ [ "methods" word-prop ] dip call ]
71 [ drop make-generic drop ]
74 : method-word-name ( class word -- string )
75 word-name "/" rot word-name 3append ;
77 PREDICATE: method-body < word
78 "method-generic" word-prop >boolean ;
80 M: method-body stack-effect
81 "method-generic" word-prop stack-effect ;
83 M: method-body crossref?
84 "forgotten" word-prop not ;
86 : method-word-props ( class generic -- assoc )
92 : <method> ( class generic -- method )
94 [ method-word-props ] 2keep
95 method-word-name f <word>
96 [ set-word-props ] keep ;
98 : with-implementors ( class generic quot -- )
99 [ swap implementors-map get at ] dip call ; inline
101 : reveal-method ( method class generic -- )
102 [ [ conjoin ] with-implementors ]
103 [ [ set-at ] with-methods ]
106 : create-method ( class generic -- method )
110 drop [ <method> dup ] 2keep reveal-method
113 : <default-method> ( generic combination -- method )
114 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
115 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
117 : define-default-method ( generic combination -- )
118 dupd <default-method> "default-method" set-word-prop ;
120 ! Definition protocol
122 dup first2 method [ ] [ second ] ?if where ;
124 M: method-spec set-where
125 first2 method set-where ;
127 M: method-spec definer
128 first2 method definer ;
130 M: method-spec definition
131 first2 method definition ;
133 M: method-spec forget*
134 first2 method forget* ;
136 M: method-spec smart-usage
139 M: method-body definer
142 M: method-body forget*
143 dup "forgotten" word-prop [ drop ] [
145 dup "default" word-prop [ drop ] [
147 [ "method-class" word-prop ]
148 [ "method-generic" word-prop ] bi
152 [ [ delete-at ] with-methods ]
153 [ [ delete-at ] with-implementors ]
158 [ call-next-method ] bi
161 M: method-body smart-usage
162 "method-generic" word-prop smart-usage ;
164 M: sequence update-methods ( class seq -- )
166 [ update-generic ] [ make-generic drop ] 2bi
169 : define-generic ( word combination -- )
170 over "combination" word-prop over = [
173 2dup "combination" set-word-prop
174 over "methods" word-prop values forget-all
175 over H{ } clone "methods" set-word-prop
176 dupd define-default-method
182 [ "default-method" word-prop , ]
183 [ "methods" word-prop values % ]
184 [ "engines" word-prop % ]
189 [ subwords forget-all ] [ call-next-method ] bi ;
191 : xref-generics ( -- )
192 all-words [ subwords [ xref ] each ] each ;