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 [ nip ] [ order min-class ] 2bi
40 dup [ swap method ] [ 2drop f ] if ;
42 GENERIC: effective-method ( generic -- method )
44 : next-method-class ( class generic -- class/f )
45 order [ class<= ] with filter reverse dup length 1 =
46 [ drop f ] [ second ] if ;
48 : next-method ( class generic -- method/f )
49 [ next-method-class ] keep method ;
51 GENERIC: next-method-quot* ( class generic combination -- quot )
53 : next-method-quot ( method -- quot )
54 next-method-quot-cache get [
55 [ "method-class" word-prop ]
57 "method-generic" word-prop
58 dup "combination" word-prop
59 ] bi next-method-quot*
62 ERROR: no-next-method method ;
64 : (call-next-method) ( method -- )
65 dup next-method-quot [ call ] [ no-next-method ] ?if ;
67 TUPLE: check-method class generic ;
69 : check-method ( class generic -- class generic )
70 2dup [ class? ] [ generic? ] bi* and [
71 \ check-method boa throw
74 : with-methods ( class generic quot -- )
75 [ drop changed-generic ]
76 [ [ "methods" word-prop ] dip call ]
77 [ drop remake-generic drop ]
80 : method-word-name ( class word -- string )
81 [ name>> ] bi@ "=>" glue ;
83 PREDICATE: method-body < word
84 "method-generic" word-prop >boolean ;
86 M: method-body stack-effect
87 "method-generic" word-prop stack-effect ;
89 M: method-body crossref?
90 "forgotten" word-prop not ;
92 : method-word-props ( class generic -- assoc )
98 : <method> ( class generic -- method )
100 [ method-word-props ] 2keep
101 method-word-name f <word>
104 : with-implementors ( class generic quot -- )
105 [ swap implementors-map get at ] dip call ; inline
107 : reveal-method ( method class generic -- )
108 [ [ conjoin ] with-implementors ]
109 [ [ set-at ] with-methods ]
112 : create-method ( class generic -- method )
113 2dup method dup [ 2nip ] [
115 [ <method> dup ] 2keep
120 PREDICATE: default-method < word "default" word-prop ;
122 M: default-method irrelevant? drop t ;
124 : <default-method> ( generic combination -- method )
125 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
126 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
128 : define-default-method ( generic combination -- )
129 dupd <default-method> "default-method" set-word-prop ;
131 ! Definition protocol
133 dup first2 method [ ] [ second ] ?if where ;
135 M: method-spec set-where
136 first2 method set-where ;
138 M: method-spec definer
139 first2 method definer ;
141 M: method-spec definition
142 first2 method definition ;
144 M: method-spec forget*
145 first2 method [ forgotten-definition ] [ forget* ] bi ;
147 M: method-spec smart-usage
150 M: method-body definer
153 M: method-body forget*
154 dup "forgotten" word-prop [ drop ] [
156 dup default-method? [ drop ] [
158 [ "method-class" word-prop ]
159 [ "method-generic" word-prop ] bi
163 [ [ delete-at ] with-methods ]
164 [ [ delete-at ] with-implementors ] 2bi
169 [ call-next-method ] bi
172 M: method-body smart-usage
173 "method-generic" word-prop smart-usage ;
175 M: sequence update-methods ( class seq -- )
177 [ changed-generic ] [ remake-generic drop ] 2bi
180 : define-generic ( word combination -- )
181 over "combination" word-prop over = [ drop ] [
182 2dup "combination" set-word-prop
183 over "methods" word-prop values forget-all
184 over H{ } clone "methods" set-word-prop
185 dupd define-default-method
186 ] if remake-generic ;
190 [ "default-method" word-prop , ]
191 [ "methods" word-prop values % ]
192 [ "engines" word-prop % ]
197 [ subwords forget-all ] [ call-next-method ] bi ;
199 : xref-generics ( -- )
200 all-words [ subwords [ xref ] each ] each ;