]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / core / generic / generic.factor
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
6 sets compiler.units ;
7 IN: generic
8
9 ! Method combination protocol
10 GENERIC: perform-combination ( word combination -- )
11
12 GENERIC: make-default-method ( generic combination -- method )
13
14 PREDICATE: generic < word
15     "combination" word-prop >boolean ;
16
17 M: generic definition drop f ;
18
19 : make-generic ( word -- )
20     [ { "unannotated-def" } reset-props ]
21     [ dup "combination" word-prop perform-combination ]
22     bi ;
23
24 [
25     remake-generics get keys
26     [ generic? ] filter [ make-generic ] each
27 ] remake-generics-hook set-global
28
29 : method ( class generic -- method/f )
30     "methods" word-prop at ;
31
32 PREDICATE: method-spec < pair
33     first2 generic? swap class? and ;
34
35 : order ( generic -- seq )
36     "methods" word-prop keys sort-classes ;
37
38 : specific-method ( class generic -- method/f )
39     tuck order min-class dup [ swap method ] [ 2drop f ] if ;
40
41 GENERIC: effective-method ( generic -- method )
42
43 : next-method-class ( class generic -- class/f )
44     order [ class<= ] with filter reverse dup length 1 =
45     [ drop f ] [ second ] if ;
46
47 : next-method ( class generic -- method/f )
48     [ next-method-class ] keep method ;
49
50 GENERIC: next-method-quot* ( class generic combination -- quot )
51
52 : next-method-quot ( method -- quot )
53     next-method-quot-cache get [
54         [ "method-class" word-prop ]
55         [
56             "method-generic" word-prop
57             dup "combination" word-prop
58         ] bi next-method-quot*
59     ] cache ;
60
61 ERROR: no-next-method method ;
62
63 : (call-next-method) ( method -- )
64     dup next-method-quot [ call ] [ no-next-method ] ?if ;
65
66 TUPLE: check-method class generic ;
67
68 : check-method ( class generic -- class generic )
69     2dup [ class? ] [ generic? ] bi* and [
70         \ check-method boa throw
71     ] unless ; inline
72
73 : with-methods ( class generic quot -- )
74     [ drop changed-generic ]
75     [ [ "methods" word-prop ] dip call ]
76     [ drop remake-generic drop ]
77     3tri ; inline
78
79 : method-word-name ( class word -- string )
80     [ name>> ] bi@ "=>" glue ;
81
82 PREDICATE: method-body < word
83     "method-generic" word-prop >boolean ;
84
85 M: method-body stack-effect
86     "method-generic" word-prop stack-effect ;
87
88 M: method-body crossref?
89     "forgotten" word-prop not ;
90
91 : method-word-props ( class generic -- assoc )
92     [
93         "method-generic" set
94         "method-class" set
95     ] H{ } make-assoc ;
96
97 : <method> ( class generic -- method )
98     check-method
99     [ method-word-props ] 2keep
100     method-word-name f <word>
101     swap >>props ;
102
103 : with-implementors ( class generic quot -- )
104     [ swap implementors-map get at ] dip call ; inline
105
106 : reveal-method ( method class generic -- )
107     [ [ conjoin ] with-implementors ]
108     [ [ set-at ] with-methods ]
109     2bi ;
110
111 : create-method ( class generic -- method )
112     2dup method dup [ 2nip ] [
113         drop
114         [ <method> dup ] 2keep
115         reveal-method
116         reset-caches
117     ] if ;
118
119 PREDICATE: default-method < word "default" word-prop ;
120
121 M: default-method irrelevant? drop t ;
122
123 : <default-method> ( generic combination -- method )
124     [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
125     [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
126
127 : define-default-method ( generic combination -- )
128     dupd <default-method> "default-method" set-word-prop ;
129
130 ! Definition protocol
131 M: method-spec where
132     dup first2 method [ ] [ second ] ?if where ;
133
134 M: method-spec set-where
135     first2 method set-where ;
136
137 M: method-spec definer
138     first2 method definer ;
139
140 M: method-spec definition
141     first2 method definition ;
142
143 M: method-spec forget*
144     first2 method [ forgotten-definition ] [ forget* ] bi ;
145
146 M: method-spec smart-usage
147     second smart-usage ;
148
149 M: method-body definer
150     drop \ M: \ ; ;
151
152 M: method-body forget*
153     dup "forgotten" word-prop [ drop ] [
154         [
155             dup default-method? [ drop ] [
156                 [
157                     [ "method-class" word-prop ]
158                     [ "method-generic" word-prop ] bi
159                     2dup method
160                 ] keep eq?
161                 [
162                     [ [ delete-at ] with-methods ]
163                     [ [ delete-at ] with-implementors ] 2bi
164                     reset-caches
165                 ] [ 2drop ] if
166             ] if
167         ]
168         [ call-next-method ] bi
169     ] if ;
170
171 M: method-body smart-usage
172     "method-generic" word-prop smart-usage ;
173
174 M: sequence update-methods ( class seq -- )
175     implementors [
176         [ changed-generic ] [ remake-generic drop ] 2bi
177     ] with each ;
178
179 : define-generic ( word combination -- )
180     over "combination" word-prop over = [ drop ] [
181         2dup "combination" set-word-prop
182         over "methods" word-prop values forget-all
183         over H{ } clone "methods" set-word-prop
184         dupd define-default-method
185     ] if remake-generic ;
186
187 M: generic subwords
188     [
189         [ "default-method" word-prop , ]
190         [ "methods" word-prop values % ]
191         [ "engines" word-prop % ]
192         tri
193     ] { } make ;
194
195 M: generic forget*
196     [ subwords forget-all ] [ call-next-method ] bi ;
197
198 : xref-generics ( -- )
199     all-words [ subwords [ xref ] each ] each ;