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 math.order
8 parser quotations sequences slots slots.private struct-arrays vectors
9 words compiler.tree.propagation.transforms specialized-arrays.uchar ;
10 FROM: slots => reader-word writer-word ;
15 ERROR: struct-must-have-slots ;
18 { (underlying) c-ptr read-only } ;
20 TUPLE: struct-slot-spec < slot-spec
23 PREDICATE: struct-class < tuple-class \ struct subclass-of? ;
25 : struct-slots ( struct-class -- slots )
26 "struct-slots" word-prop ;
31 2 slot { c-ptr } declare ; inline
36 [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
40 [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline
42 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
44 : memory>struct ( ptr class -- struct )
45 ! This is sub-optimal if the class is not literal, but gets
46 ! optimized down to efficient code if it is.
47 '[ _ boa ] call( ptr -- struct ) ; inline
50 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
51 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
54 : (malloc-struct) ( class -- struct )
55 [ heap-size malloc ] keep memory>struct ; inline
57 : malloc-struct ( class -- struct )
58 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
60 : (struct) ( class -- struct )
61 [ heap-size (byte-array) ] keep memory>struct ; inline
63 : <struct> ( class -- struct )
64 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
66 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
68 [ <wrapper> \ (struct) [ ] 2sequence ]
72 [ [ name>> setter-word 1quotation ] map \ spread ] bi
74 ] [ ] output>sequence ;
77 : pad-struct-slots ( values class -- values' class )
78 [ struct-slots [ initial>> ] map over length tail append ] keep ;
80 : (reader-quot) ( slot -- quot )
81 [ c-type>> c-type-getter-boxer ]
82 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
84 : (writer-quot) ( slot -- quot )
86 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
88 : (boxer-quot) ( class -- quot )
89 '[ _ memory>struct ] ;
91 : (unboxer-quot) ( class -- quot )
95 M: struct-class boa>object
97 [ <struct> ] [ struct-slots ] bi
98 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
100 ! Struct slot accessors
102 GENERIC: struct-slot-values ( struct -- sequence )
104 M: struct-class reader-quot
107 M: struct-class writer-quot
113 : struct-slot-values-quot ( class -- quot )
115 [ name>> reader-word 1quotation ] map
116 \ cleave [ ] 2sequence
117 \ output>array [ ] 2sequence ;
119 : define-inline-method ( class generic quot -- )
120 [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
122 : (define-struct-slot-values-method) ( class -- )
123 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
124 define-inline-method ;
126 : clone-underlying ( struct -- byte-array )
127 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
129 : (define-clone-method) ( class -- )
131 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
132 define-inline-method ;
134 : slot>field ( slot -- field )
135 field-spec new swap {
137 [ offset>> >>offset ]
139 [ name>> reader-word >>reader ]
140 [ name>> writer-word >>writer ]
143 : define-struct-for-class ( class -- )
147 [ "struct-size" word-prop ]
148 [ "struct-align" word-prop ]
149 [ struct-slots [ slot>field ] map ]
151 struct-type (define-struct)
155 [ (unboxer-quot) >>unboxer-quot ]
156 [ (boxer-quot) >>boxer-quot ]
161 : align-offset ( offset class -- offset' )
164 : struct-offsets ( slots -- size )
166 [ c-type>> align-offset ] keep
167 [ (>>offset) ] [ c-type>> heap-size + ] 2bi
170 : union-struct-offsets ( slots -- size )
171 [ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ;
173 : struct-align ( slots -- align )
174 [ c-type>> c-type-align ] [ max ] map-reduce ;
177 M: struct-class c-type
180 M: struct-class c-type-align
181 "struct-align" word-prop ;
183 M: struct-class c-type-getter
184 drop [ swap <displaced-alien> ] ;
186 M: struct-class c-type-setter
187 [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri
188 '[ @ swap @ _ memcpy ] ;
190 M: struct-class c-type-boxer-quot
193 M: struct-class c-type-unboxer-quot
196 M: struct-class heap-size
197 "struct-size" word-prop ;
199 M: struct byte-length
200 class "struct-size" word-prop ; foldable
205 : make-struct-prototype ( class -- prototype )
206 [ heap-size <byte-array> ]
212 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
215 : (struct-methods) ( class -- )
216 [ (define-struct-slot-values-method) ]
217 [ (define-clone-method) ]
220 : (struct-word-props) ( class slots size align -- )
222 [ "struct-slots" set-word-prop ]
223 [ define-accessors ] 2bi
225 [ "struct-size" set-word-prop ]
226 [ "struct-align" set-word-prop ] tri-curry*
228 [ dup make-struct-prototype "prototype" set-word-prop ]
229 [ (struct-methods) ] tri ;
231 : check-struct-slots ( slots -- )
232 [ c-type>> c-type drop ] each ;
234 : redefine-struct-tuple-class ( class -- )
235 [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
237 : (define-struct-class) ( class slots offsets-quot -- )
239 [ struct-must-have-slots ]
240 [ drop redefine-struct-tuple-class ] if-empty
244 [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
247 [ drop define-struct-for-class ] 2tri ; inline
250 : define-struct-class ( class slots -- )
251 [ struct-offsets ] (define-struct-class) ;
253 : define-union-struct-class ( class slots -- )
254 [ union-struct-offsets ] (define-struct-class) ;
256 ERROR: invalid-struct-slot token ;
258 : struct-slot-class ( c-type -- class' )
259 c-type c-type-boxed-class
260 dup \ byte-array = [ drop \ c-ptr ] when ;
262 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
263 [ struct-slot-spec new ] 3dip
265 [ [ >>c-type ] [ struct-slot-class >>class ] bi ]
266 [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
269 : scan-c-type ( -- c-type )
270 scan dup "{" = [ drop \ } parse-until >array ] when ;
272 : parse-struct-slot ( -- slot )
273 scan scan-c-type \ } parse-until <struct-slot-spec> ;
275 : parse-struct-slots ( slots -- slots' more? )
278 { "{" [ parse-struct-slot over push t ] }
279 [ invalid-struct-slot ]
282 : parse-struct-definition ( -- class slots )
283 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
287 parse-struct-definition define-struct-class ;
288 SYNTAX: UNION-STRUCT:
289 parse-struct-definition define-union-struct-class ;
292 scan-word dup struct-slots parse-tuple-literal-slots parsed ;
295 scan-word scan-object swap memory>struct parsed ;
300 : scan-c-type` ( -- c-type/param )
301 scan dup "{" = [ drop \ } parse-until >array ] [ >string-param ] if ;
303 : parse-struct-slot` ( accum -- accum )
304 scan-string-param scan-c-type` \ } parse-until
305 [ <struct-slot-spec> over push ] 3curry over push-all ;
307 : parse-struct-slots` ( accum -- accum more? )
310 { "{" [ parse-struct-slot` t ] }
311 [ invalid-struct-slot ]
315 FUNCTOR-SYNTAX: STRUCT:
317 [ 8 <vector> ] over push-all
318 [ parse-struct-slots` ] [ ] while
319 [ >array define-struct-class ] over push-all ;
321 USING: vocabs vocabs.loader ;
323 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when