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 [ 1array ] dip slots>tuple ;
48 dup struct-class? [ '[ _ boa ] ] [ drop f ] if
49 ] 1 define-partial-eval
52 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
53 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
56 : (malloc-struct) ( class -- struct )
57 [ heap-size malloc ] keep memory>struct ; inline
59 : malloc-struct ( class -- struct )
60 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
62 : (struct) ( class -- struct )
63 [ heap-size (byte-array) ] keep memory>struct ; inline
65 : <struct> ( class -- struct )
66 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
68 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
70 [ <wrapper> \ (struct) [ ] 2sequence ]
74 [ [ name>> setter-word 1quotation ] map \ spread ] bi
76 ] [ ] output>sequence ;
79 : pad-struct-slots ( values class -- values' class )
80 [ struct-slots [ initial>> ] map over length tail append ] keep ;
82 : (reader-quot) ( slot -- quot )
83 [ c-type>> c-type-getter-boxer ]
84 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
86 : (writer-quot) ( slot -- quot )
88 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
90 : (boxer-quot) ( class -- quot )
91 '[ _ memory>struct ] ;
93 : (unboxer-quot) ( class -- quot )
97 M: struct-class boa>object
99 [ <struct> ] [ struct-slots ] bi
100 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
102 ! Struct slot accessors
104 GENERIC: struct-slot-values ( struct -- sequence )
106 M: struct-class reader-quot
109 M: struct-class writer-quot
115 : struct-slot-values-quot ( class -- quot )
117 [ name>> reader-word 1quotation ] map
118 \ cleave [ ] 2sequence
119 \ output>array [ ] 2sequence ;
121 : define-inline-method ( class generic quot -- )
122 [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
124 : (define-struct-slot-values-method) ( class -- )
125 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
126 define-inline-method ;
128 : clone-underlying ( struct -- byte-array )
129 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
131 : (define-clone-method) ( class -- )
133 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
134 define-inline-method ;
136 : slot>field ( slot -- field )
137 field-spec new swap {
139 [ offset>> >>offset ]
141 [ name>> reader-word >>reader ]
142 [ name>> writer-word >>writer ]
145 : define-struct-for-class ( class -- )
149 [ "struct-size" word-prop ]
150 [ "struct-align" word-prop ]
151 [ struct-slots [ slot>field ] map ]
153 struct-type (define-struct)
157 [ (unboxer-quot) >>unboxer-quot ]
158 [ (boxer-quot) >>boxer-quot ]
163 : align-offset ( offset class -- offset' )
166 : struct-offsets ( slots -- size )
168 [ c-type>> align-offset ] keep
169 [ (>>offset) ] [ c-type>> heap-size + ] 2bi
172 : union-struct-offsets ( slots -- size )
173 [ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ;
175 : struct-align ( slots -- align )
176 [ c-type>> c-type-align ] [ max ] map-reduce ;
179 M: struct-class c-type
182 M: struct-class c-type-align
183 "struct-align" word-prop ;
185 M: struct-class c-type-getter
186 drop [ swap <displaced-alien> ] ;
188 M: struct-class c-type-setter
189 [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri
190 '[ @ swap @ _ memcpy ] ;
192 M: struct-class c-type-boxer-quot
195 M: struct-class c-type-unboxer-quot
198 M: struct-class heap-size
199 "struct-size" word-prop ;
201 M: struct byte-length
202 class "struct-size" word-prop ; foldable
207 : make-struct-prototype ( class -- prototype )
208 [ heap-size <byte-array> ]
214 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
217 : (struct-methods) ( class -- )
218 [ (define-struct-slot-values-method) ]
219 [ (define-clone-method) ]
222 : (struct-word-props) ( class slots size align -- )
224 [ "struct-slots" set-word-prop ]
225 [ define-accessors ] 2bi
227 [ "struct-size" set-word-prop ]
228 [ "struct-align" set-word-prop ] tri-curry*
230 [ dup make-struct-prototype "prototype" set-word-prop ]
231 [ (struct-methods) ] tri ;
233 : check-struct-slots ( slots -- )
234 [ c-type>> c-type drop ] each ;
236 : redefine-struct-tuple-class ( class -- )
237 [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
239 : (define-struct-class) ( class slots offsets-quot -- )
241 [ struct-must-have-slots ]
242 [ drop redefine-struct-tuple-class ] if-empty
246 [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
249 [ drop define-struct-for-class ] 2tri ; inline
252 : define-struct-class ( class slots -- )
253 [ struct-offsets ] (define-struct-class) ;
255 : define-union-struct-class ( class slots -- )
256 [ union-struct-offsets ] (define-struct-class) ;
258 ERROR: invalid-struct-slot token ;
260 : struct-slot-class ( c-type -- class' )
261 c-type c-type-boxed-class
262 dup \ byte-array = [ drop \ c-ptr ] when ;
264 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
265 [ struct-slot-spec new ] 3dip
267 [ [ >>c-type ] [ struct-slot-class >>class ] bi ]
268 [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
271 : scan-c-type ( -- c-type )
272 scan dup "{" = [ drop \ } parse-until >array ] when ;
274 : parse-struct-slot ( -- slot )
275 scan scan-c-type \ } parse-until <struct-slot-spec> ;
277 : parse-struct-slots ( slots -- slots' more? )
280 { "{" [ parse-struct-slot over push t ] }
281 [ invalid-struct-slot ]
284 : parse-struct-definition ( -- class slots )
285 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
289 parse-struct-definition define-struct-class ;
290 SYNTAX: UNION-STRUCT:
291 parse-struct-definition define-union-struct-class ;
294 scan-word dup struct-slots parse-tuple-literal-slots parsed ;
297 scan-word scan-object swap memory>struct parsed ;
302 : scan-c-type` ( -- c-type/param )
303 scan dup "{" = [ drop \ } parse-until >array ] [ >string-param ] if ;
305 : parse-struct-slot` ( accum -- accum )
306 scan-string-param scan-c-type` \ } parse-until
307 [ <struct-slot-spec> over push ] 3curry over push-all ;
309 : parse-struct-slots` ( accum -- accum more? )
312 { "{" [ parse-struct-slot` t ] }
313 [ invalid-struct-slot ]
317 FUNCTOR-SYNTAX: STRUCT:
319 [ 8 <vector> ] over push-all
320 [ parse-struct-slots` ] [ ] while
321 [ >array define-struct-class ] over push-all ;
323 USING: vocabs vocabs.loader ;
325 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when