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
10 compiler.tree.propagation.transforms ;
11 FROM: slots => reader-word writer-word ;
14 SPECIALIZED-ARRAY: uchar
16 ERROR: struct-must-have-slots ;
19 { (underlying) c-ptr read-only } ;
21 TUPLE: struct-slot-spec < slot-spec
24 PREDICATE: struct-class < tuple-class \ struct subclass-of? ;
26 : struct-slots ( struct-class -- slots )
27 "struct-slots" word-prop ;
32 2 slot { c-ptr } declare ; inline
37 [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
41 [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline
43 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
45 : memory>struct ( ptr class -- struct )
46 ! This is sub-optimal if the class is not literal, but gets
47 ! optimized down to efficient code if it is.
48 '[ _ boa ] call( ptr -- struct ) ; inline
51 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
52 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
55 : (malloc-struct) ( class -- struct )
56 [ heap-size malloc ] keep memory>struct ; inline
58 : malloc-struct ( class -- struct )
59 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
61 : (struct) ( class -- struct )
62 [ heap-size (byte-array) ] keep memory>struct ; inline
64 : <struct> ( class -- struct )
65 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
67 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
69 [ <wrapper> \ (struct) [ ] 2sequence ]
73 [ [ name>> setter-word 1quotation ] map \ spread ] bi
75 ] [ ] output>sequence ;
78 : pad-struct-slots ( values class -- values' class )
79 [ struct-slots [ initial>> ] map over length tail append ] keep ;
81 : (reader-quot) ( slot -- quot )
82 [ c-type>> c-type-getter-boxer ]
83 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
85 : (writer-quot) ( slot -- quot )
87 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
89 : (boxer-quot) ( class -- quot )
90 '[ _ memory>struct ] ;
92 : (unboxer-quot) ( class -- quot )
96 M: struct-class boa>object
98 [ <struct> ] [ struct-slots ] bi
99 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
101 ! Struct slot accessors
103 GENERIC: struct-slot-values ( struct -- sequence )
105 M: struct-class reader-quot
108 M: struct-class writer-quot
114 : struct-slot-values-quot ( class -- quot )
116 [ name>> reader-word 1quotation ] map
117 \ cleave [ ] 2sequence
118 \ output>array [ ] 2sequence ;
120 : define-inline-method ( class generic quot -- )
121 [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
123 : (define-struct-slot-values-method) ( class -- )
124 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
125 define-inline-method ;
127 : clone-underlying ( struct -- byte-array )
128 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
130 : (define-clone-method) ( class -- )
132 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
133 define-inline-method ;
135 : slot>field ( slot -- field )
136 field-spec new swap {
138 [ offset>> >>offset ]
140 [ name>> reader-word >>reader ]
141 [ name>> writer-word >>writer ]
144 : define-struct-for-class ( class -- )
148 [ "struct-size" word-prop ]
149 [ "struct-align" word-prop ]
150 [ struct-slots [ slot>field ] map ]
152 struct-type (define-struct)
156 [ (unboxer-quot) >>unboxer-quot ]
157 [ (boxer-quot) >>boxer-quot ]
162 : align-offset ( offset class -- offset' )
165 : struct-offsets ( slots -- size )
167 [ c-type>> align-offset ] keep
168 [ (>>offset) ] [ c-type>> heap-size + ] 2bi
171 : union-struct-offsets ( slots -- size )
172 [ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ;
174 : struct-align ( slots -- align )
175 [ c-type>> c-type-align ] [ max ] map-reduce ;
178 M: struct-class c-type
181 M: struct-class c-type-align
182 "struct-align" word-prop ;
184 M: struct-class c-type-getter
185 drop [ swap <displaced-alien> ] ;
187 M: struct-class c-type-setter
188 [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri
189 '[ @ swap @ _ memcpy ] ;
191 M: struct-class c-type-boxer-quot
194 M: struct-class c-type-unboxer-quot
197 M: struct-class heap-size
198 "struct-size" word-prop ;
200 M: struct byte-length
201 class "struct-size" word-prop ; foldable
206 : make-struct-prototype ( class -- prototype )
207 [ heap-size <byte-array> ]
213 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
216 : (struct-methods) ( class -- )
217 [ (define-struct-slot-values-method) ]
218 [ (define-clone-method) ]
221 : (struct-word-props) ( class slots size align -- )
223 [ "struct-slots" set-word-prop ]
224 [ define-accessors ] 2bi
226 [ "struct-size" set-word-prop ]
227 [ "struct-align" set-word-prop ] tri-curry*
229 [ dup make-struct-prototype "prototype" set-word-prop ]
230 [ (struct-methods) ] tri ;
232 : check-struct-slots ( slots -- )
233 [ c-type>> c-type drop ] each ;
235 : redefine-struct-tuple-class ( class -- )
236 [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
238 : (define-struct-class) ( class slots offsets-quot -- )
240 [ struct-must-have-slots ]
241 [ drop redefine-struct-tuple-class ] if-empty
245 [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
248 [ drop define-struct-for-class ] 2tri ; inline
251 : define-struct-class ( class slots -- )
252 [ struct-offsets ] (define-struct-class) ;
254 : define-union-struct-class ( class slots -- )
255 [ union-struct-offsets ] (define-struct-class) ;
257 ERROR: invalid-struct-slot token ;
259 : struct-slot-class ( c-type -- class' )
260 c-type c-type-boxed-class
261 dup \ byte-array = [ drop \ c-ptr ] when ;
263 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
264 [ struct-slot-spec new ] 3dip
266 [ [ >>c-type ] [ struct-slot-class >>class ] bi ]
267 [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
270 : scan-c-type ( -- c-type )
271 scan dup "{" = [ drop \ } parse-until >array ] when ;
273 : parse-struct-slot ( -- slot )
274 scan scan-c-type \ } parse-until <struct-slot-spec> ;
276 : parse-struct-slots ( slots -- slots' more? )
279 { "{" [ parse-struct-slot over push t ] }
280 [ invalid-struct-slot ]
283 : parse-struct-definition ( -- class slots )
284 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
288 parse-struct-definition define-struct-class ;
289 SYNTAX: UNION-STRUCT:
290 parse-struct-definition define-union-struct-class ;
293 scan-word dup struct-slots parse-tuple-literal-slots parsed ;
296 scan-word scan-object swap memory>struct parsed ;
301 : scan-c-type` ( -- c-type/param )
302 scan dup "{" = [ drop \ } parse-until >array ] [ >string-param ] if ;
304 : parse-struct-slot` ( accum -- accum )
305 scan-string-param scan-c-type` \ } parse-until
306 [ <struct-slot-spec> over push ] 3curry over push-all ;
308 : parse-struct-slots` ( accum -- accum more? )
311 { "{" [ parse-struct-slot` t ] }
312 [ invalid-struct-slot ]
316 FUNCTOR-SYNTAX: STRUCT:
318 [ 8 <vector> ] over push-all
319 [ parse-struct-slots` ] [ ] while
320 [ >array define-struct-class ] over push-all ;
322 USING: vocabs vocabs.loader ;
324 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when