1 ! Copyright (C) 2006, 2009 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 ]
24 : method ( class generic -- method/f )
25 "methods" word-prop at ;
27 : order ( generic -- seq )
28 "methods" word-prop keys sort-classes ;
30 : specific-method ( class generic -- method/f )
31 [ nip ] [ order min-class ] 2bi
32 dup [ swap method ] [ 2drop f ] if ;
34 GENERIC: effective-method ( generic -- method )
36 \ effective-method t "no-compile" set-word-prop
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 ( method -- quot )
48 next-method-quot-cache get [
49 [ "method-class" word-prop ]
51 "method-generic" word-prop
52 dup "combination" word-prop
53 ] bi next-method-quot*
56 ERROR: no-next-method method ;
58 : (call-next-method) ( method -- )
59 dup next-method-quot [ call ] [ no-next-method ] ?if ;
61 TUPLE: check-method class generic ;
63 : check-method ( class generic -- class generic )
64 2dup [ class? ] [ generic? ] bi* and [
65 \ check-method boa throw
68 : changed-generic ( class generic -- )
70 [ [ [ class-or ] when* ] change-at ] [ no-compilation-unit ] if* ;
72 : remake-generic ( generic -- )
73 dup outdated-generics get set-in-unit ;
75 : remake-generics ( -- )
76 outdated-generics get keys [ generic? ] filter [ make-generic ] each ;
78 : with-methods ( class generic quot -- )
79 [ drop changed-generic ]
80 [ [ "methods" word-prop ] dip call ]
81 [ drop remake-generic drop ]
84 : method-word-name ( class word -- string )
85 [ name>> ] bi@ "=>" glue ;
87 PREDICATE: method-body < word
88 "method-generic" word-prop >boolean ;
90 M: method-body stack-effect
91 "method-generic" word-prop stack-effect ;
93 M: method-body crossref?
94 "forgotten" word-prop not ;
96 : method-word-props ( class generic -- assoc )
102 : <method> ( class generic -- method )
104 [ method-word-props ] 2keep
105 method-word-name f <word>
108 : with-implementors ( class generic quot -- )
109 [ swap implementors-map get at ] dip call ; inline
111 : reveal-method ( method class generic -- )
112 [ [ conjoin ] with-implementors ]
113 [ [ set-at ] with-methods ]
116 : create-method ( class generic -- method )
117 2dup method dup [ 2nip dup reset-generic ] [
119 [ <method> dup ] 2keep
124 PREDICATE: default-method < word "default" word-prop ;
126 : <default-method> ( generic combination -- method )
127 [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
128 [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
130 : define-default-method ( generic combination -- )
131 dupd <default-method> "default-method" set-word-prop ;
133 ! Definition protocol
134 M: method-body definer
137 M: method-body forget*
138 dup "forgotten" word-prop [ drop ] [
140 dup default-method? [ drop ] [
142 [ "method-class" word-prop ]
143 [ "method-generic" word-prop ] bi
147 [ [ delete-at ] with-methods ]
148 [ [ delete-at ] with-implementors ] 2bi
153 [ call-next-method ] bi
156 M: sequence update-methods ( class seq -- )
158 [ changed-generic ] [ remake-generic drop ] 2bi
161 : define-generic ( word combination effect -- )
162 [ nip swap set-stack-effect ]
165 2dup [ "combination" word-prop ] dip = [ 2drop ] [
167 [ drop reset-generic ]
168 [ "combination" set-word-prop ]
169 [ drop H{ } clone "methods" set-word-prop ]
170 [ define-default-method ]
175 [ 2drop remake-generic ] 3tri ;
179 [ "default-method" word-prop , ]
180 [ "methods" word-prop values % ]
181 [ "engines" word-prop % ]
186 [ subwords forget-all ] [ call-next-method ] bi ;
188 M: class forget-methods
189 [ implementors ] [ [ swap method ] curry ] bi map forget-all ;