]> gitweb.factorcode.org Git - factor.git/blob - core/generic/generic.factor
Builtinn types now use new slot accessors; tuple slot type declaration work in progress
[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 assocs
4 hashtables definitions kernel.private classes classes.private
5 classes.algebra quotations arrays vocabs effects combinators
6 sets ;
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 : method ( class generic -- method/f )
25     "methods" word-prop at ;
26
27 PREDICATE: method-spec < pair
28     first2 generic? swap class? and ;
29
30 : order ( generic -- seq )
31     "methods" word-prop keys sort-classes ;
32
33 : specific-method ( class word -- class )
34     order min-class ;
35
36 GENERIC: effective-method ( ... generic -- method )
37
38 : next-method-class ( class generic -- class/f )
39     order [ class<= ] with filter reverse dup length 1 =
40     [ drop f ] [ second ] if ;
41
42 : next-method ( class generic -- class/f )
43     [ next-method-class ] keep method ;
44
45 GENERIC: next-method-quot* ( class generic -- quot )
46
47 : next-method-quot ( class generic -- quot )
48     dup "combination" word-prop next-method-quot* ;
49
50 : (call-next-method) ( class generic -- )
51     next-method-quot call ;
52
53 TUPLE: check-method class generic ;
54
55 : check-method ( class generic -- class generic )
56     over class? over generic? and [
57         \ check-method boa throw
58     ] unless ; inline
59
60 : affected-methods ( class generic -- seq )
61     "methods" word-prop swap
62     [ nip [ classes-intersect? ] [ class<= ] 2bi or ] curry assoc-filter
63     values ;
64
65 : update-generic ( class generic -- )
66     affected-methods [ +called+ changed-definition ] each ;
67
68 : with-methods ( class generic quot -- )
69     [ drop update-generic ]
70     [ [ "methods" word-prop ] dip call ]
71     [ drop make-generic drop ]
72     3tri ; inline
73
74 : method-word-name ( class word -- string )
75     [ name>> ] bi@ "=>" swap 3append ;
76
77 PREDICATE: method-body < word
78     "method-generic" word-prop >boolean ;
79
80 M: method-body stack-effect
81     "method-generic" word-prop stack-effect ;
82
83 M: method-body crossref?
84     "forgotten" word-prop not ;
85
86 : method-word-props ( class generic -- assoc )
87     [
88         "method-generic" set
89         "method-class" set
90     ] H{ } make-assoc ;
91
92 : <method> ( class generic -- method )
93     check-method
94     [ method-word-props ] 2keep
95     method-word-name f <word>
96     swap >>props ;
97
98 : with-implementors ( class generic quot -- )
99     [ swap implementors-map get at ] dip call ; inline
100
101 : reveal-method ( method class generic -- )
102     [ [ conjoin ] with-implementors ]
103     [ [ set-at ] with-methods ]
104     2bi ;
105
106 : create-method ( class generic -- method )
107     2dup method dup [
108         2nip
109     ] [
110         drop [ <method> dup ] 2keep reveal-method
111     ] if ;
112
113 : <default-method> ( generic combination -- method )
114     [ drop object bootstrap-word swap <method> ] [ make-default-method ] 2bi
115     [ define ] [ drop t "default" set-word-prop ] [ drop ] 2tri ;
116
117 : define-default-method ( generic combination -- )
118     dupd <default-method> "default-method" set-word-prop ;
119
120 ! Definition protocol
121 M: method-spec where
122     dup first2 method [ ] [ second ] ?if where ;
123
124 M: method-spec set-where
125     first2 method set-where ;
126
127 M: method-spec definer
128     first2 method definer ;
129
130 M: method-spec definition
131     first2 method definition ;
132
133 M: method-spec forget*
134     first2 method forget* ;
135
136 M: method-spec smart-usage
137     second smart-usage ;
138
139 M: method-body definer
140     drop \ M: \ ; ;
141
142 M: method-body forget*
143     dup "forgotten" word-prop [ drop ] [
144         [
145             dup "default" word-prop [ drop ] [
146                 [
147                     [ "method-class" word-prop ]
148                     [ "method-generic" word-prop ] bi
149                     2dup method
150                 ] keep eq?
151                 [
152                     [ [ delete-at ] with-methods ]
153                     [ [ delete-at ] with-implementors ]
154                     2bi
155                 ] [ 2drop ] if
156             ] if
157         ]
158         [ call-next-method ] bi
159     ] if ;
160
161 M: method-body smart-usage
162     "method-generic" word-prop smart-usage ;
163
164 M: sequence update-methods ( class seq -- )
165     implementors [
166         [ update-generic ] [ make-generic drop ] 2bi
167     ] with each ;
168
169 : define-generic ( word combination -- )
170     over "combination" word-prop over = [
171         2drop
172     ] [
173         2dup "combination" set-word-prop
174         over "methods" word-prop values forget-all
175         over H{ } clone "methods" set-word-prop
176         dupd define-default-method
177         make-generic
178     ] if ;
179
180 M: generic subwords
181     [
182         [ "default-method" word-prop , ]
183         [ "methods" word-prop values % ]
184         [ "engines" word-prop % ]
185         tri
186     ] { } make ;
187
188 M: generic forget*
189     [ subwords forget-all ] [ call-next-method ] bi ;
190
191 : xref-generics ( -- )
192     all-words [ subwords [ xref ] each ] each ;