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 ;
14 SPECIALIZED-ARRAY: uchar
16 ERROR: struct-must-have-slots ;
18 M: struct-must-have-slots summary
19 drop "Struct definitions must have slots" ;
22 { (underlying) c-ptr read-only } ;
24 TUPLE: struct-slot-spec < slot-spec
27 PREDICATE: struct-class < tuple-class
28 superclass \ struct eq? ;
30 M: struct-class valid-superclass? drop f ;
32 GENERIC: struct-slots ( struct-class -- slots )
34 M: struct-class struct-slots "struct-slots" word-prop ;
39 2 slot { c-ptr } declare ; inline
44 [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
48 [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline
50 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
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
58 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
59 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
62 : (malloc-struct) ( class -- struct )
63 [ heap-size malloc ] keep memory>struct ; inline
65 : malloc-struct ( class -- struct )
66 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
68 : (struct) ( class -- struct )
69 [ heap-size (byte-array) ] keep memory>struct ; inline
71 : <struct> ( class -- struct )
72 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
74 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
76 [ <wrapper> \ (struct) [ ] 2sequence ]
80 [ [ name>> setter-word 1quotation ] map \ spread ] bi
82 ] [ ] output>sequence ;
85 : pad-struct-slots ( values class -- values' class )
86 [ struct-slots [ initial>> ] map over length tail append ] keep ;
88 : (reader-quot) ( slot -- quot )
89 [ c-type>> c-type-getter-boxer ]
90 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
92 : (writer-quot) ( slot -- quot )
94 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
96 : (boxer-quot) ( class -- quot )
97 '[ _ memory>struct ] ;
99 : (unboxer-quot) ( class -- quot )
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 ;
108 ! Struct slot accessors
110 GENERIC: struct-slot-values ( struct -- sequence )
112 M: struct-class reader-quot
115 M: struct-class writer-quot
121 : struct-slot-values-quot ( class -- quot )
123 [ name>> reader-word 1quotation ] map
124 \ cleave [ ] 2sequence
125 \ output>array [ ] 2sequence ;
127 : define-inline-method ( class generic quot -- )
128 [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
130 : (define-struct-slot-values-method) ( class -- )
131 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
132 define-inline-method ;
134 : clone-underlying ( struct -- byte-array )
135 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
137 : (define-clone-method) ( class -- )
139 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
140 define-inline-method ;
142 : slot>field ( slot -- field )
143 field-spec new swap {
145 [ offset>> >>offset ]
147 [ name>> reader-word >>reader ]
148 [ name>> writer-word >>writer ]
151 : define-struct-for-class ( class -- )
155 [ "struct-size" word-prop ]
156 [ "struct-align" word-prop ]
157 [ struct-slots [ slot>field ] map ]
159 struct-type (define-struct)
163 [ (unboxer-quot) >>unboxer-quot ]
164 [ (boxer-quot) >>boxer-quot ]
169 : align-offset ( offset class -- offset' )
172 : struct-offsets ( slots -- size )
174 [ c-type>> align-offset ] keep
175 [ (>>offset) ] [ c-type>> heap-size + ] 2bi
178 : union-struct-offsets ( slots -- size )
179 [ 0 >>offset c-type>> heap-size ] [ max ] map-reduce ;
181 : struct-align ( slots -- align )
182 [ c-type>> c-type-align ] [ max ] map-reduce ;
185 M: struct-class c-type name>> c-type ;
187 M: struct-class c-type-align c-type c-type-align ;
189 M: struct-class c-type-getter c-type c-type-getter ;
191 M: struct-class c-type-setter c-type c-type-setter ;
193 M: struct-class c-type-boxer-quot c-type c-type-boxer-quot ;
195 M: struct-class c-type-unboxer-quot c-type c-type-boxer-quot ;
197 M: struct-class heap-size c-type heap-size ;
199 M: struct byte-length class "struct-size" word-prop ; foldable
204 : make-struct-prototype ( class -- prototype )
205 [ "struct-size" word-prop <byte-array> ]
211 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
214 : (struct-methods) ( class -- )
215 [ (define-struct-slot-values-method) ]
216 [ (define-clone-method) ]
219 : (struct-word-props) ( class slots size align -- )
221 [ "struct-slots" set-word-prop ]
222 [ define-accessors ] 2bi
224 [ "struct-size" set-word-prop ]
225 [ "struct-align" set-word-prop ] tri-curry*
227 [ dup make-struct-prototype "prototype" set-word-prop ]
228 [ (struct-methods) ] tri ;
230 : check-struct-slots ( slots -- )
231 [ c-type>> c-type drop ] each ;
233 : redefine-struct-tuple-class ( class -- )
234 [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
236 : (define-struct-class) ( class slots offsets-quot -- )
239 [ struct-must-have-slots ]
240 [ redefine-struct-tuple-class ] if
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 M: struct-class reset-class
257 [ call-next-method ] [ name>> c-types get delete-at ] bi ;
259 ERROR: invalid-struct-slot token ;
261 : struct-slot-class ( c-type -- class' )
262 c-type c-type-boxed-class
263 dup \ byte-array = [ drop \ c-ptr ] when ;
265 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
266 [ struct-slot-spec new ] 3dip
268 [ [ >>c-type ] [ struct-slot-class >>class ] bi ]
269 [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
272 : scan-c-type ( -- c-type )
273 scan dup "{" = [ drop \ } parse-until >array ] when ;
275 : parse-struct-slot ( -- slot )
276 scan scan-c-type \ } parse-until <struct-slot-spec> ;
278 : parse-struct-slots ( slots -- slots' more? )
281 { "{" [ parse-struct-slot over push t ] }
282 { f [ unexpected-eof ] }
283 [ invalid-struct-slot ]
286 : parse-struct-definition ( -- class slots )
287 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
291 parse-struct-definition define-struct-class ;
292 SYNTAX: UNION-STRUCT:
293 parse-struct-definition define-union-struct-class ;
296 scan-word dup struct-slots parse-tuple-literal-slots parsed ;
299 scan-word scan-object swap memory>struct parsed ;
304 : scan-c-type` ( -- c-type/param )
305 scan dup "{" = [ drop \ } parse-until >array ] [ >string-param ] if ;
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 ;
311 : parse-struct-slots` ( accum -- accum more? )
314 { "{" [ parse-struct-slot` t ] }
315 [ invalid-struct-slot ]
319 FUNCTOR-SYNTAX: STRUCT:
321 [ 8 <vector> ] over push-all
322 [ parse-struct-slots` ] [ ] while
323 [ >array define-struct-class ] over push-all ;
325 USING: vocabs vocabs.loader ;
327 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when