]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
Fixing failing unit tests in compiler.tree.propagation due to constraints
[factor.git] / basis / classes / struct / struct.factor
1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.structs
3 alien.structs.fields arrays byte-arrays classes classes.parser
4 classes.tuple classes.tuple.parser classes.tuple.private
5 combinators combinators.short-circuit combinators.smart
6 definitions functors.backend fry generalizations generic.parser
7 kernel kernel.private lexer libc locals macros make math
8 math.order parser quotations sequences slots slots.private
9 specialized-arrays vectors words summary namespaces assocs
10 compiler.tree.propagation.transforms ;
11 FROM: slots => reader-word writer-word ;
12 IN: classes.struct
13
14 SPECIALIZED-ARRAY: uchar
15
16 ERROR: struct-must-have-slots ;
17
18 M: struct-must-have-slots summary
19     drop "Struct definitions must have slots" ;
20
21 TUPLE: struct
22     { (underlying) c-ptr read-only } ;
23
24 TUPLE: struct-slot-spec < slot-spec
25     c-type ;
26
27 PREDICATE: struct-class < tuple-class
28     superclass \ struct eq? ;
29
30 M: struct-class valid-superclass? drop f ;
31
32 GENERIC: struct-slots ( struct-class -- slots )
33
34 M: struct-class struct-slots "struct-slots" word-prop ;
35
36 ! struct allocation
37
38 M: struct >c-ptr
39     2 slot { c-ptr } declare ; inline
40
41 M: struct equal?
42     {
43         [ [ class ] bi@ = ]
44         [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
45     } 2&& ; inline
46
47 M: struct hashcode*
48     [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline    
49
50 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
51
52 : memory>struct ( ptr class -- struct )
53     ! This is sub-optimal if the class is not literal, but gets
54     ! optimized down to efficient code if it is.
55     '[ _ boa ] call( ptr -- struct ) ; inline
56
57 <PRIVATE
58 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
59     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
60 PRIVATE>
61
62 : (malloc-struct) ( class -- struct )
63     [ heap-size malloc ] keep memory>struct ; inline
64
65 : malloc-struct ( class -- struct )
66     [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
67
68 : (struct) ( class -- struct )
69     [ heap-size (byte-array) ] keep memory>struct ; inline
70
71 : <struct> ( class -- struct )
72     [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
73
74 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
75     [
76         [ <wrapper> \ (struct) [ ] 2sequence ]
77         [
78             struct-slots
79             [ length \ ndip ]
80             [ [ name>> setter-word 1quotation ] map \ spread ] bi
81         ] bi
82     ] [ ] output>sequence ;
83
84 <PRIVATE
85 : pad-struct-slots ( values class -- values' class )
86     [ struct-slots [ initial>> ] map over length tail append ] keep ;
87
88 : (reader-quot) ( slot -- quot )
89     [ c-type>> c-type-getter-boxer ]
90     [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
91
92 : (writer-quot) ( slot -- quot )
93     [ c-type>> c-setter ]
94     [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
95
96 : (boxer-quot) ( class -- quot )
97     '[ _ memory>struct ] ;
98
99 : (unboxer-quot) ( class -- quot )
100     drop [ >c-ptr ] ;
101 PRIVATE>
102
103 M: struct-class boa>object
104     swap pad-struct-slots
105     [ <struct> ] [ struct-slots ] bi 
106     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
107
108 ! Struct slot accessors
109
110 GENERIC: struct-slot-values ( struct -- sequence )
111
112 M: struct-class reader-quot
113     nip (reader-quot) ;
114
115 M: struct-class writer-quot
116     nip (writer-quot) ;
117
118 ! c-types
119
120 <PRIVATE
121 : struct-slot-values-quot ( class -- quot )
122     struct-slots
123     [ name>> reader-word 1quotation ] map
124     \ cleave [ ] 2sequence
125     \ output>array [ ] 2sequence ;
126
127 : define-inline-method ( class generic quot -- )
128     [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
129
130 : (define-struct-slot-values-method) ( class -- )
131     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
132     define-inline-method ;
133
134 : clone-underlying ( struct -- byte-array )
135     [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
136
137 : (define-clone-method) ( class -- )
138     [ \ clone ]
139     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
140     define-inline-method ;
141
142 : slot>field ( slot -- field )
143     field-spec new swap {
144         [ name>> >>name ]
145         [ offset>> >>offset ]
146         [ c-type>> >>type ]
147         [ name>> reader-word >>reader ]
148         [ name>> writer-word >>writer ]
149     } cleave ;
150
151 : define-struct-for-class ( class -- )
152     [
153         {
154             [ name>> ]
155             [ "struct-size" word-prop ]
156             [ "struct-align" word-prop ]
157             [ struct-slots [ slot>field ] map ]
158         } cleave
159         struct-type (define-struct)
160     ] [
161         {
162             [ name>> c-type ]
163             [ (unboxer-quot) >>unboxer-quot ]
164             [ (boxer-quot) >>boxer-quot ]
165             [ >>boxed-class ]
166         } cleave drop
167     ] bi ;
168
169 : align-offset ( offset class -- offset' )
170     c-type-align align ;
171
172 : struct-offsets ( slots -- size )
173     0 [
174         [ c-type>> align-offset ] keep
175         [ (>>offset) ] [ c-type>> heap-size + ] 2bi
176     ] reduce ;
177
178 : union-struct-offsets ( slots -- size )
179     [ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ;
180
181 : struct-align ( slots -- align )
182     [ c-type>> c-type-align ] [ max ] map-reduce ;
183 PRIVATE>
184
185 M: struct-class c-type name>> c-type ;
186
187 M: struct-class c-type-align c-type c-type-align ;
188
189 M: struct-class c-type-getter c-type c-type-getter ;
190
191 M: struct-class c-type-setter c-type c-type-setter ;
192
193 M: struct-class c-type-boxer-quot c-type c-type-boxer-quot ;
194
195 M: struct-class c-type-unboxer-quot c-type c-type-boxer-quot ;
196
197 M: struct-class heap-size c-type heap-size ;
198
199 M: struct byte-length class "struct-size" word-prop ; foldable
200
201 ! class definition
202
203 <PRIVATE
204 : make-struct-prototype ( class -- prototype )
205     [ "struct-size" word-prop <byte-array> ]
206     [ memory>struct ]
207     [ struct-slots ] tri
208     [
209         [ initial>> ]
210         [ (writer-quot) ] bi
211         over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
212     ] each ;
213
214 : (struct-methods) ( class -- )
215     [ (define-struct-slot-values-method) ]
216     [ (define-clone-method) ]
217     bi ;
218
219 : (struct-word-props) ( class slots size align -- )
220     [
221         [ "struct-slots" set-word-prop ]
222         [ define-accessors ] 2bi
223     ]
224     [ "struct-size" set-word-prop ]
225     [ "struct-align" set-word-prop ] tri-curry*
226     [ tri ] 3curry
227     [ dup make-struct-prototype "prototype" set-word-prop ]
228     [ (struct-methods) ] tri ;
229
230 : check-struct-slots ( slots -- )
231     [ c-type>> c-type drop ] each ;
232
233 : redefine-struct-tuple-class ( class -- )
234     [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
235
236 : (define-struct-class) ( class slots offsets-quot -- )
237     [ 
238         empty?
239         [ struct-must-have-slots ]
240         [ redefine-struct-tuple-class ] if
241     ]
242     swap '[
243         make-slots dup
244         [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
245         (struct-word-props)
246     ]
247     [ drop define-struct-for-class ] 2tri ; inline
248 PRIVATE>
249
250 : define-struct-class ( class slots -- )
251     [ struct-offsets ] (define-struct-class) ;
252
253 : define-union-struct-class ( class slots -- )
254     [ union-struct-offsets ] (define-struct-class) ;
255
256 M: struct-class reset-class
257     [ call-next-method ] [ name>> c-types get delete-at ] bi ;
258
259 ERROR: invalid-struct-slot token ;
260
261 : struct-slot-class ( c-type -- class' )
262     c-type c-type-boxed-class
263     dup \ byte-array = [ drop \ c-ptr ] when ;
264
265 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
266     [ struct-slot-spec new ] 3dip
267     [ >>name ]
268     [ [ >>c-type ] [ struct-slot-class >>class ] bi ]
269     [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
270
271 <PRIVATE
272 : scan-c-type ( -- c-type )
273     scan dup "{" = [ drop \ } parse-until >array ] when ;
274
275 : parse-struct-slot ( -- slot )
276     scan scan-c-type \ } parse-until <struct-slot-spec> ;
277     
278 : parse-struct-slots ( slots -- slots' more? )
279     scan {
280         { ";" [ f ] }
281         { "{" [ parse-struct-slot over push t ] }
282         { f [ unexpected-eof ] }
283         [ invalid-struct-slot ]
284     } case ;
285
286 : parse-struct-definition ( -- class slots )
287     CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
288 PRIVATE>
289
290 SYNTAX: STRUCT:
291     parse-struct-definition define-struct-class ;
292 SYNTAX: UNION-STRUCT:
293     parse-struct-definition define-union-struct-class ;
294
295 SYNTAX: S{
296     scan-word dup struct-slots parse-tuple-literal-slots parsed ;
297
298 SYNTAX: S@
299     scan-word scan-object swap memory>struct parsed ;
300
301 ! functor support
302
303 <PRIVATE
304 : scan-c-type` ( -- c-type/param )
305     scan dup "{" = [ drop \ } parse-until >array ] [ >string-param ] if ;
306
307 : parse-struct-slot` ( accum -- accum )
308     scan-string-param scan-c-type` \ } parse-until
309     [ <struct-slot-spec> over push ] 3curry over push-all ;
310
311 : parse-struct-slots` ( accum -- accum more? )
312     scan {
313         { ";" [ f ] }
314         { "{" [ parse-struct-slot` t ] }
315         [ invalid-struct-slot ]
316     } case ;
317 PRIVATE>
318
319 FUNCTOR-SYNTAX: STRUCT:
320     scan-param parsed
321     [ 8 <vector> ] over push-all
322     [ parse-struct-slots` ] [ ] while
323     [ >array define-struct-class ] over push-all ;
324
325 USING: vocabs vocabs.loader ;
326
327 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when