1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors words kernel sequences namespaces 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 ]
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 generic -- method/f )
34 tuck order min-class dup [ swap method ] [ 2drop f ] if ;
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 -- method/f )
43 [ next-method-class ] keep method ;
45 GENERIC: next-method-quot* ( class generic combination -- 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 2dup [ class? ] [ generic? ] bi* and [
57 \ check-method boa throw
60 : with-methods ( class generic quot -- )
61 [ drop changed-generic ]
62 [ [ "methods" word-prop ] dip call ]
63 [ drop make-generic drop ]
66 : method-word-name ( class word -- string )
67 [ name>> ] bi@ "=>" swap 3append ;
69 PREDICATE: method-body < word
70 "method-generic" word-prop >boolean ;
72 M: method-body inline?
73 "method-generic" word-prop inline? ;
75 M: method-body stack-effect
76 "method-generic" word-prop stack-effect ;
78 M: method-body crossref?
79 "forgotten" word-prop not ;
81 : method-word-props ( class generic -- assoc )
87 : <method> ( class generic -- method )
89 [ method-word-props ] 2keep
90 method-word-name f <word>
93 : with-implementors ( class generic quot -- )
94 [ swap implementors-map get at ] dip call ; inline
96 : reveal-method ( method class generic -- )
97 [ [ conjoin ] with-implementors ]
98 [ [ set-at ] with-methods ]
101 : create-method ( class generic -- method )
105 drop [ <method> dup ] 2keep reveal-method
108 PREDICATE: default-method < word "default" word-prop ;
110 M: default-method irrelevant? drop t ;
112 : <default-method> ( generic combination -- method )
113 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
114 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
116 : define-default-method ( generic combination -- )
117 dupd <default-method> "default-method" set-word-prop ;
119 ! Definition protocol
121 dup first2 method [ ] [ second ] ?if where ;
123 M: method-spec set-where
124 first2 method set-where ;
126 M: method-spec definer
127 first2 method definer ;
129 M: method-spec definition
130 first2 method definition ;
132 M: method-spec forget*
133 first2 method [ forgotten-definition ] [ forget* ] bi ;
135 M: method-spec smart-usage
138 M: method-body definer
141 M: method-body forget*
142 dup "forgotten" word-prop [ drop ] [
144 dup default-method? [ drop ] [
146 [ "method-class" word-prop ]
147 [ "method-generic" word-prop ] bi
151 [ [ delete-at ] with-methods ]
152 [ [ delete-at ] with-implementors ]
157 [ call-next-method ] bi
160 M: method-body smart-usage
161 "method-generic" word-prop smart-usage ;
163 M: sequence update-methods ( class seq -- )
165 [ changed-generic ] [ make-generic drop ] 2bi
168 : define-generic ( word combination -- )
169 over "combination" word-prop over = [ drop ] [
170 2dup "combination" set-word-prop
171 over "methods" word-prop values forget-all
172 over H{ } clone "methods" set-word-prop
173 dupd define-default-method
178 [ "default-method" word-prop , ]
179 [ "methods" word-prop values % ]
180 [ "engines" word-prop % ]
185 [ subwords forget-all ] [ call-next-method ] bi ;
187 : xref-generics ( -- )
188 all-words [ subwords [ xref ] each ] each ;