1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.parser arrays
3 byte-arrays classes classes.parser classes.tuple classes.tuple.parser
4 classes.tuple.private combinators combinators.short-circuit
5 combinators.smart cpu.architecture definitions functors.backend
6 fry generalizations generic.parser kernel kernel.private lexer
7 libc locals macros make math math.order parser quotations
8 sequences slots slots.private specialized-arrays vectors words
9 summary namespaces assocs vocabs.parser ;
12 SPECIALIZED-ARRAY: uchar
14 ERROR: struct-must-have-slots ;
16 M: struct-must-have-slots summary
17 drop "Struct definitions must have slots" ;
20 { (underlying) c-ptr read-only } ;
22 TUPLE: struct-slot-spec < slot-spec
25 PREDICATE: struct-class < tuple-class
26 superclass \ struct eq? ;
28 M: struct-class valid-superclass? drop f ;
30 GENERIC: struct-slots ( struct-class -- slots )
32 M: struct-class struct-slots "struct-slots" word-prop ;
37 2 slot { c-ptr } declare ; inline
42 [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
46 [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline
48 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
50 : memory>struct ( ptr class -- struct )
51 ! This is sub-optimal if the class is not literal, but gets
52 ! optimized down to efficient code if it is.
53 '[ _ boa ] call( ptr -- struct ) ; inline
56 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
57 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
60 : (malloc-struct) ( class -- struct )
61 [ heap-size malloc ] keep memory>struct ; inline
63 : malloc-struct ( class -- struct )
64 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
66 : (struct) ( class -- struct )
67 [ heap-size (byte-array) ] keep memory>struct ; inline
69 : <struct> ( class -- struct )
70 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
72 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
74 [ <wrapper> \ (struct) [ ] 2sequence ]
78 [ [ name>> setter-word 1quotation ] map \ spread ] bi
80 ] [ ] output>sequence ;
83 : pad-struct-slots ( values class -- values' class )
84 [ struct-slots [ initial>> ] map over length tail append ] keep ;
86 : (reader-quot) ( slot -- quot )
87 [ type>> c-type-getter-boxer ]
88 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
90 : (writer-quot) ( slot -- quot )
92 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
94 : (boxer-quot) ( class -- quot )
95 '[ _ memory>struct ] ;
97 : (unboxer-quot) ( class -- quot )
101 M: struct-class boa>object
102 swap pad-struct-slots
103 [ <struct> ] [ struct-slots ] bi
104 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
106 ! Struct slot accessors
108 GENERIC: struct-slot-values ( struct -- sequence )
110 M: struct-class reader-quot
113 M: struct-class writer-quot
118 TUPLE: struct-c-type < abstract-c-type
120 return-in-registers? ;
122 INSTANCE: struct-c-type value-type
124 M: struct-c-type c-type ;
126 M: struct-c-type c-type-stack-align? drop f ;
128 : if-value-struct ( ctype true false -- )
129 [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
131 M: struct-c-type unbox-parameter
132 [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
134 M: struct-c-type box-parameter
135 [ %box-large-struct ] [ box-parameter ] if-value-struct ;
137 : if-small-struct ( c-type true false -- ? )
138 [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
140 M: struct-c-type unbox-return
141 [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
143 M: struct-c-type box-return
144 [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
146 M: struct-c-type stack-size
147 [ heap-size ] [ stack-size ] if-value-struct ;
149 M: struct-c-type c-struct? drop t ;
152 : struct-slot-values-quot ( class -- quot )
154 [ name>> reader-word 1quotation ] map
155 \ cleave [ ] 2sequence
156 \ output>array [ ] 2sequence ;
158 : define-inline-method ( class generic quot -- )
159 [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
161 : (define-struct-slot-values-method) ( class -- )
162 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
163 define-inline-method ;
165 : clone-underlying ( struct -- byte-array )
166 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
168 : (define-clone-method) ( class -- )
170 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
171 define-inline-method ;
173 : c-type-for-class ( class -- c-type )
174 struct-c-type new swap {
175 [ drop byte-array >>class ]
177 [ struct-slots >>fields ]
178 [ "struct-size" word-prop >>size ]
179 [ "struct-align" word-prop >>align ]
180 [ (unboxer-quot) >>unboxer-quot ]
181 [ (boxer-quot) >>boxer-quot ]
184 : align-offset ( offset class -- offset' )
187 : struct-offsets ( slots -- size )
189 [ type>> align-offset ] keep
190 [ (>>offset) ] [ type>> heap-size + ] 2bi
193 : union-struct-offsets ( slots -- size )
194 [ 0 >>offset type>> heap-size ] [ max ] map-reduce ;
196 : struct-align ( slots -- align )
197 [ type>> c-type-align ] [ max ] map-reduce ;
200 M: struct byte-length class "struct-size" word-prop ; foldable
205 : make-struct-prototype ( class -- prototype )
206 [ "struct-size" word-prop <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 [ 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 -- )
240 [ struct-must-have-slots ]
241 [ redefine-struct-tuple-class ] if
245 [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
248 [ drop [ c-type-for-class ] keep typedef ] 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 M: struct-class reset-class
258 [ call-next-method ] [ name>> c-types get delete-at ] bi ;
260 ERROR: invalid-struct-slot token ;
262 : struct-slot-class ( c-type -- class' )
263 c-type c-type-boxed-class
264 dup \ byte-array = [ drop \ c-ptr ] when ;
266 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
267 [ struct-slot-spec new ] 3dip
269 [ [ >>type ] [ struct-slot-class >>class ] bi ]
270 [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
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 { f [ unexpected-eof ] }
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 ] [ search ] 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