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 ;
8 ! Method combination protocol
9 GENERIC: perform-combination ( word combination -- )
11 GENERIC: make-default-method ( generic combination -- method )
13 PREDICATE: generic < word
14 "combination" word-prop >boolean ;
16 M: generic definition drop f ;
18 : make-generic ( word -- )
19 [ { "unannotated-def" } reset-props ]
20 [ dup "combination" word-prop perform-combination ]
23 : method ( class generic -- method/f )
24 "methods" word-prop at ;
26 PREDICATE: method-spec < pair
27 first2 generic? swap class? and ;
29 : order ( generic -- seq )
30 "methods" word-prop keys sort-classes ;
32 : specific-method ( class word -- class )
35 GENERIC: effective-method ( ... generic -- method )
37 : next-method-class ( class generic -- class/f )
38 order [ class<= ] with filter reverse dup length 1 =
39 [ drop f ] [ second ] if ;
41 : next-method ( class generic -- class/f )
42 [ next-method-class ] keep method ;
44 GENERIC: next-method-quot* ( class generic -- quot )
46 : next-method-quot ( class generic -- quot )
47 dup "combination" word-prop next-method-quot* ;
49 : (call-next-method) ( class generic -- )
50 next-method-quot call ;
52 TUPLE: check-method class generic ;
54 : check-method ( class generic -- class generic )
55 over class? over generic? and [
56 \ check-method boa throw
59 : affected-methods ( class generic -- seq )
60 "methods" word-prop swap
61 [ nip classes-intersect? ] curry assoc-filter
64 : update-generic ( class generic -- )
65 [ affected-methods [ +called+ changed-definition ] each ]
69 : with-methods ( class generic quot -- )
70 [ [ "methods" word-prop ] dip call ]
71 [ drop update-generic ] 3bi ;
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?
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 : reveal-method ( method class generic -- )
99 [ set-at ] with-methods ;
101 : create-method ( class generic -- method )
105 drop [ <method> dup ] 2keep reveal-method
108 : <default-method> ( generic combination -- method )
109 object bootstrap-word pick <method>
110 [ -rot make-default-method define ] keep ;
112 : define-default-method ( generic combination -- )
113 dupd <default-method> "default-method" set-word-prop ;
115 ! Definition protocol
117 dup first2 method [ ] [ second ] ?if where ;
119 M: method-spec set-where
120 first2 method set-where ;
122 M: method-spec definer
123 first2 method definer ;
125 M: method-spec definition
126 first2 method definition ;
128 M: method-spec forget*
129 first2 method forget* ;
131 M: method-spec smart-usage
134 M: method-body definer
137 M: method-body forget*
138 dup "forgotten" word-prop [ drop ] [
141 [ "method-class" word-prop ]
142 [ "method-generic" word-prop ] tri
144 [ delete-at ] with-methods
148 [ t "forgotten" set-word-prop ] bi
151 M: method-body smart-usage
152 "method-generic" word-prop smart-usage ;
154 GENERIC: implementors ( class/classes -- seq )
156 M: class implementors
157 all-words [ "methods" word-prop key? ] with filter ;
159 M: assoc implementors
161 "methods" word-prop keys
162 swap [ key? ] curry contains?
165 : forget-methods ( class -- )
166 [ implementors ] [ [ swap 2array ] curry ] bi map forget-all ;
168 M: class forget* ( class -- )
178 [ call-next-method ] bi ;
180 M: assoc update-methods ( class assoc -- )
181 implementors [ update-generic ] with each ;
183 : define-generic ( word combination -- )
184 over "combination" word-prop over = [
187 2dup "combination" set-word-prop
188 over H{ } clone "methods" set-word-prop
189 dupd define-default-method
195 [ "default-method" word-prop , ]
196 [ "methods" word-prop values % ]
197 [ "engines" word-prop % ]
202 [ subwords forget-all ] [ call-next-method ] bi ;
204 : xref-generics ( -- )
205 all-words [ subwords [ xref ] each ] each ;